23 #ifndef HX711_MULTI_H_253BF37A_8356_462B_B8F9_39E09A7193E6
24 #define HX711_MULTI_H_253BF37A_8356_462B_B8F9_39E09A7193E6
28 #include "hardware/pio.h"
29 #include "pico/mutex.h"
30 #include "pico/platform.h"
43 #define HX711_MULTI_CONVERSION_DONE_IRQ_NUM UINT8_C(0)
52 #define HX711_MULTI_DATA_READY_IRQ_NUM UINT8_C(4)
60 #define HX711_MULTI_ASYNC_READ_COUNT UINT8_C(NUM_PIOS)
65 #define HX711_MULTI_ASYNC_PIO_IRQ_IDX UINT8_C(0)
66 #define HX711_MULTI_ASYNC_DMA_IRQ_IDX UINT8_C(0)
71 #define HX711_MULTI_MIN_CHIPS UINT8_C(1)
80 #define HX711_MULTI_MAX_CHIPS UINT8_C(MIN(NUM_BANK0_GPIOS, 32))
118 #ifndef HX711_NO_MUTEX
352 const uint32_t*
const pinvals,
353 int32_t*
const values,
386 int32_t*
const values);
401 int32_t*
const values,
431 int32_t*
const values);
void hx711_multi_power_up(hx711_multi_t *const hxm, const hx711_gain_t gain)
void hx711_multi_power_down(hx711_multi_t *const hxm)
static void hx711_multi__async_finish(hx711_multi_t *const hxm)
Stop any current async reads and stop listening for DMA and PIO IRQs.
void hx711_multi_async_get_values(hx711_multi_t *const hxm, int32_t *const values)
Get the values from the last asynchronous read. This function is not mutex protected.
hx711_multi_t * hx711_multi__async_read_array[HX711_MULTI_ASYNC_READ_COUNT]
Array of hxm for ISR to access. This is a global variable.
void hx711_multi_init(hx711_multi_t *const hxm, const hx711_multi_config_t *const config)
void hx711_multi_set_gain(hx711_multi_t *const hxm, const hx711_gain_t gain)
Sets the HX711s' gain.
hx711_multi_async_state_t
State of the read as it moves through the async process.
@ HX711_MULTI_ASYNC_STATE_WAITING
@ HX711_MULTI_ASYNC_STATE_NONE
@ HX711_MULTI_ASYNC_STATE_READING
@ HX711_MULTI_ASYNC_STATE_DONE
static bool hx711_multi__async_pio_irq_is_set(hx711_multi_t *const hxm)
Whether a given hxm is the cause of the current PIO IRQ.
static void __isr __not_in_flash_func() hx711_multi__async_pio_irq_handler()
ISR handler for PIO IRQs.
static hx711_multi_t *const hx711_multi__async_get_dma_irq_request()
Get the hxm which caused the current DMA IRQ. Returns NULL if none found.
void(* hx711_multi_program_init_t)(hx711_multi_t *const)
static bool hx711_multi__is_state_machines_enabled(hx711_multi_t *const hxm)
Check whether the hxm struct has PIO State Machines which are enabled.
static bool hx711_multi__async_is_running(hx711_multi_t *const hxm)
Check whether an async read is currently occurring.
static void hx711_multi__init_dma(hx711_multi_t *const hxm)
Subroutine for initialising DMA.
static void hx711_multi__async_start_dma(hx711_multi_t *const hxm)
Triggers DMA reading; moves request state from WAITING to READING.
static hx711_multi_t *const hx711_multi__async_get_pio_irq_request()
Get the hxm which caused the current PIO IRQ. Returns NULL if none found.
bool hx711_multi_get_values_timeout(hx711_multi_t *const hxm, int32_t *const values, const uint timeout)
Fill an array with one value from each HX711, timing out if failing to obtain values within the timeo...
bool hx711_multi_async_done(hx711_multi_t *const hxm)
Check whether an asynchronous read is complete. This function is not mutex protected.
static void hx711_multi__init_asert(const hx711_multi_config_t *const config)
static void __isr __not_in_flash_func() hx711_multi__async_dma_irq_handler()
ISR handler for DMA IRQs.
void hx711_multi_async_start(hx711_multi_t *const hxm)
Start an asynchronos read. This function is not mutex protected.
#define HX711_MULTI_ASYNC_READ_COUNT
Only one instance of a hx711_multi can operate within a PIO. So the maximum number of concurrent asyn...
static bool hx711_multi__is_initd(hx711_multi_t *const hxm)
Check whether the hxm struct has been initialised.
void hx711_multi_sync(hx711_multi_t *const hxm, const hx711_gain_t gain)
Attempt to synchronise all connected chips. This does not include a settling time.
void hx711_multi_get_values(hx711_multi_t *const hxm, int32_t *const values)
Fill an array with one value from each HX711. Blocks until values are obtained.
static void hx711_multi__async_remove_reader(const hx711_multi_t *const hxm)
Removes the given hxm from the request array.
void hx711_multi_pinvals_to_values(const uint32_t *const pinvals, int32_t *const values, const size_t len)
Convert an array of pinvals to regular HX711 values.
bool hx711_multi_is_syncd(hx711_multi_t *const hxm)
Determines whether all chips are in sync.
uint32_t hx711_multi_get_sync_state(hx711_multi_t *const hxm)
Returns the state of each chip as a bitmask. The 0th bit is the first chip, 1th bit is the second,...
void hx711_multi_close(hx711_multi_t *const hxm)
Stop communication with all HX711s.
static void hx711_multi__init_pio(hx711_multi_t *const hxm)
Subroutine for initialising PIO.
static bool hx711_multi__async_dma_irq_is_set(hx711_multi_t *const hxm)
Whether a given hxm is the cause of the current DMA IRQ.
void(* hx711_multi_pio_init_t)(hx711_multi_t *const)
static void hx711_multi__init_irq(hx711_multi_t *const hxm)
Subroutine for initialising IRQ.
static bool hx711_multi__async_add_reader(hx711_multi_t *const hxm)
Adds hxm to the array for ISR access. Returns false if no space.
size_t chips_len
Number of HX711 chips connected.
uint data_pin_base
Lowest GPIO pin number connected to a HX711 chip.
uint dma_irq_index
Which index to use for a DMA interrupt. Either 0 or 1. Corresponds to DMA_IRQ[IRQ_INDEX] NVIC IRQ num...
const pio_program_t * awaiter_prog
PIO awaiter program.
hx711_multi_program_init_t awaiter_prog_init
PIO awaiter init function. This is called to set up the State Machine for the awaiter program....
const pio_program_t * reader_prog
PIO reader program.
hx711_multi_program_init_t reader_prog_init
PIO reader init function. This is called to set up the State Machine for the reader program....
uint clock_pin
GPIO pin number connected to all HX711 chips.
PIO pio
Which PIO to use. Either pio0 or pio1.
hx711_multi_pio_init_t pio_init
PIO init function. This is called to set up any PIO functions (pio_*) as opposed to any State Machine...
uint pio_irq_index
Which index to use for a PIO interrupt. Either 0 or 1. Corresponds to PIO[PIO_INDEX]_IRQ[IRQ_INDEX] N...
pio_sm_config _awaiter_default_config
volatile hx711_multi_async_state_t _async_state
const pio_program_t * _reader_prog
pio_sm_config _reader_default_config
const pio_program_t * _awaiter_prog