2011-11-17 17:11:56 +01:00
|
|
|
/**
|
|
|
|
******************************************************************************
|
|
|
|
* @addtogroup PIOS PIOS Core hardware abstraction layer
|
|
|
|
* @{
|
|
|
|
* @addtogroup PIOS_MPU6000 MPU6000 Functions
|
|
|
|
* @brief Deals with the hardware interface to the 3-axis gyro
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file pios_mpu000.c
|
2012-01-05 02:29:29 +01:00
|
|
|
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
|
2011-11-17 17:11:56 +01:00
|
|
|
* @brief MPU6000 6-axis gyro and accel chip
|
|
|
|
* @see The GNU Public License (GPL) Version 3
|
|
|
|
*
|
|
|
|
******************************************************************************
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
/*
|
|
|
|
* 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
|
2011-11-17 17:11:56 +01:00
|
|
|
* (at your option) any later version.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* 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
|
2011-11-17 17:11:56 +01:00
|
|
|
* for more details.
|
2013-05-19 16:37:30 +02:00
|
|
|
*
|
|
|
|
* 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.,
|
2011-11-17 17:11:56 +01:00
|
|
|
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "pios.h"
|
2014-12-23 21:30:00 +01:00
|
|
|
#include <pios_mpu6000.h>
|
2013-03-15 15:28:53 +01:00
|
|
|
#ifdef PIOS_INCLUDE_MPU6000
|
2011-11-17 17:11:56 +01:00
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
#include <pios_constants.h>
|
2011-11-17 17:11:56 +01:00
|
|
|
/* Global Variables */
|
|
|
|
|
2012-03-26 10:03:38 +02:00
|
|
|
enum pios_mpu6000_dev_magic {
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_MPU6000_DEV_MAGIC = 0x9da9b3ed,
|
2012-03-26 10:03:38 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
struct mpu6000_dev {
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t spi_id;
|
|
|
|
uint32_t slave_num;
|
|
|
|
xQueueHandle queue;
|
|
|
|
const struct pios_mpu6000_cfg *cfg;
|
|
|
|
enum pios_mpu6000_range gyro_range;
|
|
|
|
enum pios_mpu6000_accel_range accel_range;
|
|
|
|
enum pios_mpu6000_filter filter;
|
|
|
|
enum pios_mpu6000_dev_magic magic;
|
2012-03-26 10:03:38 +02:00
|
|
|
};
|
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
#ifdef PIOS_MPU6000_ACCEL
|
|
|
|
#define PIOS_MPU6000_SAMPLES_BYTES 14
|
|
|
|
#define PIOS_MPU6000_SENSOR_FIRST_REG PIOS_MPU6000_ACCEL_X_OUT_MSB
|
|
|
|
#else
|
|
|
|
#define PIOS_MPU6000_SENSOR_FIRST_REG PIOS_MPU6000_TEMP_OUT_MSB
|
|
|
|
#define PIOS_MPU6000_SAMPLES_BYTES 8
|
|
|
|
#endif
|
|
|
|
|
|
|
|
typedef union {
|
|
|
|
uint8_t buffer[1 + PIOS_MPU6000_SAMPLES_BYTES];
|
|
|
|
struct {
|
|
|
|
uint8_t dummy;
|
|
|
|
#ifdef PIOS_MPU6000_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;
|
|
|
|
} data;
|
|
|
|
} mpu6000_data_t;
|
|
|
|
|
|
|
|
#define GET_SENSOR_DATA(mpudataptr, sensor) (mpudataptr.data.sensor##_h << 8 | mpudataptr.data.sensor##_l)
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// ! Global structure for this device device
|
|
|
|
static struct mpu6000_dev *dev;
|
2012-03-26 10:03:38 +02:00
|
|
|
volatile bool mpu6000_configured = false;
|
2014-10-10 01:21:40 +02:00
|
|
|
static mpu6000_data_t mpu6000_data;
|
2012-03-26 10:03:38 +02:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
// ! Private functions
|
2014-10-10 01:21:40 +02:00
|
|
|
static struct mpu6000_dev *PIOS_MPU6000_alloc(const struct pios_mpu6000_cfg *cfg);
|
2013-05-19 16:37:30 +02:00
|
|
|
static int32_t PIOS_MPU6000_Validate(struct mpu6000_dev *dev);
|
|
|
|
static void PIOS_MPU6000_Config(struct pios_mpu6000_cfg const *cfg);
|
2011-11-17 17:11:56 +01:00
|
|
|
static int32_t PIOS_MPU6000_SetReg(uint8_t address, uint8_t buffer);
|
|
|
|
static int32_t PIOS_MPU6000_GetReg(uint8_t address);
|
2014-10-07 11:40:39 +02:00
|
|
|
static void PIOS_MPU6000_SetSpeed(const bool fast);
|
2014-10-10 01:21:40 +02:00
|
|
|
static bool PIOS_MPU6000_HandleData();
|
|
|
|
static bool PIOS_MPU6000_ReadFifo(bool *woken);
|
|
|
|
static bool PIOS_MPU6000_ReadSensor(bool *woken);
|
2012-03-26 10:03:38 +02:00
|
|
|
/**
|
|
|
|
* @brief Allocate a new device
|
|
|
|
*/
|
2014-10-10 01:21:40 +02:00
|
|
|
static struct mpu6000_dev *PIOS_MPU6000_alloc(const struct pios_mpu6000_cfg *cfg)
|
2012-03-26 10:03:38 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
struct mpu6000_dev *mpu6000_dev;
|
|
|
|
|
2014-06-11 20:11:48 +02:00
|
|
|
mpu6000_dev = (struct mpu6000_dev *)pios_malloc(sizeof(*mpu6000_dev));
|
2013-05-19 16:37:30 +02:00
|
|
|
if (!mpu6000_dev) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpu6000_dev->magic = PIOS_MPU6000_DEV_MAGIC;
|
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
mpu6000_dev->queue = xQueueCreate(cfg->max_downsample + 1, sizeof(struct pios_mpu6000_data));
|
2013-05-19 16:37:30 +02:00
|
|
|
if (mpu6000_dev->queue == NULL) {
|
|
|
|
vPortFree(mpu6000_dev);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return mpu6000_dev;
|
2012-03-26 10:03:38 +02:00
|
|
|
}
|
2011-11-17 17:11:56 +01:00
|
|
|
|
2012-03-26 10:03:38 +02:00
|
|
|
/**
|
|
|
|
* @brief Validate the handle to the spi device
|
|
|
|
* @returns 0 for valid device or -1 otherwise
|
|
|
|
*/
|
2013-05-13 23:31:45 +02:00
|
|
|
static int32_t PIOS_MPU6000_Validate(struct mpu6000_dev *vdev)
|
2012-03-26 10:03:38 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (vdev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (vdev->magic != PIOS_MPU6000_DEV_MAGIC) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
if (vdev->spi_id == 0) {
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-03-26 10:03:38 +02:00
|
|
|
}
|
2011-12-29 07:10:13 +01:00
|
|
|
|
2011-11-17 17:11:56 +01:00
|
|
|
/**
|
2012-01-05 02:29:29 +01:00
|
|
|
* @brief Initialize the MPU6000 3-axis gyro sensor.
|
2012-03-26 10:03:38 +02:00
|
|
|
* @return 0 for success, -1 for failure
|
2011-11-17 17:11:56 +01:00
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t PIOS_MPU6000_Init(uint32_t spi_id, uint32_t slave_num, const struct pios_mpu6000_cfg *cfg)
|
2011-11-17 17:11:56 +01:00
|
|
|
{
|
2014-10-10 01:21:40 +02:00
|
|
|
dev = PIOS_MPU6000_alloc(cfg);
|
2013-05-19 16:37:30 +02:00
|
|
|
if (dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->spi_id = spi_id;
|
|
|
|
dev->slave_num = slave_num;
|
|
|
|
dev->cfg = cfg;
|
|
|
|
|
|
|
|
/* Configure the MPU6000 Sensor */
|
|
|
|
PIOS_MPU6000_Config(cfg);
|
|
|
|
|
|
|
|
/* Set up EXTI line */
|
|
|
|
PIOS_EXTI_Init(cfg->exti_cfg);
|
|
|
|
return 0;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-01-05 02:29:29 +01:00
|
|
|
* @brief Initialize the MPU6000 3-axis gyro sensor
|
2011-11-17 17:11:56 +01:00
|
|
|
* \return none
|
|
|
|
* \param[in] PIOS_MPU6000_ConfigTypeDef struct to be used to configure sensor.
|
2013-02-17 12:16:24 +01:00
|
|
|
*
|
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
static void PIOS_MPU6000_Config(struct pios_mpu6000_cfg const *cfg)
|
2011-11-17 17:11:56 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_MPU6000_Test();
|
|
|
|
|
|
|
|
// Reset chip
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_PWR_MGMT_REG, PIOS_MPU6000_PWRMGMT_IMU_RST) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
PIOS_DELAY_WaitmS(50);
|
|
|
|
|
|
|
|
// Reset chip and fifo
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_USER_CTRL_REG,
|
|
|
|
PIOS_MPU6000_USERCTL_GYRO_RST |
|
|
|
|
PIOS_MPU6000_USERCTL_SIG_COND |
|
|
|
|
PIOS_MPU6000_USERCTL_FIFO_RST) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait for reset to finish
|
|
|
|
while (PIOS_MPU6000_GetReg(PIOS_MPU6000_USER_CTRL_REG) &
|
|
|
|
(PIOS_MPU6000_USERCTL_GYRO_RST |
|
|
|
|
PIOS_MPU6000_USERCTL_SIG_COND |
|
|
|
|
PIOS_MPU6000_USERCTL_FIFO_RST)) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
PIOS_DELAY_WaitmS(10);
|
|
|
|
// Power management configuration
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_PWR_MGMT_REG, cfg->Pwr_mgmt_clk) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interrupt configuration
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_INT_CFG_REG, cfg->interrupt_cfg) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interrupt configuration
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_INT_EN_REG, cfg->interrupt_en) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIFO storage
|
2011-12-29 07:10:13 +01:00
|
|
|
#if defined(PIOS_MPU6000_ACCEL)
|
2013-05-19 16:37:30 +02:00
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_FIFO_EN_REG, cfg->Fifo_store | PIOS_MPU6000_ACCEL_OUT) != 0) {
|
|
|
|
;
|
|
|
|
}
|
2011-12-29 07:10:13 +01:00
|
|
|
#else
|
2013-05-19 16:37:30 +02:00
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_FIFO_EN_REG, cfg->Fifo_store) != 0) {
|
|
|
|
;
|
|
|
|
}
|
2011-12-29 07:10:13 +01:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_MPU6000_ConfigureRanges(cfg->gyro_range, cfg->accel_range, cfg->filter);
|
|
|
|
// Interrupt configuration
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_USER_CTRL_REG, cfg->User_ctl) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interrupt configuration
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_PWR_MGMT_REG, cfg->Pwr_mgmt_clk) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interrupt configuration
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_INT_CFG_REG, cfg->interrupt_cfg) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Interrupt configuration
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_INT_EN_REG, cfg->interrupt_en) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if ((PIOS_MPU6000_GetReg(PIOS_MPU6000_INT_EN_REG)) != cfg->interrupt_en) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpu6000_configured = true;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
2013-03-03 20:05:27 +01:00
|
|
|
/**
|
|
|
|
* @brief Configures Gyro, accel and Filter ranges/setings
|
|
|
|
* @return 0 if successful, -1 if device has not been initialized
|
|
|
|
*/
|
|
|
|
int32_t PIOS_MPU6000_ConfigureRanges(
|
2013-05-19 16:37:30 +02:00
|
|
|
enum pios_mpu6000_range gyroRange,
|
|
|
|
enum pios_mpu6000_accel_range accelRange,
|
|
|
|
enum pios_mpu6000_filter filterSetting)
|
2013-03-03 20:05:27 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (dev == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// update filter settings
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_DLPF_CFG_REG, filterSetting) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sample rate divider, chosen upon digital filtering settings
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_SMPLRT_DIV_REG,
|
|
|
|
filterSetting == PIOS_MPU6000_LOWPASS_256_HZ ?
|
|
|
|
dev->cfg->Smpl_rate_div_no_dlp : dev->cfg->Smpl_rate_div_dlp) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->filter = filterSetting;
|
|
|
|
|
|
|
|
// Gyro range
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_GYRO_CFG_REG, gyroRange) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->gyro_range = gyroRange;
|
2013-03-03 20:05:27 +01:00
|
|
|
#if defined(PIOS_MPU6000_ACCEL)
|
2013-05-19 16:37:30 +02:00
|
|
|
// Set the accel range
|
|
|
|
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_ACCEL_CFG_REG, accelRange) != 0) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev->accel_range = accelRange;
|
2013-03-03 20:05:27 +01:00
|
|
|
#endif
|
2013-05-19 16:37:30 +02:00
|
|
|
return 0;
|
2013-03-03 20:05:27 +01:00
|
|
|
}
|
2011-11-17 17:11:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Claim the SPI bus for the accel communications and select this chip
|
2012-03-26 10:03:38 +02:00
|
|
|
* @return 0 if successful, -1 for invalid device, -2 if unable to claim bus
|
2011-11-17 17:11:56 +01:00
|
|
|
*/
|
2014-10-07 11:40:39 +02:00
|
|
|
static int32_t PIOS_MPU6000_ClaimBus(bool fast_spi)
|
2011-11-17 17:11:56 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (PIOS_MPU6000_Validate(dev) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PIOS_SPI_ClaimBus(dev->spi_id) != 0) {
|
|
|
|
return -2;
|
|
|
|
}
|
2014-10-07 11:40:39 +02:00
|
|
|
PIOS_MPU6000_SetSpeed(fast_spi);
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_SPI_RC_PinSet(dev->spi_id, dev->slave_num, 0);
|
|
|
|
return 0;
|
2013-05-11 08:48:34 +02:00
|
|
|
}
|
|
|
|
|
2014-10-07 11:40:39 +02:00
|
|
|
|
|
|
|
static void PIOS_MPU6000_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-11 08:48:34 +02:00
|
|
|
/**
|
|
|
|
* @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
|
|
|
|
*/
|
2014-10-07 11:40:39 +02:00
|
|
|
static int32_t PIOS_MPU6000_ClaimBusISR(bool *woken, bool fast_spi)
|
2013-05-11 08:48:34 +02:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (PIOS_MPU6000_Validate(dev) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
if (PIOS_SPI_ClaimBusISR(dev->spi_id, woken) != 0) {
|
|
|
|
return -2;
|
|
|
|
}
|
2014-10-07 11:40:39 +02:00
|
|
|
PIOS_MPU6000_SetSpeed(fast_spi);
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_SPI_RC_PinSet(dev->spi_id, dev->slave_num, 0);
|
|
|
|
return 0;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Release the SPI bus for the accel communications and end the transaction
|
|
|
|
* @return 0 if successful
|
|
|
|
*/
|
2013-05-11 08:48:34 +02:00
|
|
|
static int32_t PIOS_MPU6000_ReleaseBus()
|
2011-11-17 17:11:56 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (PIOS_MPU6000_Validate(dev) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PIOS_SPI_RC_PinSet(dev->spi_id, dev->slave_num, 1);
|
|
|
|
return PIOS_SPI_ReleaseBus(dev->spi_id);
|
2013-05-11 08:48:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @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_MPU6000_ReleaseBusISR(bool *woken)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (PIOS_MPU6000_Validate(dev) != 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PIOS_SPI_RC_PinSet(dev->spi_id, dev->slave_num, 1);
|
|
|
|
return PIOS_SPI_ReleaseBusISR(dev->spi_id, woken);
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read a register from MPU6000
|
|
|
|
* @returns The register value or -1 if failure to get bus
|
|
|
|
* @param reg[in] Register address to be read
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU6000_GetReg(uint8_t reg)
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t data;
|
|
|
|
|
2014-10-07 11:40:39 +02:00
|
|
|
if (PIOS_MPU6000_ClaimBus(false) != 0) {
|
2013-05-19 16:37:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, (0x80 | reg)); // request byte
|
|
|
|
data = PIOS_SPI_TransferByte(dev->spi_id, 0); // receive response
|
|
|
|
|
|
|
|
PIOS_MPU6000_ReleaseBus();
|
|
|
|
return data;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Writes one byte to the MPU6000
|
|
|
|
* \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 claim i2c device
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU6000_SetReg(uint8_t reg, uint8_t data)
|
|
|
|
{
|
2014-10-07 11:40:39 +02:00
|
|
|
if (PIOS_MPU6000_ClaimBus(false) != 0) {
|
2013-05-19 16:37:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIOS_SPI_TransferByte(dev->spi_id, 0x7f & reg) != 0) {
|
|
|
|
PIOS_MPU6000_ReleaseBus();
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIOS_SPI_TransferByte(dev->spi_id, data) != 0) {
|
|
|
|
PIOS_MPU6000_ReleaseBus();
|
|
|
|
return -3;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_MPU6000_ReleaseBus();
|
|
|
|
|
|
|
|
return 0;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Read current X, Z, Y values (in that order)
|
|
|
|
* \param[out] int16_t array of size 3 to store X, Z, and Y magnetometer readings
|
2012-12-30 15:01:25 +01:00
|
|
|
* \returns 0 if succesful
|
2011-11-17 17:11:56 +01:00
|
|
|
*/
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t PIOS_MPU6000_ReadGyros(struct pios_mpu6000_data *data)
|
2011-11-17 17:11:56 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
// THIS FUNCTION IS DEPRECATED AND DOES NOT PERFORM A ROTATION
|
|
|
|
uint8_t buf[7] = { PIOS_MPU6000_GYRO_X_OUT_MSB | 0x80, 0, 0, 0, 0, 0, 0 };
|
|
|
|
uint8_t rec[7];
|
|
|
|
|
2014-10-07 11:40:39 +02:00
|
|
|
if (PIOS_MPU6000_ClaimBus(true) != 0) {
|
2013-05-19 16:37:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (PIOS_SPI_TransferBlock(dev->spi_id, &buf[0], &rec[0], sizeof(buf), NULL) < 0) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
PIOS_MPU6000_ReleaseBus();
|
|
|
|
|
|
|
|
data->gyro_x = rec[1] << 8 | rec[2];
|
|
|
|
data->gyro_y = rec[3] << 8 | rec[4];
|
|
|
|
data->gyro_z = rec[5] << 8 | rec[6];
|
|
|
|
|
|
|
|
return 0;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
|
|
|
|
2012-01-05 02:29:29 +01:00
|
|
|
/*
|
|
|
|
* @brief Read the identification bytes from the MPU6000 sensor
|
|
|
|
* \return ID read from MPU6000 or -1 if failure
|
2013-05-19 16:37:30 +02:00
|
|
|
*/
|
2011-11-17 17:11:56 +01:00
|
|
|
int32_t PIOS_MPU6000_ReadID()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
int32_t mpu6000_id = PIOS_MPU6000_GetReg(PIOS_MPU6000_WHOAMI);
|
|
|
|
|
|
|
|
if (mpu6000_id < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return mpu6000_id;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2012-03-26 10:03:38 +02:00
|
|
|
* \brief Reads the queue handle
|
|
|
|
* \return Handle to the queue or null if invalid device
|
2011-11-17 17:11:56 +01:00
|
|
|
*/
|
2012-03-26 10:03:38 +02:00
|
|
|
xQueueHandle PIOS_MPU6000_GetQueue()
|
2011-11-17 17:11:56 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
if (PIOS_MPU6000_Validate(dev) != 0) {
|
|
|
|
return (xQueueHandle)NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return dev->queue;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
float PIOS_MPU6000_GetScale()
|
2011-11-17 17:11:56 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
switch (dev->gyro_range) {
|
|
|
|
case PIOS_MPU6000_SCALE_250_DEG:
|
|
|
|
return 1.0f / 131.0f;
|
|
|
|
|
|
|
|
case PIOS_MPU6000_SCALE_500_DEG:
|
|
|
|
return 1.0f / 65.5f;
|
|
|
|
|
|
|
|
case PIOS_MPU6000_SCALE_1000_DEG:
|
|
|
|
return 1.0f / 32.8f;
|
|
|
|
|
|
|
|
case PIOS_MPU6000_SCALE_2000_DEG:
|
|
|
|
return 1.0f / 16.4f;
|
|
|
|
}
|
|
|
|
return 0;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
2011-12-29 07:10:13 +01:00
|
|
|
|
|
|
|
float PIOS_MPU6000_GetAccelScale()
|
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
switch (dev->accel_range) {
|
|
|
|
case PIOS_MPU6000_ACCEL_2G:
|
2014-10-10 01:21:40 +02:00
|
|
|
return PIOS_CONST_MKS_GRAV_ACCEL_F / 16384.0f;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
case PIOS_MPU6000_ACCEL_4G:
|
2014-10-10 01:21:40 +02:00
|
|
|
return PIOS_CONST_MKS_GRAV_ACCEL_F / 8192.0f;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
case PIOS_MPU6000_ACCEL_8G:
|
2014-10-10 01:21:40 +02:00
|
|
|
return PIOS_CONST_MKS_GRAV_ACCEL_F / 4096.0f;
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
case PIOS_MPU6000_ACCEL_16G:
|
2014-10-10 01:21:40 +02:00
|
|
|
return PIOS_CONST_MKS_GRAV_ACCEL_F / 2048.0f;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2011-12-29 07:10:13 +01:00
|
|
|
}
|
|
|
|
|
2011-11-17 17:11:56 +01:00
|
|
|
/**
|
|
|
|
* @brief Run self-test operation.
|
|
|
|
* \return 0 if test succeeded
|
|
|
|
* \return non-zero value if test succeeded
|
|
|
|
*/
|
2012-06-09 18:18:14 +02:00
|
|
|
int32_t PIOS_MPU6000_Test(void)
|
2011-11-17 17:11:56 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
/* Verify that ID matches (MPU6000 ID is 0x69) */
|
|
|
|
int32_t mpu6000_id = PIOS_MPU6000_ReadID();
|
|
|
|
|
|
|
|
if (mpu6000_id < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mpu6000_id != 0x68) {
|
|
|
|
return -2;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
2011-11-17 17:11:56 +01:00
|
|
|
}
|
|
|
|
|
2013-07-27 17:46:22 +02:00
|
|
|
/**
|
|
|
|
* @brief Reads the contents of the MPU6000 Interrupt Status register from an ISR
|
|
|
|
* @return The register value or -1 on failure to claim the bus
|
|
|
|
*/
|
|
|
|
static int32_t PIOS_MPU6000_GetInterruptStatusRegISR(bool *woken)
|
|
|
|
{
|
|
|
|
/* Interrupt Status register can be read at high SPI clock speed */
|
|
|
|
uint8_t data;
|
|
|
|
|
2014-10-07 11:40:39 +02:00
|
|
|
if (PIOS_MPU6000_ClaimBusISR(woken, false) != 0) {
|
2013-07-27 17:46:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
PIOS_SPI_TransferByte(dev->spi_id, (0x80 | PIOS_MPU6000_INT_STATUS_REG));
|
|
|
|
data = PIOS_SPI_TransferByte(dev->spi_id, 0);
|
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Resets the MPU6000 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_MPU6000_ResetFifoISR(bool *woken)
|
|
|
|
{
|
|
|
|
int32_t result = 0;
|
|
|
|
|
2014-10-07 11:40:39 +02:00
|
|
|
if (PIOS_MPU6000_ClaimBusISR(woken, false) != 0) {
|
2013-07-27 17:46:22 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
/* Reset FIFO. */
|
|
|
|
if (PIOS_SPI_TransferByte(dev->spi_id, 0x7f & PIOS_MPU6000_USER_CTRL_REG) != 0) {
|
|
|
|
result = -2;
|
|
|
|
} else if (PIOS_SPI_TransferByte(dev->spi_id, (dev->cfg->User_ctl | PIOS_MPU6000_USERCTL_FIFO_RST)) != 0) {
|
|
|
|
result = -2;
|
|
|
|
}
|
2014-10-07 11:40:39 +02:00
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
2013-07-27 17:46:22 +02:00
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2011-11-18 07:24:55 +01:00
|
|
|
/**
|
2013-05-11 08:48:34 +02:00
|
|
|
* @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
|
2011-11-18 07:24:55 +01:00
|
|
|
*/
|
2013-05-11 08:48:34 +02:00
|
|
|
static int32_t PIOS_MPU6000_FifoDepthISR(bool *woken)
|
2011-11-18 07:24:55 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
uint8_t mpu6000_send_buf[3] = { PIOS_MPU6000_FIFO_CNT_MSB | 0x80, 0, 0 };
|
|
|
|
uint8_t mpu6000_rec_buf[3];
|
|
|
|
|
2014-10-07 11:40:39 +02:00
|
|
|
if (PIOS_MPU6000_ClaimBusISR(woken, false) != 0) {
|
2013-05-19 16:37:30 +02:00
|
|
|
return -1;
|
|
|
|
}
|
2011-11-18 07:24:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu6000_send_buf[0], &mpu6000_rec_buf[0], sizeof(mpu6000_send_buf), NULL) < 0) {
|
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
|
|
|
return -1;
|
|
|
|
}
|
2011-11-18 07:24:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
2011-11-18 07:24:55 +01:00
|
|
|
|
2013-05-19 16:37:30 +02:00
|
|
|
return (mpu6000_rec_buf[1] << 8) | mpu6000_rec_buf[2];
|
2011-11-18 07:24:55 +01:00
|
|
|
}
|
|
|
|
|
2011-11-17 17:11:56 +01:00
|
|
|
/**
|
2013-05-19 16:37:30 +02:00
|
|
|
* @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
|
|
|
|
*/
|
2011-11-17 17:11:56 +01:00
|
|
|
uint32_t mpu6000_irq = 0;
|
2011-11-18 07:24:55 +01:00
|
|
|
int32_t mpu6000_count;
|
2013-05-19 16:37:30 +02:00
|
|
|
uint32_t mpu6000_fifo_backup = 0;
|
2011-11-18 07:00:55 +01:00
|
|
|
|
2011-11-17 18:23:31 +01:00
|
|
|
uint8_t mpu6000_last_read_count = 0;
|
|
|
|
uint32_t mpu6000_fails = 0;
|
|
|
|
|
|
|
|
uint32_t mpu6000_interval_us;
|
|
|
|
uint32_t mpu6000_time_us;
|
|
|
|
uint32_t mpu6000_transfer_size;
|
|
|
|
|
2012-09-27 21:34:11 +02:00
|
|
|
bool PIOS_MPU6000_IRQHandler(void)
|
2011-11-17 17:11:56 +01:00
|
|
|
{
|
2013-05-19 16:37:30 +02:00
|
|
|
bool woken = false;
|
|
|
|
static uint32_t timeval;
|
|
|
|
|
|
|
|
mpu6000_interval_us = PIOS_DELAY_DiffuS(timeval);
|
|
|
|
timeval = PIOS_DELAY_GetRaw();
|
|
|
|
|
|
|
|
if (!mpu6000_configured) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
bool read_ok = false;
|
|
|
|
if (dev->cfg->User_ctl & PIOS_MPU6000_USERCTL_FIFO_EN) {
|
|
|
|
read_ok = PIOS_MPU6000_ReadFifo(&woken);
|
|
|
|
} else {
|
|
|
|
read_ok = PIOS_MPU6000_ReadSensor(&woken);
|
|
|
|
}
|
|
|
|
if (read_ok) {
|
|
|
|
bool woken2 = PIOS_MPU6000_HandleData();
|
|
|
|
woken |= woken2;
|
|
|
|
}
|
|
|
|
|
|
|
|
mpu6000_irq++;
|
|
|
|
|
|
|
|
mpu6000_time_us = PIOS_DELAY_DiffuS(timeval);
|
|
|
|
|
|
|
|
return woken;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PIOS_MPU6000_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_mpu6000_data data;
|
|
|
|
|
|
|
|
// Currently we only support rotations on top so switch X/Y accordingly
|
|
|
|
switch (dev->cfg->orientation) {
|
|
|
|
case PIOS_MPU6000_TOP_0DEG:
|
|
|
|
#ifdef PIOS_MPU6000_ACCEL
|
|
|
|
data.accel_y = GET_SENSOR_DATA(mpu6000_data, Accel_X); // chip X
|
|
|
|
data.accel_x = GET_SENSOR_DATA(mpu6000_data, Accel_Y); // chip Y
|
|
|
|
#endif
|
|
|
|
data.gyro_y = GET_SENSOR_DATA(mpu6000_data, Gyro_X); // chip X
|
|
|
|
data.gyro_x = GET_SENSOR_DATA(mpu6000_data, Gyro_Y); // chip Y
|
|
|
|
break;
|
|
|
|
case PIOS_MPU6000_TOP_90DEG:
|
|
|
|
// -1 to bring it back to -32768 +32767 range
|
|
|
|
#ifdef PIOS_MPU6000_ACCEL
|
|
|
|
data.accel_y = -1 - (GET_SENSOR_DATA(mpu6000_data, Accel_Y)); // chip Y
|
|
|
|
data.accel_x = GET_SENSOR_DATA(mpu6000_data, Accel_X); // chip X
|
|
|
|
#endif
|
|
|
|
data.gyro_y = -1 - (GET_SENSOR_DATA(mpu6000_data, Gyro_Y)); // chip Y
|
|
|
|
data.gyro_x = GET_SENSOR_DATA(mpu6000_data, Gyro_X); // chip X
|
|
|
|
break;
|
|
|
|
case PIOS_MPU6000_TOP_180DEG:
|
|
|
|
#ifdef PIOS_MPU6000_ACCEL
|
|
|
|
data.accel_y = -1 - (GET_SENSOR_DATA(mpu6000_data, Accel_X)); // chip X
|
|
|
|
data.accel_x = -1 - (GET_SENSOR_DATA(mpu6000_data, Accel_Y)); // chip Y
|
|
|
|
#endif
|
|
|
|
data.gyro_y = -1 - (GET_SENSOR_DATA(mpu6000_data, Gyro_X)); // chip X
|
|
|
|
data.gyro_x = -1 - (GET_SENSOR_DATA(mpu6000_data, Gyro_Y)); // chip Y
|
|
|
|
break;
|
|
|
|
case PIOS_MPU6000_TOP_270DEG:
|
|
|
|
#ifdef PIOS_MPU6000_ACCEL
|
|
|
|
data.accel_y = GET_SENSOR_DATA(mpu6000_data, Accel_Y); // chip Y
|
|
|
|
data.accel_x = -1 - (GET_SENSOR_DATA(mpu6000_data, Accel_X)); // chip X
|
|
|
|
#endif
|
|
|
|
data.gyro_y = GET_SENSOR_DATA(mpu6000_data, Gyro_Y); // chip Y
|
|
|
|
data.gyro_x = -1 - (GET_SENSOR_DATA(mpu6000_data, Gyro_X)); // chip X
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
#ifdef PIOS_MPU6000_ACCEL
|
|
|
|
data.accel_z = -1 - (GET_SENSOR_DATA(mpu6000_data, Accel_Z));
|
|
|
|
#endif
|
|
|
|
data.gyro_z = -1 - (GET_SENSOR_DATA(mpu6000_data, Gyro_Z));
|
|
|
|
data.temperature = GET_SENSOR_DATA(mpu6000_data, Temperature);
|
|
|
|
|
|
|
|
BaseType_t higherPriorityTaskWoken;
|
|
|
|
xQueueSendToBackFromISR(dev->queue, (void *)&data, &higherPriorityTaskWoken);
|
|
|
|
return higherPriorityTaskWoken == pdTRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PIOS_MPU6000_ReadSensor(bool *woken)
|
|
|
|
{
|
|
|
|
const uint8_t mpu6000_send_buf[1 + PIOS_MPU6000_SAMPLES_BYTES] = { PIOS_MPU6000_SENSOR_FIRST_REG | 0x80 };
|
|
|
|
|
|
|
|
if (PIOS_MPU6000_ClaimBusISR(woken, true) != 0) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu6000_send_buf[0], &mpu6000_data.buffer[0], sizeof(mpu6000_data_t), NULL) < 0) {
|
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
|
|
|
mpu6000_fails++;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool PIOS_MPU6000_ReadFifo(bool *woken)
|
|
|
|
{
|
2013-07-27 17:46:22 +02:00
|
|
|
/* 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_MPU6000_INT_CLR_ANYRD set in
|
|
|
|
* interrupt config register) */
|
|
|
|
int32_t result;
|
2014-10-10 01:21:40 +02:00
|
|
|
|
|
|
|
if ((result = PIOS_MPU6000_GetInterruptStatusRegISR(woken)) < 0) {
|
|
|
|
return false;
|
2013-07-27 17:46:22 +02:00
|
|
|
}
|
|
|
|
if (result & PIOS_MPU6000_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. */
|
2014-10-10 01:21:40 +02:00
|
|
|
PIOS_MPU6000_ResetFifoISR(woken);
|
2013-07-27 17:46:22 +02:00
|
|
|
/* Return and wait for the next new sample. */
|
2014-10-10 01:21:40 +02:00
|
|
|
return false;
|
2013-07-27 17:46:22 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Usual case - FIFO has not overflowed. */
|
2014-10-10 01:21:40 +02:00
|
|
|
mpu6000_count = PIOS_MPU6000_FifoDepthISR(woken);
|
2013-07-27 17:46:22 +02:00
|
|
|
if (mpu6000_count < PIOS_MPU6000_SAMPLES_BYTES) {
|
2014-10-10 01:21:40 +02:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-02-17 02:02:12 +01:00
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
if (PIOS_MPU6000_ClaimBusISR(woken, true) != 0) {
|
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-02-17 02:02:12 +01:00
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
const uint8_t mpu6000_send_buf[1 + PIOS_MPU6000_SAMPLES_BYTES] = { PIOS_MPU6000_FIFO_REG | 0x80 };
|
2013-02-17 02:02:12 +01:00
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu6000_send_buf[0], &mpu6000_data.buffer[0], sizeof(mpu6000_data_t), NULL) < 0) {
|
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
2013-05-19 16:37:30 +02:00
|
|
|
mpu6000_fails++;
|
2014-10-10 01:21:40 +02:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2013-02-17 02:02:12 +01:00
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
2013-05-19 16:37:30 +02:00
|
|
|
|
|
|
|
// In the case where extras samples backed up grabbed an extra
|
2013-07-27 17:46:22 +02:00
|
|
|
if (mpu6000_count >= PIOS_MPU6000_SAMPLES_BYTES * 2) {
|
2013-05-19 16:37:30 +02:00
|
|
|
mpu6000_fifo_backup++;
|
2014-10-10 01:21:40 +02:00
|
|
|
if (PIOS_MPU6000_ClaimBusISR(woken, true) != 0) {
|
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
if (PIOS_SPI_TransferBlock(dev->spi_id, &mpu6000_send_buf[0], &mpu6000_data.buffer[0], sizeof(mpu6000_data_t), NULL) < 0) {
|
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
2013-05-19 16:37:30 +02:00
|
|
|
mpu6000_fails++;
|
2014-10-10 01:21:40 +02:00
|
|
|
return false;
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
|
|
|
|
2014-10-10 01:21:40 +02:00
|
|
|
PIOS_MPU6000_ReleaseBusISR(woken);
|
2013-05-19 16:37:30 +02:00
|
|
|
}
|
2014-10-10 01:21:40 +02:00
|
|
|
return true;
|
2013-02-17 02:02:12 +01:00
|
|
|
}
|
2013-03-15 15:28:53 +01:00
|
|
|
#endif /* PIOS_INCLUDE_MPU6000 */
|
2011-11-17 17:11:56 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|