Paparazzi UAS v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
hmc58xx.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2011 Gautier Hattenberger <gautier.hattenberger@enac.fr>
3 * 2013 Felix Ruess <felix.ruess@gmail.com>
4 *
5 * This file is part of paparazzi.
6 *
7 * paparazzi is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
11 *
12 * paparazzi is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with paparazzi; see the file COPYING. If not, write to
19 * the Free Software Foundation, 59 Temple Place - Suite 330,
20 * Boston, MA 02111-1307, USA.
21 */
22
30#include "peripherals/hmc58xx.h"
31#include "mcu_periph/sys_time.h"
32#include "std.h"
33
34
35/* HMC58XX default conf */
36#ifndef HMC58XX_DEFAULT_DO
37#define HMC58XX_DEFAULT_DO 0x6 // Data Output Rate (6 -> 50Hz with HMC5843, 75Hz with HMC5883)
38#endif
39#ifndef HMC58XX_DEFAULT_MS
40#define HMC58XX_DEFAULT_MS 0x0 // Measurement configuration
41#endif
42#ifndef HMC58XX_DEFAULT_GN
43#define HMC58XX_DEFAULT_GN 0x1 // Gain configuration (1 -> +- 1 Gauss)
44#endif
45#ifndef HMC58XX_DEFAULT_MD
46#define HMC58XX_DEFAULT_MD 0x0 // Continious measurement mode
47#endif
48#ifndef HMC58XX_DEFAULT_SA
49#define HMC58XX_DEFAULT_SA 0x0 // Number of samples averaged
50#endif
51#ifndef HMC58XX_DEFAULT_TC
52#define HMC58XX_DEFAULT_TC 0x0 // Automatic compensation of sensitivity over temperature
53#endif
54
63#ifndef HMC58XX_STARTUP_DELAY
64#define HMC58XX_STARTUP_DELAY 1.5
65#endif
66
76
83void hmc58xx_init(struct Hmc58xx *hmc, struct i2c_periph *i2c_p, uint8_t addr)
84{
85 /* set i2c_peripheral */
86 hmc->i2c_p = i2c_p;
87 /* set i2c address */
88 hmc->i2c_trans.slave_addr = addr;
90 /* set default config options */
92 hmc->type = HMC_TYPE_5883;
93 hmc->initialized = false;
95 hmc->adc_overflow_cnt = 0;
96}
97
98static void hmc58xx_i2c_tx_reg(struct Hmc58xx *hmc, uint8_t reg, uint8_t val)
99{
101 hmc->i2c_trans.buf[0] = reg;
102 hmc->i2c_trans.buf[1] = val;
103 hmc->i2c_trans.len_r = 0;
104 hmc->i2c_trans.len_w = 2;
105 i2c_submit(hmc->i2c_p, &(hmc->i2c_trans));
106}
107
109static void hmc58xx_send_config(struct Hmc58xx *hmc)
110{
111 switch (hmc->init_status) {
112 case HMC_CONF_CRA:
113 hmc58xx_i2c_tx_reg(hmc, HMC58XX_REG_CFGA, (hmc->config.temp_comp << 7) | (hmc->config.samples_averaged << 5) | (hmc->config.rate << 2) | (hmc->config.meas));
114 hmc->init_status++;
115 break;
116 case HMC_CONF_CRB:
118 hmc->init_status++;
119 break;
120 case HMC_CONF_MODE:
122 hmc->init_status++;
123 break;
124 case HMC_CONF_DONE:
125 hmc->initialized = true;
127 break;
128 default:
129 break;
130 }
131}
132
133// Configure
135{
136 // wait before starting the configuration
137 // doing to early may void the mode configuration
139 hmc->init_status++;
142 }
143 }
144}
145
146// Normal reading
147void hmc58xx_read(struct Hmc58xx *hmc)
148{
149 if (hmc->initialized && hmc->i2c_trans.status == I2CTransDone) {
152 hmc->i2c_trans.len_r = 6;
153 hmc->i2c_trans.len_w = 1;
154 i2c_submit(hmc->i2c_p, &(hmc->i2c_trans));
155 }
156}
157
158#define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
159
160void hmc58xx_event(struct Hmc58xx *hmc)
161{
162 if (hmc->initialized) {
163 if (hmc->i2c_trans.status == I2CTransFailed) {
165 } else if (hmc->i2c_trans.status == I2CTransSuccess) {
166 if (hmc->type == HMC_TYPE_5843) {
167 hmc->data.vect.x = Int16FromBuf(hmc->i2c_trans.buf, 0);
168 hmc->data.vect.y = Int16FromBuf(hmc->i2c_trans.buf, 2);
169 hmc->data.vect.z = Int16FromBuf(hmc->i2c_trans.buf, 4);
170 }
171 /* HMC5883 has xzy order of axes in returned data */
172 else {
173 hmc->data.vect.x = Int16FromBuf(hmc->i2c_trans.buf, 0);
174 hmc->data.vect.y = Int16FromBuf(hmc->i2c_trans.buf, 4);
175 hmc->data.vect.z = Int16FromBuf(hmc->i2c_trans.buf, 2);
176 }
177 /* only set available if measurements valid: -4096 if ADC under/overflow in sensor */
178 if (hmc->data.vect.x != -4096 && hmc->data.vect.y != -4096 &&
179 hmc->data.vect.z != -4096) {
180 hmc->data_available = true;
181 }
182 else {
183 hmc->adc_overflow_cnt++;
184 }
186 }
187 } else if (hmc->init_status != HMC_CONF_UNINIT) { // Configuring but not yet initialized
191 }
192 if (hmc->i2c_trans.status == I2CTransFailed) {
193 hmc->init_status--;
195 hmc58xx_send_config(hmc); // Retry config (TODO max retry)
196 }
197 }
198}
199
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition i2c.h:122
uint16_t len_r
Number of bytes to read/receive.
Definition i2c.h:110
enum I2CTransactionType type
Transaction type.
Definition i2c.h:98
enum I2CTransactionStatus status
Transaction status.
Definition i2c.h:126
uint8_t slave_addr
Slave address.
Definition i2c.h:104
uint8_t len_w
Number of bytes to write/transmit.
Definition i2c.h:116
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition i2c.h:266
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition i2c.h:57
@ I2CTransFailed
transaction failed
Definition i2c.h:58
@ I2CTransDone
transaction set to done by user level
Definition i2c.h:59
@ I2CTransTx
transmit only transaction
Definition i2c.h:47
@ I2CTransTxRx
transmit and receive transaction
Definition i2c.h:49
#define HMC58XX_DEFAULT_GN
Definition hmc58xx.c:43
#define HMC58XX_DEFAULT_SA
Definition hmc58xx.c:49
#define HMC58XX_DEFAULT_TC
Definition hmc58xx.c:52
void hmc58xx_init(struct Hmc58xx *hmc, struct i2c_periph *i2c_p, uint8_t addr)
Initialize Hmc58xx struct and set default config options.
Definition hmc58xx.c:83
static void hmc58xx_set_default_config(struct Hmc58xxConfig *c)
Definition hmc58xx.c:67
static void hmc58xx_send_config(struct Hmc58xx *hmc)
Configuration function called once before normal use.
Definition hmc58xx.c:109
#define HMC58XX_DEFAULT_MS
Definition hmc58xx.c:40
#define HMC58XX_STARTUP_DELAY
HMC58XX startup delay.
Definition hmc58xx.c:64
void hmc58xx_event(struct Hmc58xx *hmc)
Definition hmc58xx.c:160
#define HMC58XX_DEFAULT_DO
Definition hmc58xx.c:37
void hmc58xx_read(struct Hmc58xx *hmc)
Definition hmc58xx.c:147
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition hmc58xx.c:134
#define HMC58XX_DEFAULT_MD
Definition hmc58xx.c:46
#define Int16FromBuf(_buf, _idx)
Definition hmc58xx.c:158
static void hmc58xx_i2c_tx_reg(struct Hmc58xx *hmc, uint8_t reg, uint8_t val)
Definition hmc58xx.c:98
uint8_t mode
Measurement mode.
Definition hmc58xx.h:43
bool initialized
config done flag
Definition hmc58xx.h:65
union Hmc58xx::@323 data
enum Hmc58xxType type
Definition hmc58xx.h:73
struct i2c_periph * i2c_p
Definition hmc58xx.h:63
struct i2c_transaction i2c_trans
Definition hmc58xx.h:64
@ HMC_TYPE_5883
Definition hmc58xx.h:59
@ HMC_TYPE_5843
Definition hmc58xx.h:58
uint8_t samples_averaged
Number of samples averaged per measurement output.
Definition hmc58xx.h:44
uint8_t temp_comp
Enable temperature sensor (only on HMC5983)
Definition hmc58xx.h:45
@ HMC_CONF_MODE
Definition hmc58xx.h:53
@ HMC_CONF_UNINIT
Definition hmc58xx.h:50
@ HMC_CONF_DONE
Definition hmc58xx.h:54
@ HMC_CONF_CRB
Definition hmc58xx.h:52
@ HMC_CONF_CRA
Definition hmc58xx.h:51
uint8_t meas
Measurement configuration.
Definition hmc58xx.h:41
uint8_t gain
Gain configuration (1 -> +- 1 Gauss)
Definition hmc58xx.h:42
struct Hmc58xxConfig config
Definition hmc58xx.h:72
enum Hmc58xxConfStatus init_status
init status
Definition hmc58xx.h:66
volatile bool data_available
data ready flag
Definition hmc58xx.h:67
uint8_t rate
Data Output Rate Bits(6 -> 50Hz with HMC5843, 75Hz with HMC5883)
Definition hmc58xx.h:40
uint16_t adc_overflow_cnt
counts number of ADC measurement under/overflows
Definition hmc58xx.h:74
#define HMC58XX_REG_CFGB
#define HMC58XX_REG_CFGA
#define HMC58XX_REG_MODE
#define HMC58XX_REG_DATXM
Architecture independent timing functions.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition sys_time.h:138
uint16_t val[TCOUPLE_NB]
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.