Paparazzi UAS  v5.8.2_stable-0-g6260b7c
Paparazzi is a free software Unmanned Aircraft System.
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Modules Pages
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 
27 #include "subsystems/imu.h"
28 #include "subsystems/abi.h"
29 #include "mcu_periph/sys_time.h"
30 #include "mcu_periph/spi.h"
32 
33 /* defaults suitable for Lisa */
34 #ifndef ASPIRIN_2_SPI_SLAVE_IDX
35 #define ASPIRIN_2_SPI_SLAVE_IDX SPI_SLAVE2
36 #endif
37 PRINT_CONFIG_VAR(ASPIRIN_2_SPI_SLAVE_IDX)
38 
39 #ifndef ASPIRIN_2_SPI_DEV
40 #define ASPIRIN_2_SPI_DEV spi2
41 #endif
42 PRINT_CONFIG_VAR(ASPIRIN_2_SPI_DEV)
43 
44 /* MPU60x0 gyro/accel internal lowpass frequency */
45 #if !defined ASPIRIN_2_LOWPASS_FILTER && !defined ASPIRIN_2_SMPLRT_DIV
46 #if (PERIODIC_FREQUENCY == 60) || (PERIODIC_FREQUENCY == 120)
47 /* Accelerometer: Bandwidth 44Hz, Delay 4.9ms
48  * Gyroscope: Bandwidth 42Hz, Delay 4.8ms sampling 1kHz
49  */
50 #define ASPIRIN_2_LOWPASS_FILTER MPU60X0_DLPF_42HZ
51 #define ASPIRIN_2_SMPLRT_DIV 9
52 PRINT_CONFIG_MSG("Gyro/Accel output rate is 100Hz at 1kHz internal sampling")
53 #elif PERIODIC_FREQUENCY == 512
54 /* Accelerometer: Bandwidth 260Hz, Delay 0ms
55  * Gyroscope: Bandwidth 256Hz, Delay 0.98ms sampling 8kHz
56  */
57 #define ASPIRIN_2_LOWPASS_FILTER MPU60X0_DLPF_256HZ
58 #define ASPIRIN_2_SMPLRT_DIV 3
59 PRINT_CONFIG_MSG("Gyro/Accel output rate is 2kHz at 8kHz internal sampling")
60 #else
61 #error Non-default PERIODIC_FREQUENCY: please define ASPIRIN_2_LOWPASS_FILTER and ASPIRIN_2_SMPLRT_DIV.
62 #endif
63 #endif
64 PRINT_CONFIG_VAR(ASPIRIN_2_LOWPASS_FILTER)
65 PRINT_CONFIG_VAR(ASPIRIN_2_SMPLRT_DIV)
66 
67 #ifndef ASPIRIN_2_GYRO_RANGE
68 #define ASPIRIN_2_GYRO_RANGE MPU60X0_GYRO_RANGE_2000
69 #endif
70 PRINT_CONFIG_VAR(ASPIRIN_2_GYRO_RANGE)
71 
72 #ifndef ASPIRIN_2_ACCEL_RANGE
73 #define ASPIRIN_2_ACCEL_RANGE MPU60X0_ACCEL_RANGE_16G
74 #endif
75 PRINT_CONFIG_VAR(ASPIRIN_2_ACCEL_RANGE)
76 
77 
78 /* HMC58XX default conf */
79 #ifndef HMC58XX_DO
80 #define HMC58XX_DO 0x6 // Data Output Rate (6 -> 75Hz with HMC5883)
81 #endif
82 #ifndef HMC58XX_MS
83 #define HMC58XX_MS 0x0 // Measurement configuration
84 #endif
85 #ifndef HMC58XX_GN
86 #define HMC58XX_GN 0x1 // Gain configuration (1 -> +- 1 Gauss)
87 #endif
88 #ifndef HMC58XX_MD
89 #define HMC58XX_MD 0x0 // Continious measurement mode
90 #endif
91 
92 #define HMC58XX_CRA ((HMC58XX_DO<<2)|(HMC58XX_MS))
93 #define HMC58XX_CRB (HMC58XX_GN<<5)
94 
96 #ifndef ASPIRIN_2_MAG_STARTUP_DELAY
97 #define ASPIRIN_2_MAG_STARTUP_DELAY 1.5
98 #endif
99 
101 
102 void mpu_wait_slave4_ready(void);
105 
106 void imu_impl_init(void)
107 {
109  // change the default configuration
110  imu_aspirin2.mpu.config.smplrt_div = ASPIRIN_2_SMPLRT_DIV;
111  imu_aspirin2.mpu.config.dlpf_cfg = ASPIRIN_2_LOWPASS_FILTER;
114 
115  /* read 15 bytes for status, accel, gyro + 6 bytes for mag slave */
117 
118  /* use mag if not disabled */
119 #if !ASPIRIN_2_DISABLE_MAG
120  /* HMC5883 magnetometer as I2C slave */
122 
123  /* set function to configure mag */
125 
126  /* Set MPU I2C master clock */
128  /* Enable I2C slave0 delayed sample rate */
130 
131 
132  /* configure spi transaction for wait_slave4 */
138 
147 
150 #endif
151 }
152 
153 
154 void imu_periodic(void)
155 {
157 }
158 
159 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
160 
162 {
163  uint32_t now_ts = get_sys_time_usec();
164 
167 #if !ASPIRIN_2_DISABLE_MAG
168  /* HMC5883 has xzy order of axes in returned data */
169  struct Int32Vect3 mag;
173 #endif
174 
175  /* Handle axis assignement for Lisa/S integrated Aspirin like IMU. */
176 #ifdef LISA_S
177 #ifdef LISA_S_UPSIDE_DOWN
179  imu_aspirin2.mpu.data_rates.rates.p,
180  -imu_aspirin2.mpu.data_rates.rates.q,
181  -imu_aspirin2.mpu.data_rates.rates.r);
183  imu_aspirin2.mpu.data_accel.vect.x,
184  -imu_aspirin2.mpu.data_accel.vect.y,
185  -imu_aspirin2.mpu.data_accel.vect.z);
186  VECT3_ASSIGN(imu.mag_unscaled, mag.x, -mag.y, -mag.z);
187 #else
190 #if !ASPIRIN_2_DISABLE_MAG
192 #endif
193 #endif
194 #else
195 
196  /* Handle axis assignement for Lisa/M or Lisa/MX V2.1 integrated Aspirin like
197  * IMU.
198  */
199 #ifdef LISA_M_OR_MX_21
201  -imu_aspirin2.mpu.data_rates.rates.q,
202  imu_aspirin2.mpu.data_rates.rates.p,
203  imu_aspirin2.mpu.data_rates.rates.r);
205  -imu_aspirin2.mpu.data_accel.vect.y,
206  imu_aspirin2.mpu.data_accel.vect.x,
207  imu_aspirin2.mpu.data_accel.vect.z);
208 #if !ASPIRIN_2_DISABLE_MAG
209  VECT3_ASSIGN(imu.mag_unscaled, -mag.y, mag.x, mag.z);
210 #endif
211 #else
212 
213  /* Handle real Aspirin IMU axis assignement. */
214 #ifdef LISA_M_LONGITUDINAL_X
216  imu_aspirin2.mpu.data_rates.rates.q,
217  -imu_aspirin2.mpu.data_rates.rates.p,
218  imu_aspirin2.mpu.data_rates.rates.r);
220  imu_aspirin2.mpu.data_accel.vect.y,
221  -imu_aspirin2.mpu.data_accel.vect.x,
222  imu_aspirin2.mpu.data_accel.vect.z);
223 #if !ASPIRIN_2_DISABLE_MAG
224  VECT3_ASSIGN(imu.mag_unscaled, -mag.x, -mag.y, mag.z);
225 #endif
226 #else
229 #if !ASPIRIN_2_DISABLE_MAG
230  VECT3_ASSIGN(imu.mag_unscaled, mag.y, -mag.x, mag.z)
231 #endif
232 #endif
233 #endif
234 #endif
235 
237 
240  AbiSendMsgIMU_GYRO_INT32(IMU_ASPIRIN2_ID, now_ts, &imu.gyro);
241  AbiSendMsgIMU_ACCEL_INT32(IMU_ASPIRIN2_ID, now_ts, &imu.accel);
242 #if !ASPIRIN_2_DISABLE_MAG
243  imu_scale_mag(&imu);
244  AbiSendMsgIMU_MAG_INT32(IMU_ASPIRIN2_ID, now_ts, &imu.mag);
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)) {
310  } else {
312  }
313  t->status = SPITransDone;
314 }
volatile bool_t slave4_ready
void imu_periodic(void)
optional.
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:149
#define HMC58XX_MD
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu60x0_spi.c:31
#define MPU60X0_REG_I2C_SLV0_REG
Definition: mpu60x0_regs.h:61
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:153
struct ImuAspirin2Spi imu_aspirin2
uint8_t data_ext[MPU60X0_BUFFER_EXT_LEN]
Definition: mpu60x0_spi.h:66
#define MPU60X0_I2C_SLV4_DONE
Definition: mpu60x0_regs.h:131
uint16_t output_length
number of data words to write
Definition: spi.h:146
void imu_scale_gyro(struct Imu *_imu)
Definition: ahrs_gx3.c:349
#define MPU60X0_REG_I2C_SLV0_CTRL
Definition: mpu60x0_regs.h:62
void imu_scale_accel(struct Imu *_imu)
Definition: ahrs_gx3.c:350
volatile bool_t data_available
data ready flag
Definition: mpu60x0_spi.h:56
CPHA = 1.
Definition: spi.h:69
void mpu_wait_slave4_ready(void)
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
Definition: mpu60x0_spi.c:100
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:139
volatile uint8_t wait_slave4_tx_buf[1]
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:124
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:329
Main include for ABI (AirBorneInterface).
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:74
SPI transaction structure.
Definition: spi.h:142
#define ASPIRIN_2_ACCEL_RANGE
#define HMC58XX_REG_CFGA
Definition: hmc58xx_regs.h:34
CPOL = 1.
Definition: spi.h:78
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:152
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:76
#define ASPIRIN_2_GYRO_RANGE
#define ASPIRIN_2_SPI_DEV
bool_t imu_aspirin2_configure_mag_slave(Mpu60x0ConfigSet mpu_set, void *mpu)
function to configure hmc5883 mag
union Mpu60x0_Spi::@43 data_accel
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:144
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:124
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:77
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:148
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:75
#define FALSE
Definition: std.h:5
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:70
static uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.h:39
Architecture independent SPI (Serial Peripheral Interface) API.
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:52
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:150
enum Mpu60x0MstClk i2c_mst_clk
MPU I2C master clock speed.
Definition: mpu60x0.h:91
#define MPU60X0_SPI_READ
Definition: mpu60x0_regs.h:35
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:50
#define TRUE
Definition: std.h:4
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:43
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_spi.h:78
Definition: spi.h:84
bool_t spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit a spi transaction.
Definition: spi_arch.c:48
Architecture independent timing functions.
struct spi_transaction spi_trans
Definition: mpu60x0_spi.h:53
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:155
struct Imu imu
global IMU state
Definition: imu_aspirin2.c:43
void imu_aspirin2_event(void)
unsigned long uint32_t
Definition: types.h:18
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:89
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
struct Mpu60x0_Spi mpu
#define MPU60X0_REG_I2C_SLV4_REG
Definition: mpu60x0_regs.h:81
struct spi_periph * spi_p
Definition: mpu60x0_spi.h:52
#define HMC58XX_REG_DATXM
Definition: hmc58xx_regs.h:37
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:44
Inertial Measurement Unit interface.
struct Mpu60x0I2cSlave slaves[5]
I2C slaves.
Definition: mpu60x0.h:90
uint16_t input_length
number of data words to read
Definition: spi.h:145
static void mpu_set_and_wait(Mpu60x0ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
volatile uint8_t wait_slave4_rx_buf[2]
#define IMU_ASPIRIN2_ID
#define MPU60X0_REG_I2C_SLV4_ADDR
Definition: mpu60x0_regs.h:80
Register defs for Honeywell HMC5843 and HMC5883 magnetometers.
#define MPU60X0_REG_I2C_SLV0_ADDR
Definition: mpu60x0_regs.h:60
#define Int16FromBuf(_buf, _idx)
struct spi_transaction wait_slave4_trans
#define MPU60X0_REG_I2C_SLV4_CTRL
Definition: mpu60x0_regs.h:83
#define HMC58XX_CRA
unsigned char uint8_t
Definition: types.h:14
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:351
#define ASPIRIN_2_SPI_SLAVE_IDX
slave is selected before transaction and unselected after
Definition: spi.h:57
#define MPU60X0_REG_I2C_MST_STATUS
Definition: mpu60x0_regs.h:57
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:151
uint8_t i2c_mst_delay
MPU I2C slaves delayed sample rate.
Definition: mpu60x0.h:92
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:147
Definition: spi.h:119
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:143
union Mpu60x0_Spi::@44 data_rates
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:51
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:154
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:336
#define HMC58XX_REG_CFGB
Definition: hmc58xx_regs.h:35
#define MPU60X0_REG_I2C_SLV4_DO
Definition: mpu60x0_regs.h:82
#define HMC58XX_CRB
void mpu_wait_slave4_ready_cb(struct spi_transaction *t)
uint8_t nb_bytes
number of bytes to read starting with MPU60X0_REG_INT_STATUS
Definition: mpu60x0.h:80
#define ASPIRIN_2_MAG_STARTUP_DELAY
delay in seconds before starting to configure HMC58xx mag slave
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:64
#define HMC58XX_REG_MODE
Definition: hmc58xx_regs.h:36
enum SPITransactionStatus status
Definition: spi.h:156
void imu_impl_init(void)
must be defined by underlying hardware
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:42