Paparazzi UAS  v5.18.0_stable
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 
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 PRINT_CONFIG_VAR(ASPIRIN_2_GYRO_RANGE)
68 PRINT_CONFIG_VAR(ASPIRIN_2_ACCEL_RANGE)
69 
70 
71 /* HMC58XX default conf */
72 #ifndef HMC58XX_DO
73 #define HMC58XX_DO 0x6 // Data Output Rate (6 -> 75Hz with HMC5883)
74 #endif
75 #ifndef HMC58XX_MS
76 #define HMC58XX_MS 0x0 // Measurement configuration
77 #endif
78 #ifndef HMC58XX_GN
79 #define HMC58XX_GN 0x1 // Gain configuration (1 -> +- 1 Gauss)
80 #endif
81 #ifndef HMC58XX_MD
82 #define HMC58XX_MD 0x0 // Continious measurement mode
83 #endif
84 
85 #define HMC58XX_CRA ((HMC58XX_DO<<2)|(HMC58XX_MS))
86 #define HMC58XX_CRB (HMC58XX_GN<<5)
87 
89 #ifndef ASPIRIN_2_MAG_STARTUP_DELAY
90 #define ASPIRIN_2_MAG_STARTUP_DELAY 1.5
91 #endif
92 
94 
95 void mpu_wait_slave4_ready(void);
98 
100 {
102  // change the default configuration
103  imu_aspirin2.mpu.config.smplrt_div = ASPIRIN_2_SMPLRT_DIV;
104  imu_aspirin2.mpu.config.dlpf_cfg = ASPIRIN_2_LOWPASS_FILTER;
107 
108  /* read 15 bytes for status, accel, gyro + 6 bytes for mag slave */
110 
111  /* use mag if not disabled */
112 #if !ASPIRIN_2_DISABLE_MAG
113  /* HMC5883 magnetometer as I2C slave */
115 
116  /* set function to configure mag */
118 
119  /* Set MPU I2C master clock */
121  /* Enable I2C slave0 delayed sample rate */
123 
124 
125  /* configure spi transaction for wait_slave4 */
131 
140 
142  imu_aspirin2.slave4_ready = false;
143 #endif
144 }
145 
146 
148 {
150 }
151 
152 #define Int16FromBuf(_buf,_idx) ((int16_t)((_buf[_idx]<<8) | _buf[_idx+1]))
153 
155 {
156  uint32_t now_ts = get_sys_time_usec();
157 
160 #if !ASPIRIN_2_DISABLE_MAG
161  /* HMC5883 has xzy order of axes in returned data */
162  struct Int32Vect3 mag;
166 #endif
167 
168  /* Handle axis assignement for Lisa/S integrated Aspirin like IMU. */
169 #ifdef LISA_S
170 #ifdef LISA_S_UPSIDE_DOWN
172  imu_aspirin2.mpu.data_rates.rates.p,
173  -imu_aspirin2.mpu.data_rates.rates.q,
174  -imu_aspirin2.mpu.data_rates.rates.r);
176  imu_aspirin2.mpu.data_accel.vect.x,
177  -imu_aspirin2.mpu.data_accel.vect.y,
178  -imu_aspirin2.mpu.data_accel.vect.z);
179 #if !ASPIRIN_2_DISABLE_MAG
180  VECT3_ASSIGN(imu.mag_unscaled, mag.x, -mag.y, -mag.z);
181 #endif
182 #else
185 #if !ASPIRIN_2_DISABLE_MAG
187 #endif
188 #endif
189 #else
190 
191  /* Handle axis assignement for Lisa/M or Lisa/MX V2.1 integrated Aspirin like
192  * IMU.
193  */
194 #ifdef LISA_M_OR_MX_21
196  -imu_aspirin2.mpu.data_rates.rates.q,
197  imu_aspirin2.mpu.data_rates.rates.p,
198  imu_aspirin2.mpu.data_rates.rates.r);
200  -imu_aspirin2.mpu.data_accel.vect.y,
201  imu_aspirin2.mpu.data_accel.vect.x,
202  imu_aspirin2.mpu.data_accel.vect.z);
203 #if !ASPIRIN_2_DISABLE_MAG
204  VECT3_ASSIGN(imu.mag_unscaled, -mag.y, mag.x, mag.z);
205 #endif
206 #else
207 
208  /* Handle real Aspirin IMU axis assignement. */
209 #ifdef LISA_M_LONGITUDINAL_X
211  imu_aspirin2.mpu.data_rates.rates.q,
212  -imu_aspirin2.mpu.data_rates.rates.p,
213  imu_aspirin2.mpu.data_rates.rates.r);
215  imu_aspirin2.mpu.data_accel.vect.y,
216  -imu_aspirin2.mpu.data_accel.vect.x,
217  imu_aspirin2.mpu.data_accel.vect.z);
218 #if !ASPIRIN_2_DISABLE_MAG
219  VECT3_ASSIGN(imu.mag_unscaled, -mag.x, -mag.y, mag.z);
220 #endif
221 #else
224 #if !ASPIRIN_2_DISABLE_MAG
225  VECT3_ASSIGN(imu.mag_unscaled, mag.y, -mag.x, mag.z)
226 #endif
227 #endif
228 #endif
229 #endif
230 
232 
235  AbiSendMsgIMU_GYRO_INT32(IMU_ASPIRIN2_ID, now_ts, &imu.gyro);
236  AbiSendMsgIMU_ACCEL_INT32(IMU_ASPIRIN2_ID, now_ts, &imu.accel);
237 #if !ASPIRIN_2_DISABLE_MAG
238  imu_scale_mag(&imu);
239  AbiSendMsgIMU_MAG_INT32(IMU_ASPIRIN2_ID, now_ts, &imu.mag);
240 #endif
241  }
242 }
243 
244 // hack with waiting to avoid creating another event loop to check the mag config status
245 static inline void mpu_set_and_wait(Mpu60x0ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
246 {
247  mpu_set(mpu, _reg, _val);
249 }
250 
255 {
256  // wait before starting the configuration of the HMC58xx mag
257  // doing to early may void the mode configuration
259  return false;
260  }
261 
265  mpu_set_and_wait(mpu_set, mpu, MPU60X0_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
266 
268 
272  mpu_set_and_wait(mpu_set, mpu, MPU60X0_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
273 
275 
279  mpu_set_and_wait(mpu_set, mpu, MPU60X0_REG_I2C_SLV4_CTRL, (1 << 7)); // Slave 4 enable
280 
283  // Put the enable command as last.
285  (1 << 7) | // Slave 0 enable
286  (6 << 0)); // Read 6 bytes
287 
288  return true;
289 }
290 
292 {
293  while (!imu_aspirin2.slave4_ready) {
297  }
298  }
299 }
300 
302 {
303  if (bit_is_set(t->input_buf[1], MPU60X0_I2C_SLV4_DONE)) {
304  imu_aspirin2.slave4_ready = true;
305  } else {
306  imu_aspirin2.slave4_ready = false;
307  }
308  t->status = SPITransDone;
309 }
Imu::gyro_unscaled
struct Int32Rates gyro_unscaled
unscaled gyroscope measurements
Definition: imu.h:46
Mpu60x0Config::nb_bytes
uint8_t nb_bytes
number of bytes to read starting with MPU60X0_REG_INT_STATUS
Definition: mpu60x0.h:147
Mpu60x0_Spi::spi_trans
struct spi_transaction spi_trans
Definition: mpu60x0_spi.h:53
spi_transaction::cdiv
enum SPIClockDiv cdiv
prescaler of main clock to use as SPI clock
Definition: spi.h:159
Mpu60x0Config::smplrt_div
uint8_t smplrt_div
Sample rate divider.
Definition: mpu60x0.h:140
spi_transaction::cpol
enum SPIClockPolarity cpol
clock polarity control
Definition: spi.h:155
MPU60X0_REG_I2C_SLV0_CTRL
#define MPU60X0_REG_I2C_SLV0_CTRL
Definition: mpu60x0_regs.h:64
IMU_ASPIRIN2_ID
#define IMU_ASPIRIN2_ID
Definition: abi_sender_ids.h:287
HMC58XX_REG_MODE
#define HMC58XX_REG_MODE
Definition: hmc58xx_regs.h:36
ASPIRIN_2_GYRO_RANGE
#define ASPIRIN_2_GYRO_RANGE
Definition: imu_aspirin_2_spi.h:37
Mpu60x0Config::gyro_range
enum Mpu60x0GyroRanges gyro_range
deg/s Range
Definition: mpu60x0.h:143
ImuAspirin2Spi::mpu
struct Mpu60x0_Spi mpu
Definition: imu_aspirin_2_spi.h:71
abi.h
Imu::accel
struct Int32Vect3 accel
accelerometer measurements in m/s^2 in BFP with INT32_ACCEL_FRAC
Definition: imu.h:39
mpu_wait_slave4_ready
void mpu_wait_slave4_ready(void)
Definition: imu_aspirin_2_spi.c:291
MPU60X0_REG_I2C_SLV0_REG
#define MPU60X0_REG_I2C_SLV0_REG
Definition: mpu60x0_regs.h:63
spi_transaction::output_length
uint16_t output_length
number of data words to write
Definition: spi.h:152
imu_aspirin2_event
void imu_aspirin2_event(void)
Definition: imu_aspirin_2_spi.c:154
MPU60X0_I2C_SLV4_DONE
#define MPU60X0_I2C_SLV4_DONE
Definition: mpu60x0_regs.h:137
get_sys_time_float
static float get_sys_time_float(void)
Get the time in seconds since startup.
Definition: sys_time.h:129
spi.h
spi_transaction
SPI transaction structure.
Definition: spi.h:148
Int32Vect3::z
int32_t z
Definition: pprz_algebra_int.h:91
imu_aspirin2_init
void imu_aspirin2_init(void)
Definition: imu_aspirin_2_spi.c:99
Mpu60x0_Spi::data_ext
uint8_t data_ext[MPU60X0_BUFFER_EXT_LEN]
Definition: mpu60x0_spi.h:66
uint32_t
unsigned long uint32_t
Definition: types.h:18
MPU60X0_SPI_READ
#define MPU60X0_SPI_READ
Definition: mpu60x0_regs.h:35
Mpu60x0Config::i2c_mst_clk
enum Mpu60x0MstClk i2c_mst_clk
MPU I2C master clock speed.
Definition: mpu60x0.h:159
imu_aspirin2_configure_mag_slave
bool imu_aspirin2_configure_mag_slave(Mpu60x0ConfigSet mpu_set, void *mpu)
function to configure hmc5883 mag
Definition: imu_aspirin_2_spi.c:254
Mpu60x0Config::accel_range
enum Mpu60x0AccelRanges accel_range
g Range
Definition: mpu60x0.h:144
Mpu60x0Config::dlpf_cfg
enum Mpu60x0DLPF dlpf_cfg
Digital Low Pass Filter.
Definition: mpu60x0.h:141
mpu_wait_slave4_ready_cb
void mpu_wait_slave4_ready_cb(struct spi_transaction *t)
Definition: imu_aspirin_2_spi.c:301
SPISelectUnselect
@ SPISelectUnselect
slave is selected before transaction and unselected after
Definition: spi.h:63
ASPIRIN_2_SPI_DEV
#define ASPIRIN_2_SPI_DEV
Definition: imu_aspirin_2_spi.c:40
SPITransSuccess
@ SPITransSuccess
Definition: spi.h:99
ASPIRIN_2_SPI_SLAVE_IDX
#define ASPIRIN_2_SPI_SLAVE_IDX
Definition: imu_aspirin_2_spi.c:35
spi_transaction::bitorder
enum SPIBitOrder bitorder
MSB/LSB order.
Definition: spi.h:158
MPU60X0_MST_CLK_400KHZ
@ MPU60X0_MST_CLK_400KHZ
Definition: mpu60x0_regs.h:195
HMC58XX_CRA
#define HMC58XX_CRA
Definition: imu_aspirin_2_spi.c:85
Mpu60x0ConfigSet
void(* Mpu60x0ConfigSet)(void *mpu, uint8_t _reg, uint8_t _val)
Configuration function prototype.
Definition: mpu60x0.h:129
Imu::accel_unscaled
struct Int32Vect3 accel_unscaled
unscaled accelerometer measurements
Definition: imu.h:47
spi_transaction::output_buf
volatile uint8_t * output_buf
pointer to transmit buffer for DMA
Definition: spi.h:150
spi_transaction::select
enum SPISlaveSelect select
slave selection behavior
Definition: spi.h:154
hmc58xx_regs.h
get_sys_time_usec
uint32_t get_sys_time_usec(void)
Get the time in microseconds since startup.
Definition: sys_time_arch.c:68
HMC58XX_ADDR
#define HMC58XX_ADDR
Definition: hmc58xx_regs.h:31
imu.h
RATES_ASSIGN
#define RATES_ASSIGN(_ra, _p, _q, _r)
Definition: pprz_algebra.h:330
MPU60X0_REG_I2C_SLV4_REG
#define MPU60X0_REG_I2C_SLV4_REG
Definition: mpu60x0_regs.h:83
HMC58XX_REG_CFGA
#define HMC58XX_REG_CFGA
Definition: hmc58xx_regs.h:34
SPIMSBFirst
@ SPIMSBFirst
Definition: spi.h:112
Mpu60x0I2cSlave::configure
Mpu60x0I2cSlaveConfigure configure
Definition: mpu60x0.h:135
Mpu60x0_Spi::spi_p
struct spi_periph * spi_p
Definition: mpu60x0_spi.h:52
spi_transaction::cpha
enum SPIClockPhase cpha
clock phase control
Definition: spi.h:156
imu_aspirin2_periodic
void imu_aspirin2_periodic(void)
Definition: imu_aspirin_2_spi.c:147
spi_transaction::after_cb
SPICallback after_cb
NULL or function called after the transaction.
Definition: spi.h:161
spi_submit
bool spi_submit(struct spi_periph *p, struct spi_transaction *t)
Submit SPI transaction.
Definition: spi_arch.c:511
Int32Vect3
Definition: pprz_algebra_int.h:88
MPU60X0_REG_I2C_SLV0_ADDR
#define MPU60X0_REG_I2C_SLV0_ADDR
Definition: mpu60x0_regs.h:62
sys_time.h
Architecture independent timing functions.
ImuAspirin2Spi
Definition: imu_aspirin_2_spi.h:70
HMC58XX_REG_DATXM
#define HMC58XX_REG_DATXM
Definition: hmc58xx_regs.h:37
SPICpolIdleHigh
@ SPICpolIdleHigh
CPOL = 1.
Definition: spi.h:84
imu_scale_gyro
void imu_scale_gyro(struct Imu *_imu)
Definition: imu_vectornav.c:43
uint8_t
unsigned char uint8_t
Definition: types.h:14
Int32Vect3::y
int32_t y
Definition: pprz_algebra_int.h:90
MPU60X0_REG_I2C_SLV4_ADDR
#define MPU60X0_REG_I2C_SLV4_ADDR
Definition: mpu60x0_regs.h:82
SPIDss8bit
@ SPIDss8bit
Definition: spi.h:90
imu
struct Imu imu
global IMU state
Definition: imu.c:108
SPIDiv64
@ SPIDiv64
Definition: spi.h:125
Mpu60x0Config::nb_slaves
uint8_t nb_slaves
number of used I2C slaves
Definition: mpu60x0.h:156
PRINT_CONFIG_MSG
PRINT_CONFIG_MSG("USE_INS_NAV_INIT defaulting to TRUE")
mpu_set_and_wait
static void mpu_set_and_wait(Mpu60x0ConfigSet mpu_set, void *mpu, uint8_t _reg, uint8_t _val)
Definition: imu_aspirin_2_spi.c:245
imu_aspirin2
struct ImuAspirin2Spi imu_aspirin2
Definition: imu_aspirin_2_spi.c:93
mpu60x0_spi_periodic
static void mpu60x0_spi_periodic(struct Mpu60x0_Spi *mpu)
convenience function: read or start configuration if not already initialized
Definition: mpu60x0_spi.h:78
spi_transaction::input_length
uint16_t input_length
number of data words to read
Definition: spi.h:151
spi_transaction::dss
enum SPIDataSizeSelect dss
data transfer word size
Definition: spi.h:157
Int16FromBuf
#define Int16FromBuf(_buf, _idx)
Definition: imu_aspirin_2_spi.c:152
spi_transaction::slave_idx
uint8_t slave_idx
slave id: SPI_SLAVE0 to SPI_SLAVE4
Definition: spi.h:153
MPU60X0_REG_I2C_SLV4_DO
#define MPU60X0_REG_I2C_SLV4_DO
Definition: mpu60x0_regs.h:84
SPITransDone
@ SPITransDone
Definition: spi.h:101
Mpu60x0_Spi::data_accel
union Mpu60x0_Spi::@333 data_accel
imu_scale_mag
void imu_scale_mag(struct Imu *_imu)
Definition: ahrs_gx3.c:352
Mpu60x0Config::i2c_mst_delay
uint8_t i2c_mst_delay
MPU I2C slaves delayed sample rate.
Definition: mpu60x0.h:160
Mpu60x0Config::slaves
struct Mpu60x0I2cSlave slaves[MPU60X0_I2C_NB_SLAVES]
I2C slaves.
Definition: mpu60x0.h:158
spi_transaction::input_buf
volatile uint8_t * input_buf
pointer to receive buffer for DMA
Definition: spi.h:149
Mpu60x0_Spi::data_available
volatile bool data_available
data ready flag
Definition: mpu60x0_spi.h:56
SPICphaEdge2
@ SPICphaEdge2
CPHA = 1.
Definition: spi.h:75
Int32Vect3::x
int32_t x
Definition: pprz_algebra_int.h:89
Imu::mag
struct Int32Vect3 mag
magnetometer measurements scaled to 1 in BFP with INT32_MAG_FRAC
Definition: imu.h:40
Imu::mag_unscaled
struct Int32Vect3 mag_unscaled
unscaled magnetometer measurements
Definition: imu.h:48
ASPIRIN_2_ACCEL_RANGE
#define ASPIRIN_2_ACCEL_RANGE
Definition: imu_aspirin_2_spi.h:41
ImuAspirin2Spi::slave4_ready
volatile bool slave4_ready
Definition: imu_aspirin_2_spi.h:76
spi_transaction::before_cb
SPICallback before_cb
NULL or function called before the transaction.
Definition: spi.h:160
MPU60X0_REG_I2C_SLV4_CTRL
#define MPU60X0_REG_I2C_SLV4_CTRL
Definition: mpu60x0_regs.h:85
HMC58XX_CRB
#define HMC58XX_CRB
Definition: imu_aspirin_2_spi.c:86
VECT3_ASSIGN
#define VECT3_ASSIGN(_a, _x, _y, _z)
Definition: pprz_algebra.h:125
RATES_COPY
#define RATES_COPY(_a, _b)
Definition: pprz_algebra.h:337
HMC58XX_REG_CFGB
#define HMC58XX_REG_CFGB
Definition: hmc58xx_regs.h:35
Mpu60x0_Spi::data_rates
union Mpu60x0_Spi::@334 data_rates
Mpu60x0_Spi::config
struct Mpu60x0Config config
Definition: mpu60x0_spi.h:67
MPU60X0_REG_I2C_MST_STATUS
#define MPU60X0_REG_I2C_MST_STATUS
Definition: mpu60x0_regs.h:59
ImuAspirin2Spi::wait_slave4_trans
struct spi_transaction wait_slave4_trans
Definition: imu_aspirin_2_spi.h:73
mpu60x0_spi_init
void mpu60x0_spi_init(struct Mpu60x0_Spi *mpu, struct spi_periph *spi_p, uint8_t slave_idx)
Definition: mpu60x0_spi.c:31
spi_transaction::status
enum SPITransactionStatus status
Definition: spi.h:162
mpu60x0_spi_event
void mpu60x0_spi_event(struct Mpu60x0_Spi *mpu)
Definition: mpu60x0_spi.c:128
imu_scale_accel
void imu_scale_accel(struct Imu *_imu)
Definition: imu_vectornav.c:44
ASPIRIN_2_MAG_STARTUP_DELAY
#define ASPIRIN_2_MAG_STARTUP_DELAY
delay in seconds before starting to configure HMC58xx mag slave
Definition: imu_aspirin_2_spi.c:90
VECT3_COPY
#define VECT3_COPY(_a, _b)
Definition: pprz_algebra.h:140
ImuAspirin2Spi::wait_slave4_tx_buf
volatile uint8_t wait_slave4_tx_buf[1]
Definition: imu_aspirin_2_spi.h:74
ImuAspirin2Spi::wait_slave4_rx_buf
volatile uint8_t wait_slave4_rx_buf[2]
Definition: imu_aspirin_2_spi.h:75
Imu::gyro
struct Int32Rates gyro
gyroscope measurements in rad/s in BFP with INT32_RATE_FRAC
Definition: imu.h:38
HMC58XX_MD
#define HMC58XX_MD
Definition: imu_aspirin_2_spi.c:82