Hi,
I'm working with the BMI270 with STM32F072 over SPI. I'm using the Gihub driver and I have problems with the initialization of the devide.
Debugging the code I noticed that the initialization fails when reading for the config files back after writing.
The error I get is always `BMI2_E_CONFIG_LOAD`.
Following my initialization code:
// Enable spi interface
bmi270_dev.read = bm_spi_read;
bmi270_dev.write = bm_spi_write;
bmi270_dev.delay_us = delayUs;
bmi270_dev.read_write_len = SPI_BUFF_SZ;
bmi270_dev.intf = BMI2_SPI_INTERFACE;
bmi270_dev.dev_id = 0;
bmi270_dev.dummy_byte = 1;
bmi270_dev.config_file_ptr = NULL; // Load the default `bmi270_config_file` hard coded in the driver
// Initialize bmi270
rslt = bmi270_init(&bmi270_dev);
if (rslt != BMI2_OK)
return rslt;
where:
int8_t bm_spi_read(uint8_t dev_addr, uint8_t reg_addr, uint8_t* data, uint16_t len) {
UNUSED(dev_addr);
if( len > SPI_BUFF_SZ ) {
Error_Handler();
}
uint8_t rxarr[SPI_BUFF_SZ] = { reg_addr };
HAL_GPIO_WritePin(SPI_CS_N_GPIO_Port, SPI_CS_N_Pin, GPIO_PIN_RESET);
HAL_StatusTypeDef status = HAL_SPI_TransmitReceive(hspi_bmi, rxarr, rxarr,
len + 1,
COMM_DELAY);
HAL_GPIO_WritePin(SPI_CS_N_GPIO_Port, SPI_CS_N_Pin, GPIO_PIN_SET);
memcpy(data, rxarr + 1, len);
return (status == HAL_OK ? BMI2_OK : BMI2_E_COM_FAIL);
}
int8_t bm_spi_write(uint8_t dev_addr, uint8_t reg_addr, const uint8_t *data, uint16_t len) {
UNUSED(dev_addr);
if( len > SPI_BUFF_SZ ) {
Error_Handler();
}
uint8_t txarr[SPI_BUFF_SZ] = { reg_addr };
memcpy(txarr + 1, data, len);
HAL_GPIO_WritePin(SPI_CS_N_GPIO_Port, SPI_CS_N_Pin, GPIO_PIN_RESET);
HAL_StatusTypeDef status = HAL_SPI_Transmit(hspi_bmi, txarr, len + 1,
COMM_DELAY);
HAL_GPIO_WritePin(SPI_CS_N_GPIO_Port, SPI_CS_N_Pin, GPIO_PIN_SET);
return (status == HAL_OK ? BMI2_OK : BMI2_E_COM_FAIL);
}
uint32_t getUs(void) {
uint32_t usTicks = HAL_RCC_GetSysClockFreq() / 1000000;
register uint32_t ms, cycle_cnt;
do {
ms = HAL_GetTick();
cycle_cnt = SysTick->VAL;
} while (ms != HAL_GetTick());
return (ms * 1000) + (usTicks * 1000 - cycle_cnt) / usTicks;
}
void delayUs(uint32_t micros) {
uint32_t start = getUs();
while (getUs()-start < (uint32_t) micros) {
asm("nop");
}
}
Thank you
Walter