hx711-pico-c
hx711_reader.pio.h File Reference
#include "hardware/pio.h"
#include <assert.h>
#include <stddef.h>
#include "hardware/clocks.h"
#include "hardware/structs/clocks.h"
#include "hx711.h"

Go to the source code of this file.

Macros

#define hx711_reader_wrap_target   3
 
#define hx711_reader_wrap   13
 
#define hx711_reader_HZ   10000000
 

Functions

static pio_sm_config hx711_reader_program_get_default_config (uint offset)
 
void hx711_reader_pio_init (hx711_t *const hx)
 
void hx711_reader_program_init (hx711_t *const hx)
 

Variables

static const uint16_t hx711_reader_program_instructions []
 
static const struct pio_program hx711_reader_program
 

Macro Definition Documentation

◆ hx711_reader_HZ

#define hx711_reader_HZ   10000000

Definition at line 18 of file hx711_reader.pio.h.

◆ hx711_reader_wrap

#define hx711_reader_wrap   13

Definition at line 16 of file hx711_reader.pio.h.

◆ hx711_reader_wrap_target

#define hx711_reader_wrap_target   3

Definition at line 15 of file hx711_reader.pio.h.

Function Documentation

◆ hx711_reader_pio_init()

void hx711_reader_pio_init ( hx711_t *const  hx)

Definition at line 80 of file hx711_reader.pio.h.

80  {
81  assert(hx != NULL);
82  assert(hx->_pio != NULL);
83  pio_gpio_init(
84  hx->_pio,
85  hx->_clock_pin);
86  //clock pin setup
87  pio_sm_set_out_pins(
88  hx->_pio,
89  hx->_reader_sm,
90  hx->_clock_pin,
91  1);
92  pio_sm_set_set_pins(
93  hx->_pio,
94  hx->_reader_sm,
95  hx->_clock_pin,
96  1);
97  pio_sm_set_consecutive_pindirs(
98  hx->_pio,
99  hx->_reader_sm,
100  hx->_clock_pin,
101  1,
102  true);
103  //data pin setup
104  pio_gpio_init(
105  hx->_pio,
106  hx->_data_pin);
107  pio_sm_set_in_pins(
108  hx->_pio,
109  hx->_reader_sm,
110  hx->_data_pin);
111  pio_sm_set_consecutive_pindirs(
112  hx->_pio,
113  hx->_reader_sm,
114  hx->_data_pin,
115  1,
116  false);
117 }
uint _data_pin
Definition: hx711.h:76
PIO _pio
Definition: hx711.h:78
uint _reader_sm
Definition: hx711.h:81
uint _clock_pin
Definition: hx711.h:75

References hx711_t::_clock_pin, hx711_t::_data_pin, hx711_t::_pio, and hx711_t::_reader_sm.

◆ hx711_reader_program_get_default_config()

static pio_sm_config hx711_reader_program_get_default_config ( uint  offset)
inlinestatic

Definition at line 46 of file hx711_reader.pio.h.

46  {
47  pio_sm_config c = pio_get_default_sm_config();
48  sm_config_set_wrap(&c, offset + hx711_reader_wrap_target, offset + hx711_reader_wrap);
49  sm_config_set_sideset(&c, 2, true, false);
50  return c;
51 }
#define hx711_reader_wrap
#define hx711_reader_wrap_target

References hx711_reader_wrap, and hx711_reader_wrap_target.

Referenced by hx711_reader_program_init().

◆ hx711_reader_program_init()

void hx711_reader_program_init ( hx711_t *const  hx)

Why enable autopush?

"The state machine keeps an eye on the total amount of data shifted into the ISR, and on the in which reaches or breaches a total shift count of 32 (or whatever number you have configured), the ISR contents, along with the new data from the in. goes straight to the RX FIFO. The ISR is cleared to zero in the same operation."

  • Raspberry Pi Pico C/C++ SDK pg. 45

When manually pushing using noblock, the FIFO contents are NOT changed.

"The PIO assembler sets the Block bit by default. If the Block bit is not set, the PUSH does not stall on a full RX FIFO, instead continuing immediately to the next instruction. The FIFO state and contents are unchanged when this happens. The ISR is still cleared to all-zeroes, and the FDEBUG_RXSTALL flag is set (the same as a blocking PUSH or autopush to a full RX FIFO) to indicate data was lost."

  • Raspberry Pi Pico C/C++ SDK pg. 64

Manually pushing is not ideal. Application code should be able to look at the FIFO and assume that the value inside is the most up-to-date data available. Autopushing does this.

Definition at line 118 of file hx711_reader.pio.h.

118  {
119  assert(hx != NULL);
120  assert(hx->_pio != NULL);
121  pio_sm_config cfg = hx711_reader_program_get_default_config(
122  hx->_reader_offset);
123  const float div = (float)(clock_get_hz(clk_sys)) / (uint)hx711_reader_HZ;
124  sm_config_set_clkdiv(
125  &cfg,
126  div);
127  sm_config_set_set_pins(
128  &cfg,
129  hx->_clock_pin,
130  1);
131  sm_config_set_out_pins(
132  &cfg,
133  hx->_clock_pin,
134  1);
135  sm_config_set_sideset_pins(
136  &cfg,
137  hx->_clock_pin);
138  sm_config_set_in_pins(
139  &cfg,
140  hx->_data_pin);
160  sm_config_set_in_shift(
161  &cfg,
162  false, //false = shift in left
163  true, //true = autopush enabled
164  HX711_READ_BITS); //autopush on 24 bits
165  pio_sm_clear_fifos(
166  hx->_pio,
167  hx->_reader_sm);
168  //store a copy of the configuration for resetting the sm
169  hx->_reader_prog_default_config = cfg;
170 }
#define HX711_READ_BITS
Definition: hx711.h:49
#define hx711_reader_HZ
static pio_sm_config hx711_reader_program_get_default_config(uint offset)
pio_sm_config _reader_prog_default_config
Definition: hx711.h:80
uint _reader_offset
Definition: hx711.h:82

References hx711_t::_clock_pin, hx711_t::_data_pin, hx711_t::_pio, hx711_t::_reader_offset, hx711_t::_reader_prog_default_config, hx711_t::_reader_sm, HX711_READ_BITS, hx711_reader_HZ, and hx711_reader_program_get_default_config().

Variable Documentation

◆ hx711_reader_program

const struct pio_program hx711_reader_program
static
Initial value:
= {
.length = 14,
.origin = -1,
}
static const uint16_t hx711_reader_program_instructions[]

Definition at line 20 of file hx711_reader.pio.h.

◆ hx711_reader_program_instructions

const uint16_t hx711_reader_program_instructions[]
static
Initial value:
= {
0xe020,
0x8080,
0x6020,
0xe057,
0x2020,
0xe001,
0x4001,
0x1185,
0x9880,
0x6020,
0x1023,
0xa041,
0xe101,
0x118c,
}

Definition at line 20 of file hx711_reader.pio.h.