Recent

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

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #15 on: August 06, 2022, 12:41:57 pm »
First document to read, even if not directly relevant to Pascal:

https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf

Second document to read:

https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf

After that you're basically at the mercy of what's in this forum and the relevant wiki pages (or equivalent for other languages not supported by the RPi Foundation).

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

  • Sr. Member
  • ****
  • Posts: 250
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #16 on: August 06, 2022, 12:52:12 pm »
Not sure what sio is, did you mean pio?
You can always grep through the sources in the repository of my pascal bindings, check the pico-SDK in GitHub and if you want to go the hard way you can always check the hardware reference manual. But I‘d advise against going fully lowlevel, with the new pico w things like wifi and multitasking are hard to implement on your own, it is better to stand on the shoulders of giants like FreeRTOS and lwip.


One more hint, pico functions often only exist as macros in C, so you cannot link to them and have to implement them in Pascal, there are tons of examples in my code for this.

Michael

MiR

  • Sr. Member
  • ****
  • Posts: 250
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #17 on: August 06, 2022, 02:41:50 pm »
I just realized that I already did a lot more with pio as I thought....

This is what I found on my computer, looks like 90% of the way to get things working:

Code: Pascal  [Select][+][-]
  1. program pio_ws2812;
  2. {
  3.   This file is part of pico-fpcsamples
  4.   Copyright (c) 2021 -  Michael Ring
  5.  
  6.   This program is free software: you can redistribute it and/or modify it under the terms of the FPC modified GNU
  7.   Library General Public License for more
  8.  
  9.   This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied
  10.   warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the FPC modified GNU Library General Public
  11.   License for more details.
  12. }
  13.  
  14. {$MODE OBJFPC}
  15. {$H+}
  16. {$MEMORY 10000,10000}
  17.  
  18. uses
  19.   pico_c,
  20.   pico_gpio_c,
  21.   pico_clocks_c,
  22.   pico_pio_c;
  23.  
  24. const
  25.   ws2812_wrap_target=0;
  26.   ws2812_wrap=3;
  27.   ws2812_T1=2;
  28.   ws2812_T2=5;
  29.   ws2812_T3=3;
  30.  
  31.   ws2812_program_instructions : array of word = (
  32.            //     .wrap_target
  33.     $6221, //  0: out    x, 1            side 0 [2]
  34.     $1123, //  1: jmp    !x, 3           side 1 [1]
  35.     $1400, //  2: jmp    0               side 1 [4]
  36.     $a442  //  3: nop                    side 0 [4]
  37.            //     .wrap
  38.   );
  39.  
  40.   ws2812_program : Tpio_program  = (
  41.     instructions : @ws2812_program_instructions;
  42.     length : 4;
  43.     origin : -1;
  44.   );
  45.  
  46. function ws2812_program_get_default_config(offset:longWord):Tpio_sm_config;
  47. begin
  48.   Result := pio_get_default_sm_config();
  49.   sm_config_set_wrap(Result, offset + ws2812_wrap_target, offset + ws2812_wrap);
  50.   sm_config_set_sideset(Result, 1, false, false);
  51. end;
  52.  
  53. procedure ws2812_program_init(var pio : TPIO_Registers; sm : longWord; offset : longWord; pin : TPinIdentifier; freq : Real; rgbw : boolean);
  54. var
  55.   c : Tpio_sm_config;
  56.   cycles_per_bit : longWord;
  57.   &div : real;
  58. begin
  59.   pio_gpio_init(pio, pin);
  60.   pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
  61.   c := ws2812_program_get_default_config(offset);
  62.   sm_config_set_sideset_pins(c, pin);
  63.   if rgbw = true then
  64.     sm_config_set_out_shift(c, false, true, 32)
  65.   else
  66.     sm_config_set_out_shift(c, false, true, 24);
  67.  
  68.   sm_config_set_fifo_join(c, PIO_FIFO_JOIN_TX);
  69.   cycles_per_bit := ws2812_T1 + ws2812_T2 + ws2812_T3;
  70.   &div := clock_get_hz(clk_sys) / (freq * cycles_per_bit);
  71.   sm_config_set_clkdiv(c, &div);
  72.   pio_sm_init(pio, sm, offset, c);
  73.   pio_sm_set_enabled(pio, sm, true);
  74. end;
  75.  
  76. var
  77.   sm : longWord;
  78.   offset : longWord;
  79. begin
  80.   sm := 0;
  81.   offset := pio_add_program(pio0, ws2812_program);
  82.   ws2812_program_init(pio0, sm, offset, TPicoPin.UART_TX, 800000, true);
  83.   //pio_sm_put_blocking(pio0, 0, pixel_grb shl 8);
  84. end.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #18 on: August 06, 2022, 06:09:43 pm »
Interesting, TFT. So in principle a preprocessor could recognise e.g. //#begin and //#end markers for pioasm source, and use that to generate a file for an immediately-following $include directive.

Looking closely at pioasm, it apparently supports hex- and Ada-format output... I wonder how close those are to being usable without a complex wrapper?

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

  • Sr. Member
  • ****
  • Posts: 250
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #19 on: August 06, 2022, 06:42:35 pm »
Likely (at least from my point of view) this is not worth the effort. Most people will not want want to go down the rabbit hole of low level coding and do not have the equipment to do so, like a logic analyzer.

Once ws2812 works flawlessly it can be wrapped in an unit and people will simply use that unit, ignoring the underlying complexity.

Looking at the examples that are in pico-examples on github the main usecases are apa102, manchester/quadrature encoding, st7789 interface and ws2812. The other stuff is more or less duplication of already available peripherals like spi, uart, pwm. Anyway, it is always the same pattern, compile pio file, convert the interface for your pio code and then apply.

If you really really want make things better then you could create a patch for picoasm so that it generates pascal output, this is likely the best and easiest way to fix the issue if you still feel fixing is needed.

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #20 on: August 06, 2022, 07:15:37 pm »
If you really really want make things better then you could create a patch for picoasm so that it generates pascal output, this is likely the best and easiest way to fix the issue if you still feel fixing is needed.

Yes, definitely the way to go. It looks as though Python is the only language that embeds PIO source, and C- which in fairness is a special case- has a special facility for embedding C source in a .pio file. It looks as though converting the Ada variant would be trivial, possibly with the presence of the -p option determining whether the result was an include file or a unit.

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

  • Sr. Member
  • ****
  • Posts: 250
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #21 on: August 06, 2022, 07:18:24 pm »
Just fo the fun of it I already started the patch, creating freepascal output is totally easy, I like the way the guys from Raspberry Foundation write their code...

Code: Text  [Select][+][-]
  1. ./pioasm/pioasm                                                                                             (master)pico-examples
  2. error: expected input filename
  3.  
  4. usage: pioasm <options> <input> (<output>)
  5.  
  6. Assemble file of PIO program(s) for use in applications.
  7.    <input>             the input filename
  8.    <output>            the output filename (or filename prefix if the output format produces multiple outputs).
  9.                        if not specified, a single output will be written to stdout
  10.  
  11. options:
  12.   -o <output_format>   select output_format (default 'c-sdk'); available options are:
  13.                            c-sdk
  14.                                C header suitable for use with the Raspberry Pi Pico SDK
  15.                            python
  16.                                Python file suitable for use with MicroPython
  17.                            hex
  18.                                Raw hex output (only valid for single program inputs)
  19.                            ada
  20.                                Ada specification
  21.                            freepascal
  22.                                Freepascal include file suitable for use with the Raspberry Pi Pico SDK
  23.   -p <output_param>    add a parameter to be passed to the output format generator
  24.   -?, --help           print this help and exit

MiR

  • Sr. Member
  • ****
  • Posts: 250
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #22 on: August 06, 2022, 08:07:55 pm »
Done.. Patch for pioasm attached...


Code: Text  [Select][+][-]
  1. ring@macbook-pro build-pico $ ./pioasm/pioasm -o freepascal ~/devel/pico-fpcexamples/pio_ws2812/ws2812.pio                                (master)pico-examples
  2. // -------------------------------------------------- //
  3. // This file is autogenerated by pioasm; do not edit! //
  4. // -------------------------------------------------- //
  5.  
  6. // ------ //
  7. // ws2812 //
  8. // ------ //
  9.  
  10. const ws2812_wrap_target=0;
  11. const ws2812_wrap=3;
  12.  
  13. const ws2812_T1=2;
  14. const ws2812_T2=5;
  15. const ws2812_T3=3;
  16.  
  17. const ws2812_program_instructions : array of word = (
  18.            //     .wrap_target
  19.     $6221, //  0: out    x, 1            side 0 [2]
  20.     $1123, //  1: jmp    !x, 3           side 1 [1]
  21.     $1400, //  2: jmp    0               side 1 [4]
  22.     $a442  //  3: nop                    side 0 [4]
  23.            //     .wrap
  24. );
  25.  
  26. const ws2812_program : Tpio_program = (
  27.     instructions : @ws2812_program_instructions;
  28.     length : 4;
  29.     origin : -1;
  30. );
  31.  
  32. function ws2812_program_get_default_config(offset : longWord):Tpio_sm_config;
  33. begin
  34.   Result := pio_get_default_sm_config();
  35.   sm_config_set_wrap(Result, offset + ws2812_wrap_target, offset + ws2812_wrap);
  36.   sm_config_set_sideset(Result, 1, false, false);
  37. end;
  38.  
  39.  
  40. procedure ws2812_program_init(var pio : TPIO_Registers; sm : longWord; offset : longWord; pin : TPinIdentifier; freq : Real; rgbw : boolean);
  41. var
  42.   c : Tpio_sm_config;
  43.   cycles_per_bit : longWord;
  44.   &div : real;
  45. begin
  46.   pio_gpio_init(pio, pin);
  47.   pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
  48.   c := ws2812_program_get_default_config(offset);
  49.   sm_config_set_sideset_pins(c, pin);
  50.   if rgbw = true then
  51.     sm_config_set_out_shift(c, false, true, 32)
  52.   else
  53.     sm_config_set_out_shift(c, false, true, 24);
  54.   sm_config_set_fifo_join(c, PIO_FIFO_JOIN_TX);
  55.   cycles_per_bit := ws2812_T1 + ws2812_T2 + ws2812_T3;
  56.   &div := clock_get_hz(clk_sys) / (freq * cycles_per_bit);
  57.   sm_config_set_clkdiv(c, &div);
  58.   pio_sm_init(pio, sm, offset, c);
  59.   pio_sm_set_enabled(pio, sm, true);
  60. end;
  61.  
  62.  
  63. // --------------- //
  64. // ws2812_parallel //
  65. // --------------- //
  66.  
  67. const ws2812_parallel_wrap_target=0;
  68. const ws2812_parallel_wrap=3;
  69.  
  70. const ws2812_parallel_T1=2;
  71. const ws2812_parallel_T2=5;
  72. const ws2812_parallel_T3=3;
  73.  
  74. const ws2812_parallel_program_instructions : array of word = (
  75.            //     .wrap_target
  76.     $6020, //  0: out    x, 32
  77.     $a10b, //  1: mov    pins, !null            [1]
  78.     $a401, //  2: mov    pins, x                [4]
  79.     $a103  //  3: mov    pins, null             [1]
  80.            //     .wrap
  81. );
  82.  
  83. const ws2812_parallel_program : Tpio_program = (
  84.     instructions : @ws2812_parallel_program_instructions;
  85.     length : 4;
  86.     origin : -1;
  87. );
  88.  
  89. function ws2812_parallel_program_get_default_config(offset : longWord):Tpio_sm_config;
  90. begin
  91.   Result := pio_get_default_sm_config();
  92.   sm_config_set_wrap(Result, offset + ws2812_parallel_wrap_target, offset + ws2812_parallel_wrap);
  93. end;

jb007

  • Full Member
  • ***
  • Posts: 145
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #23 on: August 06, 2022, 08:09:20 pm »

https://datasheets.raspberrypi.com/pico/getting-started-with-pico.pdf

https://datasheets.raspberrypi.com/pico/raspberry-pi-pico-c-sdk.pdf

MarkMLl

Thanks!
At first quickview these documents, it will defenitely give me more understanding.

( Last few days I was studying the rp2040-datasheet itself ).
 
Tnx also for the other replies, it will take a while for me to understand the content!

Kind regards.





« Last Edit: August 06, 2022, 08:11:54 pm by jb007 »
to DIY or not to DIY

MiR

  • Sr. Member
  • ****
  • Posts: 250
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #24 on: August 06, 2022, 08:11:00 pm »
There also need to be a few changes in the pio file for the freepascal specific code:

Code: Text  [Select][+][-]
  1. .wrap_target
  2. bitloop:
  3.     out x, 1       side 0 [T3 - 1] ; Side-set still takes place when instruction stalls
  4.     jmp !x do_zero side 1 [T1 - 1] ; Branch on the bit we shifted out. Positive pulse
  5. do_one:
  6.     jmp  bitloop   side 1 [T2 - 1] ; Continue driving high, for a long pulse
  7. do_zero:
  8.     nop            side 0 [T2 - 1] ; Or drive low, for a short pulse
  9. .wrap
  10.  
  11. % c-sdk {
  12. #include "hardware/clocks.h"
  13.  
  14. static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, float freq, bool rgbw) {
  15.  
  16.     pio_gpio_init(pio, pin);
  17.     pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
  18.  
  19.     pio_sm_config c = ws2812_program_get_default_config(offset);
  20.     sm_config_set_sideset_pins(&c, pin);
  21.     sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24);
  22.     sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
  23.  
  24.     int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3;
  25.     float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
  26.     sm_config_set_clkdiv(&c, div);
  27.  
  28.     pio_sm_init(pio, sm, offset, &c);
  29.     pio_sm_set_enabled(pio, sm, true);
  30. }
  31. %}
  32.  
  33. % freepascal {
  34.  
  35. procedure ws2812_program_init(var pio : TPIO_Registers; sm : longWord; offset : longWord; pin : TPinIdentifier; freq : Real; rgbw : boolean);
  36. var
  37.   c : Tpio_sm_config;
  38.   cycles_per_bit : longWord;
  39.   &div : real;
  40. begin
  41.   pio_gpio_init(pio, pin);
  42.   pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
  43.   c := ws2812_program_get_default_config(offset);
  44.   sm_config_set_sideset_pins(c, pin);
  45.   if rgbw = true then
  46.     sm_config_set_out_shift(c, false, true, 32)
  47.   else
  48.     sm_config_set_out_shift(c, false, true, 24);
  49.  
  50.   sm_config_set_fifo_join(c, PIO_FIFO_JOIN_TX);
  51.   cycles_per_bit := ws2812_T1 + ws2812_T2 + ws2812_T3;
  52.   &div := clock_get_hz(clk_sys) / (freq * cycles_per_bit);
  53.   sm_config_set_clkdiv(c, &div);
  54.   pio_sm_init(pio, sm, offset, c);
  55.   pio_sm_set_enabled(pio, sm, true);
  56. end;
  57.  
  58. %}

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #25 on: August 06, 2022, 09:21:05 pm »
/Very/ nicely done. It occurs to me that getting that patch into the SDK- including modified PIO files- would be a /very/ good advert for FPC since anybody looking at the PIO files will see it.

Might also be better to use "object-pascal" rather than "freepascal", which would be in line with the way that the other targets specify a language but not an implementation.

MarkMLl
« Last Edit: August 07, 2022, 11:53:19 am by 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

  • Sr. Member
  • ****
  • Posts: 250
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #26 on: August 07, 2022, 01:37:35 pm »
Unfortunately pio needs spinlocks, not sure if it is enough to simply reset them on runtime init or if more complex work is needed.

So for now I'd advise not to spend much time on this unless you are willing to really dig deep.

If there is a simple solution I will come back here, if things are more complex I will first have to rethink the overall integration with the switch to pico sdk 1.4.

 

MarkMLl

  • Hero Member
  • *****
  • Posts: 6692
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #27 on: August 07, 2022, 02:16:27 pm »
What you've done is certainly very interesting, and I'm sure (or at least fervently hope) that I'm not the only person that appreciates it.

As I've noted elsewhere, I'm a little uncertain as to the extent to which the appropriate SDK version is /required/ for a particular Pico iteration. For example, I'm still not sure about this but I /suspect/ the the onboard LED has been moved to a different electrical signal for the -W.

And however much the RPi foundation loves it, I'm very much inclined to avoid Visual Studio which once installed appears to leave a Trojan running on a Linux system.

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

  • Sr. Member
  • ****
  • Posts: 250
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #28 on: August 07, 2022, 07:59:10 pm »
success...

I managed to make pio work with fortunately very little extra changes.

please pull

https://github.com/michael-ring/pico-fpcexamples

and enjoy!

Michael

jb007

  • Full Member
  • ***
  • Posts: 145
Re: pi PICO controlling ws2812b leds, ledstrips
« Reply #29 on: August 07, 2022, 08:29:59 pm »
Hi,

 tnx for all the repleis and guidelines etc.

I decided ( al least for the coming time ) not to dive into all off the stuff
'cause it is getting too much/difficult for me right now...
Again all, the replies are apriciated!

Next step is to use the example spi_st7735 ( got the screens allready ) and the serial uart example.
Allready have made a nice setup with AVR mega2560 ( 2560 core pcb ).

Designed and made some pcb's myself. Here some pcb's combined.
https://www.youtube.com/watch?v=XCanufiR4r4&list=PLcCKmeWXkrzRS5It-4WRdyveafrhZfa_2&index=2

The pico wil be ad later and via Tx/Rx conencted to mega2560.

PCB's designed/routed with Altium's CircuitMaker. PCB produced by JLC pcb.
Soldered all the components myself.

For testing the 2560 is connected to pc ( later it will be by RC  a DIY transmitter ).

pc to mega2560: https://www.youtube.com/watch?v=bSEvQQRRrq8&t=2s

Fot those interrested, one can checkout the rest on same youtubecahnnel as above.t
to see the DIY truck, modified excavator etc.https://www.youtube.com/channel/UCGfykOE4EGG5v1ArdRYYOCw/playlists

Michaels Ring, thank you very much again for you examples!
They are very good for me to continue the PICO-things I want to achieve.

Kind regards. Marcel


Soon, I'll post my ws2812b code when it's ready and clear.











« Last Edit: August 07, 2022, 08:35:11 pm by jb007 »
to DIY or not to DIY

 

TinyPortal © 2005-2018