Loading...
Searching...
No Matches
pio.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: 2021 Otto-von-Guericke-Universität Magdeburg
3 * SPDX-License-Identifier: LGPL-2.1-only
4 */
5
6#pragma once
7
17
18#include "periph_conf.h"
19#include "periph/gpio.h"
20#include "periph/pio.h"
21
22#ifdef __cplusplus
23extern "C" {
24#endif
25
26#if defined(CPU_MODEL_RP2040) || defined(DOXYGEN)
30#define PIO_SM_NUMOF 4
31
35#define PIO_INSTR_NUMOF 32
36
40#define PIO_IRQ_NUMOF 8
41#endif
42
46typedef uint16_t pio_instr_t;
47
53#define PIO_GPIO_INIT_HIGH(pin) (((gpio_t)(1)) << (pin))
54
60#define PIO_GPIO_INIT_LOW(pin) ((gpio_t)0)
61
67#define PIO_GPIO_INIT_OUT(pin) (((gpio_t)(1)) << (pin))
68
74#define PIO_GPIO_INIT_IN(pin) ((gpio_t)0)
75
87
95#define PIO_INST_JMP (0u << 13)
99#define PIO_INST_JMP_MASK (7u << 13)
103#define PIO_INST_JMP_CONDITION_SHIFT 5
107#define PIO_INST_JMP_CONDITION_MASK (7u << PIO_INST_JMP_CONDITION_SHIFT)
111#define PIO_INST_JMP_ADDRESS_MASK (31u)
125
128#define PIO_INST_WAIT (1u << 13)
132#define PIO_INST_WAIT_MASK (7u << 13)
136#define PIO_INST_WAIT_POL_SHIFT 7
140#define PIO_INST_WAIT_POL_MASK (1u << PIO_INST_WAIT_POL_SHIFT)
144#define PIO_INST_WAIT_SOURCE_SHIFT 5
148#define PIO_INST_WAIT_SOURCE_MASK (3u << PIO_INST_WAIT_SOURCE_SHIFT)
152#define PIO_INST_WAIT_INDEX_MASK (31u)
160
168
171#define PIO_INST_IN (2u << 13)
175#define PIO_INST_IN_MASK (7u << 13)
179#define PIO_INST_IN_SOURCE_SHIFT 5
183#define PIO_INST_IN_SOURCE_MASK (7u << PIO_INST_IN_SOURCE_SHIFT)
187#define PIO_INST_IN_BIT_COUNT_MASK (31u)
199
202#define PIO_INST_OUT (3u << 13)
206#define PIO_INST_OUT_MASK (7u << 13)
210#define PIO_INST_OUT_DESTINATION_SHIFT 5
214#define PIO_INST_OUT_DESTINATION_MASK (7u << PIO_INST_OUT_DESTINATION_SHIFT)
218#define PIO_INST_OUT_BIT_COUNT_MASK (31u)
232
235#define PIO_INST_PUSH (4u << 13)
239#define PIO_INST_PUSH_MASK ((7u << 13) | (1u << 7) | 31u)
243#define PIO_INST_PUSH_IF_FULL_SHIFT 6
247#define PIO_INST_PUSH_IF_FULL_MASK (1u << PIO_INST_PUSH_IF_FULL_SHIFT)
251#define PIO_INST_PUSH_BLOCK_SHIFT 5
255#define PIO_INST_PUSH_BLOCK_MASK (1u << PIO_INST_PUSH_BLOCK_SHIFT)
259#define PIO_INST_PULL ((4u << 13) | (1u << 7))
263#define PIO_INST_PULL_MASK ((7u << 13) | (1u << 7) | 31u)
267#define PIO_INST_PULL_IF_EMPTY_SHIFT 6
271#define PIO_INST_PULL_IF_EMPTY_MASK (1u << PIO_INST_PULL_IF_EMPTY_SHIFT)
275#define PIO_INST_PULL_BLOCK_SHIFT 5
279#define PIO_INST_PULL_BLOCK_MASK (1u << PIO_INST_PULL_BLOCK_SHIFT)
283#define PIO_INST_MOV (5u << 13)
287#define PIO_INST_MOV_MASK (7u << 13)
291#define PIO_INST_MOV_DESTINATION_SHIFT 5
295#define PIO_INST_MOV_DESTINATION_MASK (7u << PIO_INST_MOV_DESTINATION_SHIFT)
299#define PIO_INST_MOV_OP_SHIFT 3
303#define PIO_INST_MOV_OP_MASK (3u << PIO_INST_MOV_OP_SHIFT)
307#define PIO_INST_MOV_SOURCE_SHIFT 0
311#define PIO_INST_MOV_SOURCE_MASK (7u)
324
332
344
347#define PIO_INST_IRQ (6u << 13)
351#define PIO_INST_IRQ_MASK ((7u << 13) | (1u << 7))
355#define PIO_INST_IRQ_CLR_SHIFT 6
359#define PIO_INST_IRQ_CLR_MASK (1u << PIO_INST_IRQ_CLR_SHIFT)
363#define PIO_INST_IRQ_WAIT_SHIFT 5
367#define PIO_INST_IRQ_WAIT_MASK (1u << PIO_INST_IRQ_WAIT_SHIFT)
371#define PIO_INST_IRQ_INDEX_MASK (31u)
375#define PIO_INST_SET (7u << 13)
379#define PIO_INST_SET_MASK (7u << 13)
383#define PIO_INST_SET_DESTINATION_SHIFT 5
387#define PIO_INST_SET_DESTINATION_MASK (7u << PIO_INST_SET_DESTINATION_SHIFT)
391#define PIO_INST_SET_DATA_MASK (31u)
401
411 unsigned address)
412{
413 return PIO_INST_JMP |
414 (((unsigned)condition) << PIO_INST_JMP_CONDITION_SHIFT) |
415 address;
416}
417
428 pio_inst_wait_src_t source,
429 bool relative,
430 unsigned index)
431{
432 return PIO_INST_WAIT |
433 ((unsigned)polarity << PIO_INST_WAIT_POL_SHIFT) |
434 ((unsigned)source << PIO_INST_WAIT_SOURCE_SHIFT) |
435 ((!!relative) << 4) | index;
436}
437
446 unsigned bit_count)
447{
448 return PIO_INST_IN |
449 ((unsigned)source << PIO_INST_IN_SOURCE_SHIFT) |
450 (bit_count % 32);
451}
452
461 unsigned bit_count)
462{
463 return PIO_INST_OUT |
464 ((unsigned)destination << PIO_INST_OUT_DESTINATION_SHIFT) |
465 (bit_count % 32);
466}
467
475static inline pio_instr_t pio_inst_push(bool if_full,
476 bool block)
477{
478 return PIO_INST_PUSH |
479 ((!!if_full) << PIO_INST_PUSH_IF_FULL_SHIFT) |
480 ((!!block) << PIO_INST_PUSH_BLOCK_SHIFT);
481}
482
490static inline pio_instr_t pio_inst_pull(bool if_empty,
491 bool block)
492{
493 return PIO_INST_PULL |
494 ((!!if_empty) << PIO_INST_PULL_IF_EMPTY_SHIFT) |
495 ((!!block) << PIO_INST_PULL_BLOCK_SHIFT);
496}
497
507 pio_inst_mov_op_t operation,
508 pio_inst_mov_src_t source)
509{
510 return PIO_INST_MOV |
511 ((unsigned)destination << PIO_INST_MOV_DESTINATION_SHIFT) |
512 ((unsigned)operation << PIO_INST_MOV_OP_SHIFT) |
513 ((unsigned)source << PIO_INST_MOV_SOURCE_SHIFT);
514}
515
525static inline pio_instr_t pio_inst_irq(bool clear,
526 bool wait,
527 bool relative,
528 unsigned index)
529{
530 return PIO_INST_IRQ |
531 (!!clear << PIO_INST_IRQ_CLR_SHIFT) |
532 (!!wait << PIO_INST_IRQ_WAIT_SHIFT) |
533 ((!!relative << 4) | index);
534}
535
544 unsigned data)
545{
546 return PIO_INST_SET |
547 ((unsigned)destination << PIO_INST_SET_DESTINATION_SHIFT) |
548 data;
549}
550
561static inline pio_instr_t pio_inst_delay_sideset(unsigned sideset,
562 unsigned sideset_count,
563 bool sideset_opt,
564 unsigned delay)
565{
566 return ((!!sideset_opt) << 12) |
567 (sideset << (13 - (!!sideset_opt + sideset_count))) |
568 (delay << 8);
569}
570
571
575typedef struct pio_sm_ctrl_regs {
576 volatile uint32_t clkdiv;
577 volatile uint32_t execctrl;
578 volatile uint32_t shiftctrl;
579 const volatile uint32_t addr;
580 volatile uint32_t instr;
581 volatile uint32_t pinctrl;
583
587#define PIO_SM_CTRL_BASE(dev) ((pio_sm_ctrl_regs_t *)(&((dev)->SM0_CLKDIV)))
588
592#define PIO_SM_CLKDIV_MAX 65536
593
597#define PIO_SM_MASK(sm) (1u << (sm))
598
602#define PIO_IRQ_MASK(irq) (1u << (irq))
603
636static inline unsigned pio_irq_rel_index(unsigned irq_abs, unsigned sm) {
637 assert(irq_abs < PIO_IRQ_NUMOF);
638 assert(sm < PIO_SM_NUMOF);
639 return (irq_abs & 0b100u) | ((irq_abs + sm) & 0b011u);
640}
641
652static inline unsigned pio_irq_rel_sm(unsigned irq_abs, unsigned irq_rel) {
653 assert(irq_abs < PIO_IRQ_NUMOF);
654 assert(irq_rel < PIO_IRQ_NUMOF);
655 return (irq_abs & 0b100u) | ((irq_rel - irq_abs) & 0b011u);
656}
657
661#define PIO_IRQ_REL_MASK(irq, sm) PIO_IRQ_MASK(pio_irq_rel_index(irq, sm))
662
666typedef enum {
667 /* lower 4 hardware IRQs which are issued with 'irq <0-3> [rel]' and are routed to NVIC */
668 /* This does not encode the state machine index of which state machine fired! */
669 PIO_IRQ_SM_3 = PIO0_INTR_SM3_Msk,
670 PIO_IRQ_SM_2 = PIO0_INTR_SM2_Msk,
671 PIO_IRQ_SM_1 = PIO0_INTR_SM1_Msk,
672 PIO_IRQ_SM_0 = PIO0_INTR_SM0_Msk,
673 /* FIFO interrupts per state machine */
674 PIO_IRQ_TXNFULL_SM3 = PIO0_INTR_SM3_TXNFULL_Msk,
675 PIO_IRQ_TXNFULL_SM2 = PIO0_INTR_SM2_TXNFULL_Msk,
676 PIO_IRQ_TXNFULL_SM1 = PIO0_INTR_SM1_TXNFULL_Msk,
677 PIO_IRQ_TXNFULL_SM0 = PIO0_INTR_SM0_TXNFULL_Msk,
678 PIO_IRQ_RXNEMPTY_SM3 = PIO0_INTR_SM3_RXNEMPTY_Msk,
679 PIO_IRQ_RXNEMPTY_SM2 = PIO0_INTR_SM2_RXNEMPTY_Msk,
680 PIO_IRQ_RXNEMPTY_SM1 = PIO0_INTR_SM1_RXNEMPTY_Msk,
681 PIO_IRQ_RXNEMPTY_SM0 = PIO0_INTR_SM0_RXNEMPTY_Msk,
682 /* all routed state machine interrupts */
686 PIO_IRQ_SM_0),
687 /* all interrupts */
698
707
719
723typedef struct pio_isr_vec {
730 void (*tx_ready)(pio_t pio, pio_sm_t sm);
737 void (*rx_ready)(pio_t pio, pio_sm_t sm);
739
743typedef struct pio_isr_sm_vec {
750 void (*sm)(pio_t pio, unsigned irq);
752
764
768typedef struct pio_sm_clkdiv {
769 uint16_t div;
770 uint8_t frac_100;
772
788
801
812 int pio_write_program(pio_t pio, pio_program_t *prog, const pio_instr_t *instr);
813
821
829
837void pio_set_isr_vec(pio_t pio, pio_sm_t sm, const pio_isr_vec_t *vec);
838
846void pio_set_isr_sm_vec(pio_t pio, unsigned irq, const pio_isr_sm_vec_t *vec);
847
859void pio_sm_set_out_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base, unsigned pin_count);
860
870void pio_sm_set_in_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base);
871
883void pio_sm_set_set_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base, unsigned pin_count);
884
893void pio_sm_set_sideset_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base);
894
907void pio_sm_set_sideset_count(pio_t pio, pio_sm_t sm, unsigned pin_count, bool enable);
908
916void pio_sm_set_sideset_target(pio_t pio, pio_sm_t sm, bool pindir);
917
929
943
953void pio_sm_clkdiv_restart(pio_t pio, unsigned sm_mask);
954
964void pio_sm_set_wrap(pio_t pio, pio_sm_t sm, unsigned prog_loc, uint8_t top, uint8_t bottom);
965
975void pio_sm_set_jmp_pin(pio_t pio, pio_sm_t sm, gpio_t pin);
976
986void pio_sm_set_in_shift(pio_t pio, pio_sm_t sm, bool right, bool autopush, unsigned threshold);
987
997void pio_sm_set_out_shift(pio_t pio, pio_sm_t sm, bool right, bool autopull, unsigned threshold);
998
1006
1014
1022
1030
1039
1048
1062int pio_sm_transmit_word(pio_t pio, pio_sm_t sm, uint32_t word);
1063
1073void pio_sm_transmit_word_block(pio_t pio, pio_sm_t sm, uint32_t word);
1074
1085void pio_sm_transmit_words_block(pio_t pio, pio_sm_t sm, const uint32_t *words, unsigned count);
1086
1100int pio_sm_receive_word(pio_t pio, pio_sm_t sm, uint32_t *word);
1101
1111void pio_sm_receive_word_block(pio_t pio, pio_sm_t sm, uint32_t *word);
1112
1123void pio_sm_receive_words_block(pio_t pio, pio_sm_t sm, uint32_t *words, unsigned count);
1124
1132uint32_t pio_irq_get(pio_t pio);
1133
1140void pio_irq_clear(pio_t pio, unsigned irq_flags);
1141
1157 const pio_program_conf_t *conf);
1158
1169void pio_sm_set_pindirs_with_mask(pio_t pio, pio_sm_t sm, gpio_t values, gpio_t mask);
1170
1181void pio_sm_set_pins_with_mask(pio_t pio, pio_sm_t sm, gpio_t values, gpio_t mask);
1182
1192
1202
1212
1219
1226
1233static inline void pio_sm_clear_debug_txstall(pio_t pio, unsigned sm_mask)
1234{
1235 io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1236 ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_TXSTALL_Pos));
1237}
1238
1245static inline void pio_sm_clear_debug_txover(pio_t pio, unsigned sm_mask)
1246{
1247 io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1248 ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_TXOVER_Pos));
1249}
1250
1257static inline void pio_sm_clear_debug_rxunder(pio_t pio, unsigned sm_mask)
1258{
1259 io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1260 ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_RXUNDER_Pos));
1261}
1262
1269static inline void pio_sm_clear_debug_rxstall(pio_t pio, unsigned sm_mask)
1270{
1271 io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1272 ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_RXSTALL_Pos));
1273}
1274
1283static inline bool pio_sm_tx_fifo_empty(pio_t pio, pio_sm_t sm)
1284{
1285 return !!(pio_config[pio].dev->FSTAT & ((1u << sm) << PIO0_FSTAT_TXEMPTY_Pos));
1286}
1287
1296static inline bool pio_sm_tx_fifo_full(pio_t pio, pio_sm_t sm)
1297{
1298 return !!(pio_config[pio].dev->FSTAT & ((1u << sm) << PIO0_FSTAT_TXFULL_Pos));
1299}
1300
1309static inline bool pio_sm_rx_fifo_empty(pio_t pio, pio_sm_t sm)
1310{
1311 return !!(pio_config[pio].dev->FSTAT & ((1u << sm) << PIO0_FSTAT_RXEMPTY_Pos));
1312}
1313
1322static inline bool pio_sm_rx_fifo_full(pio_t pio, pio_sm_t sm)
1323{
1324 return !!((pio_config[pio].dev)->FSTAT & ((1u << sm) << PIO0_FSTAT_RXFULL_Pos));
1325}
1326
1327#ifdef __cplusplus
1328}
1329#endif
1330
#define assert(cond)
abort the program if assertion is false
Definition assert.h:146
static const pio_conf_t pio_config[]
Array of PIO configurations.
static unsigned pio_irq_rel_index(unsigned irq_abs, unsigned sm)
Convert absolute IRQ index to relative IRQ index.
Definition pio.h:636
uint16_t pio_instr_t
Type to represent the width of an instruction.
Definition pio.h:46
static pio_instr_t pio_inst_jmp(pio_inst_jmp_cond_t condition, unsigned address)
Construct a JMP instruction.
Definition pio.h:410
void pio_sm_clear_fifos(pio_t pio, pio_sm_t sm)
Drop all pending words in the tx and rx FIFO.
static void pio_sm_clear_debug_txover(pio_t pio, unsigned sm_mask)
Clear TX overflow debug flag.
Definition pio.h:1245
pio_inst_jmp_cond_t
JMP conditions.
Definition pio.h:115
@ PIO_INST_JMP_COND_NOT_X_EQ_Y
If scratch X != scratch Y.
Definition pio.h:121
@ PIO_INST_JMP_COND_NONE
Unconditionally.
Definition pio.h:116
@ PIO_INST_JMP_COND_PIN
If jump pin is high.
Definition pio.h:122
@ PIO_INST_JMP_COND_X_DEC
If scratch X– is not 0.
Definition pio.h:118
@ PIO_INST_JMP_COND_X_ZERO
If scratch X is 0.
Definition pio.h:117
@ PIO_INST_JMP_COND_Y_ZERO
If scratch Y is 0.
Definition pio.h:119
@ PIO_INST_JMP_COND_Y_DEC
If scratch Y– is not 0.
Definition pio.h:120
@ PIO_INST_JMP_COND_NOT_OSR_EMPTY
If OSR is not empty.
Definition pio.h:123
struct pio_sm_ctrl_regs pio_sm_ctrl_regs_t
Internal state machine registers.
void pio_sm_set_sideset_pins_init(pio_t pio, pio_sm_t sm, const pio_gpio_init_t *pin_init)
Set pins affected by sideset instructions and initialize the pins as PIO pins and a state according t...
void pio_sm_set_sideset_count(pio_t pio, pio_sm_t sm, unsigned pin_count, bool enable)
Configure how many pins are sideset pins and whether a sideset is optional or required for every inst...
#define PIO_SM_NUMOF
Number of state machines per PIO.
Definition pio.h:30
pio_inst_out_dst_t
OUT destinations.
Definition pio.h:222
@ PIO_INST_OUT_DST_PINS
To output pins.
Definition pio.h:223
@ PIO_INST_OUT_DST_NULL
Discard data.
Definition pio.h:226
@ PIO_INST_OUT_DST_PC
To program counter.
Definition pio.h:228
@ PIO_INST_OUT_DST_PINDIRS
To output pin directions.
Definition pio.h:227
@ PIO_INST_OUT_DST_Y
To scratch Y.
Definition pio.h:225
@ PIO_INST_OUT_DST_X
To scratch X.
Definition pio.h:224
@ PIO_INST_OUT_DST_ISR
To input shift register.
Definition pio.h:229
@ PIO_INST_OUT_DST_EXEC
Execute as instruction.
Definition pio.h:230
void pio_sm_set_out_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base, unsigned pin_count)
Configure which pins are writeable by an 'out pins' instruction.
#define PIO_INST_JMP_CONDITION_SHIFT
JMP condition shift position.
Definition pio.h:103
static pio_instr_t pio_inst_push(bool if_full, bool block)
Construct a PUSH instruction.
Definition pio.h:475
void pio_print_debug(pio_t pio)
Print debug information about the current state of this PIO.
void pio_irq_disable(pio_t pio, pio_irq_line_t irq, pio_irq_source_t irq_mask)
Disable PIO<pio>_IRQ<irq> interrupts.
#define PIO_INST_PULL
PULL opcode.
Definition pio.h:259
struct pio_isr_vec pio_isr_vec_t
PIO interrupt callbacks for FIFO interrupts.
#define PIO_INST_PUSH_IF_FULL_SHIFT
PUSH if input shift register is full shift position.
Definition pio.h:243
void pio_sm_set_in_shift(pio_t pio, pio_sm_t sm, bool right, bool autopush, unsigned threshold)
Configure the shift in behaviour of a state machine.
void pio_set_isr_vec(pio_t pio, pio_sm_t sm, const pio_isr_vec_t *vec)
Set ISR callbacks for FIFO interrupts per state machine.
#define PIO_INST_PULL_IF_EMPTY_SHIFT
PULL if output shift register is empty flag shift position.
Definition pio.h:267
#define PIO_INST_JMP
JMP opcode.
Definition pio.h:95
#define PIO_INST_SET_DESTINATION_SHIFT
SET destination shift position.
Definition pio.h:383
#define PIO_INST_IRQ
IRQ opcode.
Definition pio.h:347
void pio_sm_set_wrap(pio_t pio, pio_sm_t sm, unsigned prog_loc, uint8_t top, uint8_t bottom)
Set program instruction wrap boundaries.
static unsigned pio_irq_rel_sm(unsigned irq_abs, unsigned irq_rel)
Get state machine index from relative IRQ index.
Definition pio.h:652
#define PIO_INST_MOV_OP_SHIFT
MOV operation shift position.
Definition pio.h:299
static pio_instr_t pio_inst_delay_sideset(unsigned sideset, unsigned sideset_count, bool sideset_opt, unsigned delay)
Encode the delay/sideset instruction field.
Definition pio.h:561
void pio_sm_set_pins_with_mask(pio_t pio, pio_sm_t sm, gpio_t values, gpio_t mask)
Apply pin values in values for which the corresponding bit in mask is set.
uint32_t pio_irq_get(pio_t pio)
Read interrupt flags.
struct pio_sm_clkdiv pio_sm_clkdiv_t
PIO clock configuration.
pio_inst_mov_src_t
MOV source.
Definition pio.h:335
@ PIO_INST_MOV_SRC_NULL
Fill with zeros.
Definition pio.h:339
@ PIO_INST_MOV_SRC_X
From scratch X.
Definition pio.h:337
@ PIO_INST_MOV_SRC_STATUS
From status.
Definition pio.h:340
@ PIO_INST_MOV_SRC_Y
From scratch Y.
Definition pio.h:338
@ PIO_INST_MOV_SRC_OSR
From output shift register.
Definition pio.h:342
@ PIO_INST_MOV_SRC_ISR
From input shift register.
Definition pio.h:341
@ PIO_INST_MOV_SRC_PINS
From input pins.
Definition pio.h:336
#define PIO_INST_OUT
OUT opcode.
Definition pio.h:202
#define PIO_INST_MOV
MOV opcode.
Definition pio.h:283
void pio_set_isr_sm_vec(pio_t pio, unsigned irq, const pio_isr_sm_vec_t *vec)
Set ISR callbacks for state machine interrupts.
static bool pio_sm_rx_fifo_full(pio_t pio, pio_sm_t sm)
Check if RX FIFO is full.
Definition pio.h:1322
static pio_instr_t pio_inst_irq(bool clear, bool wait, bool relative, unsigned index)
Construct an IRQ instruction.
Definition pio.h:525
void pio_sm_set_out_pins_init(pio_t pio, pio_sm_t sm, const pio_gpio_init_t *pin_init)
Set output pins affected by 'out pins', 'out pindirs' and 'mov pins' instructions and initialize the ...
static pio_instr_t pio_inst_set(pio_inst_set_dst_t destination, unsigned data)
Construct a SET instruction.
Definition pio.h:543
static pio_instr_t pio_inst_out(pio_inst_out_dst_t destination, unsigned bit_count)
Construct an OUT instruction.
Definition pio.h:460
int pio_sm_exec(pio_t pio, pio_sm_t sm, pio_instr_t inst)
Execute a single instruction.
void pio_sm_reset_fifos(pio_t pio, pio_sm_t sm)
No joined FIFO.
static pio_instr_t pio_inst_mov(pio_inst_mov_dst_t destination, pio_inst_mov_op_t operation, pio_inst_mov_src_t source)
Construct a MOV instruction.
Definition pio.h:506
void pio_sm_reset(pio_t pio, pio_sm_t sm)
Apply the default state machine configuration.
static bool pio_sm_tx_fifo_full(pio_t pio, pio_sm_t sm)
Check if TX FIFO is full.
Definition pio.h:1296
pio_irq_line_t
PIO interrupt lines.
Definition pio.h:702
@ PIO_IRQ_LINE_1
IRQ line 1.
Definition pio.h:704
@ PIO_IRQ_LINE_0
IRQ line 0.
Definition pio.h:703
@ PIO_IRQ_LINE_NUMOF
Number of IRQ lines.
Definition pio.h:705
static pio_instr_t pio_inst_pull(bool if_empty, bool block)
Construct a PULL instruction.
Definition pio.h:490
static bool pio_sm_tx_fifo_empty(pio_t pio, pio_sm_t sm)
Check if TX FIFO is empty.
Definition pio.h:1283
static void pio_sm_clear_debug_rxstall(pio_t pio, unsigned sm_mask)
Clear RX stall debug flag.
Definition pio.h:1269
void pio_sm_set_set_pins_init(pio_t pio, pio_sm_t sm, const pio_gpio_init_t *pin_init)
Set pins affected by 'set pins' and 'set pindirs' instructions and initialize the pins as PIO pins an...
static void pio_sm_clear_debug_rxunder(pio_t pio, unsigned sm_mask)
Clear RX underflow debug flag.
Definition pio.h:1257
void pio_print_status(pio_t pio)
Print status information about the FIFOs and programs.
pio_sm_clkdiv_t pio_sm_clkdiv(uint32_t f_hz)
Create a clock divider struct.
struct pio_isr_sm_vec pio_isr_sm_vec_t
PIO state machine interrupt callbacks for state machine interrupts.
pio_inst_in_src_t
IN sources.
Definition pio.h:191
@ PIO_INST_IN_SRC_PINS
From input pins.
Definition pio.h:192
@ PIO_INST_IN_SRC_Y
From scratch Y.
Definition pio.h:194
@ PIO_INST_IN_SRC_ISR
From input shift register.
Definition pio.h:196
@ PIO_INST_IN_SRC_OSR
From output shift register.
Definition pio.h:197
@ PIO_INST_IN_SRC_X
From scratch X.
Definition pio.h:193
@ PIO_INST_IN_SRC_NULL
Fill with zeros.
Definition pio.h:195
void pio_irq_clear(pio_t pio, unsigned irq_flags)
Clear interrupt flags.
#define PIO_INST_IRQ_CLR_SHIFT
IRQ clear flag shift position.
Definition pio.h:355
void pio_sm_exec_block(pio_t pio, pio_sm_t sm, pio_instr_t inst)
Execute a single instruction.
pio_inst_mov_dst_t
MOV destinations.
Definition pio.h:315
@ PIO_INST_MOV_DST_PINS
To output pins.
Definition pio.h:316
@ PIO_INST_MOV_DST_ISR
To input shift register.
Definition pio.h:321
@ PIO_INST_MOV_DST_Y
To scratch Y.
Definition pio.h:318
@ PIO_INST_MOV_DST_OSR
To output shift register.
Definition pio.h:322
@ PIO_INST_MOV_DST_PC
To program counter.
Definition pio.h:320
@ PIO_INST_MOV_DST_EXEC
Execute data as instruction.
Definition pio.h:319
@ PIO_INST_MOV_DST_X
To scratch X.
Definition pio.h:317
pio_inst_wait_src_t
WAIT sources.
Definition pio.h:163
@ PIO_INST_WAIT_SRC_IRQ
Wait on interrupt.
Definition pio.h:166
@ PIO_INST_WAIT_SRC_PIN
Wait on input pin.
Definition pio.h:165
@ PIO_INST_WAIT_SRC_GPIO
Wait on GPIO.
Definition pio.h:164
void pio_sm_restart(pio_t pio, pio_sm_t sm)
Restart a state machine.
pio_irq_source_t
PIO IRQ flags.
Definition pio.h:666
@ PIO_IRQ_TXNFULL_SM3
SM3 TX FIFO is not full.
Definition pio.h:674
@ PIO_IRQ_TXNFULL_SM2
SM2 TX FIFO is not full.
Definition pio.h:675
@ PIO_IRQ_SM_3
Any state machine issued irq 3.
Definition pio.h:669
@ PIO_IRQ_SM_0
any state machine issued irq 0
Definition pio.h:672
@ PIO_IRQ_RXNEMPTY_SM2
SM2 RX FIFO is not empty.
Definition pio.h:679
@ PIO_IRQ_RXNEMPTY_SM3
SM3 RX FIFO is not empty.
Definition pio.h:678
@ PIO_IRQ_SM_2
Any state machine issued irq 2.
Definition pio.h:670
@ PIO_IRQ_SM_1
Any state machine issued irq 1.
Definition pio.h:671
@ PIO_IRQ_TXNFULL_SM0
SM0 TX FIFO is not full.
Definition pio.h:677
@ PIO_IRQ_TXNFULL_SM1
SM1 TX FIFO is not full.
Definition pio.h:676
@ PIO_IRQ_RXNEMPTY_SM1
SM1 RX FIFO is not empty.
Definition pio.h:680
@ PIO_IRQ_RXNEMPTY_SM0
SM0 RX FIFO is not empty.
Definition pio.h:681
@ PIO_IRQ_ALL_SM
All SM interrupts.
Definition pio.h:683
@ PIO_IRQ_ALL
All flags above.
Definition pio.h:688
void pio_sm_transmit_words_block(pio_t pio, pio_sm_t sm, const uint32_t *words, unsigned count)
Send count words to a state machine.
#define PIO_INST_SET
SET opcode.
Definition pio.h:375
#define PIO_INST_OUT_DESTINATION_SHIFT
OUT destination shift position.
Definition pio.h:210
#define PIO_IRQ_NUMOF
Number of interrupt flags per PIO.
Definition pio.h:40
#define PIO_INST_WAIT
WAIT opcode.
Definition pio.h:128
#define PIO_INST_IN
IN opcode.
Definition pio.h:171
static bool pio_sm_rx_fifo_empty(pio_t pio, pio_sm_t sm)
Check if RX FIFO is empty.
Definition pio.h:1309
pio_inst_wait_pol_t
WAIT polarities.
Definition pio.h:156
@ PIO_INST_WAIT_POL_LOW
Wait for 0.
Definition pio.h:157
@ PIO_INST_WAIT_POL_HIGH
Wait for 1.
Definition pio.h:158
void pio_sm_set_pindirs_with_mask(pio_t pio, pio_sm_t sm, gpio_t values, gpio_t mask)
Apply pin directions in values for which the corresponding bit in mask is set.
#define PIO_INST_WAIT_POL_SHIFT
WAIT polarity shift position.
Definition pio.h:136
void pio_sm_receive_word_block(pio_t pio, pio_sm_t sm, uint32_t *word)
Receive one word from a state machine.
int pio_sm_init_common(pio_t pio, pio_sm_t sm, const pio_program_t *prog, const pio_program_conf_t *conf)
Apply common program configuration.
void pio_sm_receive_words_block(pio_t pio, pio_sm_t sm, uint32_t *words, unsigned count)
Receive words from a state machine.
#define PIO_INST_MOV_DESTINATION_SHIFT
MOV destination shift position.
Definition pio.h:291
int pio_sm_receive_word(pio_t pio, pio_sm_t sm, uint32_t *word)
Receive a word from a state machine.
#define PIO_INST_PULL_BLOCK_SHIFT
PULL if TX FIFO is not empty flag shift position.
Definition pio.h:275
void pio_sm_set_fifo_join_tx(pio_t pio, pio_sm_t sm)
Join the RX FIFO to the TX FIFO.
pio_sm_mask_t
PIO state machine flags.
Definition pio.h:711
@ PIO_SM1
Mask bit of SM 1.
Definition pio.h:713
@ PIO_SM0
Mask bit of SM 0.
Definition pio.h:712
@ PIO_SM_ALL
All flags above.
Definition pio.h:716
@ PIO_SM2
Mask bit of SM 2.
Definition pio.h:714
@ PIO_SM3
Mask bit of SM 3.
Definition pio.h:715
int pio_sm_transmit_word(pio_t pio, pio_sm_t sm, uint32_t word)
Send one word to a state machine.
void pio_sm_set_sideset_target(pio_t pio, pio_sm_t sm, bool pindir)
Configure whether a sideset effects pins or pin directions.
#define PIO_INST_PUSH_BLOCK_SHIFT
PUSH if RX FIFO is not full shift position.
Definition pio.h:251
void pio_sm_set_clkdiv(pio_t pio, pio_sm_t sm, pio_sm_clkdiv_t clk)
Apply the clock configuration to a PIO.
struct pio_program_conf pio_program_conf_t
PIO program configuration.
static pio_instr_t pio_inst_wait(pio_inst_wait_pol_t polarity, pio_inst_wait_src_t source, bool relative, unsigned index)
Construct a WAIT instruction.
Definition pio.h:427
#define PIO_INST_PUSH
PUSH opcode.
Definition pio.h:235
#define PIO_INST_MOV_SOURCE_SHIFT
MOV source shift position.
Definition pio.h:307
void pio_sm_transmit_word_block(pio_t pio, pio_sm_t sm, uint32_t word)
Send one word to a state machine.
void pio_sm_set_out_shift(pio_t pio, pio_sm_t sm, bool right, bool autopull, unsigned threshold)
Configure the shift out behaviour of a state machine.
int pio_write_program(pio_t pio, pio_program_t *prog, const pio_instr_t *instr)
Write program instructions to their allocated location.
#define PIO_INST_WAIT_SOURCE_SHIFT
WAIT source shift positions.
Definition pio.h:144
void pio_sm_set_set_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base, unsigned pin_count)
Configure which pins are effected by a 'set pins' instructions.
void pio_sm_set_sideset_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base)
Set the first pin of a sequence of sideset count pins which are effected by sideset instructions.
pio_inst_set_dst_t
SET destinations.
Definition pio.h:395
@ PIO_INST_SET_DST_Y
To scratch Y.
Definition pio.h:398
@ PIO_INST_SET_DST_PINDIRS
To set pin directions.
Definition pio.h:399
@ PIO_INST_SET_DST_PINS
To set pins.
Definition pio.h:396
@ PIO_INST_SET_DST_X
To scratch X.
Definition pio.h:397
static void pio_sm_clear_debug_txstall(pio_t pio, unsigned sm_mask)
Clear TX stall debug flag.
Definition pio.h:1233
void pio_sm_set_fifo_join_rx(pio_t pio, pio_sm_t sm)
Join the TX FIFO to the RX FIFO.
#define PIO_SM_MASK(sm)
Convert state machine index to bitmask.
Definition pio.h:597
void pio_sm_clkdiv_restart(pio_t pio, unsigned sm_mask)
Restart the clock divider of several state machines.
void pio_sm_set_jmp_pin(pio_t pio, pio_sm_t sm, gpio_t pin)
Configure the pin on which to branch on a 'jmp pins' instruction.
void pio_sm_set_in_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base)
Configure the state machine input pin mapping.
pio_inst_mov_op_t
MOV operation.
Definition pio.h:327
@ PIO_INST_MOV_OP_REVERSE
Bitwise reverse.
Definition pio.h:330
@ PIO_INST_MOV_OP_NONE
No operation.
Definition pio.h:328
@ PIO_INST_MOV_OP_INVERT
Bitwise complement.
Definition pio.h:329
#define PIO_INST_IN_SOURCE_SHIFT
IN source shift position.
Definition pio.h:179
#define PIO_INST_IRQ_WAIT_SHIFT
IRQ wait until cleared flag shift position.
Definition pio.h:363
void pio_irq_enable(pio_t pio, pio_irq_line_t irq, pio_irq_source_t irq_mask)
Enable PIO<pio>_IRQ<irq> interrupts.
static pio_instr_t pio_inst_in(pio_inst_in_src_t source, unsigned bit_count)
Construct an IN instruction.
Definition pio.h:445
High-level PIO peripheral driver interface.
Low-level GPIO peripheral driver interface definitions.
unsigned pio_t
PIO index type.
Definition pio.h:75
int pio_sm_t
PIO state machine index type.
Definition pio.h:80
void delay(unsigned long msec)
Sleep for a given amount of time [milliseconds].
static void io_reg_atomic_set(volatile uint32_t *reg, uint32_t mask)
Set the bits in the register at address reg as given by the set bits in operand op.
Definition io_reg.h:84
Memory layout of GPIO control register in IO bank 0.
Definition periph_cpu.h:376
Memory layout of GPIO control register in pads bank 0.
Definition periph_cpu.h:286
Type used to configure PIO gpios pins.
Definition pio.h:79
gpio_t gpio_direction
GPIO directions applied to pins, where the LSBit is the base.
Definition pio.h:83
gpio_pad_ctrl_t pad
Pads bank GPIO control register configuration.
Definition pio.h:80
gpio_io_ctrl_t io
IO bank GPIO control register.
Definition pio.h:81
unsigned gpio_count
Number of GPIOs starting at base.
Definition pio.h:85
gpio_t gpio_base
GPIO base.
Definition pio.h:84
gpio_t gpio_state
GPIO states applied to pins, where the LSBit is the base.
Definition pio.h:82
PIO state machine interrupt callbacks for state machine interrupts.
Definition pio.h:743
void(* sm)(pio_t pio, unsigned irq)
Called when any SM issues an interrupt [0-3].
Definition pio.h:750
PIO interrupt callbacks for FIFO interrupts.
Definition pio.h:723
void(* tx_ready)(pio_t pio, pio_sm_t sm)
Called when Tx FIFO is not full.
Definition pio.h:730
void(* rx_ready)(pio_t pio, pio_sm_t sm)
Called when Rx FIFO is not empty.
Definition pio.h:737
PIO program configuration.
Definition pio.h:756
unsigned pc_start
Initial program counter.
Definition pio.h:757
unsigned wrap_bottom
Instruction index after which the PC wraps around.
Definition pio.h:758
bool sideset_optional
Whether the sideset is optional.
Definition pio.h:761
bool sideset_pindirs
Whether the sideset effects pin directions.
Definition pio.h:762
unsigned sideset_count
Number of bits used for sideset.
Definition pio.h:760
unsigned wrap_top
Instruction index the PC wraps to.
Definition pio.h:759
Struct that models a PIO program.
Definition pio.h:85
PIO clock configuration.
Definition pio.h:768
uint16_t div
Integer divider.
Definition pio.h:769
uint8_t frac_100
Fractional divider, two digits after comma.
Definition pio.h:770
Internal state machine registers.
Definition pio.h:575
volatile uint32_t clkdiv
SMx_CLKDIV.
Definition pio.h:576
volatile uint32_t instr
SMx_INSTR.
Definition pio.h:580
volatile uint32_t pinctrl
SMx_PINCTRL.
Definition pio.h:581
volatile uint32_t shiftctrl
SMx_SHIFTCTRL.
Definition pio.h:578
const volatile uint32_t addr
SMx_ADDR.
Definition pio.h:579
volatile uint32_t execctrl
SMx_EXECCTRL.
Definition pio.h:577