hx711-pico-c
hx711.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 HX711_H_0ED0E077_8980_484C_BB94_AF52973CDC09
24 #define HX711_H_0ED0E077_8980_484C_BB94_AF52973CDC09
25 
26 #include <stdbool.h>
27 #include <stdint.h>
28 #include "hardware/pio.h"
29 #include "pico/mutex.h"
30 
31 #ifdef __cplusplus
32 extern "C" {
33 #endif
34 
35 #ifndef HX711_NO_MUTEX
36  #define HX711_MUTEX_BLOCK(mut, ...) \
37  do { \
38  mutex_enter_blocking(&mut); \
39  __VA_ARGS__ \
40  mutex_exit(&mut); \
41  } while(0)
42 #else
43  #define HX711_MUTEX_BLOCK(mut, ...) \
44  do { \
45  __VA_ARGS__ \
46  } while(0)
47 #endif
48 
49 #define HX711_READ_BITS UINT8_C(24)
50 #define HX711_POWER_DOWN_TIMEOUT UINT8_C(60) //microseconds
51 
52 #define HX711_MIN_VALUE INT32_C(-0x800000) //−8,388,608
53 #define HX711_MAX_VALUE INT32_C(0x7fffff) //8,388,607
54 
55 #define HX711_PIO_MIN_GAIN UINT8_C(0)
56 #define HX711_PIO_MAX_GAIN UINT8_C(2)
57 
58 extern const unsigned short HX711_SETTLING_TIMES[3]; //milliseconds
59 extern const unsigned char HX711_SAMPLE_RATES[2];
60 extern const unsigned char HX711_CLOCK_PULSES[3];
61 
62 typedef enum {
66 
67 typedef enum {
72 
73 typedef struct {
74 
75  uint _clock_pin;
76  uint _data_pin;
77 
78  PIO _pio;
79  const pio_program_t* _reader_prog;
81  uint _reader_sm;
83 
84 #ifndef HX711_NO_MUTEX
85  mutex_t _mut;
86 #endif
87 
88 } hx711_t;
89 
90 typedef void (*hx711_pio_init_t)(hx711_t* const);
91 typedef void (*hx711_program_init_t)(hx711_t* const);
92 
93 typedef struct {
94 
95  uint clock_pin;
96  uint data_pin;
97 
98  PIO pio;
100 
101  const pio_program_t* reader_prog;
103 
105 
106 void hx711_init(
107  hx711_t* const hx,
108  const hx711_config_t* const config);
109 
115 void hx711_close(hx711_t* const hx);
116 
123 void hx711_set_gain(
124  hx711_t* const hx,
125  const hx711_gain_t gain);
126 
133 int32_t hx711_get_twos_comp(const uint32_t raw);
134 
143 bool hx711_is_min_saturated(const int32_t val);
144 
153 bool hx711_is_max_saturated(const int32_t val);
154 
162 unsigned short hx711_get_settling_time(const hx711_rate_t rate);
163 
170 unsigned char hx711_get_rate_sps(const hx711_rate_t rate);
171 
178 unsigned char hx711_get_clock_pulses(const hx711_gain_t gain);
179 
187 int32_t hx711_get_value(hx711_t* const hx);
188 
200  hx711_t* const hx,
201  int32_t* const val,
202  const uint timeout);
203 
214  hx711_t* const hx,
215  int32_t* const val);
216 
224 static bool hx711__is_initd(hx711_t* const hx);
225 
234 static bool hx711__is_state_machine_enabled(hx711_t* const hx);
235 
244 bool hx711_is_value_valid(const int32_t v);
245 
255 bool hx711_is_pio_gain_valid(const uint32_t g);
256 
265 bool hx711_is_rate_valid(const hx711_rate_t r);
266 
275 bool hx711_is_gain_valid(const hx711_gain_t g);
276 
285 void hx711_power_up(
286  hx711_t* const hx,
287  const hx711_gain_t gain);
288 
296 void hx711_power_down(hx711_t* const hx);
297 
305 void hx711_wait_settle(const hx711_rate_t rate);
306 
312 void hx711_wait_power_down();
313 
321 uint32_t hx711_gain_to_pio_gain(const hx711_gain_t gain);
322 
332 static bool hx711__try_get_value(
333  PIO const pio,
334  const uint sm,
335  uint32_t* const val);
336 
337 #ifdef __cplusplus
338 }
339 #endif
340 
341 #endif
void hx711_power_down(hx711_t *const hx)
Definition: hx711.c:455
void hx711_power_up(hx711_t *const hx, const hx711_gain_t gain)
Definition: hx711.c:389
void hx711_set_gain(hx711_t *const hx, const hx711_gain_t gain)
Sets the HX711 gain.
Definition: hx711.c:149
bool hx711_is_max_saturated(const int32_t val)
Returns true if the HX711 is saturated at its maximum level.
Definition: hx711.c:242
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.
Definition: hx711.c:322
void hx711_init(hx711_t *const hx, const hx711_config_t *const config)
Definition: hx711.c:50
int32_t hx711_get_value(hx711_t *const hx)
Obtains a value from the HX711. Blocks until a value is available.
Definition: hx711.c:265
void hx711_close(hx711_t *const hx)
Stop communication with the HX711.
Definition: hx711.c:123
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...
Definition: hx711.c:247
bool hx711_is_min_saturated(const int32_t val)
Returns true if the HX711 is saturated at its minimum level.
Definition: hx711.c:237
static bool hx711__is_state_machine_enabled(hx711_t *const hx)
Check whether the hx struct's state machines are running.
Definition: hx711.c:356
bool hx711_is_rate_valid(const hx711_rate_t r)
Check whether the given rate is within the range of the predefined rates.
Definition: hx711.c:375
bool hx711_is_gain_valid(const hx711_gain_t g)
Check whether the given gain is within the range of the predefined gains.
Definition: hx711.c:382
static bool hx711__is_initd(hx711_t *const hx)
Check whether the hx struct has been initalised.
Definition: hx711.c:347
unsigned char hx711_get_rate_sps(const hx711_rate_t rate)
Returns the numeric sample rate of the given rate.
Definition: hx711.c:253
const unsigned char HX711_CLOCK_PULSES[3]
Definition: hx711.c:44
void hx711_wait_settle(const hx711_rate_t rate)
Convenience function for sleeping for the appropriate amount of time according to the given sample ra...
Definition: hx711.c:487
bool hx711_is_value_valid(const int32_t v)
Check whether the given value is valid for a HX711 implementation.
Definition: hx711.c:361
void(* hx711_program_init_t)(hx711_t *const)
Definition: hx711.h:91
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.
Definition: hx711.c:516
int32_t hx711_get_twos_comp(const uint32_t raw)
Convert a raw value from the HX711 to a 32-bit signed int.
Definition: hx711.c:231
void hx711_wait_power_down()
Convenience function for sleeping for the appropriate amount of time to allow the HX711 to power down...
Definition: hx711.c:491
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 ga...
Definition: hx711.c:368
hx711_rate_t
Definition: hx711.h:62
@ hx711_rate_10
Definition: hx711.h:63
@ hx711_rate_80
Definition: hx711.h:64
const unsigned short HX711_SETTLING_TIMES[3]
Definition: hx711.c:34
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.
Definition: hx711.c:292
void(* hx711_pio_init_t)(hx711_t *const)
Definition: hx711.h:90
hx711_gain_t
Definition: hx711.h:67
@ hx711_gain_64
Definition: hx711.h:70
@ hx711_gain_32
Definition: hx711.h:69
@ hx711_gain_128
Definition: hx711.h:68
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.
Definition: hx711.c:495
const unsigned char HX711_SAMPLE_RATES[2]
Definition: hx711.c:39
unsigned char hx711_get_clock_pulses(const hx711_gain_t gain)
Returns the clock pulse count for a given gain value.
Definition: hx711.c:259
uint clock_pin
Definition: hx711.h:95
hx711_pio_init_t pio_init
Definition: hx711.h:99
const pio_program_t * reader_prog
Definition: hx711.h:101
hx711_program_init_t reader_prog_init
Definition: hx711.h:102
uint data_pin
Definition: hx711.h:96
Definition: hx711.h:73
const pio_program_t * _reader_prog
Definition: hx711.h:79
uint _data_pin
Definition: hx711.h:76
PIO _pio
Definition: hx711.h:78
uint _reader_sm
Definition: hx711.h:81
pio_sm_config _reader_prog_default_config
Definition: hx711.h:80
uint _clock_pin
Definition: hx711.h:75
mutex_t _mut
Definition: hx711.h:85
uint _reader_offset
Definition: hx711.h:82