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
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 #ifndef IMU_MPU9250_GYRO_RANGE
71 #define IMU_MPU9250_GYRO_RANGE MPU9250_GYRO_RANGE_1000
72 #endif
73 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_RANGE)
74 
75 #ifndef IMU_MPU9250_ACCEL_RANGE
76 #define IMU_MPU9250_ACCEL_RANGE MPU9250_ACCEL_RANGE_8G
77 #endif
78 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_RANGE)
79 
80 // Default channels order
81 #ifndef IMU_MPU9250_CHAN_X
82 #define IMU_MPU9250_CHAN_X 0
83 #endif
84 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_X)
85 #ifndef IMU_MPU9250_CHAN_Y
86 #define IMU_MPU9250_CHAN_Y 1
87 #endif
88 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Y)
89 #ifndef IMU_MPU9250_CHAN_Z
90 #define IMU_MPU9250_CHAN_Z 2
91 #endif
92 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Z)
93 
94 #ifndef IMU_MPU9250_X_SIGN
95 #define IMU_MPU9250_X_SIGN 1
96 #endif
97 PRINT_CONFIG_VAR(IMU_MPU9250_X_SIGN)
98 #ifndef IMU_MPU9250_Y_SIGN
99 #define IMU_MPU9250_Y_SIGN 1
100 #endif
101 PRINT_CONFIG_VAR(IMU_MPU9250_Y_SIGN)
102 #ifndef IMU_MPU9250_Z_SIGN
103 #define IMU_MPU9250_Z_SIGN 1
104 #endif
105 PRINT_CONFIG_VAR(IMU_MPU9250_Z_SIGN)
106 
107 #ifndef IMU_MPU9250_READ_MAG
108 #define IMU_MPU9250_READ_MAG TRUE
109 #endif
110 
111 #ifndef IMU_MPU9250_MAG_STARTUP_DELAY
112 #define IMU_MPU9250_MAG_STARTUP_DELAY 1
113 #endif
114 
116 
117 void mpu_wait_slave4_ready(void);
120 
121 void imu_impl_init(void)
122 {
123  /* MPU9250 */
124  mpu9250_spi_init(&imu_mpu9250.mpu, &(IMU_MPU9250_SPI_DEV), IMU_MPU9250_SPI_SLAVE_IDX);
125  // change the default configuration
131 
132 
133  /* "internal" ak8963 magnetometer as I2C slave */
134 #if IMU_MPU9250_READ_MAG
135  /* read 15 bytes for status, accel, gyro + 7 bytes for mag slave */
138 #endif
139  /* set callback function to configure mag */
141 
142  /* Set MPU I2C master clock */
144  /* Enable I2C slave0 delayed sample rate */
146 
147 
148  /* configure spi transaction for wait_slave4 */
154 
156  imu_mpu9250.wait_slave4_trans.slave_idx = IMU_MPU9250_SPI_SLAVE_IDX;
163 
166 }
167 
168 void imu_periodic(void)
169 {
171 }
172 
173 #define Int16FromBuf(_buf,_idx) ((int16_t)(_buf[_idx] | (_buf[_idx+1] << 8)))
175 {
176  uint32_t now_ts = get_sys_time_usec();
177 
178  // If the MPU9250 SPI transaction has succeeded: convert the data
180 
182  // set channel order
183  struct Int32Vect3 accel = {
187  };
188  struct Int32Rates rates = {
192  };
193  // unscaled vector
194  VECT3_COPY(imu.accel_unscaled, accel);
195  RATES_COPY(imu.gyro_unscaled, rates);
196 
197 #if IMU_MPU9250_READ_MAG
198  if (!bit_is_set(imu_mpu9250.mpu.data_ext[6], 3)) { //mag valid just HOFL == 0
200  struct Int32Vect3 mag;
205  imu_scale_mag(&imu);
206  AbiSendMsgIMU_MAG_INT32(IMU_MPU9250_ID, now_ts, &imu.mag);
207  }
208 #endif
209 
211 
214  AbiSendMsgIMU_GYRO_INT32(IMU_MPU9250_ID, now_ts, &imu.gyro);
215  AbiSendMsgIMU_ACCEL_INT32(IMU_MPU9250_ID, now_ts, &imu.accel);
216  }
217 
218 }
219 
220 // hack with waiting to avoid creating another event loop to check the mag config status
221 static inline void mpu_set_and_wait(Mpu9250ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
222 {
223  mpu_set(mpu, _reg, _val);
224  while (imu_mpu9250.mpu.spi_trans.status != SPITransSuccess);
225 }
226 
231 {
232  // wait before starting the configuration of the mag
233  // doing to early may void the mode configuration
235  return FALSE;
236  }
237 
238  //config AK8963 soft reset
241  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_DO, 1);
242  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
243 
245 
246  // Set it to continious measuring mode 2
250  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
251 
253 
254  //Config SLV0 for continus Read
257  // Put the enable command as last.
259  (1 << 7) | // Slave 0 enable
260  (7 << 0)); // Read 7 bytes (mag x,y,z + status)
261 
262  return TRUE;
263 }
264 
266 {
267  while (!imu_mpu9250.slave4_ready) {
271  }
272  }
273 }
274 
276 {
277  if (bit_is_set(t->input_buf[1], MPU9250_I2C_SLV4_DONE)) {
279  } else {
281  }
282  t->status = SPITransDone;
283 }
enum Mpu9250MstClk i2c_mst_clk
MPU I2C master clock speed.
Definition: mpu9250.h:94
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
Mpu9250I2cSlaveConfigure configure
Definition: mpu9250.h:72
#define MPU9250_MAG_ADDR
Definition: mpu9250_regs.h:35
angular rates
union Mpu9250_I2c::@46 data_rates
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:78
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
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
#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:68
#define MPU9250_REG_I2C_SLV0_ADDR
Definition: mpu9250_regs.h:64
void imu_scale_accel(struct Imu *_imu)
Definition: ahrs_gx3.c:350
#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:79
#define IMU_MPU9250_ACCEL_LOWPASS_FILTER
uint8_t nb_bytes
number of bytes to read starting with MPU9250_REG_INT_STATUS
Definition: mpu9250.h:83
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:139
#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
uint8_t data_ext[MPU9250_BUFFER_EXT_LEN]
Definition: mpu9250_i2c.h:67
CPOL = 1.
Definition: spi.h:78
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
#define Int16FromBuf(_buf, _idx)
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
volatile bool_t slave4_ready
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:124
#define IMU_MPU9250_CHAN_X
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
void imu_impl_init(void)
must be defined by underlying hardware
#define FALSE
Definition: std.h:5
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:77
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
void mpu9250_spi_event(struct Mpu9250_Spi *mpu)
Definition: mpu9250_spi.c:100
union Mpu9250_I2c::@45 data_accel
Architecture independent SPI (Serial Peripheral Interface) API.
struct Mpu9250I2cSlave slaves[5]
I2C slaves.
Definition: mpu9250.h:93
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:52
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:50
#define TRUE
Definition: std.h:4
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:43
Definition: spi.h:84
struct ImuMpu9250 imu_mpu9250
#define IMU_MPU9250_ACCEL_RANGE
bool_t spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit a spi transaction.
Definition: spi_arch.c:48
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
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:43
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:66
struct Mpu9250_I2c mpu
#define IMU_MPU9250_ID
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:44
#define MPU9250_REG_I2C_SLV4_CTRL
Definition: mpu9250_regs.h:87
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu9250.h:92
Inertial Measurement Unit interface.
#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
void imu_mpu9250_event(void)
volatile bool_t data_available
data ready flag
Definition: mpu9250_i2c.h:58
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:80
#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
slave is selected before transaction and unselected after
Definition: spi.h:57
#define MPU9250_REG_I2C_SLV0_REG
Definition: mpu9250_regs.h:65
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
void imu_periodic(void)
optional.
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:51
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
#define IMU_MPU9250_GYRO_RANGE
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:336
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:76
#define IMU_MPU9250_X_SIGN
#define IMU_MPU9250_CHAN_Y
struct spi_transaction wait_slave4_trans
bool_t imu_mpu9250_configure_mag_slave(Mpu9250ConfigSet mpu_set, void *mpu)
function to configure akm8963 mag
#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:95
#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:42