Paparazzi UAS  v7.0_unstable
Paparazzi is a free software Unmanned Aircraft System.
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 
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 == 60) || (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
54 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
55 #elif PERIODIC_FREQUENCY == 512
56 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
57  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
58  */
59 #define ASPIRIN_2_LOWPASS_FILTER MPU60X0_DLPF_256HZ
60 #define ASPIRIN_2_SMPLRT_DIV 3
61 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
62 #else
63 #error Non-default PERIODIC_FREQUENCY: please define ASPIRIN_2_LOWPASS_FILTER and ASPIRIN_2_SMPLRT_DIV.
64 #endif
65 #endif
66 PRINT_CONFIG_VAR(ASPIRIN_2_LOWPASS_FILTER)
67 PRINT_CONFIG_VAR(ASPIRIN_2_SMPLRT_DIV)
68 
71 
72 
73 /* HMC58XX default conf */
74 #ifndef HMC58XX_DO
75 #define HMC58XX_DO 0x6 // Data Output Rate (6 -> 75Hz with HMC5883)
76 #endif
77 #ifndef HMC58XX_MS
78 #define HMC58XX_MS 0x0 // Measurement configuration
79 #endif
80 #ifndef HMC58XX_GN
81 #define HMC58XX_GN 0x1 // Gain configuration (1 -> +- 1 Gauss)
82 #endif
83 #ifndef HMC58XX_MD
84 #define HMC58XX_MD 0x0 // Continious measurement mode
85 #endif
86 
87 #define HMC58XX_CRA ((HMC58XX_DO<<2)|(HMC58XX_MS))
88 #define HMC58XX_CRB (HMC58XX_GN<<5)
89 
91 #ifndef ASPIRIN_2_MAG_STARTUP_DELAY
92 #define ASPIRIN_2_MAG_STARTUP_DELAY 1.5
93 #endif
94 
96 
97 void mpu_wait_slave4_ready(void);
100 
102 {
104  // change the default configuration
105  imu_aspirin2.mpu.config.smplrt_div = ASPIRIN_2_SMPLRT_DIV;
106  imu_aspirin2.mpu.config.dlpf_cfg = ASPIRIN_2_LOWPASS_FILTER;
109 
110  // Set the default scaling
113 
114  /* read 15 bytes for status, accel, gyro + 6 bytes for mag slave */
116 
117  /* use mag if not disabled */
118 #if !ASPIRIN_2_DISABLE_MAG
119  /* HMC5883 magnetometer as I2C slave */
121 
122  /* set function to configure mag */
124 
125  /* Set MPU I2C master clock */
127  /* Enable I2C slave0 delayed sample rate */
129 
130 
131  /* configure spi transaction for wait_slave4 */
137 
146 
148  imu_aspirin2.slave4_ready = false;
149 #endif
150 }
151 
152 
154 {
156 }
157 
158 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
159 
161 {
162  uint32_t now_ts = get_sys_time_usec();
163 
166 #if !ASPIRIN_2_DISABLE_MAG
167  /* HMC5883 has xzy order of axes in returned data */
168  struct Int32Vect3 mag, mag_rot;
172 #endif
173 
174  /* Handle axis assignement for Lisa/S integrated Aspirin like IMU. */
175  struct Int32Rates gyro;
176  struct Int32Vect3 accel;
177 #ifdef LISA_S
178 #ifdef LISA_S_UPSIDE_DOWN
179  RATES_ASSIGN(gyro,
180  imu_aspirin2.mpu.data_rates.rates.p,
181  -imu_aspirin2.mpu.data_rates.rates.q,
182  -imu_aspirin2.mpu.data_rates.rates.r);
183  VECT3_ASSIGN(accel,
184  imu_aspirin2.mpu.data_accel.vect.x,
185  -imu_aspirin2.mpu.data_accel.vect.y,
186  -imu_aspirin2.mpu.data_accel.vect.z);
187 #if !ASPIRIN_2_DISABLE_MAG
188  VECT3_ASSIGN(mag_rot, mag.x, -mag.y, -mag.z);
189 #endif
190 #else
191  RATES_COPY(gyro, imu_aspirin2.mpu.data_rates.rates);
192  VECT3_COPY(accel, imu_aspirin2.mpu.data_accel.vect);
193 #if !ASPIRIN_2_DISABLE_MAG
194  VECT3_COPY(mag_rot, mag);
195 #endif
196 #endif
197 #else
198 
199  /* Handle axis assignement for Lisa/M or Lisa/MX V2.1 integrated Aspirin like
200  * IMU.
201  */
202 #ifdef LISA_M_OR_MX_21
203  RATES_ASSIGN(gyro,
204  -imu_aspirin2.mpu.data_rates.rates.q,
205  imu_aspirin2.mpu.data_rates.rates.p,
206  imu_aspirin2.mpu.data_rates.rates.r);
207  VECT3_ASSIGN(accel,
208  -imu_aspirin2.mpu.data_accel.vect.y,
209  imu_aspirin2.mpu.data_accel.vect.x,
210  imu_aspirin2.mpu.data_accel.vect.z);
211 #if !ASPIRIN_2_DISABLE_MAG
212  VECT3_ASSIGN(mag_rot, -mag.y, mag.x, mag.z);
213 #endif
214 #else
215 
216  /* Handle real Aspirin IMU axis assignement. */
217 #ifdef LISA_M_LONGITUDINAL_X
218  RATES_ASSIGN(gyro,
219  imu_aspirin2.mpu.data_rates.rates.q,
220  -imu_aspirin2.mpu.data_rates.rates.p,
221  imu_aspirin2.mpu.data_rates.rates.r);
222  VECT3_ASSIGN(accel,
223  imu_aspirin2.mpu.data_accel.vect.y,
224  -imu_aspirin2.mpu.data_accel.vect.x,
225  imu_aspirin2.mpu.data_accel.vect.z);
226 #if !ASPIRIN_2_DISABLE_MAG
227  VECT3_ASSIGN(mag_rot, -mag.x, -mag.y, mag.z);
228 #endif
229 #else
230  RATES_COPY(gyro, imu_aspirin2.mpu.data_rates.rates);
231  VECT3_COPY(accel, imu_aspirin2.mpu.data_accel.vect);
232 #if !ASPIRIN_2_DISABLE_MAG
233  VECT3_ASSIGN(mag_rot, mag.y, -mag.x, mag.z)
234 #endif
235 #endif
236 #endif
237 #endif
238 
240 
241  AbiSendMsgIMU_GYRO_RAW(IMU_ASPIRIN2_ID, now_ts, &gyro, 1, IMU_ASPIRIN2_PERIODIC_FREQ, imu_aspirin2.mpu.temp);
242  AbiSendMsgIMU_ACCEL_RAW(IMU_ASPIRIN2_ID, now_ts, &accel, 1, IMU_ASPIRIN2_PERIODIC_FREQ, imu_aspirin2.mpu.temp);
243 #if !ASPIRIN_2_DISABLE_MAG
244  AbiSendMsgIMU_MAG_RAW(IMU_ASPIRIN2_ID, now_ts, &mag_rot);
245 #endif
246  }
247 }
248 
249 // hack with waiting to avoid creating another event loop to check the mag config status
250 static inline void mpu_set_and_wait(Mpu60x0ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
251 {
252  mpu_set(mpu, _reg, _val);
254 }
255 
260 {
261  // wait before starting the configuration of the HMC58xx mag
262  // doing to early may void the mode configuration
264  return false;
265  }
266 
270  mpu_set_and_wait(mpu_set, mpu, MPU60X0_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
271 
273 
277  mpu_set_and_wait(mpu_set, mpu, MPU60X0_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
278 
280 
284  mpu_set_and_wait(mpu_set, mpu, MPU60X0_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
285 
288  // Put the enable command as last.
290  (1 << 7) | // Slave 0 enable
291  (6 << 0)); // Read 6 bytes
292 
293  return true;
294 }
295 
297 {
298  while (!imu_aspirin2.slave4_ready) {
302  }
303  }
304 }
305 
307 {
308  if (bit_is_set(t->input_buf[1], MPU60X0_I2C_SLV4_DONE)) {
309  imu_aspirin2.slave4_ready = true;
310  } else {
311  imu_aspirin2.slave4_ready = false;
312  }
313  t->status = SPITransDone;
314 }
Main include for ABI (AirBorneInterface).
#define IMU_ASPIRIN2_ID
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:71
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:330
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
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
Register defs for Honeywell HMC5843 and HMC5883 magnetometers.
#define HMC58XX_REG_CFGB
Definition: hmc58xx_regs.h:35
#define HMC58XX_REG_CFGA
Definition: hmc58xx_regs.h:34
#define HMC58XX_REG_MODE
Definition: hmc58xx_regs.h:36
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
#define HMC58XX_REG_DATXM
Definition: hmc58xx_regs.h:37
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:521
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:491
Inertial Measurement Unit interface.
#define ASPIRIN_2_SPI_DEV
void imu_aspirin2_event(void)
#define ASPIRIN_2_MAG_STARTUP_DELAY
delay in seconds before starting to configure HMC58xx mag slave
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.
volatile bool slave4_ready
#define ASPIRIN_2_GYRO_RANGE
volatile uint8_t wait_slave4_rx_buf[2]
struct Mpu60x0_Spi mpu
#define ASPIRIN_2_ACCEL_RANGE
volatile uint8_t wait_slave4_tx_buf[1]
struct spi_transaction wait_slave4_trans
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
const struct Int32Rates MPU60X0_GYRO_SENS_FRAC[4][2]
Definition: mpu60x0.c:38
const struct Int32Vect3 MPU60X0_ACCEL_SENS_FRAC[4][2]
Definition: mpu60x0.c:56
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:135
uint8_t i2c_mst_delay
MPU I2C slaves delayed sample rate.
Definition: mpu60x0.h:160
enum Mpu60x0MstClk i2c_mst_clk
MPU I2C master clock speed.
Definition: mpu60x0.h:159
uint8_t nb_bytes
number of bytes to read starting with MPU60X0_REG_INT_STATUS
Definition: mpu60x0.h:147
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:156
struct Mpu60x0I2cSlave slaves[MPU60X0_I2C_NB_SLAVES]
I2C slaves.
Definition: mpu60x0.h:158
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:129
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
#define MPU60X0_I2C_SLV4_DONE
Definition: mpu60x0_regs.h:137
#define MPU60X0_REG_I2C_SLV0_ADDR
Definition: mpu60x0_regs.h:62
#define MPU60X0_REG_I2C_SLV4_REG
Definition: mpu60x0_regs.h:83
#define MPU60X0_REG_I2C_SLV4_CTRL
Definition: mpu60x0_regs.h:85
#define MPU60X0_REG_I2C_MST_STATUS
Definition: mpu60x0_regs.h:59
#define MPU60X0_REG_I2C_SLV0_REG
Definition: mpu60x0_regs.h:63
#define MPU60X0_REG_I2C_SLV0_CTRL
Definition: mpu60x0_regs.h:64
#define MPU60X0_SPI_READ
Definition: mpu60x0_regs.h:35
#define MPU60X0_REG_I2C_SLV4_ADDR
Definition: mpu60x0_regs.h:82
@ MPU60X0_MST_CLK_400KHZ
Definition: mpu60x0_regs.h:195
#define MPU60X0_REG_I2C_SLV4_DO
Definition: mpu60x0_regs.h:84
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
Definition: mpu60x0_spi.c:128
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu60x0_spi.c:31
volatile bool data_available
data ready flag
Definition: mpu60x0_spi.h:56
uint8_t data_ext[MPU60X0_BUFFER_EXT_LEN]
Definition: mpu60x0_spi.h:66
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
struct spi_transaction spi_trans
Definition: mpu60x0_spi.h:53
float temp
temperature in degrees Celcius
Definition: mpu60x0_spi.h:65
union Mpu60x0_Spi::@342 data_rates
struct spi_periph * spi_p
Definition: mpu60x0_spi.h:52
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_spi.h:78
union Mpu60x0_Spi::@341 data_accel
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
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