hx711-pico-c
util.h
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2023 Daniel Robertson
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #ifndef UTIL_H_BC9FF78B_B978_444A_8AA1_FF169B09B09E
24 #define UTIL_H_BC9FF78B_B978_444A_8AA1_FF169B09B09E
25 
26 #include <stdint.h>
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"
32 
33 #ifdef __cplusplus
34 extern "C" {
35 #endif
36 
37 // RP2040 sdk doesn't seem to define this
38 #define UTIL_NUM_DMA_IRQS UINT8_C(2)
39 
40 #define UTIL_DMA_IRQ_INDEX_MIN UINT8_C(0)
41 #define UTIL_DMA_IRQ_INDEX_MAX UINT8_C(UTIL_NUM_DMA_IRQS - 1)
42 
43 #define UTIL_PIO_IRQ_INDEX_MIN UINT8_C(0)
44 #define UTIL_PIO_IRQ_INDEX_MAX UINT8_C(NUM_PIOS - 1)
45 
46 #define UTIL_PIO_INTERRUPT_NUM_MIN UINT8_C(0)
47 #define UTIL_PIO_INTERRUPT_NUM_MAX UINT8_C(7)
48 
49 #define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MIN UINT8_C(0)
50 #define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MAX UINT8_C(3)
51 
59 #define UTIL_MUTEX_BLOCK(mut, ...) \
60  do { \
61  mutex_enter_blocking(&mut); \
62  __VA_ARGS__ \
63  mutex_exit(&mut); \
64  } while(0)
65 
75 #define UTIL_INTERRUPTS_OFF_BLOCK(...) \
76  do { \
77  const uint32_t interrupt_status_cb918069_eadf_49bc_9d8c_a8a4defad20c = save_and_disable_interrupts(); \
78  __VA_ARGS__ \
79  restore_interrupts(interrupt_status_cb918069_eadf_49bc_9d8c_a8a4defad20c); \
80  } while(0)
81 
82 #define UTIL_DECL_IN_RANGE_FUNC(TYPE) \
83  bool util_ ## TYPE ##_in_range( \
84  const TYPE val, \
85  const TYPE min, \
86  const TYPE max);
87 
92 
93 
99 extern const uint8_t util_pio_to_irq_map[NUM_PIOS * 2];
100 
105 extern const uint8_t util_dma_to_irq_map[UTIL_NUM_DMA_IRQS];
106 
114 bool util_dma_irq_index_is_valid(const uint idx);
115 
123 uint util_dma_get_irq_from_index(const uint idx);
124 
132 int util_dma_get_index_from_irq(const uint irq_num);
133 
144  const uint irq_index,
145  const uint channel,
146  const irq_handler_t handler,
147  const bool enabled);
148 
157 uint32_t util_dma_get_transfer_count(const uint channel);
158 
169  const uint channel,
170  const absolute_time_t* const end);
171 
179 uint util_dma_get_irqn(const uint irq_index);
180 
188  const uint channel,
189  const bool quiet);
190 
198  const uint base,
199  const uint len);
200 
206 void util_gpio_set_output(const uint gpio);
207 
219  PIO const pio,
220  const uint irq_index,
221  const uint pio_interrupt_num,
222  const irq_handler_t handler,
223  const bool enabled);
224 
232 bool util_pio_irq_index_is_valid(const uint idx);
233 
242  PIO const pio,
243  const uint idx);
244 
252 int util_pio_get_index_from_irq(const uint irq_num);
253 
260 PIO const util_pio_get_pio_from_irq(const uint irq_num);
261 
272 uint util_pion_get_irqn(
273  PIO const pio,
274  const uint irq_index);
275 
287  const uint pio_interrupt_num);
288 
297  PIO const pio,
298  const uint base,
299  const uint len);
300 
308  PIO const pio,
309  const uint sm);
310 
318  PIO const pio,
319  const uint sm);
320 
328  PIO const pio,
329  const uint sm);
330 
340  PIO const pio,
341  const uint sm);
342 
351  const uint pio_interrupt_num);
352 
362  const uint pio_interrupt_num);
363 
371  PIO const pio,
372  const uint pio_interrupt_num);
373 
381  PIO const pio,
382  const uint pio_interrupt_num);
383 
395  PIO const pio,
396  const uint pio_interrupt_num,
397  const absolute_time_t* const end);
398 
407  PIO const pio,
408  const uint pio_interrupt_num);
409 
421  PIO const pio,
422  const uint pio_interrupt_num,
423  const absolute_time_t* const end);
424 
436  PIO const pio,
437  const uint pio_interrupt_num,
438  const absolute_time_t* const end);
439 
453  PIO const pio,
454  const uint sm,
455  uint32_t* const word,
456  const uint threshold);
457 
458 #undef UTIL_DECL_IN_RANGE_FUNC
459 
460 #ifdef __cplusplus
461 }
462 #endif
463 
464 #endif
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.
Definition: util.c:191
void util_gpio_set_output(const uint gpio)
Initialises and sets GPIO pin to output.
Definition: util.c:185
bool util_pio_sm_is_enabled(PIO const pio, const uint sm)
Check whether a given state machine is enabled.
Definition: util.c:366
void util_pio_sm_clear_osr(PIO const pio, const uint sm)
Clears a given state machine's OSR.
Definition: util.c:344
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.
Definition: util.c:439
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.
Definition: util.c:459
PIO const util_pio_get_pio_from_irq(const uint irq_num)
Gets the PIO using the NVIC IRQ number.
Definition: util.c:284
bool util_pio_irq_index_is_valid(const uint idx)
Check whether PIO IRQ index is valid.
Definition: util.c:226
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.
Definition: util.c:382
int util_pio_get_index_from_irq(const uint irq_num)
Gets the PIO IRQ index using the NVIC IRQ number.
Definition: util.c:267
void util_pio_interrupt_wait_cleared(PIO const pio, const uint pio_interrupt_num)
Waits until a given PIO interrupt is cleared.
Definition: util.c:400
void util_pio_sm_clear_isr(PIO const pio, const uint sm)
Clears a given state machine's ISR.
Definition: util.c:355
void util_pio_sm_clear_rx_fifo(PIO const pio, const uint sm)
Clears a given state machine's RX FIFO.
Definition: util.c:334
int util_dma_get_index_from_irq(const uint irq_num)
Gets the DMA IRQ index using the NVIC IRQ number.
Definition: util.c:78
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.
Definition: util.c:317
uint util_pio_get_pis_from_pio_interrupt_num(const uint pio_interrupt_num)
Definition: util.c:301
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.
Definition: util.c:48
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.
Definition: util.c:126
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.
Definition: util.c:55
void util_dma_channel_set_quiet(const uint channel, const bool quiet)
Sets a DMA channel's IRQ quiet mode.
Definition: util.c:161
#define UTIL_NUM_DMA_IRQS
Definition: util.h:38
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.
Definition: util.c:430
void util_gpio_set_contiguous_input_pins(const uint base, const uint len)
Sets GPIO pins from base to base + len to input.
Definition: util.c:170
uint util_dma_get_irqn(const uint irq_index)
Gets the NVIC IRQ number based on the DMA IRQ index.
Definition: util.c:144
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.
Definition: util.c:250
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...
Definition: util.c:121
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.
Definition: util.c:410
void util_pio_interrupt_wait(PIO const pio, const uint pio_interrupt_num)
Waits for a given PIO interrupt to be set.
Definition: util.c:390
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...
Definition: util.c:484
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.
Definition: util.c:94
bool util_dma_irq_index_is_valid(const uint idx)
Check whether a DMA IRQ index is valid.
Definition: util.c:65
#define UTIL_DECL_IN_RANGE_FUNC(TYPE)
Definition: util.h:82
bool util_pio_interrupt_num_is_valid(const uint pio_interrupt_num)
Check whether a PIO interrupt number is valid.
Definition: util.c:374
uint util_dma_get_irq_from_index(const uint idx)
Gets the NVIC DMA IRQ number using the DMA IRQ index.
Definition: util.c:72
uint util_pion_get_irqn(PIO const pio, const uint irq_index)
Definition: util.c:233