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
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  struct Mpu60x0_I2c* mpu_i2c = (struct Mpu60x0_I2c*)(mpu);
54  mpu_i2c->i2c_trans.buf[0] = _reg;
55  mpu_i2c->i2c_trans.buf[1] = _val;
56  i2c_transmit(mpu_i2c->i2c_p, &(mpu_i2c->i2c_trans), mpu_i2c->i2c_trans.slave_addr, 2);
57 }
58 
59 // Configuration function called once before normal use
61 {
63  mpu->config.init_status++;
66  }
67  }
68 }
69 
70 void mpu60x0_i2c_read(struct Mpu60x0_I2c *mpu)
71 {
72  if (mpu->config.initialized && mpu->i2c_trans.status == I2CTransDone) {
73  /* set read bit and multiple byte bit, then address */
75  i2c_transceive(mpu->i2c_p, &(mpu->i2c_trans), mpu->i2c_trans.slave_addr, 1, mpu->config.nb_bytes);
76  }
77 }
78 
79 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
80 
81 void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
82 {
83  if (mpu->config.initialized) {
84  if (mpu->i2c_trans.status == I2CTransFailed) {
86  }
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  // if we are reading slaves through the mpu, copy the ext_sens_data
99  if ((mpu->config.i2c_bypass == FALSE) && (mpu->config.nb_slaves > 0))
100  memcpy(mpu->data_ext, (void *) &(mpu->i2c_trans.buf[15]), mpu->config.nb_bytes - 15);
101 
102  mpu->data_available = TRUE;
103  }
105  }
106  }
107  else if (mpu->config.init_status != MPU60X0_CONF_UNINIT) { // Configuring but not yet initialized
108  switch (mpu->i2c_trans.status) {
109  case I2CTransFailed:
110  mpu->config.init_status--; // Retry config (TODO max retry)
111  case I2CTransSuccess:
112  case I2CTransDone:
114  if (mpu->config.initialized)
116  break;
117  default:
118  break;
119  }
120  }
121 }
122 
125 {
126  struct Mpu60x0_I2c* mpu_i2c = (struct Mpu60x0_I2c*)(mpu);
127 
129  mpu_i2c->slave_init_status++;
130 
131  switch (mpu_i2c->slave_init_status) {
133  mpu_set(mpu, MPU60X0_REG_USER_CTRL, 0);
134  mpu_i2c->slave_init_status++;
135  break;
137  /* switch to I2C passthrough */
138  mpu_set(mpu, MPU60X0_REG_INT_PIN_CFG, (1<<1));
139  mpu_i2c->slave_init_status++;
140  break;
142  /* configure each slave. TODO: currently only one */
143  if (mpu_i2c->config.slaves[0].configure(mpu_set, mpu))
144  mpu_i2c->slave_init_status++;
145  break;
147  if (mpu_i2c->config.i2c_bypass) {
148  /* if bypassing I2C skip MPU I2C master setup */
150  }
151  else {
152  /* disable I2C passthrough again */
153  mpu_set(mpu, MPU60X0_REG_INT_PIN_CFG, (0<<1));
154  mpu_i2c->slave_init_status++;
155  }
156  break;
158  /* configure MPU I2C master clock and stop/start between slave reads */
159  mpu_set(mpu, MPU60X0_REG_I2C_MST_CTRL,
160  ((1<<4) | mpu_i2c->config.i2c_mst_clk));
161  mpu_i2c->slave_init_status++;
162  break;
164  /* Set I2C slaves delayed sample rate */
165  mpu_set(mpu, MPU60X0_REG_I2C_MST_DELAY, mpu_i2c->config.i2c_mst_delay);
166  mpu_i2c->slave_init_status++;
167  break;
169  /* I2C slave0 sample rate/2 = 100/2 = 50Hz */
170  mpu_set(mpu, MPU60X0_REG_I2C_SLV4_CTRL, 0);
171  mpu_i2c->slave_init_status++;
172  break;
174  /* enable internal I2C master */
175  mpu_set(mpu, MPU60X0_REG_USER_CTRL, (1 << MPU60X0_I2C_MST_EN));
176  mpu_i2c->slave_init_status++;
177  break;
179  return TRUE;
180  default:
181  break;
182  }
183  return FALSE;
184 }
struct i2c_periph * i2c_p
Definition: mpu60x0_i2c.h:55
enum Mpu60x0I2cSlaveInitStatus slave_init_status
Definition: mpu60x0_i2c.h:68
#define MPU60X0_REG_USER_CTRL
Definition: mpu60x0_regs.h:39
void mpu60x0_i2c_event(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:81
#define MPU60X0_I2C_MST_EN
Definition: mpu60x0_regs.h:127
void mpu60x0_i2c_init(struct Mpu60x0_I2c *mpu, struct i2c_periph *i2c_p, uint8_t addr)
Definition: mpu60x0_i2c.c:31
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:89
#define MPU60X0_REG_I2C_MST_CTRL
Definition: mpu60x0_regs.h:56
#define Int16FromBuf(_buf, _idx)
Definition: mpu60x0_i2c.c:79
bool_t i2c_transceive(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len_w, uint16_t len_r)
Definition: i2c.c:105
if(PrimarySpektrumState.SpektrumTimer)--PrimarySpektrumState.SpektrumTimer
union Mpu60x0_I2c::@23 data_accel
void mpu60x0_i2c_read(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:70
uint8_t slave_addr
Definition: i2c.h:79
#define MPU60X0_REG_INT_PIN_CFG
Definition: mpu60x0_regs.h:87
#define FALSE
Definition: imu_chimu.h:141
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:70
struct i2c_transaction i2c_trans
Definition: mpu60x0_i2c.h:56
#define MPU60X0_REG_I2C_MST_DELAY
Definition: mpu60x0_regs.h:58
uint8_t nb_bytes
number of bytes to read starting with MPU60X0_REG_INT_STATUS
Definition: mpu60x0.h:80
uint8_t data_ext[MPU60X0_BUFFER_EXT_LEN]
Definition: mpu60x0_i2c.h:66
bool_t i2c_transmit(struct i2c_periph *p, struct i2c_transaction *t, uint8_t s_addr, uint8_t len)
Definition: i2c.c:85
bool_t initialized
config done flag
Definition: mpu60x0.h:82
union Mpu60x0_I2c::@24 data_rates
enum Mpu60x0ConfStatus init_status
init status
Definition: mpu60x0.h:81
enum I2CTransactionStatus status
Definition: i2c.h:83
struct Mpu60x0I2cSlave slaves[5]
I2C slaves.
Definition: mpu60x0.h:90
volatile bool_t data_available
data ready flag
Definition: mpu60x0_i2c.h:57
struct Int16Rates rates
rates data as angular rates in gyro coordinate system
Definition: mpu60x0_i2c.h:63
#define TRUE
Definition: imu_chimu.h:144
uint8_t i2c_mst_delay
MPU I2C slaves delayed sample rate.
Definition: mpu60x0.h:92
void mpu60x0_send_config(Mpu60x0ConfigSet mpu_set, void *mpu, struct Mpu60x0Config *config)
Configuration sequence called once before normal use.
Definition: mpu60x0.c:51
volatile uint8_t buf[I2C_BUF_LEN]
Definition: i2c.h:82
#define MPU60X0_REG_INT_STATUS
Definition: mpu60x0_regs.h:89
enum Mpu60x0MstClk i2c_mst_clk
MPU I2C master clock speed.
Definition: mpu60x0.h:91
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:124
struct Mpu60x0Config config
Definition: mpu60x0_i2c.h:67
#define MPU60X0_REG_I2C_SLV4_CTRL
Definition: mpu60x0_regs.h:83
bool_t i2c_bypass
Bypass MPU I2C.
Definition: mpu60x0.h:87
unsigned char uint8_t
Definition: types.h:14
void mpu60x0_i2c_start_configure(struct Mpu60x0_I2c *mpu)
Definition: mpu60x0_i2c.c:60
Definition: i2c.h:90
static void mpu60x0_i2c_write_to_reg(void *mpu, uint8_t _reg, uint8_t _val)
Definition: mpu60x0_i2c.c:52
struct Int16Vect3 vect
accel data vector in accel coordinate system
Definition: mpu60x0_i2c.h:59
void mpu60x0_set_default_config(struct Mpu60x0Config *c)
Definition: mpu60x0.c:32
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:64