2015-05-18 21:27:42 +02:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_MPU9250 MPU9250 Functions
|
|
|
|
* @brief Deals with the hardware interface to the 9 DOF sensor.
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file pios_mp9250.c
|
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2014.
|
|
|
|
* @brief MPU9250 9-axis gyro, accel and mag chip
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
|
|
|
/*
|
|
|
|
* This program is free software; you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
|
|
|
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
|
|
* for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License along
|
|
|
|
* with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pios.h"
|
|
|
|
#include "pios_mpu9250.h"
|
|
|
|
#ifdef PIOS_INCLUDE_MPU9250
|
|
|
|
|
|
|
|
#include <pios_constants.h>
|
|
|
|
/* Global Variables */
|
|
|
|
|
|
|
|
enum pios_mpu9250_dev_magic {
|
|
|
|
PIOS_MPU9250_DEV_MAGIC = 0x9da9b3ed,
|
|
|
|
};
|
|
|
|
|
|
|
|
struct mpu9250_dev {
|
|
|
|
uint32_t spi_id;
|
|
|
|
uint32_t slave_num;
|
|
|
|
xQueueHandle queue;
|
|
|
|
const struct pios_mpu9250_cfg *cfg;
|
|
|
|
enum pios_mpu9250_range gyro_range;
|
|
|
|
enum pios_mpu9250_accel_range accel_range;
|
|
|
|
enum pios_mpu9250_filter filter;
|
|
|
|
enum pios_mpu9250_dev_magic magic;
|
|
|
|
float mag_sens_adj[PIOS_MPU9250_MAG_ASA_NB_BYTE];
|
|
|
|
};
|
|
|
|
|
|
|
|
#ifdef PIOS_MPU9250_ACCEL
|
2014-12-03 00:35:17 +01:00
|
|
|
#define PIOS_MPU9250_ACCEL_SAMPLES_BYTES (6)
|
2015-05-18 21:27:42 +02:00
|
|
|
#else
|
2014-12-03 00:35:17 +01:00
|
|
|
#define PIOS_MPU9250_ACCEL_SAMPLES_BYTES (0)
|
2015-05-18 21:27:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef PIOS_MPU9250_MAG
|
2014-12-03 00:35:17 +01:00
|
|
|
#define PIOS_MPU9250_MAG_SAMPLES_BYTES (8)
|
2015-05-18 21:27:42 +02:00
|
|
|
#else
|
2014-12-03 00:35:17 +01:00
|
|
|
#define PIOS_MPU9250_MAG_SAMPLES_BYTES (0)
|
2015-05-18 21:27:42 +02:00
|
|
|
#endif
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
#define PIOS_MPU9250_GYRO_SAMPLES_BYTES (6)
|
|
|
|
#define PIOS_MPU9250_TEMP_SAMPLES_BYTES (2)
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
#define PIOS_MPU9250_SAMPLES_BYTES \
|
|
|
|
(PIOS_MPU9250_ACCEL_SAMPLES_BYTES + \
|
|
|
|
PIOS_MPU9250_GYRO_SAMPLES_BYTES + \
|
|
|
|
PIOS_MPU9250_TEMP_SAMPLES_BYTES + \
|
|
|
|
PIOS_MPU9250_MAG_SAMPLES_BYTES)
|
2015-05-18 21:27:42 +02:00
|
|
|
|
|
|
|
#ifdef PIOS_MPU9250_ACCEL
|
2014-12-03 00:35:17 +01:00
|
|
|
#define PIOS_MPU9250_SENSOR_FIRST_REG PIOS_MPU9250_ACCEL_X_OUT_MSB
|
2015-05-18 21:27:42 +02:00
|
|
|
#else
|
2014-12-03 00:35:17 +01:00
|
|
|
#define PIOS_MPU9250_SENSOR_FIRST_REG PIOS_MPU9250_TEMP_OUT_MSB
|
2015-05-18 21:27:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(PIOS_MPU9250_MAG) && !defined(PIOS_MPU9250_ACCEL)
|
|
|
|
#error ERROR: PIOS_MPU9250_ACCEL not defined! THIS CONFIGURATION IS NOT SUPPORTED
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
uint8_t buffer[2 + PIOS_MPU9250_SAMPLES_BYTES];
|
|
|
|
struct {
|
|
|
|
uint8_t dummy;
|
|
|
|
#ifdef PIOS_MPU9250_ACCEL
|
|
|
|
uint8_t Accel_X_h;
|
|
|
|
uint8_t Accel_X_l;
|
|
|
|
uint8_t Accel_Y_h;
|
|
|
|
uint8_t Accel_Y_l;
|
|
|
|
uint8_t Accel_Z_h;
|
|
|
|
uint8_t Accel_Z_l;
|
|
|
|
#endif
|
|
|
|
uint8_t Temperature_h;
|
|
|
|
uint8_t Temperature_l;
|
|
|
|
uint8_t Gyro_X_h;
|
|
|
|
uint8_t Gyro_X_l;
|
|
|
|
uint8_t Gyro_Y_h;
|
|
|
|
uint8_t Gyro_Y_l;
|
|
|
|
uint8_t Gyro_Z_h;
|
|
|
|
uint8_t Gyro_Z_l;
|
|
|
|
#ifdef PIOS_MPU9250_MAG
|
|
|
|
uint8_t st1;
|
2014-12-03 00:35:17 +01:00
|
|
|
uint8_t Mag_X_l;
|
|
|
|
uint8_t Mag_X_h;
|
|
|
|
uint8_t Mag_Y_l;
|
|
|
|
uint8_t Mag_Y_h;
|
|
|
|
uint8_t Mag_Z_l;
|
|
|
|
uint8_t Mag_Z_h;
|
|
|
|
uint8_t st2;
|
2015-05-18 21:27:42 +02:00
|
|
|
#endif
|
|
|
|
} data;
|
|
|
|
} mpu9250_data_t;
|
|
|
|
|
|
|
|
|
|
|
|
#define GET_SENSOR_DATA(mpudataptr, sensor) (mpudataptr.data.sensor##_h << 8 | mpudataptr.data.sensor##_l)
|
|
|
|
|
|
|
|
// ! Global structure for this device device
|
|
|
|
static struct mpu9250_dev *dev;
|
|
|
|
volatile bool mpu9250_configured = false;
|
|
|
|
static mpu9250_data_t mpu9250_data;
|
|
|
|
|
|
|
|
// ! Private functions
|
|
|
|
static struct mpu9250_dev *PIOS_MPU9250_alloc(const struct pios_mpu9250_cfg *cfg);
|
|
|
|
static int32_t PIOS_MPU9250_Validate(struct mpu9250_dev *dev);
|
|
|
|
static void PIOS_MPU9250_Config(struct pios_mpu9250_cfg const *cfg);
|
|
|
|
static int32_t PIOS_MPU9250_SetReg(uint8_t address, uint8_t buffer);
|
|
|
|
static int32_t PIOS_MPU9250_GetReg(uint8_t address);
|
|
|
|
static void PIOS_MPU9250_SetSpeed(const bool fast);
|
|
|
|
static bool PIOS_MPU9250_HandleData();
|
|
|
|
static bool PIOS_MPU9250_ReadFifo(bool *woken);
|
|
|
|
static bool PIOS_MPU9250_ReadSensor(bool *woken);
|
|
|
|
#if defined(PIOS_MPU9250_MAG)
|
|
|
|
static int32_t PIOS_MPU9250_Mag_Test(void);
|
|
|
|
static int32_t PIOS_MPU9250_Mag_Init(void);
|
|
|
|
#endif
|
|
|
|
/**
|
|
|
|
* @brief Allocate a new device
|
|
|
|
*/
|
|
|
|
static struct mpu9250_dev *PIOS_MPU9250_alloc(const struct pios_mpu9250_cfg *cfg)
|
|
|
|
{
|
|
|
|
struct mpu9250_dev *mpu9250_dev;
|
|
|
|
|
|
|
|
mpu9250_dev = (struct mpu9250_dev *)pios_malloc(sizeof(*mpu9250_dev));
|
|
|
|
if (!mpu9250_dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpu9250_dev->magic = PIOS_MPU9250_DEV_MAGIC;
|
|
|
|
|
|
|
|
mpu9250_dev->queue = xQueueCreate(cfg->max_downsample + 1, sizeof(struct pios_mpu9250_data));
|
|
|
|
if (mpu9250_dev->queue == NULL) {
|
|
|
|
vPortFree(mpu9250_dev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mpu9250_dev;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Validate the handle to the spi device
|
|
|
|
* @returns 0 for valid device or -1 otherwise
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_Validate(struct mpu9250_dev *vdev)
|
|
|
|
{
|
|
|
|
if (vdev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (vdev->magic != PIOS_MPU9250_DEV_MAGIC) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (vdev->spi_id == 0) {
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize the MPU9250 3-axis gyro sensor.
|
|
|
|
* @return 0 for success, -1 for failure
|
|
|
|
*/
|
|
|
|
int32_t PIOS_MPU9250_Init(uint32_t spi_id, uint32_t slave_num, const struct pios_mpu9250_cfg *cfg)
|
|
|
|
{
|
|
|
|
dev = PIOS_MPU9250_alloc(cfg);
|
|
|
|
if (dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->spi_id = spi_id;
|
|
|
|
dev->slave_num = slave_num;
|
|
|
|
dev->cfg = cfg;
|
|
|
|
|
|
|
|
/* Configure the MPU9250 Sensor */
|
|
|
|
PIOS_MPU9250_Config(cfg);
|
|
|
|
|
|
|
|
/* Set up EXTI line */
|
|
|
|
PIOS_EXTI_Init(cfg->exti_cfg);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Initialize the MPU9250 3-axis gyro sensor
|
|
|
|
* \return none
|
|
|
|
* \param[in] PIOS_MPU9250_ConfigTypeDef struct to be used to configure sensor.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
static void PIOS_MPU9250_Config(struct pios_mpu9250_cfg const *cfg)
|
|
|
|
{
|
2014-12-03 00:35:17 +01:00
|
|
|
uint8_t power;
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
while (PIOS_MPU9250_Test() != 0) {
|
2015-05-18 21:27:42 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reset chip
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_PWR_MGMT_REG, PIOS_MPU9250_PWRMGMT_IMU_RST) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_DELAY_WaitmS(100);
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Wake up the chip
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_PWR_MGMT_REG, 0) != 0) {
|
|
|
|
;
|
|
|
|
}
|
2015-05-18 21:27:42 +02:00
|
|
|
// Reset sensors and fifo
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_USER_CTRL_REG,
|
2014-12-03 00:35:17 +01:00
|
|
|
PIOS_MPU9250_USERCTL_DIS_I2C |
|
2015-05-18 21:27:42 +02:00
|
|
|
PIOS_MPU9250_USERCTL_SIG_COND |
|
|
|
|
PIOS_MPU9250_USERCTL_FIFO_RST) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
PIOS_DELAY_WaitmS(100);
|
|
|
|
|
|
|
|
// Power management configuration
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_PWR_MGMT_REG, cfg->Pwr_mgmt_clk) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_USER_CTRL_REG, cfg->User_ctl) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIFO storage by default, do not include accelerometer and external sense data.
|
2014-12-03 00:35:17 +01:00
|
|
|
power = PIOS_MPU9250_PWRMGMT2_DISABLE_ACCEL;
|
|
|
|
|
2015-05-18 21:27:42 +02:00
|
|
|
#if defined(PIOS_MPU9250_ACCEL)
|
2014-12-03 00:35:17 +01:00
|
|
|
|
|
|
|
power &= ~PIOS_MPU9250_PWRMGMT2_DISABLE_ACCEL;
|
2015-05-18 21:27:42 +02:00
|
|
|
#endif
|
2014-12-03 00:35:17 +01:00
|
|
|
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_FIFO_EN_REG, cfg->Fifo_store) != 0) {
|
2015-05-18 21:27:42 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
PIOS_MPU9250_SetReg(PIOS_MPU9250_PWR_MGMT2_REG, power);
|
|
|
|
|
|
|
|
#if defined(PIOS_MPU9250_ACCEL)
|
|
|
|
PIOS_MPU9250_ConfigureRanges(cfg->gyro_range, cfg->accel_range, cfg->filter);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Interrupt configuration
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_INT_CFG_REG, cfg->interrupt_cfg) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef PIOS_MPU9250_MAG
|
|
|
|
PIOS_MPU9250_Mag_Init();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// Interrupt enable
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_INT_EN_REG, cfg->interrupt_en) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if ((PIOS_MPU9250_GetReg(PIOS_MPU9250_INT_EN_REG)) != cfg->interrupt_en) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
mpu9250_configured = true;
|
|
|
|
}
|
|
|
|
/**
|
|
|
|
* @brief Configures Gyro, accel and Filter ranges/setings
|
|
|
|
* @return 0 if successful, -1 if device has not been initialized
|
|
|
|
*/
|
|
|
|
int32_t PIOS_MPU9250_ConfigureRanges(
|
|
|
|
enum pios_mpu9250_range gyroRange,
|
|
|
|
enum pios_mpu9250_accel_range accelRange,
|
|
|
|
enum pios_mpu9250_filter filterSetting)
|
|
|
|
{
|
|
|
|
if (dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update filter settings
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_DLPF_CFG_REG, filterSetting) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sample rate divider, chosen upon digital filtering settings
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_SMPLRT_DIV_REG,
|
|
|
|
filterSetting == PIOS_MPU9250_LOWPASS_256_HZ ?
|
|
|
|
dev->cfg->Smpl_rate_div_no_dlp : dev->cfg->Smpl_rate_div_dlp) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->filter = filterSetting;
|
|
|
|
|
|
|
|
// Gyro range
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_GYRO_CFG_REG, gyroRange) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->gyro_range = gyroRange;
|
|
|
|
#if defined(PIOS_MPU9250_ACCEL)
|
|
|
|
// Set the accel range
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_ACCEL_CFG_REG, accelRange) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->accel_range = accelRange;
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Claim the SPI bus for the accel communications and select this chip
|
|
|
|
* @return 0 if successful, -1 for invalid device, -2 if unable to claim bus
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_ClaimBus(bool fast_spi)
|
|
|
|
{
|
|
|
|
if (PIOS_MPU9250_Validate(dev) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PIOS_SPI_ClaimBus(dev->spi_id) != 0) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
PIOS_MPU9250_SetSpeed(fast_spi);
|
|
|
|
PIOS_SPI_RC_PinSet(dev->spi_id, dev->slave_num, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static void PIOS_MPU9250_SetSpeed(const bool fast)
|
|
|
|
{
|
|
|
|
if (fast) {
|
|
|
|
PIOS_SPI_SetClockSpeed(dev->spi_id, dev->cfg->fast_prescaler);
|
|
|
|
} else {
|
|
|
|
PIOS_SPI_SetClockSpeed(dev->spi_id, dev->cfg->std_prescaler);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Claim the SPI bus for the accel communications and select this chip
|
|
|
|
* @return 0 if successful, -1 for invalid device, -2 if unable to claim bus
|
|
|
|
* @param woken[in,out] If non-NULL, will be set to true if woken was false and a higher priority
|
|
|
|
* task has is now eligible to run, else unchanged
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_ClaimBusISR(bool *woken, bool fast_spi)
|
|
|
|
{
|
|
|
|
if (PIOS_MPU9250_Validate(dev) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PIOS_SPI_ClaimBusISR(dev->spi_id, woken) != 0) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
PIOS_MPU9250_SetSpeed(fast_spi);
|
|
|
|
PIOS_SPI_RC_PinSet(dev->spi_id, dev->slave_num, 0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Release the SPI bus for the accel communications and end the transaction
|
|
|
|
* @return 0 if successful
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_ReleaseBus()
|
|
|
|
{
|
|
|
|
if (PIOS_MPU9250_Validate(dev) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PIOS_SPI_RC_PinSet(dev->spi_id, dev->slave_num, 1);
|
|
|
|
return PIOS_SPI_ReleaseBus(dev->spi_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Release the SPI bus for the accel communications and end the transaction
|
|
|
|
* @return 0 if successful
|
|
|
|
* @param woken[in,out] If non-NULL, will be set to true if woken was false and a higher priority
|
|
|
|
* task has is now eligible to run, else unchanged
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_ReleaseBusISR(bool *woken)
|
|
|
|
{
|
|
|
|
if (PIOS_MPU9250_Validate(dev) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PIOS_SPI_RC_PinSet(dev->spi_id, dev->slave_num, 1);
|
|
|
|
return PIOS_SPI_ReleaseBusISR(dev->spi_id, woken);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read a register from MPU9250
|
|
|
|
* @returns The register value or -1 if failure to get bus
|
|
|
|
* @param reg[in] Register address to be read
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_GetReg(uint8_t reg)
|
|
|
|
{
|
|
|
|
uint8_t data;
|
|
|
|
|
|
|
|
if (PIOS_MPU9250_ClaimBus(false) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, (0x80 | reg)); // request byte
|
|
|
|
data = PIOS_SPI_TransferByte(dev->spi_id, 0); // receive response
|
|
|
|
|
|
|
|
PIOS_MPU9250_ReleaseBus();
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Writes one byte to the MPU9250
|
|
|
|
* \param[in] reg Register address
|
|
|
|
* \param[in] data Byte to write
|
|
|
|
* \return 0 if operation was successful
|
|
|
|
* \return -1 if unable to claim SPI bus
|
|
|
|
* \return -2 if unable to send the command
|
|
|
|
* \return -3 if unable to receive the response
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_SetReg(uint8_t reg, uint8_t data)
|
|
|
|
{
|
2014-12-03 00:35:17 +01:00
|
|
|
int ret = 0;
|
2015-05-18 21:27:42 +02:00
|
|
|
|
|
|
|
if (PIOS_MPU9250_ClaimBus(false) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, 0x7f & reg);
|
2014-12-03 00:35:17 +01:00
|
|
|
// if (PIOS_SPI_TransferByte(dev->spi_id, 0x7f & reg) != 0) {
|
|
|
|
// PIOS_MPU9250_ReleaseBus();
|
|
|
|
// return -2;
|
|
|
|
// }
|
2015-05-18 21:27:42 +02:00
|
|
|
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, data);
|
2014-12-03 00:35:17 +01:00
|
|
|
// if (PIOS_SPI_TransferByte(dev->spi_id, data) != 0) {
|
|
|
|
// PIOS_MPU9250_ReleaseBus();
|
|
|
|
// return -3;
|
|
|
|
// }
|
2015-05-18 21:27:42 +02:00
|
|
|
|
|
|
|
PIOS_MPU9250_ReleaseBus();
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief Read the identification bytes from the MPU9250 sensor
|
|
|
|
* \return ID read from MPU9250 or -1 if failure
|
|
|
|
*/
|
|
|
|
int32_t PIOS_MPU9250_ReadID()
|
|
|
|
{
|
|
|
|
int32_t mpu9250_id = PIOS_MPU9250_GetReg(PIOS_MPU9250_WHOAMI);
|
|
|
|
|
|
|
|
if (mpu9250_id < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return mpu9250_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Reads the queue handle
|
|
|
|
* \return Handle to the queue or null if invalid device
|
|
|
|
*/
|
|
|
|
xQueueHandle PIOS_MPU9250_GetQueue()
|
|
|
|
{
|
|
|
|
if (PIOS_MPU9250_Validate(dev) != 0) {
|
|
|
|
return (xQueueHandle)NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dev->queue;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
float PIOS_MPU9250_GetScale()
|
|
|
|
{
|
|
|
|
switch (dev->gyro_range) {
|
|
|
|
case PIOS_MPU9250_SCALE_250_DEG:
|
|
|
|
return 1.0f / 131.0f;
|
|
|
|
|
|
|
|
case PIOS_MPU9250_SCALE_500_DEG:
|
|
|
|
return 1.0f / 65.5f;
|
|
|
|
|
|
|
|
case PIOS_MPU9250_SCALE_1000_DEG:
|
|
|
|
return 1.0f / 32.8f;
|
|
|
|
|
|
|
|
case PIOS_MPU9250_SCALE_2000_DEG:
|
|
|
|
return 1.0f / 16.4f;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
float PIOS_MPU9250_GetAccelScale()
|
|
|
|
{
|
|
|
|
switch (dev->accel_range) {
|
|
|
|
case PIOS_MPU9250_ACCEL_2G:
|
|
|
|
return PIOS_CONST_MKS_GRAV_ACCEL_F / 16384.0f;
|
|
|
|
|
|
|
|
case PIOS_MPU9250_ACCEL_4G:
|
|
|
|
return PIOS_CONST_MKS_GRAV_ACCEL_F / 8192.0f;
|
|
|
|
|
|
|
|
case PIOS_MPU9250_ACCEL_8G:
|
|
|
|
return PIOS_CONST_MKS_GRAV_ACCEL_F / 4096.0f;
|
|
|
|
|
|
|
|
case PIOS_MPU9250_ACCEL_16G:
|
|
|
|
return PIOS_CONST_MKS_GRAV_ACCEL_F / 2048.0f;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Run self-test operation.
|
|
|
|
* \return 0 if test succeeded
|
|
|
|
* \return non-zero value if test failed
|
|
|
|
*/
|
|
|
|
int32_t PIOS_MPU9250_Test(void)
|
|
|
|
{
|
|
|
|
/* Verify that ID matches */
|
|
|
|
int32_t mpu9250_id = PIOS_MPU9250_ReadID();
|
|
|
|
|
|
|
|
if (mpu9250_id < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mpu9250_id != PIOS_MPU9250_GYRO_ACC_ID) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PIOS_MPU9250_MAG)
|
|
|
|
/**
|
|
|
|
* @brief Read a mag register from MPU9250
|
|
|
|
* @returns The register value or -1 if failure to get bus
|
|
|
|
* @param reg[in] Register address to be read
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_Mag_GetReg(uint8_t reg)
|
|
|
|
{
|
2014-12-03 00:35:17 +01:00
|
|
|
int32_t data;
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Set the I2C slave address and read command.
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_ADDR, PIOS_MPU9250_MAG_I2C_ADDR |
|
|
|
|
PIOS_MPU9250_MAG_I2C_READ_FLAG) != PIOS_MPU9250_MAG_OK) {
|
2015-05-18 21:27:42 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Set the address of the register to read.
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_REG, reg) != PIOS_MPU9250_MAG_OK) {
|
2015-05-18 21:27:42 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Trigger the byte transfer.
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_CTRL, PIOS_MPU9250_I2C_SLV_ENABLE) != PIOS_MPU9250_MAG_OK) {
|
2015-05-18 21:27:42 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
PIOS_DELAY_WaitmS(1);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Read result.
|
|
|
|
data = PIOS_MPU9250_GetReg(PIOS_MPU9250_I2C_SLV4_DI);
|
|
|
|
PIOS_DELAY_WaitmS(1);
|
|
|
|
return data;
|
2015-05-18 21:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Writes one byte to the MPU9250
|
|
|
|
* \param[in] reg Register address
|
|
|
|
* \param[in] data Byte to write
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_Mag_SetReg(uint8_t reg, uint8_t data)
|
|
|
|
{
|
2014-12-03 00:35:17 +01:00
|
|
|
// Set the I2C slave address.
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_ADDR, PIOS_MPU9250_MAG_I2C_ADDR) != PIOS_MPU9250_MAG_OK) {
|
2015-05-18 21:27:42 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Set the address of the register to write.
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_REG, reg) != PIOS_MPU9250_MAG_OK) {
|
2015-05-18 21:27:42 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Set the byte to write.
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_DO, data) != PIOS_MPU9250_MAG_OK) {
|
2015-05-18 21:27:42 +02:00
|
|
|
;
|
|
|
|
}
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Trigger the byte transfer.
|
|
|
|
while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_CTRL, PIOS_MPU9250_I2C_SLV_ENABLE) != PIOS_MPU9250_MAG_OK) {
|
2015-05-18 21:27:42 +02:00
|
|
|
;
|
|
|
|
}
|
2014-12-03 00:35:17 +01:00
|
|
|
PIOS_DELAY_WaitmS(1);
|
2015-05-18 21:27:42 +02:00
|
|
|
return PIOS_MPU9250_MAG_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @rief Get ASAx registers from fuse ROM
|
|
|
|
* Hadj = H*((ASA-128)*0.5/128+1)
|
|
|
|
* \return 0 if test succeeded
|
|
|
|
* \return non-zero value if test failed
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_Mag_Sensitivity(void)
|
|
|
|
{
|
2014-12-03 00:35:17 +01:00
|
|
|
int i;
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
/* Put mag in power down state before changing mode */
|
2015-05-18 21:27:42 +02:00
|
|
|
PIOS_MPU9250_Mag_SetReg(PIOS_MPU9250_CNTL1, PIOS_MPU9250_MAG_POWER_DOWN_MODE);
|
|
|
|
PIOS_DELAY_WaitmS(1);
|
|
|
|
|
|
|
|
/* Enable fuse ROM for access */
|
|
|
|
PIOS_MPU9250_Mag_SetReg(PIOS_MPU9250_CNTL1, PIOS_MPU9250_MAG_FUSE_ROM_MODE);
|
|
|
|
PIOS_DELAY_WaitmS(1);
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
if (PIOS_MPU9250_ClaimBus(false) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2015-05-18 21:27:42 +02:00
|
|
|
|
|
|
|
/* Set addres and read flag */
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_I2C_SLV0_ADDR);
|
2014-12-03 00:35:17 +01:00
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_MAG_I2C_ADDR | PIOS_MPU9250_MAG_I2C_READ_FLAG);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
/* Set the address of the register to read. */
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_I2C_SLV0_REG);
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_ASAX);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
/* Trigger the byte transfer. */
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_I2C_SLV0_CTRL);
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_I2C_SLV_ENABLE | 0x3);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
PIOS_DELAY_WaitmS(1);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
/* Read the mag data from SPI block */
|
|
|
|
for (i = 0; i < 0x3; i++) {
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, (PIOS_MPU9250_EXT_SENS_DATA_00 | 0x80) + i);
|
|
|
|
dev->mag_sens_adj[i] = (float)(PIOS_SPI_TransferByte(dev->spi_id, 0x0) - 128) / 256 + 1;
|
|
|
|
}
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
PIOS_MPU9250_ReleaseBus();
|
2015-05-18 21:27:42 +02:00
|
|
|
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
/* Put mag in power down state before changing mode */
|
2015-05-18 21:27:42 +02:00
|
|
|
PIOS_MPU9250_Mag_SetReg(PIOS_MPU9250_CNTL1, PIOS_MPU9250_MAG_POWER_DOWN_MODE);
|
|
|
|
|
|
|
|
return PIOS_MPU9250_MAG_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read a mag register from MPU9250
|
|
|
|
* @returns The register value or -1 if failure to get bus
|
|
|
|
* @param reg[in] Register address to be read
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_Mag_Init(void)
|
|
|
|
{
|
2014-12-03 00:35:17 +01:00
|
|
|
// I2C multi-master init.
|
|
|
|
PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_MST_CTRL, PIOS_MPU9250_I2C_MST_P_NSR | PIOS_MPU9250_I2C_MST_CLOCK_400);
|
|
|
|
PIOS_DELAY_WaitmS(1);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Reset Mag.
|
|
|
|
PIOS_MPU9250_Mag_SetReg(PIOS_MPU9250_CNTL2, PIOS_MPU9250_MAG_RESET);
|
|
|
|
PIOS_DELAY_WaitmS(1);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// read fuse ROM to get the sensitivity adjustment values.
|
|
|
|
if (PIOS_MPU9250_Mag_Sensitivity() != PIOS_MPU9250_MAG_OK) {
|
|
|
|
;
|
|
|
|
}
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Confirm Mag ID.
|
|
|
|
while (false && (PIOS_MPU9250_Mag_Test() != PIOS_MPU9250_MAG_OK)) {
|
|
|
|
;
|
|
|
|
}
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Make sure no other registers will be triggered before entering continuous mode.
|
|
|
|
PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_CTRL, 0x0);
|
|
|
|
PIOS_DELAY_WaitmS(1);
|
|
|
|
PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV0_DO, 0x0);
|
|
|
|
PIOS_DELAY_WaitmS(1);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Making sure register are accessible.
|
|
|
|
PIOS_MPU9250_Mag_SetReg(PIOS_MPU9250_CNTL1, PIOS_MPU9250_MAG_OUTPUT_16BITS | PIOS_MPU9250_MAG_CONTINUOUS_MODE2);
|
|
|
|
PIOS_DELAY_WaitmS(1);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Get ST1, the 6 mag data and ST2.
|
|
|
|
// This is to save 2 SPI access.
|
|
|
|
// Set the I2C slave address and read command.
|
|
|
|
PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV0_ADDR, PIOS_MPU9250_MAG_I2C_ADDR | PIOS_MPU9250_MAG_I2C_READ_FLAG);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Set the address of the register to read.
|
|
|
|
PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV0_REG, PIOS_MPU9250_ST1);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
// Trigger the byte transfer.
|
|
|
|
PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV0_CTRL, PIOS_MPU9250_I2C_SLV_ENABLE | 0x8);
|
|
|
|
PIOS_DELAY_WaitmS(1);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
return PIOS_MPU9250_MAG_OK;
|
2015-05-18 21:27:42 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* @brief Read the mag identification bytes from the MPU9250 sensor
|
|
|
|
*/
|
|
|
|
int32_t PIOS_MPU9250_Mag_ReadID()
|
|
|
|
{
|
|
|
|
int32_t mpu9250_mag_id = PIOS_MPU9250_Mag_GetReg(PIOS_MPU9250_WIA);
|
|
|
|
|
|
|
|
if (mpu9250_mag_id < PIOS_MPU9250_MAG_OK) {
|
|
|
|
return PIOS_MPU9250_ERR_MAG_READ_ID;
|
|
|
|
}
|
|
|
|
return mpu9250_mag_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Run self-test operation.
|
|
|
|
* \return 0 if test succeeded
|
|
|
|
* \return non-zero value if test failed
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_Mag_Test(void)
|
|
|
|
{
|
|
|
|
/* Verify that ID matches */
|
|
|
|
int32_t mpu9250_mag_id = PIOS_MPU9250_Mag_ReadID();
|
|
|
|
|
|
|
|
if (mpu9250_mag_id < PIOS_MPU9250_MAG_OK) {
|
|
|
|
return PIOS_MPU9250_ERR_MAG_READ_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mpu9250_mag_id != PIOS_MPU9250_MAG_ID) {
|
|
|
|
return PIOS_MPU9250_ERR_MAG_BAD_ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* TODO: run self-test */
|
|
|
|
|
|
|
|
return PIOS_MPU9250_MAG_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read the mag data.
|
|
|
|
* \return true if data has been read from mpu
|
|
|
|
* \return false on error
|
|
|
|
*/
|
|
|
|
static bool PIOS_MPU9250_ReadMag(bool *woken)
|
|
|
|
{
|
2014-12-03 00:35:17 +01:00
|
|
|
if (PIOS_MPU9250_ClaimBusISR(woken, true) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
// Trigger the byte transfer.
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_I2C_SLV0_CTRL);
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_I2C_SLV_ENABLE | 0x8);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
2015-05-18 21:27:42 +02:00
|
|
|
|
2014-12-03 00:35:17 +01:00
|
|
|
return true;
|
2015-05-18 21:27:42 +02:00
|
|
|
}
|
2014-12-03 00:35:17 +01:00
|
|
|
#endif /* if defined(PIOS_MPU9250_MAG) */
|
2015-05-18 21:27:42 +02:00
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Reads the contents of the MPU9250 Interrupt Status register from an ISR
|
|
|
|
* @return The register value or -1 on failure to claim the bus
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_GetInterruptStatusRegISR(bool *woken)
|
|
|
|
{
|
|
|
|
/* Interrupt Status register can be read at high SPI clock speed */
|
|
|
|
uint8_t data;
|
|
|
|
|
|
|
|
if (PIOS_MPU9250_ClaimBusISR(woken, false) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, (0x80 | PIOS_MPU9250_INT_STATUS_REG));
|
|
|
|
data = PIOS_SPI_TransferByte(dev->spi_id, 0);
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Resets the MPU9250 FIFO from an ISR
|
|
|
|
* @param woken[in,out] If non-NULL, will be set to true if woken was false and a higher priority
|
|
|
|
* task has is now eligible to run, else unchanged
|
|
|
|
* @return 0 if operation was successful
|
|
|
|
* @return -1 if unable to claim SPI bus
|
|
|
|
* @return -2 if write to the device failed
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_ResetFifoISR(bool *woken)
|
|
|
|
{
|
|
|
|
int32_t result = 0;
|
|
|
|
|
|
|
|
if (PIOS_MPU9250_ClaimBusISR(woken, false) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Reset FIFO. */
|
|
|
|
if (PIOS_SPI_TransferByte(dev->spi_id, 0x7f & PIOS_MPU9250_USER_CTRL_REG) != 0) {
|
|
|
|
result = -2;
|
|
|
|
} else if (PIOS_SPI_TransferByte(dev->spi_id, (dev->cfg->User_ctl | PIOS_MPU9250_USERCTL_FIFO_RST)) != 0) {
|
|
|
|
result = -2;
|
|
|
|
}
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Obtains the number of bytes in the FIFO. Call from ISR only.
|
|
|
|
* @return the number of bytes in the FIFO
|
|
|
|
* @param woken[in,out] If non-NULL, will be set to true if woken was false and a higher priority
|
|
|
|
* task has is now eligible to run, else unchanged
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU9250_FifoDepthISR(bool *woken)
|
|
|
|
{
|
|
|
|
uint8_t mpu9250_send_buf[3] = { PIOS_MPU9250_FIFO_CNT_MSB | 0x80, 0, 0 };
|
|
|
|
uint8_t mpu9250_rec_buf[3];
|
|
|
|
|
|
|
|
if (PIOS_MPU9250_ClaimBusISR(woken, false) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu9250_send_buf[0], &mpu9250_rec_buf[0], sizeof(mpu9250_send_buf), NULL) < 0) {
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
|
|
|
|
return (mpu9250_rec_buf[1] << 8) | mpu9250_rec_buf[2];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief EXTI IRQ Handler. Read all the data from onboard buffer
|
|
|
|
* @return a boolean to the EXTI IRQ Handler wrapper indicating if a
|
|
|
|
* higher priority task is now eligible to run
|
|
|
|
*/
|
|
|
|
uint32_t mpu9250_irq = 0;
|
|
|
|
int32_t mpu9250_count;
|
|
|
|
uint32_t mpu9250_fifo_backup = 0;
|
|
|
|
|
|
|
|
uint8_t mpu9250_last_read_count = 0;
|
|
|
|
uint32_t mpu9250_fails = 0;
|
|
|
|
|
|
|
|
uint32_t mpu9250_interval_us;
|
|
|
|
uint32_t mpu9250_time_us;
|
|
|
|
uint32_t mpu9250_transfer_size;
|
|
|
|
|
|
|
|
bool PIOS_MPU9250_IRQHandler(void)
|
|
|
|
{
|
|
|
|
bool woken = false;
|
|
|
|
static uint32_t timeval;
|
|
|
|
|
|
|
|
mpu9250_interval_us = PIOS_DELAY_DiffuS(timeval);
|
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
if (!mpu9250_configured) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
#if defined(PIOS_MPU9250_MAG)
|
|
|
|
PIOS_MPU9250_ReadMag(&woken);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
bool read_ok = false;
|
|
|
|
if (dev->cfg->User_ctl & PIOS_MPU9250_USERCTL_FIFO_EN) {
|
|
|
|
read_ok = PIOS_MPU9250_ReadFifo(&woken);
|
|
|
|
} else {
|
|
|
|
read_ok = PIOS_MPU9250_ReadSensor(&woken);
|
|
|
|
}
|
|
|
|
if (read_ok) {
|
|
|
|
bool woken2 = PIOS_MPU9250_HandleData();
|
|
|
|
woken |= woken2;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpu9250_irq++;
|
|
|
|
|
|
|
|
mpu9250_time_us = PIOS_DELAY_DiffuS(timeval);
|
|
|
|
|
|
|
|
return woken;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PIOS_MPU9250_HandleData()
|
|
|
|
{
|
|
|
|
// Rotate the sensor to OP convention. The datasheet defines X as towards the right
|
|
|
|
// and Y as forward. OP convention transposes this. Also the Z is defined negatively
|
|
|
|
// to our convention
|
|
|
|
|
|
|
|
static struct pios_mpu9250_data data;
|
|
|
|
|
|
|
|
// Currently we only support rotations on top so switch X/Y accordingly
|
|
|
|
switch (dev->cfg->orientation) {
|
|
|
|
case PIOS_MPU9250_TOP_0DEG:
|
|
|
|
#ifdef PIOS_MPU9250_ACCEL
|
|
|
|
data.accel_y = GET_SENSOR_DATA(mpu9250_data, Accel_X); // chip X
|
|
|
|
data.accel_x = GET_SENSOR_DATA(mpu9250_data, Accel_Y); // chip Y
|
|
|
|
#endif
|
|
|
|
data.gyro_y = GET_SENSOR_DATA(mpu9250_data, Gyro_X); // chip X
|
|
|
|
data.gyro_x = GET_SENSOR_DATA(mpu9250_data, Gyro_Y); // chip Y
|
2014-12-20 20:23:23 +01:00
|
|
|
#ifdef PIOS_MPU9250_MAG
|
|
|
|
data.mag_y = GET_SENSOR_DATA(mpu9250_data, Mag_X); // chip X
|
|
|
|
data.mag_x = GET_SENSOR_DATA(mpu9250_data, Mag_Y); // chip Y
|
|
|
|
#endif
|
2015-05-18 21:27:42 +02:00
|
|
|
break;
|
|
|
|
case PIOS_MPU9250_TOP_90DEG:
|
|
|
|
// -1 to bring it back to -32768 +32767 range
|
|
|
|
#ifdef PIOS_MPU9250_ACCEL
|
|
|
|
data.accel_y = -1 - (GET_SENSOR_DATA(mpu9250_data, Accel_Y)); // chip Y
|
|
|
|
data.accel_x = GET_SENSOR_DATA(mpu9250_data, Accel_X); // chip X
|
|
|
|
#endif
|
|
|
|
data.gyro_y = -1 - (GET_SENSOR_DATA(mpu9250_data, Gyro_Y)); // chip Y
|
|
|
|
data.gyro_x = GET_SENSOR_DATA(mpu9250_data, Gyro_X); // chip X
|
2014-12-20 20:23:23 +01:00
|
|
|
#ifdef PIOS_MPU9250_MAG
|
|
|
|
data.mag_y = -1 - (GET_SENSOR_DATA(mpu9250_data, Mag_Y)); // chip Y
|
|
|
|
data.mag_x = GET_SENSOR_DATA(mpu9250_data, Mag_X); // chip X
|
|
|
|
#endif
|
2015-05-18 21:27:42 +02:00
|
|
|
break;
|
|
|
|
case PIOS_MPU9250_TOP_180DEG:
|
|
|
|
#ifdef PIOS_MPU9250_ACCEL
|
|
|
|
data.accel_y = -1 - (GET_SENSOR_DATA(mpu9250_data, Accel_X)); // chip X
|
|
|
|
data.accel_x = -1 - (GET_SENSOR_DATA(mpu9250_data, Accel_Y)); // chip Y
|
|
|
|
#endif
|
|
|
|
data.gyro_y = -1 - (GET_SENSOR_DATA(mpu9250_data, Gyro_X)); // chip X
|
|
|
|
data.gyro_x = -1 - (GET_SENSOR_DATA(mpu9250_data, Gyro_Y)); // chip Y
|
2014-12-20 20:23:23 +01:00
|
|
|
#ifdef PIOS_MPU9250_MAG
|
|
|
|
data.mag_y = -1 - (GET_SENSOR_DATA(mpu9250_data, Mag_X)); // chip X
|
|
|
|
data.mag_x = -1 - (GET_SENSOR_DATA(mpu9250_data, Mag_Y)); // chip Y
|
|
|
|
#endif
|
2015-05-18 21:27:42 +02:00
|
|
|
break;
|
|
|
|
case PIOS_MPU9250_TOP_270DEG:
|
|
|
|
#ifdef PIOS_MPU9250_ACCEL
|
|
|
|
data.accel_y = GET_SENSOR_DATA(mpu9250_data, Accel_Y); // chip Y
|
|
|
|
data.accel_x = -1 - (GET_SENSOR_DATA(mpu9250_data, Accel_X)); // chip X
|
|
|
|
#endif
|
|
|
|
data.gyro_y = GET_SENSOR_DATA(mpu9250_data, Gyro_Y); // chip Y
|
|
|
|
data.gyro_x = -1 - (GET_SENSOR_DATA(mpu9250_data, Gyro_X)); // chip X
|
2014-12-20 20:23:23 +01:00
|
|
|
#ifdef PIOS_MPU9250_MAG
|
|
|
|
data.mag_y = GET_SENSOR_DATA(mpu9250_data, Mag_Y); // chip Y
|
|
|
|
data.mag_x = -1 - (GET_SENSOR_DATA(mpu9250_data, Mag_X)); // chip X
|
|
|
|
#endif
|
2015-05-18 21:27:42 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef PIOS_MPU9250_ACCEL
|
|
|
|
data.accel_z = -1 - (GET_SENSOR_DATA(mpu9250_data, Accel_Z));
|
|
|
|
#endif
|
|
|
|
data.gyro_z = -1 - (GET_SENSOR_DATA(mpu9250_data, Gyro_Z));
|
|
|
|
data.temperature = GET_SENSOR_DATA(mpu9250_data, Temperature);
|
|
|
|
|
|
|
|
#ifdef PIOS_MPU9250_MAG
|
2014-12-03 00:35:17 +01:00
|
|
|
data.mag_z = GET_SENSOR_DATA(mpu9250_data, Mag_Z); // chip Z
|
|
|
|
data.mag_valid = mpu9250_data.data.st1 & PIOS_MPU9250_MAG_DATA_RDY;
|
2015-05-18 21:27:42 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
BaseType_t higherPriorityTaskWoken;
|
|
|
|
xQueueSendToBackFromISR(dev->queue, (void *)&data, &higherPriorityTaskWoken);
|
|
|
|
return higherPriorityTaskWoken == pdTRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PIOS_MPU9250_ReadSensor(bool *woken)
|
|
|
|
{
|
|
|
|
const uint8_t mpu9250_send_buf[1 + PIOS_MPU9250_SAMPLES_BYTES] = { PIOS_MPU9250_SENSOR_FIRST_REG | 0x80 };
|
|
|
|
|
|
|
|
if (PIOS_MPU9250_ClaimBusISR(woken, true) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu9250_send_buf[0], &mpu9250_data.buffer[0], sizeof(mpu9250_data_t), NULL) < 0) {
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
mpu9250_fails++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PIOS_MPU9250_ReadFifo(bool *woken)
|
|
|
|
{
|
|
|
|
/* Temporary fix for OP-1049. Expected to be superceded for next major release
|
|
|
|
* by code changes for OP-1039.
|
|
|
|
* Read interrupt status register to check for FIFO overflow. Must be the
|
|
|
|
* first read after interrupt, in case the device is configured so that
|
|
|
|
* any read clears in the status register (PIOS_MPU9250_INT_CLR_ANYRD set in
|
|
|
|
* interrupt config register) */
|
|
|
|
int32_t result;
|
2014-12-03 00:35:17 +01:00
|
|
|
|
2015-05-18 21:27:42 +02:00
|
|
|
if ((result = PIOS_MPU9250_GetInterruptStatusRegISR(woken)) < 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (result & PIOS_MPU9250_INT_STATUS_FIFO_OVERFLOW) {
|
|
|
|
/* The FIFO has overflowed, so reset it,
|
|
|
|
* to enable sample sync to be recovered.
|
|
|
|
* If the reset fails, we are in trouble, but
|
|
|
|
* we keep trying on subsequent interrupts. */
|
|
|
|
PIOS_MPU9250_ResetFifoISR(woken);
|
|
|
|
/* Return and wait for the next new sample. */
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Usual case - FIFO has not overflowed. */
|
|
|
|
mpu9250_count = PIOS_MPU9250_FifoDepthISR(woken);
|
|
|
|
if (mpu9250_count < PIOS_MPU9250_SAMPLES_BYTES) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIOS_MPU9250_ClaimBusISR(woken, true) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const uint8_t mpu9250_send_buf[1 + PIOS_MPU9250_SAMPLES_BYTES] = { PIOS_MPU9250_FIFO_REG | 0x80 };
|
|
|
|
|
|
|
|
if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu9250_send_buf[0], &mpu9250_data.buffer[0], sizeof(mpu9250_data_t), NULL) < 0) {
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
mpu9250_fails++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
|
|
|
|
// In the case where extras samples backed up grabbed an extra
|
|
|
|
if (mpu9250_count >= PIOS_MPU9250_SAMPLES_BYTES * 2) {
|
|
|
|
mpu9250_fifo_backup++;
|
|
|
|
if (PIOS_MPU9250_ClaimBusISR(woken, true) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu9250_send_buf[0], &mpu9250_data.buffer[0], sizeof(mpu9250_data_t), NULL) < 0) {
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
mpu9250_fails++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_MPU9250_ReleaseBusISR(woken);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#endif /* PIOS_INCLUDE_MPU9250 */
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|