Paparazzi UAS  v5.0.5_stable-7-g4b8bbb7
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 "std.h"
32 
33 
34 /* HMC58XX default conf */
35 #ifndef HMC58XX_DEFAULT_DO
36 #define HMC58XX_DEFAULT_DO 0x6 // Data Output Rate (6 -> 50Hz with HMC5843, 75Hz with HMC5883)
37 #endif
38 #ifndef HMC58XX_DEFAULT_MS
39 #define HMC58XX_DEFAULT_MS 0x0 // Measurement configuration
40 #endif
41 #ifndef HMC58XX_DEFAULT_GN
42 #define HMC58XX_DEFAULT_GN 0x1 // Gain configuration (1 -> +- 1 Gauss)
43 #endif
44 #ifndef HMC58XX_DEFAULT_MD
45 #define HMC58XX_DEFAULT_MD 0x0 // Continious measurement mode
46 #endif
47 
49 {
54 }
55 
62 void hmc58xx_init(struct Hmc58xx *hmc, struct i2c_periph *i2c_p, uint8_t addr)
63 {
64  /* set i2c_peripheral */
65  hmc->i2c_p = i2c_p;
66  /* set i2c address */
67  hmc->i2c_trans.slave_addr = addr;
69  /* set default config options */
71  hmc->type = HMC_TYPE_5883;
72  hmc->initialized = FALSE;
74 }
75 
76 static void hmc58xx_i2c_tx_reg(struct Hmc58xx *hmc, uint8_t reg, uint8_t val)
77 {
78  hmc->i2c_trans.type = I2CTransTx;
79  hmc->i2c_trans.buf[0] = reg;
80  hmc->i2c_trans.buf[1] = val;
81  hmc->i2c_trans.len_r = 0;
82  hmc->i2c_trans.len_w = 2;
83  i2c_submit(hmc->i2c_p, &(hmc->i2c_trans));
84 }
85 
87 static void hmc58xx_send_config(struct Hmc58xx *hmc)
88 {
89  switch (hmc->init_status) {
90  case HMC_CONF_CRA:
91  hmc58xx_i2c_tx_reg(hmc, HMC58XX_REG_CFGA, (hmc->config.rate<<2)|(hmc->config.meas));
92  hmc->init_status++;
93  break;
94  case HMC_CONF_CRB:
96  hmc->init_status++;
97  break;
98  case HMC_CONF_MODE:
100  hmc->init_status++;
101  break;
102  case HMC_CONF_DONE:
103  hmc->initialized = TRUE;
105  break;
106  default:
107  break;
108  }
109 }
110 
111 // Configure
113 {
114  if (hmc->init_status == HMC_CONF_UNINIT) {
115  hmc->init_status++;
116  if (hmc->i2c_trans.status == I2CTransSuccess || hmc->i2c_trans.status == I2CTransDone) {
117  hmc58xx_send_config(hmc);
118  }
119  }
120 }
121 
122 // Normal reading
123 void hmc58xx_read(struct Hmc58xx *hmc)
124 {
125  if (hmc->initialized && hmc->i2c_trans.status == I2CTransDone){
126  hmc->i2c_trans.buf[0] = HMC58XX_REG_DATXM;
127  hmc->i2c_trans.type = I2CTransTxRx;
128  hmc->i2c_trans.len_r = 6;
129  hmc->i2c_trans.len_w = 1;
130  i2c_submit(hmc->i2c_p, &(hmc->i2c_trans));
131  }
132 }
133 
134 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
135 
136 void hmc58xx_event(struct Hmc58xx *hmc)
137 {
138  if (hmc->initialized) {
139  if (hmc->i2c_trans.status == I2CTransFailed) {
141  }
142  else if (hmc->i2c_trans.status == I2CTransSuccess) {
143  if (hmc->type == HMC_TYPE_5843) {
144  hmc->data.vect.x = Int16FromBuf(hmc->i2c_trans.buf,0);
145  hmc->data.vect.y = Int16FromBuf(hmc->i2c_trans.buf,2);
146  hmc->data.vect.z = Int16FromBuf(hmc->i2c_trans.buf,4);
147  }
148  /* HMC5883 has xzy order of axes in returned data */
149  else {
150  hmc->data.vect.x = Int16FromBuf(hmc->i2c_trans.buf,0);
151  hmc->data.vect.y = Int16FromBuf(hmc->i2c_trans.buf,4);
152  hmc->data.vect.z = Int16FromBuf(hmc->i2c_trans.buf,2);
153  }
154  hmc->data_available = TRUE;
156  }
157  }
158  else if (hmc->init_status != HMC_CONF_UNINIT) { // Configuring but not yet initialized
159  if (hmc->i2c_trans.status == I2CTransSuccess || hmc->i2c_trans.status == I2CTransDone) {
161  hmc58xx_send_config(hmc);
162  }
163  if (hmc->i2c_trans.status == I2CTransFailed) {
164  hmc->init_status--;
166  hmc58xx_send_config(hmc); // Retry config (TODO max retry)
167  }
168  }
169 }
170 
#define HMC58XX_DEFAULT_MD
Definition: hmc58xx.c:45
uint8_t mode
Measurement mode.
Definition: hmc58xx.h:43
bool_t i2c_submit(struct i2c_periph *p, struct i2c_transaction *t)
Definition: i2c_arch.c:333
union Hmc58xx::@20 data
void hmc58xx_start_configure(struct Hmc58xx *hmc)
Definition: hmc58xx.c:112
enum Hmc58xxConfStatus init_status
init status
Definition: hmc58xx.h:64
#define HMC58XX_REG_CFGA
Definition: hmc58xx_regs.h:34
uint8_t slave_addr
Definition: i2c.h:79
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:87
void hmc58xx_read(struct Hmc58xx *hmc)
Definition: hmc58xx.c:123
uint16_t val[TCOUPLE_NB]
#define HMC58XX_DEFAULT_MS
Definition: hmc58xx.c:39
static void hmc58xx_i2c_tx_reg(struct Hmc58xx *hmc, uint8_t reg, uint8_t val)
Definition: hmc58xx.c:76
#define HMC58XX_REG_DATXM
Definition: hmc58xx_regs.h:37
enum Hmc58xxType type
Definition: hmc58xx.h:71
enum I2CTransactionStatus status
Definition: i2c.h:83
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]
Definition: i2c.h:82
#define HMC58XX_DEFAULT_GN
Definition: hmc58xx.c:42
uint8_t len_w
Definition: i2c.h:81
uint16_t len_r
Definition: i2c.h:80
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
Definition: i2c.h:90
struct Hmc58xxConfig config
Definition: hmc58xx.h:70
static void hmc58xx_set_default_config(struct Hmc58xxConfig *c)
Definition: hmc58xx.c:48
Definition: i2c.h:45
enum I2CTransactionType type
Definition: i2c.h:78
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:62
#define Int16FromBuf(_buf, _idx)
Definition: hmc58xx.c:134
#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:36
static struct point c
Definition: discsurvey.c:39
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:136
volatile bool_t data_available
data ready flag
Definition: hmc58xx.h:65
#define HMC58XX_REG_MODE
Definition: hmc58xx_regs.h:36