I have a custom board using NRF52832 which communicates with a BMI160 accelerometer/gyroscope chip by SPI.
I have been developing in Segger Embedded Studio (v5.10d) in debug mode, and I can configure the BMI160 and read data from the sensor without problems.
However I have come across a strange issue: when I change from debug mode to release mode in the IDE, something goes wrong with the spi_read function. In main() I have a simple check that the BMI160 is correctly initialised, which suddenly breaks in release mode:
uint8_t tmpData = 0;
uint8_t rslt = bmi160_get_regs(BMI160_CHIP_ID_ADDR, &tmpData, 1, &bmi160);
// rslt = bmi160_get_regs(BMI160_ACCEL_RANGE_ADDR, &tmpData, 1, &bmi160);
if(tmpData == BMI160_CHIP_ID) // BMI160_CHIP_ID BMI160_ACCEL_RANGE_4G BMI160_ACCEL_RANGE_2G
{
ledOn(GREEN_LED_MASK);
}
else
{
ledOn(RED_LED_MASK);
}
In debug mode, this works as expected (with line 4 commented out) - tmpData contains the correct BMI160_CHIP_ID value, and the green led goes on.
However in release mode, this doesn't work, and a second call to bmi160_get_regs() (i.e. line 4 uncommented) is required before tmpData == BMI160_CHIP_ID. It doesn't matter what register the second get_regs reads - the result in tmpData after the two calls will be as it should be after the first. (In this example, if you were to add a third call of bmi160_get_regs() to any register, tmpData will hold the correct value for BMI160_ACCEL_RANGE_ADDR, and so on.)
I'm not sure whether it's a problem with my spi_read function as it seems to be fine otherwise, but here is that code:
int8_t bmi160_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t length)
{
static uint8_t rx_data[SPI_BUFFER_LEN];
ret_code_t ret = nrf_drv_spi_transfer(&spi, ®_addr, length, rx_data, length + 1);
while (!spi_xfer_done)
{
__WFE();
}
if (ret == NRF_SUCCESS)
{
for (int i = 0; i < length; i++)
{
*(reg_data + i) = rx_data[i + 1];
}
}
return (int8_t)ret;
}
Could there be some difference in spi clock speeds between debug and release modes causing this? (I haven't had a chance to put it on an oscilloscope yet but will have access to workshop tomorrow..) Or something else I can't think of..?