Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
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 {
63  c->rate = HMC58XX_DEFAULT_DO;
64  c->meas = HMC58XX_DEFAULT_MS;
65  c->gain = HMC58XX_DEFAULT_GN;
66  c->mode = HMC58XX_DEFAULT_MD;
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 
Hmc58xx::type
enum Hmc58xxType type
Definition: hmc58xx.h:71
c
VIC slots used for the LPC2148 define name e g gps UART1_VIC_SLOT e g modem SPI1_VIC_SLOT SPI1 in mcu_periph spi_arch c or spi_slave_hs_arch c(and some others not using the SPI peripheral yet..) I2C0_VIC_SLOT 8 mcu_periph/i2c_arch.c I2C1_VIC_SLOT 9 mcu_periph/i2c_arch.c USB_VIC_SLOT 10 usb
HMC_TYPE_5883
@ HMC_TYPE_5883
Definition: hmc58xx.h:57
i2c_transaction::buf
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
Hmc58xxConfig::mode
uint8_t mode
Measurement mode.
Definition: hmc58xx.h:43
val
uint16_t val[TCOUPLE_NB]
Definition: temp_tcouple_adc.c:49
i2c_transaction::len_r
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
hmc58xx_start_configure
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:126
hmc58xx_event
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:152
Hmc58xx::data
union Hmc58xx::@317 data
HMC58XX_REG_MODE
#define HMC58XX_REG_MODE
Definition: hmc58xx_regs.h:36
I2CTransTx
@ I2CTransTx
transmit only transaction
Definition: i2c.h:47
HMC_CONF_MODE
@ HMC_CONF_MODE
Definition: hmc58xx.h:51
HMC58XX_DEFAULT_MD
#define HMC58XX_DEFAULT_MD
Definition: hmc58xx.c:46
get_sys_time_float
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:129
Hmc58xx::i2c_trans
struct i2c_transaction i2c_trans
Definition: hmc58xx.h:62
HMC_CONF_DONE
@ HMC_CONF_DONE
Definition: hmc58xx.h:52
Hmc58xxConfig::meas
uint8_t meas
Measurement configuration.
Definition: hmc58xx.h:41
I2CTransFailed
@ I2CTransFailed
transaction failed
Definition: i2c.h:58
Hmc58xx::init_status
enum Hmc58xxConfStatus init_status
init status
Definition: hmc58xx.h:64
HMC58XX_STARTUP_DELAY
#define HMC58XX_STARTUP_DELAY
HMC58XX startup delay.
Definition: hmc58xx.c:58
Hmc58xxConfig::gain
uint8_t gain
Gain configuration (1 -> +- 1 Gauss)
Definition: hmc58xx.h:42
HMC_CONF_UNINIT
@ HMC_CONF_UNINIT
Definition: hmc58xx.h:48
i2c_transaction::len_w
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
I2CTransSuccess
@ I2CTransSuccess
transaction successfully finished by I2C driver
Definition: i2c.h:57
std.h
HMC58XX_DEFAULT_MS
#define HMC58XX_DEFAULT_MS
Definition: hmc58xx.c:40
HMC58XX_REG_CFGA
#define HMC58XX_REG_CFGA
Definition: hmc58xx_regs.h:34
Hmc58xxConfig
Definition: hmc58xx.h:39
sys_time.h
Architecture independent timing functions.
HMC58XX_REG_DATXM
#define HMC58XX_REG_DATXM
Definition: hmc58xx_regs.h:37
uint8_t
unsigned char uint8_t
Definition: types.h:14
hmc58xx_send_config
static void hmc58xx_send_config(struct Hmc58xx *hmc)
Configuration function called once before normal use.
Definition: hmc58xx.c:101
i2c_transaction::status
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
hmc58xx_read
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:139
HMC_CONF_CRB
@ HMC_CONF_CRB
Definition: hmc58xx.h:50
I2CTransTxRx
@ I2CTransTxRx
transmit and receive transaction
Definition: i2c.h:49
hmc58xx_i2c_tx_reg
static void hmc58xx_i2c_tx_reg(struct Hmc58xx *hmc, uint8_t reg, uint8_t val)
Definition: hmc58xx.c:90
i2c_transaction::slave_addr
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
Hmc58xx::adc_overflow_cnt
uint16_t adc_overflow_cnt
counts number of ADC measurement under/overflows
Definition: hmc58xx.h:72
hmc58xx_init
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
HMC58XX_DEFAULT_DO
#define HMC58XX_DEFAULT_DO
Definition: hmc58xx.c:37
HMC58XX_DEFAULT_GN
#define HMC58XX_DEFAULT_GN
Definition: hmc58xx.c:43
HMC_TYPE_5843
@ HMC_TYPE_5843
Definition: hmc58xx.h:56
Hmc58xxConfig::rate
uint8_t rate
Data Output Rate Bits(6 -> 50Hz with HMC5843, 75Hz with HMC5883)
Definition: hmc58xx.h:40
hmc58xx.h
I2CTransDone
@ I2CTransDone
transaction set to done by user level
Definition: i2c.h:59
hmc58xx_set_default_config
static void hmc58xx_set_default_config(struct Hmc58xxConfig *c)
Definition: hmc58xx.c:61
Hmc58xx
Definition: hmc58xx.h:60
i2c_submit
static bool i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c.h:266
HMC_CONF_CRA
@ HMC_CONF_CRA
Definition: hmc58xx.h:49
Hmc58xx::config
struct Hmc58xxConfig config
Definition: hmc58xx.h:70
Hmc58xx::data_available
volatile bool data_available
data ready flag
Definition: hmc58xx.h:65
Int16FromBuf
#define Int16FromBuf(_buf, _idx)
Definition: hmc58xx.c:150
HMC58XX_REG_CFGB
#define HMC58XX_REG_CFGB
Definition: hmc58xx_regs.h:35
Hmc58xx::i2c_p
struct i2c_periph * i2c_p
Definition: hmc58xx.h:61
i2c_periph
Definition: i2c.h:144
i2c_transaction::type
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
Hmc58xx::initialized
bool initialized
config done flag
Definition: hmc58xx.h:63