hx711-pico-c
hx711_reader.pio.h
Go to the documentation of this file.
1 // -------------------------------------------------- //
2 // This file is autogenerated by pioasm; do not edit! //
3 // -------------------------------------------------- //
4 
5 #pragma once
6 
7 #if !PICO_NO_HARDWARE
8 #include "hardware/pio.h"
9 #endif
10 
11 // ------------ //
12 // hx711_reader //
13 // ------------ //
14 
15 #define hx711_reader_wrap_target 3
16 #define hx711_reader_wrap 13
17 
18 #define hx711_reader_HZ 10000000
19 
20 static const uint16_t hx711_reader_program_instructions[] = {
21  0xe020, // 0: set x, 0
22  0x8080, // 1: pull noblock
23  0x6020, // 2: out x, 32
24  // .wrap_target
25  0xe057, // 3: set y, 23
26  0x2020, // 4: wait 0 pin, 0
27  0xe001, // 5: set pins, 1
28  0x4001, // 6: in pins, 1
29  0x1185, // 7: jmp y--, 5 side 0 [1]
30  0x9880, // 8: pull noblock side 1
31  0x6020, // 9: out x, 32
32  0x1023, // 10: jmp !x, 3 side 0
33  0xa041, // 11: mov y, x
34  0xe101, // 12: set pins, 1 [1]
35  0x118c, // 13: jmp y--, 12 side 0 [1]
36  // .wrap
37 };
38 
39 #if !PICO_NO_HARDWARE
40 static const struct pio_program hx711_reader_program = {
41  .instructions = hx711_reader_program_instructions,
42  .length = 14,
43  .origin = -1,
44 };
45 
46 static inline pio_sm_config hx711_reader_program_get_default_config(uint offset) {
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 }
52 
53 // MIT License
54 //
55 // Copyright (c) 2023 Daniel Robertson
56 //
57 // Permission is hereby granted, free of charge, to any person obtaining a copy
58 // of this software and associated documentation files (the "Software"), to deal
59 // in the Software without restriction, including without limitation the rights
60 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
61 // copies of the Software, and to permit persons to whom the Software is
62 // furnished to do so, subject to the following conditions:
63 //
64 // The above copyright notice and this permission notice shall be included in all
65 // copies or substantial portions of the Software.
66 //
67 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
68 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
69 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
70 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
71 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
72 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
73 // SOFTWARE.
74 #include <assert.h>
75 #include <stddef.h>
76 #include "hardware/clocks.h"
77 #include "hardware/pio.h"
78 #include "hardware/structs/clocks.h"
79 #include "hx711.h"
80 void hx711_reader_pio_init(hx711_t* const hx) {
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 }
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 }
171 
172 #endif
173 
#define HX711_READ_BITS
Definition: hx711.h:49
#define hx711_reader_HZ
static const struct pio_program hx711_reader_program
#define hx711_reader_wrap
#define hx711_reader_wrap_target
void hx711_reader_program_init(hx711_t *const hx)
static const uint16_t hx711_reader_program_instructions[]
void hx711_reader_pio_init(hx711_t *const hx)
static pio_sm_config hx711_reader_program_get_default_config(uint offset)
Definition: hx711.h:73
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
uint _reader_offset
Definition: hx711.h:82