Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
hx711.c
Go to the documentation of this file.
1/*
2 * Copyright (C) Freek van Tienen <freek.v.tienen@gmail.com>
3 *
4 * This file is part of paparazzi.
5 *
6 * paparazzi is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2, or (at your option)
9 * any later version.
10 *
11 * paparazzi is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with paparazzi; see the file COPYING. If not, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
28#include "hx711.h"
29#include "mcu_periph/gpio.h"
30#include "mcu_periph/sys_time.h"
31#include "modules/core/abi.h"
34#include BOARD_CONFIG
35
36#ifndef HX711_DEVICES_NB
37#define HX711_DEVICES_NB 4
38#endif
39
40#ifndef HX711_GAIN
41#define HX711_GAIN 1
42#endif
43
44#ifndef HX711_PWM_FREQUENCY
45#define HX711_PWM_FREQUENCY 6000000
46#endif
47
48#ifndef HX711_DEVICES
49#define HX711_DEVICES {}
50#endif
51
52#ifndef HX711_FORCE_SENSOR_ID
53#define HX711_FORCE_SENSOR_ID ABI_BROADCAST
54#endif
55
56#ifndef HX711_DEBUG
57#define HX711_DEBUG false
58#endif
59
60#if HX711_DEBUG
61#include <stdio.h>
62#endif
63
64// Running at 50kHz for a full clock pulse (10us high, 10us low)
65#define HX711_PERIOD (HX711_PWM_FREQUENCY / 50000)
66
73
74struct hx711_t {
75 volatile bool busy;
76 volatile bool measurement_ready;
78
80};
81
82
83static void pwmpcb(PWMDriver *pwmp __attribute__((unused)));
84
85static volatile struct hx711_t hx711 = {
86 .busy = false,
87 .measurement_ready = false,
88 .devices = {
90 },
91 .read_bit_idx = 0
92};
93
94static PWMConfig pwmcfg = {
95 .frequency = HX711_PWM_FREQUENCY,
96 .period = HX711_PERIOD,
97 .callback = NULL,
98 .channels = {
103 },
104 .cr2 = 0,
105 .bdtr = 0,
106 .dier = 0
107};
108
109
110void hx711_init(void)
111{
112 /*----------------
113 * Configure GPIO
114 *----------------*/
117 pwmcfg.channels[HX711_PWM_CHANNEL].callback = pwmpcb;
118
119 for(uint8_t i = 0; i < HX711_DEVICES_NB; i++) {
121 }
122
123 /*---------------
124 * Configure PWM
125 *---------------*/
127 hx711.busy = false;
128 hx711.measurement_ready = false;
130}
131
135void hx711_event(void)
136{
137 // If busy reading data, return
138 if(hx711.busy)
139 return;
140
141 // Check if we have a measurement to read
144 for(uint8_t i = 0; i < HX711_DEVICES_NB; i++) {
145 values[i] = hx711.devices[i].measurement;
146 }
147
149
150 // Send down for debug
152 for(uint8_t i = 0; i < HX711_DEVICES_NB; i++) {
153 raw_val[i] = values[i];
154 }
155
156#if HX711_DEBUG
158 float freq = 1 / (current_time - hx711_meas_time);
160
161 char hx711_str[10];
162 char hx711_freq_str[15];
163 int hx711_len = snprintf(hx711_str, sizeof(hx711_str), "HX711");
164 int hx711_freq_len = snprintf(hx711_freq_str, sizeof(hx711_freq_str), "HX711 freq");
165 RunOnceEvery(10, {
167 });
168 RunOnceEvery(15, {
170 });
173#endif
174
175 hx711.measurement_ready = false;
176 }
177
178 // Check if all data pins are low, ready to read data
179 for(uint8_t i = 0; i < HX711_DEVICES_NB; i++) {
181 return;
182 }
183
184 // Start reading data
185 chSysLock();
186 hx711.busy = true;
187 hx711.measurement_ready = false;
191 chSysUnlock();
192}
193
197static void pwmpcb(PWMDriver *pwmp __attribute__((unused))) {
199
200 /* In case we where not reading just stop */
201 if(!hx711.busy) {
205 return;
206 }
207
208 /* Parse the measurements for all devices */
209 if(hx711.read_bit_idx < 24) {
210 for(uint8_t i = 0; i < HX711_DEVICES_NB; i++) {
211 volatile struct hx711_dev_t *dev = &hx711.devices[i];
212
213 // Reset or bitshift
214 if(hx711.read_bit_idx == 0)
215 dev->measurement = 0;
216 else
217 dev->measurement <<= 1;
218
219 // Read the bit
220 if(gpio_get(dev->data_port, dev->data_pin) != 0)
221 dev->measurement |= 1;
222
223 // Fix 2s complement for 32 bit signed integer from 24 bit
224 if(dev->measurement & (1 << 23)) {
225 dev->measurement |= 0xFF000000;
226 }
227 }
228 }
230
231 /* Send gain and finish reading data */
232 if(hx711.read_bit_idx >= (24 + HX711_GAIN)) {
235 hx711.busy = false;
238 return;
239 }
240
242}
Main include for ABI (AirBorneInterface).
void gpio_setup_pin_af(ioportid_t port, uint16_t pin, uint8_t af, bool is_output)
Setup a gpio for input or output with alternate function.
Definition gpio_arch.c:65
void gpio_setup_input(ioportid_t port, uint16_t gpios)
Setup one or more pins of the given GPIO port as inputs.
Definition gpio_arch.c:40
static uint8_t gpio_get(ioportid_t port, uint16_t pin)
Get level of a gpio.
Definition gpio_arch.h:94
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
static struct uart_periph * dev
Some architecture independent helper functions for GPIOs.
#define HX711_DEVICES_NB
Definition hx711.c:37
uint16_t data_pin
Definition hx711.c:70
static void pwmpcb(PWMDriver *pwmp)
Callback on the falling edge of the clock signal.
Definition hx711.c:197
#define HX711_GAIN
Definition hx711.c:41
struct hx711_dev_t devices[HX711_DEVICES_NB]
Definition hx711.c:77
static volatile struct hx711_t hx711
Definition hx711.c:85
#define HX711_FORCE_SENSOR_ID
Definition hx711.c:53
static PWMConfig pwmcfg
Definition hx711.c:94
volatile uint8_t read_bit_idx
Definition hx711.c:79
float hx711_meas_time
Definition hx711.c:67
ioportid_t data_port
Definition hx711.c:69
#define HX711_PERIOD
Definition hx711.c:65
#define HX711_PWM_FREQUENCY
Definition hx711.c:45
volatile bool measurement_ready
Definition hx711.c:76
int32_t measurement
Definition hx711.c:71
#define HX711_DEVICES
Definition hx711.c:49
void hx711_init(void)
Definition hx711.c:110
void hx711_event(void)
Start a measurement and publish the results when possible.
Definition hx711.c:135
volatile bool busy
Definition hx711.c:75
Interface for the HX711 sensor.
uint16_t foo
Definition main_demo5.c:58
struct pprzlog_transport pprzlog_tp
PPRZLOG transport structure.
Definition pprzlog_tp.c:29
Architecture independent timing functions.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition sys_time.h:168
Periodic telemetry system header (includes downlink utility and generated code).
unsigned short uint16_t
Typedef defining 16 bit unsigned short type.
int int32_t
Typedef defining 32 bit int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.