Recent

Author Topic: pi PICO controlling ws2812b leds, ledstrips  (Read 5951 times)

jb007

  • Full Member
  • ***
  • Posts: 145
pi PICO controlling ws2812b leds, ledstrips
« on: August 03, 2022, 10:16:13 pm »
Hi, recently started coding pi PICO using fpcLaz!

A lots of tnx to Michael Ring for his example programms!

After understanding the PICO's Software IO, I started to code for ws2812b.

I managed to get the timing right and things are going nice!
( allready had this done on Arduino, using pascal: https://www.youtube.com/playlist?list=PLcCKmeWXkrzSm4-kT055wc9f1ITOjBx16 )

When the code is kinda finished and clear, I will post it if wanted, here some video's of ws2812b led's/bars on my PICO:
https://www.youtube.com/playlist?list=PLcCKmeWXkrzRoul43A6f0xf8baJPggiu0


Kind regards. Marcel





to DIY or not to DIY

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #1 on: August 03, 2022, 10:51:58 pm »
What technique are you using: bitbanging or PIO state machine?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jb007

  • Full Member
  • ***
  • Posts: 145
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #2 on: August 04, 2022, 03:00:29 pm »
Oops...first had to google for the meaning 'bitbanging'... :)

So...Yes, my code is bitbanging!
Wasn't aware of this term, just started coding with ( in this case ) the single NRZ protocol AND my scope next to me.


BUT...to get the 'electrical One or Zero I'm using:
( in this case I have ledbars, each 8xRGB, so 192 bit )


procedure writeLogicOne(pinNr:byte);
var
  byCntr: byte;
begin
  sio.gpio_set := 1 shl pinNr;  //   800
  for byCntr:= 0 to 4 do;            // = 720
  sio.gpio_clr := 1 shl pinNr;  //   400
  for byCntr:= 0 to 1 do;            // = 420

end; // p\writeLogicOne


procedure writeLogicZero(pinNr: byte);
var
  byCntr: byte;
begin
  sio.gpio_set := 1 shl pinNr;  //   400
  for byCntr:= 0 to 1 do;            // = 320
  sio.gpio_clr := 1 shl pinNr;  //   800
  for byCntr:= 0 to 4 do;            // = 820

end; // p\writeLogicZero       


procedure doLEDwrite(pinNR: byte);
var
  byCntrBIT: byte;
begin
  for byCntrBIT:= 0 to 191 do
    begin
      if (lightBL[byCntrBIT] = true ) then writeLogicOne(pinLightBR)
      else writeLogicZero(pinLightBR);
    end;

  for byCntrBIT:= 0 to 191 do
    begin
      if (lightBL[byCntrBIT] = true ) then writeLogicOne(pinlightBL)
      else writeLogicZero(pinLightBL);
    end;

end; // p\





BE AWARE of the fOLLOWING:

The timing is NOT ONLY determed by the for byCntr:= 0 to 1
and for byCntr:= 0 to 4 do; but by the whole 'process' above'.

This 'proces' is time-sollid at itself!

Sure, there are multiple ways leading to Rome... ;D


As written before, just jumped into 'pascal for pico' and this
way of setting/resetting an output pin is new for me.
I'm used to this: portA.4:= 1 (or 0 )  instead of sio.gpio_set/clr 'thing'


I'm using this array method, cause between ( refreshrate ) writing a bunch of ws2812b, there's is plenty of time
to calcutale/determen and placing the 24bits for each led in the array.
Multiple led arrays can be used, if eachr strip/bar has difference colourvariations/patterns.

To get an idea:
f_bit = 800kHz 
T_bit = 1.25us
T_led = 24 x 1.25 = 30us
f_led = 1/30us = 33k33Hz

When suggestions ( assembly? ) about setting resetting output bit, please commecnt!

Kind regards.

 






« Last Edit: August 04, 2022, 03:03:46 pm by jb007 »
to DIY or not to DIY

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #3 on: August 04, 2022, 03:29:33 pm »
Nice work!

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #4 on: August 04, 2022, 03:49:33 pm »
Well done :-)

Now obviously there's been a lot of experience built up around these devices, largely based on the Arduino. The dominant technique is bitbanging using e.g. https://github.com/FastLED/FastLED , but some people have also experimented using the SPI bus... I forget the precise detail, but I think it was something like sending three consecutive bytes at double-clock-speed to get the appropriate mark/space ratio.

/However/, since a strip of these LEDs is intolerant of glitches it effectively meant that the processor would still have to give the transfer its entire attention, since the IRQ etc. latency was quite simply unacceptable.

There's a fair amount of interest in the idea of using the RP2040's PIO hardware to generate the pulsetrain to drive an LED string: see https://dubinko.info/blog/2021/09/raspberry-pi-neopixel-code-wrong/ as a particular example.

I've not looked at this yet using C++, and I don't think that anybody's looked at it using Pascal... in fact I'm not sure anybody's looked at supporting the PIO hardware (using pioasm source) from Pascal yet. So if you're looking for a challenge :-)

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MiR

  • Full Member
  • ***
  • Posts: 246
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #5 on: August 04, 2022, 05:59:42 pm »
Pio Hardware is the way to go, however I never played with this part of pico.

The unit for integration is already in the source tree:

https://github.com/michael-ring/pico-fpcexamples/blob/main/units/pico_pio_c.pas

but untested.

With APA106 using SPI is pretty straightforward, ws2812b is another story….

Example for APA106 is here (pascal code, but not for pico)

https://github.com/michael-ring/mbf/blob/master/Samples/APA106Demo/APA106Demo.lpr

jb007

  • Full Member
  • ***
  • Posts: 145
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #6 on: August 04, 2022, 08:45:42 pm »
Tnx for all the replies.

Sure, I started ( and learning fpc for PICo ) with the Micheal Ring's examples.

For the ws2812b I dove into that pico_gpio_c file

There's is this gpio_put procedure.

      procedure gpio_put(gpio:TPinIdentifier; value:boolean);
      begin
        if value = true then
          gpio_set_mask(1 shl gpio)
        else
          gpio_clr_mask(1 shl gpio);
      end;   

               
                this is calling


         procedure gpio_set_mask(mask:longWord);
         begin
           sio.gpio_set := mask;
         end;     

                 or
           
         procedure gpio_clr_mask(mask:longWord);
         begin
           sio.gpio_clr := mask;
         end;   


For the ws2812b on PCIO all this calling takes to long. ( +/- 150ns )

So, I shortened it to this:

procedure writeLogicOne(pinNr:byte);
var
  byCntr: byte;
begin
  sio.gpio_set := 1 shl pinNr;  //   800
  for byCntr:= 0 to 4 do;            // = 720
  sio.gpio_clr := 1 shl pinNr;  //   400
  for byCntr:= 0 to 1 do;            // = 420

end; // p\writeLogicOne

AND


procedure writeLogicZero(pinNr: byte);
var
  byCntr: byte;
begin
  sio.gpio_set := 1 shl pinNr;  //   400
  for byCntr:= 0 to 1 do;            // = 320
  sio.gpio_clr := 1 shl pinNr;  //   800
  for byCntr:= 0 to 4 do;            // = 820

end; // p\writeLogicZero   





(1)                       if (lightBL[byCntrBIT] = true )
(2)                         then writeLogicOne(pinLightBR)
(3)                            else writeLogicZero(pinLightBR);


But still, this can also be shortenend when placing the code from procedures writeLogicOne and writeLogicZero in the 3 lines above!

Gonna play whit that, and conenct my scope and see the exact timing values!

To bee continued!

Kind regards. Marcel










« Last Edit: August 04, 2022, 08:51:13 pm by jb007 »
to DIY or not to DIY

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #7 on: August 04, 2022, 08:48:36 pm »
Note that PIO <> GPIO, it's a functional block with its own state machine etc.

Michael, is there an example for the Pascal unit you highlighted earlier?

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MiR

  • Full Member
  • ***
  • Posts: 246
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #8 on: August 04, 2022, 09:08:40 pm »
Unfortunately there is no example but when I remember correctly everything is there that is needed. I think one has to upload the sequence that pico should run and then trigger this small ‚program‘ and that‘s it.

Only issue was that you have to compile the sequence but you may be able to steal that compiled code from the pico SDK example for ws2812…

Michael

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #9 on: August 04, 2022, 09:52:36 pm »
Only issue was that you have to compile the sequence but you may be able to steal that compiled code from the pico SDK example for ws2812…

At present that's quite a biggy. My understanding is that the new (~FPC 3.3) attribute specification can be used for target-specific things like specifying linkage sections (as well as its nominal role for decorating types), but I've not seen any suggestion that this can be generalised to do things like invoking pioasm.

I've previously raised my head above the parapet and risked suggesting that FPC really could do with an improved frontend/macro capability that would allow it to e.g. embed SQL (or in the current case source for the PIO state machine), and was given to understand that the core team were dead against that sort of thing (I'm British. We're into understatement. OK?).

I appreciate that this is "argumentum ab auctoritate", but I've been knocking around for long enough and seen enough languages and development systems come and go that my experience suggests that the core team is wrong. At the same time, I recognise that the project isn't a democracy and that if I feel strongly about it I should nail my colours to the mast and start a new fork. But I've been knocking around for long enough that I feel I don't have the years to do it justice, or anything like the acuity I once had.

So, what are we left with? I still feel that it would be desirable to be able to define the PIO state machine as close as possible to the Pascal that will interface with it. I'm reminded of one or other of the mainframe systems programming languages which allowed machine-level opcodes to be inserted in the instruction sequence by having a predefined function for each, it might /possibly/ be feasible to do something like that for pioasm in conjunction with backend-specific attributes.

However much I loathe Python, I have to admire the fact that the language can cope with this sort of thing without missing a beat https://github.com/raspberrypi/pico-micropython-examples/blob/master/pio/pio_ws2812.py

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

MiR

  • Full Member
  • ***
  • Posts: 246
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #10 on: August 05, 2022, 10:15:22 am »
I had a quick look at the example from the pico sdk, things may not be that difficult after all because the header of the pio example includes the 'compiled' program:

Code: C  [Select][+][-]
  1. #define ws2812_wrap_target 0
  2. #define ws2812_wrap 3
  3.  
  4. #define ws2812_T1 2
  5. #define ws2812_T2 5
  6. #define ws2812_T3 3
  7.  
  8. static const uint16_t ws2812_program_instructions[] = {
  9.             //     .wrap_target
  10.     0x6221, //  0: out    x, 1            side 0 [2]
  11.     0x1123, //  1: jmp    !x, 3           side 1 [1]
  12.     0x1400, //  2: jmp    0               side 1 [4]
  13.     0xa442, //  3: nop                    side 0 [4]
  14.             //     .wrap
  15. };

https://github.com/raspberrypi/pico-examples/tree/master/pio/ws2812

So Marcel, if you like a challenge then I'd propose you look at that code, things should translate quite easy in the Freepascal Implementation.

I might create the example myself, but not right now, I am busy with real life work atm....

Michael

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #11 on: August 05, 2022, 10:56:20 am »
I had a quick look at the example from the pico sdk, things may not be that difficult after all because the header of the pio example includes the 'compiled' program:

Well spotted.So the question is going to be how to integrate it, and I suspect that we'll be told that we have to run pioasm separately and build a resource file.

One thing I noticed about the SDK yesterday was that it includes the entire TinyUSB stack, so searching for files by name or content is complicated by the fact that there are files for all supported processors (i.e. not just the RP2040). There's also a stub in there for lwIP which will probably have every possible network device. No wonder it's bulky...

I also spot a couple of MIT licenses and a BSD... I certainly hope that somebody has checked that these are compatible with the SDK's overall loose copyleft approach.

MarkMLl


MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jb007

  • Full Member
  • ***
  • Posts: 145
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #12 on: August 05, 2022, 01:37:08 pm »

Hi, that's a lot of info!

Have to read that multiple times to get a full understanding.

I think it will help to shorten/simplify the ws2812b WRITING part, even while things already are working fine.


Tnx in advance!
to DIY or not to DIY

MarkMLl

  • Hero Member
  • *****
  • Posts: 6676
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #13 on: August 05, 2022, 01:58:57 pm »
READING the WS2812 string is left as an exercise... :-)

While not all of the parts are in place yet, the ultimate idea is that one of those chips will be able to run two simultaneous hardware threads, two simultaneous state machines driving PIOs, and SPI etc. on GPIO pins. You do have to give close attention though to what functionality is available on which pin: there's not a full crossbar exchange in there.

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

jb007

  • Full Member
  • ***
  • Posts: 145
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #14 on: August 06, 2022, 12:34:30 pm »
Hi,

 where can I find the declarations/code of all those pi PICO

- sio.gpio_set
- gpio_clr_mask
- sio.gpio_clr

etc.?

I'm used to work directly at bitlevel in the registers of my AVR-controllers
( PWM, ADC, UART etc )

Kind regards.






to DIY or not to DIY

 

TinyPortal © 2005-2018