rp2040_pio.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // -------------------------------------------------- //
  2. // This file is autogenerated by pioasm; do not edit! //
  3. // -------------------------------------------------- //
  4. // Unless you know what you are doing...
  5. // Lines 47 and 52 have been edited to set transmit bit count
  6. #if !PICO_NO_HARDWARE
  7. #include "hardware/pio.h"
  8. #endif
  9. // ------ //
  10. // ws2812 //
  11. // ------ //
  12. #define ws2812_wrap_target 0
  13. #define ws2812_wrap 3
  14. #define ws2812_T1 2
  15. #define ws2812_T2 5
  16. #define ws2812_T3 3
  17. static const uint16_t ws2812_program_instructions[] = {
  18. // .wrap_target
  19. 0x6221, // 0: out x, 1 side 0 [2]
  20. 0x1123, // 1: jmp !x, 3 side 1 [1]
  21. 0x1400, // 2: jmp 0 side 1 [4]
  22. 0xa442, // 3: nop side 0 [4]
  23. // .wrap
  24. };
  25. #if !PICO_NO_HARDWARE
  26. static const struct pio_program ws2812_program = {
  27. .instructions = ws2812_program_instructions,
  28. .length = 4,
  29. .origin = -1,
  30. };
  31. static inline pio_sm_config ws2812_program_get_default_config(uint offset) {
  32. pio_sm_config c = pio_get_default_sm_config();
  33. sm_config_set_wrap(&c, offset + ws2812_wrap_target, offset + ws2812_wrap);
  34. sm_config_set_sideset(&c, 1, false, false);
  35. return c;
  36. }
  37. #include "hardware/clocks.h"
  38. static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, float freq, uint bits) {
  39. pio_gpio_init(pio, pin);
  40. pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
  41. pio_sm_config c = ws2812_program_get_default_config(offset);
  42. sm_config_set_sideset_pins(&c, pin);
  43. sm_config_set_out_shift(&c, false, true, bits); // <----<<< Length changed to "bits"
  44. sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
  45. int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3;
  46. float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
  47. sm_config_set_clkdiv(&c, div);
  48. pio_sm_init(pio, sm, offset, &c);
  49. pio_sm_set_enabled(pio, sm, true);
  50. }
  51. #endif