Datasheet

Table Of Contents
This facility allows you to encapsulate both the PIO program and the associated setup required in the same source file.
See Section 3.3.9 for a more complete example.
3.3.9. Language generators
The following example shows a multi program source file (with multiple programs) which we will use to highlight c-sdk
and python output features
Pico Examples: https://github.com/raspberrypi/pico-examples/tree/pre_release/pio/ws2812/ws2812.pio Lines 1 - 85
Ê1 ;
Ê2 ; Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
Ê3 ;
Ê4 ; SPDX-License-Identifier: BSD-3-Clause
Ê5 ;
Ê6
Ê7 .program ws2812
Ê8 .side_set 1
Ê9
10 .define public T1 2
11 .define public T2 5
12 .define public T3 3
13
14 .lang_opt python sideset_init = pico.PIO.OUT_HIGH
15 .lang_opt python out_init = pico.PIO.OUT_HIGH
16 .lang_opt python out_shiftdir = 1
17
18 .wrap_target
19 bitloop:
20 out x, 1 side 0 [T3 - 1] ; Side-set still takes place when instruction stalls
21 jmp !x do_zero side 1 [T1 - 1] ; Branch on the bit we shifted out. Positive pulse
22 do_one:
23 jmp bitloop side 1 [T2 - 1] ; Continue driving high, for a long pulse
24 do_zero:
25 nop side 0 [T2 - 1] ; Or drive low, for a short pulse
26 .wrap
27
28 % c-sdk {
29 #include "hardware/clocks.h"
30
31 static inline void ws2812_program_init(PIO pio, uint sm, uint offset, uint pin, float freq,
Ê bool rgbw) {
32
33 pio_gpio_select(pio, pin);
34 pio_sm_set_consecutive_pindirs(pio, sm, pin, 1, true);
35
36 pio_sm_config c = ws2812_program_get_default_config(offset);
37 sm_config_set_sideset_pins(&c, pin);
38 sm_config_set_out_shift(&c, false, true, rgbw ? 32 : 24);
39 sm_config_set_fifo_join(&c, PIO_FIFO_JOIN_TX);
40
41 int cycles_per_bit = ws2812_T1 + ws2812_T2 + ws2812_T3;
42 float div = clock_get_hz(clk_sys) / (freq * cycles_per_bit);
43 sm_config_set_clkdiv(&c, div);
44
45 pio_sm_init(pio, sm, offset, &c);
46 pio_sm_enable(pio, sm, true);
47 }
48 %}
49
50 .program ws2812_parallel
RP2040 Datasheet
3.3. PIO Assembler (pioasm) 317