Paparazzi UAS  v5.2.2_stable-0-gd6b9f29
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures 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 }
88 
89 static void hmc58xx_i2c_tx_reg(struct Hmc58xx *hmc, uint8_t reg, uint8_t val)
90 {
91  hmc->i2c_trans.type = I2CTransTx;
92  hmc->i2c_trans.buf[0] = reg;
93  hmc->i2c_trans.buf[1] = val;
94  hmc->i2c_trans.len_r = 0;
95  hmc->i2c_trans.len_w = 2;
96  i2c_submit(hmc->i2c_p, &(hmc->i2c_trans));
97 }
98 
100 static void hmc58xx_send_config(struct Hmc58xx *hmc)
101 {
102  switch (hmc->init_status) {
103  case HMC_CONF_CRA:
104  hmc58xx_i2c_tx_reg(hmc, HMC58XX_REG_CFGA, (hmc->config.rate<<2)|(hmc->config.meas));
105  hmc->init_status++;
106  break;
107  case HMC_CONF_CRB:
108  hmc58xx_i2c_tx_reg(hmc, HMC58XX_REG_CFGB, (hmc->config.gain << 5));
109  hmc->init_status++;
110  break;
111  case HMC_CONF_MODE:
113  hmc->init_status++;
114  break;
115  case HMC_CONF_DONE:
116  hmc->initialized = TRUE;
118  break;
119  default:
120  break;
121  }
122 }
123 
124 // Configure
126 {
127  // wait before starting the configuration
128  // doing to early may void the mode configuration
130  hmc->init_status++;
131  if (hmc->i2c_trans.status == I2CTransSuccess || hmc->i2c_trans.status == I2CTransDone) {
132  hmc58xx_send_config(hmc);
133  }
134  }
135 }
136 
137 // Normal reading
138 void hmc58xx_read(struct Hmc58xx *hmc)
139 {
140  if (hmc->initialized && hmc->i2c_trans.status == I2CTransDone){
141  hmc->i2c_trans.buf[0] = HMC58XX_REG_DATXM;
142  hmc->i2c_trans.type = I2CTransTxRx;
143  hmc->i2c_trans.len_r = 6;
144  hmc->i2c_trans.len_w = 1;
145  i2c_submit(hmc->i2c_p, &(hmc->i2c_trans));
146  }
147 }
148 
149 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
150 
151 void hmc58xx_event(struct Hmc58xx *hmc)
152 {
153  if (hmc->initialized) {
154  if (hmc->i2c_trans.status == I2CTransFailed) {
156  }
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  hmc->data_available = TRUE;
171  }
172  }
173  else if (hmc->init_status != HMC_CONF_UNINIT) { // Configuring but not yet initialized
174  if (hmc->i2c_trans.status == I2CTransSuccess || hmc->i2c_trans.status == I2CTransDone) {
176  hmc58xx_send_config(hmc);
177  }
178  if (hmc->i2c_trans.status == I2CTransFailed) {
179  hmc->init_status--;
181  hmc58xx_send_config(hmc); // Retry config (TODO max retry)
182  }
183  }
184 }
185 
#define HMC58XX_DEFAULT_MD
Definition: hmc58xx.c:46
uint8_t mode
Measurement mode.
Definition: hmc58xx.h:43
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Submit a I2C transaction.
Definition: i2c_arch.c:335
transaction successfully finished by I2C driver
Definition: i2c.h:57
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:125
enum Hmc58xxConfStatus init_status
init status
Definition: hmc58xx.h:64
transmit and receive transaction
Definition: i2c.h:49
#define HMC58XX_STARTUP_DELAY
HMC58XX startup delay.
Definition: hmc58xx.c:58
#define HMC58XX_REG_CFGA
Definition: hmc58xx_regs.h:34
union Hmc58xx::@29 data
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:123
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
struct i2c_transaction i2c_trans
Definition: hmc58xx.h:62
#define FALSE
Definition: imu_chimu.h:141
static void hmc58xx_send_config(struct Hmc58xx *hmc)
Configuration function called once before normal use.
Definition: hmc58xx.c:100
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:138
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
static void hmc58xx_i2c_tx_reg(struct Hmc58xx *hmc, uint8_t reg, uint8_t val)
Definition: hmc58xx.c:89
#define HMC58XX_REG_DATXM
Definition: hmc58xx_regs.h:37
transaction failed
Definition: i2c.h:58
enum Hmc58xxType type
Definition: hmc58xx.h:71
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
struct Int16Vect3 vect
data vector in mag coordinate system
Definition: hmc58xx.h:67
#define TRUE
Definition: imu_chimu.h:144
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
#define HMC58XX_DEFAULT_GN
Definition: hmc58xx.c:43
uint8_t len_w
Number of bytes to write/transmit.
Definition: i2c.h:116
uint16_t len_r
Number of bytes to read/receive.
Definition: i2c.h:110
uint8_t rate
Data Output Rate Bits(6 -> 50Hz with HMC5843, 75Hz with HMC5883)
Definition: hmc58xx.h:40
unsigned char uint8_t
Definition: types.h:14
uint8_t meas
Measurement configuration.
Definition: hmc58xx.h:41
I2C peripheral structure.
Definition: i2c.h:138
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
enum I2CTransactionType type
Transaction type.
Definition: i2c.h:98
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:149
#define HMC58XX_REG_CFGB
Definition: hmc58xx_regs.h:35
struct i2c_periph * i2c_p
Definition: hmc58xx.h:61
#define HMC58XX_DEFAULT_DO
Definition: hmc58xx.c:37
bool_t initialized
config done flag
Definition: hmc58xx.h:63
uint8_t gain
Gain configuration (1 -> +- 1 Gauss)
Definition: hmc58xx.h:42
void hmc58xx_event(struct Hmc58xx *hmc)
Definition: hmc58xx.c:151
volatile bool_t data_available
data ready flag
Definition: hmc58xx.h:65
#define HMC58XX_REG_MODE
Definition: hmc58xx_regs.h:36