The following is a relevant excerpt of the initialization code. The ellipses indicate where stuff is omitted. #define FORM_READ_REG( reg ) ( ( (reg) & 0x7F ) | 0x80 )
#define FORM_WRITE_REG( reg ) ( ( (reg) & 0x7F ) | 0x00 )
...
enum bmx160_reg
{
BMX160_REG_CHIPID = 0x00,
...
BMX160_REG_CMD = 0x7E,
};
enum bmx160_cmd {
...
BMX160_CMD_SOFTRESET = 0xB6,
};
...
static void BMX160_CMD( BMX160_OBJ* thisObj, enum bmx160_cmd cmd)
{
uint8_t reg;
thisObj->spiIf->writeReg( thisObj->spiIf, &thisObj->spiSlave, FORM_WRITE_REG( BMX160_REG_CMD ), cmd );
}
...
{
static uint8_t reg = 0x00;
int try, i;
...
// Initialize sensor's SPI interface
thisObj->spiIf->readReg( thisObj->spiIf, &thisObj->spiSlave, FORM_READ_REG( 0x7F ), ® );
// Read CHIP ID to make sure were talking to the expected BMX160 sensor
// Usually works here in the first attempt
reg = 0;
try = 3;
do {
thisObj->spiIf->readReg( thisObj->spiIf, &thisObj->spiSlave,
FORM_READ_REG( BMX160_REG_CHIPID ), ® );
} while (--try > 0 && reg != 0xd8 );
if ( reg != 0xd8 ) {
// Bad CHIP ID
return -1;
}
// Reset the chip
BMX160_CMD( thisObj, BMX160_CMD_SOFTRESET );
// Wait for 2 ms (tested to be waiting sufficiently long using oscilloscope).
delay_ms(2);
// Initialize sensor's SPI interface again
thisObj->spiIf->readReg( thisObj->spiIf, &thisObj->spiSlave,
FORM_READ_REG( 0x7F ), ® );
// Test communication by reading CHIPID again.
// Reads 0x00 or 0xFF and never succeeds within the 3 allotted attempts.
reg = 0;
try = 3;
do {
thisObj->spiIf->readReg( thisObj->spiIf, &thisObj->spiSlave,
FORM_READ_REG( BMX160_REG_CHIPID ), ® );
} while (--try > 0 && reg != 0xd8);
if ( reg != 0xd8 ) {
// Bad CHIP ID
return -1;
}
...
}
...
... View more