1 /*
2 * Copyright (c) 2020 Raspberry Pi (Trading) Ltd.
3 *
4 * SPDX-License-Identifier: BSD-3-Clause
5 */
6
7 #ifndef _HARDWARE_PIO_H
8 #define _HARDWARE_PIO_H
9
10 #include "pico.h"
11 #include "hardware/address_mapped.h"
12 #include "hardware/structs/pio.h"
13 #include "hardware/gpio.h"
14 #include "hardware/regs/dreq.h"
15 #include "hardware/pio_instructions.h"
16
17 // PICO_CONFIG: PARAM_ASSERTIONS_ENABLED_PIO, Enable/disable assertions in the PIO module, type=bool, default=0, group=hardware_pio
18 #ifndef PARAM_ASSERTIONS_ENABLED_PIO
19 #define PARAM_ASSERTIONS_ENABLED_PIO 0
20 #endif
21
22 /** \file hardware/pio.h
23 * \defgroup hardware_pio hardware_pio
24 *
25 * Programmable I/O (PIO) API
26 *
27 * A programmable input/output block (PIO) is a versatile hardware interface which
28 * can support a number of different IO standards. There are two PIO blocks in the RP2040.
29 *
30 * Each PIO is programmable in the same sense as a processor: the four state machines independently
31 * execute short, sequential programs, to manipulate GPIOs and transfer data. Unlike a general
32 * purpose processor, PIO state machines are highly specialised for IO, with a focus on determinism,
33 * precise timing, and close integration with fixed-function hardware. Each state machine is equipped
34 * with:
35 * * Two 32-bit shift registers – either direction, any shift count
36 * * Two 32-bit scratch registers
37 * * 4×32 bit bus FIFO in each direction (TX/RX), reconfigurable as 8×32 in a single direction
38 * * Fractional clock divider (16 integer, 8 fractional bits)
39 * * Flexible GPIO mapping
40 * * DMA interface, sustained throughput up to 1 word per clock from system DMA
41 * * IRQ flag set/clear/status
42 *
43 * Full details of the PIO can be found in the RP2040 datasheet.
44 */
45
46 #ifdef __cplusplus
47 extern "C" {
48 #endif
49
50 static_assert(PIO_SM0_SHIFTCTRL_FJOIN_RX_LSB == PIO_SM0_SHIFTCTRL_FJOIN_TX_LSB + 1, "");
51
52 /** \brief FIFO join states
53 * \ingroup hardware_pio
54 */
55 enum pio_fifo_join {
56 PIO_FIFO_JOIN_NONE = 0,
57 PIO_FIFO_JOIN_TX = 1,
58 PIO_FIFO_JOIN_RX = 2,
59 };
60
61 /** \brief MOV status types
62 * \ingroup hardware_pio
63 */
64 enum pio_mov_status_type {
65 STATUS_TX_LESSTHAN = 0,
66 STATUS_RX_LESSTHAN = 1
67 };
68
69 typedef pio_hw_t *PIO;
70
71 /** Identifier for the first (PIO 0) hardware PIO instance (for use in PIO functions).
72 *
73 * e.g. pio_gpio_init(pio0, 5)
74 *
75 * \ingroup hardware_pio
76 */
77 #define pio0 pio0_hw
78
79 /** Identifier for the second (PIO 1) hardware PIO instance (for use in PIO functions).
80 *
81 * e.g. pio_gpio_init(pio1, 5)
82 *
83 * \ingroup hardware_pio
84 */
85 #define pio1 pio1_hw
86
87 /** \brief PIO state machine configuration
88 * \defgroup sm_config sm_config
89 * \ingroup hardware_pio
90 *
91 * A PIO block needs to be configured, these functions provide helpers to set up configuration
92 * structures. See \ref pio_sm_set_config
93 *
94 */
95
96 /** \brief PIO Configuration structure
97 * \ingroup sm_config
98 *
99 * This structure is an in-memory representation of the configuration that can be applied to a PIO
100 * state machine later using pio_sm_set_config() or pio_sm_init().
101 */
102 typedef struct {
103 uint32_t clkdiv;
104 uint32_t execctrl;
105 uint32_t shiftctrl;
106 uint32_t pinctrl;
107 } pio_sm_config;
108
check_sm_param(__unused uint sm)109 static inline void check_sm_param(__unused uint sm) {
110 valid_params_if(PIO, sm < NUM_PIO_STATE_MACHINES);
111 }
112
check_sm_mask(__unused uint mask)113 static inline void check_sm_mask(__unused uint mask) {
114 valid_params_if(PIO, mask < (1u << NUM_PIO_STATE_MACHINES));
115 }
116
117
check_pio_param(__unused PIO pio)118 static inline void check_pio_param(__unused PIO pio) {
119 valid_params_if(PIO, pio == pio0 || pio == pio1);
120 }
121
122 /*! \brief Set the 'out' pins in a state machine configuration
123 * \ingroup sm_config
124 *
125 * Can overlap with the 'in', 'set' and 'sideset' pins
126 *
127 * \param c Pointer to the configuration structure to modify
128 * \param out_base 0-31 First pin to set as output
129 * \param out_count 0-32 Number of pins to set.
130 */
sm_config_set_out_pins(pio_sm_config * c,uint out_base,uint out_count)131 static inline void sm_config_set_out_pins(pio_sm_config *c, uint out_base, uint out_count) {
132 valid_params_if(PIO, out_base < 32);
133 valid_params_if(PIO, out_count <= 32);
134 c->pinctrl = (c->pinctrl & ~(PIO_SM0_PINCTRL_OUT_BASE_BITS | PIO_SM0_PINCTRL_OUT_COUNT_BITS)) |
135 (out_base << PIO_SM0_PINCTRL_OUT_BASE_LSB) |
136 (out_count << PIO_SM0_PINCTRL_OUT_COUNT_LSB);
137 }
138
139 /*! \brief Set the 'set' pins in a state machine configuration
140 * \ingroup sm_config
141 *
142 * Can overlap with the 'in', 'out' and 'sideset' pins
143 *
144 * \param c Pointer to the configuration structure to modify
145 * \param set_base 0-31 First pin to set as
146 * \param set_count 0-5 Number of pins to set.
147 */
sm_config_set_set_pins(pio_sm_config * c,uint set_base,uint set_count)148 static inline void sm_config_set_set_pins(pio_sm_config *c, uint set_base, uint set_count) {
149 valid_params_if(PIO, set_base < 32);
150 valid_params_if(PIO, set_count <= 5);
151 c->pinctrl = (c->pinctrl & ~(PIO_SM0_PINCTRL_SET_BASE_BITS | PIO_SM0_PINCTRL_SET_COUNT_BITS)) |
152 (set_base << PIO_SM0_PINCTRL_SET_BASE_LSB) |
153 (set_count << PIO_SM0_PINCTRL_SET_COUNT_LSB);
154 }
155
156 /*! \brief Set the 'in' pins in a state machine configuration
157 * \ingroup sm_config
158 *
159 * Can overlap with the 'out', 'set' and 'sideset' pins
160 *
161 * \param c Pointer to the configuration structure to modify
162 * \param in_base 0-31 First pin to use as input
163 */
sm_config_set_in_pins(pio_sm_config * c,uint in_base)164 static inline void sm_config_set_in_pins(pio_sm_config *c, uint in_base) {
165 valid_params_if(PIO, in_base < 32);
166 c->pinctrl = (c->pinctrl & ~PIO_SM0_PINCTRL_IN_BASE_BITS) |
167 (in_base << PIO_SM0_PINCTRL_IN_BASE_LSB);
168 }
169
170 /*! \brief Set the 'sideset' pins in a state machine configuration
171 * \ingroup sm_config
172 *
173 * Can overlap with the 'in', 'out' and 'set' pins
174 *
175 * \param c Pointer to the configuration structure to modify
176 * \param sideset_base 0-31 base pin for 'side set'
177 */
sm_config_set_sideset_pins(pio_sm_config * c,uint sideset_base)178 static inline void sm_config_set_sideset_pins(pio_sm_config *c, uint sideset_base) {
179 valid_params_if(PIO, sideset_base < 32);
180 c->pinctrl = (c->pinctrl & ~PIO_SM0_PINCTRL_SIDESET_BASE_BITS) |
181 (sideset_base << PIO_SM0_PINCTRL_SIDESET_BASE_LSB);
182 }
183
184 /*! \brief Set the 'sideset' options in a state machine configuration
185 * \ingroup sm_config
186 *
187 * \param c Pointer to the configuration structure to modify
188 * \param bit_count Number of bits to steal from delay field in the instruction for use of side set (max 5)
189 * \param optional True if the topmost side set bit is used as a flag for whether to apply side set on that instruction
190 * \param pindirs True if the side set affects pin directions rather than values
191 */
sm_config_set_sideset(pio_sm_config * c,uint bit_count,bool optional,bool pindirs)192 static inline void sm_config_set_sideset(pio_sm_config *c, uint bit_count, bool optional, bool pindirs) {
193 valid_params_if(PIO, bit_count <= 5);
194 valid_params_if(PIO, !optional || bit_count >= 1);
195 c->pinctrl = (c->pinctrl & ~PIO_SM0_PINCTRL_SIDESET_COUNT_BITS) |
196 (bit_count << PIO_SM0_PINCTRL_SIDESET_COUNT_LSB);
197
198 c->execctrl = (c->execctrl & ~(PIO_SM0_EXECCTRL_SIDE_EN_BITS | PIO_SM0_EXECCTRL_SIDE_PINDIR_BITS)) |
199 (bool_to_bit(optional) << PIO_SM0_EXECCTRL_SIDE_EN_LSB) |
200 (bool_to_bit(pindirs) << PIO_SM0_EXECCTRL_SIDE_PINDIR_LSB);
201 }
202
203 /*! \brief Set the state machine clock divider (from integer and fractional parts - 16:8) in a state machine configuration
204 * \ingroup sm_config
205 *
206 * The clock divider can slow the state machine's execution to some rate below
207 * the system clock frequency, by enabling the state machine on some cycles
208 * but not on others, in a regular pattern. This can be used to generate e.g.
209 * a given UART baud rate. See the datasheet for further detail.
210 *
211 * \param c Pointer to the configuration structure to modify
212 * \param div_int Integer part of the divisor
213 * \param div_frac Fractional part in 1/256ths
214 * \sa sm_config_set_clkdiv()
215 */
sm_config_set_clkdiv_int_frac(pio_sm_config * c,uint16_t div_int,uint8_t div_frac)216 static inline void sm_config_set_clkdiv_int_frac(pio_sm_config *c, uint16_t div_int, uint8_t div_frac) {
217 invalid_params_if(PIO, div_int == 0 && div_frac != 0);
218 c->clkdiv =
219 (((uint)div_frac) << PIO_SM0_CLKDIV_FRAC_LSB) |
220 (((uint)div_int) << PIO_SM0_CLKDIV_INT_LSB);
221 }
222
pio_calculate_clkdiv_from_float(float div,uint16_t * div_int,uint8_t * div_frac)223 static inline void pio_calculate_clkdiv_from_float(float div, uint16_t *div_int, uint8_t *div_frac) {
224 valid_params_if(PIO, div >= 1 && div <= 65536);
225 *div_int = (uint16_t)div;
226 if (*div_int == 0) {
227 *div_frac = 0;
228 } else {
229 *div_frac = (uint8_t)((div - (float)*div_int) * (1u << 8u));
230 }
231 }
232
233 /*! \brief Set the state machine clock divider (from a floating point value) in a state machine configuration
234 * \ingroup sm_config
235 *
236 * The clock divider slows the state machine's execution by masking the
237 * system clock on some cycles, in a repeating pattern, so that the state
238 * machine does not advance. Effectively this produces a slower clock for the
239 * state machine to run from, which can be used to generate e.g. a particular
240 * UART baud rate. See the datasheet for further detail.
241 *
242 * \param c Pointer to the configuration structure to modify
243 * \param div The fractional divisor to be set. 1 for full speed. An integer clock divisor of n
244 * will cause the state machine to run 1 cycle in every n.
245 * Note that for small n, the jitter introduced by a fractional divider (e.g. 2.5) may be unacceptable
246 * although it will depend on the use case.
247 */
sm_config_set_clkdiv(pio_sm_config * c,float div)248 static inline void sm_config_set_clkdiv(pio_sm_config *c, float div) {
249 uint16_t div_int;
250 uint8_t div_frac;
251 pio_calculate_clkdiv_from_float(div, &div_int, &div_frac);
252 sm_config_set_clkdiv_int_frac(c, div_int, div_frac);
253 }
254
255 /*! \brief Set the wrap addresses in a state machine configuration
256 * \ingroup sm_config
257 *
258 * \param c Pointer to the configuration structure to modify
259 * \param wrap_target the instruction memory address to wrap to
260 * \param wrap the instruction memory address after which to set the program counter to wrap_target
261 * if the instruction does not itself update the program_counter
262 */
sm_config_set_wrap(pio_sm_config * c,uint wrap_target,uint wrap)263 static inline void sm_config_set_wrap(pio_sm_config *c, uint wrap_target, uint wrap) {
264 valid_params_if(PIO, wrap < PIO_INSTRUCTION_COUNT);
265 valid_params_if(PIO, wrap_target < PIO_INSTRUCTION_COUNT);
266 c->execctrl = (c->execctrl & ~(PIO_SM0_EXECCTRL_WRAP_TOP_BITS | PIO_SM0_EXECCTRL_WRAP_BOTTOM_BITS)) |
267 (wrap_target << PIO_SM0_EXECCTRL_WRAP_BOTTOM_LSB) |
268 (wrap << PIO_SM0_EXECCTRL_WRAP_TOP_LSB);
269 }
270
271 /*! \brief Set the 'jmp' pin in a state machine configuration
272 * \ingroup sm_config
273 *
274 * \param c Pointer to the configuration structure to modify
275 * \param pin The raw GPIO pin number to use as the source for a `jmp pin` instruction
276 */
sm_config_set_jmp_pin(pio_sm_config * c,uint pin)277 static inline void sm_config_set_jmp_pin(pio_sm_config *c, uint pin) {
278 valid_params_if(PIO, pin < 32);
279 c->execctrl = (c->execctrl & ~PIO_SM0_EXECCTRL_JMP_PIN_BITS) |
280 (pin << PIO_SM0_EXECCTRL_JMP_PIN_LSB);
281 }
282
283 /*! \brief Setup 'in' shifting parameters in a state machine configuration
284 * \ingroup sm_config
285 *
286 * \param c Pointer to the configuration structure to modify
287 * \param shift_right true to shift ISR to right, false to shift ISR to left
288 * \param autopush whether autopush is enabled
289 * \param push_threshold threshold in bits to shift in before auto/conditional re-pushing of the ISR
290 */
sm_config_set_in_shift(pio_sm_config * c,bool shift_right,bool autopush,uint push_threshold)291 static inline void sm_config_set_in_shift(pio_sm_config *c, bool shift_right, bool autopush, uint push_threshold) {
292 valid_params_if(PIO, push_threshold <= 32);
293 c->shiftctrl = (c->shiftctrl &
294 ~(PIO_SM0_SHIFTCTRL_IN_SHIFTDIR_BITS |
295 PIO_SM0_SHIFTCTRL_AUTOPUSH_BITS |
296 PIO_SM0_SHIFTCTRL_PUSH_THRESH_BITS)) |
297 (bool_to_bit(shift_right) << PIO_SM0_SHIFTCTRL_IN_SHIFTDIR_LSB) |
298 (bool_to_bit(autopush) << PIO_SM0_SHIFTCTRL_AUTOPUSH_LSB) |
299 ((push_threshold & 0x1fu) << PIO_SM0_SHIFTCTRL_PUSH_THRESH_LSB);
300 }
301
302 /*! \brief Setup 'out' shifting parameters in a state machine configuration
303 * \ingroup sm_config
304 *
305 * \param c Pointer to the configuration structure to modify
306 * \param shift_right true to shift OSR to right, false to shift OSR to left
307 * \param autopull whether autopull is enabled
308 * \param pull_threshold threshold in bits to shift out before auto/conditional re-pulling of the OSR
309 */
sm_config_set_out_shift(pio_sm_config * c,bool shift_right,bool autopull,uint pull_threshold)310 static inline void sm_config_set_out_shift(pio_sm_config *c, bool shift_right, bool autopull, uint pull_threshold) {
311 valid_params_if(PIO, pull_threshold <= 32);
312 c->shiftctrl = (c->shiftctrl &
313 ~(PIO_SM0_SHIFTCTRL_OUT_SHIFTDIR_BITS |
314 PIO_SM0_SHIFTCTRL_AUTOPULL_BITS |
315 PIO_SM0_SHIFTCTRL_PULL_THRESH_BITS)) |
316 (bool_to_bit(shift_right) << PIO_SM0_SHIFTCTRL_OUT_SHIFTDIR_LSB) |
317 (bool_to_bit(autopull) << PIO_SM0_SHIFTCTRL_AUTOPULL_LSB) |
318 ((pull_threshold & 0x1fu) << PIO_SM0_SHIFTCTRL_PULL_THRESH_LSB);
319 }
320
321 /*! \brief Setup the FIFO joining in a state machine configuration
322 * \ingroup sm_config
323 *
324 * \param c Pointer to the configuration structure to modify
325 * \param join Specifies the join type. \see enum pio_fifo_join
326 */
sm_config_set_fifo_join(pio_sm_config * c,enum pio_fifo_join join)327 static inline void sm_config_set_fifo_join(pio_sm_config *c, enum pio_fifo_join join) {
328 valid_params_if(PIO, join == PIO_FIFO_JOIN_NONE || join == PIO_FIFO_JOIN_TX || join == PIO_FIFO_JOIN_RX);
329 c->shiftctrl = (c->shiftctrl & (uint)~(PIO_SM0_SHIFTCTRL_FJOIN_TX_BITS | PIO_SM0_SHIFTCTRL_FJOIN_RX_BITS)) |
330 (((uint)join) << PIO_SM0_SHIFTCTRL_FJOIN_TX_LSB);
331 }
332
333 /*! \brief Set special 'out' operations in a state machine configuration
334 * \ingroup sm_config
335 *
336 * \param c Pointer to the configuration structure to modify
337 * \param sticky to enable 'sticky' output (i.e. re-asserting most recent OUT/SET pin values on subsequent cycles)
338 * \param has_enable_pin true to enable auxiliary OUT enable pin
339 * \param enable_pin_index pin index for auxiliary OUT enable
340 */
sm_config_set_out_special(pio_sm_config * c,bool sticky,bool has_enable_pin,uint enable_pin_index)341 static inline void sm_config_set_out_special(pio_sm_config *c, bool sticky, bool has_enable_pin, uint enable_pin_index) {
342 c->execctrl = (c->execctrl &
343 (uint)~(PIO_SM0_EXECCTRL_OUT_STICKY_BITS | PIO_SM0_EXECCTRL_INLINE_OUT_EN_BITS |
344 PIO_SM0_EXECCTRL_OUT_EN_SEL_BITS)) |
345 (bool_to_bit(sticky) << PIO_SM0_EXECCTRL_OUT_STICKY_LSB) |
346 (bool_to_bit(has_enable_pin) << PIO_SM0_EXECCTRL_INLINE_OUT_EN_LSB) |
347 ((enable_pin_index << PIO_SM0_EXECCTRL_OUT_EN_SEL_LSB) & PIO_SM0_EXECCTRL_OUT_EN_SEL_BITS);
348 }
349
350 /*! \brief Set source for 'mov status' in a state machine configuration
351 * \ingroup sm_config
352 *
353 * \param c Pointer to the configuration structure to modify
354 * \param status_sel the status operation selector. \see enum pio_mov_status_type
355 * \param status_n parameter for the mov status operation (currently a bit count)
356 */
sm_config_set_mov_status(pio_sm_config * c,enum pio_mov_status_type status_sel,uint status_n)357 static inline void sm_config_set_mov_status(pio_sm_config *c, enum pio_mov_status_type status_sel, uint status_n) {
358 valid_params_if(PIO, status_sel == STATUS_TX_LESSTHAN || status_sel == STATUS_RX_LESSTHAN);
359 c->execctrl = (c->execctrl
360 & ~(PIO_SM0_EXECCTRL_STATUS_SEL_BITS | PIO_SM0_EXECCTRL_STATUS_N_BITS))
361 | ((((uint)status_sel) << PIO_SM0_EXECCTRL_STATUS_SEL_LSB) & PIO_SM0_EXECCTRL_STATUS_SEL_BITS)
362 | ((status_n << PIO_SM0_EXECCTRL_STATUS_N_LSB) & PIO_SM0_EXECCTRL_STATUS_N_BITS);
363 }
364
365
366 /*! \brief Get the default state machine configuration
367 * \ingroup sm_config
368 *
369 * Setting | Default
370 * --------|--------
371 * Out Pins | 32 starting at 0
372 * Set Pins | 0 starting at 0
373 * In Pins (base) | 0
374 * Side Set Pins (base) | 0
375 * Side Set | disabled
376 * Wrap | wrap=31, wrap_to=0
377 * In Shift | shift_direction=right, autopush=false, push_threshold=32
378 * Out Shift | shift_direction=right, autopull=false, pull_threshold=32
379 * Jmp Pin | 0
380 * Out Special | sticky=false, has_enable_pin=false, enable_pin_index=0
381 * Mov Status | status_sel=STATUS_TX_LESSTHAN, n=0
382 *
383 * \return the default state machine configuration which can then be modified.
384 */
pio_get_default_sm_config(void)385 static inline pio_sm_config pio_get_default_sm_config(void) {
386 pio_sm_config c = {0, 0, 0, 0};
387 sm_config_set_clkdiv_int_frac(&c, 1, 0);
388 sm_config_set_wrap(&c, 0, 31);
389 sm_config_set_in_shift(&c, true, false, 32);
390 sm_config_set_out_shift(&c, true, false, 32);
391 return c;
392 }
393
394 /*! \brief Apply a state machine configuration to a state machine
395 * \ingroup hardware_pio
396 *
397 * \param pio Handle to PIO instance; either \ref pio0 or \ref pio1
398 * \param sm State machine index (0..3)
399 * \param config the configuration to apply
400 */
pio_sm_set_config(PIO pio,uint sm,const pio_sm_config * config)401 static inline void pio_sm_set_config(PIO pio, uint sm, const pio_sm_config *config) {
402 check_pio_param(pio);
403 check_sm_param(sm);
404 pio->sm[sm].clkdiv = config->clkdiv;
405 pio->sm[sm].execctrl = config->execctrl;
406 pio->sm[sm].shiftctrl = config->shiftctrl;
407 pio->sm[sm].pinctrl = config->pinctrl;
408 }
409
410 /*! \brief Return the instance number of a PIO instance
411 * \ingroup hardware_pio
412 *
413 * \param pio The PIO instance; either \ref pio0 or \ref pio1
414 * \return the PIO instance number (either 0 or 1)
415 */
pio_get_index(PIO pio)416 static inline uint pio_get_index(PIO pio) {
417 check_pio_param(pio);
418 return pio == pio1 ? 1 : 0;
419 }
420
421 /*! \brief Setup the function select for a GPIO to use output from the given PIO instance
422 * \ingroup hardware_pio
423 *
424 * PIO appears as an alternate function in the GPIO muxing, just like an SPI
425 * or UART. This function configures that multiplexing to connect a given PIO
426 * instance to a GPIO. Note that this is not necessary for a state machine to
427 * be able to read the *input* value from a GPIO, but only for it to set the
428 * output value or output enable.
429 *
430 * \param pio The PIO instance; either \ref pio0 or \ref pio1
431 * \param pin the GPIO pin whose function select to set
432 */
pio_gpio_init(PIO pio,uint pin)433 static inline void pio_gpio_init(PIO pio, uint pin) {
434 check_pio_param(pio);
435 valid_params_if(PIO, pin < 32);
436 gpio_set_function(pin, pio == pio0 ? GPIO_FUNC_PIO0 : GPIO_FUNC_PIO1);
437 }
438
439 /*! \brief Return the DREQ to use for pacing transfers to/from a particular state machine FIFO
440 * \ingroup hardware_pio
441 *
442 * \param pio The PIO instance; either \ref pio0 or \ref pio1
443 * \param sm State machine index (0..3)
444 * \param is_tx true for sending data to the state machine, false for receiving data from the state machine
445 */
pio_get_dreq(PIO pio,uint sm,bool is_tx)446 static inline uint pio_get_dreq(PIO pio, uint sm, bool is_tx) {
447 static_assert(DREQ_PIO0_TX1 == DREQ_PIO0_TX0 + 1, "");
448 static_assert(DREQ_PIO0_TX2 == DREQ_PIO0_TX0 + 2, "");
449 static_assert(DREQ_PIO0_TX3 == DREQ_PIO0_TX0 + 3, "");
450 static_assert(DREQ_PIO0_RX0 == DREQ_PIO0_TX0 + NUM_PIO_STATE_MACHINES, "");
451 static_assert(DREQ_PIO1_RX0 == DREQ_PIO1_TX0 + NUM_PIO_STATE_MACHINES, "");
452 check_pio_param(pio);
453 check_sm_param(sm);
454 return sm + (is_tx ? 0 : NUM_PIO_STATE_MACHINES) + (pio == pio0 ? DREQ_PIO0_TX0 : DREQ_PIO1_TX0);
455 }
456
457 typedef struct pio_program {
458 const uint16_t *instructions;
459 uint8_t length;
460 int8_t origin; // required instruction memory origin or -1
461 } __packed pio_program_t;
462
463 /*! \brief Determine whether the given program can (at the time of the call) be loaded onto the PIO instance
464 * \ingroup hardware_pio
465 *
466 * \param pio The PIO instance; either \ref pio0 or \ref pio1
467 * \param program the program definition
468 * \return true if the program can be loaded; false if there is not suitable space in the instruction memory
469 */
470 bool pio_can_add_program(PIO pio, const pio_program_t *program);
471
472 /*! \brief Determine whether the given program can (at the time of the call) be loaded onto the PIO instance starting at a particular location
473 * \ingroup hardware_pio
474 *
475 * \param pio The PIO instance; either \ref pio0 or \ref pio1
476 * \param program the program definition
477 * \param offset the instruction memory offset wanted for the start of the program
478 * \return true if the program can be loaded at that location; false if there is not space in the instruction memory
479 */
480 bool pio_can_add_program_at_offset(PIO pio, const pio_program_t *program, uint offset);
481
482 /*! \brief Attempt to load the program, panicking if not possible
483 * \ingroup hardware_pio
484 *
485 * \see pio_can_add_program() if you need to check whether the program can be loaded
486 *
487 * \param pio The PIO instance; either \ref pio0 or \ref pio1
488 * \param program the program definition
489 * \return the instruction memory offset the program is loaded at
490 */
491 uint pio_add_program(PIO pio, const pio_program_t *program);
492
493 /*! \brief Attempt to load the program at the specified instruction memory offset, panicking if not possible
494 * \ingroup hardware_pio
495 *
496 * \see pio_can_add_program_at_offset() if you need to check whether the program can be loaded
497 *
498 * \param pio The PIO instance; either \ref pio0 or \ref pio1
499 * \param program the program definition
500 * \param offset the instruction memory offset wanted for the start of the program
501 */
502 void pio_add_program_at_offset(PIO pio, const pio_program_t *program, uint offset);
503
504 /*! \brief Remove a program from a PIO instance's instruction memory
505 * \ingroup hardware_pio
506 *
507 * \param pio The PIO instance; either \ref pio0 or \ref pio1
508 * \param program the program definition
509 * \param loaded_offset the loaded offset returned when the program was added
510 */
511 void pio_remove_program(PIO pio, const pio_program_t *program, uint loaded_offset);
512
513 /*! \brief Clears all of a PIO instance's instruction memory
514 * \ingroup hardware_pio
515 *
516 * \param pio The PIO instance; either \ref pio0 or \ref pio1
517 */
518 void pio_clear_instruction_memory(PIO pio);
519
520 /*! \brief Resets the state machine to a consistent state, and configures it
521 * \ingroup hardware_pio
522 *
523 * This method:
524 * - Disables the state machine (if running)
525 * - Clears the FIFOs
526 * - Applies the configuration specified by 'config'
527 * - Resets any internal state e.g. shift counters
528 * - Jumps to the initial program location given by 'initial_pc'
529 *
530 * The state machine is left disabled on return from this call.
531 *
532 * \param pio The PIO instance; either \ref pio0 or \ref pio1
533 * \param sm State machine index (0..3)
534 * \param initial_pc the initial program memory offset to run from
535 * \param config the configuration to apply (or NULL to apply defaults)
536 */
537 void pio_sm_init(PIO pio, uint sm, uint initial_pc, const pio_sm_config *config);
538
539 /*! \brief Enable or disable a PIO state machine
540 * \ingroup hardware_pio
541 *
542 * \param pio The PIO instance; either \ref pio0 or \ref pio1
543 * \param sm State machine index (0..3)
544 * \param enabled true to enable the state machine; false to disable
545 */
pio_sm_set_enabled(PIO pio,uint sm,bool enabled)546 static inline void pio_sm_set_enabled(PIO pio, uint sm, bool enabled) {
547 check_pio_param(pio);
548 check_sm_param(sm);
549 pio->ctrl = (pio->ctrl & ~(1u << sm)) | (bool_to_bit(enabled) << sm);
550 }
551
552 /*! \brief Enable or disable multiple PIO state machines
553 * \ingroup hardware_pio
554 *
555 * Note that this method just sets the enabled state of the state machine;
556 * if now enabled they continue exactly from where they left off.
557 *
558 * \see pio_enable_sm_mask_in_sync() if you wish to enable multiple state machines
559 * and ensure their clock dividers are in sync.
560 *
561 * \param pio The PIO instance; either \ref pio0 or \ref pio1
562 * \param mask bit mask of state machine indexes to modify the enabled state of
563 * \param enabled true to enable the state machines; false to disable
564 */
pio_set_sm_mask_enabled(PIO pio,uint32_t mask,bool enabled)565 static inline void pio_set_sm_mask_enabled(PIO pio, uint32_t mask, bool enabled) {
566 check_pio_param(pio);
567 check_sm_mask(mask);
568 pio->ctrl = (pio->ctrl & ~mask) | (enabled ? mask : 0u);
569 }
570
571 /*! \brief Restart a state machine with a known state
572 * \ingroup hardware_pio
573 *
574 * This method clears the ISR, shift counters, clock divider counter
575 * pin write flags, delay counter, latched EXEC instruction, and IRQ wait condition.
576 *
577 * \param pio The PIO instance; either \ref pio0 or \ref pio1
578 * \param sm State machine index (0..3)
579 */
pio_sm_restart(PIO pio,uint sm)580 static inline void pio_sm_restart(PIO pio, uint sm) {
581 check_pio_param(pio);
582 check_sm_param(sm);
583 hw_set_bits(&pio->ctrl, 1u << (PIO_CTRL_SM_RESTART_LSB + sm));
584 }
585
586 /*! \brief Restart multiple state machine with a known state
587 * \ingroup hardware_pio
588 *
589 * This method clears the ISR, shift counters, clock divider counter
590 * pin write flags, delay counter, latched EXEC instruction, and IRQ wait condition.
591 *
592 * \param pio The PIO instance; either \ref pio0 or \ref pio1
593 * \param mask bit mask of state machine indexes to modify the enabled state of
594 */
pio_restart_sm_mask(PIO pio,uint32_t mask)595 static inline void pio_restart_sm_mask(PIO pio, uint32_t mask) {
596 check_pio_param(pio);
597 check_sm_mask(mask);
598 hw_set_bits(&pio->ctrl, (mask << PIO_CTRL_SM_RESTART_LSB) & PIO_CTRL_SM_RESTART_BITS);
599 }
600
601 /*! \brief Restart a state machine's clock divider from a phase of 0
602 * \ingroup hardware_pio
603 *
604 * Each state machine's clock divider is a free-running piece of hardware,
605 * that generates a pattern of clock enable pulses for the state machine,
606 * based *only* on the configured integer/fractional divisor. The pattern of
607 * running/halted cycles slows the state machine's execution to some
608 * controlled rate.
609 *
610 * This function clears the divider's integer and fractional phase
611 * accumulators so that it restarts this pattern from the beginning. It is
612 * called automatically by pio_sm_init() but can also be called at a later
613 * time, when you enable the state machine, to ensure precisely consistent
614 * timing each time you load and run a given PIO program.
615 *
616 * More commonly this hardware mechanism is used to synchronise the execution
617 * clocks of multiple state machines -- see pio_clkdiv_restart_sm_mask().
618 *
619 * \param pio The PIO instance; either \ref pio0 or \ref pio1
620 * \param sm State machine index (0..3)
621 */
pio_sm_clkdiv_restart(PIO pio,uint sm)622 static inline void pio_sm_clkdiv_restart(PIO pio, uint sm) {
623 check_pio_param(pio);
624 check_sm_param(sm);
625 hw_set_bits(&pio->ctrl, 1u << (PIO_CTRL_CLKDIV_RESTART_LSB + sm));
626 }
627
628 /*! \brief Restart multiple state machines' clock dividers from a phase of 0.
629 * \ingroup hardware_pio
630 *
631 * Each state machine's clock divider is a free-running piece of hardware,
632 * that generates a pattern of clock enable pulses for the state machine,
633 * based *only* on the configured integer/fractional divisor. The pattern of
634 * running/halted cycles slows the state machine's execution to some
635 * controlled rate.
636 *
637 * This function simultaneously clears the integer and fractional phase
638 * accumulators of multiple state machines' clock dividers. If these state
639 * machines all have the same integer and fractional divisors configured,
640 * their clock dividers will run in precise deterministic lockstep from this
641 * point.
642 *
643 * With their execution clocks synchronised in this way, it is then safe to
644 * e.g. have multiple state machines performing a 'wait irq' on the same flag,
645 * and all clear it on the same cycle.
646 *
647 * Also note that this function can be called whilst state machines are
648 * running (e.g. if you have just changed the clock divisors of some state
649 * machines and wish to resynchronise them), and that disabling a state
650 * machine does not halt its clock divider: that is, if multiple state
651 * machines have their clocks synchronised, you can safely disable and
652 * reenable one of the state machines without losing synchronisation.
653 *
654 * \param pio The PIO instance; either \ref pio0 or \ref pio1
655 * \param mask bit mask of state machine indexes to modify the enabled state of
656 */
pio_clkdiv_restart_sm_mask(PIO pio,uint32_t mask)657 static inline void pio_clkdiv_restart_sm_mask(PIO pio, uint32_t mask) {
658 check_pio_param(pio);
659 check_sm_mask(mask);
660 hw_set_bits(&pio->ctrl, (mask << PIO_CTRL_CLKDIV_RESTART_LSB) & PIO_CTRL_CLKDIV_RESTART_BITS);
661 }
662
663 /*! \brief Enable multiple PIO state machines synchronizing their clock dividers
664 * \ingroup hardware_pio
665 *
666 * This is equivalent to calling both pio_set_sm_mask_enabled() and
667 * pio_clkdiv_restart_sm_mask() on the *same* clock cycle. All state machines
668 * specified by 'mask' are started simultaneously and, assuming they have the
669 * same clock divisors, their divided clocks will stay precisely synchronised.
670 *
671 * \param pio The PIO instance; either \ref pio0 or \ref pio1
672 * \param mask bit mask of state machine indexes to modify the enabled state of
673 */
pio_enable_sm_mask_in_sync(PIO pio,uint32_t mask)674 static inline void pio_enable_sm_mask_in_sync(PIO pio, uint32_t mask) {
675 check_pio_param(pio);
676 check_sm_mask(mask);
677 hw_set_bits(&pio->ctrl,
678 ((mask << PIO_CTRL_CLKDIV_RESTART_LSB) & PIO_CTRL_CLKDIV_RESTART_BITS) |
679 ((mask << PIO_CTRL_SM_ENABLE_LSB) & PIO_CTRL_SM_ENABLE_BITS));
680 }
681
682 /*! \brief PIO interrupt source numbers for pio related IRQs
683 * \ingroup hardware_pio
684 */
685 enum pio_interrupt_source {
686 pis_interrupt0 = PIO_INTR_SM0_LSB,
687 pis_interrupt1 = PIO_INTR_SM1_LSB,
688 pis_interrupt2 = PIO_INTR_SM2_LSB,
689 pis_interrupt3 = PIO_INTR_SM3_LSB,
690 pis_sm0_tx_fifo_not_full = PIO_INTR_SM0_TXNFULL_LSB,
691 pis_sm1_tx_fifo_not_full = PIO_INTR_SM1_TXNFULL_LSB,
692 pis_sm2_tx_fifo_not_full = PIO_INTR_SM2_TXNFULL_LSB,
693 pis_sm3_tx_fifo_not_full = PIO_INTR_SM3_TXNFULL_LSB,
694 pis_sm0_rx_fifo_not_empty = PIO_INTR_SM0_RXNEMPTY_LSB,
695 pis_sm1_rx_fifo_not_empty = PIO_INTR_SM1_RXNEMPTY_LSB,
696 pis_sm2_rx_fifo_not_empty = PIO_INTR_SM2_RXNEMPTY_LSB,
697 pis_sm3_rx_fifo_not_empty = PIO_INTR_SM3_RXNEMPTY_LSB,
698 };
699
700 /*! \brief Enable/Disable a single source on a PIO's IRQ 0
701 * \ingroup hardware_pio
702 *
703 * \param pio The PIO instance; either \ref pio0 or \ref pio1
704 * \param source the source number (see \ref pio_interrupt_source)
705 * \param enabled true to enable IRQ 0 for the source, false to disable.
706 */
pio_set_irq0_source_enabled(PIO pio,enum pio_interrupt_source source,bool enabled)707 static inline void pio_set_irq0_source_enabled(PIO pio, enum pio_interrupt_source source, bool enabled) {
708 check_pio_param(pio);
709 invalid_params_if(PIO, source >= 12);
710 if (enabled)
711 hw_set_bits(&pio->inte0, 1u << source);
712 else
713 hw_clear_bits(&pio->inte0, 1u << source);
714 }
715
716 /*! \brief Enable/Disable a single source on a PIO's IRQ 1
717 * \ingroup hardware_pio
718 *
719 * \param pio The PIO instance; either \ref pio0 or \ref pio1
720 * \param source the source number (see \ref pio_interrupt_source)
721 * \param enabled true to enable IRQ 0 for the source, false to disable.
722 */
pio_set_irq1_source_enabled(PIO pio,enum pio_interrupt_source source,bool enabled)723 static inline void pio_set_irq1_source_enabled(PIO pio, enum pio_interrupt_source source, bool enabled) {
724 check_pio_param(pio);
725 invalid_params_if(PIO, source >= 12);
726 if (enabled)
727 hw_set_bits(&pio->inte1, 1u << source);
728 else
729 hw_clear_bits(&pio->inte1, 1u << source);
730 }
731
732 /*! \brief Enable/Disable multiple sources on a PIO's IRQ 0
733 * \ingroup hardware_pio
734 *
735 * \param pio The PIO instance; either \ref pio0 or \ref pio1
736 * \param source_mask Mask of bits, one for each source number (see \ref pio_interrupt_source) to affect
737 * \param enabled true to enable all the sources specified in the mask on IRQ 0, false to disable all the sources specified in the mask on IRQ 0
738 */
pio_set_irq0_source_mask_enabled(PIO pio,uint32_t source_mask,bool enabled)739 static inline void pio_set_irq0_source_mask_enabled(PIO pio, uint32_t source_mask, bool enabled) {
740 check_pio_param(pio);
741 invalid_params_if(PIO, source_mask > PIO_INTR_BITS);
742 if (enabled) {
743 hw_set_bits(&pio->inte0, source_mask);
744 } else {
745 hw_clear_bits(&pio->inte0, source_mask);
746 }
747 }
748
749 /*! \brief Enable/Disable multiple sources on a PIO's IRQ 1
750 * \ingroup hardware_pio
751 *
752 * \param pio The PIO instance; either \ref pio0 or \ref pio1
753 * \param source_mask Mask of bits, one for each source number (see \ref pio_interrupt_source) to affect
754 * \param enabled true to enable all the sources specified in the mask on IRQ 1, false to disable all the source specified in the mask on IRQ 1
755 */
pio_set_irq1_source_mask_enabled(PIO pio,uint32_t source_mask,bool enabled)756 static inline void pio_set_irq1_source_mask_enabled(PIO pio, uint32_t source_mask, bool enabled) {
757 check_pio_param(pio);
758 invalid_params_if(PIO, source_mask > PIO_INTR_BITS);
759 if (enabled) {
760 hw_set_bits(&pio->inte1, source_mask);
761 } else {
762 hw_clear_bits(&pio->inte1, source_mask);
763 }
764 }
765
766 /*! \brief Enable/Disable a single source on a PIO's specified (0/1) IRQ index
767 * \ingroup hardware_pio
768 *
769 * \param pio The PIO instance; either \ref pio0 or \ref pio1
770 * \param irq_index the IRQ index; either 0 or 1
771 * \param source the source number (see \ref pio_interrupt_source)
772 * \param enabled true to enable the source on the specified IRQ, false to disable.
773 */
pio_set_irqn_source_enabled(PIO pio,uint irq_index,enum pio_interrupt_source source,bool enabled)774 static inline void pio_set_irqn_source_enabled(PIO pio, uint irq_index, enum pio_interrupt_source source, bool enabled) {
775 invalid_params_if(PIO, irq_index > 1);
776 if (irq_index) {
777 pio_set_irq1_source_enabled(pio, source, enabled);
778 } else {
779 pio_set_irq0_source_enabled(pio, source, enabled);
780 }
781 }
782
783 /*! \brief Enable/Disable multiple sources on a PIO's specified (0/1) IRQ index
784 * \ingroup hardware_pio
785 *
786 * \param pio The PIO instance; either \ref pio0 or \ref pio1
787 * \param irq_index the IRQ index; either 0 or 1
788 * \param source_mask Mask of bits, one for each source number (see \ref pio_interrupt_source) to affect
789 * \param enabled true to enable all the sources specified in the mask on the specified IRQ, false to disable all the sources specified in the mask on the specified IRQ
790 */
pio_set_irqn_source_mask_enabled(PIO pio,uint irq_index,uint32_t source_mask,bool enabled)791 static inline void pio_set_irqn_source_mask_enabled(PIO pio, uint irq_index, uint32_t source_mask, bool enabled) {
792 invalid_params_if(PIO, irq_index > 1);
793 if (irq_index) {
794 pio_set_irq1_source_mask_enabled(pio, source_mask, enabled);
795 } else {
796 pio_set_irq0_source_mask_enabled(pio, source_mask, enabled);
797 }
798 }
799
800 /*! \brief Determine if a particular PIO interrupt is set
801 * \ingroup hardware_pio
802 *
803 * \param pio The PIO instance; either \ref pio0 or \ref pio1
804 * \param pio_interrupt_num the PIO interrupt number 0-7
805 * \return true if corresponding PIO interrupt is currently set
806 */
pio_interrupt_get(PIO pio,uint pio_interrupt_num)807 static inline bool pio_interrupt_get(PIO pio, uint pio_interrupt_num) {
808 check_pio_param(pio);
809 invalid_params_if(PIO, pio_interrupt_num >= 8);
810 return pio->irq & (1u << pio_interrupt_num);
811 }
812
813 /*! \brief Clear a particular PIO interrupt
814 * \ingroup hardware_pio
815 *
816 * \param pio The PIO instance; either \ref pio0 or \ref pio1
817 * \param pio_interrupt_num the PIO interrupt number 0-7
818 */
pio_interrupt_clear(PIO pio,uint pio_interrupt_num)819 static inline void pio_interrupt_clear(PIO pio, uint pio_interrupt_num) {
820 check_pio_param(pio);
821 invalid_params_if(PIO, pio_interrupt_num >= 8);
822 pio->irq = (1u << pio_interrupt_num);
823 }
824
825 /*! \brief Return the current program counter for a state machine
826 * \ingroup hardware_pio
827 *
828 * \param pio The PIO instance; either \ref pio0 or \ref pio1
829 * \param sm State machine index (0..3)
830 * \return the program counter
831 */
pio_sm_get_pc(PIO pio,uint sm)832 static inline uint8_t pio_sm_get_pc(PIO pio, uint sm) {
833 check_pio_param(pio);
834 check_sm_param(sm);
835 return (uint8_t) pio->sm[sm].addr;
836 }
837
838 /*! \brief Immediately execute an instruction on a state machine
839 * \ingroup hardware_pio
840 *
841 * This instruction is executed instead of the next instruction in the normal control flow on the state machine.
842 * Subsequent calls to this method replace the previous executed
843 * instruction if it is still running. \see pio_sm_is_exec_stalled() to see if an executed instruction
844 * is still running (i.e. it is stalled on some condition)
845 *
846 * \param pio The PIO instance; either \ref pio0 or \ref pio1
847 * \param sm State machine index (0..3)
848 * \param instr the encoded PIO instruction
849 */
pio_sm_exec(PIO pio,uint sm,uint instr)850 inline static void pio_sm_exec(PIO pio, uint sm, uint instr) {
851 check_pio_param(pio);
852 check_sm_param(sm);
853 pio->sm[sm].instr = instr;
854 }
855
856 /*! \brief Determine if an instruction set by pio_sm_exec() is stalled executing
857 * \ingroup hardware_pio
858 *
859 * \param pio The PIO instance; either \ref pio0 or \ref pio1
860 * \param sm State machine index (0..3)
861 * \return true if the executed instruction is still running (stalled)
862 */
pio_sm_is_exec_stalled(PIO pio,uint sm)863 static inline bool pio_sm_is_exec_stalled(PIO pio, uint sm) {
864 check_pio_param(pio);
865 check_sm_param(sm);
866 return !!(pio->sm[sm].execctrl & PIO_SM0_EXECCTRL_EXEC_STALLED_BITS);
867 }
868
869 /*! \brief Immediately execute an instruction on a state machine and wait for it to complete
870 * \ingroup hardware_pio
871 *
872 * This instruction is executed instead of the next instruction in the normal control flow on the state machine.
873 * Subsequent calls to this method replace the previous executed
874 * instruction if it is still running. \see pio_sm_is_exec_stalled() to see if an executed instruction
875 * is still running (i.e. it is stalled on some condition)
876 *
877 * \param pio The PIO instance; either \ref pio0 or \ref pio1
878 * \param sm State machine index (0..3)
879 * \param instr the encoded PIO instruction
880 */
pio_sm_exec_wait_blocking(PIO pio,uint sm,uint instr)881 static inline void pio_sm_exec_wait_blocking(PIO pio, uint sm, uint instr) {
882 check_pio_param(pio);
883 check_sm_param(sm);
884 pio_sm_exec(pio, sm, instr);
885 while (pio_sm_is_exec_stalled(pio, sm)) tight_loop_contents();
886 }
887
888 /*! \brief Set the current wrap configuration for a state machine
889 * \ingroup hardware_pio
890 *
891 * \param pio The PIO instance; either \ref pio0 or \ref pio1
892 * \param sm State machine index (0..3)
893 * \param wrap_target the instruction memory address to wrap to
894 * \param wrap the instruction memory address after which to set the program counter to wrap_target
895 * if the instruction does not itself update the program_counter
896 */
pio_sm_set_wrap(PIO pio,uint sm,uint wrap_target,uint wrap)897 static inline void pio_sm_set_wrap(PIO pio, uint sm, uint wrap_target, uint wrap) {
898 check_pio_param(pio);
899 check_sm_param(sm);
900 valid_params_if(PIO, wrap < PIO_INSTRUCTION_COUNT);
901 valid_params_if(PIO, wrap_target < PIO_INSTRUCTION_COUNT);
902 pio->sm[sm].execctrl =
903 (pio->sm[sm].execctrl & ~(PIO_SM0_EXECCTRL_WRAP_TOP_BITS | PIO_SM0_EXECCTRL_WRAP_BOTTOM_BITS)) |
904 (wrap_target << PIO_SM0_EXECCTRL_WRAP_BOTTOM_LSB) |
905 (wrap << PIO_SM0_EXECCTRL_WRAP_TOP_LSB);
906 }
907
908 /*! \brief Set the current 'out' pins for a state machine
909 * \ingroup hardware_pio
910 *
911 * Can overlap with the 'in', 'set' and 'sideset' pins
912 *
913 * \param pio The PIO instance; either \ref pio0 or \ref pio1
914 * \param sm State machine index (0..3)
915 * \param out_base 0-31 First pin to set as output
916 * \param out_count 0-32 Number of pins to set.
917 */
pio_sm_set_out_pins(PIO pio,uint sm,uint out_base,uint out_count)918 static inline void pio_sm_set_out_pins(PIO pio, uint sm, uint out_base, uint out_count) {
919 check_pio_param(pio);
920 check_sm_param(sm);
921 valid_params_if(PIO, out_base < 32);
922 valid_params_if(PIO, out_count <= 32);
923 pio->sm[sm].pinctrl = (pio->sm[sm].pinctrl & ~(PIO_SM0_PINCTRL_OUT_BASE_BITS | PIO_SM0_PINCTRL_OUT_COUNT_BITS)) |
924 (out_base << PIO_SM0_PINCTRL_OUT_BASE_LSB) |
925 (out_count << PIO_SM0_PINCTRL_OUT_COUNT_LSB);
926 }
927
928
929 /*! \brief Set the current 'set' pins for a state machine
930 * \ingroup hardware_pio
931 *
932 * Can overlap with the 'in', 'out' and 'sideset' pins
933 *
934 * \param pio The PIO instance; either \ref pio0 or \ref pio1
935 * \param sm State machine index (0..3)
936 * \param set_base 0-31 First pin to set as
937 * \param set_count 0-5 Number of pins to set.
938 */
pio_sm_set_set_pins(PIO pio,uint sm,uint set_base,uint set_count)939 static inline void pio_sm_set_set_pins(PIO pio, uint sm, uint set_base, uint set_count) {
940 check_pio_param(pio);
941 check_sm_param(sm);
942 valid_params_if(PIO, set_base < 32);
943 valid_params_if(PIO, set_count <= 5);
944 pio->sm[sm].pinctrl = (pio->sm[sm].pinctrl & ~(PIO_SM0_PINCTRL_SET_BASE_BITS | PIO_SM0_PINCTRL_SET_COUNT_BITS)) |
945 (set_base << PIO_SM0_PINCTRL_SET_BASE_LSB) |
946 (set_count << PIO_SM0_PINCTRL_SET_COUNT_LSB);
947 }
948
949 /*! \brief Set the current 'in' pins for a state machine
950 * \ingroup hardware_pio
951 *
952 * Can overlap with the 'out', 'set' and 'sideset' pins
953 *
954 * \param pio The PIO instance; either \ref pio0 or \ref pio1
955 * \param sm State machine index (0..3)
956 * \param in_base 0-31 First pin to use as input
957 */
pio_sm_set_in_pins(PIO pio,uint sm,uint in_base)958 static inline void pio_sm_set_in_pins(PIO pio, uint sm, uint in_base) {
959 check_pio_param(pio);
960 check_sm_param(sm);
961 valid_params_if(PIO, in_base < 32);
962 pio->sm[sm].pinctrl = (pio->sm[sm].pinctrl & ~PIO_SM0_PINCTRL_IN_BASE_BITS) |
963 (in_base << PIO_SM0_PINCTRL_IN_BASE_LSB);
964 }
965
966 /*! \brief Set the current 'sideset' pins for a state machine
967 * \ingroup hardware_pio
968 *
969 * Can overlap with the 'in', 'out' and 'set' pins
970 *
971 * \param pio The PIO instance; either \ref pio0 or \ref pio1
972 * \param sm State machine index (0..3)
973 * \param sideset_base 0-31 base pin for 'side set'
974 */
pio_sm_set_sideset_pins(PIO pio,uint sm,uint sideset_base)975 static inline void pio_sm_set_sideset_pins(PIO pio, uint sm, uint sideset_base) {
976 check_pio_param(pio);
977 check_sm_param(sm);
978 valid_params_if(PIO, sideset_base < 32);
979 pio->sm[sm].pinctrl = (pio->sm[sm].pinctrl & ~PIO_SM0_PINCTRL_SIDESET_BASE_BITS) |
980 (sideset_base << PIO_SM0_PINCTRL_SIDESET_BASE_LSB);
981 }
982
983 /*! \brief Write a word of data to a state machine's TX FIFO
984 * \ingroup hardware_pio
985 *
986 * This is a raw FIFO access that does not check for fullness. If the FIFO is
987 * full, the FIFO contents and state are not affected by the write attempt.
988 * Hardware sets the TXOVER sticky flag for this FIFO in FDEBUG, to indicate
989 * that the system attempted to write to a full FIFO.
990 *
991 * \param pio The PIO instance; either \ref pio0 or \ref pio1
992 * \param sm State machine index (0..3)
993 * \param data the 32 bit data value
994 *
995 * \sa pio_sm_put_blocking()
996 */
pio_sm_put(PIO pio,uint sm,uint32_t data)997 static inline void pio_sm_put(PIO pio, uint sm, uint32_t data) {
998 check_pio_param(pio);
999 check_sm_param(sm);
1000 pio->txf[sm] = data;
1001 }
1002
1003 /*! \brief Read a word of data from a state machine's RX FIFO
1004 * \ingroup hardware_pio
1005 *
1006 * This is a raw FIFO access that does not check for emptiness. If the FIFO is
1007 * empty, the hardware ignores the attempt to read from the FIFO (the FIFO
1008 * remains in an empty state following the read) and the sticky RXUNDER flag
1009 * for this FIFO is set in FDEBUG to indicate that the system tried to read
1010 * from this FIFO when empty. The data returned by this function is undefined
1011 * when the FIFO is empty.
1012 *
1013 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1014 * \param sm State machine index (0..3)
1015 *
1016 * \sa pio_sm_get_blocking()
1017 */
pio_sm_get(PIO pio,uint sm)1018 static inline uint32_t pio_sm_get(PIO pio, uint sm) {
1019 check_pio_param(pio);
1020 check_sm_param(sm);
1021 return pio->rxf[sm];
1022 }
1023
1024 /*! \brief Determine if a state machine's RX FIFO is full
1025 * \ingroup hardware_pio
1026 *
1027 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1028 * \param sm State machine index (0..3)
1029 * \return true if the RX FIFO is full
1030 */
pio_sm_is_rx_fifo_full(PIO pio,uint sm)1031 static inline bool pio_sm_is_rx_fifo_full(PIO pio, uint sm) {
1032 check_pio_param(pio);
1033 check_sm_param(sm);
1034 return (pio->fstat & (1u << (PIO_FSTAT_RXFULL_LSB + sm))) != 0;
1035 }
1036
1037 /*! \brief Determine if a state machine's RX FIFO is empty
1038 * \ingroup hardware_pio
1039 *
1040 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1041 * \param sm State machine index (0..3)
1042 * \return true if the RX FIFO is empty
1043 */
pio_sm_is_rx_fifo_empty(PIO pio,uint sm)1044 static inline bool pio_sm_is_rx_fifo_empty(PIO pio, uint sm) {
1045 check_pio_param(pio);
1046 check_sm_param(sm);
1047 return (pio->fstat & (1u << (PIO_FSTAT_RXEMPTY_LSB + sm))) != 0;
1048 }
1049
1050 /*! \brief Return the number of elements currently in a state machine's RX FIFO
1051 * \ingroup hardware_pio
1052 *
1053 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1054 * \param sm State machine index (0..3)
1055 * \return the number of elements in the RX FIFO
1056 */
pio_sm_get_rx_fifo_level(PIO pio,uint sm)1057 static inline uint pio_sm_get_rx_fifo_level(PIO pio, uint sm) {
1058 check_pio_param(pio);
1059 check_sm_param(sm);
1060 uint bitoffs = PIO_FLEVEL_RX0_LSB + sm * (PIO_FLEVEL_RX1_LSB - PIO_FLEVEL_RX0_LSB);
1061 const uint32_t mask = PIO_FLEVEL_RX0_BITS >> PIO_FLEVEL_RX0_LSB;
1062 return (pio->flevel >> bitoffs) & mask;
1063 }
1064
1065 /*! \brief Determine if a state machine's TX FIFO is full
1066 * \ingroup hardware_pio
1067 *
1068 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1069 * \param sm State machine index (0..3)
1070 * \return true if the TX FIFO is full
1071 */
pio_sm_is_tx_fifo_full(PIO pio,uint sm)1072 static inline bool pio_sm_is_tx_fifo_full(PIO pio, uint sm) {
1073 check_pio_param(pio);
1074 check_sm_param(sm);
1075 return (pio->fstat & (1u << (PIO_FSTAT_TXFULL_LSB + sm))) != 0;
1076 }
1077
1078 /*! \brief Determine if a state machine's TX FIFO is empty
1079 * \ingroup hardware_pio
1080 *
1081 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1082 * \param sm State machine index (0..3)
1083 * \return true if the TX FIFO is empty
1084 */
pio_sm_is_tx_fifo_empty(PIO pio,uint sm)1085 static inline bool pio_sm_is_tx_fifo_empty(PIO pio, uint sm) {
1086 check_pio_param(pio);
1087 check_sm_param(sm);
1088 return (pio->fstat & (1u << (PIO_FSTAT_TXEMPTY_LSB + sm))) != 0;
1089 }
1090
1091 /*! \brief Return the number of elements currently in a state machine's TX FIFO
1092 * \ingroup hardware_pio
1093 *
1094 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1095 * \param sm State machine index (0..3)
1096 * \return the number of elements in the TX FIFO
1097 */
pio_sm_get_tx_fifo_level(PIO pio,uint sm)1098 static inline uint pio_sm_get_tx_fifo_level(PIO pio, uint sm) {
1099 check_pio_param(pio);
1100 check_sm_param(sm);
1101 unsigned int bitoffs = PIO_FLEVEL_TX0_LSB + sm * (PIO_FLEVEL_TX1_LSB - PIO_FLEVEL_TX0_LSB);
1102 const uint32_t mask = PIO_FLEVEL_TX0_BITS >> PIO_FLEVEL_TX0_LSB;
1103 return (pio->flevel >> bitoffs) & mask;
1104 }
1105
1106 /*! \brief Write a word of data to a state machine's TX FIFO, blocking if the FIFO is full
1107 * \ingroup hardware_pio
1108 *
1109 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1110 * \param sm State machine index (0..3)
1111 * \param data the 32 bit data value
1112 */
pio_sm_put_blocking(PIO pio,uint sm,uint32_t data)1113 static inline void pio_sm_put_blocking(PIO pio, uint sm, uint32_t data) {
1114 check_pio_param(pio);
1115 check_sm_param(sm);
1116 while (pio_sm_is_tx_fifo_full(pio, sm)) tight_loop_contents();
1117 pio_sm_put(pio, sm, data);
1118 }
1119
1120 /*! \brief Read a word of data from a state machine's RX FIFO, blocking if the FIFO is empty
1121 * \ingroup hardware_pio
1122 *
1123 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1124 * \param sm State machine index (0..3)
1125 */
pio_sm_get_blocking(PIO pio,uint sm)1126 static inline uint32_t pio_sm_get_blocking(PIO pio, uint sm) {
1127 check_pio_param(pio);
1128 check_sm_param(sm);
1129 while (pio_sm_is_rx_fifo_empty(pio, sm)) tight_loop_contents();
1130 return pio_sm_get(pio, sm);
1131 }
1132
1133 /*! \brief Empty out a state machine's TX FIFO
1134 * \ingroup hardware_pio
1135 *
1136 * This method executes `pull` instructions on the state machine until the TX
1137 * FIFO is empty. This disturbs the contents of the OSR, so see also
1138 * pio_sm_clear_fifos() which clears both FIFOs but leaves the state machine's
1139 * internal state undisturbed.
1140 *
1141 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1142 * \param sm State machine index (0..3)
1143 *
1144 * \sa pio_sm_clear_fifos()
1145 */
1146 void pio_sm_drain_tx_fifo(PIO pio, uint sm);
1147
1148 /*! \brief set the current clock divider for a state machine using a 16:8 fraction
1149 * \ingroup hardware_pio
1150 *
1151 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1152 * \param sm State machine index (0..3)
1153 * \param div_int the integer part of the clock divider
1154 * \param div_frac the fractional part of the clock divider in 1/256s
1155 */
pio_sm_set_clkdiv_int_frac(PIO pio,uint sm,uint16_t div_int,uint8_t div_frac)1156 static inline void pio_sm_set_clkdiv_int_frac(PIO pio, uint sm, uint16_t div_int, uint8_t div_frac) {
1157 check_pio_param(pio);
1158 check_sm_param(sm);
1159 invalid_params_if(PIO, div_int == 0 && div_frac != 0);
1160 pio->sm[sm].clkdiv =
1161 (((uint)div_frac) << PIO_SM0_CLKDIV_FRAC_LSB) |
1162 (((uint)div_int) << PIO_SM0_CLKDIV_INT_LSB);
1163 }
1164
1165 /*! \brief set the current clock divider for a state machine
1166 * \ingroup hardware_pio
1167 *
1168 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1169 * \param sm State machine index (0..3)
1170 * \param div the floating point clock divider
1171 */
pio_sm_set_clkdiv(PIO pio,uint sm,float div)1172 static inline void pio_sm_set_clkdiv(PIO pio, uint sm, float div) {
1173 check_pio_param(pio);
1174 check_sm_param(sm);
1175 uint16_t div_int;
1176 uint8_t div_frac;
1177 pio_calculate_clkdiv_from_float(div, &div_int, &div_frac);
1178 pio_sm_set_clkdiv_int_frac(pio, sm, div_int, div_frac);
1179 }
1180
1181 /*! \brief Clear a state machine's TX and RX FIFOs
1182 * \ingroup hardware_pio
1183 *
1184 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1185 * \param sm State machine index (0..3)
1186 */
pio_sm_clear_fifos(PIO pio,uint sm)1187 static inline void pio_sm_clear_fifos(PIO pio, uint sm) {
1188 // changing the FIFO join state clears the fifo
1189 check_pio_param(pio);
1190 check_sm_param(sm);
1191 hw_xor_bits(&pio->sm[sm].shiftctrl, PIO_SM0_SHIFTCTRL_FJOIN_RX_BITS);
1192 hw_xor_bits(&pio->sm[sm].shiftctrl, PIO_SM0_SHIFTCTRL_FJOIN_RX_BITS);
1193 }
1194
1195 /*! \brief Use a state machine to set a value on all pins for the PIO instance
1196 * \ingroup hardware_pio
1197 *
1198 * This method repeatedly reconfigures the target state machine's pin configuration and executes 'set' instructions to set values on all 32 pins,
1199 * before restoring the state machine's pin configuration to what it was.
1200 *
1201 * This method is provided as a convenience to set initial pin states, and should not be used against a state machine that is enabled.
1202 *
1203 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1204 * \param sm State machine index (0..3) to use
1205 * \param pin_values the pin values to set
1206 */
1207 void pio_sm_set_pins(PIO pio, uint sm, uint32_t pin_values);
1208
1209 /*! \brief Use a state machine to set a value on multiple pins for the PIO instance
1210 * \ingroup hardware_pio
1211 *
1212 * This method repeatedly reconfigures the target state machine's pin configuration and executes 'set' instructions to set values on up to 32 pins,
1213 * before restoring the state machine's pin configuration to what it was.
1214 *
1215 * This method is provided as a convenience to set initial pin states, and should not be used against a state machine that is enabled.
1216 *
1217 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1218 * \param sm State machine index (0..3) to use
1219 * \param pin_values the pin values to set (if the corresponding bit in pin_mask is set)
1220 * \param pin_mask a bit for each pin to indicate whether the corresponding pin_value for that pin should be applied.
1221 */
1222 void pio_sm_set_pins_with_mask(PIO pio, uint sm, uint32_t pin_values, uint32_t pin_mask);
1223
1224 /*! \brief Use a state machine to set the pin directions for multiple pins for the PIO instance
1225 * \ingroup hardware_pio
1226 *
1227 * This method repeatedly reconfigures the target state machine's pin configuration and executes 'set' instructions to set pin directions on up to 32 pins,
1228 * before restoring the state machine's pin configuration to what it was.
1229 *
1230 * This method is provided as a convenience to set initial pin directions, and should not be used against a state machine that is enabled.
1231 *
1232 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1233 * \param sm State machine index (0..3) to use
1234 * \param pin_dirs the pin directions to set - 1 = out, 0 = in (if the corresponding bit in pin_mask is set)
1235 * \param pin_mask a bit for each pin to indicate whether the corresponding pin_value for that pin should be applied.
1236 */
1237 void pio_sm_set_pindirs_with_mask(PIO pio, uint sm, uint32_t pin_dirs, uint32_t pin_mask);
1238
1239 /*! \brief Use a state machine to set the same pin direction for multiple consecutive pins for the PIO instance
1240 * \ingroup hardware_pio
1241 *
1242 * This method repeatedly reconfigures the target state machine's pin configuration and executes 'set' instructions to set the pin direction on consecutive pins,
1243 * before restoring the state machine's pin configuration to what it was.
1244 *
1245 * This method is provided as a convenience to set initial pin directions, and should not be used against a state machine that is enabled.
1246 *
1247 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1248 * \param sm State machine index (0..3) to use
1249 * \param pin_base the first pin to set a direction for
1250 * \param pin_count the count of consecutive pins to set the direction for
1251 * \param is_out the direction to set; true = out, false = in
1252 */
1253 void pio_sm_set_consecutive_pindirs(PIO pio, uint sm, uint pin_base, uint pin_count, bool is_out);
1254
1255 /*! \brief Mark a state machine as used
1256 * \ingroup hardware_pio
1257 *
1258 * Method for cooperative claiming of hardware. Will cause a panic if the state machine
1259 * is already claimed. Use of this method by libraries detects accidental
1260 * configurations that would fail in unpredictable ways.
1261 *
1262 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1263 * \param sm State machine index (0..3)
1264 */
1265 void pio_sm_claim(PIO pio, uint sm);
1266
1267 /*! \brief Mark multiple state machines as used
1268 * \ingroup hardware_pio
1269 *
1270 * Method for cooperative claiming of hardware. Will cause a panic if any of the state machines
1271 * are already claimed. Use of this method by libraries detects accidental
1272 * configurations that would fail in unpredictable ways.
1273 *
1274 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1275 * \param sm_mask Mask of state machine indexes
1276 */
1277 void pio_claim_sm_mask(PIO pio, uint sm_mask);
1278
1279 /*! \brief Mark a state machine as no longer used
1280 * \ingroup hardware_pio
1281 *
1282 * Method for cooperative claiming of hardware.
1283 *
1284 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1285 * \param sm State machine index (0..3)
1286 */
1287 void pio_sm_unclaim(PIO pio, uint sm);
1288
1289 /*! \brief Claim a free state machine on a PIO instance
1290 * \ingroup hardware_pio
1291 *
1292 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1293 * \param required if true the function will panic if none are available
1294 * \return the state machine index or -1 if required was false, and none were free
1295 */
1296 int pio_claim_unused_sm(PIO pio, bool required);
1297
1298 /*! \brief Determine if a PIO state machine is claimed
1299 * \ingroup hardware_pio
1300 *
1301 * \param pio The PIO instance; either \ref pio0 or \ref pio1
1302 * \param sm State machine index (0..3)
1303 * \return true if claimed, false otherwise
1304 * \see pio_sm_claim
1305 * \see pio_claim_sm_mask
1306 */
1307 bool pio_sm_is_claimed(PIO pio, uint sm);
1308
1309 #ifdef __cplusplus
1310 }
1311 #endif
1312
1313 #endif // _PIO_H_
1314