Datasheet

Table Of Contents
50 setup_default_uart();
51
52 pio_spi_inst_t spi = {
53 .pio = pio0,
54 .sm = 0
55 };
56 float clkdiv = 31.25f; // 1 MHz @ 125 clk_sys
57 uint cpha0_prog_offs = pio_add_program(spi.pio, &spi_cpha0_program);
58 uint cpha1_prog_offs = pio_add_program(spi.pio, &spi_cpha1_program);
59
60 for (int cpha = 0; cpha <= 1; ++cpha) {
61 for (int cpol = 0; cpol <= 1; ++cpol) {
62 printf("CPHA = %d, CPOL = %d\n", cpha, cpol);
63 pio_spi_init(spi.pio, spi.sm,
64 cpha ? cpha1_prog_offs : cpha0_prog_offs,
65 8, // 8 bits per SPI frame
66 clkdiv,
67 cpha,
68 cpol,
69 PIN_SCK,
70 PIN_MOSI,
71 PIN_MISO
72 );
73 test(&spi);
74 sleep_ms(10);
75 }
76 }
77 }
3.6.2. WS2812 LEDs
WS2812 LEDs are driven by a proprietary pulse-width serial format, with a wide positive pulse representing a "1" bit, and
narrow positive pulse a "0". Each LED has a serial input and a serial output; LEDs are connected in a chain, with each serial
input connected to the previous LED’s serial output.
Symbol
Output
1 0 0 1 Latch
Figure 49. WS2812
line format. Wide
positive pulse for 1,
narrow positive pulse
for 0, very long
negative pulse for
latch enable
LEDs consume 24 bits of pixel data, then pass any additional input data on to their output. In this way a single serial burst
can individually program the colour of each LED in a chain. A long negative pulse latches the pixel data into the LEDs.
TODO link to examples
Ê1 .program ws2812
Ê2 .side_set 1
Ê3
Ê4 .wrap_target
Ê5 bitloop:
Ê6 out x, 1 [3] set 0 ; Side-set still takes place when instruction stalls
Ê7 jmp !x do_zero [2] set 1 ; Branch on the bit we shifted out. Positive pulse
Ê8 do_one:
Ê9 jmp bitloop [3] set 1 ; Continue driving high, for a long pulse
10 do_zero:
11 nop [3] set 0 ; Or drive low, for a short pulse
12 .wrap
This program shifts bits from the OSR into X, and produces a wide or narrow pulse on side-set pin 0, based on the value of
RP2040 Datasheet
3.6. Examples 347