7 #ifndef _HARDWARE_PIO_H
8 #define _HARDWARE_PIO_H
12 #include "hardware/structs/pio.h"
14 #include "hardware/regs/dreq.h"
15 #include "hardware/pio_instructions.h"
18 #ifndef PARAM_ASSERTIONS_ENABLED_PIO
19 #define PARAM_ASSERTIONS_ENABLED_PIO 0
50 static_assert(PIO_SM0_SHIFTCTRL_FJOIN_RX_LSB == PIO_SM0_SHIFTCTRL_FJOIN_TX_LSB + 1,
"");
56 PIO_FIFO_JOIN_NONE = 0,
65 STATUS_TX_LESSTHAN = 0,
66 STATUS_RX_LESSTHAN = 1
109 static inline void check_sm_param(__unused uint sm) {
110 valid_params_if(
PIO, sm < NUM_PIO_STATE_MACHINES);
113 static inline void check_sm_mask(__unused uint mask) {
114 valid_params_if(
PIO, mask < (1u << NUM_PIO_STATE_MACHINES));
118 static inline void check_pio_param(__unused
PIO pio) {
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);
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);
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);
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);
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);
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);
217 invalid_params_if(
PIO, div_int == 0 && div_frac != 0);
219 (((uint)div_frac) << PIO_SM0_CLKDIV_FRAC_LSB) |
220 (((uint)div_int) << PIO_SM0_CLKDIV_INT_LSB);
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;
229 *div_frac = (uint8_t)((div - (
float)*div_int) * (1u << 8u));
251 pio_calculate_clkdiv_from_float(div, &div_int, &div_frac);
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);
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);
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);
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);
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);
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);
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);
402 check_pio_param(pio);
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;
417 check_pio_param(pio);
418 return pio ==
pio1 ? 1 : 0;
434 check_pio_param(pio);
435 valid_params_if(
PIO, pin < 32);
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);
454 return sm + (is_tx ? 0 : NUM_PIO_STATE_MACHINES) + (pio ==
pio0 ? DREQ_PIO0_TX0 : DREQ_PIO1_TX0);
458 const uint16_t *instructions;
547 check_pio_param(pio);
549 pio->ctrl = (pio->ctrl & ~(1u << sm)) | (bool_to_bit(enabled) << sm);
566 check_pio_param(pio);
568 pio->ctrl = (pio->ctrl & ~mask) | (enabled ? mask : 0u);
581 check_pio_param(pio);
583 hw_set_bits(&pio->ctrl, 1u << (PIO_CTRL_SM_RESTART_LSB + sm));
596 check_pio_param(pio);
598 hw_set_bits(&pio->ctrl, (mask << PIO_CTRL_SM_RESTART_LSB) & PIO_CTRL_SM_RESTART_BITS);
623 check_pio_param(pio);
625 hw_set_bits(&pio->ctrl, 1u << (PIO_CTRL_CLKDIV_RESTART_LSB + sm));
658 check_pio_param(pio);
660 hw_set_bits(&pio->ctrl, (mask << PIO_CTRL_CLKDIV_RESTART_LSB) & PIO_CTRL_CLKDIV_RESTART_BITS);
675 check_pio_param(pio);
678 ((mask << PIO_CTRL_CLKDIV_RESTART_LSB) & PIO_CTRL_CLKDIV_RESTART_BITS) |
679 ((mask << PIO_CTRL_SM_ENABLE_LSB) & PIO_CTRL_SM_ENABLE_BITS));
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,
708 check_pio_param(pio);
709 invalid_params_if(
PIO, source >= 12);
724 check_pio_param(pio);
725 invalid_params_if(
PIO, source >= 12);
740 check_pio_param(pio);
741 invalid_params_if(
PIO, source_mask > PIO_INTR_BITS);
757 check_pio_param(pio);
758 invalid_params_if(
PIO, source_mask > PIO_INTR_BITS);
775 invalid_params_if(
PIO, irq_index > 1);
792 invalid_params_if(
PIO, irq_index > 1);
808 check_pio_param(pio);
809 invalid_params_if(
PIO, pio_interrupt_num >= 8);
810 return pio->irq & (1u << pio_interrupt_num);
820 check_pio_param(pio);
821 invalid_params_if(
PIO, pio_interrupt_num >= 8);
822 pio->irq = (1u << pio_interrupt_num);
833 check_pio_param(pio);
835 return (uint8_t) pio->sm[sm].addr;
851 check_pio_param(pio);
853 pio->sm[sm].instr = instr;
864 check_pio_param(pio);
866 return !!(pio->sm[sm].execctrl & PIO_SM0_EXECCTRL_EXEC_STALLED_BITS);
882 check_pio_param(pio);
898 check_pio_param(pio);
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);
919 check_pio_param(pio);
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);
940 check_pio_param(pio);
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);
959 check_pio_param(pio);
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);
976 check_pio_param(pio);
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);
998 check_pio_param(pio);
1000 pio->txf[sm] = data;
1019 check_pio_param(pio);
1021 return pio->rxf[sm];
1032 check_pio_param(pio);
1034 return (pio->fstat & (1u << (PIO_FSTAT_RXFULL_LSB + sm))) != 0;
1045 check_pio_param(pio);
1047 return (pio->fstat & (1u << (PIO_FSTAT_RXEMPTY_LSB + sm))) != 0;
1058 check_pio_param(pio);
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;
1073 check_pio_param(pio);
1075 return (pio->fstat & (1u << (PIO_FSTAT_TXFULL_LSB + sm))) != 0;
1086 check_pio_param(pio);
1088 return (pio->fstat & (1u << (PIO_FSTAT_TXEMPTY_LSB + sm))) != 0;
1099 check_pio_param(pio);
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;
1114 check_pio_param(pio);
1127 check_pio_param(pio);
1157 check_pio_param(pio);
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);
1173 check_pio_param(pio);
1177 pio_calculate_clkdiv_from_float(div, &div_int, &div_frac);
1189 check_pio_param(pio);
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);