1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-05 21:52:10 +01:00

OP-378: Continue updating BMA180 sensor driver.

This commit is contained in:
James Cotton 2011-05-19 16:04:22 -05:00
parent 5d78a68bee
commit 50161b4ad8
2 changed files with 52 additions and 65 deletions

View File

@ -33,8 +33,11 @@
#include "pios.h" #include "pios.h"
static uint32_t PIOS_SPI_ACCEL; static uint32_t PIOS_SPI_ACCEL;
static uint8_t EEPROM_WRITEABLE=0;
static int32_t PIOS_BMA180_GetReg(uint8_t reg);
static int32_t PIOS_BMA180_SetReg(uint8_t reg, uint8_t data);
static int32_t PIOS_BMA180_SelectBW(uint8_t bw);
static int32_t PIOS_BMA180_SetRange(uint8_t range);
/** /**
* @brief Claim the SPI bus for the accel communications and select this chip * @brief Claim the SPI bus for the accel communications and select this chip
@ -58,27 +61,6 @@ int32_t PIOS_BMA180_ReleaseBus()
return PIOS_SPI_ReleaseBus(PIOS_SPI_ACCEL); return PIOS_SPI_ReleaseBus(PIOS_SPI_ACCEL);
} }
/**
* @brief Set the EEPROM write-enable bit. Must be set to 1 (unlocked) before writing control registers.
* @return returns 0 if successful or < 0 if failure
* @param _we[in] bit to set, 1 to enable writes or 0 to disable writes
*/
int32_t PIOS_BMA180_WriteEnable(uint8_t _we)
{
uint8_t addr_reg[2] = {BMA_WE_ADDR,0};
if(PIOS_BMA180_ClaimBus() != 0)
return -1;
addr_reg[1] = PIOS_SPI_TransferByte(PIOS_SPI_ACCEL,(0x80 | BMA_WE_ADDR) );
addr_reg[1] &= 0xEF;
addr_reg[1] |= ( (0x01 & _we) << 4);
PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,addr_reg,NULL,sizeof(addr_reg),NULL);
PIOS_BMA180_ReleaseBus();
EEPROM_WRITEABLE=_we;
return 0;
}
/** /**
* @brief Read a register from BMA180 * @brief Read a register from BMA180
* @returns The register value or -1 if failure to get bus * @returns The register value or -1 if failure to get bus
@ -104,49 +86,46 @@ int32_t PIOS_BMA180_GetReg(uint8_t reg)
* @param reg[in] address of register to be written * @param reg[in] address of register to be written
* @param data[in] data that is to be written to register * @param data[in] data that is to be written to register
*/ */
void PIOS_BMA180_SetReg(uint8_t reg, uint8_t data) int32_t PIOS_BMA180_SetReg(uint8_t reg, uint8_t data)
{ {
uint8_t reg_data[2] = { (0x7F & reg), data}; if(PIOS_BMA180_ClaimBus() != 0)
PIOS_BMA180_ClaimBus(); return -1;
PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,reg_data,NULL,2,NULL);
PIOS_SPI_TransferByte(PIOS_SPI_ACCEL, 0x7f & reg);
PIOS_SPI_TransferByte(PIOS_SPI_ACCEL, data);
PIOS_BMA180_ReleaseBus(); PIOS_BMA180_ReleaseBus();
return 0;
} }
/** /**
* @brief Select the bandwidth the digital filter pass allows. * @brief Select the bandwidth the digital filter pass allows.
* @return none * @return 0 if successful, -1 if not
* @param rate[in] Bandwidth setting to be used * @param rate[in] Bandwidth setting to be used
* *
* EEPROM must be write-enabled before calling this function. * EEPROM must be write-enabled before calling this function.
*/ */
void PIOS_BMA180_SelectBW(uint8_t bw) static int32_t PIOS_BMA180_SelectBW(uint8_t bw)
{ {
uint8_t addr_reg[2] = { BMA_BW_ADDR, 0}; uint8_t reg;
reg = PIOS_BMA180_GetReg(BMA_BW_ADDR);
PIOS_BMA180_ClaimBus(); reg = (reg & ~BMA_BW_MASK) | ((bw << BMA_BW_SHIFT) & BMA_BW_MASK);
addr_reg[1] = PIOS_SPI_TransferByte(PIOS_SPI_ACCEL,(0x80|BMA_BW_ADDR)); return PIOS_BMA180_SetReg(BMA_BW_ADDR, reg);
addr_reg[1] &= 0x0F;
addr_reg[1] |= (bw << 4);
PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,addr_reg,NULL,sizeof(addr_reg),NULL);
PIOS_BMA180_ReleaseBus();
} }
/** /**
* @brief Select the full scale acceleration range. * @brief Select the full scale acceleration range.
* @return none * @return 0 if successful, -1 if not
* @param rate[in] Range setting to be used * @param rate[in] Range setting to be used
* *
*/ */
void PIOS_BMA180_SetRange(uint8_t range) static int32_t PIOS_BMA180_SetRange(uint8_t range)
{ {
uint8_t addr_reg[2] = { BMA_RANGE_ADDR, 0}; uint8_t reg;
reg = PIOS_BMA180_GetReg(BMA_RANGE_ADDR);
PIOS_BMA180_ClaimBus(); reg = (reg & ~BMA_RANGE_MASK) | ((range << BMA_RANGE_SHIFT) & BMA_RANGE_MASK);
addr_reg[1] = PIOS_SPI_TransferByte(PIOS_SPI_ACCEL,(0x80|BMA_RANGE_ADDR)); return PIOS_BMA180_SetReg(BMA_RANGE_ADDR, reg);
addr_reg[1] &= 0x0F;
addr_reg[1] |= (range << 4);
PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,addr_reg,NULL,sizeof(addr_reg),NULL);
PIOS_BMA180_ReleaseBus();
} }
/** /**
@ -162,20 +141,15 @@ void PIOS_BMA180_Attach(uint32_t spi_id)
*/ */
void PIOS_BMA180_Init() void PIOS_BMA180_Init()
{ {
/* if(0){
PIOS_BMA180_ReleaseBus(); PIOS_BMA180_SelectBW(BMA_BW_150HZ);
PIOS_BMA180_WriteEnable(1);
PIOS_BMA180_SelectRate(BMA_RATE_3200);
PIOS_BMA180_SetRange(BMA_RANGE_8G); PIOS_BMA180_SetRange(BMA_RANGE_8G);
PIOS_BMA180_FifoDepth(16); }
PIOS_BMA180_SetMeasure(1);
PIOS_BMA180_WriteEnable(0);
*/
} }
/** /**
* @brief Read a single set of values from the x y z channels * @brief Read a single set of values from the x y z channels
* @returns The number of samples remaining in the fifo or < 0 if failure * @returns 0 if successful
* @retval -1 unable to claim bus * @retval -1 unable to claim bus
* @retval -2 unable to transfer data * @retval -2 unable to transfer data
*/ */
@ -183,9 +157,8 @@ int32_t PIOS_BMA180_Read(struct pios_bma180_data * data)
{ {
// To save memory use same buffer for in and out but offset by // To save memory use same buffer for in and out but offset by
// a byte // a byte
uint8_t buf[7] = {0,0,0,0,0,0}; uint8_t buf[7] = {BMA_X_LSB_ADDR | 0x80,0,0,0,0,0};
uint8_t rec[7] = {0,0,0,0,0,0}; uint8_t rec[7] = {0,0,0,0,0,0};
buf[0] = BMA_X_LSB_ADDR | 0x80 ; // Multibyte read starting at X LSB
if(PIOS_BMA180_ClaimBus() != 0) if(PIOS_BMA180_ClaimBus() != 0)
return -1; return -1;
@ -194,13 +167,22 @@ int32_t PIOS_BMA180_Read(struct pios_bma180_data * data)
PIOS_BMA180_ReleaseBus(); PIOS_BMA180_ReleaseBus();
// | MSB | LSB | 0 | new_data | // | MSB | LSB | 0 | new_data |
data->x = ( (rec[2] << 8) | rec[1] ) >> 2; data->x = ( (rec[2] << 8) | rec[1] ) / 4;
data->y = ( (rec[4] << 8) | rec[3] ) >> 2; data->y = ( (rec[4] << 8) | rec[3] ) / 4;
data->z = ( (rec[6] << 8) | rec[5] ) >> 2; data->z = ( (rec[6] << 8) | rec[5] ) / 4;
return 0; // return number of remaining entries return 0; // return number of remaining entries
} }
/**
* @brief Returns the scale the BMA180 chip is using
* @return Scale (m / s^2) / LSB
*/
float PIOS_BMA_GetScale()
{
return 1;
}
/** /**
* @brief Test SPI and chip functionality by reading chip ID register * @brief Test SPI and chip functionality by reading chip ID register
* @return 0 if success, -1 if failure. * @return 0 if success, -1 if failure.
@ -218,12 +200,16 @@ int32_t PIOS_BMA180_Test()
return -2; return -2;
PIOS_BMA180_ReleaseBus(); PIOS_BMA180_ReleaseBus();
if(rec[1] != 0x3) struct pios_bma180_data data;
if(PIOS_BMA180_Read(&data) != 0)
return -3; return -3;
if(rec[2] < 0x12) if(rec[1] != 0x3)
return -4; return -4;
if(rec[2] < 0x12)
return -5;
return 0; return 0;
} }

View File

@ -44,6 +44,8 @@
#define BMA_RANGE_ADDR 0x35 #define BMA_RANGE_ADDR 0x35
/* Accel range */ /* Accel range */
#define BMA_RANGE_MASK 0x0E
#define BMA_RANGE_SHIFT 1
#define BMA_RANGE_1G 0x00 // +/- 1G ADC resolution 0.13 mg/LSB #define BMA_RANGE_1G 0x00 // +/- 1G ADC resolution 0.13 mg/LSB
#define BMA_RANGE_1_5G 0x01 // +/- 1.5G ADC resolution 0.19 mg/LSB #define BMA_RANGE_1_5G 0x01 // +/- 1.5G ADC resolution 0.19 mg/LSB
#define BMA_RANGE_2G 0x02 // +/- 2G ADC resolution 0.25 mg/LSB *** default *** #define BMA_RANGE_2G 0x02 // +/- 2G ADC resolution 0.25 mg/LSB *** default ***
@ -53,6 +55,8 @@
#define BMA_RANGE_16G 0x06 // +/- 16G ADC resolution 1.98 mg/LSB #define BMA_RANGE_16G 0x06 // +/- 16G ADC resolution 1.98 mg/LSB
/* Measurement bandwidth */ /* Measurement bandwidth */
#define BMA_BW_MASK 0xF0
#define BMA_BW_SHIFT 4
#define BMA_BW_10HZ 0x00 #define BMA_BW_10HZ 0x00
#define BMA_BW_20HZ 0x01 #define BMA_BW_20HZ 0x01
#define BMA_BW_40HZ 0x02 #define BMA_BW_40HZ 0x02
@ -71,9 +75,6 @@ struct pios_bma180_data {
}; };
/* Public Functions */ /* Public Functions */
int32_t PIOS_BMA180_WriteEnable(uint8_t _we);
int32_t PIOS_BMA180_GetReg(uint8_t reg);
void PIOS_BMA180_SetReg(uint8_t reg, uint8_t data);
void PIOS_BMA180_Attach(uint32_t spi_id); void PIOS_BMA180_Attach(uint32_t spi_id);
void PIOS_BMA180_Init(); void PIOS_BMA180_Init();
int32_t PIOS_BMA180_Read(struct pios_bma180_data * data); int32_t PIOS_BMA180_Read(struct pios_bma180_data * data);