hx711-pico-c
util.h File Reference
#include <stdint.h>
#include "hardware/pio.h"
#include "hardware/platform_defs.h"
#include "hardware/sync.h"
#include "pico/mutex.h"
#include "pico/types.h"

Go to the source code of this file.

Macros

#define UTIL_NUM_DMA_IRQS   UINT8_C(2)
 
#define UTIL_DMA_IRQ_INDEX_MIN   UINT8_C(0)
 
#define UTIL_DMA_IRQ_INDEX_MAX   UINT8_C(UTIL_NUM_DMA_IRQS - 1)
 
#define UTIL_PIO_IRQ_INDEX_MIN   UINT8_C(0)
 
#define UTIL_PIO_IRQ_INDEX_MAX   UINT8_C(NUM_PIOS - 1)
 
#define UTIL_PIO_INTERRUPT_NUM_MIN   UINT8_C(0)
 
#define UTIL_PIO_INTERRUPT_NUM_MAX   UINT8_C(7)
 
#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MIN   UINT8_C(0)
 
#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MAX   UINT8_C(3)
 
#define UTIL_MUTEX_BLOCK(mut, ...)
 
#define UTIL_INTERRUPTS_OFF_BLOCK(...)
 
#define UTIL_DECL_IN_RANGE_FUNC(TYPE)
 

Functions

bool util_dma_irq_index_is_valid (const uint idx)
 Check whether a DMA IRQ index is valid. More...
 
uint util_dma_get_irq_from_index (const uint idx)
 Gets the NVIC DMA IRQ number using the DMA IRQ index. More...
 
int util_dma_get_index_from_irq (const uint irq_num)
 Gets the DMA IRQ index using the NVIC IRQ number. More...
 
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. More...
 
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 number of transfers remaining. More...
 
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. More...
 
uint util_dma_get_irqn (const uint irq_index)
 Gets the NVIC IRQ number based on the DMA IRQ index. More...
 
void util_dma_channel_set_quiet (const uint channel, const bool quiet)
 Sets a DMA channel's IRQ quiet mode. More...
 
void util_gpio_set_contiguous_input_pins (const uint base, const uint len)
 Sets GPIO pins from base to base + len to input. More...
 
void util_gpio_set_output (const uint gpio)
 Initialises and sets GPIO pin to output. More...
 
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. More...
 
bool util_pio_irq_index_is_valid (const uint idx)
 Check whether PIO IRQ index is valid. More...
 
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. More...
 
int util_pio_get_index_from_irq (const uint irq_num)
 Gets the PIO IRQ index using the NVIC IRQ number. More...
 
PIO const util_pio_get_pio_from_irq (const uint irq_num)
 Gets the PIO using the NVIC IRQ number. More...
 
uint util_pion_get_irqn (PIO const pio, const uint irq_index)
 
uint util_pio_get_pis_from_pio_interrupt_num (const uint pio_interrupt_num)
 
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. More...
 
void util_pio_sm_clear_rx_fifo (PIO const pio, const uint sm)
 Clears a given state machine's RX FIFO. More...
 
void util_pio_sm_clear_osr (PIO const pio, const uint sm)
 Clears a given state machine's OSR. More...
 
void util_pio_sm_clear_isr (PIO const pio, const uint sm)
 Clears a given state machine's ISR. More...
 
bool util_pio_sm_is_enabled (PIO const pio, const uint sm)
 Check whether a given state machine is enabled. More...
 
bool util_pio_interrupt_num_is_valid (const uint pio_interrupt_num)
 Check whether a PIO interrupt number is valid. More...
 
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. More...
 
void util_pio_interrupt_wait (PIO const pio, const uint pio_interrupt_num)
 Waits for a given PIO interrupt to be set. More...
 
void util_pio_interrupt_wait_cleared (PIO const pio, const uint pio_interrupt_num)
 Waits until a given PIO interrupt is cleared. More...
 
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. More...
 
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. More...
 
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. More...
 
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. More...
 
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 buffer. More...
 

Variables

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. More...
 
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. More...
 

Macro Definition Documentation

◆ UTIL_DECL_IN_RANGE_FUNC

#define UTIL_DECL_IN_RANGE_FUNC (   TYPE)
Value:
bool util_ ## TYPE ##_in_range( \
const TYPE val, \
const TYPE min, \
const TYPE max);

Definition at line 82 of file util.h.

◆ UTIL_DMA_IRQ_INDEX_MAX

#define UTIL_DMA_IRQ_INDEX_MAX   UINT8_C(UTIL_NUM_DMA_IRQS - 1)

Definition at line 41 of file util.h.

◆ UTIL_DMA_IRQ_INDEX_MIN

#define UTIL_DMA_IRQ_INDEX_MIN   UINT8_C(0)

Definition at line 40 of file util.h.

◆ UTIL_INTERRUPTS_OFF_BLOCK

#define UTIL_INTERRUPTS_OFF_BLOCK (   ...)
Value:
do { \
const uint32_t interrupt_status_cb918069_eadf_49bc_9d8c_a8a4defad20c = save_and_disable_interrupts(); \
__VA_ARGS__ \
restore_interrupts(interrupt_status_cb918069_eadf_49bc_9d8c_a8a4defad20c); \
} while(0)

Definition at line 75 of file util.h.

◆ UTIL_MUTEX_BLOCK

#define UTIL_MUTEX_BLOCK (   mut,
  ... 
)
Value:
do { \
mutex_enter_blocking(&mut); \
__VA_ARGS__ \
mutex_exit(&mut); \
} while(0)

Definition at line 59 of file util.h.

◆ UTIL_NUM_DMA_IRQS

#define UTIL_NUM_DMA_IRQS   UINT8_C(2)

Definition at line 38 of file util.h.

◆ UTIL_PIO_INTERRUPT_NUM_MAX

#define UTIL_PIO_INTERRUPT_NUM_MAX   UINT8_C(7)

Definition at line 47 of file util.h.

◆ UTIL_PIO_INTERRUPT_NUM_MIN

#define UTIL_PIO_INTERRUPT_NUM_MIN   UINT8_C(0)

Definition at line 46 of file util.h.

◆ UTIL_PIO_IRQ_INDEX_MAX

#define UTIL_PIO_IRQ_INDEX_MAX   UINT8_C(NUM_PIOS - 1)

Definition at line 44 of file util.h.

◆ UTIL_PIO_IRQ_INDEX_MIN

#define UTIL_PIO_IRQ_INDEX_MIN   UINT8_C(0)

Definition at line 43 of file util.h.

◆ UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MAX

#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MAX   UINT8_C(3)

Definition at line 50 of file util.h.

◆ UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MIN

#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MIN   UINT8_C(0)

Definition at line 49 of file util.h.

Function Documentation

◆ util_dma_channel_set_quiet()

void util_dma_channel_set_quiet ( const uint  channel,
const bool  quiet 
)

Sets a DMA channel's IRQ quiet mode.

Parameters
channel
quiettrue for quiet otherwise false

Definition at line 161 of file util.c.

163  {
164  check_dma_channel_param(channel);
165  dma_channel_config cfg = dma_get_channel_config(channel);
166  channel_config_set_irq_quiet(&cfg, quiet);
167  dma_channel_set_config(channel, &cfg, false);
168 }

Referenced by hx711_multi_close().

◆ util_dma_channel_wait_for_finish_timeout()

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.

Parameters
channel
end
Returns
true if transfer completed
false is timeout was reached

Definition at line 126 of file util.c.

128  {
129 
130  check_dma_channel_param(channel);
131  assert(end != NULL);
132  assert(!is_nil_time(*end));
133 
134  while(!time_reached(*end)) {
135  if(!dma_channel_is_busy(channel)) {
136  return true;
137  }
138  }
139 
140  return false;
141 
142 }

◆ util_dma_get_index_from_irq()

int util_dma_get_index_from_irq ( const uint  irq_num)

Gets the DMA IRQ index using the NVIC IRQ number.

Parameters
irq_num
Returns
int -1 is returned for no match.

Definition at line 78 of file util.c.

78  {
79 
80  check_irq_param(irq_num);
81  assert(util_dma_to_irq_map != NULL);
82 
83  switch(irq_num) {
84  case DMA_IRQ_0:
85  return 0;
86  case DMA_IRQ_1:
87  return 1;
88  default:
89  return -1;
90  }
91 
92 }
const uint8_t util_dma_to_irq_map[]
Quick lookup for finding an NVIC IRQ number for a DMA interrupt index number.
Definition: util.c:55

References util_dma_to_irq_map.

◆ util_dma_get_irq_from_index()

uint util_dma_get_irq_from_index ( const uint  idx)

Gets the NVIC DMA IRQ number using the DMA IRQ index.

Parameters
idx
Returns
uint

Definition at line 72 of file util.c.

72  {
73  assert(util_dma_irq_index_is_valid(idx));
74  assert(util_dma_to_irq_map != NULL);
75  return util_dma_to_irq_map[idx];
76 }
bool util_dma_irq_index_is_valid(const uint idx)
Check whether a DMA IRQ index is valid.
Definition: util.c:65

References util_dma_irq_index_is_valid(), and util_dma_to_irq_map.

◆ util_dma_get_irqn()

uint util_dma_get_irqn ( const uint  irq_index)

Gets the NVIC IRQ number based on the DMA IRQ index.

Parameters
irq_index0 or 1
Returns
uint DMA_IRQ_0 or DMA_IRQ_1

Definition at line 144 of file util.c.

144  {
145 
146  assert(util_dma_to_irq_map != NULL);
147  assert(util_uint_in_range(
148  irq_index,
151 
152  const uint irq_num = util_dma_to_irq_map[
153  irq_index];
154 
155  check_irq_param(irq_num);
156 
157  return irq_num;
158 
159 }
#define UTIL_DMA_IRQ_INDEX_MIN
Definition: util.h:40
#define UTIL_DMA_IRQ_INDEX_MAX
Definition: util.h:41

References UTIL_DMA_IRQ_INDEX_MAX, UTIL_DMA_IRQ_INDEX_MIN, and util_dma_to_irq_map.

Referenced by hx711_multi__async_dma_irq_handler(), hx711_multi__init_irq(), hx711_multi__is_initd(), hx711_multi_close(), and util_dma_set_exclusive_channel_irq_handler().

◆ util_dma_get_transfer_count()

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 number of transfers remaining.

Parameters
channel
Returns
uint32_t

Definition at line 121 of file util.c.

121  {
122  check_dma_channel_param(channel);
123  return (uint32_t)dma_hw->ch[channel].transfer_count;
124 }

◆ util_dma_irq_index_is_valid()

bool util_dma_irq_index_is_valid ( const uint  idx)

Check whether a DMA IRQ index is valid.

Parameters
idx
Returns
true
false

Definition at line 65 of file util.c.

65  {
66  return util_uint_in_range(
67  idx,
70 }

References UTIL_DMA_IRQ_INDEX_MAX, and UTIL_DMA_IRQ_INDEX_MIN.

Referenced by hx711_multi__init_asert(), util_dma_get_irq_from_index(), and util_dma_set_exclusive_channel_irq_handler().

◆ util_dma_set_exclusive_channel_irq_handler()

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.

Parameters
irq_index
channel
handler
enabled

Definition at line 94 of file util.c.

98  {
99 
100  assert(util_dma_irq_index_is_valid(irq_index));
101  check_dma_channel_param(channel);
102  assert(handler != NULL);
103 
104  const uint irq_num = util_dma_get_irqn(irq_index);
105 
106  dma_irqn_set_channel_enabled(
107  irq_index,
108  channel,
109  enabled);
110 
111  irq_set_exclusive_handler(
112  irq_num,
113  handler);
114 
115  irq_set_enabled(
116  irq_num,
117  enabled);
118 
119 }
uint util_dma_get_irqn(const uint irq_index)
Gets the NVIC IRQ number based on the DMA IRQ index.
Definition: util.c:144

References util_dma_get_irqn(), and util_dma_irq_index_is_valid().

◆ util_gpio_set_contiguous_input_pins()

void util_gpio_set_contiguous_input_pins ( const uint  base,
const uint  len 
)

Sets GPIO pins from base to base + len to input.

Parameters
base
len

Definition at line 170 of file util.c.

172  {
173 
174  assert(len > 0);
175 
176  const uint l = base + len - 1;
177 
178  for(uint i = base; i <= l; ++i) {
179  check_gpio_param(i);
180  gpio_set_input_enabled(i, true);
181  }
182 
183 }

Referenced by hx711_multi_init().

◆ util_gpio_set_output()

void util_gpio_set_output ( const uint  gpio)

Initialises and sets GPIO pin to output.

Parameters
gpio

Definition at line 185 of file util.c.

185  {
186  check_gpio_param(gpio);
187  gpio_init(gpio);
188  gpio_set_dir(gpio, true);
189 }

Referenced by hx711_init(), and hx711_multi_init().

◆ util_irq_set_exclusive_pio_interrupt_num_handler()

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.

Parameters
pio
irq_index
pio_interrupt_num
handler
enabled

Definition at line 191 of file util.c.

196  {
197 
198  check_pio_param(pio);
199  assert(util_pio_irq_index_is_valid(irq_index));
200  assert(util_routable_pio_interrupt_num_is_valid(pio_interrupt_num));
201  assert(handler != NULL);
202 
203  const uint irq_num = util_pio_get_irq_from_index(
204  pio,
205  irq_index);
206 
208  pio_interrupt_num);
209 
210  pio_set_irqn_source_enabled(
211  pio,
212  irq_index,
213  pis,
214  enabled);
215 
216  irq_set_exclusive_handler(
217  irq_num,
218  handler);
219 
220  irq_set_enabled(
221  irq_num,
222  enabled);
223 
224 }
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
uint util_pio_get_pis_from_pio_interrupt_num(const uint pio_interrupt_num)
Definition: util.c:301
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

References util_pio_get_irq_from_index(), util_pio_get_pis_from_pio_interrupt_num(), util_pio_irq_index_is_valid(), and util_routable_pio_interrupt_num_is_valid().

◆ util_pio_get_index_from_irq()

int util_pio_get_index_from_irq ( const uint  irq_num)

Gets the PIO IRQ index using the NVIC IRQ number.

Parameters
irq_num
Returns
int -1 is returned for no match.

Definition at line 267 of file util.c.

267  {
268 
269  check_irq_param(irq_num);
270 
271  switch(irq_num) {
272  case PIO0_IRQ_0:
273  case PIO1_IRQ_0:
274  return 0;
275  case PIO0_IRQ_1:
276  case PIO1_IRQ_1:
277  return 1;
278  default:
279  return -1;
280  }
281 
282 }

◆ util_pio_get_irq_from_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.

Parameters
idx
Returns
uint

Definition at line 250 of file util.c.

252  {
253 
254  check_pio_param(pio);
255  assert(util_pio_irq_index_is_valid(idx));
256  assert(util_pio_to_irq_map != NULL);
257 
258  const uint irq_num = util_pio_to_irq_map[
259  pio_get_index(pio) + idx];
260 
261  check_irq_param(irq_num);
262 
263  return irq_num;
264 
265 }
const uint8_t util_pio_to_irq_map[]
Quick lookup for finding an NVIC IRQ number for a PIO and interrupt index number.
Definition: util.c:48

References util_pio_irq_index_is_valid(), and util_pio_to_irq_map.

Referenced by hx711_multi__async_pio_irq_handler(), hx711_multi__init_irq(), hx711_multi__is_initd(), hx711_multi_async_start(), hx711_multi_close(), and util_irq_set_exclusive_pio_interrupt_num_handler().

◆ util_pio_get_pio_from_irq()

PIO const util_pio_get_pio_from_irq ( const uint  irq_num)

Gets the PIO using the NVIC IRQ number.

Parameters
irq_num
Returns
PIO const

Definition at line 284 of file util.c.

284  {
285 
286  check_irq_param(irq_num);
287 
288  switch(irq_num) {
289  case PIO0_IRQ_0:
290  case PIO0_IRQ_1:
291  return pio0;
292  case PIO1_IRQ_0:
293  case PIO1_IRQ_1:
294  return pio1;
295  default:
296  return NULL;
297  }
298 
299 }

◆ util_pio_get_pis_from_pio_interrupt_num()

uint util_pio_get_pis_from_pio_interrupt_num ( const uint  pio_interrupt_num)

Definition at line 301 of file util.c.

302  {
303 
305  pio_interrupt_num));
306 
307  const uint basePis = pis_interrupt0;
308  const uint pis = basePis + pio_interrupt_num;
309 
310  assert(util_uint_in_range(
311  pis, pis_interrupt0, pis_interrupt3));
312 
313  return pis;
314 
315 }

References util_routable_pio_interrupt_num_is_valid().

Referenced by hx711_multi__async_finish(), hx711_multi__async_pio_irq_handler(), hx711_multi__init_irq(), hx711_multi_close(), and util_irq_set_exclusive_pio_interrupt_num_handler().

◆ util_pio_gpio_contiguous_init()

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.

Parameters
pio
base
len

Definition at line 317 of file util.c.

320  {
321 
322  check_pio_param(pio);
323  assert(len > 0);
324 
325  const uint l = base + len - 1;
326 
327  for(uint i = base; i <= l; ++i) {
328  check_gpio_param(i);
329  pio_gpio_init(pio, i);
330  }
331 
332 }

Referenced by hx711_multi_pio_init().

◆ util_pio_interrupt_num_is_valid()

bool util_pio_interrupt_num_is_valid ( const uint  pio_interrupt_num)

Check whether a PIO interrupt number is valid.

Parameters
pio_interrupt_num
Returns
true
false

Definition at line 374 of file util.c.

375  {
376  return util_uint_in_range(
377  pio_interrupt_num,
380 }
#define UTIL_PIO_INTERRUPT_NUM_MIN
Definition: util.h:46
#define UTIL_PIO_INTERRUPT_NUM_MAX
Definition: util.h:47

References UTIL_PIO_INTERRUPT_NUM_MAX, and UTIL_PIO_INTERRUPT_NUM_MIN.

Referenced by hx711_multi_pio_init(), util_pio_interrupt_wait(), util_pio_interrupt_wait_clear(), util_pio_interrupt_wait_clear_timeout(), util_pio_interrupt_wait_cleared(), util_pio_interrupt_wait_cleared_timeout(), and util_pio_interrupt_wait_timeout().

◆ util_pio_interrupt_wait()

void util_pio_interrupt_wait ( PIO const  pio,
const uint  pio_interrupt_num 
)

Waits for a given PIO interrupt to be set.

Parameters
pio
pio_interrupt_num

Definition at line 390 of file util.c.

392  {
393  check_pio_param(pio);
394  assert(util_pio_interrupt_num_is_valid(pio_interrupt_num));
395  while(!pio_interrupt_get(pio, pio_interrupt_num)) {
396  tight_loop_contents();
397  }
398 }
bool util_pio_interrupt_num_is_valid(const uint pio_interrupt_num)
Check whether a PIO interrupt number is valid.
Definition: util.c:374

References util_pio_interrupt_num_is_valid().

Referenced by util_pio_interrupt_wait_clear().

◆ util_pio_interrupt_wait_clear()

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.

Parameters
pio
pio_interrupt_num

Definition at line 430 of file util.c.

432  {
433  check_pio_param(pio);
434  assert(util_pio_interrupt_num_is_valid(pio_interrupt_num));
435  util_pio_interrupt_wait(pio, pio_interrupt_num);
436  pio_interrupt_clear(pio, pio_interrupt_num);
437 }
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

References util_pio_interrupt_num_is_valid(), and util_pio_interrupt_wait().

◆ util_pio_interrupt_wait_clear_timeout()

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.

Parameters
pio
pio_interrupt_num
end
Returns
true if the interrupt was set within the timeout
false if the timeout was reached

Definition at line 459 of file util.c.

462  {
463 
464  check_pio_param(pio);
465  assert(util_pio_interrupt_num_is_valid(pio_interrupt_num));
466  assert(end != NULL);
467  assert(!is_nil_time(*end));
468 
469  const bool ok = util_pio_interrupt_wait_timeout(
470  pio,
471  pio_interrupt_num,
472  end);
473 
474  if(ok) {
475  pio_interrupt_clear(
476  pio,
477  pio_interrupt_num);
478  }
479 
480  return ok;
481 
482 }
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

References util_pio_interrupt_num_is_valid(), and util_pio_interrupt_wait_timeout().

◆ util_pio_interrupt_wait_cleared()

void util_pio_interrupt_wait_cleared ( PIO const  pio,
const uint  pio_interrupt_num 
)

Waits until a given PIO interrupt is cleared.

Parameters
pio
pio_interrupt_num

Definition at line 400 of file util.c.

402  {
403  check_pio_param(pio);
404  assert(util_pio_interrupt_num_is_valid(pio_interrupt_num));
405  while(pio_interrupt_get(pio, pio_interrupt_num)) {
406  tight_loop_contents();
407  }
408 }

References util_pio_interrupt_num_is_valid().

◆ util_pio_interrupt_wait_cleared_timeout()

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.

Parameters
pio
pio_interrupt_num
end
Returns
true
false

Definition at line 410 of file util.c.

413  {
414 
415  check_pio_param(pio);
416  assert(util_pio_interrupt_num_is_valid(pio_interrupt_num));
417  assert(end != NULL);
418  assert(!is_nil_time(*end));
419 
420  while(!time_reached(*end)) {
421  if(!pio_interrupt_get(pio, pio_interrupt_num)) {
422  return true;
423  }
424  }
425 
426  return false;
427 
428 }

References util_pio_interrupt_num_is_valid().

◆ util_pio_interrupt_wait_timeout()

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.

Parameters
pio
pio_interrupt_num
end
Returns
true if the interrupt was set within the timeout
false if the timeout was reached

Definition at line 439 of file util.c.

442  {
443 
444  check_pio_param(pio);
445  assert(util_pio_interrupt_num_is_valid(pio_interrupt_num));
446  assert(end != NULL);
447  assert(!is_nil_time(*end));
448 
449  while(!time_reached(*end)) {
450  if(pio_interrupt_get(pio, pio_interrupt_num)) {
451  return true;
452  }
453  }
454 
455  return false;
456 
457 }

References util_pio_interrupt_num_is_valid().

Referenced by util_pio_interrupt_wait_clear_timeout().

◆ util_pio_irq_index_is_valid()

bool util_pio_irq_index_is_valid ( const uint  idx)

Check whether PIO IRQ index is valid.

Parameters
idx
Returns
true
false

Definition at line 226 of file util.c.

226  {
227  return util_uint_in_range(
228  idx,
231 }
#define UTIL_PIO_IRQ_INDEX_MIN
Definition: util.h:43
#define UTIL_PIO_IRQ_INDEX_MAX
Definition: util.h:44

References UTIL_PIO_IRQ_INDEX_MAX, and UTIL_PIO_IRQ_INDEX_MIN.

Referenced by hx711_multi__init_asert(), util_irq_set_exclusive_pio_interrupt_num_handler(), util_pio_get_irq_from_index(), and util_pion_get_irqn().

◆ util_pio_sm_clear_isr()

void util_pio_sm_clear_isr ( PIO const  pio,
const uint  sm 
)

Clears a given state machine's ISR.

Parameters
pio
sm

Definition at line 355 of file util.c.

357  {
358  check_pio_param(pio);
359  check_sm_param(sm);
360  const uint instr = pio_encode_pull(false, false);
361  while(!pio_sm_is_tx_fifo_empty(pio, sm)) {
362  pio_sm_exec(pio, sm, instr);
363  }
364 }

◆ util_pio_sm_clear_osr()

void util_pio_sm_clear_osr ( PIO const  pio,
const uint  sm 
)

Clears a given state machine's OSR.

Parameters
pio
sm

Definition at line 344 of file util.c.

346  {
347  check_pio_param(pio);
348  check_sm_param(sm);
349  const uint instr = pio_encode_push(false, false);
350  while(!pio_sm_is_rx_fifo_empty(pio, sm)) {
351  pio_sm_exec(pio, sm, instr);
352  }
353 }

◆ util_pio_sm_clear_rx_fifo()

void util_pio_sm_clear_rx_fifo ( PIO const  pio,
const uint  sm 
)

Clears a given state machine's RX FIFO.

Parameters
pio
sm

Definition at line 334 of file util.c.

336  {
337  check_pio_param(pio);
338  check_sm_param(sm);
339  while(!pio_sm_is_rx_fifo_empty(pio, sm)) {
340  pio_sm_get(pio, sm);
341  }
342 }

Referenced by hx711_multi__async_start_dma().

◆ util_pio_sm_is_enabled()

bool util_pio_sm_is_enabled ( PIO const  pio,
const uint  sm 
)

Check whether a given state machine is enabled.

Parameters
pio
sm
Returns
true
false

Definition at line 366 of file util.c.

368  {
369  check_pio_param(pio);
370  check_sm_param(sm);
371  return (pio->ctrl & (1u << (PIO_CTRL_SM_ENABLE_LSB + sm))) != 0;
372 }

Referenced by hx711__is_state_machine_enabled(), hx711__try_get_value(), and hx711_multi__is_state_machines_enabled().

◆ util_pio_sm_try_get()

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 buffer.

Parameters
pio
sm
wordvariable to be set
thresholdword count (1 word = 8 bytes)
Returns
true if word was set with the value from the RX FIFO
false if the number of words in the RX FIFO is below the threshold

Definition at line 484 of file util.c.

488  {
489 
490  check_pio_param(pio);
491  check_sm_param(sm);
492  assert(word != NULL);
493 
494  if(pio_sm_get_rx_fifo_level(pio, sm) >= threshold) {
495  *word = pio_sm_get(pio, sm);
496  return true;
497  }
498 
499  return false;
500 
501 }

Referenced by hx711__try_get_value().

◆ util_pion_get_irqn()

uint util_pion_get_irqn ( PIO const  pio,
const uint  irq_index 
)

Definition at line 233 of file util.c.

235  {
236 
237  check_pio_param(pio);
238  assert(util_pio_irq_index_is_valid(irq_index));
239  assert(util_pio_to_irq_map != NULL);
240 
241  const uint irq_num = util_pio_to_irq_map[
242  pio_get_index(pio) + irq_index];
243 
244  check_irq_param(irq_num);
245 
246  return irq_num;
247 
248 }

References util_pio_irq_index_is_valid(), and util_pio_to_irq_map.

◆ util_routable_pio_interrupt_num_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.

Parameters
pio_interrupt_num
Returns
true
false

Definition at line 382 of file util.c.

383  {
384  return util_uint_in_range(
385  pio_interrupt_num,
388 }
#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MAX
Definition: util.h:50
#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MIN
Definition: util.h:49

References UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MAX, and UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MIN.

Referenced by hx711_multi_pio_init(), util_irq_set_exclusive_pio_interrupt_num_handler(), and util_pio_get_pis_from_pio_interrupt_num().

Variable Documentation

◆ util_dma_to_irq_map

const uint8_t util_dma_to_irq_map[UTIL_NUM_DMA_IRQS]
extern

Quick lookup for finding an NVIC IRQ number for a DMA interrupt index number.

Definition at line 55 of file util.c.

Referenced by util_dma_get_index_from_irq(), util_dma_get_irq_from_index(), and util_dma_get_irqn().

◆ util_pio_to_irq_map

const uint8_t util_pio_to_irq_map[NUM_PIOS *2]
extern

Quick lookup for finding an NVIC IRQ number for a PIO and interrupt index number.

Note
Each PIO has two interrupts, hence why this array is doubled.

Definition at line 48 of file util.c.

Referenced by util_pio_get_irq_from_index(), and util_pion_get_irqn().