Paparazzi UAS v7.1_unstable
Paparazzi is a free software Unmanned Aircraft System.
Loading...
Searching...
No Matches
imu_aspirin_2_spi.c
Go to the documentation of this file.
1/*
2 * Copyright (C) 2013 Felix Ruess <felix.ruess@gmail.com>
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, write to
18 * the Free Software Foundation, 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
26
28#include "modules/imu/imu.h"
29#include "modules/core/abi.h"
30#include "mcu_periph/sys_time.h"
31#include "mcu_periph/spi.h"
33#include "generated/modules.h"
34
35/* defaults suitable for Lisa */
36#ifndef ASPIRIN_2_SPI_SLAVE_IDX
37#define ASPIRIN_2_SPI_SLAVE_IDX SPI_SLAVE2
38#endif
40
41#ifndef ASPIRIN_2_SPI_DEV
42#define ASPIRIN_2_SPI_DEV spi2
43#endif
45
46/* MPU60x0 gyro/accel internal lowpass frequency */
47#if !defined ASPIRIN_2_LOWPASS_FILTER && !defined ASPIRIN_2_SMPLRT_DIV
48#if PERIODIC_FREQUENCY <= 120
49/* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
50 * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
51 */
52#define ASPIRIN_2_LOWPASS_FILTER MPU60X0_DLPF_42HZ
53#define ASPIRIN_2_SMPLRT_DIV 9
54PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
55
56#elif PERIODIC_FREQUENCY < 500
57/* Accelerometer: Bandwidth 94Hz, Delay 3ms
58 * Gyroscope: Bandwidth 98Hz, Delay 2.8ms sampling 1kHz
59 */
60#define ASPIRIN_2_LOWPASS_FILTER MPU60X0_DLPF_98HZ
61#define ASPIRIN_2_SMPLRT_DIV 1
62PRINT_CONFIG_MSG("Gyro/Accel output rate is 500Hz at 1kHz internal sampling")
63
64#else // PERIODIC_FREQUENCY >= 500
65/* Accelerometer: Bandwidth 260Hz, Delay 0ms
66 * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
67 */
68#define ASPIRIN_2_LOWPASS_FILTER MPU60X0_DLPF_256HZ
69#define ASPIRIN_2_SMPLRT_DIV 3
70PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
71
72#endif
73#endif
74
79
80
81/* HMC58XX default conf */
82#ifndef HMC58XX_DO
83#define HMC58XX_DO 0x6 // Data Output Rate (6 -> 75Hz with HMC5883)
84#endif
85#ifndef HMC58XX_MS
86#define HMC58XX_MS 0x0 // Measurement configuration
87#endif
88#ifndef HMC58XX_GN
89#define HMC58XX_GN 0x1 // Gain configuration (1 -> +- 1 Gauss)
90#endif
91#ifndef HMC58XX_MD
92#define HMC58XX_MD 0x0 // Continious measurement mode
93#endif
94
95#define HMC58XX_CRA ((HMC58XX_DO<<2)|(HMC58XX_MS))
96#define HMC58XX_CRB (HMC58XX_GN<<5)
97
99#ifndef ASPIRIN_2_MAG_STARTUP_DELAY
100#define ASPIRIN_2_MAG_STARTUP_DELAY 1.5
101#endif
102
104
105void mpu_wait_slave4_ready(void);
108
110{
112 // change the default configuration
113 imu_aspirin2.mpu.config.smplrt_div = ASPIRIN_2_SMPLRT_DIV;
114 imu_aspirin2.mpu.config.dlpf_cfg = ASPIRIN_2_LOWPASS_FILTER;
115 imu_aspirin2.mpu.config.gyro_range = ASPIRIN_2_GYRO_RANGE;
116 imu_aspirin2.mpu.config.accel_range = ASPIRIN_2_ACCEL_RANGE;
117
118 // Set the default scaling
121
122 /* read 15 bytes for status, accel, gyro + 6 bytes for mag slave */
123 imu_aspirin2.mpu.config.nb_bytes = 21;
124
125 /* use mag if not disabled */
126#if !ASPIRIN_2_DISABLE_MAG
127 /* HMC5883 magnetometer as I2C slave */
128 imu_aspirin2.mpu.config.nb_slaves = 1;
129
130 /* set function to configure mag */
131 imu_aspirin2.mpu.config.slaves[0].configure = &imu_aspirin2_configure_mag_slave;
132
133 /* Set MPU I2C master clock */
134 imu_aspirin2.mpu.config.i2c_mst_clk = MPU60X0_MST_CLK_400KHZ;
135 /* Enable I2C slave0 delayed sample rate */
136 imu_aspirin2.mpu.config.i2c_mst_delay = 1;
137
138
139 /* configure spi transaction for wait_slave4 */
140 imu_aspirin2.wait_slave4_trans.cpol = SPICpolIdleHigh;
141 imu_aspirin2.wait_slave4_trans.cpha = SPICphaEdge2;
142 imu_aspirin2.wait_slave4_trans.dss = SPIDss8bit;
143 imu_aspirin2.wait_slave4_trans.bitorder = SPIMSBFirst;
144 imu_aspirin2.wait_slave4_trans.cdiv = SPIDiv64;
145
146 imu_aspirin2.wait_slave4_trans.select = SPISelectUnselect;
147 imu_aspirin2.wait_slave4_trans.slave_idx = ASPIRIN_2_SPI_SLAVE_IDX;
148 imu_aspirin2.wait_slave4_trans.output_length = 1;
149 imu_aspirin2.wait_slave4_trans.input_length = 2;
150 imu_aspirin2.wait_slave4_trans.before_cb = NULL;
151 imu_aspirin2.wait_slave4_trans.after_cb = mpu_wait_slave4_ready_cb;
152 imu_aspirin2.wait_slave4_trans.input_buf = &(imu_aspirin2.wait_slave4_rx_buf[0]);
153 imu_aspirin2.wait_slave4_trans.output_buf = &(imu_aspirin2.wait_slave4_tx_buf[0]);
154
155 imu_aspirin2.wait_slave4_trans.status = SPITransDone;
156 imu_aspirin2.slave4_ready = false;
157#endif
158}
159
160
165
166#define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
167
169{
170 uint32_t now_ts = get_sys_time_usec();
171
173 if (imu_aspirin2.mpu.data_available) {
174#if !ASPIRIN_2_DISABLE_MAG
175 /* HMC5883 has xzy order of axes in returned data */
176 struct Int32Vect3 mag, mag_rot;
177 mag.x = Int16FromBuf(imu_aspirin2.mpu.data_ext, 0);
178 mag.z = Int16FromBuf(imu_aspirin2.mpu.data_ext, 2);
179 mag.y = Int16FromBuf(imu_aspirin2.mpu.data_ext, 4);
180#endif
181
182 /* Handle axis assignement for Lisa/S integrated Aspirin like IMU. */
183 struct Int32Rates gyro;
184 struct Int32Vect3 accel;
185#ifdef LISA_S
186#ifdef LISA_S_UPSIDE_DOWN
187 RATES_ASSIGN(gyro,
188 imu_aspirin2.mpu.data_rates.rates.p,
189 -imu_aspirin2.mpu.data_rates.rates.q,
190 -imu_aspirin2.mpu.data_rates.rates.r);
191 VECT3_ASSIGN(accel,
192 imu_aspirin2.mpu.data_accel.vect.x,
193 -imu_aspirin2.mpu.data_accel.vect.y,
194 -imu_aspirin2.mpu.data_accel.vect.z);
195#if !ASPIRIN_2_DISABLE_MAG
196 VECT3_ASSIGN(mag_rot, mag.x, -mag.y, -mag.z);
197#endif
198#else
199 RATES_COPY(gyro, imu_aspirin2.mpu.data_rates.rates);
200 VECT3_COPY(accel, imu_aspirin2.mpu.data_accel.vect);
201#if !ASPIRIN_2_DISABLE_MAG
202 VECT3_COPY(mag_rot, mag);
203#endif
204#endif
205#else
206
207 /* Handle axis assignement for Lisa/M or Lisa/MX V2.1 integrated Aspirin like
208 * IMU.
209 */
210#ifdef LISA_M_OR_MX_21
211 RATES_ASSIGN(gyro,
212 -imu_aspirin2.mpu.data_rates.rates.q,
213 imu_aspirin2.mpu.data_rates.rates.p,
214 imu_aspirin2.mpu.data_rates.rates.r);
215 VECT3_ASSIGN(accel,
216 -imu_aspirin2.mpu.data_accel.vect.y,
217 imu_aspirin2.mpu.data_accel.vect.x,
218 imu_aspirin2.mpu.data_accel.vect.z);
219#if !ASPIRIN_2_DISABLE_MAG
220 VECT3_ASSIGN(mag_rot, -mag.y, mag.x, mag.z);
221#endif
222#else
223
224 /* Handle real Aspirin IMU axis assignement. */
225#ifdef LISA_M_LONGITUDINAL_X
226 RATES_ASSIGN(gyro,
227 imu_aspirin2.mpu.data_rates.rates.q,
228 -imu_aspirin2.mpu.data_rates.rates.p,
229 imu_aspirin2.mpu.data_rates.rates.r);
230 VECT3_ASSIGN(accel,
231 imu_aspirin2.mpu.data_accel.vect.y,
232 -imu_aspirin2.mpu.data_accel.vect.x,
233 imu_aspirin2.mpu.data_accel.vect.z);
234#if !ASPIRIN_2_DISABLE_MAG
235 VECT3_ASSIGN(mag_rot, -mag.x, -mag.y, mag.z);
236#endif
237#else
238 RATES_COPY(gyro, imu_aspirin2.mpu.data_rates.rates);
239 VECT3_COPY(accel, imu_aspirin2.mpu.data_accel.vect);
240#if !ASPIRIN_2_DISABLE_MAG
241 VECT3_ASSIGN(mag_rot, mag.y, -mag.x, mag.z)
242#endif
243#endif
244#endif
245#endif
246
247 imu_aspirin2.mpu.data_available = false;
248
249 AbiSendMsgIMU_GYRO_RAW(IMU_ASPIRIN2_ID, now_ts, &gyro, 1, IMU_ASPIRIN2_PERIODIC_FREQ, imu_aspirin2.mpu.temp);
250 AbiSendMsgIMU_ACCEL_RAW(IMU_ASPIRIN2_ID, now_ts, &accel, 1, IMU_ASPIRIN2_PERIODIC_FREQ, imu_aspirin2.mpu.temp);
251#if !ASPIRIN_2_DISABLE_MAG
252 AbiSendMsgIMU_MAG_RAW(IMU_ASPIRIN2_ID, now_ts, &mag_rot);
253#endif
254 }
255}
256
257// hack with waiting to avoid creating another event loop to check the mag config status
258static inline void mpu_set_and_wait(Mpu60x0ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
259{
260 mpu_set(mpu, _reg, _val);
261 while (imu_aspirin2.mpu.spi_trans.status != SPITransSuccess);
262}
263
268{
269 // wait before starting the configuration of the HMC58xx mag
270 // doing to early may void the mode configuration
272 return false;
273 }
274
278 mpu_set_and_wait(mpu_set, mpu, MPU60X0_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
279
281
285 mpu_set_and_wait(mpu_set, mpu, MPU60X0_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
286
288
292 mpu_set_and_wait(mpu_set, mpu, MPU60X0_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
293
296 // Put the enable command as last.
298 (1 << 7) | // Slave 0 enable
299 (6 << 0)); // Read 6 bytes
300
301 return true;
302}
303
305{
306 while (!imu_aspirin2.slave4_ready) {
307 if (imu_aspirin2.wait_slave4_trans.status == SPITransDone) {
309 spi_submit(imu_aspirin2.mpu.spi_p, &(imu_aspirin2.wait_slave4_trans));
310 }
311 }
312}
313
315{
316 if (bit_is_set(t->input_buf[1], MPU60X0_I2C_SLV4_DONE)) {
317 imu_aspirin2.slave4_ready = true;
318 } else {
319 imu_aspirin2.slave4_ready = false;
320 }
321 t->status = SPITransDone;
322}
Main include for ABI (AirBorneInterface).
#define IMU_ASPIRIN2_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
#define RATES_COPY(_a, _b)
#define RATES_ASSIGN(_ra, _p, _q, _r)
#define VECT3_ASSIGN(_a, _x, _y, _z)
#define VECT3_COPY(_a, _b)
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
Register defs for Honeywell HMC5843, HMC5883 and HMC5983 magnetometers.
#define HMC58XX_REG_CFGB
#define HMC58XX_REG_CFGA
#define HMC58XX_REG_MODE
#define HMC58XX_ADDR
#define HMC58XX_REG_DATXM
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.
#define ASPIRIN_2_SPI_DEV
void imu_aspirin2_event(void)
#define ASPIRIN_2_SMPLRT_DIV
#define ASPIRIN_2_MAG_STARTUP_DELAY
delay in seconds before starting to configure HMC58xx mag slave
#define ASPIRIN_2_LOWPASS_FILTER
struct ImuAspirin2Spi imu_aspirin2
#define HMC58XX_CRA
#define HMC58XX_CRB
void mpu_wait_slave4_ready_cb(struct spi_transaction *t)
#define ASPIRIN_2_SPI_SLAVE_IDX
#define HMC58XX_MD
void imu_aspirin2_periodic(void)
void mpu_wait_slave4_ready(void)
void imu_aspirin2_init(void)
bool imu_aspirin2_configure_mag_slave(Mpu60x0ConfigSet mpu_set, void *mpu)
function to configure hmc5883 mag
#define Int16FromBuf(_buf, _idx)
static void mpu_set_and_wait(Mpu60x0ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
Driver for the Aspirin v2.x IMU using SPI for the MPU6000.
#define ASPIRIN_2_GYRO_RANGE
struct Mpu60x0_Spi mpu
#define ASPIRIN_2_ACCEL_RANGE
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
const struct FloatVect3 MPU60X0_ACCEL_SENS_F[4]
Definition mpu60x0.c:54
const struct FloatRates MPU60X0_GYRO_SENS_F[4]
Definition mpu60x0.c:38
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition mpu60x0.h:112
#define MPU60X0_I2C_SLV4_DONE
#define MPU60X0_REG_I2C_SLV0_ADDR
#define MPU60X0_REG_I2C_SLV4_REG
#define MPU60X0_REG_I2C_SLV4_CTRL
#define MPU60X0_REG_I2C_MST_STATUS
#define MPU60X0_REG_I2C_SLV0_REG
#define MPU60X0_REG_I2C_SLV0_CTRL
#define MPU60X0_SPI_READ
#define MPU60X0_REG_I2C_SLV4_ADDR
@ MPU60X0_MST_CLK_400KHZ
#define MPU60X0_REG_I2C_SLV4_DO
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition mpu60x0_spi.c:31
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition mpu60x0_spi.h:78
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
unsigned int uint32_t
Typedef defining 32 bit unsigned int type.
unsigned char uint8_t
Typedef defining 8 bit unsigned char type.