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 
9 #pragma once
10 
21 #include "periph_conf.h"
22 #include "periph/gpio.h"
23 #include "periph/pio.h"
24 
25 #ifdef __cplusplus
26 extern "C" {
27 #endif
28 
29 #if defined(CPU_MODEL_RP2040) || defined(DOXYGEN)
33 #define PIO_SM_NUMOF 4
34 
38 #define PIO_INSTR_NUMOF 32
39 
43 #define PIO_IRQ_NUMOF 8
44 #endif
45 
49 typedef uint16_t pio_instr_t;
50 
56 #define PIO_GPIO_INIT_HIGH(pin) (((gpio_t)(1)) << (pin))
57 
63 #define PIO_GPIO_INIT_LOW(pin) ((gpio_t)0)
64 
70 #define PIO_GPIO_INIT_OUT(pin) (((gpio_t)(1)) << (pin))
71 
77 #define PIO_GPIO_INIT_IN(pin) ((gpio_t)0)
78 
82 typedef struct {
85  gpio_t gpio_state;
86  gpio_t gpio_direction;
87  gpio_t gpio_base;
88  unsigned gpio_count;
90 
98 #define PIO_INST_JMP (0u << 13)
102 #define PIO_INST_JMP_MASK (7u << 13)
106 #define PIO_INST_JMP_CONDITION_SHIFT 5
110 #define PIO_INST_JMP_CONDITION_MASK (7u << PIO_INST_JMP_CONDITION_SHIFT)
114 #define PIO_INST_JMP_ADDRESS_MASK (31u)
118 typedef enum {
131 #define PIO_INST_WAIT (1u << 13)
135 #define PIO_INST_WAIT_MASK (7u << 13)
139 #define PIO_INST_WAIT_POL_SHIFT 7
143 #define PIO_INST_WAIT_POL_MASK (1u << PIO_INST_WAIT_POL_SHIFT)
147 #define PIO_INST_WAIT_SOURCE_SHIFT 5
151 #define PIO_INST_WAIT_SOURCE_MASK (3u << PIO_INST_WAIT_SOURCE_SHIFT)
155 #define PIO_INST_WAIT_INDEX_MASK (31u)
159 typedef enum {
166 typedef enum {
174 #define PIO_INST_IN (2u << 13)
178 #define PIO_INST_IN_MASK (7u << 13)
182 #define PIO_INST_IN_SOURCE_SHIFT 5
186 #define PIO_INST_IN_SOURCE_MASK (7u << PIO_INST_IN_SOURCE_SHIFT)
190 #define PIO_INST_IN_BIT_COUNT_MASK (31u)
194 typedef enum {
205 #define PIO_INST_OUT (3u << 13)
209 #define PIO_INST_OUT_MASK (7u << 13)
213 #define PIO_INST_OUT_DESTINATION_SHIFT 5
217 #define PIO_INST_OUT_DESTINATION_MASK (7u << PIO_INST_OUT_DESTINATION_SHIFT)
221 #define PIO_INST_OUT_BIT_COUNT_MASK (31u)
225 typedef enum {
238 #define PIO_INST_PUSH (4u << 13)
242 #define PIO_INST_PUSH_MASK ((7u << 13) | (1u << 7) | 31u)
246 #define PIO_INST_PUSH_IF_FULL_SHIFT 6
250 #define PIO_INST_PUSH_IF_FULL_MASK (1u << PIO_INST_PUSH_IF_FULL_SHIFT)
254 #define PIO_INST_PUSH_BLOCK_SHIFT 5
258 #define PIO_INST_PUSH_BLOCK_MASK (1u << PIO_INST_PUSH_BLOCK_SHIFT)
262 #define PIO_INST_PULL ((4u << 13) | (1u << 7))
266 #define PIO_INST_PULL_MASK ((7u << 13) | (1u << 7) | 31u)
270 #define PIO_INST_PULL_IF_EMPTY_SHIFT 6
274 #define PIO_INST_PULL_IF_EMPTY_MASK (1u << PIO_INST_PULL_IF_EMPTY_SHIFT)
278 #define PIO_INST_PULL_BLOCK_SHIFT 5
282 #define PIO_INST_PULL_BLOCK_MASK (1u << PIO_INST_PULL_BLOCK_SHIFT)
286 #define PIO_INST_MOV (5u << 13)
290 #define PIO_INST_MOV_MASK (7u << 13)
294 #define PIO_INST_MOV_DESTINATION_SHIFT 5
298 #define PIO_INST_MOV_DESTINATION_MASK (7u << PIO_INST_MOV_DESTINATION_SHIFT)
302 #define PIO_INST_MOV_OP_SHIFT 3
306 #define PIO_INST_MOV_OP_MASK (3u << PIO_INST_MOV_OP_SHIFT)
310 #define PIO_INST_MOV_SOURCE_SHIFT 0
314 #define PIO_INST_MOV_SOURCE_MASK (7u)
318 typedef enum {
330 typedef enum {
338 typedef enum {
350 #define PIO_INST_IRQ (6u << 13)
354 #define PIO_INST_IRQ_MASK ((7u << 13) | (1u << 7))
358 #define PIO_INST_IRQ_CLR_SHIFT 6
362 #define PIO_INST_IRQ_CLR_MASK (1u << PIO_INST_IRQ_CLR_SHIFT)
366 #define PIO_INST_IRQ_WAIT_SHIFT 5
370 #define PIO_INST_IRQ_WAIT_MASK (1u << PIO_INST_IRQ_WAIT_SHIFT)
374 #define PIO_INST_IRQ_INDEX_MASK (31u)
378 #define PIO_INST_SET (7u << 13)
382 #define PIO_INST_SET_MASK (7u << 13)
386 #define PIO_INST_SET_DESTINATION_SHIFT 5
390 #define PIO_INST_SET_DESTINATION_MASK (7u << PIO_INST_SET_DESTINATION_SHIFT)
394 #define PIO_INST_SET_DATA_MASK (31u)
398 typedef enum {
404 
414  unsigned address)
415 {
416  return PIO_INST_JMP |
417  (((unsigned)condition) << PIO_INST_JMP_CONDITION_SHIFT) |
418  address;
419 }
431  pio_inst_wait_src_t source,
432  bool relative,
433  unsigned index)
434 {
435  return PIO_INST_WAIT |
436  ((unsigned)polarity << PIO_INST_WAIT_POL_SHIFT) |
437  ((unsigned)source << PIO_INST_WAIT_SOURCE_SHIFT) |
438  ((!!relative) << 4) | index;
439 }
449  unsigned bit_count)
450 {
451  return PIO_INST_IN |
452  ((unsigned)source << PIO_INST_IN_SOURCE_SHIFT) |
453  (bit_count % 32);
454 }
463 static inline pio_instr_t pio_inst_out(pio_inst_out_dst_t destination,
464  unsigned bit_count)
465 {
466  return PIO_INST_OUT |
467  ((unsigned)destination << PIO_INST_OUT_DESTINATION_SHIFT) |
468  (bit_count % 32);
469 }
478 static inline pio_instr_t pio_inst_push(bool if_full,
479  bool block)
480 {
481  return PIO_INST_PUSH |
482  ((!!if_full) << PIO_INST_PUSH_IF_FULL_SHIFT) |
483  ((!!block) << PIO_INST_PUSH_BLOCK_SHIFT);
484 }
493 static inline pio_instr_t pio_inst_pull(bool if_empty,
494  bool block)
495 {
496  return PIO_INST_PULL |
497  ((!!if_empty) << PIO_INST_PULL_IF_EMPTY_SHIFT) |
498  ((!!block) << PIO_INST_PULL_BLOCK_SHIFT);
499 }
509 static inline pio_instr_t pio_inst_mov(pio_inst_mov_dst_t destination,
510  pio_inst_mov_op_t operation,
511  pio_inst_mov_src_t source)
512 {
513  return PIO_INST_MOV |
514  ((unsigned)destination << PIO_INST_MOV_DESTINATION_SHIFT) |
515  ((unsigned)operation << PIO_INST_MOV_OP_SHIFT) |
516  ((unsigned)source << PIO_INST_MOV_SOURCE_SHIFT);
517 }
528 static inline pio_instr_t pio_inst_irq(bool clear,
529  bool wait,
530  bool relative,
531  unsigned index)
532 {
533  return PIO_INST_IRQ |
534  (!!clear << PIO_INST_IRQ_CLR_SHIFT) |
535  (!!wait << PIO_INST_IRQ_WAIT_SHIFT) |
536  ((!!relative << 4) | index);
537 }
546 static inline pio_instr_t pio_inst_set(pio_inst_set_dst_t destination,
547  unsigned data)
548 {
549  return PIO_INST_SET |
550  ((unsigned)destination << PIO_INST_SET_DESTINATION_SHIFT) |
551  data;
552 }
553 
564 static inline pio_instr_t pio_inst_delay_sideset(unsigned sideset,
565  unsigned sideset_count,
566  bool sideset_opt,
567  unsigned delay)
568 {
569  return ((!!sideset_opt) << 12) |
570  (sideset << (13 - (!!sideset_opt + sideset_count))) |
571  (delay << 8);
572 }
578 typedef struct pio_sm_ctrl_regs {
579  volatile uint32_t clkdiv;
580  volatile uint32_t execctrl;
581  volatile uint32_t shiftctrl;
582  const volatile uint32_t addr;
583  volatile uint32_t instr;
584  volatile uint32_t pinctrl;
586 
590 #define PIO_SM_CTRL_BASE(dev) ((pio_sm_ctrl_regs_t *)(&((dev)->SM0_CLKDIV)))
591 
595 #define PIO_SM_CLKDIV_MAX 65536
596 
600 #define PIO_SM_MASK(sm) (1u << (sm))
601 
605 #define PIO_IRQ_MASK(irq) (1u << (irq))
606 
639 static inline unsigned pio_irq_rel_index(unsigned irq_abs, unsigned sm) {
640  assert(irq_abs < PIO_IRQ_NUMOF);
641  assert(sm < PIO_SM_NUMOF);
642  return (irq_abs & 0b100u) | ((irq_abs + sm) & 0b011u);
643 }
644 
655 static inline unsigned pio_irq_rel_sm(unsigned irq_abs, unsigned irq_rel) {
656  assert(irq_abs < PIO_IRQ_NUMOF);
657  assert(irq_rel < PIO_IRQ_NUMOF);
658  return (irq_abs & 0b100u) | ((irq_rel - irq_abs) & 0b011u);
659 }
660 
664 #define PIO_IRQ_REL_MASK(irq, sm) PIO_IRQ_MASK(pio_irq_rel_index(irq, sm))
665 
669 typedef enum {
670  /* lower 4 hardware IRQs which are issued with 'irq <0-3> [rel]' and are routed to NVIC */
671  /* This does not encode the state machine index of which state machine fired! */
672  PIO_IRQ_SM_3 = PIO0_INTR_SM3_Msk,
673  PIO_IRQ_SM_2 = PIO0_INTR_SM2_Msk,
674  PIO_IRQ_SM_1 = PIO0_INTR_SM1_Msk,
675  PIO_IRQ_SM_0 = PIO0_INTR_SM0_Msk,
676  /* FIFO interrupts per state machine */
677  PIO_IRQ_TXNFULL_SM3 = PIO0_INTR_SM3_TXNFULL_Msk,
678  PIO_IRQ_TXNFULL_SM2 = PIO0_INTR_SM2_TXNFULL_Msk,
679  PIO_IRQ_TXNFULL_SM1 = PIO0_INTR_SM1_TXNFULL_Msk,
680  PIO_IRQ_TXNFULL_SM0 = PIO0_INTR_SM0_TXNFULL_Msk,
681  PIO_IRQ_RXNEMPTY_SM3 = PIO0_INTR_SM3_RXNEMPTY_Msk,
682  PIO_IRQ_RXNEMPTY_SM2 = PIO0_INTR_SM2_RXNEMPTY_Msk,
683  PIO_IRQ_RXNEMPTY_SM1 = PIO0_INTR_SM1_RXNEMPTY_Msk,
684  PIO_IRQ_RXNEMPTY_SM0 = PIO0_INTR_SM0_RXNEMPTY_Msk,
685  /* all routed state machine interrupts */
687  PIO_IRQ_SM_2 |
688  PIO_IRQ_SM_1 |
690  /* all interrupts */
701 
705 typedef enum {
710 
714 typedef enum {
719  PIO_SM_ALL = (PIO_SM0 | PIO_SM1 |
720  PIO_SM2 | PIO_SM3)
722 
726 typedef struct pio_isr_vec {
733  void (*tx_ready)(pio_t pio, pio_sm_t sm);
740  void (*rx_ready)(pio_t pio, pio_sm_t sm);
742 
746 typedef struct pio_isr_sm_vec {
753  void (*sm)(pio_t pio, unsigned irq);
755 
759 typedef struct pio_program_conf {
760  unsigned pc_start;
761  unsigned wrap_bottom;
762  unsigned wrap_top;
763  unsigned sideset_count;
767 
771 typedef struct pio_sm_clkdiv {
772  uint16_t div;
773  uint8_t frac_100;
775 
791 
804 
815  int pio_write_program(pio_t pio, pio_program_t *prog, const pio_instr_t *instr);
816 
824 
832 
840 void pio_set_isr_vec(pio_t pio, pio_sm_t sm, const pio_isr_vec_t *vec);
841 
849 void pio_set_isr_sm_vec(pio_t pio, unsigned irq, const pio_isr_sm_vec_t *vec);
850 
862 void pio_sm_set_out_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base, unsigned pin_count);
863 
873 void pio_sm_set_in_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base);
874 
886 void pio_sm_set_set_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base, unsigned pin_count);
887 
896 void pio_sm_set_sideset_pins(pio_t pio, pio_sm_t sm, gpio_t pin_base);
897 
910 void pio_sm_set_sideset_count(pio_t pio, pio_sm_t sm, unsigned pin_count, bool enable);
911 
919 void pio_sm_set_sideset_target(pio_t pio, pio_sm_t sm, bool pindir);
920 
932 
946 
956 void pio_sm_clkdiv_restart(pio_t pio, unsigned sm_mask);
957 
967 void pio_sm_set_wrap(pio_t pio, pio_sm_t sm, unsigned prog_loc, uint8_t top, uint8_t bottom);
968 
978 void pio_sm_set_jmp_pin(pio_t pio, pio_sm_t sm, gpio_t pin);
979 
989 void pio_sm_set_in_shift(pio_t pio, pio_sm_t sm, bool right, bool autopush, unsigned threshold);
990 
1000 void pio_sm_set_out_shift(pio_t pio, pio_sm_t sm, bool right, bool autopull, unsigned threshold);
1001 
1009 
1017 
1025 
1033 
1042 
1051 
1065 int pio_sm_transmit_word(pio_t pio, pio_sm_t sm, uint32_t word);
1066 
1076 void pio_sm_transmit_word_block(pio_t pio, pio_sm_t sm, uint32_t word);
1077 
1088 void pio_sm_transmit_words_block(pio_t pio, pio_sm_t sm, const uint32_t *words, unsigned count);
1089 
1103 int pio_sm_receive_word(pio_t pio, pio_sm_t sm, uint32_t *word);
1104 
1114 void pio_sm_receive_word_block(pio_t pio, pio_sm_t sm, uint32_t *word);
1115 
1126 void pio_sm_receive_words_block(pio_t pio, pio_sm_t sm, uint32_t *words, unsigned count);
1127 
1135 uint32_t pio_irq_get(pio_t pio);
1136 
1143 void pio_irq_clear(pio_t pio, unsigned irq_flags);
1144 
1160  const pio_program_conf_t *conf);
1161 
1172 void pio_sm_set_pindirs_with_mask(pio_t pio, pio_sm_t sm, gpio_t values, gpio_t mask);
1173 
1184 void pio_sm_set_pins_with_mask(pio_t pio, pio_sm_t sm, gpio_t values, gpio_t mask);
1185 
1195 
1205 
1215 
1222 
1229 
1236 static inline void pio_sm_clear_debug_txstall(pio_t pio, unsigned sm_mask)
1237 {
1238  io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1239  ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_TXSTALL_Pos));
1240 }
1241 
1248 static inline void pio_sm_clear_debug_txover(pio_t pio, unsigned sm_mask)
1249 {
1250  io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1251  ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_TXOVER_Pos));
1252 }
1253 
1260 static inline void pio_sm_clear_debug_rxunder(pio_t pio, unsigned sm_mask)
1261 {
1262  io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1263  ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_RXUNDER_Pos));
1264 }
1265 
1272 static inline void pio_sm_clear_debug_rxstall(pio_t pio, unsigned sm_mask)
1273 {
1274  io_reg_atomic_set(&pio_config[pio].dev->FDEBUG,
1275  ((sm_mask & PIO_SM_ALL) << PIO0_FDEBUG_RXSTALL_Pos));
1276 }
1277 
1286 static inline bool pio_sm_tx_fifo_empty(pio_t pio, pio_sm_t sm)
1287 {
1288  return !!(pio_config[pio].dev->FSTAT & ((1u << sm) << PIO0_FSTAT_TXEMPTY_Pos));
1289 }
1290 
1299 static inline bool pio_sm_tx_fifo_full(pio_t pio, pio_sm_t sm)
1300 {
1301  return !!(pio_config[pio].dev->FSTAT & ((1u << sm) << PIO0_FSTAT_TXFULL_Pos));
1302 }
1303 
1312 static inline bool pio_sm_rx_fifo_empty(pio_t pio, pio_sm_t sm)
1313 {
1314  return !!(pio_config[pio].dev->FSTAT & ((1u << sm) << PIO0_FSTAT_RXEMPTY_Pos));
1315 }
1316 
1325 static inline bool pio_sm_rx_fifo_full(pio_t pio, pio_sm_t sm)
1326 {
1327  return !!((pio_config[pio].dev)->FSTAT & ((1u << sm) << PIO0_FSTAT_RXFULL_Pos));
1328 }
1329 
1330 #ifdef __cplusplus
1331 }
1332 #endif
1333 
#define assert(cond)
abort the program if assertion is false
Definition: assert.h:135
static const pio_conf_t pio_config[]
Array of PIO configurations.
Definition: periph_conf.h:140
static unsigned pio_irq_rel_index(unsigned irq_abs, unsigned sm)
Convert absolute IRQ index to relative IRQ index.
Definition: pio.h:639
uint16_t pio_instr_t
Type to represent the width of an instruction.
Definition: pio.h:49
static pio_instr_t pio_inst_jmp(pio_inst_jmp_cond_t condition, unsigned address)
Construct a JMP instruction.
Definition: pio.h:413
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:1248
pio_inst_jmp_cond_t
JMP conditions.
Definition: pio.h:118
@ PIO_INST_JMP_COND_NOT_X_EQ_Y
If scratch X != scratch Y.
Definition: pio.h:124
@ PIO_INST_JMP_COND_NONE
Unconditionally.
Definition: pio.h:119
@ PIO_INST_JMP_COND_PIN
If jump pin is high.
Definition: pio.h:125
@ PIO_INST_JMP_COND_X_DEC
If scratch X– is not 0.
Definition: pio.h:121
@ PIO_INST_JMP_COND_X_ZERO
If scratch X is 0.
Definition: pio.h:120
@ PIO_INST_JMP_COND_Y_ZERO
If scratch Y is 0.
Definition: pio.h:122
@ PIO_INST_JMP_COND_Y_DEC
If scratch Y– is not 0.
Definition: pio.h:123
@ PIO_INST_JMP_COND_NOT_OSR_EMPTY
If OSR is not empty.
Definition: pio.h:126
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:33
pio_inst_out_dst_t
OUT destinations.
Definition: pio.h:225
@ PIO_INST_OUT_DST_PINS
To output pins.
Definition: pio.h:226
@ PIO_INST_OUT_DST_NULL
Discard data.
Definition: pio.h:229
@ PIO_INST_OUT_DST_PC
To program counter.
Definition: pio.h:231
@ PIO_INST_OUT_DST_PINDIRS
To output pin directions.
Definition: pio.h:230
@ PIO_INST_OUT_DST_Y
To scratch Y.
Definition: pio.h:228
@ PIO_INST_OUT_DST_X
To scratch X.
Definition: pio.h:227
@ PIO_INST_OUT_DST_ISR
To input shift register.
Definition: pio.h:232
@ PIO_INST_OUT_DST_EXEC
Execute as instruction.
Definition: pio.h:233
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:106
static pio_instr_t pio_inst_push(bool if_full, bool block)
Construct a PUSH instruction.
Definition: pio.h:478
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:262
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:246
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:270
#define PIO_INST_JMP
JMP opcode.
Definition: pio.h:98
#define PIO_INST_SET_DESTINATION_SHIFT
SET destination shift position.
Definition: pio.h:386
#define PIO_INST_IRQ
IRQ opcode.
Definition: pio.h:350
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:655
#define PIO_INST_MOV_OP_SHIFT
MOV operation shift position.
Definition: pio.h:302
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:564
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:338
@ PIO_INST_MOV_SRC_NULL
Fill with zeros.
Definition: pio.h:342
@ PIO_INST_MOV_SRC_X
From scratch X.
Definition: pio.h:340
@ PIO_INST_MOV_SRC_STATUS
From status.
Definition: pio.h:343
@ PIO_INST_MOV_SRC_Y
From scratch Y.
Definition: pio.h:341
@ PIO_INST_MOV_SRC_OSR
From output shift register.
Definition: pio.h:345
@ PIO_INST_MOV_SRC_ISR
From input shift register.
Definition: pio.h:344
@ PIO_INST_MOV_SRC_PINS
From input pins.
Definition: pio.h:339
#define PIO_INST_OUT
OUT opcode.
Definition: pio.h:205
#define PIO_INST_MOV
MOV opcode.
Definition: pio.h:286
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:1325
static pio_instr_t pio_inst_irq(bool clear, bool wait, bool relative, unsigned index)
Construct an IRQ instruction.
Definition: pio.h:528
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:546
static pio_instr_t pio_inst_out(pio_inst_out_dst_t destination, unsigned bit_count)
Construct an OUT instruction.
Definition: pio.h:463
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:509
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:1299
pio_irq_line_t
PIO interrupt lines.
Definition: pio.h:705
@ PIO_IRQ_LINE_1
IRQ line 1.
Definition: pio.h:707
@ PIO_IRQ_LINE_0
IRQ line 0.
Definition: pio.h:706
@ PIO_IRQ_LINE_NUMOF
Number of IRQ lines.
Definition: pio.h:708
static pio_instr_t pio_inst_pull(bool if_empty, bool block)
Construct a PULL instruction.
Definition: pio.h:493
static bool pio_sm_tx_fifo_empty(pio_t pio, pio_sm_t sm)
Check if TX FIFO is empty.
Definition: pio.h:1286
static void pio_sm_clear_debug_rxstall(pio_t pio, unsigned sm_mask)
Clear RX stall debug flag.
Definition: pio.h:1272
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:1260
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:194
@ PIO_INST_IN_SRC_PINS
From input pins.
Definition: pio.h:195
@ PIO_INST_IN_SRC_Y
From scratch Y.
Definition: pio.h:197
@ PIO_INST_IN_SRC_ISR
From input shift register.
Definition: pio.h:199
@ PIO_INST_IN_SRC_OSR
From output shift register.
Definition: pio.h:200
@ PIO_INST_IN_SRC_X
From scratch X.
Definition: pio.h:196
@ PIO_INST_IN_SRC_NULL
Fill with zeros.
Definition: pio.h:198
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:358
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:318
@ PIO_INST_MOV_DST_PINS
To output pins.
Definition: pio.h:319
@ PIO_INST_MOV_DST_ISR
To input shift register.
Definition: pio.h:324
@ PIO_INST_MOV_DST_Y
To scratch Y.
Definition: pio.h:321
@ PIO_INST_MOV_DST_OSR
To output shift register.
Definition: pio.h:325
@ PIO_INST_MOV_DST_PC
To program counter.
Definition: pio.h:323
@ PIO_INST_MOV_DST_EXEC
Execute data as instruction.
Definition: pio.h:322
@ PIO_INST_MOV_DST_X
To scratch X.
Definition: pio.h:320
pio_inst_wait_src_t
WAIT sources.
Definition: pio.h:166
@ PIO_INST_WAIT_SRC_IRQ
Wait on interrupt.
Definition: pio.h:169
@ PIO_INST_WAIT_SRC_PIN
Wait on input pin.
Definition: pio.h:168
@ PIO_INST_WAIT_SRC_GPIO
Wait on GPIO.
Definition: pio.h:167
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:669
@ PIO_IRQ_TXNFULL_SM3
SM3 TX FIFO is not full.
Definition: pio.h:677
@ PIO_IRQ_TXNFULL_SM2
SM2 TX FIFO is not full.
Definition: pio.h:678
@ PIO_IRQ_SM_3
Any state machine issued irq 3.
Definition: pio.h:672
@ PIO_IRQ_SM_0
any state machine issued irq 0
Definition: pio.h:675
@ PIO_IRQ_RXNEMPTY_SM2
SM2 RX FIFO is not empty.
Definition: pio.h:682
@ PIO_IRQ_RXNEMPTY_SM3
SM3 RX FIFO is not empty.
Definition: pio.h:681
@ PIO_IRQ_SM_2
Any state machine issued irq 2.
Definition: pio.h:673
@ PIO_IRQ_SM_1
Any state machine issued irq 1.
Definition: pio.h:674
@ PIO_IRQ_TXNFULL_SM0
SM0 TX FIFO is not full.
Definition: pio.h:680
@ PIO_IRQ_TXNFULL_SM1
SM1 TX FIFO is not full.
Definition: pio.h:679
@ PIO_IRQ_RXNEMPTY_SM1
SM1 RX FIFO is not empty.
Definition: pio.h:683
@ PIO_IRQ_RXNEMPTY_SM0
SM0 RX FIFO is not empty.
Definition: pio.h:684
@ PIO_IRQ_ALL_SM
All SM interrupts.
Definition: pio.h:686
@ PIO_IRQ_ALL
All flags above.
Definition: pio.h:691
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:378
#define PIO_INST_OUT_DESTINATION_SHIFT
OUT destination shift position.
Definition: pio.h:213
#define PIO_IRQ_NUMOF
Number of interrupt flags per PIO.
Definition: pio.h:43
#define PIO_INST_WAIT
WAIT opcode.
Definition: pio.h:131
#define PIO_INST_IN
IN opcode.
Definition: pio.h:174
static bool pio_sm_rx_fifo_empty(pio_t pio, pio_sm_t sm)
Check if RX FIFO is empty.
Definition: pio.h:1312
pio_inst_wait_pol_t
WAIT polarities.
Definition: pio.h:159
@ PIO_INST_WAIT_POL_LOW
Wait for 0.
Definition: pio.h:160
@ PIO_INST_WAIT_POL_HIGH
Wait for 1.
Definition: pio.h:161
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:139
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:294
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:278
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:714
@ PIO_SM1
Mask bit of SM 1.
Definition: pio.h:716
@ PIO_SM0
Mask bit of SM 0.
Definition: pio.h:715
@ PIO_SM_ALL
All flags above.
Definition: pio.h:719
@ PIO_SM2
Mask bit of SM 2.
Definition: pio.h:717
@ PIO_SM3
Mask bit of SM 3.
Definition: pio.h:718
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:254
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:430
#define PIO_INST_PUSH
PUSH opcode.
Definition: pio.h:238
#define PIO_INST_MOV_SOURCE_SHIFT
MOV source shift position.
Definition: pio.h:310
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:147
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:398
@ PIO_INST_SET_DST_Y
To scratch Y.
Definition: pio.h:401
@ PIO_INST_SET_DST_PINDIRS
To set pin directions.
Definition: pio.h:402
@ PIO_INST_SET_DST_PINS
To set pins.
Definition: pio.h:399
@ PIO_INST_SET_DST_X
To scratch X.
Definition: pio.h:400
static void pio_sm_clear_debug_txstall(pio_t pio, unsigned sm_mask)
Clear TX stall debug flag.
Definition: pio.h:1236
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:600
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:330
@ PIO_INST_MOV_OP_REVERSE
Bitwise reverse.
Definition: pio.h:333
@ PIO_INST_MOV_OP_NONE
No operation.
Definition: pio.h:331
@ PIO_INST_MOV_OP_INVERT
Bitwise complement.
Definition: pio.h:332
#define PIO_INST_IN_SOURCE_SHIFT
IN source shift position.
Definition: pio.h:182
#define PIO_INST_IRQ_WAIT_SHIFT
IRQ wait until cleared flag shift position.
Definition: pio.h:366
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:448
High-level PIO peripheral driver interface.
Low-level GPIO peripheral driver interface definitions.
unsigned pio_t
PIO index type.
Definition: pio.h:70
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:87
Memory layout of GPIO control register in IO bank 0.
Definition: periph_cpu.h:379
Memory layout of GPIO control register in pads bank 0.
Definition: periph_cpu.h:289
PIO0_Type * dev
PIO device.
Definition: periph_cpu.h:466
Type used to configure PIO gpios pins.
Definition: pio.h:82
gpio_t gpio_direction
GPIO directions applied to pins, where the LSBit is the base.
Definition: pio.h:86
gpio_pad_ctrl_t pad
Pads bank GPIO control register configuration.
Definition: pio.h:83
gpio_io_ctrl_t io
IO bank GPIO control register.
Definition: pio.h:84
unsigned gpio_count
Number of GPIOs starting at base.
Definition: pio.h:88
gpio_t gpio_base
GPIO base.
Definition: pio.h:87
gpio_t gpio_state
GPIO states applied to pins, where the LSBit is the base.
Definition: pio.h:85
PIO state machine interrupt callbacks for state machine interrupts.
Definition: pio.h:746
void(* sm)(pio_t pio, unsigned irq)
Called when any SM issues an interrupt [0-3].
Definition: pio.h:753
PIO interrupt callbacks for FIFO interrupts.
Definition: pio.h:726
void(* tx_ready)(pio_t pio, pio_sm_t sm)
Called when Tx FIFO is not full.
Definition: pio.h:733
void(* rx_ready)(pio_t pio, pio_sm_t sm)
Called when Rx FIFO is not empty.
Definition: pio.h:740
PIO program configuration.
Definition: pio.h:759
unsigned pc_start
Initial program counter.
Definition: pio.h:760
unsigned wrap_bottom
Instruction index after which the PC wraps around.
Definition: pio.h:761
bool sideset_optional
Whether the sideset is optional.
Definition: pio.h:764
bool sideset_pindirs
Whether the sideset effects pin directions.
Definition: pio.h:765
unsigned sideset_count
Number of bits used for sideset.
Definition: pio.h:763
unsigned wrap_top
Instruction index the PC wraps to.
Definition: pio.h:762
Struct that models a PIO program.
Definition: pio.h:85
PIO clock configuration.
Definition: pio.h:771
uint16_t div
Integer divider.
Definition: pio.h:772
uint8_t frac_100
Fractional divider, two digits after comma.
Definition: pio.h:773
Internal state machine registers.
Definition: pio.h:578
volatile uint32_t clkdiv
SMx_CLKDIV.
Definition: pio.h:579
volatile uint32_t instr
SMx_INSTR.
Definition: pio.h:583
volatile uint32_t pinctrl
SMx_PINCTRL.
Definition: pio.h:584
volatile uint32_t shiftctrl
SMx_SHIFTCTRL.
Definition: pio.h:581
const volatile uint32_t addr
SMx_ADDR.
Definition: pio.h:582
volatile uint32_t execctrl
SMx_EXECCTRL.
Definition: pio.h:580