Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
mpu60x0_i2c.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2013 Gautier Hattenberger
3  *
4  * This file is part of paparazzi.
5  *
6  * paparazzi is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2, or (at your option)
9  * any later version.
10  *
11  * paparazzi is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with paparazzi; see the file COPYING. If not, write to
18  * the Free Software Foundation, 59 Temple Place - Suite 330,
19  * Boston, MA 02111-1307, USA.
20  */
21 
30 
31 void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
32 {
33  /* set i2c_peripheral */
34  mpu->i2c_p = i2c_p;
35 
36  /* slave address */
37  mpu->i2c_trans.slave_addr = addr;
38  /* set inital status: Success or Done */
40 
41  /* set default MPU60X0 config options */
43 
44  mpu->data_available = FALSE;
45  mpu->config.initialized = FALSE;
47 
49 }
50 
51 
52 static void mpu60x0_i2c_write_to_reg(void *mpu, uint8_t _reg, uint8_t _val)
53 {
54  struct Mpu60x0_I2c *mpu_i2c = (struct Mpu60x0_I2c *)(mpu);
55  mpu_i2c->i2c_trans.buf[0] = _reg;
56  mpu_i2c->i2c_trans.buf[1] = _val;
57  i2c_transmit(mpu_i2c->i2c_p, &(mpu_i2c->i2c_trans), mpu_i2c->i2c_trans.slave_addr, 2);
58 }
59 
60 // Configuration function called once before normal use
62 {
64  mpu->config.init_status++;
67  }
68  }
69 }
70 
71 void mpu60x0_i2c_read(struct Mpu60x0_I2c *mpu)
72 {
73  if (mpu->config.initialized && mpu->i2c_trans.status == I2CTransDone) {
74  /* set read bit and multiple byte bit, then address */
76  i2c_transceive(mpu->i2c_p, &(mpu->i2c_trans), mpu->i2c_trans.slave_addr, 1, mpu->config.nb_bytes);
77  }
78 }
79 
80 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
81 
82 void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
83 {
84  if (mpu->config.initialized) {
85  if (mpu->i2c_trans.status == I2CTransFailed) {
87  } else if (mpu->i2c_trans.status == I2CTransSuccess) {
88  // Successfull reading
89  if (bit_is_set(mpu->i2c_trans.buf[0], 0)) {
90  // new data
91  mpu->data_accel.vect.x = Int16FromBuf(mpu->i2c_trans.buf, 1);
92  mpu->data_accel.vect.y = Int16FromBuf(mpu->i2c_trans.buf, 3);
93  mpu->data_accel.vect.z = Int16FromBuf(mpu->i2c_trans.buf, 5);
94  mpu->data_rates.rates.p = Int16FromBuf(mpu->i2c_trans.buf, 9);
95  mpu->data_rates.rates.q = Int16FromBuf(mpu->i2c_trans.buf, 11);
96  mpu->data_rates.rates.r = Int16FromBuf(mpu->i2c_trans.buf, 13);
97 
98  int16_t temp_raw = Int16FromBuf(mpu->i2c_trans.buf, 7);
99  mpu->temp = (float)temp_raw / 340.0f + 36.53f;
100 
101  // if we are reading slaves through the mpu, copy the ext_sens_data
102  if ((mpu->config.i2c_bypass == FALSE) && (mpu->config.nb_slaves > 0)) {
103  /* the buffer is volatile, since filled from ISR
104  * but we know it's ok to use it here so we silence the warning
105  */
106 #pragma GCC diagnostic push
107 #pragma GCC diagnostic ignored "-Wcast-qual"
108  memcpy(mpu->data_ext, (uint8_t *) & (mpu->i2c_trans.buf[15]), mpu->config.nb_bytes - 15);
109 #pragma GCC diagnostic pop
110  }
111 
112  mpu->data_available = TRUE;
113  }
115  }
116  } else if (mpu->config.init_status != MPU60X0_CONF_UNINIT) { // Configuring but not yet initialized
117  switch (mpu->i2c_trans.status) {
118  case I2CTransFailed:
119  mpu->config.init_status--; // Retry config (TODO max retry)
120  case I2CTransSuccess:
121  case I2CTransDone:
122  mpu60x0_send_config(mpu60x0_i2c_write_to_reg, (void *)mpu, &(mpu->config));
123  if (mpu->config.initialized) {
125  }
126  break;
127  default:
128  break;
129  }
130  }
131 }
132 
135 {
136  struct Mpu60x0_I2c *mpu_i2c = (struct Mpu60x0_I2c *)(mpu);
137 
138  if (mpu_i2c->slave_init_status == MPU60X0_I2C_CONF_UNINIT) {
139  mpu_i2c->slave_init_status++;
140  }
141 
142  switch (mpu_i2c->slave_init_status) {
144  mpu_set(mpu, MPU60X0_REG_USER_CTRL, 0);
145  mpu_i2c->slave_init_status++;
146  break;
148  /* switch to I2C passthrough */
149  mpu_set(mpu, MPU60X0_REG_INT_PIN_CFG, (1 << 1));
150  mpu_i2c->slave_init_status++;
151  break;
153  /* configure each slave. TODO: currently only one */
154  if (mpu_i2c->config.slaves[0].configure(mpu_set, mpu)) {
155  mpu_i2c->slave_init_status++;
156  }
157  break;
159  if (mpu_i2c->config.i2c_bypass) {
160  /* if bypassing I2C skip MPU I2C master setup */
162  } else {
163  /* disable I2C passthrough again */
164  mpu_set(mpu, MPU60X0_REG_INT_PIN_CFG, (0 << 1));
165  mpu_i2c->slave_init_status++;
166  }
167  break;
169  /* configure MPU I2C master clock and stop/start between slave reads */
170  mpu_set(mpu, MPU60X0_REG_I2C_MST_CTRL,
171  ((1 << 4) | mpu_i2c->config.i2c_mst_clk));
172  mpu_i2c->slave_init_status++;
173  break;
175  /* Set I2C slaves delayed sample rate */
176  mpu_set(mpu, MPU60X0_REG_I2C_MST_DELAY, mpu_i2c->config.i2c_mst_delay);
177  mpu_i2c->slave_init_status++;
178  break;
180  /* I2C slave0 sample rate/2 = 100/2 = 50Hz */
181  mpu_set(mpu, MPU60X0_REG_I2C_SLV4_CTRL, 0);
182  mpu_i2c->slave_init_status++;
183  break;
185  /* enable internal I2C master */
186  mpu_set(mpu, MPU60X0_REG_USER_CTRL, (1 << MPU60X0_I2C_MST_EN));
187  mpu_i2c->slave_init_status++;
188  break;
190  return TRUE;
191  default:
192  break;
193  }
194  return FALSE;
195 }
uint8_t data_ext[MPU60X0_BUFFER_EXT_LEN]
Definition: mpu60x0_i2c.h:67
#define MPU60X0_REG_USER_CTRL
Definition: mpu60x0_regs.h:39
volatile uint8_t buf[I2C_BUF_LEN]
Transaction buffer With I2C_BUF_LEN number of bytes.
Definition: i2c.h:122
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:82
#define MPU60X0_I2C_MST_EN
Definition: mpu60x0_regs.h:127
transaction successfully finished by I2C driver
Definition: i2c.h:57
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:68
union Mpu60x0_I2c::@41 data_accel
#define MPU60X0_REG_I2C_MST_CTRL
Definition: mpu60x0_regs.h:56
#define Int16FromBuf(_buf, _idx)
Definition: mpu60x0_i2c.c:80
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Submit a write/read transaction.
Definition: i2c.c:278
union Mpu60x0_I2c::@42 data_rates
void mpu60x0_i2c_read(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:71
#define MPU60X0_REG_INT_PIN_CFG
Definition: mpu60x0_regs.h:87
#define FALSE
Definition: std.h:5
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:70
struct i2c_transaction i2c_trans
Definition: mpu60x0_i2c.h:56
float temp
temperature in degrees Celcius
Definition: mpu60x0_i2c.h:66
enum Mpu60x0ConfStatus init_status
init status
Definition: mpu60x0.h:81
enum Mpu60x0MstClk i2c_mst_clk
MPU I2C master clock speed.
Definition: mpu60x0.h:91
#define TRUE
Definition: std.h:4
transaction set to done by user level
Definition: i2c.h:59
#define MPU60X0_REG_I2C_MST_DELAY
Definition: mpu60x0_regs.h:58
bool_t i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Submit a write only transaction.
Definition: i2c.c:258
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:89
signed short int16_t
Definition: types.h:17
bool_t initialized
config done flag
Definition: mpu60x0.h:82
transaction failed
Definition: i2c.h:58
struct Mpu60x0I2cSlave slaves[5]
I2C slaves.
Definition: mpu60x0.h:90
enum I2CTransactionStatus status
Transaction status.
Definition: i2c.h:126
bool_t i2c_bypass
Bypass MPU I2C.
Definition: mpu60x0.h:87
void mpu60x0_send_config(Mpu60x0ConfigSet mpu_set, void *mpu, struct Mpu60x0Config *config)
Configuration sequence called once before normal use.
Definition: mpu60x0.c:51
#define MPU60X0_REG_INT_STATUS
Definition: mpu60x0_regs.h:89
uint8_t slave_addr
Slave address.
Definition: i2c.h:104
Driver for the MPU-60X0 using I2C.
bool_t mpu60x0_configure_i2c_slaves(Mpu60x0ConfigSet mpu_set, void *mpu)
Configure I2C slaves of the MPU.
Definition: mpu60x0_i2c.c:134
I2C peripheral structure.
Definition: i2c.h:138
#define MPU60X0_REG_I2C_SLV4_CTRL
Definition: mpu60x0_regs.h:83
unsigned char uint8_t
Definition: types.h:14
void mpu60x0_i2c_start_configure(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:61
static void mpu60x0_i2c_write_to_reg(void *mpu, uint8_t _reg, uint8_t _val)
Definition: mpu60x0_i2c.c:52
uint8_t i2c_mst_delay
MPU I2C slaves delayed sample rate.
Definition: mpu60x0.h:92
enum Mpu60x0I2cSlaveInitStatus slave_init_status
Definition: mpu60x0_i2c.h:69
void mpu60x0_set_default_config(struct Mpu60x0Config *c)
Definition: mpu60x0.c:32
struct i2c_periph * i2c_p
Definition: mpu60x0_i2c.h:55
uint8_t nb_bytes
number of bytes to read starting with MPU60X0_REG_INT_STATUS
Definition: mpu60x0.h:80
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:64
volatile bool_t data_available
data ready flag
Definition: mpu60x0_i2c.h:57