diff --git a/flight/pios/common/pios_mpu9250.c b/flight/pios/common/pios_mpu9250.c new file mode 100644 index 000000000..a7c920bda --- /dev/null +++ b/flight/pios/common/pios_mpu9250.c @@ -0,0 +1,1033 @@ +/** + ****************************************************************************** + * @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 +/* 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 +#define PIOS_MPU9250_ACCEL_SAMPLES_BYTES (6) +#else +#define PIOS_MPU9250_ACCEL_SAMPLES_BYTES (0) +#endif + +#ifdef PIOS_MPU9250_MAG +#define PIOS_MPU9250_MAG_SAMPLES_BYTES (8) +#else +#define PIOS_MPU9250_MAG_SAMPLES_BYTES (0) +#endif + +#define PIOS_MPU9250_GYRO_SAMPLES_BYTES (6) +#define PIOS_MPU9250_TEMP_SAMPLES_BYTES (2) + +#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) + +#ifdef PIOS_MPU9250_ACCEL +#define PIOS_MPU9250_SENSOR_FIRST_REG PIOS_MPU9250_ACCEL_X_OUT_MSB +#else +#define PIOS_MPU9250_SENSOR_FIRST_REG PIOS_MPU9250_TEMP_OUT_MSB +#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; + 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; +#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) +{ + + uint8_t power; + uint8_t fifo_store; + + while (PIOS_MPU9250_Test() != 0) { + ; + } + + // Reset chip + while (PIOS_MPU9250_SetReg(PIOS_MPU9250_PWR_MGMT_REG, PIOS_MPU9250_PWRMGMT_IMU_RST) != 0) { + ; + } + + PIOS_DELAY_WaitmS(100); + + + // Reset sensors and fifo + while (PIOS_MPU9250_SetReg(PIOS_MPU9250_USER_CTRL_REG, + PIOS_MPU9250_USERCTL_DIS_I2C | + 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. + power = 0x38; + fifo_store = cfg->Fifo_store; +#if defined(PIOS_MPU9250_ACCEL) + fifo_store |= PIOS_MPU9250_ACCEL_OUT; + power = 0; +#endif +#if defined(PIOS_MPU9250_MAG) + fifo_store |= PIOS_MPU9250_EXT0_OUT; +#endif + while (PIOS_MPU9250_SetReg(PIOS_MPU9250_FIFO_EN_REG, fifo_store) != 0) { + ; + } + 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) +{ + int ret = 0; + + if (PIOS_MPU9250_ClaimBus(false) != 0) { + return -1; + } + + PIOS_SPI_TransferByte(dev->spi_id, 0x7f & reg); + //if (PIOS_SPI_TransferByte(dev->spi_id, 0x7f & reg) != 0) { + // PIOS_MPU9250_ReleaseBus(); + // return -2; + //} + + PIOS_SPI_TransferByte(dev->spi_id, data); + //if (PIOS_SPI_TransferByte(dev->spi_id, data) != 0) { + // PIOS_MPU9250_ReleaseBus(); + // return -3; + //} + + 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) +{ + int32_t data; + + // 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) { + ; + } + + // Set the address of the register to read. + while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_REG, reg) != PIOS_MPU9250_MAG_OK) { + ; + } + + // Trigger the byte transfer. + while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_CTRL, PIOS_MPU9250_I2C_SLV_ENABLE) != PIOS_MPU9250_MAG_OK) { + ; + } + + PIOS_DELAY_WaitmS(1); + + // Read result. + data = PIOS_MPU9250_GetReg(PIOS_MPU9250_I2C_SLV4_DI); + PIOS_DELAY_WaitmS(1); + return data; +} + +/** + * @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) +{ + // Set the I2C slave address. + while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_ADDR, PIOS_MPU9250_MAG_I2C_ADDR) != PIOS_MPU9250_MAG_OK) { + ; + } + + // Set the address of the register to write. + while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_REG, reg) != PIOS_MPU9250_MAG_OK) { + ; + } + + // Set the byte to write. + while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_DO, data) != PIOS_MPU9250_MAG_OK) { + ; + } + + // Trigger the byte transfer. + while (PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV4_CTRL, PIOS_MPU9250_I2C_SLV_ENABLE) != PIOS_MPU9250_MAG_OK) { + ; + } + PIOS_DELAY_WaitmS(1); + 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) +{ + + int i; + + /* Put mag in power down state before changing mode */ + 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); + + if (PIOS_MPU9250_ClaimBus(false) != 0) { + return -1; + } + + /* Set addres and read flag */ + PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_I2C_SLV0_ADDR); + PIOS_SPI_TransferByte(dev->spi_id, PIOS_MPU9250_MAG_I2C_ADDR | PIOS_MPU9250_MAG_I2C_READ_FLAG); + + /* 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); + + /* 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); + + PIOS_DELAY_WaitmS(1); + + /* 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; + } + + PIOS_MPU9250_ReleaseBus(); + + + /* Put mag in power down state before changing mode */ + 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) +{ + // 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); + + // Reset Mag. + PIOS_MPU9250_Mag_SetReg(PIOS_MPU9250_CNTL2, PIOS_MPU9250_MAG_RESET); + PIOS_DELAY_WaitmS(1); + + + // read fuse ROM to get the sensitivity adjustment values. + if (PIOS_MPU9250_Mag_Sensitivity() != PIOS_MPU9250_MAG_OK) { + ; + } + + // Confirm Mag ID. + while (false && (PIOS_MPU9250_Mag_Test() != PIOS_MPU9250_MAG_OK)) { + ; + } + + // 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); + + // 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); + + // 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); + + // Set the address of the register to read. + PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV0_REG, PIOS_MPU9250_ST1); + + // Trigger the byte transfer. + PIOS_MPU9250_SetReg(PIOS_MPU9250_I2C_SLV0_CTRL, PIOS_MPU9250_I2C_SLV_ENABLE | 0x8); + PIOS_DELAY_WaitmS(1); + + return PIOS_MPU9250_MAG_OK; +} + +/* + * @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) +{ + 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); + + PIOS_MPU9250_ReleaseBusISR(woken); + + return true; +} +#endif + + +/** + * @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 + 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 + 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 + 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 + 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 + data.mag_y = GET_SENSOR_DATA(mpu9250_data, Mag_X); // chip X + data.mag_x = GET_SENSOR_DATA(mpu9250_data, Mag_Y); // chip Y + data.mag_z = GET_SENSOR_DATA(mpu9250_data, Mag_Z); // chip Z + data.mag_valid = mpu9250_data.data.st1 & PIOS_MPU9250_MAG_DATA_RDY; +#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; + 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 */ + +/** + * @} + * @} + */ diff --git a/flight/pios/inc/pios_mpu9250.h b/flight/pios/inc/pios_mpu9250.h new file mode 100644 index 000000000..070ac411e --- /dev/null +++ b/flight/pios/inc/pios_mpu9250.h @@ -0,0 +1,266 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_MPU9250 MPU9250 Functions + * @brief Deals with the hardware interface to the 3-axis gyro + * @{ + * + * @file PIOS_MPU9250.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012. + * @brief MPU9250 3-axis gyro function headers + * @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 + */ + +#ifndef PIOS_MPU9250_H +#define PIOS_MPU9250_H + +/* MPU9250 Addresses */ +#define PIOS_MPU9250_SMPLRT_DIV_REG 0X19 +#define PIOS_MPU9250_DLPF_CFG_REG 0X1A +#define PIOS_MPU9250_GYRO_CFG_REG 0X1B +#define PIOS_MPU9250_ACCEL_CFG_REG 0X1C +#define PIOS_MPU9250_FIFO_EN_REG 0x23 +#define PIOS_MPU9250_I2C_MST_CTRL 0x24 +#define PIOS_MPU9250_I2C_SLV0_ADDR 0x25 +#define PIOS_MPU9250_I2C_SLV0_REG 0x26 +#define PIOS_MPU9250_I2C_SLV0_CTRL 0x27 +#define PIOS_MPU9250_I2C_SLV1_ADDR 0x28 +#define PIOS_MPU9250_I2C_SLV1_REG 0x29 +#define PIOS_MPU9250_I2C_SLV1_CTRL 0x2A +#define PIOS_MPU9250_I2C_SLV4_ADDR 0x31 +#define PIOS_MPU9250_I2C_SLV4_REG 0x32 +#define PIOS_MPU9250_I2C_SLV4_DO 0x33 +#define PIOS_MPU9250_I2C_SLV4_CTRL 0x34 +#define PIOS_MPU9250_I2C_SLV4_DI 0x35 +#define PIOS_MPU9250_INT_CFG_REG 0x37 +#define PIOS_MPU9250_INT_EN_REG 0x38 +#define PIOS_MPU9250_INT_STATUS_REG 0x3A +#define PIOS_MPU9250_ACCEL_X_OUT_MSB 0x3B +#define PIOS_MPU9250_ACCEL_X_OUT_LSB 0x3C +#define PIOS_MPU9250_ACCEL_Y_OUT_MSB 0x3D +#define PIOS_MPU9250_ACCEL_Y_OUT_LSB 0x3E +#define PIOS_MPU9250_ACCEL_Z_OUT_MSB 0x3F +#define PIOS_MPU9250_ACCEL_Z_OUT_LSB 0x40 +#define PIOS_MPU9250_TEMP_OUT_MSB 0x41 +#define PIOS_MPU9250_TEMP_OUT_LSB 0x42 +#define PIOS_MPU9250_GYRO_X_OUT_MSB 0x43 +#define PIOS_MPU9250_GYRO_X_OUT_LSB 0x44 +#define PIOS_MPU9250_GYRO_Y_OUT_MSB 0x45 +#define PIOS_MPU9250_GYRO_Y_OUT_LSB 0x46 +#define PIOS_MPU9250_GYRO_Z_OUT_MSB 0x47 +#define PIOS_MPU9250_GYRO_Z_OUT_LSB 0x48 +#define PIOS_MPU9250_EXT_SENS_DATA_00 0x49 +#define PIOS_MPU9250_I2C_SLV0_DO 0x63 +#define PIOS_MPU9250_I2C_SLV1_DO 0x64 +#define PIOS_MPU9250_I2C_MST_DELAY_CTRL 0x67 +#define PIOS_MPU9250_USER_CTRL_REG 0x6A +#define PIOS_MPU9250_PWR_MGMT_REG 0x6B +#define PIOS_MPU9250_PWR_MGMT2_REG 0x6C +#define PIOS_MPU9250_FIFO_CNT_MSB 0x72 +#define PIOS_MPU9250_FIFO_CNT_LSB 0x73 +#define PIOS_MPU9250_FIFO_REG 0x74 +#define PIOS_MPU9250_WHOAMI 0x75 + +/* FIFO enable for storing different values */ +#define PIOS_MPU9250_FIFO_TEMP_OUT 0x80 +#define PIOS_MPU9250_FIFO_GYRO_X_OUT 0x40 +#define PIOS_MPU9250_FIFO_GYRO_Y_OUT 0x20 +#define PIOS_MPU9250_FIFO_GYRO_Z_OUT 0x10 +#define PIOS_MPU9250_ACCEL_OUT 0x08 + +/* Interrupt Configuration */ +#define PIOS_MPU9250_INT_ACTL 0x80 +#define PIOS_MPU9250_INT_OPEN 0x40 +#define PIOS_MPU9250_INT_LATCH_EN 0x20 +#define PIOS_MPU9250_INT_CLR_ANYRD 0x10 + +#define PIOS_MPU9250_INTEN_OVERFLOW 0x10 +#define PIOS_MPU9250_INTEN_DATA_RDY 0x01 + +/* Interrupt status */ +#define PIOS_MPU9250_INT_STATUS_FIFO_FULL 0x80 +#define PIOS_MPU9250_INT_STATUS_FIFO_OVERFLOW 0x10 +#define PIOS_MPU9250_INT_STATUS_IMU_RDY 0X04 +#define PIOS_MPU9250_INT_STATUS_DATA_RDY 0X01 + +/* User control functionality */ +#define PIOS_MPU9250_USERCTL_FIFO_EN 0X40 +#define PIOS_MPU9250_USERCTL_I2C_MST_EN 0x20 +#define PIOS_MPU9250_USERCTL_DIS_I2C 0X10 +#define PIOS_MPU9250_USERCTL_FIFO_RST 0X04 +#define PIOS_MPU9250_USERCTL_SIG_COND 0X01 +#define PIOS_MPU9250_USERCTL_I2C_MST_RST 0X02 + +/* Power management and clock selection */ +#define PIOS_MPU9250_PWRMGMT_IMU_RST 0X80 +#define PIOS_MPU9250_PWRMGMT_INTERN_CLK 0X00 +#define PIOS_MPU9250_PWRMGMT_PLL_X_CLK 0X01 +#define PIOS_MPU9250_PWRMGMT_PLL_Y_CLK 0X02 +#define PIOS_MPU9250_PWRMGMT_PLL_Z_CLK 0X03 +#define PIOS_MPU9250_PWRMGMT_STOP_CLK 0X07 + +/* I2C */ +#define PIOS_MPU9250_I2C_MST_ENABLE 0x80 +#define PIOS_MPU9250_I2C_SLV_ENABLE 0x80 +#define PIOS_MPU9250_I2C_MST_CLOCK_400 0x0D +#define PIOS_MPU9250_I2C_MST_P_NSR 0x10 +#define PIOS_MPU9250_EXT0_OUT 0x01 + +/* AK893 MAG registers */ +/* Read-only Register */ +#define PIOS_MPU9250_WIA 0X00 +#define PIOS_MPU9250_INFO 0X01 +#define PIOS_MPU9250_ST1 0X02 +#define PIOS_MPU9250_HXL 0X03 +#define PIOS_MPU9250_HXH 0X04 +#define PIOS_MPU9250_HYL 0X05 +#define PIOS_MPU9250_HYH 0X06 +#define PIOS_MPU9250_HZL 0X07 +#define PIOS_MPU9250_HZH 0X08 +#define PIOS_MPU9250_ST2 0X09 +/* Write/read Register */ +#define PIOS_MPU9250_CNTL1 0X0A +#define PIOS_MPU9250_CNTL2 0X0B +#define PIOS_MPU9250_ASTC 0X0C +#define PIOS_MPU9250_TS1 0X0D +#define PIOS_MPU9250_TS2 0X0E +#define PIOS_MPU9250_I2CDIS 0X0F +/* Read-only Register */ +#define PIOS_MPU9250_ASAX 0X10 +#define PIOS_MPU9250_ASAY 0X11 +#define PIOS_MPU9250_ASAZ 0X12 + +/* IDs */ +#define PIOS_MPU9250_GYRO_ACC_ID 0x71 +#define PIOS_MPU9250_MAG_ID 0x48 + +#define PIOS_MPU9250_MAG_DATA_RDY 0x01 +#define PIOS_MPU9250_MAG_RESET 0x01 +#define PIOS_MPU9250_MAG_POWER_DOWN_MODE 0x00 +#define PIOS_MPU9250_MAG_SINGLE_MODE 0x01 +#define PIOS_MPU9250_MAG_CONTINUOUS_MODE1 0x02 +#define PIOS_MPU9250_MAG_CONTINUOUS_MODE2 0x06 +#define PIOS_MPU9250_MAG_FUSE_ROM_MODE 0x0F +#define PIOS_MPU9250_MAG_OUTPUT_16BITS 0x10 +#define PIOS_MPU9250_MAG_I2C_ADDR 0x0C +#define PIOS_MPU9250_MAG_I2C_READ_FLAG 0x80 +#define PIOS_MPU9250_MAG_ASA_NB_BYTE 3 +#define PIOS_MPU9250_MAG_ASAX_IDX 1 +#define PIOS_MPU9250_MAG_ASAY_IDX 2 +#define PIOS_MPU9250_MAG_ASAZ_IDX 3 + +#define PIOS_MPU9250_MAG_OK 0 +#define PIOS_MPU9250_ERR_MAG_SET_ADDR -1 +#define PIOS_MPU9250_ERR_MAG_SET_REG -2 +#define PIOS_MPU9250_ERR_MAG_SET_DO -3 +#define PIOS_MPU9250_ERR_MAG_SET_TRIGGER -4 +#define PIOS_MPU9250_ERR_MAG_READ_ID -5 +#define PIOS_MPU9250_ERR_MAG_BAD_ID -6 +#define PIOS_MPU9250_ERR_MAG_READ_ASA -7 + +enum pios_mpu9250_range { + PIOS_MPU9250_SCALE_250_DEG = 0x00, + PIOS_MPU9250_SCALE_500_DEG = 0x08, + PIOS_MPU9250_SCALE_1000_DEG = 0x10, + PIOS_MPU9250_SCALE_2000_DEG = 0x18 +}; + +enum pios_mpu9250_filter { + PIOS_MPU9250_LOWPASS_256_HZ = 0x00, + PIOS_MPU9250_LOWPASS_188_HZ = 0x01, + PIOS_MPU9250_LOWPASS_98_HZ = 0x02, + PIOS_MPU9250_LOWPASS_42_HZ = 0x03, + PIOS_MPU9250_LOWPASS_20_HZ = 0x04, + PIOS_MPU9250_LOWPASS_10_HZ = 0x05, + PIOS_MPU9250_LOWPASS_5_HZ = 0x06 +}; + +enum pios_mpu9250_accel_range { + PIOS_MPU9250_ACCEL_2G = 0x00, + PIOS_MPU9250_ACCEL_4G = 0x08, + PIOS_MPU9250_ACCEL_8G = 0x10, + PIOS_MPU9250_ACCEL_16G = 0x18 +}; + +enum pios_mpu9250_orientation { // clockwise rotation from board forward + PIOS_MPU9250_TOP_0DEG = 0x00, + PIOS_MPU9250_TOP_90DEG = 0x01, + PIOS_MPU9250_TOP_180DEG = 0x02, + PIOS_MPU9250_TOP_270DEG = 0x03 +}; + +struct pios_mpu9250_data { + int16_t gyro_x; + int16_t gyro_y; + int16_t gyro_z; +#if defined(PIOS_MPU9250_ACCEL) + int16_t accel_x; + int16_t accel_y; + int16_t accel_z; +#endif /* PIOS_MPU9250_ACCEL */ +#if defined(PIOS_MPU9250_MAG) + int16_t mag_x; + int16_t mag_y; + int16_t mag_z; + int8_t mag_valid; +#endif /* PIOS_MPU9250_MAG */ + int16_t temperature; +}; + + + +struct pios_mpu9250_cfg { + const struct pios_exti_cfg *exti_cfg; /* Pointer to the EXTI configuration */ + + uint8_t Fifo_store; /* FIFO storage of different readings (See datasheet page 31 for more details) */ + + /* Sample rate divider to use (See datasheet page 32 for more details).*/ + uint8_t Smpl_rate_div_no_dlp; /* used when no dlp is applied (fs=8KHz)*/ + uint8_t Smpl_rate_div_dlp; /* used when dlp is on (fs=1kHz)*/ + uint8_t interrupt_cfg; /* Interrupt configuration (See datasheet page 35 for more details) */ + uint8_t interrupt_en; /* Interrupt configuration (See datasheet page 35 for more details) */ + uint8_t User_ctl; /* User control settings (See datasheet page 41 for more details) */ + uint8_t Pwr_mgmt_clk; /* Power management and clock selection (See datasheet page 32 for more details) */ + enum pios_mpu9250_accel_range accel_range; + enum pios_mpu9250_range gyro_range; + enum pios_mpu9250_filter filter; + enum pios_mpu9250_orientation orientation; + SPIPrescalerTypeDef fast_prescaler; + SPIPrescalerTypeDef std_prescaler; + uint8_t max_downsample; +}; + +/* Public Functions */ +extern int32_t PIOS_MPU9250_Init(uint32_t spi_id, uint32_t slave_num, const struct pios_mpu9250_cfg *new_cfg); +extern int32_t PIOS_MPU9250_ConfigureRanges(enum pios_mpu9250_range gyroRange, enum pios_mpu9250_accel_range accelRange, enum pios_mpu9250_filter filterSetting); +extern xQueueHandle PIOS_MPU9250_GetQueue(); +extern int32_t PIOS_MPU9250_ReadID(); +extern int32_t PIOS_MPU9250_Test(); +extern float PIOS_MPU9250_GetScale(); +extern float PIOS_MPU9250_GetAccelScale(); +extern bool PIOS_MPU9250_IRQHandler(void); + +#endif /* PIOS_MPU9250_H */ + +/** + * @} + * @} + */ diff --git a/flight/pios/inc/pios_mpu9250_config.h b/flight/pios/inc/pios_mpu9250_config.h new file mode 100644 index 000000000..b354ec6cf --- /dev/null +++ b/flight/pios/inc/pios_mpu9250_config.h @@ -0,0 +1,74 @@ +/** + ****************************************************************************** + * @addtogroup PIOS PIOS Core hardware abstraction layer + * @{ + * @addtogroup PIOS_MPU9250 OpenPilot layer configuration utilities + * @brief provides mpu9250 configuration helpers function + * @{ + * + * @file PIOS_MPU9250_CONFIG.h + * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2013. + * @brief MPU9250 UAVO-based configuration functions + * @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 + */ + +#ifndef PIOS_MPU9250_CONFIG_H +#define PIOS_MPU9250_CONFIG_H + +#include "mpu9250settings.h" +#include "pios_mpu9250.h" + +#define PIOS_MPU9250_CONFIG_MAP_GYROSCALE(x) \ + (x == MPU9250SETTINGS_GYROSCALE_SCALE_250 ? PIOS_MPU9250_SCALE_250_DEG : \ + x == MPU9250SETTINGS_GYROSCALE_SCALE_500 ? PIOS_MPU9250_SCALE_500_DEG : \ + x == MPU9250SETTINGS_GYROSCALE_SCALE_1000 ? PIOS_MPU9250_SCALE_1000_DEG : \ + PIOS_MPU9250_SCALE_2000_DEG) + +#define PIOS_MPU9250_CONFIG_MAP_ACCELSCALE(x) \ + (x == MPU9250SETTINGS_ACCELSCALE_SCALE_2G ? PIOS_MPU9250_ACCEL_2G : \ + x == MPU9250SETTINGS_ACCELSCALE_SCALE_4G ? PIOS_MPU9250_ACCEL_4G : \ + x == MPU9250SETTINGS_ACCELSCALE_SCALE_16G ? PIOS_MPU9250_ACCEL_16G : \ + PIOS_MPU9250_ACCEL_8G) + +#define PIOS_MPU9250_CONFIG_MAP_FILTERSETTING(x) \ + (x == MPU9250SETTINGS_FILTERSETTING_LOWPASS_188_HZ ? PIOS_MPU9250_LOWPASS_188_HZ : \ + x == MPU9250SETTINGS_FILTERSETTING_LOWPASS_98_HZ ? PIOS_MPU9250_LOWPASS_98_HZ : \ + x == MPU9250SETTINGS_FILTERSETTING_LOWPASS_42_HZ ? PIOS_MPU9250_LOWPASS_42_HZ : \ + x == MPU9250SETTINGS_FILTERSETTING_LOWPASS_20_HZ ? PIOS_MPU9250_LOWPASS_20_HZ : \ + x == MPU9250SETTINGS_FILTERSETTING_LOWPASS_10_HZ ? PIOS_MPU9250_LOWPASS_10_HZ : \ + x == MPU9250SETTINGS_FILTERSETTING_LOWPASS_5_HZ ? PIOS_MPU9250_LOWPASS_5_HZ : \ + PIOS_MPU9250_LOWPASS_256_HZ) +/** + * @brief Updates MPU9250 config based on Mpu9250Settings UAVO + * @returns 0 if succeed or -1 otherwise + */ +int32_t PIOS_MPU9250_CONFIG_Configure() +{ + Mpu9250SettingsInitialize(); + Mpu9250SettingsData mpu9250settings; + Mpu9250SettingsGet(&mpu9250settings); + return PIOS_MPU9250_ConfigureRanges( + PIOS_MPU9250_CONFIG_MAP_GYROSCALE(mpu9250settings.GyroScale), + PIOS_MPU9250_CONFIG_MAP_ACCELSCALE(mpu9250settings.AccelScale), + PIOS_MPU9250_CONFIG_MAP_FILTERSETTING(mpu9250settings.FilterSetting) + ); +} + +#endif /* PIOS_MPU9250_CONFIG_H */ diff --git a/ground/openpilotgcs/src/plugins/uavobjects/uavobjects.pro b/ground/openpilotgcs/src/plugins/uavobjects/uavobjects.pro index 569a5020b..6248db679 100644 --- a/ground/openpilotgcs/src/plugins/uavobjects/uavobjects.pro +++ b/ground/openpilotgcs/src/plugins/uavobjects/uavobjects.pro @@ -132,6 +132,7 @@ HEADERS += \ $$UAVOBJECT_SYNTHETICS/waypoint.h \ $$UAVOBJECT_SYNTHETICS/waypointactive.h \ $$UAVOBJECT_SYNTHETICS/mpu6000settings.h \ + $$UAVOBJECT_SYNTHETICS/mpu9250settings.h \ $$UAVOBJECT_SYNTHETICS/takeofflocation.h \ $$UAVOBJECT_SYNTHETICS/auxmagsensor.h \ $$UAVOBJECT_SYNTHETICS/auxmagsettings.h \ @@ -245,6 +246,7 @@ SOURCES += \ $$UAVOBJECT_SYNTHETICS/waypoint.cpp \ $$UAVOBJECT_SYNTHETICS/waypointactive.cpp \ $$UAVOBJECT_SYNTHETICS/mpu6000settings.cpp \ + $$UAVOBJECT_SYNTHETICS/mpu9250settings.cpp \ $$UAVOBJECT_SYNTHETICS/takeofflocation.cpp \ $$UAVOBJECT_SYNTHETICS/auxmagsensor.cpp \ $$UAVOBJECT_SYNTHETICS/auxmagsettings.cpp \ diff --git a/make/apps-defs.mk b/make/apps-defs.mk index 8754c05e7..9d7b92b04 100644 --- a/make/apps-defs.mk +++ b/make/apps-defs.mk @@ -74,6 +74,7 @@ SRC += $(PIOSCOMMON)/pios_hmc5x83.c SRC += $(PIOSCOMMON)/pios_i2c_esc.c SRC += $(PIOSCOMMON)/pios_l3gd20.c SRC += $(PIOSCOMMON)/pios_mpu6000.c +SRC += $(PIOSCOMMON)/pios_mpu9250.c SRC += $(PIOSCOMMON)/pios_mpxv.c SRC += $(PIOSCOMMON)/pios_ms4525do.c SRC += $(PIOSCOMMON)/pios_ms5611.c diff --git a/shared/uavobjectdefinition/mpu9250settings.xml b/shared/uavobjectdefinition/mpu9250settings.xml new file mode 100644 index 000000000..199e4e020 --- /dev/null +++ b/shared/uavobjectdefinition/mpu9250settings.xml @@ -0,0 +1,38 @@ + + + Settings for the @ref MPU9250 sensor used on revobone. Reboot the board for this to takes effect + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +