1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-01-30 15:52:12 +01:00

Add SPI MPU6000 driver. Unfortunate hack in HMC5883 EXTI handler to call

MPU6000.  Need to generalize EXTI ASAP.
This commit is contained in:
James Cotton 2011-11-17 10:11:56 -06:00
parent 8477c2229b
commit d97e5dbc59
3 changed files with 544 additions and 0 deletions

View File

@ -410,12 +410,18 @@ void PIOS_HMC5883_IRQHandler(void)
* Soon this will be generic in pios_exti and the BMA180 will register * Soon this will be generic in pios_exti and the BMA180 will register
* against it. Right now this is crap! * against it. Right now this is crap!
*/ */
extern void PIOS_MPU6000_IRQHandler();
void EXTI9_5_IRQHandler(void) void EXTI9_5_IRQHandler(void)
{ {
if (EXTI_GetITStatus(dev_cfg->eoc_exti.init.EXTI_Line) != RESET) { if (EXTI_GetITStatus(dev_cfg->eoc_exti.init.EXTI_Line) != RESET) {
PIOS_HMC5883_IRQHandler(); PIOS_HMC5883_IRQHandler();
EXTI_ClearITPendingBit(dev_cfg->eoc_exti.init.EXTI_Line); EXTI_ClearITPendingBit(dev_cfg->eoc_exti.init.EXTI_Line);
} }
if (EXTI_GetITStatus(EXTI_Line8) != RESET) {
PIOS_MPU6000_IRQHandler();
EXTI_ClearITPendingBit(EXTI_Line8);
}
} }

View File

@ -0,0 +1,380 @@
/**
******************************************************************************
* @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
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
* @brief MPU6000 6-axis gyro and accel 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
*/
/* Project Includes */
#include "pios.h"
#if defined(PIOS_INCLUDE_MPU6000)
/* Global Variables */
uint32_t pios_spi_gyro;
/* Local Variables */
#define DEG_TO_RAD (M_PI / 180.0)
static void PIOS_MPU6000_Config(struct pios_mpu6000_cfg const * cfg);
static int32_t PIOS_MPU6000_SetReg(uint8_t address, uint8_t buffer);
static int32_t PIOS_MPU6000_GetReg(uint8_t address);
#define PIOS_MPU6000_MAX_DOWNSAMPLE 100
static int16_t pios_mpu6000_buffer[PIOS_MPU6000_MAX_DOWNSAMPLE * sizeof(struct pios_mpu6000_data)];
static t_fifo_buffer pios_mpu6000_fifo;
volatile bool mpu6000_configured = false;
volatile bool mpu6000_cb_ready = true;
static struct pios_mpu6000_cfg const * cfg;
/**
* @brief Initialize the MPU6050 3-axis gyro sensor.
* @return none
*/
void PIOS_MPU6000_Init(const struct pios_mpu6000_cfg * new_cfg)
{
cfg = new_cfg;
fifoBuf_init(&pios_mpu6000_fifo, (uint8_t *) pios_mpu6000_buffer, sizeof(pios_mpu6000_buffer));
/* Configure EOC pin as input floating */
GPIO_Init(cfg->drdy.gpio, &cfg->drdy.init);
/* Configure the End Of Conversion (EOC) interrupt */
SYSCFG_EXTILineConfig(cfg->eoc_exti.port_source, cfg->eoc_exti.pin_source);
EXTI_Init(&cfg->eoc_exti.init);
/* Enable and set EOC EXTI Interrupt to the lowest priority */
NVIC_Init(&cfg->eoc_irq.init);
/* Configure the MPU6050 Sensor */
PIOS_MPU6000_Config(cfg);
}
/**
* @brief Initialize the MPU6050 3-axis gyro sensor
* \return none
* \param[in] PIOS_MPU6000_ConfigTypeDef struct to be used to configure sensor.
*
*/
static void PIOS_MPU6000_Config(struct pios_mpu6000_cfg const * cfg)
{
mpu6000_cb_ready = true;
// Reset chip and fifo
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_USER_CTRL_REG, 0x01 | 0x02 | 0x04) != 0);
PIOS_DELAY_WaituS(20);
// FIFO storage
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_FIFO_EN_REG, cfg->Fifo_store) != 0);
// Sample rate divider
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_SMPLRT_DIV_REG, cfg->Smpl_rate_div) != 0) ;
// Digital low-pass filter and scale
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_DLPF_CFG_REG, cfg->filter) != 0) ;
// Digital low-pass filter and scale
while (PIOS_MPU6000_SetReg(PIOS_MPU6000_GYRO_CFG_REG, cfg->gyro_range) != 0) ;
// 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;
}
/**
* @brief Claim the SPI bus for the accel communications and select this chip
* @return 0 if successful, -1 if unable to claim bus
*/
int32_t PIOS_MPU6000_ClaimBus()
{
if(PIOS_SPI_ClaimBus(pios_spi_gyro) != 0)
return -1;
PIOS_SPI_RC_PinSet(pios_spi_gyro,0,0);
return 0;
}
/**
* @brief Release the SPI bus for the accel communications and end the transaction
* @return 0 if successful
*/
int32_t PIOS_MPU6000_ReleaseBus()
{
PIOS_SPI_RC_PinSet(pios_spi_gyro,0,1);
return PIOS_SPI_ReleaseBus(pios_spi_gyro);
}
/**
* @brief Connect to the correct SPI bus
*/
void PIOS_MPU6000_Attach(uint32_t spi_id)
{
pios_spi_gyro = spi_id;
}
/**
* @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)
{
uint8_t data;
if(PIOS_MPU6000_ClaimBus() != 0)
return -1;
PIOS_SPI_TransferByte(pios_spi_gyro,(0x80 | reg) ); // request byte
data = PIOS_SPI_TransferByte(pios_spi_gyro,0 ); // receive response
PIOS_MPU6000_ReleaseBus();
return data;
}
/**
* @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)
{
if(PIOS_MPU6000_ClaimBus() != 0)
return -1;
if(PIOS_SPI_TransferByte(pios_spi_gyro, 0x7f & reg) != 0) {
PIOS_MPU6000_ReleaseBus();
return -2;
}
if(PIOS_SPI_TransferByte(pios_spi_gyro, data) != 0) {
PIOS_MPU6000_ReleaseBus();
return -3;
}
PIOS_MPU6000_ReleaseBus();
return 0;
}
/**
* @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
* \returns The number of samples remaining in the fifo
*/
int32_t PIOS_MPU6000_ReadGyros(struct pios_mpu6000_data * data)
{
uint8_t buf[7] = {PIOS_MPU6000_GYRO_X_OUT_MSB | 0x80, 0, 0, 0, 0, 0, 0};
uint8_t rec[7];
if(PIOS_MPU6000_ClaimBus() != 0)
return -1;
/*
if(PIOS_SPI_TransferBlock(pios_spi_gyro, &buf[0], &rec[0], sizeof(buf), NULL) < 0)
return -2;
*/
PIOS_SPI_TransferByte(pios_spi_gyro,(0x80 | PIOS_MPU6000_GYRO_X_OUT_MSB) ); // request byte
rec[1] = PIOS_SPI_TransferByte(pios_spi_gyro,0 ); // receive response
rec[2] = PIOS_SPI_TransferByte(pios_spi_gyro,0 ); // receive response
rec[3] = PIOS_SPI_TransferByte(pios_spi_gyro,0 ); // receive response
rec[4] = PIOS_SPI_TransferByte(pios_spi_gyro,0 ); // receive response
rec[5] = PIOS_SPI_TransferByte(pios_spi_gyro,0 ); // receive response
rec[6] = PIOS_SPI_TransferByte(pios_spi_gyro,0 ); // receive response
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;
}
/**
* @brief Read the identification bytes from the MPU6050 sensor
* \return ID read from MPU6050 or -1 if failure
*/
int32_t PIOS_MPU6000_ReadID()
{
int32_t mpu6000_id = PIOS_MPU6000_GetReg(PIOS_MPU6000_WHOAMI);
if(mpu6000_id < 0)
return -1;
return mpu6000_id;
}
/**
* \brief Reads the data from the MPU6050 FIFO
* \param[out] buffer destination buffer
* \param[in] len maximum number of bytes which should be read
* \note This returns the data as X, Y, Z the temperature
* \return number of bytes transferred if operation was successful
* \return -1 if error during I2C transfer
*/
int32_t PIOS_MPU6000_ReadFifo(struct pios_mpu6000_data * buffer)
{
if(fifoBuf_getUsed(&pios_mpu6000_fifo) < sizeof(*buffer))
return -1;
fifoBuf_getData(&pios_mpu6000_fifo, (uint8_t *) buffer, sizeof(*buffer));
return 0;
}
float PIOS_MPU6000_GetScale()
{
switch (cfg->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;
}
/**
* @brief Run self-test operation.
* \return 0 if test succeeded
* \return non-zero value if test succeeded
*/
uint8_t PIOS_MPU6000_Test(void)
{
/* Verify that ID matches (MPU6050 ID is 0x69) */
int32_t mpu6000_id = PIOS_MPU6000_ReadID();
if(mpu6000_id < 0)
return -1;
if(mpu6000_id != 0x68);
return -2;
return 0;
}
/**
* @brief IRQ Handler. Read all the data from onboard buffer
*/
uint32_t mpu6000_irq = 0;
uint32_t mpu6000_count;
uint32_t mpu6000_fifo_full = 0;
void PIOS_MPU6000_IRQHandler(void)
{
if(!mpu6000_configured)
return;
struct pios_mpu6000_data data;
mpu6000_irq++;
if(PIOS_MPU6000_ClaimBus() != 0)
return;
// Get number of bytes in the fifo
uint8_t buf[3] = {PIOS_MPU6000_FIFO_CNT_MSB | 0x80, 0, 0};
uint8_t rec[3];
if(PIOS_SPI_TransferBlock(pios_spi_gyro, &buf[0], &rec[0], sizeof(buf), NULL) < 0) {
PIOS_MPU6000_ReleaseBus();
return;
}
mpu6000_count = rec[1] << 8 | rec[2];
uint32_t count = mpu6000_count > 40 ? 40 : mpu6000_count;
while(count--) {
if(fifoBuf_getFree(&pios_mpu6000_fifo) < sizeof(data)) {
PIOS_MPU6000_ReleaseBus();
mpu6000_fifo_full++;
return;
}
uint8_t buf[1+8] = {PIOS_MPU6000_FIFO_REG | 0x80, 0, 0, 0, 0, 0, 0, 0, 0};
uint8_t rec[1+8];
if(PIOS_SPI_TransferBlock(pios_spi_gyro, &buf[0], &rec[0], sizeof(buf), NULL) < 0) {
PIOS_MPU6000_ReleaseBus();
return;
}
data.temperature = rec[1] << 8 | rec[2];
data.gyro_x = rec[3] << 8 | rec[4];
data.gyro_y = rec[5] << 8 | rec[6];
data.gyro_z = rec[7] << 8 | rec[8];
fifoBuf_putData(&pios_mpu6000_fifo, (uint8_t *) &data, sizeof(data));
}
PIOS_MPU6000_ReleaseBus();
mpu6000_irq++;
}
#if defined(PIOS_INCLUDE_MPU6000)
/**
* The physical IRQ handler
* Soon this will be generic in pios_exti and the BMA180 will register
* against it. Right now this is crap!
*/
void EXTI1_IRQHandler(void)
{
if (EXTI_GetITStatus(EXTI_Line1) != RESET)
{
PIOS_MPU6000_IRQHandler();
EXTI_ClearITPendingBit(EXTI_Line1);
}
}
#endif
#endif
/**
* @}
* @}
*/

View File

@ -0,0 +1,158 @@
/**
******************************************************************************
* @addtogroup PIOS PIOS Core hardware abstraction layer
* @{
* @addtogroup PIOS_MPU6000 MPU6050 Functions
* @brief Deals with the hardware interface to the 3-axis gyro
* @{
*
* @file PIOS_MPU6000.h
* @author David "Buzz" Carlson (buzz@chebuzz.com)
* The OpenPilot Team, http://www.openpilot.org Copyright (C) 2011.
* @brief MPU6050 3-axis gyor 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_MPU6000_H
#define PIOS_MPU6000_H
#include "pios.h"
/* MPU6050 Addresses */
#define PIOS_MPU6000_SMPLRT_DIV_REG 0X19
#define PIOS_MPU6000_DLPF_CFG_REG 0X1A
#define PIOS_MPU6000_GYRO_CFG_REG 0X1B
#define PIOS_MPU6000_ACCEL_CFG_REG 0X1C
#define PIOS_MPU6000_FIFO_EN_REG 0x23
#define PIOS_MPU6000_INT_CFG_REG 0x37
#define PIOS_MPU6000_INT_EN_REG 0x38
#define PIOS_MPU6000_INT_STATUS_REG 0x3A
#define PIOS_MPU6000_ACCEL_X_OUT_MSB 0x3B
#define PIOS_MPU6000_ACCEL_X_OUT_LSB 0x3C
#define PIOS_MPU6000_ACCEL_Y_OUT_MSB 0x3D
#define PIOS_MPU6000_ACCEL_Y_OUT_LSB 0x3E
#define PIOS_MPU6000_ACCEL_Z_OUT_MSB 0x3F
#define PIOS_MPU6000_ACCEL_Z_OUT_LSB 0x40
#define PIOS_MPU6000_TEMP_OUT_MSB 0x41
#define PIOS_MPU6000_TEMP_OUT_LSB 0x42
#define PIOS_MPU6000_GYRO_X_OUT_MSB 0x43
#define PIOS_MPU6000_GYRO_X_OUT_LSB 0x44
#define PIOS_MPU6000_GYRO_Y_OUT_MSB 0x45
#define PIOS_MPU6000_GYRO_Y_OUT_LSB 0x46
#define PIOS_MPU6000_GYRO_Z_OUT_MSB 0x47
#define PIOS_MPU6000_GYRO_Z_OUT_LSB 0x48
#define PIOS_MPU6000_USER_CTRL_REG 0x6A
#define PIOS_MPU6000_PWR_MGMT_REG 0x6B
#define PIOS_MPU6000_FIFO_CNT_MSB 0x72
#define PIOS_MPU6000_FIFO_CNT_LSB 0x73
#define PIOS_MPU6000_FIFO_REG 0x74
#define PIOS_MPU6000_WHOAMI 0x75
/* FIFO enable for storing different values */
#define PIOS_MPU6000_FIFO_TEMP_OUT 0x80
#define PIOS_MPU6000_FIFO_GYRO_X_OUT 0x40
#define PIOS_MPU6000_FIFO_GYRO_Y_OUT 0x20
#define PIOS_MPU6000_FIFO_GYRO_Z_OUT 0x10
#define PIOS_MPU6000_ACCEL_OUT 0x04
/* Interrupt Configuration */
#define PIOS_MPU6000_INT_ACTL 0x80
#define PIOS_MPU6000_INT_OPEN 0x40
#define PIOS_MPU6000_INT_LATCH_EN 0x20
#define PIOS_MPU6000_INT_CLR_ANYRD 0x10
#define PIOS_MPU6000_INTEN_OVERFLOW 0x10
#define PIOS_MPU6000_INTEN_DATA_RDY 0x01
/* Interrupt status */
#define PIOS_MPU6000_INT_STATUS_FIFO_FULL 0x80
#define PIOS_MPU6000_INT_STATUS_IMU_RDY 0X04
#define PIOS_MPU6000_INT_STATUS_DATA_RDY 0X01
/* User control functionality */
#define PIOS_MPU6000_USERCTL_FIFO_EN 0X40
#define PIOS_MPU6000_USERCTL_FIFO_RST 0X02
#define PIOS_MPU6000_USERCTL_GYRO_RST 0X01
/* Power management and clock selection */
#define PIOS_MPU6000_PWRMGMT_IMU_RST 0X80
#define PIOS_MPU6000_PWRMGMT_INTERN_CLK 0X00
#define PIOS_MPU6000_PWRMGMT_PLL_X_CLK 0X01
#define PIOS_MPU6000_PWRMGMT_PLL_Y_CLK 0X02
#define PIOS_MPU6000_PWRMGMT_PLL_Z_CLK 0X03
#define PIOS_MPU6000_PWRMGMT_STOP_CLK 0X07
enum pios_mpu6000_range {
PIOS_MPU6000_SCALE_250_DEG = 0x00,
PIOS_MPU6000_SCALE_500_DEG = 0x08,
PIOS_MPU6000_SCALE_1000_DEG = 0x10,
PIOS_MPU6000_SCALE_2000_DEG = 0x18
};
enum pios_mpu6000_filter {
PIOS_MPU6000_LOWPASS_256_HZ = 0x00,
PIOS_MPU6000_LOWPASS_188_HZ = 0x01,
PIOS_MPU6000_LOWPASS_98_HZ = 0x02,
PIOS_MPU6000_LOWPASS_42_HZ = 0x03,
PIOS_MPU6000_LOWPASS_20_HZ = 0x04,
PIOS_MPU6000_LOWPASS_10_HZ = 0x05,
PIOS_MPU6000_LOWPASS_5_HZ = 0x06
};
struct pios_mpu6000_data {
int16_t gyro_x;
int16_t gyro_y;
int16_t gyro_z;
int16_t accel_x;
int16_t accel_y;
int16_t accel_z;
int16_t temperature;
};
struct pios_mpu6000_cfg {
struct stm32_gpio drdy;
struct stm32_exti eoc_exti;
struct stm32_irq eoc_irq;
uint8_t Fifo_store; /* FIFO storage of different readings (See datasheet page 31 for more details) */
uint8_t Smpl_rate_div; /* Sample rate divider to use (See datasheet page 32 for more details) */
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_mpu6000_range gyro_range;
enum pios_mpu6000_filter filter;
};
/* Public Functions */
extern void PIOS_MPU6000_Init(const struct pios_mpu6000_cfg * cfg);
extern void PIOS_MPU6000_Attach(uint32_t spi_id);
extern int32_t PIOS_MPU6000_ReadFifo(struct pios_mpu6000_data * buffer);
extern int32_t PIOS_MPU6000_ReadGyros(struct pios_mpu6000_data * buffer);
extern int32_t PIOS_MPU6000_ReadID();
extern uint8_t PIOS_MPU6000_Test();
extern float PIOS_MPU6000_GetScale();
#endif /* PIOS_MPU6000_H */
/**
* @}
* @}
*/