Skip to content

Commit ed336d2

Browse files
committed
changes
1 parent bec9800 commit ed336d2

File tree

9 files changed

+346
-226
lines changed

9 files changed

+346
-226
lines changed

include/hx711.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,14 +32,14 @@
3232
extern "C" {
3333
#endif
3434

35-
#define HX711_READ_BITS 24u
36-
#define HX711_POWER_DOWN_TIMEOUT 60u //microseconds
35+
#define HX711_READ_BITS UINT8_C(24)
36+
#define HX711_POWER_DOWN_TIMEOUT UINT8_C(60) //microseconds
3737

38-
#define HX711_MIN_VALUE INT32_C(-0x800000)
39-
#define HX711_MAX_VALUE INT32_C(0x7fffff)
38+
#define HX711_MIN_VALUE INT32_C(-0x800000) //−8,388,608
39+
#define HX711_MAX_VALUE INT32_C(0x7fffff) //8,388,607
4040

41-
#define HX711_PIO_MIN_GAIN 0u
42-
#define HX711_PIO_MAX_GAIN 2u
41+
#define HX711_PIO_MIN_GAIN UINT8_C(0)
42+
#define HX711_PIO_MAX_GAIN UINT8_C(2)
4343

4444
extern const unsigned short HX711_SETTLING_TIMES[3]; //milliseconds
4545
extern const unsigned char HX711_SAMPLE_RATES[2];

include/hx711_multi.h

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -34,15 +34,15 @@
3434
extern "C" {
3535
#endif
3636

37-
#define HX711_MULTI_CONVERSION_DONE_IRQ_NUM 0u
38-
#define HX711_MULTI_DATA_READY_IRQ_NUM 4u
37+
#define HX711_MULTI_CONVERSION_DONE_IRQ_NUM UINT8_C(0)
38+
#define HX711_MULTI_DATA_READY_IRQ_NUM UINT8_C(4)
3939

40-
#define HX711_MULTI_ASYNC_REQ_COUNT NUM_PIOS
41-
#define HX711_MULTI_ASYNC_PIO_IRQ_IDX 0u
42-
#define HX711_MULTI_ASYNC_DMA_IRQ_IDX 0u
40+
#define HX711_MULTI_ASYNC_REQ_COUNT UINT8_C(NUM_PIOS)
41+
#define HX711_MULTI_ASYNC_PIO_IRQ_IDX UINT8_C(0)
42+
#define HX711_MULTI_ASYNC_DMA_IRQ_IDX UINT8_C(0)
4343

44-
#define HX711_MULTI_MIN_CHIPS 1u
45-
#define HX711_MULTI_MAX_CHIPS 32u
44+
#define HX711_MULTI_MIN_CHIPS UINT8_C(1)
45+
#define HX711_MULTI_MAX_CHIPS UINT8_C(32)
4646

4747
typedef enum {
4848
HX711_MULTI_ASYNC_STATE_NONE = 0,
@@ -111,6 +111,21 @@ typedef struct {
111111
extern hx711_multi_t* hx711_multi__async_request_map[
112112
HX711_MULTI_ASYNC_REQ_COUNT];
113113

114+
static void hx711_multi__init_asert(
115+
hx711_multi_t* const hxm,
116+
const hx711_multi_config_t* const config);
117+
118+
static void hx711_multi__init_pio(
119+
hx711_multi_t* const hxm,
120+
const hx711_multi_config_t* const config);
121+
122+
static void hx711_multi__init_dma(
123+
hx711_multi_t* const hxm,
124+
const hx711_multi_config_t* const config);
125+
126+
static void hx711_multi__init_irq(
127+
hx711_multi_t* const hxm);
128+
114129
/**
115130
* @brief Whether a given hxm is the cause of the current
116131
* DMA IRQ.

include/util.h

Lines changed: 68 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525

2626
#include <stdint.h>
2727
#include "hardware/pio.h"
28+
#include "hardware/platform_defs.h"
2829
#include "hardware/sync.h"
2930
#include "pico/mutex.h"
3031
#include "pico/types.h"
@@ -33,44 +34,75 @@
3334
extern "C" {
3435
#endif
3536

36-
#define UTIL_DMA_IRQ_INDEX_MIN 0u
37-
#define UTIL_DMA_IRQ_INDEX_MAX 1u
37+
// RP2040 sdk doesn't seem to define this
38+
#define UTIL_NUM_DMA_IRQS UINT8_C(2)
3839

39-
#define UTIL_PIO_IRQ_INDEX_MIN 0u
40-
#define UTIL_PIO_IRQ_INDEX_MAX 1u
40+
#define UTIL_DMA_IRQ_INDEX_MIN UINT8_C(0)
41+
#define UTIL_DMA_IRQ_INDEX_MAX UINT8_C(UTIL_NUM_DMA_IRQS - 1)
4142

42-
#define UTIL_PIO_INTERRUPT_NUM_MIN 0u
43-
#define UTIL_PIO_INTERRUPT_NUM_MAX 7u
43+
#define UTIL_PIO_IRQ_INDEX_MIN UINT8_C(0)
44+
#define UTIL_PIO_IRQ_INDEX_MAX UINT8_C(NUM_PIOS - 1)
4445

45-
#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MIN 0u
46-
#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MAX 3u
46+
#define UTIL_PIO_INTERRUPT_NUM_MIN UINT8_C(0)
47+
#define UTIL_PIO_INTERRUPT_NUM_MAX UINT8_C(7)
4748

49+
#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MIN UINT8_C(0)
50+
#define UTIL_ROUTABLE_PIO_INTERRUPT_NUM_MAX UINT8_C(3)
51+
52+
/**
53+
* @brief Own a mutex for the duration of this block of
54+
* code.
55+
* @example UTIL_MUTEX_BLOCK(mut,
56+
* // do something...
57+
* );
58+
*/
4859
#define UTIL_MUTEX_BLOCK(mut, ...) \
4960
do { \
5061
mutex_enter_blocking(&mut); \
5162
__VA_ARGS__ \
5263
mutex_exit(&mut); \
5364
} while(0)
5465

66+
/**
67+
* @brief Disable interrupts for the duration of this block of
68+
* code.
69+
* @example UTIL_INTERRUPTS_OFF_BLOCK(
70+
* // do something atomically...
71+
* );
72+
* @note the interrupt_status var is uniquely named to avoid
73+
* variable conflicts within the block.
74+
*/
5575
#define UTIL_INTERRUPTS_OFF_BLOCK(...) \
5676
do { \
57-
const uint32_t _interrupt_status = save_and_disable_interrupts(); \
77+
const uint32_t interrupt_status_cb918069_eadf_49bc_9d8c_a8a4defad20c = save_and_disable_interrupts(); \
5878
__VA_ARGS__ \
59-
restore_interrupts(_interrupt_status); \
79+
restore_interrupts(interrupt_status_cb918069_eadf_49bc_9d8c_a8a4defad20c); \
6080
} while(0)
6181

62-
#define DECL_IN_RANGE_FUNC(TYPE) \
82+
#define UTIL_DECL_IN_RANGE_FUNC(TYPE) \
6383
bool util_ ## TYPE ##_in_range( \
6484
const TYPE val, \
6585
const TYPE min, \
6686
const TYPE max);
6787

68-
DECL_IN_RANGE_FUNC(int32_t)
69-
DECL_IN_RANGE_FUNC(uint32_t)
70-
DECL_IN_RANGE_FUNC(int)
71-
DECL_IN_RANGE_FUNC(uint)
88+
UTIL_DECL_IN_RANGE_FUNC(int32_t)
89+
UTIL_DECL_IN_RANGE_FUNC(uint32_t)
90+
UTIL_DECL_IN_RANGE_FUNC(int)
91+
UTIL_DECL_IN_RANGE_FUNC(uint)
92+
93+
/**
94+
* @brief Quick lookup for finding an NVIC IRQ number
95+
* for a PIO and interrupt index number.
96+
* @note Each PIO has two interrupts, hence why this
97+
* array is doubled.
98+
*/
99+
extern const uint8_t util_pio_to_irq_map[NUM_PIOS * 2];
72100

73-
#undef DECL_IN_RANGE_FUNC
101+
/**
102+
* @brief Quick lookup for finding an NVIC IRQ number
103+
* for a DMA interrupt index number.
104+
*/
105+
extern const uint8_t util_dma_to_irq_map[UTIL_NUM_DMA_IRQS];
74106

75107
/**
76108
* @brief Check whether a DMA IRQ index is valid.
@@ -151,17 +183,17 @@ bool util_pio_irq_index_is_valid(const uint idx);
151183

152184
/**
153185
* @brief Gets the correct NVIC IRQ number for a PIO
154-
* according to the IRQ number.
186+
* according to the IRQ index.
155187
*
156188
* @example util_pion_get_irqn(pio1, 1); //returns PIO1_IRQ_1
157189
*
158190
* @param pio
159-
* @param irq_num 0 or 1
191+
* @param irq_index 0 or 1
160192
* @return uint
161193
*/
162194
uint util_pion_get_irqn(
163195
PIO const pio,
164-
const uint irq_num);
196+
const uint irq_index);
165197

166198
/**
167199
* @brief Gets the correct PIO interrupt source number according
@@ -220,9 +252,24 @@ bool util_pio_sm_is_enabled(
220252
PIO const pio,
221253
const uint sm);
222254

255+
/**
256+
* @brief Check whether a PIO interrupt number is valid.
257+
*
258+
* @param pio_interrupt_num
259+
* @return true
260+
* @return false
261+
*/
223262
bool util_pio_interrupt_num_is_valid(
224263
const uint pio_interrupt_num);
225264

265+
/**
266+
* @brief Check whether a PIO interrupt number is
267+
* a valid routable interrupt number.
268+
*
269+
* @param pio_interrupt_num
270+
* @return true
271+
* @return false
272+
*/
226273
bool util_routable_pio_interrupt_num_is_valid(
227274
const uint pio_interrupt_num);
228275

@@ -320,6 +367,8 @@ bool util_pio_sm_try_get(
320367
uint32_t* const word,
321368
const uint threshold);
322369

370+
#undef UTIL_DECL_IN_RANGE_FUNC
371+
323372
#ifdef __cplusplus
324373
}
325374
#endif

src/hx711.c

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ void hx711_init(
6262
assert(config->reader_prog != NULL);
6363
assert(config->reader_prog_init != NULL);
6464

65+
check_gpio_param(config->clock_pin);
66+
check_gpio_param(config->data_pin);
6567
assert(config->clock_pin != config->data_pin);
6668

6769
mutex_init(&hx->_mut);
@@ -231,26 +233,31 @@ int32_t hx711_get_twos_comp(const uint32_t raw) {
231233
}
232234

233235
bool hx711_is_min_saturated(const int32_t val) {
234-
return val == HX711_MIN_VALUE; //−8,388,608
236+
assert(hx711_is_value_valid(val));
237+
return val == HX711_MIN_VALUE;
235238
}
236239

237240
bool hx711_is_max_saturated(const int32_t val) {
238-
return val == HX711_MAX_VALUE; //8,388,607
241+
assert(hx711_is_value_valid(val));
242+
return val == HX711_MAX_VALUE;
239243
}
240244

241245
unsigned short hx711_get_settling_time(const hx711_rate_t rate) {
242-
assert((uint)rate <= count_of(HX711_SETTLING_TIMES) - 1);
243-
return HX711_SETTLING_TIMES[(uint)rate];
246+
assert(hx711_is_rate_valid(rate));
247+
assert((int)rate <= count_of(HX711_SETTLING_TIMES) - 1);
248+
return HX711_SETTLING_TIMES[(int)rate];
244249
}
245250

246251
unsigned char hx711_get_rate_sps(const hx711_rate_t rate) {
247-
assert((uint)rate <= count_of(HX711_SAMPLE_RATES) - 1);
248-
return HX711_SAMPLE_RATES[(uint)rate];
252+
assert(hx711_is_rate_valid(rate));
253+
assert((int)rate <= count_of(HX711_SAMPLE_RATES) - 1);
254+
return HX711_SAMPLE_RATES[(int)rate];
249255
}
250256

251257
unsigned char hx711_get_clock_pulses(const hx711_gain_t gain) {
252-
assert((uint)gain <= count_of(HX711_CLOCK_PULSES) - 1);
253-
return HX711_CLOCK_PULSES[(uint)gain];
258+
assert(hx711_is_gain_valid(gain));
259+
assert((int)gain <= count_of(HX711_CLOCK_PULSES) - 1);
260+
return HX711_CLOCK_PULSES[(int)gain];
254261
}
255262

256263
int32_t hx711_get_value(hx711_t* const hx) {
@@ -363,14 +370,14 @@ bool hx711_is_pio_gain_valid(const uint32_t g) {
363370

364371
bool hx711_is_rate_valid(const hx711_rate_t r) {
365372
return util_int_in_range(
366-
r,
373+
(int)r,
367374
hx711_rate_10,
368375
hx711_rate_80);
369376
}
370377

371378
bool hx711_is_gain_valid(const hx711_gain_t g) {
372379
return util_int_in_range(
373-
g,
380+
(int)g,
374381
hx711_gain_128,
375382
hx711_gain_64);
376383
}
@@ -379,12 +386,12 @@ void hx711_power_up(
379386
hx711_t* const hx,
380387
const hx711_gain_t gain) {
381388

382-
assert(!hx711__is_state_machine_enabled(hx));
389+
assert(hx711__is_initd(hx));
390+
assert(hx711_is_gain_valid(gain));
383391

384392
//state machines are not running at this point, so don't
385393
//check for that, only if the hx struct is initd
386-
assert(hx711__is_initd(hx));
387-
assert(hx711_is_gain_valid(gain));
394+
assert(!hx711__is_state_machine_enabled(hx));
388395

389396
const uint32_t gainVal = hx711_gain_to_pio_gain(gain);
390397

@@ -443,7 +450,8 @@ void hx711_power_up(
443450

444451
void hx711_power_down(hx711_t* const hx) {
445452

446-
assert(hx711__is_state_machine_enabled(hx));
453+
//don't have to have SMs running; just check for init
454+
assert(hx711__is_initd(hx));
447455

448456
UTIL_MUTEX_BLOCK(hx->_mut,
449457

@@ -492,8 +500,12 @@ uint32_t hx711_gain_to_pio_gain(const hx711_gain_t gain) {
492500

493501
assert(hx711_is_gain_valid(gain));
494502

495-
const uint32_t clockPulses = hx711_get_clock_pulses(gain);
496-
return clockPulses - HX711_READ_BITS - 1;
503+
const uint32_t clockPulses =
504+
hx711_get_clock_pulses(gain) - HX711_READ_BITS - 1;
505+
506+
assert(hx711_is_pio_gain_valid(clockPulses));
507+
508+
return clockPulses;
497509

498510
}
499511

0 commit comments

Comments
 (0)