Datasheet

Table Of Contents
60 const int PIN_TX = 0;
61
62 int main()
63 {
64 tb_init();
65
66 puts("WS2812 Smoke Test");
67
68 pio_sm_init(pio0, 0);
69 pio_load_program_arr(pio0, ws2812_program, 0);
70 pio_setup_shiftctrl(pio0, 0, SHIFT_TO_RIGHT, SHIFT_TO_LEFT, false, true, 32, 24);
71 pio_set_clkdiv_int_frac(pio0, 0, 5, 0);
72 pio_setup_pinctrl(pio0, 0, 0, 0, 0, 0, PIN_TX, 0);
73 pio_setup_sideset(pio0, 0, 1, false, false);
74 pio_set_wrap(pio0, 0, ws2812_wrap_target, ws2812_wrap);
75 pio_sm_enable(pio0, 0, true);
76
77 pio_set_pindirs_with_mask(pio0, 0, 1u << PIN_TX, 1u << PIN_TX);
78 gpio_set_function(PIN_TX, GPIO_FUNC_PIO0);
79
80 int t = 0;
81
82 while (1)
83 {
84 int pat = rand() % count_of(pattern_table);
85 int dir = (rand() >> 30) & 1 ? 1 : -1;
86 puts(pattern_table[pat].name);
87 puts(dir == 1 ? "(forward)" : "(backward)");
88 for (int i = 0; i < 1000; ++i)
89 {
90 pattern_table[pat].pat(150, t);
91 sleep_ms(10);
92 t += dir;
93 button_prev = button;
94 button = gpio_get(PIN_BUTTON);
95 }
96 }
97
98 return tb_exit(0);
99 }
A C program configures the state machine to execute this program correctly, and sends some test patterns to a string of
150 LEDs. This program transmits on GPIO 0, but any pin can be selected, by changing the constant PIN_TX.
The state machine’s clock divider is configured to slow execution to around 10 MIPS. If system clock speed is 120 MHz,
this is a clock divisor of 12.
Note it is possible to make this program as short as 3 instructions, at the cost of making transmission time dependent on
data content:
1 .program ws2812_mini
2 .side_set 1
3
4 .wrap_target
5 bitloop:
6 out x, 1 [5] set 0
7 jmp !x bitloop [2] set 1
8 nop [3] set 1
9 .wrap
RP2040 Datasheet
3.6. Examples 349