Paparazzi UAS  v7.0_unstable
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 
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 #include "generated/modules.h"
35 
36 /* SPI defaults set in subsystem makefile, can be configured from airframe file */
37 PRINT_CONFIG_VAR(IMU_MPU9250_SPI_SLAVE_IDX)
38 PRINT_CONFIG_VAR(IMU_MPU9250_SPI_DEV)
39 
40 
41 #if !defined IMU_MPU9250_GYRO_LOWPASS_FILTER && !defined IMU_MPU9250_ACCEL_LOWPASS_FILTER && !defined IMU_MPU9250_SMPLRT_DIV
42 #if (PERIODIC_FREQUENCY >= 60) && (PERIODIC_FREQUENCY <= 120)
43 /* Accelerometer: Bandwidth 41Hz, Delay 5.9ms
44  * Gyroscope: Bandwidth 41Hz, Delay 5.9ms sampling 1kHz
45  * Output rate: 100Hz
46  */
47 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
48 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
49 #define IMU_MPU9250_SMPLRT_DIV 9
50 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
51 #elif (PERIODIC_FREQUENCY == 512) || (PERIODIC_FREQUENCY == 500)
52 /* Accelerometer: Bandwidth 184Hz, Delay 5.8ms
53  * Gyroscope: Bandwidth 250Hz, Delay 0.97ms sampling 8kHz
54  * Output rate: 2kHz
55  */
56 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_250HZ
57 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_184HZ
58 #define IMU_MPU9250_SMPLRT_DIV 3
59 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
60 #else
61 /* By default, don't go too fast */
62 #define IMU_MPU9250_SMPLRT_DIV 9
63 #define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_41HZ
64 #define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_41HZ
65 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
66 #warning "Non-default PERIODIC_FREQUENCY: please define IMU_MPU9250_GYRO_LOWPASS_FILTER, IMU_MPU9250_ACCEL_LOWPASS_FILTER and IMU_MPU9250_SMPLRT_DIV."
67 #endif
68 #endif
72 
75 
76 // Default channels order
77 #ifndef IMU_MPU9250_CHAN_X
78 #define IMU_MPU9250_CHAN_X 0
79 #endif
81 #ifndef IMU_MPU9250_CHAN_Y
82 #define IMU_MPU9250_CHAN_Y 1
83 #endif
85 #ifndef IMU_MPU9250_CHAN_Z
86 #define IMU_MPU9250_CHAN_Z 2
87 #endif
89 
90 #ifndef IMU_MPU9250_X_SIGN
91 #define IMU_MPU9250_X_SIGN 1
92 #endif
94 #ifndef IMU_MPU9250_Y_SIGN
95 #define IMU_MPU9250_Y_SIGN 1
96 #endif
98 #ifndef IMU_MPU9250_Z_SIGN
99 #define IMU_MPU9250_Z_SIGN 1
100 #endif
102 
103 #ifndef IMU_MPU9250_READ_MAG
104 #define IMU_MPU9250_READ_MAG TRUE
105 #endif
106 
107 #ifndef IMU_MPU9250_MAG_STARTUP_DELAY
108 #define IMU_MPU9250_MAG_STARTUP_DELAY 1
109 #endif
110 
111 struct ImuMpu9250 imu_mpu9250;
112 
113 void mpu_wait_slave4_ready(void);
116 
118 {
119  /* MPU9250 */
120  mpu9250_spi_init(&imu_mpu9250.mpu, &(IMU_MPU9250_SPI_DEV), IMU_MPU9250_SPI_SLAVE_IDX);
121  // change the default configuration
127 
128  // Set the default scaling
131 
132  /* "internal" ak8963 magnetometer as I2C slave */
133 #if IMU_MPU9250_READ_MAG
134  /* read 15 bytes for status, accel, gyro + 7 bytes for mag slave */
137 #endif
138  /* set callback function to configure mag */
140 
141  /* Set MPU I2C master clock */
143  /* Enable I2C slave0 delayed sample rate */
145 
146 
147  /* configure spi transaction for wait_slave4 */
153 
155  imu_mpu9250.wait_slave4_trans.slave_idx = IMU_MPU9250_SPI_SLAVE_IDX;
162 
164  imu_mpu9250.slave4_ready = false;
165 }
166 
168 {
170 }
171 
172 #define Int16FromBuf(_buf,_idx) ((int16_t)(_buf[_idx] | (_buf[_idx+1] << 8)))
174 {
175  uint32_t now_ts = get_sys_time_usec();
176 
177  // If the MPU9250 SPI transaction has succeeded: convert the data
179 
181  // set channel order
182  struct Int32Vect3 accel = {
186  };
187  struct Int32Rates rates = {
191  };
192 
193 #if IMU_MPU9250_READ_MAG
194  if (!bit_is_set(imu_mpu9250.mpu.data_ext[6], 3)) { //mag valid just HOFL == 0
196  struct Int32Vect3 mag;
200  AbiSendMsgIMU_MAG_RAW(IMU_MPU9250_ID, now_ts, &mag);
201  }
202 #endif
203 
205  AbiSendMsgIMU_GYRO_RAW(IMU_MPU9250_ID, now_ts, &rates, 1, IMU_MPU9250_PERIODIC_FREQ, NAN);
206  AbiSendMsgIMU_ACCEL_RAW(IMU_MPU9250_ID, now_ts, &accel, 1, IMU_MPU9250_PERIODIC_FREQ, NAN);
207  }
208 
209 }
210 
211 // hack with waiting to avoid creating another event loop to check the mag config status
212 static inline void mpu_set_and_wait(Mpu9250ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
213 {
214  mpu_set(mpu, _reg, _val);
215  while (imu_mpu9250.mpu.spi_trans.status != SPITransSuccess);
216 }
217 
222 {
223  // wait before starting the configuration of the mag
224  // doing to early may void the mode configuration
226  return false;
227  }
228 
229  //config AK8963 soft reset
232  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_DO, 1);
233  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
234 
236 
237  // Set it to continious measuring mode 2
241  mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
242 
244 
245  //Config SLV0 for continus Read
248  // Put the enable command as last.
250  (1 << 7) | // Slave 0 enable
251  (7 << 0)); // Read 7 bytes (mag x,y,z + status)
252 
253  return true;
254 }
255 
257 {
258  while (!imu_mpu9250.slave4_ready) {
262  }
263  }
264 }
265 
267 {
268  if (bit_is_set(t->input_buf[1], MPU9250_I2C_SLV4_DONE)) {
269  imu_mpu9250.slave4_ready = true;
270  } else {
271  imu_mpu9250.slave4_ready = false;
272  }
273  t->status = SPITransDone;
274 }
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:71
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:533
@ 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:610
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:580
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
union Mpu9250_I2c::@346 data_rates
struct Mpu9250Config config
Definition: mpu9250_i2c.h:72
uint8_t data_ext[MPU9250_BUFFER_EXT_LEN]
Definition: mpu9250_i2c.h:71
union Mpu9250_I2c::@345 data_accel
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
PRINT_CONFIG_VAR(ONELOOP_ANDI_FILT_CUTOFF)
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