Datasheet

Table Of Contents
ÊÊ9 #endif
Ê10
Ê11 // ------ //
Ê12 // ws2812 //
Ê13 // ------ //
Ê14
Ê15 #define ws2812_wrap_target 0
Ê16 #define ws2812_wrap 3
Ê17
Ê18 #define ws2812_T1 2
Ê19 #define ws2812_T2 5
Ê20 #define ws2812_T3 3
Ê21
Ê22 static const uint16_t ws2812_program_instructions[] = {
Ê23 // .wrap_target
Ê24 0x6221, // 0: out x, 1 side 0 [2]
Ê25 0x1123, // 1: jmp !x, 3 side 1 [1]
Ê26 0x1400, // 2: jmp 0 side 1 [4]
Ê27 0xa442, // 3: nop side 0 [4]
Ê28 // .wrap
Ê29 };
Ê30
Ê31 #if !PICO_NO_HARDWARE
Ê32 static const struct pio_program ws2812_program = {
Ê33 .instructions = ws2812_program_instructions,
Ê34 .length = 4,
Ê35 .origin = -1,
Ê36 };
Ê37
Ê38 static inline pio_sm_config ws2812_program_get_default_config(uint offset) {
Ê39 pio_sm_config c = pio_get_default_sm_config();
Ê40 sm_config_set_wrap(&c, offset + ws2812_wrap_target, offset + ws2812_wrap);
Ê41 sm_config_set_sideset(&c, 1, false, false);
Ê42 return c;
Ê43 }
Ê44
Ê45 #include "hardware/clocks.h"
Ê46
Ê47 static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, float freq,
Ê bool rgbw) {
Ê48 pio_gpio_select(pio, pin);
Ê49 pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
Ê50 pio_sm_config c = ws2812_program_get_default_config(offset);
Ê51 sm_config_set_sideset_pins(&c, pin);
Ê52 sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24);
Ê53 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
Ê54 int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3;
Ê55 float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
Ê56 sm_config_set_clkdiv(&c, div);
Ê57 pio_sm_init(pio, sm, offset, &c);
Ê58 pio_sm_enable(pio, sm, true);
Ê59 }
Ê60
Ê61 #endif
Ê62
Ê63 // --------------- //
Ê64 // ws2812_parallel //
Ê65 // --------------- //
Ê66
Ê67 #define ws2812_parallel_wrap_target 0
Ê68 #define ws2812_parallel_wrap 3
Ê69
Ê70 #define ws2812_parallel_T1 2
RP2040 Datasheet
3.3. PIO Assembler (pioasm) 319