1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2025-02-20 10:54:14 +01:00

OP-378: Got BMA180 working.

This commit is contained in:
James Cotton 2011-05-19 11:35:49 -05:00
parent 3093d26cf4
commit 5d78a68bee
5 changed files with 49 additions and 30 deletions

View File

@ -86,7 +86,7 @@ OPUAVSYNTHDIR = $(OUTDIR)/../uavobject-synthetics/flight
# use file-extension c for "c-only"-files # use file-extension c for "c-only"-files
## INS: ## INS:
SRC = ins.c SRC = test.c
SRC += pios_board.c SRC += pios_board.c
SRC += ahrs_timer.c SRC += ahrs_timer.c
SRC += insgps13state.c SRC += insgps13state.c

View File

@ -167,8 +167,8 @@ static const struct pios_spi_cfg pios_spi_accel_cfg = {
.SPI_FirstBit = SPI_FirstBit_MSB, .SPI_FirstBit = SPI_FirstBit_MSB,
.SPI_CRCPolynomial = 7, .SPI_CRCPolynomial = 7,
.SPI_CPOL = SPI_CPOL_High, .SPI_CPOL = SPI_CPOL_High,
.SPI_CPHA = SPI_CPHA_1Edge, .SPI_CPHA = SPI_CPHA_2Edge,
.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_2, .SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_128,
}, },
.use_crc = FALSE, .use_crc = FALSE,
.dma = { .dma = {

View File

@ -38,52 +38,64 @@ static uint8_t EEPROM_WRITEABLE=0;
/** /**
* @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
* @return 0 if successful, -1 if unable to claim bus
*/ */
void PIOS_BMA180_ClaimBus() int32_t PIOS_BMA180_ClaimBus()
{ {
PIOS_SPI_ClaimBus(PIOS_SPI_ACCEL); if(PIOS_SPI_ClaimBus(PIOS_SPI_ACCEL) != 0)
return -1;
PIOS_BMA_ENABLE; PIOS_BMA_ENABLE;
return 0;
} }
/** /**
* @brief Release the SPI bus for the accel communications and end the transaction * @brief Release the SPI bus for the accel communications and end the transaction
* @return 0 if successful
*/ */
void PIOS_BMA180_ReleaseBus() int32_t PIOS_BMA180_ReleaseBus()
{ {
PIOS_BMA_DISABLE; PIOS_BMA_DISABLE;
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. * @brief Set the EEPROM write-enable bit. Must be set to 1 (unlocked) before writing control registers.
* @return none * @return returns 0 if successful or < 0 if failure
* @param _we[in] bit to set, 1 to enable writes or 0 to disable writes * @param _we[in] bit to set, 1 to enable writes or 0 to disable writes
*/ */
void PIOS_BMA180_WriteEnable(uint8_t _we) int32_t PIOS_BMA180_WriteEnable(uint8_t _we)
{ {
uint8_t addr_reg[2] = {BMA_WE_ADDR,0}; uint8_t addr_reg[2] = {BMA_WE_ADDR,0};
PIOS_BMA180_ClaimBus(); if(PIOS_BMA180_ClaimBus() != 0)
return -1;
addr_reg[1] = PIOS_SPI_TransferByte(PIOS_SPI_ACCEL,(0x80 | BMA_WE_ADDR) ); addr_reg[1] = PIOS_SPI_TransferByte(PIOS_SPI_ACCEL,(0x80 | BMA_WE_ADDR) );
addr_reg[1] &= 0xEF; addr_reg[1] &= 0xEF;
addr_reg[1] |= ( (0x01 & _we) << 4); addr_reg[1] |= ( (0x01 & _we) << 4);
PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,addr_reg,NULL,sizeof(addr_reg),NULL); PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,addr_reg,NULL,sizeof(addr_reg),NULL);
PIOS_BMA180_ReleaseBus(); PIOS_BMA180_ReleaseBus();
EEPROM_WRITEABLE=_we; EEPROM_WRITEABLE=_we;
return 0;
} }
/** /**
* @brief Read a register from BMA180 * @brief Read a register from BMA180
* @returns The register value * @returns The register value or -1 if failure to get bus
* @param reg[in] Register address to be read * @param reg[in] Register address to be read
*/ */
uint8_t PIOS_BMA180_GetReg(uint8_t reg) int32_t PIOS_BMA180_GetReg(uint8_t reg)
{ {
uint8_t data; uint8_t data;
PIOS_BMA180_ClaimBus();
data = PIOS_SPI_TransferByte(PIOS_SPI_ACCEL,(0x80 | reg) ); if(PIOS_BMA180_ClaimBus() != 0)
return -1;
PIOS_SPI_TransferByte(PIOS_SPI_ACCEL,(0x80 | reg) ); // request byte
data = PIOS_SPI_TransferByte(PIOS_SPI_ACCEL,0 ); // receive response
PIOS_BMA180_ReleaseBus(); PIOS_BMA180_ReleaseBus();
return(data); return data;
} }
/** /**
@ -163,9 +175,11 @@ void PIOS_BMA180_Init()
/** /**
* @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 * @returns The number of samples remaining in the fifo or < 0 if failure
* @retval -1 unable to claim bus
* @retval -2 unable to transfer data
*/ */
uint8_t PIOS_BMA180_Read(struct pios_bma180_data * data) 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
@ -173,8 +187,10 @@ uint8_t PIOS_BMA180_Read(struct pios_bma180_data * data)
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 buf[0] = BMA_X_LSB_ADDR | 0x80 ; // Multibyte read starting at X LSB
PIOS_BMA180_ClaimBus(); if(PIOS_BMA180_ClaimBus() != 0)
PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,&buf[0],&rec[0],7,NULL); return -1;
if(PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,&buf[0],&rec[0],7,NULL) != 0)
return -2;
PIOS_BMA180_ReleaseBus(); PIOS_BMA180_ReleaseBus();
// | MSB | LSB | 0 | new_data | // | MSB | LSB | 0 | new_data |
@ -190,21 +206,23 @@ uint8_t PIOS_BMA180_Read(struct pios_bma180_data * data)
* @return 0 if success, -1 if failure. * @return 0 if success, -1 if failure.
* *
*/ */
uint8_t PIOS_BMA180_Test() int32_t PIOS_BMA180_Test()
{ {
// Read chip ID then version ID // Read chip ID then version ID
uint8_t buf[3] = {0x80 | BMA_CHIPID_ADDR, 0, 0}; uint8_t buf[3] = {0x80 | BMA_CHIPID_ADDR, 0, 0};
uint8_t rec[3] = {0,0, 0}; uint8_t rec[3] = {0,0, 0};
PIOS_BMA180_ClaimBus(); if(PIOS_BMA180_ClaimBus() != 0)
PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,&buf[0],&rec[0],sizeof(buf),NULL); return -1;
if(PIOS_SPI_TransferBlock(PIOS_SPI_ACCEL,&buf[0],&rec[0],sizeof(buf),NULL) != 0)
return -2;
PIOS_BMA180_ReleaseBus(); PIOS_BMA180_ReleaseBus();
if((rec[1] & 0x07) != 0x3) if(rec[1] != 0x3)
return -1; return -3;
if(rec[2] != 0x12) if(rec[2] < 0x12)
return -1; return -4;
return 0; return 0;
} }

View File

@ -332,6 +332,7 @@ int32_t PIOS_HMC5883_Test(void)
if(strncmp("H43\0",id,4) != 0) // match H43 if(strncmp("H43\0",id,4) != 0) // match H43
return -1; return -1;
return 0;
int32_t passed = 1; int32_t passed = 1;
uint8_t registers[3] = {0,0,0}; uint8_t registers[3] = {0,0,0};

View File

@ -71,13 +71,13 @@ struct pios_bma180_data {
}; };
/* Public Functions */ /* Public Functions */
void PIOS_BMA180_WriteEnable(uint8_t _we); int32_t PIOS_BMA180_WriteEnable(uint8_t _we);
uint8_t PIOS_BMA180_GetReg(uint8_t reg); int32_t PIOS_BMA180_GetReg(uint8_t reg);
void PIOS_BMA180_SetReg(uint8_t reg, uint8_t data); 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();
uint8_t PIOS_BMA180_Read(struct pios_bma180_data * data); int32_t PIOS_BMA180_Read(struct pios_bma180_data * data);
uint8_t PIOS_BMA180_Test(); int32_t PIOS_BMA180_Test();
#endif /* PIOS_BMA180_H */ #endif /* PIOS_BMA180_H */