Loading...
Searching...
No Matches
pio.h
Go to the documentation of this file.
1/*
2 * Copyright (C) 2021 Otto-von-Guericke-Universität Magdeburg
3 *
4 * This file is subject to the terms and conditions of the GNU Lesser General
5 * Public License v2.1. See the file LICENSE in the top level directory for more
6 * details.
7 */
8
19#ifndef PIO_PIO_H
20#define PIO_PIO_H
21
22#include "periph_conf.h"
23#include "periph/gpio.h"
24#include "periph/pio.h"
25
26#ifdef __cplusplus
27extern "C" {
28#endif
29
30#if defined(CPU_MODEL_RP2040) || defined(DOXYGEN)
34#define PIO_SM_NUMOF 4
35
39#define PIO_INSTR_NUMOF 32
40
44#define PIO_IRQ_NUMOF 8
45#endif
46
50typedef uint16_t pio_instr_t;
51
57#define PIO_GPIO_INIT_HIGH(pin) (((gpio_t)(1)) << (pin))
58
64#define PIO_GPIO_INIT_LOW(pin) ((gpio_t)0)
65
71#define PIO_GPIO_INIT_OUT(pin) (((gpio_t)(1)) << (pin))
72
78#define PIO_GPIO_INIT_IN(pin) ((gpio_t)0)
79
91
99#define PIO_INST_JMP (0u << 13)
103#define PIO_INST_JMP_MASK (7u << 13)
107#define PIO_INST_JMP_CONDITION_SHIFT 5
111#define PIO_INST_JMP_CONDITION_MASK (7u << PIO_INST_JMP_CONDITION_SHIFT)
115#define PIO_INST_JMP_ADDRESS_MASK (31u)
132#define PIO_INST_WAIT (1u << 13)
136#define PIO_INST_WAIT_MASK (7u << 13)
140#define PIO_INST_WAIT_POL_SHIFT 7
144#define PIO_INST_WAIT_POL_MASK (1u << PIO_INST_WAIT_POL_SHIFT)
148#define PIO_INST_WAIT_SOURCE_SHIFT 5
152#define PIO_INST_WAIT_SOURCE_MASK (3u << PIO_INST_WAIT_SOURCE_SHIFT)
156#define PIO_INST_WAIT_INDEX_MASK (31u)
175#define PIO_INST_IN (2u << 13)
179#define PIO_INST_IN_MASK (7u << 13)
183#define PIO_INST_IN_SOURCE_SHIFT 5
187#define PIO_INST_IN_SOURCE_MASK (7u << PIO_INST_IN_SOURCE_SHIFT)
191#define PIO_INST_IN_BIT_COUNT_MASK (31u)
206#define PIO_INST_OUT (3u << 13)
210#define PIO_INST_OUT_MASK (7u << 13)
214#define PIO_INST_OUT_DESTINATION_SHIFT 5
218#define PIO_INST_OUT_DESTINATION_MASK (7u << PIO_INST_OUT_DESTINATION_SHIFT)
222#define PIO_INST_OUT_BIT_COUNT_MASK (31u)
239#define PIO_INST_PUSH (4u << 13)
243#define PIO_INST_PUSH_MASK ((7u << 13) | (1u << 7) | 31u)
247#define PIO_INST_PUSH_IF_FULL_SHIFT 6
251#define PIO_INST_PUSH_IF_FULL_MASK (1u << PIO_INST_PUSH_IF_FULL_SHIFT)
255#define PIO_INST_PUSH_BLOCK_SHIFT 5
259#define PIO_INST_PUSH_BLOCK_MASK (1u << PIO_INST_PUSH_BLOCK_SHIFT)
263#define PIO_INST_PULL ((4u << 13) | (1u << 7))
267#define PIO_INST_PULL_MASK ((7u << 13) | (1u << 7) | 31u)
271#define PIO_INST_PULL_IF_EMPTY_SHIFT 6
275#define PIO_INST_PULL_IF_EMPTY_MASK (1u << PIO_INST_PULL_IF_EMPTY_SHIFT)
279#define PIO_INST_PULL_BLOCK_SHIFT 5
283#define PIO_INST_PULL_BLOCK_MASK (1u << PIO_INST_PULL_BLOCK_SHIFT)
287#define PIO_INST_MOV (5u << 13)
291#define PIO_INST_MOV_MASK (7u << 13)
295#define PIO_INST_MOV_DESTINATION_SHIFT 5
299#define PIO_INST_MOV_DESTINATION_MASK (7u << PIO_INST_MOV_DESTINATION_SHIFT)
303#define PIO_INST_MOV_OP_SHIFT 3
307#define PIO_INST_MOV_OP_MASK (3u << PIO_INST_MOV_OP_SHIFT)
311#define PIO_INST_MOV_SOURCE_SHIFT 0
315#define PIO_INST_MOV_SOURCE_MASK (7u)
351#define PIO_INST_IRQ (6u << 13)
355#define PIO_INST_IRQ_MASK ((7u << 13) | (1u << 7))
359#define PIO_INST_IRQ_CLR_SHIFT 6
363#define PIO_INST_IRQ_CLR_MASK (1u << PIO_INST_IRQ_CLR_SHIFT)
367#define PIO_INST_IRQ_WAIT_SHIFT 5
371#define PIO_INST_IRQ_WAIT_MASK (1u << PIO_INST_IRQ_WAIT_SHIFT)
375#define PIO_INST_IRQ_INDEX_MASK (31u)
379#define PIO_INST_SET (7u << 13)
383#define PIO_INST_SET_MASK (7u << 13)
387#define PIO_INST_SET_DESTINATION_SHIFT 5
391#define PIO_INST_SET_DESTINATION_MASK (7u << PIO_INST_SET_DESTINATION_SHIFT)
395#define PIO_INST_SET_DATA_MASK (31u)
405
415 unsigned address)
416{
417 return PIO_INST_JMP |
418 (((unsigned)condition) << PIO_INST_JMP_CONDITION_SHIFT) |
419 address;
420}
432 pio_inst_wait_src_t source,
433 bool relative,
434 unsigned index)
435{
436 return PIO_INST_WAIT |
437 ((unsigned)polarity << PIO_INST_WAIT_POL_SHIFT) |
438 ((unsigned)source << PIO_INST_WAIT_SOURCE_SHIFT) |
439 ((!!relative) << 4) | index;
440}
450 unsigned bit_count)
451{
452 return PIO_INST_IN |
453 ((unsigned)source << PIO_INST_IN_SOURCE_SHIFT) |
454 (bit_count % 32);
455}
465 unsigned bit_count)
466{
467 return PIO_INST_OUT |
468 ((unsigned)destination << PIO_INST_OUT_DESTINATION_SHIFT) |
469 (bit_count % 32);
470}
479static inline pio_instr_t pio_inst_push(bool if_full,
480 bool block)
481{
482 return PIO_INST_PUSH |
483 ((!!if_full) << PIO_INST_PUSH_IF_FULL_SHIFT) |
484 ((!!block) << PIO_INST_PUSH_BLOCK_SHIFT);
485}
494static inline pio_instr_t pio_inst_pull(bool if_empty,
495 bool block)
496{
497 return PIO_INST_PULL |
498 ((!!if_empty) << PIO_INST_PULL_IF_EMPTY_SHIFT) |
499 ((!!block) << PIO_INST_PULL_BLOCK_SHIFT);
500}
511 pio_inst_mov_op_t operation,
512 pio_inst_mov_src_t source)
513{
514 return PIO_INST_MOV |
515 ((unsigned)destination << PIO_INST_MOV_DESTINATION_SHIFT) |
516 ((unsigned)operation << PIO_INST_MOV_OP_SHIFT) |
517 ((unsigned)source << PIO_INST_MOV_SOURCE_SHIFT);
518}
529static inline pio_instr_t pio_inst_irq(bool clear,
530 bool wait,
531 bool relative,
532 unsigned index)
533{
534 return PIO_INST_IRQ |
535 (!!clear << PIO_INST_IRQ_CLR_SHIFT) |
536 (!!wait << PIO_INST_IRQ_WAIT_SHIFT) |
537 ((!!relative << 4) | index);
538}
548 unsigned data)
549{
550 return PIO_INST_SET |
551 ((unsigned)destination << PIO_INST_SET_DESTINATION_SHIFT) |
552 data;
553}
554
565static inline pio_instr_t pio_inst_delay_sideset(unsigned sideset,
566 unsigned sideset_count,
567 bool sideset_opt,
568 unsigned delay)
569{
570 return ((!!sideset_opt) << 12) |
571 (sideset << (13 - (!!sideset_opt + sideset_count))) |
572 (delay << 8);
573}
579typedef struct pio_sm_ctrl_regs {
580 volatile uint32_t clkdiv;
581 volatile uint32_t execctrl;
582 volatile uint32_t shiftctrl;
583 const volatile uint32_t addr;
584 volatile uint32_t instr;
585 volatile uint32_t pinctrl;
587
591#define PIO_SM_CTRL_BASE(dev) ((pio_sm_ctrl_regs_t *)(&((dev)->SM0_CLKDIV)))
592
596#define PIO_SM_CLKDIV_MAX 65536
597
601#define PIO_SM_MASK(sm) (1u << (sm))
602
606#define PIO_IRQ_MASK(irq) (1u << (irq))
607
640static inline unsigned pio_irq_rel_index(unsigned irq_abs, unsigned sm) {
641 assert(irq_abs < PIO_IRQ_NUMOF);
642 assert(sm < PIO_SM_NUMOF);
643 return (irq_abs & 0b100u) | ((irq_abs + sm) & 0b011u);
644}
645
656static inline unsigned pio_irq_rel_sm(unsigned irq_abs, unsigned irq_rel) {
657 assert(irq_abs < PIO_IRQ_NUMOF);
658 assert(irq_rel < PIO_IRQ_NUMOF);
659 return (irq_abs & 0b100u) | ((irq_rel - irq_abs) & 0b011u);
660}
661
665#define PIO_IRQ_REL_MASK(irq, sm) PIO_IRQ_MASK(pio_irq_rel_index(irq, sm))
666
670typedef enum {
671 /* lower 4 hardware IRQs which are issued with 'irq <0-3> [rel]' and are routed to NVIC */
672 /* This does not encode the state machine index of which state machine fired! */
673 PIO_IRQ_SM_3 = PIO0_INTR_SM3_Msk,
674 PIO_IRQ_SM_2 = PIO0_INTR_SM2_Msk,
675 PIO_IRQ_SM_1 = PIO0_INTR_SM1_Msk,
676 PIO_IRQ_SM_0 = PIO0_INTR_SM0_Msk,
677 /* FIFO interrupts per state machine */
678 PIO_IRQ_TXNFULL_SM3 = PIO0_INTR_SM3_TXNFULL_Msk,
679 PIO_IRQ_TXNFULL_SM2 = PIO0_INTR_SM2_TXNFULL_Msk,
680 PIO_IRQ_TXNFULL_SM1 = PIO0_INTR_SM1_TXNFULL_Msk,
681 PIO_IRQ_TXNFULL_SM0 = PIO0_INTR_SM0_TXNFULL_Msk,
682 PIO_IRQ_RXNEMPTY_SM3 = PIO0_INTR_SM3_RXNEMPTY_Msk,
683 PIO_IRQ_RXNEMPTY_SM2 = PIO0_INTR_SM2_RXNEMPTY_Msk,
684 PIO_IRQ_RXNEMPTY_SM1 = PIO0_INTR_SM1_RXNEMPTY_Msk,
685 PIO_IRQ_RXNEMPTY_SM0 = PIO0_INTR_SM0_RXNEMPTY_Msk,
686 /* all routed state machine interrupts */
691 /* all interrupts */
702
711
723
727typedef struct pio_isr_vec {
734 void (*tx_ready)(pio_t pio, pio_sm_t sm);
741 void (*rx_ready)(pio_t pio, pio_sm_t sm);
743
747typedef struct pio_isr_sm_vec {
754 void (*sm)(pio_t pio, unsigned irq);
756
768
772typedef struct pio_sm_clkdiv {
773 uint16_t div;
774 uint8_t frac_100;
776
792
805
816 int pio_write_program(pio_t pio, pio_program_t *prog, const pio_instr_t *instr);
817
825
833
841void pio_set_isr_vec(pio_t pio, pio_sm_t sm, const pio_isr_vec_t *vec);
842
850void pio_set_isr_sm_vec(pio_t pio, unsigned irq, const pio_isr_sm_vec_t *vec);
851
863void pio_sm_set_out_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base, unsigned pin_count);
864
874void pio_sm_set_in_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base);
875
887void pio_sm_set_set_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base, unsigned pin_count);
888
897void pio_sm_set_sideset_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base);
898
911void pio_sm_set_sideset_count(pio_t pio, pio_sm_t sm, unsigned pin_count, bool enable);
912
920void pio_sm_set_sideset_target(pio_t pio, pio_sm_t sm, bool pindir);
921
933
947
957void pio_sm_clkdiv_restart(pio_t pio, unsigned sm_mask);
958
968void pio_sm_set_wrap(pio_t pio, pio_sm_t sm, unsigned prog_loc, uint8_t top, uint8_t bottom);
969
979void pio_sm_set_jmp_pin(pio_t pio, pio_sm_t sm, gpio_t pin);
980
990void pio_sm_set_in_shift(pio_t pio, pio_sm_t sm, bool right, bool autopush, unsigned threshold);
991
1001void pio_sm_set_out_shift(pio_t pio, pio_sm_t sm, bool right, bool autopull, unsigned threshold);
1002
1010
1018
1026
1034
1043
1052
1066int pio_sm_transmit_word(pio_t pio, pio_sm_t sm, uint32_t word);
1067
1077void pio_sm_transmit_word_block(pio_t pio, pio_sm_t sm, uint32_t word);
1078
1089void pio_sm_transmit_words_block(pio_t pio, pio_sm_t sm, const uint32_t *words, unsigned count);
1090
1104int pio_sm_receive_word(pio_t pio, pio_sm_t sm, uint32_t *word);
1105
1115void pio_sm_receive_word_block(pio_t pio, pio_sm_t sm, uint32_t *word);
1116
1127void pio_sm_receive_words_block(pio_t pio, pio_sm_t sm, uint32_t *words, unsigned count);
1128
1136uint32_t pio_irq_get(pio_t pio);
1137
1144void pio_irq_clear(pio_t pio, unsigned irq_flags);
1145
1161 const pio_program_conf_t *conf);
1162
1173void pio_sm_set_pindirs_with_mask(pio_t pio, pio_sm_t sm, gpio_t values, gpio_t mask);
1174
1185void pio_sm_set_pins_with_mask(pio_t pio, pio_sm_t sm, gpio_t values, gpio_t mask);
1186
1196
1206
1216
1223
1230
1237static inline void pio_sm_clear_debug_txstall(pio_t pio, unsigned sm_mask)
1238{
1239 io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1240 ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_TXSTALL_Pos));
1241}
1242
1249static inline void pio_sm_clear_debug_txover(pio_t pio, unsigned sm_mask)
1250{
1251 io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1252 ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_TXOVER_Pos));
1253}
1254
1261static inline void pio_sm_clear_debug_rxunder(pio_t pio, unsigned sm_mask)
1262{
1263 io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1264 ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_RXUNDER_Pos));
1265}
1266
1273static inline void pio_sm_clear_debug_rxstall(pio_t pio, unsigned sm_mask)
1274{
1275 io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1276 ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_RXSTALL_Pos));
1277}
1278
1287static inline bool pio_sm_tx_fifo_empty(pio_t pio, pio_sm_t sm)
1288{
1289 return !!(pio_config[pio].dev->FSTAT & ((1u << sm) << PIO0_FSTAT_TXEMPTY_Pos));
1290}
1291
1300static inline bool pio_sm_tx_fifo_full(pio_t pio, pio_sm_t sm)
1301{
1302 return !!(pio_config[pio].dev->FSTAT & ((1u << sm) << PIO0_FSTAT_TXFULL_Pos));
1303}
1304
1313static inline bool pio_sm_rx_fifo_empty(pio_t pio, pio_sm_t sm)
1314{
1315 return !!(pio_config[pio].dev->FSTAT & ((1u << sm) << PIO0_FSTAT_RXEMPTY_Pos));
1316}
1317
1326static inline bool pio_sm_rx_fifo_full(pio_t pio, pio_sm_t sm)
1327{
1328 return !!((pio_config[pio].dev)->FSTAT & ((1u << sm) << PIO0_FSTAT_RXFULL_Pos));
1329}
1330
1331#ifdef __cplusplus
1332}
1333#endif
1334
1335#endif /* PIO_PIO_H */
#define assert(cond)
abort the program if assertion is false
Definition assert.h:136
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:640
uint16_t pio_instr_t
Type to represent the width of an instruction.
Definition pio.h:50
static pio_instr_t pio_inst_jmp(pio_inst_jmp_cond_t condition, unsigned address)
Construct a JMP instruction.
Definition pio.h:414
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:1249
pio_inst_jmp_cond_t
JMP conditions.
Definition pio.h:119
@ PIO_INST_JMP_COND_NOT_X_EQ_Y
If scratch X != scratch Y.
Definition pio.h:125
@ PIO_INST_JMP_COND_NONE
Unconditionally.
Definition pio.h:120
@ PIO_INST_JMP_COND_PIN
If jump pin is high.
Definition pio.h:126
@ PIO_INST_JMP_COND_X_DEC
If scratch X– is not 0.
Definition pio.h:122
@ PIO_INST_JMP_COND_X_ZERO
If scratch X is 0.
Definition pio.h:121
@ PIO_INST_JMP_COND_Y_ZERO
If scratch Y is 0.
Definition pio.h:123
@ PIO_INST_JMP_COND_Y_DEC
If scratch Y– is not 0.
Definition pio.h:124
@ PIO_INST_JMP_COND_NOT_OSR_EMPTY
If OSR is not empty.
Definition pio.h:127
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:34
pio_inst_out_dst_t
OUT destinations.
Definition pio.h:226
@ PIO_INST_OUT_DST_PINS
To output pins.
Definition pio.h:227
@ PIO_INST_OUT_DST_NULL
Discard data.
Definition pio.h:230
@ PIO_INST_OUT_DST_PC
To program counter.
Definition pio.h:232
@ PIO_INST_OUT_DST_PINDIRS
To output pin directions.
Definition pio.h:231
@ PIO_INST_OUT_DST_Y
To scratch Y.
Definition pio.h:229
@ PIO_INST_OUT_DST_X
To scratch X.
Definition pio.h:228
@ PIO_INST_OUT_DST_ISR
To input shift register.
Definition pio.h:233
@ PIO_INST_OUT_DST_EXEC
Execute as instruction.
Definition pio.h:234
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:107
static pio_instr_t pio_inst_push(bool if_full, bool block)
Construct a PUSH instruction.
Definition pio.h:479
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:263
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:247
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:271
#define PIO_INST_JMP
JMP opcode.
Definition pio.h:99
#define PIO_INST_SET_DESTINATION_SHIFT
SET destination shift position.
Definition pio.h:387
#define PIO_INST_IRQ
IRQ opcode.
Definition pio.h:351
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:656
#define PIO_INST_MOV_OP_SHIFT
MOV operation shift position.
Definition pio.h:303
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:565
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:339
@ PIO_INST_MOV_SRC_NULL
Fill with zeros.
Definition pio.h:343
@ PIO_INST_MOV_SRC_X
From scratch X.
Definition pio.h:341
@ PIO_INST_MOV_SRC_STATUS
From status.
Definition pio.h:344
@ PIO_INST_MOV_SRC_Y
From scratch Y.
Definition pio.h:342
@ PIO_INST_MOV_SRC_OSR
From output shift register.
Definition pio.h:346
@ PIO_INST_MOV_SRC_ISR
From input shift register.
Definition pio.h:345
@ PIO_INST_MOV_SRC_PINS
From input pins.
Definition pio.h:340
#define PIO_INST_OUT
OUT opcode.
Definition pio.h:206
#define PIO_INST_MOV
MOV opcode.
Definition pio.h:287
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:1326
static pio_instr_t pio_inst_irq(bool clear, bool wait, bool relative, unsigned index)
Construct an IRQ instruction.
Definition pio.h:529
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:547
static pio_instr_t pio_inst_out(pio_inst_out_dst_t destination, unsigned bit_count)
Construct an OUT instruction.
Definition pio.h:464
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:510
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:1300
pio_irq_line_t
PIO interrupt lines.
Definition pio.h:706
@ PIO_IRQ_LINE_1
IRQ line 1.
Definition pio.h:708
@ PIO_IRQ_LINE_0
IRQ line 0.
Definition pio.h:707
@ PIO_IRQ_LINE_NUMOF
Number of IRQ lines.
Definition pio.h:709
static pio_instr_t pio_inst_pull(bool if_empty, bool block)
Construct a PULL instruction.
Definition pio.h:494
static bool pio_sm_tx_fifo_empty(pio_t pio, pio_sm_t sm)
Check if TX FIFO is empty.
Definition pio.h:1287
static void pio_sm_clear_debug_rxstall(pio_t pio, unsigned sm_mask)
Clear RX stall debug flag.
Definition pio.h:1273
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:1261
void pio_print_status(pio_t pio)
Print status information about the FIFOs and programs.
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:195
@ PIO_INST_IN_SRC_PINS
From input pins.
Definition pio.h:196
@ PIO_INST_IN_SRC_Y
From scratch Y.
Definition pio.h:198
@ PIO_INST_IN_SRC_ISR
From input shift register.
Definition pio.h:200
@ PIO_INST_IN_SRC_OSR
From output shift register.
Definition pio.h:201
@ PIO_INST_IN_SRC_X
From scratch X.
Definition pio.h:197
@ PIO_INST_IN_SRC_NULL
Fill with zeros.
Definition pio.h:199
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:359
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:319
@ PIO_INST_MOV_DST_PINS
To output pins.
Definition pio.h:320
@ PIO_INST_MOV_DST_ISR
To input shift register.
Definition pio.h:325
@ PIO_INST_MOV_DST_Y
To scratch Y.
Definition pio.h:322
@ PIO_INST_MOV_DST_OSR
To output shift register.
Definition pio.h:326
@ PIO_INST_MOV_DST_PC
To program counter.
Definition pio.h:324
@ PIO_INST_MOV_DST_EXEC
Execute data as instruction.
Definition pio.h:323
@ PIO_INST_MOV_DST_X
To scratch X.
Definition pio.h:321
pio_inst_wait_src_t
WAIT sources.
Definition pio.h:167
@ PIO_INST_WAIT_SRC_IRQ
Wait on interrupt.
Definition pio.h:170
@ PIO_INST_WAIT_SRC_PIN
Wait on input pin.
Definition pio.h:169
@ PIO_INST_WAIT_SRC_GPIO
Wait on GPIO.
Definition pio.h:168
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:670
@ PIO_IRQ_TXNFULL_SM3
SM3 TX FIFO is not full.
Definition pio.h:678
@ PIO_IRQ_TXNFULL_SM2
SM2 TX FIFO is not full.
Definition pio.h:679
@ PIO_IRQ_SM_3
Any state machine issued irq 3.
Definition pio.h:673
@ PIO_IRQ_SM_0
any state machine issued irq 0
Definition pio.h:676
@ PIO_IRQ_RXNEMPTY_SM2
SM2 RX FIFO is not empty.
Definition pio.h:683
@ PIO_IRQ_RXNEMPTY_SM3
SM3 RX FIFO is not empty.
Definition pio.h:682
@ PIO_IRQ_SM_2
Any state machine issued irq 2.
Definition pio.h:674
@ PIO_IRQ_SM_1
Any state machine issued irq 1.
Definition pio.h:675
@ PIO_IRQ_TXNFULL_SM0
SM0 TX FIFO is not full.
Definition pio.h:681
@ PIO_IRQ_TXNFULL_SM1
SM1 TX FIFO is not full.
Definition pio.h:680
@ PIO_IRQ_RXNEMPTY_SM1
SM1 RX FIFO is not empty.
Definition pio.h:684
@ PIO_IRQ_RXNEMPTY_SM0
SM0 RX FIFO is not empty.
Definition pio.h:685
@ PIO_IRQ_ALL_SM
All SM interrupts.
Definition pio.h:687
@ PIO_IRQ_ALL
All flags above.
Definition pio.h:692
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:379
#define PIO_INST_OUT_DESTINATION_SHIFT
OUT destination shift position.
Definition pio.h:214
#define PIO_IRQ_NUMOF
Number of interrupt flags per PIO.
Definition pio.h:44
#define PIO_INST_WAIT
WAIT opcode.
Definition pio.h:132
#define PIO_INST_IN
IN opcode.
Definition pio.h:175
static bool pio_sm_rx_fifo_empty(pio_t pio, pio_sm_t sm)
Check if RX FIFO is empty.
Definition pio.h:1313
pio_inst_wait_pol_t
WAIT polarities.
Definition pio.h:160
@ PIO_INST_WAIT_POL_LOW
Wait for 0.
Definition pio.h:161
@ PIO_INST_WAIT_POL_HIGH
Wait for 1.
Definition pio.h:162
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:140
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:295
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:279
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:715
@ PIO_SM1
Mask bit of SM 1.
Definition pio.h:717
@ PIO_SM0
Mask bit of SM 0.
Definition pio.h:716
@ PIO_SM_ALL
All flags above.
Definition pio.h:720
@ PIO_SM2
Mask bit of SM 2.
Definition pio.h:718
@ PIO_SM3
Mask bit of SM 3.
Definition pio.h:719
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:255
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:431
#define PIO_INST_PUSH
PUSH opcode.
Definition pio.h:239
#define PIO_INST_MOV_SOURCE_SHIFT
MOV source shift position.
Definition pio.h:311
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:148
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:399
@ PIO_INST_SET_DST_Y
To scratch Y.
Definition pio.h:402
@ PIO_INST_SET_DST_PINDIRS
To set pin directions.
Definition pio.h:403
@ PIO_INST_SET_DST_PINS
To set pins.
Definition pio.h:400
@ PIO_INST_SET_DST_X
To scratch X.
Definition pio.h:401
static void pio_sm_clear_debug_txstall(pio_t pio, unsigned sm_mask)
Clear TX stall debug flag.
Definition pio.h:1237
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:601
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:331
@ PIO_INST_MOV_OP_REVERSE
Bitwise reverse.
Definition pio.h:334
@ PIO_INST_MOV_OP_NONE
No operation.
Definition pio.h:332
@ PIO_INST_MOV_OP_INVERT
Bitwise complement.
Definition pio.h:333
#define PIO_INST_IN_SOURCE_SHIFT
IN source shift position.
Definition pio.h:183
#define PIO_INST_IRQ_WAIT_SHIFT
IRQ wait until cleared flag shift position.
Definition pio.h:367
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:449
Low-level GPIO peripheral driver interface definitions.
High-level PIO peripheral driver interface.
unsigned pio_t
PIO index type.
Definition pio.h:76
int pio_sm_t
PIO state machine index type.
Definition pio.h:81
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:88
Memory layout of GPIO control register in IO bank 0.
Definition periph_cpu.h:380
Memory layout of GPIO control register in pads bank 0.
Definition periph_cpu.h:290
PIO0_Type * dev
PIO device.
Definition periph_cpu.h:467
Type used to configure PIO gpios pins.
Definition pio.h:83
gpio_t gpio_direction
GPIO directions applied to pins, where the LSBit is the base.
Definition pio.h:87
gpio_pad_ctrl_t pad
Pads bank GPIO control register configuration.
Definition pio.h:84
gpio_io_ctrl_t io
IO bank GPIO control register.
Definition pio.h:85
unsigned gpio_count
Number of GPIOs starting at base.
Definition pio.h:89
gpio_t gpio_base
GPIO base.
Definition pio.h:88
gpio_t gpio_state
GPIO states applied to pins, where the LSBit is the base.
Definition pio.h:86
PIO state machine interrupt callbacks for state machine interrupts.
Definition pio.h:747
void(* sm)(pio_t pio, unsigned irq)
Called when any SM issues an interrupt [0-3].
Definition pio.h:754
PIO interrupt callbacks for FIFO interrupts.
Definition pio.h:727
void(* tx_ready)(pio_t pio, pio_sm_t sm)
Called when Tx FIFO is not full.
Definition pio.h:734
void(* rx_ready)(pio_t pio, pio_sm_t sm)
Called when Rx FIFO is not empty.
Definition pio.h:741
PIO program configuration.
Definition pio.h:760
unsigned pc_start
Initial program counter.
Definition pio.h:761
unsigned wrap_bottom
Instruction index after which the PC wraps around.
Definition pio.h:762
bool sideset_optional
Whether the sideset is optional.
Definition pio.h:765
bool sideset_pindirs
Whether the sideset effects pin directions.
Definition pio.h:766
unsigned sideset_count
Number of bits used for sideset.
Definition pio.h:764
unsigned wrap_top
Instruction index the PC wraps to.
Definition pio.h:763
Struct that models a PIO program.
Definition pio.h:86
PIO clock configuration.
Definition pio.h:772
uint16_t div
Integer divider.
Definition pio.h:773
uint8_t frac_100
Fractional divider, two digits after comma.
Definition pio.h:774
Internal state machine registers.
Definition pio.h:579
volatile uint32_t clkdiv
SMx_CLKDIV.
Definition pio.h:580
volatile uint32_t instr
SMx_INSTR.
Definition pio.h:584
volatile uint32_t pinctrl
SMx_PINCTRL.
Definition pio.h:585
volatile uint32_t shiftctrl
SMx_SHIFTCTRL.
Definition pio.h:582
const volatile uint32_t addr
SMx_ADDR.
Definition pio.h:583
volatile uint32_t execctrl
SMx_EXECCTRL.
Definition pio.h:581