Paparazzi UAS  v5.10_stable-5-g83a0da5-dirty
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 
57 #ifndef HMC58XX_STARTUP_DELAY
58 #define HMC58XX_STARTUP_DELAY 1.5
59 #endif
60 
62 {
67 }
68 
75 void hmc58xx_init(struct Hmc58xx *hmc, struct i2c_periph *i2c_p, uint8_t addr)
76 {
77  /* set i2c_peripheral */
78  hmc->i2c_p = i2c_p;
79  /* set i2c address */
80  hmc->i2c_trans.slave_addr = addr;
82  /* set default config options */
84  hmc->type = HMC_TYPE_5883;
85  hmc->initialized = false;
87  hmc->adc_overflow_cnt = 0;
88 }
89 
90 static void hmc58xx_i2c_tx_reg(struct Hmc58xx *hmc, uint8_t reg, uint8_t val)
91 {
92  hmc->i2c_trans.type = I2CTransTx;
93  hmc->i2c_trans.buf[0] = reg;
94  hmc->i2c_trans.buf[1] = val;
95  hmc->i2c_trans.len_r = 0;
96  hmc->i2c_trans.len_w = 2;
97  i2c_submit(hmc->i2c_p, &(hmc->i2c_trans));
98 }
99 
101 static void hmc58xx_send_config(struct Hmc58xx *hmc)
102 {
103  switch (hmc->init_status) {
104  case HMC_CONF_CRA:
105  hmc58xx_i2c_tx_reg(hmc, HMC58XX_REG_CFGA, (hmc->config.rate << 2) | (hmc->config.meas));
106  hmc->init_status++;
107  break;
108  case HMC_CONF_CRB:
109  hmc58xx_i2c_tx_reg(hmc, HMC58XX_REG_CFGB, (hmc->config.gain << 5));
110  hmc->init_status++;
111  break;
112  case HMC_CONF_MODE:
114  hmc->init_status++;
115  break;
116  case HMC_CONF_DONE:
117  hmc->initialized = true;
119  break;
120  default:
121  break;
122  }
123 }
124 
125 // Configure
127 {
128  // wait before starting the configuration
129  // doing to early may void the mode configuration
131  hmc->init_status++;
132  if (hmc->i2c_trans.status == I2CTransSuccess || hmc->i2c_trans.status == I2CTransDone) {
133  hmc58xx_send_config(hmc);
134  }
135  }
136 }
137 
138 // Normal reading
139 void hmc58xx_read(struct Hmc58xx *hmc)
140 {
141  if (hmc->initialized && hmc->i2c_trans.status == I2CTransDone) {
142  hmc->i2c_trans.buf[0] = HMC58XX_REG_DATXM;
143  hmc->i2c_trans.type = I2CTransTxRx;
144  hmc->i2c_trans.len_r = 6;
145  hmc->i2c_trans.len_w = 1;
146  i2c_submit(hmc->i2c_p, &(hmc->i2c_trans));
147  }
148 }
149 
150 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
151 
152 void hmc58xx_event(struct Hmc58xx *hmc)
153 {
154  if (hmc->initialized) {
155  if (hmc->i2c_trans.status == I2CTransFailed) {
157  } else if (hmc->i2c_trans.status == I2CTransSuccess) {
158  if (hmc->type == HMC_TYPE_5843) {
159  hmc->data.vect.x = Int16FromBuf(hmc->i2c_trans.buf, 0);
160  hmc->data.vect.y = Int16FromBuf(hmc->i2c_trans.buf, 2);
161  hmc->data.vect.z = Int16FromBuf(hmc->i2c_trans.buf, 4);
162  }
163  /* HMC5883 has xzy order of axes in returned data */
164  else {
165  hmc->data.vect.x = Int16FromBuf(hmc->i2c_trans.buf, 0);
166  hmc->data.vect.y = Int16FromBuf(hmc->i2c_trans.buf, 4);
167  hmc->data.vect.z = Int16FromBuf(hmc->i2c_trans.buf, 2);
168  }
169  /* only set available if measurements valid: -4096 if ADC under/overflow in sensor */
170  if (hmc->data.vect.x != -4096 && hmc->data.vect.y != -4096 &&
171  hmc->data.vect.z != -4096) {
172  hmc->data_available = true;
173  }
174  else {
175  hmc->adc_overflow_cnt++;
176  }
178  }
179  } else if (hmc->init_status != HMC_CONF_UNINIT) { // Configuring but not yet initialized
180  if (hmc->i2c_trans.status == I2CTransSuccess || hmc->i2c_trans.status == I2CTransDone) {
182  hmc58xx_send_config(hmc);
183  }
184  if (hmc->i2c_trans.status == I2CTransFailed) {
185  hmc->init_status--;
187  hmc58xx_send_config(hmc); // Retry config (TODO max retry)
188  }
189  }
190 }
191 
#define HMC58XX_DEFAULT_MD
Definition: hmc58xx.c:46
bool initialized
config done flag
Definition: hmc58xx.h:63
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
struct i2c_periph * i2c_p
Definition: hmc58xx.h:61
enum Hmc58xxType type
Definition: hmc58xx.h:71
union Hmc58xx::@301 data
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
enum Hmc58xxConfStatus init_status
init status
Definition: hmc58xx.h:64
uint8_t mode
Measurement mode.
Definition: hmc58xx.h:43
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
transaction successfully finished by I2C driver
Definition: i2c.h:57
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:126
transmit and receive transaction
Definition: i2c.h:49
#define HMC58XX_STARTUP_DELAY
HMC58XX startup delay.
Definition: hmc58xx.c:58
struct i2c_transaction i2c_trans
Definition: hmc58xx.h:62
#define HMC58XX_REG_CFGA
Definition: hmc58xx_regs.h:34
uint8_t gain
Gain configuration (1 -> +- 1 Gauss)
Definition: hmc58xx.h:42
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:129
uint8_t meas
Measurement configuration.
Definition: hmc58xx.h:41
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
static void hmc58xx_send_config(struct Hmc58xx *hmc)
Configuration function called once before normal use.
Definition: hmc58xx.c:101
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
transaction set to done by user level
Definition: i2c.h:59
Architecture independent timing functions.
uint16_t val[TCOUPLE_NB]
#define HMC58XX_DEFAULT_MS
Definition: hmc58xx.c:40
bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
i2c_submit() function
Definition: i2c_arch.c:316
static void hmc58xx_i2c_tx_reg(struct Hmc58xx *hmc, uint8_t reg, uint8_t val)
Definition: hmc58xx.c:90
#define HMC58XX_REG_DATXM
Definition: hmc58xx_regs.h:37
transaction failed
Definition: i2c.h:58
uint8_t rate
Data Output Rate Bits(6 -> 50Hz with HMC5843, 75Hz with HMC5883)
Definition: hmc58xx.h:40
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
#define HMC58XX_DEFAULT_GN
Definition: hmc58xx.c:43
I2C peripheral structure.
Definition: i2c.h:138
uint16_t adc_overflow_cnt
counts number of ADC measurement under/overflows
Definition: hmc58xx.h:72
unsigned char uint8_t
Definition: types.h:14
struct Hmc58xxConfig config
Definition: hmc58xx.h:70
static void hmc58xx_set_default_config(struct Hmc58xxConfig *c)
Definition: hmc58xx.c:61
transmit only transaction
Definition: i2c.h:47
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:75
#define Int16FromBuf(_buf, _idx)
Definition: hmc58xx.c:150
#define HMC58XX_REG_CFGB
Definition: hmc58xx_regs.h:35
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
#define HMC58XX_DEFAULT_DO
Definition: hmc58xx.c:37
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
#define HMC58XX_REG_MODE
Definition: hmc58xx_regs.h:36