Paparazzi UAS  v5.18.0_stable
Paparazzi is a free software Unmanned Aircraft System.
imu_mpu9250_spi.c
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2014 Gautier Hattenberger <gautier.hattenberger@enac.fr>
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, see
18  * <http://www.gnu.org/licenses/>.
19  */
20 
28 #include "subsystems/imu.h"
29 #include "subsystems/abi.h"
30 #include "mcu_periph/sys_time.h"
31 #include "mcu_periph/spi.h"
33 
34 /* SPI defaults set in subsystem makefile, can be configured from airframe file */
35 PRINT_CONFIG_VAR(IMU_MPU9250_SPI_SLAVE_IDX)
36 PRINT_CONFIG_VAR(IMU_MPU9250_SPI_DEV)
37 
38 
39 #if !defined IMU_MPU9250_GYRO_LOWPASS_FILTER && !defined IMU_MPU9250_ACCEL_LOWPASS_FILTER && !defined IMU_MPU9250_SMPLRT_DIV
40 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
41 /* Accelerometer: Bandwidth 41Hz, Delay 5.9ms
42  * Gyroscope: Bandwidth 41Hz, Delay 5.9ms sampling 1kHz
43  * Output rate: 100Hz
44  */
45 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
46 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
47 #define IMU_MPU9250_SMPLRT_DIV 9
48 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
49 #elif PERIODIC_FREQUENCY == 512
50 /* Accelerometer: Bandwidth 184Hz, Delay 5.8ms
51  * Gyroscope: Bandwidth 250Hz, Delay 0.97ms sampling 8kHz
52  * Output rate: 2kHz
53  */
54 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_250HZ
55 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_184HZ
56 #define IMU_MPU9250_SMPLRT_DIV 3
57 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
58 #else
59 /* By default, don't go too fast */
60 #define IMU_MPU9250_SMPLRT_DIV 9
61 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
62 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
63 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
64 #endif
65 #endif
66 PRINT_CONFIG_VAR(IMU_MPU9250_SMPLRT_DIV)
67 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_LOWPASS_FILTER)
68 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_LOWPASS_FILTER)
69 
70 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_RANGE)
71 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_RANGE)
72 
73 // Default channels order
74 #ifndef IMU_MPU9250_CHAN_X
75 #define IMU_MPU9250_CHAN_X 0
76 #endif
77 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_X)
78 #ifndef IMU_MPU9250_CHAN_Y
79 #define IMU_MPU9250_CHAN_Y 1
80 #endif
81 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Y)
82 #ifndef IMU_MPU9250_CHAN_Z
83 #define IMU_MPU9250_CHAN_Z 2
84 #endif
85 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Z)
86 
87 #ifndef IMU_MPU9250_X_SIGN
88 #define IMU_MPU9250_X_SIGN 1
89 #endif
90 PRINT_CONFIG_VAR(IMU_MPU9250_X_SIGN)
91 #ifndef IMU_MPU9250_Y_SIGN
92 #define IMU_MPU9250_Y_SIGN 1
93 #endif
94 PRINT_CONFIG_VAR(IMU_MPU9250_Y_SIGN)
95 #ifndef IMU_MPU9250_Z_SIGN
96 #define IMU_MPU9250_Z_SIGN 1
97 #endif
98 PRINT_CONFIG_VAR(IMU_MPU9250_Z_SIGN)
99 
100 #ifndef IMU_MPU9250_READ_MAG
101 #define IMU_MPU9250_READ_MAG TRUE
102 #endif
103 
104 #ifndef IMU_MPU9250_MAG_STARTUP_DELAY
105 #define IMU_MPU9250_MAG_STARTUP_DELAY 1
106 #endif
107 
109 
110 void mpu_wait_slave4_ready(void);
113 
115 {
116  /* MPU9250 */
117  mpu9250_spi_init(&imu_mpu9250.mpu, &(IMU_MPU9250_SPI_DEV), IMU_MPU9250_SPI_SLAVE_IDX);
118  // change the default configuration
124 
125 
126  /* "internal" ak8963 magnetometer as I2C slave */
127 #if IMU_MPU9250_READ_MAG
128  /* read 15 bytes for status, accel, gyro + 7 bytes for mag slave */
131 #endif
132  /* set callback function to configure mag */
134 
135  /* Set MPU I2C master clock */
137  /* Enable I2C slave0 delayed sample rate */
139 
140 
141  /* configure spi transaction for wait_slave4 */
147 
149  imu_mpu9250.wait_slave4_trans.slave_idx = IMU_MPU9250_SPI_SLAVE_IDX;
156 
158  imu_mpu9250.slave4_ready = false;
159 }
160 
162 {
164 }
165 
166 #define Int16FromBuf(_buf,_idx) ((int16_t)(_buf[_idx] | (_buf[_idx+1] << 8)))
168 {
169  uint32_t now_ts = get_sys_time_usec();
170 
171  // If the MPU9250 SPI transaction has succeeded: convert the data
173 
175  // set channel order
176  struct Int32Vect3 accel = {
180  };
181  struct Int32Rates rates = {
185  };
186  // unscaled vector
187  VECT3_COPY(imu.accel_unscaled, accel);
188  RATES_COPY(imu.gyro_unscaled, rates);
189 
190 #if IMU_MPU9250_READ_MAG
191  if (!bit_is_set(imu_mpu9250.mpu.data_ext[6], 3)) { //mag valid just HOFL == 0
193  struct Int32Vect3 mag;
198  imu_scale_mag(&imu);
199  AbiSendMsgIMU_MAG_INT32(IMU_MPU9250_ID, now_ts, &imu.mag);
200  }
201 #endif
202 
204 
207  AbiSendMsgIMU_GYRO_INT32(IMU_MPU9250_ID, now_ts, &imu.gyro);
208  AbiSendMsgIMU_ACCEL_INT32(IMU_MPU9250_ID, now_ts, &imu.accel);
209  }
210 
211 }
212 
213 // hack with waiting to avoid creating another event loop to check the mag config status
214 static inline void mpu_set_and_wait(Mpu9250ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
215 {
216  mpu_set(mpu, _reg, _val);
217  while (imu_mpu9250.mpu.spi_trans.status != SPITransSuccess);
218 }
219 
224 {
225  // wait before starting the configuration of the mag
226  // doing to early may void the mode configuration
228  return false;
229  }
230 
231  //config AK8963 soft reset
234  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_DO, 1);
235  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
236 
238 
239  // Set it to continious measuring mode 2
243  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
244 
246 
247  //Config SLV0 for continus Read
250  // Put the enable command as last.
252  (1 << 7) | // Slave 0 enable
253  (7 << 0)); // Read 7 bytes (mag x,y,z + status)
254 
255  return true;
256 }
257 
259 {
260  while (!imu_mpu9250.slave4_ready) {
264  }
265  }
266 }
267 
269 {
270  if (bit_is_set(t->input_buf[1], MPU9250_I2C_SLV4_DONE)) {
271  imu_mpu9250.slave4_ready = true;
272  } else {
273  imu_mpu9250.slave4_ready = false;
274  }
275  t->status = SPITransDone;
276 }
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
ImuMpu9250
Definition: imu_mpu9250_i2c.h:73
imu_mpu9250_event
void imu_mpu9250_event(void)
Definition: imu_mpu9250_spi.c:167
spi_transaction::cdiv
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
mpu_wait_slave4_ready_cb
void mpu_wait_slave4_ready_cb(struct spi_transaction *t)
Definition: imu_mpu9250_spi.c:268
MPU9250_REG_I2C_SLV0_CTRL
#define MPU9250_REG_I2C_SLV0_CTRL
Definition: mpu9250_regs.h:66
mpu9250_spi_periodic
static void mpu9250_spi_periodic(struct Mpu9250_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu9250_spi.h:77
imu_mpu9250_init
void imu_mpu9250_init(void)
Definition: imu_mpu9250_spi.c:114
Mpu9250Config::i2c_mst_clk
enum Mpu9250MstClk i2c_mst_clk
MPU I2C master clock speed.
Definition: mpu9250.h:146
Mpu9250I2cSlave::configure
Mpu9250I2cSlaveConfigure configure
Definition: mpu9250.h:123
mpu_wait_slave4_ready
void mpu_wait_slave4_ready(void)
Definition: imu_mpu9250_spi.c:258
Int16FromBuf
#define Int16FromBuf(_buf, _idx)
Definition: imu_mpu9250_spi.c:166
spi_transaction::cpol
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
MPU9250_REG_I2C_MST_STATUS
#define MPU9250_REG_I2C_MST_STATUS
Definition: mpu9250_regs.h:61
MPU9250_REG_I2C_SLV4_DO
#define MPU9250_REG_I2C_SLV4_DO
Definition: mpu9250_regs.h:86
Mpu9250Config::gyro_range
enum Mpu9250GyroRanges gyro_range
deg/s Range
Definition: mpu9250.h:130
mpu9250_spi_event
void mpu9250_spi_event(struct Mpu9250_Spi *mpu)
Definition: mpu9250_spi.c:109
ImuMpu9250::mpu
struct Mpu9250_I2c mpu
Definition: imu_mpu9250_i2c.h:74
imu_mpu9250_configure_mag_slave
bool imu_mpu9250_configure_mag_slave(Mpu9250ConfigSet mpu_set, void *mpu)
function to configure akm8963 mag
Definition: imu_mpu9250_spi.c:223
AK8963_REG_CNTL1
#define AK8963_REG_CNTL1
Definition: ak8963_regs.h:53
Int32Rates
angular rates
Definition: pprz_algebra_int.h:179
Mpu9250Config::nb_bytes
uint8_t nb_bytes
number of bytes to read starting with MPU9250_REG_INT_STATUS
Definition: mpu9250.h:134
abi.h
Imu::accel
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
spi_transaction::output_length
uint16_t output_length
number of data words to write
Definition: spi.h:152
IMU_MPU9250_Z_SIGN
#define IMU_MPU9250_Z_SIGN
Definition: imu_mpu9250_spi.c:96
get_sys_time_float
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:129
spi.h
IMU_MPU9250_MAG_STARTUP_DELAY
#define IMU_MPU9250_MAG_STARTUP_DELAY
Definition: imu_mpu9250_spi.c:105
spi_transaction
SPI transaction structure.
Definition: spi.h:148
Mpu9250_I2c::data_rates
union Mpu9250_I2c::@336 data_rates
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
Mpu9250Config::dlpf_accel_cfg
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:128
ak8963_regs.h
uint32_t
unsigned long uint32_t
Definition: types.h:18
Mpu9250Config::dlpf_gyro_cfg
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:129
mpu9250_spi_init
void mpu9250_spi_init(struct Mpu9250_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu9250_spi.c:31
SPISelectUnselect
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
SPITransSuccess
@ SPITransSuccess
Definition: spi.h:99
spi_transaction::bitorder
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
MPU9250_REG_I2C_SLV4_ADDR
#define MPU9250_REG_I2C_SLV4_ADDR
Definition: mpu9250_regs.h:84
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
spi_transaction::output_buf
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
IMU_MPU9250_CHAN_X
#define IMU_MPU9250_CHAN_X
Definition: imu_mpu9250_spi.c:75
ImuMpu9250::wait_slave4_rx_buf
volatile uint8_t wait_slave4_rx_buf[2]
Definition: imu_mpu9250_spi.h:78
spi_transaction::select
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
imu_mpu9250
struct ImuMpu9250 imu_mpu9250
Definition: imu_mpu9250_spi.c:108
Mpu9250_I2c::config
struct Mpu9250Config config
Definition: mpu9250_i2c.h:72
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
IMU_MPU9250_GYRO_LOWPASS_FILTER
#define IMU_MPU9250_GYRO_LOWPASS_FILTER
Definition: imu_mpu9250_spi.c:61
IMU_MPU9250_CHAN_Z
#define IMU_MPU9250_CHAN_Z
Definition: imu_mpu9250_spi.c:83
imu.h
MPU9250_MST_CLK_400KHZ
@ MPU9250_MST_CLK_400KHZ
Definition: mpu9250_regs.h:187
SPIMSBFirst
@ SPIMSBFirst
Definition: spi.h:112
imu_mpu9250_periodic
void imu_mpu9250_periodic(void)
Definition: imu_mpu9250_spi.c:161
Mpu9250_I2c::data_ext
uint8_t data_ext[MPU9250_BUFFER_EXT_LEN]
Definition: mpu9250_i2c.h:71
spi_transaction::cpha
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
spi_transaction::after_cb
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
spi_submit
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:511
IMU_MPU9250_X_SIGN
#define IMU_MPU9250_X_SIGN
Definition: imu_mpu9250_spi.c:88
AK8963_REG_CNTL2
#define AK8963_REG_CNTL2
Definition: ak8963_regs.h:54
AK8963_REG_HXL
#define AK8963_REG_HXL
Definition: ak8963_regs.h:46
Int32Vect3
Definition: pprz_algebra_int.h:88
sys_time.h
Architecture independent timing functions.
SPICpolIdleHigh
@ SPICpolIdleHigh
CPOL = 1.
Definition: spi.h:84
imu_scale_gyro
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
uint8_t
unsigned char uint8_t
Definition: types.h:14
MPU9250_REG_I2C_SLV4_REG
#define MPU9250_REG_I2C_SLV4_REG
Definition: mpu9250_regs.h:85
Mpu9250Config::accel_range
enum Mpu9250AccelRanges accel_range
g Range
Definition: mpu9250.h:131
Int32Vect3::y
int32_t y
Definition: pprz_algebra_int.h:90
MPU9250_REG_I2C_SLV4_CTRL
#define MPU9250_REG_I2C_SLV4_CTRL
Definition: mpu9250_regs.h:87
IMU_MPU9250_Y_SIGN
#define IMU_MPU9250_Y_SIGN
Definition: imu_mpu9250_spi.c:92
AK8963_CNTL1_CM_2
#define AK8963_CNTL1_CM_2
Definition: ak8963_regs.h:37
IMU_MPU9250_ID
#define IMU_MPU9250_ID
Definition: abi_sender_ids.h:315
SPIDss8bit
@ SPIDss8bit
Definition: spi.h:90
mpu_set_and_wait
static void mpu_set_and_wait(Mpu9250ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
Definition: imu_mpu9250_spi.c:214
imu
struct Imu imu
global IMU state
Definition: imu.c:108
SPIDiv64
@ SPIDiv64
Definition: spi.h:125
Mpu9250_I2c::data_accel
union Mpu9250_I2c::@335 data_accel
PRINT_CONFIG_MSG
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
Mpu9250Config::nb_slaves
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu9250.h:143
IMU_MPU9250_SMPLRT_DIV
#define IMU_MPU9250_SMPLRT_DIV
Definition: imu_mpu9250_spi.c:60
MPU9250_I2C_SLV4_DONE
#define MPU9250_I2C_SLV4_DONE
Definition: mpu9250_regs.h:135
IMU_MPU9250_CHAN_Y
#define IMU_MPU9250_CHAN_Y
Definition: imu_mpu9250_spi.c:79
IMU_MPU9250_ACCEL_RANGE
#define IMU_MPU9250_ACCEL_RANGE
Definition: imu_mpu9250_i2c.h:43
spi_transaction::input_length
uint16_t input_length
number of data words to read
Definition: spi.h:151
Mpu9250Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:127
spi_transaction::dss
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
spi_transaction::slave_idx
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
int32_t
signed long int32_t
Definition: types.h:19
SPITransDone
@ SPITransDone
Definition: spi.h:101
imu_scale_mag
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:352
ImuMpu9250::slave4_ready
volatile bool slave4_ready
Definition: imu_mpu9250_spi.h:79
spi_transaction::input_buf
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
MPU9250_SPI_READ
#define MPU9250_SPI_READ
Definition: mpu9250_regs.h:37
Mpu9250ConfigSet
void(* Mpu9250ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu9250.h:117
SPICphaEdge2
@ SPICphaEdge2
CPHA = 1.
Definition: spi.h:75
Int32Vect3::x
int32_t x
Definition: pprz_algebra_int.h:89
MPU9250_REG_I2C_SLV0_REG
#define MPU9250_REG_I2C_SLV0_REG
Definition: mpu9250_regs.h:65
Imu::mag
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Imu::mag_unscaled
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
Mpu9250_I2c::data_available
volatile bool data_available
data ready flag
Definition: mpu9250_i2c.h:62
spi_transaction::before_cb
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
Mpu9250Config::slaves
struct Mpu9250I2cSlave slaves[MPU9250_I2C_NB_SLAVES]
I2C slaves.
Definition: mpu9250.h:145
RATES_COPY
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
ImuMpu9250::wait_slave4_tx_buf
volatile uint8_t wait_slave4_tx_buf[1]
Definition: imu_mpu9250_spi.h:77
IMU_MPU9250_ACCEL_LOWPASS_FILTER
#define IMU_MPU9250_ACCEL_LOWPASS_FILTER
Definition: imu_mpu9250_spi.c:62
spi_transaction::status
enum SPITransactionStatus status
Definition: spi.h:162
imu_scale_accel
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
VECT3_COPY
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
MPU9250_MAG_ADDR
#define MPU9250_MAG_ADDR
Definition: mpu9250_regs.h:35
IMU_MPU9250_GYRO_RANGE
#define IMU_MPU9250_GYRO_RANGE
Definition: imu_mpu9250_i2c.h:39
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
ImuMpu9250::wait_slave4_trans
struct spi_transaction wait_slave4_trans
Definition: imu_mpu9250_spi.h:76
MPU9250_REG_I2C_SLV0_ADDR
#define MPU9250_REG_I2C_SLV0_ADDR
Definition: mpu9250_regs.h:64
Mpu9250Config::i2c_mst_delay
uint8_t i2c_mst_delay
MPU I2C slaves delayed sample rate.
Definition: mpu9250.h:147