9 #include "hardware/pio.h" 16 #define ws2812_wrap_target 0 23 static const uint16_t ws2812_program_instructions[] = {
33 static const struct pio_program ws2812_program = {
34 .instructions = ws2812_program_instructions,
39 static inline pio_sm_config ws2812_program_get_default_config(uint offset) {
40 pio_sm_config c = pio_get_default_sm_config();
41 sm_config_set_wrap(&c, offset + ws2812_wrap_target, offset + ws2812_wrap);
42 sm_config_set_sideset(&c, 1,
false,
false);
46 #include "hardware/clocks.h" 47 static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin,
48 float freq, uint bits) {
49 pio_gpio_init(pio, pin);
50 pio_sm_set_consecutive_pindirs(pio, sm, pin, 1,
true);
51 pio_sm_config c = ws2812_program_get_default_config(offset);
52 sm_config_set_sideset_pins(&c, pin);
53 sm_config_set_out_shift(&c,
false,
true,
55 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
56 int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3;
57 float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
58 sm_config_set_clkdiv(&c, div);
59 pio_sm_init(pio, sm, offset, &c);
60 pio_sm_set_enabled(pio, sm,
true);