ws2812.pio.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. // -------------------------------------------------- //
  2. // This file is autogenerated by pioasm; do not edit! //
  3. // -------------------------------------------------- //
  4. #pragma once
  5. #if !PICO_NO_HARDWARE
  6. #include "hardware/pio.h"
  7. #endif
  8. // ------ //
  9. // ws2812 //
  10. // ------ //
  11. #define ws2812_wrap_target 0
  12. #define ws2812_wrap 3
  13. #define ws2812_T1 2
  14. #define ws2812_T2 5
  15. #define ws2812_T3 3
  16. static const uint16_t ws2812_program_instructions[] = {
  17. // .wrap_target
  18. 0x6221, // 0: out x, 1 side 0 [2]
  19. 0x1123, // 1: jmp !x, 3 side 1 [1]
  20. 0x1400, // 2: jmp 0 side 1 [4]
  21. 0xa442, // 3: nop side 0 [4]
  22. // .wrap
  23. };
  24. #if !PICO_NO_HARDWARE
  25. static const struct pio_program ws2812_program = {
  26. .instructions = ws2812_program_instructions,
  27. .length = 4,
  28. .origin = -1,
  29. };
  30. static inline pio_sm_config ws2812_program_get_default_config(uint offset) {
  31. pio_sm_config c = pio_get_default_sm_config();
  32. sm_config_set_wrap(&c, offset + ws2812_wrap_target, offset + ws2812_wrap);
  33. sm_config_set_sideset(&c, 1, false, false);
  34. return c;
  35. }
  36. #include "hardware/clocks.h"
  37. static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, float freq, bool rgbw) {
  38. pio_gpio_init(pio, pin);
  39. pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
  40. pio_sm_config c = ws2812_program_get_default_config(offset);
  41. sm_config_set_sideset_pins(&c, pin);
  42. sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24);
  43. sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
  44. int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3;
  45. float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
  46. sm_config_set_clkdiv(&c, div);
  47. pio_sm_init(pio, sm, offset, &c);
  48. pio_sm_set_enabled(pio, sm, true);
  49. }
  50. #endif
  51. // --------------- //
  52. // ws2812_parallel //
  53. // --------------- //
  54. #define ws2812_parallel_wrap_target 0
  55. #define ws2812_parallel_wrap 3
  56. #define ws2812_parallel_T1 2
  57. #define ws2812_parallel_T2 5
  58. #define ws2812_parallel_T3 3
  59. static const uint16_t ws2812_parallel_program_instructions[] = {
  60. // .wrap_target
  61. 0x6020, // 0: out x, 32
  62. 0xa10b, // 1: mov pins, !null [1]
  63. 0xa401, // 2: mov pins, x [4]
  64. 0xa103, // 3: mov pins, null [1]
  65. // .wrap
  66. };
  67. #if !PICO_NO_HARDWARE
  68. static const struct pio_program ws2812_parallel_program = {
  69. .instructions = ws2812_parallel_program_instructions,
  70. .length = 4,
  71. .origin = -1,
  72. };
  73. static inline pio_sm_config ws2812_parallel_program_get_default_config(uint offset) {
  74. pio_sm_config c = pio_get_default_sm_config();
  75. sm_config_set_wrap(&c, offset + ws2812_parallel_wrap_target, offset + ws2812_parallel_wrap);
  76. return c;
  77. }
  78. #include "hardware/clocks.h"
  79. static inline void ws2812_parallel_program_init(PIO pio, uint sm, uint offset, uint pin_base, uint pin_count, float freq) {
  80. for(uint i=pin_base; i<pin_base+pin_count; i++) {
  81. pio_gpio_init(pio, i);
  82. }
  83. pio_sm_set_consecutive_pindirs(pio, sm, pin_base, pin_count, true);
  84. pio_sm_config c = ws2812_parallel_program_get_default_config(offset);
  85. sm_config_set_out_shift(&c, true, true, 32);
  86. sm_config_set_out_pins(&c, pin_base, pin_count);
  87. sm_config_set_set_pins(&c, pin_base, pin_count);
  88. sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
  89. int cycles_per_bit = ws2812_parallel_T1 + ws2812_parallel_T2 + ws2812_parallel_T3;
  90. float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
  91. sm_config_set_clkdiv(&c, div);
  92. pio_sm_init(pio, sm, offset, &c);
  93. pio_sm_set_enabled(pio, sm, true);
  94. }
  95. #endif