hx711-pico-c
|
#include <stdbool.h>
#include <stdint.h>
#include "hardware/pio.h"
#include "pico/mutex.h"
Go to the source code of this file.
Data Structures | |
struct | hx711_t |
struct | hx711_config_t |
Macros | |
#define | HX711_MUTEX_BLOCK(mut, ...) |
#define | HX711_READ_BITS UINT8_C(24) |
#define | HX711_POWER_DOWN_TIMEOUT UINT8_C(60) |
#define | HX711_MIN_VALUE INT32_C(-0x800000) |
#define | HX711_MAX_VALUE INT32_C(0x7fffff) |
#define | HX711_PIO_MIN_GAIN UINT8_C(0) |
#define | HX711_PIO_MAX_GAIN UINT8_C(2) |
Typedefs | |
typedef void(* | hx711_pio_init_t) (hx711_t *const) |
typedef void(* | hx711_program_init_t) (hx711_t *const) |
Enumerations | |
enum | hx711_rate_t { hx711_rate_10 = 0 , hx711_rate_80 } |
enum | hx711_gain_t { hx711_gain_128 = 0 , hx711_gain_32 , hx711_gain_64 } |
Functions | |
void | hx711_init (hx711_t *const hx, const hx711_config_t *const config) |
void | hx711_close (hx711_t *const hx) |
Stop communication with the HX711. More... | |
void | hx711_set_gain (hx711_t *const hx, const hx711_gain_t gain) |
Sets the HX711 gain. More... | |
int32_t | hx711_get_twos_comp (const uint32_t raw) |
Convert a raw value from the HX711 to a 32-bit signed int. More... | |
bool | hx711_is_min_saturated (const int32_t val) |
Returns true if the HX711 is saturated at its minimum level. More... | |
bool | hx711_is_max_saturated (const int32_t val) |
Returns true if the HX711 is saturated at its maximum level. More... | |
unsigned short | hx711_get_settling_time (const hx711_rate_t rate) |
Returns the number of milliseconds to wait according to the given HX711 sample rate to allow readings to settle. More... | |
unsigned char | hx711_get_rate_sps (const hx711_rate_t rate) |
Returns the numeric sample rate of the given rate. More... | |
unsigned char | hx711_get_clock_pulses (const hx711_gain_t gain) |
Returns the clock pulse count for a given gain value. More... | |
int32_t | hx711_get_value (hx711_t *const hx) |
Obtains a value from the HX711. Blocks until a value is available. More... | |
bool | hx711_get_value_timeout (hx711_t *const hx, int32_t *const val, const uint timeout) |
Obtains a value from the HX711. Blocks until a value is available or the timeout is reached. More... | |
bool | hx711_get_value_noblock (hx711_t *const hx, int32_t *const val) |
Obtains a value from the HX711. Returns immediately if no value is available. More... | |
static bool | hx711__is_initd (hx711_t *const hx) |
Check whether the hx struct has been initalised. More... | |
static bool | hx711__is_state_machine_enabled (hx711_t *const hx) |
Check whether the hx struct's state machines are running. More... | |
bool | hx711_is_value_valid (const int32_t v) |
Check whether the given value is valid for a HX711 implementation. More... | |
bool | hx711_is_pio_gain_valid (const uint32_t g) |
Check whether a given value is permitted to be transmitted to a PIO State Machine to set a HX711's gain according to the PIO program implementation. More... | |
bool | hx711_is_rate_valid (const hx711_rate_t r) |
Check whether the given rate is within the range of the predefined rates. More... | |
bool | hx711_is_gain_valid (const hx711_gain_t g) |
Check whether the given gain is within the range of the predefined gains. More... | |
void | hx711_wait_settle (const hx711_rate_t rate) |
Convenience function for sleeping for the appropriate amount of time according to the given sample rate to allow readings to settle. More... | |
void | hx711_wait_power_down () |
Convenience function for sleeping for the appropriate amount of time to allow the HX711 to power down. More... | |
uint32_t | hx711_gain_to_pio_gain (const hx711_gain_t gain) |
Convert a hx711_gain_t to a numeric value appropriate for a PIO State Machine. More... | |
static bool | hx711__try_get_value (PIO const pio, const uint sm, uint32_t *const val) |
Attempts to obtain a value from the PIO RX FIFO if one is available. More... | |
Variables | |
const unsigned short | HX711_SETTLING_TIMES [3] |
const unsigned char | HX711_SAMPLE_RATES [2] |
const unsigned char | HX711_CLOCK_PULSES [3] |
#define HX711_MUTEX_BLOCK | ( | mut, | |
... | |||
) |
enum hx711_gain_t |
enum hx711_rate_t |
|
static |
Check whether the hx struct has been initalised.
hx |
Definition at line 347 of file hx711.c.
References hx711_t::_mut, hx711_t::_pio, and hx711_t::_reader_sm.
Referenced by hx711__is_state_machine_enabled(), hx711_close(), hx711_init(), hx711_power_down(), and hx711_power_up().
|
static |
Check whether the hx struct's state machines are running.
hx |
Definition at line 356 of file hx711.c.
References hx711_t::_pio, hx711_t::_reader_sm, hx711__is_initd(), and util_pio_sm_is_enabled().
Referenced by hx711_get_value(), hx711_get_value_noblock(), hx711_get_value_timeout(), hx711_power_up(), and hx711_set_gain().
|
static |
Attempts to obtain a value from the PIO RX FIFO if one is available.
pio | pointer to PIO |
sm | state machine |
val | pointer to raw value from HX711 to set |
Definition at line 516 of file hx711.c.
References HX711_READ_BITS, util_pio_sm_is_enabled(), and util_pio_sm_try_get().
Referenced by hx711_get_value_noblock(), and hx711_get_value_timeout().
void hx711_close | ( | hx711_t *const | hx | ) |
Stop communication with the HX711.
hx |
Definition at line 123 of file hx711.c.
References hx711_t::_mut, hx711_t::_pio, hx711_t::_reader_offset, hx711_t::_reader_prog, hx711_t::_reader_sm, hx711__is_initd(), and HX711_MUTEX_BLOCK.
uint32_t hx711_gain_to_pio_gain | ( | const hx711_gain_t | gain | ) |
Convert a hx711_gain_t to a numeric value appropriate for a PIO State Machine.
gain |
gain value is 0-based and calculated by: gain = clock pulses - 24 - 1 ie. gain of 128 is 25 clock pulses, so gain = 25 - 24 - 1 gain = 0
Definition at line 495 of file hx711.c.
References hx711_get_clock_pulses(), hx711_is_gain_valid(), hx711_is_pio_gain_valid(), and HX711_READ_BITS.
Referenced by hx711_multi_power_up(), hx711_multi_set_gain(), hx711_power_up(), and hx711_set_gain().
unsigned char hx711_get_clock_pulses | ( | const hx711_gain_t | gain | ) |
Returns the clock pulse count for a given gain value.
gain |
Definition at line 259 of file hx711.c.
References HX711_CLOCK_PULSES, and hx711_is_gain_valid().
Referenced by hx711_gain_to_pio_gain().
unsigned char hx711_get_rate_sps | ( | const hx711_rate_t | rate | ) |
Returns the numeric sample rate of the given rate.
rate |
Definition at line 253 of file hx711.c.
References hx711_is_rate_valid(), and HX711_SAMPLE_RATES.
unsigned short hx711_get_settling_time | ( | const hx711_rate_t | rate | ) |
Returns the number of milliseconds to wait according to the given HX711 sample rate to allow readings to settle.
rate |
Definition at line 247 of file hx711.c.
References hx711_is_rate_valid(), and HX711_SETTLING_TIMES.
Referenced by hx711_wait_settle().
int32_t hx711_get_twos_comp | ( | const uint32_t | raw | ) |
Convert a raw value from the HX711 to a 32-bit signed int.
raw |
Definition at line 231 of file hx711.c.
References HX711_MAX_VALUE, and HX711_MIN_VALUE.
Referenced by hx711_get_value(), hx711_get_value_noblock(), hx711_get_value_timeout(), and hx711_multi_pinvals_to_values().
int32_t hx711_get_value | ( | hx711_t *const | hx | ) |
Obtains a value from the HX711. Blocks until a value is available.
hx |
Block until a value is available
NOTE: remember that reading from the RX FIFO simultaneously clears it. That's why we can keep calling this function hx711_get_value and be assured we'll be getting a new value each time, even if the RX FIFO is currently empty.
Definition at line 265 of file hx711.c.
References hx711_t::_mut, hx711_t::_pio, hx711_t::_reader_sm, hx711__is_state_machine_enabled(), hx711_get_twos_comp(), and HX711_MUTEX_BLOCK.
bool hx711_get_value_noblock | ( | hx711_t *const | hx, |
int32_t *const | val | ||
) |
Obtains a value from the HX711. Returns immediately if no value is available.
hx | |
val | pointer to the value |
Definition at line 322 of file hx711.c.
References hx711_t::_mut, hx711_t::_pio, hx711_t::_reader_sm, hx711__is_state_machine_enabled(), hx711__try_get_value(), hx711_get_twos_comp(), and HX711_MUTEX_BLOCK.
bool hx711_get_value_timeout | ( | hx711_t *const | hx, |
int32_t *const | val, | ||
const uint | timeout | ||
) |
Obtains a value from the HX711. Blocks until a value is available or the timeout is reached.
hx | |
val | pointer to the value |
timeout | maximum time to wait for a value in microseconds |
Definition at line 292 of file hx711.c.
References hx711_t::_mut, hx711_t::_pio, hx711_t::_reader_sm, hx711__is_state_machine_enabled(), hx711__try_get_value(), hx711_get_twos_comp(), and HX711_MUTEX_BLOCK.
void hx711_init | ( | hx711_t *const | hx, |
const hx711_config_t *const | config | ||
) |
There was originally a call here to gpio_put on the clock pin to power up the HX711. I have decided to remove this and also remove enabling the state machine from the pio init function. This does leave the HX711's power state undefined from the perspective of the code, but does give a much clearer separation of duties. This function merely init's the hardware and state machine, and the hx711_set_power function sets the power and enables/disables the state machine.
There was originally a call here to gpio_pull_up on the data pin to prevent erroneous data ready states. This was incorrect. Page 4 of the datasheet states: "The 25th pulse at PD_SCK input will pull DOUT pin back to high (Fig.2)."
Definition at line 50 of file hx711.c.
References hx711_t::_clock_pin, hx711_t::_data_pin, hx711_t::_mut, hx711_t::_pio, hx711_t::_reader_offset, hx711_t::_reader_prog, hx711_t::_reader_sm, hx711_config_t::clock_pin, hx711_config_t::data_pin, hx711__is_initd(), HX711_MUTEX_BLOCK, hx711_config_t::pio, hx711_config_t::pio_init, hx711_config_t::reader_prog, hx711_config_t::reader_prog_init, and util_gpio_set_output().
bool hx711_is_gain_valid | ( | const hx711_gain_t | g | ) |
Check whether the given gain is within the range of the predefined gains.
g |
Definition at line 382 of file hx711.c.
References hx711_gain_128, and hx711_gain_64.
Referenced by hx711_gain_to_pio_gain(), hx711_get_clock_pulses(), hx711_power_up(), and hx711_set_gain().
bool hx711_is_max_saturated | ( | const int32_t | val | ) |
Returns true if the HX711 is saturated at its maximum level.
val |
Definition at line 242 of file hx711.c.
References hx711_is_value_valid(), and HX711_MAX_VALUE.
bool hx711_is_min_saturated | ( | const int32_t | val | ) |
Returns true if the HX711 is saturated at its minimum level.
val |
Definition at line 237 of file hx711.c.
References hx711_is_value_valid(), and HX711_MIN_VALUE.
bool hx711_is_pio_gain_valid | ( | const uint32_t | g | ) |
Check whether a given value is permitted to be transmitted to a PIO State Machine to set a HX711's gain according to the PIO program implementation.
g |
Definition at line 368 of file hx711.c.
References HX711_PIO_MAX_GAIN, and HX711_PIO_MIN_GAIN.
Referenced by hx711_gain_to_pio_gain(), hx711_multi_power_up(), hx711_multi_set_gain(), hx711_power_up(), and hx711_set_gain().
bool hx711_is_rate_valid | ( | const hx711_rate_t | r | ) |
Check whether the given rate is within the range of the predefined rates.
r |
Definition at line 375 of file hx711.c.
References hx711_rate_10, and hx711_rate_80.
Referenced by hx711_get_rate_sps(), and hx711_get_settling_time().
bool hx711_is_value_valid | ( | const int32_t | v | ) |
Check whether the given value is valid for a HX711 implementation.
v |
Definition at line 361 of file hx711.c.
References HX711_MAX_VALUE, and HX711_MIN_VALUE.
Referenced by hx711_is_max_saturated(), hx711_is_min_saturated(), and hx711_multi_pinvals_to_values().
void hx711_set_gain | ( | hx711_t *const | hx, |
const hx711_gain_t | gain | ||
) |
Sets the HX711 gain.
hx | |
gain |
Before putting anything in the TX FIFO buffer, assume the worst-case scenario which is that there's something already in there. There ought not to be, but clearing it ensures the following pio_sm_put* call does not need to block as this function to change the gain should take precedence.
At this point the current value in the RX FIFO will have been calculated based on whatever the previous set gain was. So, the RX FIFO needs to be cleared.
NOTE: checking for whether the RX FIFO is not empty won't work. A conversion may have already begun before any bits have been moved into the ISR.
UPDATE: the worst-case scenario here is that the pio_sm_put call has occurred after the pio "pull", because we then need to wait until the following "pull" in the state machine. If this happens:
To handle 1.: Clear the RX FIFO with a non-blocking read. If the RX FIFO is empty, no harm done because the call won't block.
To handle 2.: Read the "next" value with a blocking read to ensure the "next, next" value will be set to the desired gain.
Immediately following the above blocking call, the state machine will pull in the data in the pio_sm_put call above and pulse the HX711 the correct number of times to set the desired gain.
No further communication with the state machine from this function is required. Any other function(s) wishing to obtain a value from the HX711 need only block until one is there (or check the RX FIFO level).
Definition at line 149 of file hx711.c.
References hx711_t::_mut, hx711_t::_pio, hx711_t::_reader_sm, hx711__is_state_machine_enabled(), hx711_gain_to_pio_gain(), hx711_is_gain_valid(), hx711_is_pio_gain_valid(), and HX711_MUTEX_BLOCK.
void hx711_wait_power_down | ( | ) |
Convenience function for sleeping for the appropriate amount of time to allow the HX711 to power down.
Definition at line 491 of file hx711.c.
References HX711_POWER_DOWN_TIMEOUT.
Referenced by hx711_multi_sync().
void hx711_wait_settle | ( | const hx711_rate_t | rate | ) |
Convenience function for sleeping for the appropriate amount of time according to the given sample rate to allow readings to settle.
rate |
Definition at line 487 of file hx711.c.
References hx711_get_settling_time().
|
extern |
Definition at line 44 of file hx711.c.
Referenced by hx711_get_clock_pulses().
|
extern |
Definition at line 39 of file hx711.c.
Referenced by hx711_get_rate_sps().
|
extern |
Definition at line 34 of file hx711.c.
Referenced by hx711_get_settling_time().