23 #ifndef UTIL_H_BC9FF78B_B978_444A_8AA1_FF169B09B09E
24 #define UTIL_H_BC9FF78B_B978_444A_8AA1_FF169B09B09E
27 #include "hardware/pio.h"
28 #include "hardware/platform_defs.h"
29 #include "hardware/sync.h"
30 #include "pico/mutex.h"
31 #include "pico/types.h"
38 #define UTIL_NUM_DMA_IRQS UINT8_C(2)
40 #define UTIL_DMA_IRQ_INDEX_MIN UINT8_C(0)
41 #define UTIL_DMA_IRQ_INDEX_MAX UINT8_C(UTIL_NUM_DMA_IRQS - 1)
43 #define UTIL_PIO_IRQ_INDEX_MIN UINT8_C(0)
44 #define UTIL_PIO_IRQ_INDEX_MAX UINT8_C(NUM_PIOS - 1)
46 #define UTIL_PIO_INTERRUPT_NUM_MIN UINT8_C(0)
47 #define UTIL_PIO_INTERRUPT_NUM_MAX UINT8_C(7)
49 #define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MIN UINT8_C(0)
50 #define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MAX UINT8_C(3)
59 #define UTIL_MUTEX_BLOCK(mut, ...) \
61 mutex_enter_blocking(&mut); \
75 #define UTIL_INTERRUPTS_OFF_BLOCK(...) \
77 const uint32_t interrupt_status_cb918069_eadf_49bc_9d8c_a8a4defad20c = save_and_disable_interrupts(); \
79 restore_interrupts(interrupt_status_cb918069_eadf_49bc_9d8c_a8a4defad20c); \
82 #define UTIL_DECL_IN_RANGE_FUNC(TYPE) \
83 bool util_ ## TYPE ##_in_range( \
144 const uint irq_index,
146 const irq_handler_t handler,
170 const absolute_time_t*
const end);
220 const uint irq_index,
221 const uint pio_interrupt_num,
222 const irq_handler_t handler,
274 const uint irq_index);
287 const uint pio_interrupt_num);
351 const uint pio_interrupt_num);
362 const uint pio_interrupt_num);
372 const uint pio_interrupt_num);
382 const uint pio_interrupt_num);
396 const uint pio_interrupt_num,
397 const absolute_time_t*
const end);
408 const uint pio_interrupt_num);
422 const uint pio_interrupt_num,
423 const absolute_time_t*
const end);
437 const uint pio_interrupt_num,
438 const absolute_time_t*
const end);
455 uint32_t*
const word,
456 const uint threshold);
458 #undef UTIL_DECL_IN_RANGE_FUNC
void util_irq_set_exclusive_pio_interrupt_num_handler(PIO const pio, const uint irq_index, const uint pio_interrupt_num, const irq_handler_t handler, const bool enabled)
Set and enable an exclusive interrupt handler for a given pio_interrupt_num.
void util_gpio_set_output(const uint gpio)
Initialises and sets GPIO pin to output.
bool util_pio_sm_is_enabled(PIO const pio, const uint sm)
Check whether a given state machine is enabled.
void util_pio_sm_clear_osr(PIO const pio, const uint sm)
Clears a given state machine's OSR.
bool util_pio_interrupt_wait_timeout(PIO const pio, const uint pio_interrupt_num, const absolute_time_t *const end)
Waits for a given PIO to be set within the timeout period.
bool util_pio_interrupt_wait_clear_timeout(PIO const pio, const uint pio_interrupt_num, const absolute_time_t *const end)
Waits for a given PIO to be set within the timeout period and then clears it.
PIO const util_pio_get_pio_from_irq(const uint irq_num)
Gets the PIO using the NVIC IRQ number.
bool util_pio_irq_index_is_valid(const uint idx)
Check whether PIO IRQ index is valid.
bool util_routable_pio_interrupt_num_is_valid(const uint pio_interrupt_num)
Check whether a PIO interrupt number is a valid routable interrupt number.
int util_pio_get_index_from_irq(const uint irq_num)
Gets the PIO IRQ index using the NVIC IRQ number.
void util_pio_interrupt_wait_cleared(PIO const pio, const uint pio_interrupt_num)
Waits until a given PIO interrupt is cleared.
void util_pio_sm_clear_isr(PIO const pio, const uint sm)
Clears a given state machine's ISR.
void util_pio_sm_clear_rx_fifo(PIO const pio, const uint sm)
Clears a given state machine's RX FIFO.
int util_dma_get_index_from_irq(const uint irq_num)
Gets the DMA IRQ index using the NVIC IRQ number.
void util_pio_gpio_contiguous_init(PIO const pio, const uint base, const uint len)
Inits GPIO pins for PIO from base to base + len.
uint util_pio_get_pis_from_pio_interrupt_num(const uint pio_interrupt_num)
const uint8_t util_pio_to_irq_map[NUM_PIOS *2]
Quick lookup for finding an NVIC IRQ number for a PIO and interrupt index number.
bool util_dma_channel_wait_for_finish_timeout(const uint channel, const absolute_time_t *const end)
Wait until channel has completed transferring up to a timeout.
const uint8_t util_dma_to_irq_map[UTIL_NUM_DMA_IRQS]
Quick lookup for finding an NVIC IRQ number for a DMA interrupt index number.
void util_dma_channel_set_quiet(const uint channel, const bool quiet)
Sets a DMA channel's IRQ quiet mode.
#define UTIL_NUM_DMA_IRQS
void util_pio_interrupt_wait_clear(PIO const pio, const uint pio_interrupt_num)
Waits for a given PIO interrupt to be set and then clears it.
void util_gpio_set_contiguous_input_pins(const uint base, const uint len)
Sets GPIO pins from base to base + len to input.
uint util_dma_get_irqn(const uint irq_index)
Gets the NVIC IRQ number based on the DMA IRQ index.
uint util_pio_get_irq_from_index(PIO const pio, const uint idx)
Gets the NVIC PIO IRQ number using a PIO pointer and PIO IRQ index.
uint32_t util_dma_get_transfer_count(const uint channel)
Get the transfer count for a given DMA channel. When a DMA transfer is active, this count is the numb...
bool util_pio_interrupt_wait_cleared_timeout(PIO const pio, const uint pio_interrupt_num, const absolute_time_t *const end)
Waits until the given interrupt is cleared, up to a maximum timeout.
void util_pio_interrupt_wait(PIO const pio, const uint pio_interrupt_num)
Waits for a given PIO interrupt to be set.
bool util_pio_sm_try_get(PIO const pio, const uint sm, uint32_t *const word, const uint threshold)
Attempts to get a word from the state machine's RX FIFO if more than threshold words are in the buffe...
void util_dma_set_exclusive_channel_irq_handler(const uint irq_index, const uint channel, const irq_handler_t handler, const bool enabled)
Set and enable an exclusive handler for a DMA channel.
bool util_dma_irq_index_is_valid(const uint idx)
Check whether a DMA IRQ index is valid.
#define UTIL_DECL_IN_RANGE_FUNC(TYPE)
bool util_pio_interrupt_num_is_valid(const uint pio_interrupt_num)
Check whether a PIO interrupt number is valid.
uint util_dma_get_irq_from_index(const uint idx)
Gets the NVIC DMA IRQ number using the DMA IRQ index.
uint util_pion_get_irqn(PIO const pio, const uint irq_index)