Paparazzi UAS  v6.2.0_stable
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 
29 #include "modules/imu/imu.h"
30 #include "modules/core/abi.h"
31 #include "mcu_periph/sys_time.h"
32 #include "mcu_periph/spi.h"
34 
35 /* SPI defaults set in subsystem makefile, can be configured from airframe file */
36 PRINT_CONFIG_VAR(IMU_MPU9250_SPI_SLAVE_IDX)
37 PRINT_CONFIG_VAR(IMU_MPU9250_SPI_DEV)
38 
39 
40 #if !defined IMU_MPU9250_GYRO_LOWPASS_FILTER && !defined IMU_MPU9250_ACCEL_LOWPASS_FILTER && !defined IMU_MPU9250_SMPLRT_DIV
41 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
42 /* Accelerometer: Bandwidth 41Hz, Delay 5.9ms
43  * Gyroscope: Bandwidth 41Hz, Delay 5.9ms sampling 1kHz
44  * Output rate: 100Hz
45  */
46 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
47 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
48 #define IMU_MPU9250_SMPLRT_DIV 9
49 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
50 #elif PERIODIC_FREQUENCY == 512
51 /* Accelerometer: Bandwidth 184Hz, Delay 5.8ms
52  * Gyroscope: Bandwidth 250Hz, Delay 0.97ms sampling 8kHz
53  * Output rate: 2kHz
54  */
55 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_250HZ
56 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_184HZ
57 #define IMU_MPU9250_SMPLRT_DIV 3
58 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
59 #else
60 /* By default, don't go too fast */
61 #define IMU_MPU9250_SMPLRT_DIV 9
62 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
63 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
64 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
65 #endif
66 #endif
67 PRINT_CONFIG_VAR(IMU_MPU9250_SMPLRT_DIV)
68 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_LOWPASS_FILTER)
69 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_LOWPASS_FILTER)
70 
71 PRINT_CONFIG_VAR(IMU_MPU9250_GYRO_RANGE)
72 PRINT_CONFIG_VAR(IMU_MPU9250_ACCEL_RANGE)
73 
74 // Default channels order
75 #ifndef IMU_MPU9250_CHAN_X
76 #define IMU_MPU9250_CHAN_X 0
77 #endif
78 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_X)
79 #ifndef IMU_MPU9250_CHAN_Y
80 #define IMU_MPU9250_CHAN_Y 1
81 #endif
82 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Y)
83 #ifndef IMU_MPU9250_CHAN_Z
84 #define IMU_MPU9250_CHAN_Z 2
85 #endif
86 PRINT_CONFIG_VAR(IMU_MPU9250_CHAN_Z)
87 
88 #ifndef IMU_MPU9250_X_SIGN
89 #define IMU_MPU9250_X_SIGN 1
90 #endif
91 PRINT_CONFIG_VAR(IMU_MPU9250_X_SIGN)
92 #ifndef IMU_MPU9250_Y_SIGN
93 #define IMU_MPU9250_Y_SIGN 1
94 #endif
95 PRINT_CONFIG_VAR(IMU_MPU9250_Y_SIGN)
96 #ifndef IMU_MPU9250_Z_SIGN
97 #define IMU_MPU9250_Z_SIGN 1
98 #endif
99 PRINT_CONFIG_VAR(IMU_MPU9250_Z_SIGN)
100 
101 #ifndef IMU_MPU9250_READ_MAG
102 #define IMU_MPU9250_READ_MAG TRUE
103 #endif
104 
105 #ifndef IMU_MPU9250_MAG_STARTUP_DELAY
106 #define IMU_MPU9250_MAG_STARTUP_DELAY 1
107 #endif
108 
109 struct ImuMpu9250 imu_mpu9250;
110 
111 void mpu_wait_slave4_ready(void);
114 
116 {
117  /* MPU9250 */
118  mpu9250_spi_init(&imu_mpu9250.mpu, &(IMU_MPU9250_SPI_DEV), IMU_MPU9250_SPI_SLAVE_IDX);
119  // change the default configuration
125 
126  // Set the default scaling
129 
130  /* "internal" ak8963 magnetometer as I2C slave */
131 #if IMU_MPU9250_READ_MAG
132  /* read 15 bytes for status, accel, gyro + 7 bytes for mag slave */
135 #endif
136  /* set callback function to configure mag */
138 
139  /* Set MPU I2C master clock */
141  /* Enable I2C slave0 delayed sample rate */
143 
144 
145  /* configure spi transaction for wait_slave4 */
151 
153  imu_mpu9250.wait_slave4_trans.slave_idx = IMU_MPU9250_SPI_SLAVE_IDX;
160 
162  imu_mpu9250.slave4_ready = false;
163 }
164 
166 {
168 }
169 
170 #define Int16FromBuf(_buf,_idx) ((int16_t)(_buf[_idx] | (_buf[_idx+1] << 8)))
172 {
173  uint32_t now_ts = get_sys_time_usec();
174 
175  // If the MPU9250 SPI transaction has succeeded: convert the data
177 
179  // set channel order
180  struct Int32Vect3 accel = {
184  };
185  struct Int32Rates rates = {
189  };
190 
191 #if IMU_MPU9250_READ_MAG
192  if (!bit_is_set(imu_mpu9250.mpu.data_ext[6], 3)) { //mag valid just HOFL == 0
194  struct Int32Vect3 mag;
198  AbiSendMsgIMU_MAG_RAW(IMU_MPU9250_ID, now_ts, &mag);
199  }
200 #endif
201 
203  AbiSendMsgIMU_GYRO_RAW(IMU_MPU9250_ID, now_ts, &rates, 1, NAN);
204  AbiSendMsgIMU_ACCEL_RAW(IMU_MPU9250_ID, now_ts, &accel, 1, NAN);
205  }
206 
207 }
208 
209 // hack with waiting to avoid creating another event loop to check the mag config status
210 static inline void mpu_set_and_wait(Mpu9250ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
211 {
212  mpu_set(mpu, _reg, _val);
213  while (imu_mpu9250.mpu.spi_trans.status != SPITransSuccess);
214 }
215 
220 {
221  // wait before starting the configuration of the mag
222  // doing to early may void the mode configuration
224  return false;
225  }
226 
227  //config AK8963 soft reset
230  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_DO, 1);
231  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
232 
234 
235  // Set it to continious measuring mode 2
239  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
240 
242 
243  //Config SLV0 for continus Read
246  // Put the enable command as last.
248  (1 << 7) | // Slave 0 enable
249  (7 << 0)); // Read 7 bytes (mag x,y,z + status)
250 
251  return true;
252 }
253 
255 {
256  while (!imu_mpu9250.slave4_ready) {
260  }
261  }
262 }
263 
265 {
266  if (bit_is_set(t->input_buf[1], MPU9250_I2C_SLV4_DONE)) {
267  imu_mpu9250.slave4_ready = true;
268  } else {
269  imu_mpu9250.slave4_ready = false;
270  }
271  t->status = SPITransDone;
272 }
Main include for ABI (AirBorneInterface).
#define IMU_MPU9250_ID
#define AK8963_REG_HXL
Definition: ak8963_regs.h:46
#define AK8963_CNTL1_CM_2
Definition: ak8963_regs.h:37
#define AK8963_REG_CNTL2
Definition: ak8963_regs.h:54
#define AK8963_REG_CNTL1
Definition: ak8963_regs.h:53
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:75
angular rates
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
uint16_t input_length
number of data words to read
Definition: spi.h:151
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
uint16_t output_length
number of data words to write
Definition: spi.h:152
enum SPITransactionStatus status
Definition: spi.h:162
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:508
@ SPICphaEdge2
CPHA = 1.
Definition: spi.h:75
@ SPITransSuccess
Definition: spi.h:99
@ SPITransDone
Definition: spi.h:101
@ SPICpolIdleHigh
CPOL = 1.
Definition: spi.h:84
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
@ SPIMSBFirst
Definition: spi.h:112
@ SPIDiv64
Definition: spi.h:125
@ SPIDss8bit
Definition: spi.h:90
SPI transaction structure.
Definition: spi.h:148
void imu_set_defaults_accel(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Vect3 *neutral, const struct Int32Vect3 *scale)
Set the defaults for a accel sensor WARNING: Should be called before sensor is publishing messages to...
Definition: imu.c:440
void imu_set_defaults_gyro(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Rates *neutral, const struct Int32Rates *scale)
Set the defaults for a gyro sensor WARNING: Should be called before sensor is publishing messages to ...
Definition: imu.c:410
Inertial Measurement Unit interface.
#define IMU_MPU9250_ACCEL_RANGE
#define IMU_MPU9250_GYRO_RANGE
#define IMU_MPU9250_MAG_STARTUP_DELAY
#define IMU_MPU9250_GYRO_LOWPASS_FILTER
#define IMU_MPU9250_X_SIGN
#define IMU_MPU9250_CHAN_X
bool imu_mpu9250_configure_mag_slave(Mpu9250ConfigSet mpu_set, void *mpu)
function to configure akm8963 mag
void imu_mpu9250_periodic(void)
#define IMU_MPU9250_Y_SIGN
#define IMU_MPU9250_ACCEL_LOWPASS_FILTER
void mpu_wait_slave4_ready_cb(struct spi_transaction *t)
void imu_mpu9250_init(void)
void mpu_wait_slave4_ready(void)
#define IMU_MPU9250_Z_SIGN
#define IMU_MPU9250_SMPLRT_DIV
#define IMU_MPU9250_CHAN_Z
#define IMU_MPU9250_CHAN_Y
static void mpu_set_and_wait(Mpu9250ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
struct ImuMpu9250 imu_mpu9250
#define Int16FromBuf(_buf, _idx)
void imu_mpu9250_event(void)
IMU driver for the MPU9250 using SPI.
struct spi_transaction wait_slave4_trans
volatile uint8_t wait_slave4_rx_buf[2]
struct Mpu9250_I2c mpu
volatile uint8_t wait_slave4_tx_buf[1]
volatile bool slave4_ready
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
const struct Int32Vect3 MPU9250_ACCEL_SENS_FRAC[4][2]
Definition: mpu9250.c:57
const struct Int32Rates MPU9250_GYRO_SENS_FRAC[4][2]
Definition: mpu9250.c:39
enum Mpu9250DLPFGyro dlpf_gyro_cfg
Digital Low Pass Filter for gyroscope.
Definition: mpu9250.h:130
Mpu9250I2cSlaveConfigure configure
Definition: mpu9250.h:124
void(* Mpu9250ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu9250.h:118
uint8_t i2c_mst_delay
MPU I2C slaves delayed sample rate.
Definition: mpu9250.h:148
enum Mpu9250MstClk i2c_mst_clk
MPU I2C master clock speed.
Definition: mpu9250.h:147
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu9250.h:144
enum Mpu9250GyroRanges gyro_range
deg/s Range
Definition: mpu9250.h:131
uint8_t nb_bytes
number of bytes to read starting with MPU9250_REG_INT_STATUS
Definition: mpu9250.h:135
enum Mpu9250DLPFAccel dlpf_accel_cfg
Digital Low Pass Filter for accelerometer.
Definition: mpu9250.h:129
uint8_t smplrt_div
Sample rate divider.
Definition: mpu9250.h:128
enum Mpu9250AccelRanges accel_range
g Range
Definition: mpu9250.h:132
struct Mpu9250I2cSlave slaves[MPU9250_I2C_NB_SLAVES]
I2C slaves.
Definition: mpu9250.h:146
struct Mpu9250Config config
Definition: mpu9250_i2c.h:72
uint8_t data_ext[MPU9250_BUFFER_EXT_LEN]
Definition: mpu9250_i2c.h:71
union Mpu9250_I2c::@328 data_accel
union Mpu9250_I2c::@329 data_rates
volatile bool data_available
data ready flag
Definition: mpu9250_i2c.h:62
#define MPU9250_REG_I2C_SLV4_ADDR
Definition: mpu9250_regs.h:84
#define MPU9250_I2C_SLV4_DONE
Definition: mpu9250_regs.h:135
#define MPU9250_REG_I2C_MST_STATUS
Definition: mpu9250_regs.h:61
#define MPU9250_REG_I2C_SLV0_ADDR
Definition: mpu9250_regs.h:64
#define MPU9250_SPI_READ
Definition: mpu9250_regs.h:37
@ MPU9250_MST_CLK_400KHZ
Definition: mpu9250_regs.h:187
#define MPU9250_MAG_ADDR
Definition: mpu9250_regs.h:35
#define MPU9250_REG_I2C_SLV4_REG
Definition: mpu9250_regs.h:85
#define MPU9250_REG_I2C_SLV4_DO
Definition: mpu9250_regs.h:86
#define MPU9250_REG_I2C_SLV4_CTRL
Definition: mpu9250_regs.h:87
#define MPU9250_REG_I2C_SLV0_CTRL
Definition: mpu9250_regs.h:66
#define MPU9250_REG_I2C_SLV0_REG
Definition: mpu9250_regs.h:65
void mpu9250_spi_init(struct Mpu9250_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu9250_spi.c:31
void mpu9250_spi_event(struct Mpu9250_Spi *mpu)
Definition: mpu9250_spi.c:109
static void mpu9250_spi_periodic(struct Mpu9250_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu9250_spi.h:77
Architecture independent SPI (Serial Peripheral Interface) API.
Architecture independent timing functions.
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:138
int int32_t
Typedef defining 32 bit int type.
Definition: vl53l1_types.h:83
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
Definition: vl53l1_types.h:78
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.
Definition: vl53l1_types.h:98