Paparazzi UAS  v5.14.0_stable-0-g3f680d1
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 }
enum Mpu9250MstClk i2c_mst_clk
MPU I2C master clock speed.
Definition: mpu9250.h:146
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
Mpu9250I2cSlaveConfigure configure
Definition: mpu9250.h:123
#define MPU9250_MAG_ADDR
Definition: mpu9250_regs.h:35
angular rates
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:129
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
uint16_t output_length
number of data words to write
Definition: spi.h:146
union Mpu9250_I2c::@318 data_rates
#define MPU9250_REG_I2C_SLV0_CTRL
Definition: mpu9250_regs.h:66
static void mpu9250_spi_periodic(struct Mpu9250_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu9250_spi.h:77
struct Mpu9250Config config
Definition: mpu9250_i2c.h:72
#define MPU9250_REG_I2C_SLV0_ADDR
Definition: mpu9250_regs.h:64
volatile bool data_available
data ready flag
Definition: mpu9250_i2c.h:62
#define AK8963_REG_CNTL2
Definition: ak8963_regs.h:54
CPHA = 1.
Definition: spi.h:69
enum Mpu9250GyroRanges gyro_range
deg/s Range
Definition: mpu9250.h:130
#define IMU_MPU9250_ACCEL_LOWPASS_FILTER
uint8_t nb_bytes
number of bytes to read starting with MPU9250_REG_INT_STATUS
Definition: mpu9250.h:134
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
#define IMU_MPU9250_Z_SIGN
Main include for ABI (AirBorneInterface).
volatile uint8_t wait_slave4_rx_buf[2]
SPI transaction structure.
Definition: spi.h:142
#define MPU9250_REG_I2C_MST_STATUS
Definition: mpu9250_regs.h:61
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:458
union Mpu9250_I2c::@317 data_accel
uint8_t data_ext[MPU9250_BUFFER_EXT_LEN]
Definition: mpu9250_i2c.h:71
void imu_mpu9250_init(void)
CPOL = 1.
Definition: spi.h:78
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
struct Imu imu
global IMU state
Definition: imu.c:108
#define Int16FromBuf(_buf, _idx)
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:129
#define IMU_MPU9250_CHAN_X
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
volatile bool slave4_ready
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:128
void mpu9250_spi_event(struct Mpu9250_Spi *mpu)
Definition: mpu9250_spi.c:109
Architecture independent SPI (Serial Peripheral Interface) API.
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
static void mpu_set_and_wait(Mpu9250ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
Definition: spi.h:84
struct ImuMpu9250 imu_mpu9250
Architecture independent timing functions.
volatile uint8_t wait_slave4_tx_buf[1]
void mpu9250_spi_init(struct Mpu9250_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu9250_spi.c:31
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
#define MPU9250_REG_I2C_SLV4_ADDR
Definition: mpu9250_regs.h:84
void mpu_wait_slave4_ready_cb(struct spi_transaction *t)
unsigned long uint32_t
Definition: types.h:18
#define AK8963_REG_CNTL1
Definition: ak8963_regs.h:53
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
void(* Mpu9250ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu9250.h:117
struct Mpu9250_I2c mpu
bool imu_mpu9250_configure_mag_slave(Mpu9250ConfigSet mpu_set, void *mpu)
function to configure akm8963 mag
void imu_mpu9250_periodic(void)
#define IMU_MPU9250_ID
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
#define MPU9250_REG_I2C_SLV4_CTRL
Definition: mpu9250_regs.h:87
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu9250.h:143
Inertial Measurement Unit interface.
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
#define IMU_MPU9250_CHAN_Z
#define IMU_MPU9250_SMPLRT_DIV
uint16_t input_length
number of data words to read
Definition: spi.h:145
signed long int32_t
Definition: types.h:19
#define IMU_MPU9250_ACCEL_RANGE
void imu_mpu9250_event(void)
void mpu_wait_slave4_ready(void)
#define IMU_MPU9250_GYRO_LOWPASS_FILTER
#define AK8963_REG_HXL
Definition: ak8963_regs.h:46
#define MPU9250_REG_I2C_SLV4_REG
Definition: mpu9250_regs.h:85
enum Mpu9250AccelRanges accel_range
g Range
Definition: mpu9250.h:131
#define IMU_MPU9250_MAG_STARTUP_DELAY
unsigned char uint8_t
Definition: types.h:14
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
#define MPU9250_I2C_SLV4_DONE
Definition: mpu9250_regs.h:135
struct Mpu9250I2cSlave slaves[MPU9250_I2C_NB_SLAVES]
I2C slaves.
Definition: mpu9250.h:145
slave is selected before transaction and unselected after
Definition: spi.h:57
#define IMU_MPU9250_GYRO_RANGE
#define MPU9250_REG_I2C_SLV0_REG
Definition: mpu9250_regs.h:65
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
#define IMU_MPU9250_Y_SIGN
Definition: spi.h:119
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:127
#define IMU_MPU9250_X_SIGN
#define IMU_MPU9250_CHAN_Y
struct spi_transaction wait_slave4_trans
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
#define MPU9250_SPI_READ
Definition: mpu9250_regs.h:37
#define MPU9250_REG_I2C_SLV4_DO
Definition: mpu9250_regs.h:86
uint8_t i2c_mst_delay
MPU I2C slaves delayed sample rate.
Definition: mpu9250.h:147
#define AK8963_CNTL1_CM_2
Definition: ak8963_regs.h:37
enum SPITransactionStatus status
Definition: spi.h:156
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38