Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
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
27
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 */
37PRINT_CONFIG_VAR(IMU_MPU9250_SPI_SLAVE_IDX)
38PRINT_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 <= 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
50PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
51
52#elif PERIODIC_FREQUENCY < 500
53/* Accelerometer: Bandwidth 92Hz
54 * Gyroscope: Bandwidth 92Hz, Delay 3.9ms sampling 1kHz
55 * Output rate: 500Hz
56 */
57#define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_92HZ
58#define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_92HZ
59#define IMU_MPU9250_SMPLRT_DIV 1
60PRINT_CONFIG_MSG("Gyro/Accel output rate is 500Hz at 1kHz internal sampling")
61
62#else // PERIODIC_FREQUENCY >= 500
63/* Accelerometer: Bandwidth 184Hz, Delay 5.8ms
64 * Gyroscope: Bandwidth 250Hz, Delay 0.97ms sampling 8kHz
65 * Output rate: 2kHz
66 */
67#define IMU_MPU9250_GYRO_LOWPASS_FILTER MPU9250_DLPF_GYRO_250HZ
68#define IMU_MPU9250_ACCEL_LOWPASS_FILTER MPU9250_DLPF_ACCEL_184HZ
69#define IMU_MPU9250_SMPLRT_DIV 3
70PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
71
72#endif
73#endif
74
80
81// Default channels order
82#ifndef IMU_MPU9250_CHAN_X
83#define IMU_MPU9250_CHAN_X 0
84#endif
86#ifndef IMU_MPU9250_CHAN_Y
87#define IMU_MPU9250_CHAN_Y 1
88#endif
90#ifndef IMU_MPU9250_CHAN_Z
91#define IMU_MPU9250_CHAN_Z 2
92#endif
94
95#ifndef IMU_MPU9250_X_SIGN
96#define IMU_MPU9250_X_SIGN 1
97#endif
99#ifndef IMU_MPU9250_Y_SIGN
100#define IMU_MPU9250_Y_SIGN 1
101#endif
103#ifndef IMU_MPU9250_Z_SIGN
104#define IMU_MPU9250_Z_SIGN 1
105#endif
107
108#ifndef IMU_MPU9250_READ_MAG
109#define IMU_MPU9250_READ_MAG TRUE
110#endif
111
112#ifndef IMU_MPU9250_MAG_STARTUP_DELAY
113#define IMU_MPU9250_MAG_STARTUP_DELAY 1
114#endif
115
117
118void mpu_wait_slave4_ready(void);
121
123{
124 /* MPU9250 */
125 mpu9250_spi_init(&imu_mpu9250.mpu, &(IMU_MPU9250_SPI_DEV), IMU_MPU9250_SPI_SLAVE_IDX);
126 // change the default configuration
127 imu_mpu9250.mpu.config.smplrt_div = IMU_MPU9250_SMPLRT_DIV;
128 imu_mpu9250.mpu.config.dlpf_gyro_cfg = IMU_MPU9250_GYRO_LOWPASS_FILTER;
129 imu_mpu9250.mpu.config.dlpf_accel_cfg = IMU_MPU9250_ACCEL_LOWPASS_FILTER;
130 imu_mpu9250.mpu.config.gyro_range = IMU_MPU9250_GYRO_RANGE;
131 imu_mpu9250.mpu.config.accel_range = IMU_MPU9250_ACCEL_RANGE;
132
133 // Set the default scaling
136
137 /* "internal" ak8963 magnetometer as I2C slave */
138#if IMU_MPU9250_READ_MAG
139 /* read 15 bytes for status, accel, gyro + 7 bytes for mag slave */
140 imu_mpu9250.mpu.config.nb_bytes = 22;
141 imu_mpu9250.mpu.config.nb_slaves = 1;
142#endif
143 /* set callback function to configure mag */
144 imu_mpu9250.mpu.config.slaves[0].configure = &imu_mpu9250_configure_mag_slave;
145
146 /* Set MPU I2C master clock */
147 imu_mpu9250.mpu.config.i2c_mst_clk = MPU9250_MST_CLK_400KHZ;
148 /* Enable I2C slave0 delayed sample rate */
149 imu_mpu9250.mpu.config.i2c_mst_delay = 1;
150
151
152 /* configure spi transaction for wait_slave4 */
153 imu_mpu9250.wait_slave4_trans.cpol = SPICpolIdleHigh;
154 imu_mpu9250.wait_slave4_trans.cpha = SPICphaEdge2;
155 imu_mpu9250.wait_slave4_trans.dss = SPIDss8bit;
156 imu_mpu9250.wait_slave4_trans.bitorder = SPIMSBFirst;
157 imu_mpu9250.wait_slave4_trans.cdiv = SPIDiv64;
158
159 imu_mpu9250.wait_slave4_trans.select = SPISelectUnselect;
160 imu_mpu9250.wait_slave4_trans.slave_idx = IMU_MPU9250_SPI_SLAVE_IDX;
161 imu_mpu9250.wait_slave4_trans.output_length = 1;
162 imu_mpu9250.wait_slave4_trans.input_length = 2;
163 imu_mpu9250.wait_slave4_trans.before_cb = NULL;
164 imu_mpu9250.wait_slave4_trans.after_cb = mpu_wait_slave4_ready_cb;
165 imu_mpu9250.wait_slave4_trans.input_buf = &(imu_mpu9250.wait_slave4_rx_buf[0]);
166 imu_mpu9250.wait_slave4_trans.output_buf = &(imu_mpu9250.wait_slave4_tx_buf[0]);
167
168 imu_mpu9250.wait_slave4_trans.status = SPITransDone;
169 imu_mpu9250.slave4_ready = false;
170}
171
176
177#define Int16FromBuf(_buf,_idx) ((int16_t)(_buf[_idx] | (_buf[_idx+1] << 8)))
179{
180 uint32_t now_ts = get_sys_time_usec();
181
182 // If the MPU9250 SPI transaction has succeeded: convert the data
184
185 if (imu_mpu9250.mpu.data_available) {
186 // set channel order
187 struct Int32Vect3 accel = {
188 IMU_MPU9250_X_SIGN * (int32_t)(imu_mpu9250.mpu.data_accel.value[IMU_MPU9250_CHAN_X]),
189 IMU_MPU9250_Y_SIGN * (int32_t)(imu_mpu9250.mpu.data_accel.value[IMU_MPU9250_CHAN_Y]),
191 };
192 struct Int32Rates rates = {
193 IMU_MPU9250_X_SIGN * (int32_t)(imu_mpu9250.mpu.data_rates.value[IMU_MPU9250_CHAN_X]),
194 IMU_MPU9250_Y_SIGN * (int32_t)(imu_mpu9250.mpu.data_rates.value[IMU_MPU9250_CHAN_Y]),
196 };
197
198#if IMU_MPU9250_READ_MAG
199 if (!bit_is_set(imu_mpu9250.mpu.data_ext[6], 3)) { //mag valid just HOFL == 0
201 struct Int32Vect3 mag;
204 mag.z = -(IMU_MPU9250_Z_SIGN) * Int16FromBuf(imu_mpu9250.mpu.data_ext, 2 * IMU_MPU9250_CHAN_Z);
205 AbiSendMsgIMU_MAG_RAW(IMU_MPU9250_ID, now_ts, &mag);
206 }
207#endif
208
209 imu_mpu9250.mpu.data_available = false;
210 AbiSendMsgIMU_GYRO_RAW(IMU_MPU9250_ID, now_ts, &rates, 1, IMU_MPU9250_PERIODIC_FREQ, NAN);
211 AbiSendMsgIMU_ACCEL_RAW(IMU_MPU9250_ID, now_ts, &accel, 1, IMU_MPU9250_PERIODIC_FREQ, NAN);
212 }
213
214}
215
216// hack with waiting to avoid creating another event loop to check the mag config status
217static inline void mpu_set_and_wait(Mpu9250ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
218{
219 mpu_set(mpu, _reg, _val);
220 while (imu_mpu9250.mpu.spi_trans.status != SPITransSuccess);
221}
222
227{
228 // wait before starting the configuration of the mag
229 // doing to early may void the mode configuration
231 return false;
232 }
233
234 //config AK8963 soft reset
238 mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
239
241
242 // Set it to continious measuring mode 2
246 mpu_set_and_wait(mpu_set, mpu, MPU9250_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
247
249
250 //Config SLV0 for continus Read
253 // Put the enable command as last.
255 (1 << 7) | // Slave 0 enable
256 (7 << 0)); // Read 7 bytes (mag x,y,z + status)
257
258 return true;
259}
260
262{
263 while (!imu_mpu9250.slave4_ready) {
264 if (imu_mpu9250.wait_slave4_trans.status == SPITransDone) {
266 spi_submit(imu_mpu9250.mpu.spi_p, &(imu_mpu9250.wait_slave4_trans));
267 }
268 }
269}
270
272{
273 if (bit_is_set(t->input_buf[1], MPU9250_I2C_SLV4_DONE)) {
274 imu_mpu9250.slave4_ready = true;
275 } else {
276 imu_mpu9250.slave4_ready = false;
277 }
278 t->status = SPITransDone;
279}
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.
angular rates
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition spi.h:145
enum SPITransactionStatus status
Definition spi.h:158
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition spi_arch.c:548
@ SPICphaEdge2
CPHA = 1.
Definition spi.h:71
@ SPITransSuccess
Definition spi.h:95
@ SPITransDone
Definition spi.h:97
@ SPICpolIdleHigh
CPOL = 1.
Definition spi.h:80
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition spi.h:59
@ SPIMSBFirst
Definition spi.h:108
@ SPIDiv64
Definition spi.h:121
@ SPIDss8bit
Definition spi.h:86
SPI transaction structure.
Definition spi.h:144
void imu_set_defaults_gyro(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Rates *neutral, const struct FloatRates *scale_f)
Set the defaults for a gyro sensor WARNING: Should be called before sensor is publishing messages to ...
Definition imu.c:842
void imu_set_defaults_accel(uint8_t abi_id, const struct Int32RMat *imu_to_sensor, const struct Int32Vect3 *neutral, const struct FloatVect3 *scale_f)
Set the defaults for a accel sensor WARNING: Should be called before sensor is publishing messages to...
Definition imu.c:871
Inertial Measurement Unit interface.
void mpu_wait_slave4_ready_cb(struct spi_transaction *t)
#define IMU_MPU9250_GYRO_LOWPASS_FILTER
#define IMU_MPU9250_X_SIGN
#define IMU_MPU9250_CHAN_X
#define IMU_MPU9250_Y_SIGN
#define IMU_MPU9250_ACCEL_LOWPASS_FILTER
#define IMU_MPU9250_Z_SIGN
#define IMU_MPU9250_SMPLRT_DIV
#define IMU_MPU9250_CHAN_Z
#define IMU_MPU9250_CHAN_Y
struct ImuMpu9250 imu_mpu9250
#define IMU_MPU9250_ACCEL_RANGE
#define IMU_MPU9250_GYRO_RANGE
#define IMU_MPU9250_MAG_STARTUP_DELAY
bool imu_mpu9250_configure_mag_slave(Mpu9250ConfigSet mpu_set, void *mpu)
function to configure akm8963 mag
void imu_mpu9250_periodic(void)
void mpu_wait_slave4_ready_cb(struct spi_transaction *t)
void imu_mpu9250_init(void)
void mpu_wait_slave4_ready(void)
static void mpu_set_and_wait(Mpu9250ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
#define Int16FromBuf(_buf, _idx)
void imu_mpu9250_event(void)
IMU driver for the MPU9250 using SPI.
struct Mpu9250_I2c mpu
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
const struct FloatRates MPU9250_GYRO_SENS_F[4]
Definition mpu9250.c:39
const struct FloatVect3 MPU9250_ACCEL_SENS_F[4]
Definition mpu9250.c:53
void(* Mpu9250ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition mpu9250.h:103
#define MPU9250_REG_I2C_SLV4_ADDR
#define MPU9250_I2C_SLV4_DONE
#define MPU9250_REG_I2C_MST_STATUS
#define MPU9250_REG_I2C_SLV0_ADDR
#define MPU9250_SPI_READ
@ MPU9250_MST_CLK_400KHZ
#define MPU9250_MAG_ADDR
#define MPU9250_REG_I2C_SLV4_REG
#define MPU9250_REG_I2C_SLV4_DO
#define MPU9250_REG_I2C_SLV4_CTRL
#define MPU9250_REG_I2C_SLV0_CTRL
#define MPU9250_REG_I2C_SLV0_REG
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)
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:241
int int32_t
Typedef defining 32 bit int type.
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.