02-17-2022 02:49 PM
I am using the BMI270 Sensor API from Bosch to get it to work with my STM32L4 Discovery Board. I am unable to initialize the sensor properly. It keeps failing at null pointer check in bmi270_init().
Here are my I2C read and write functions. I found them in a code sample from this board.
int8_t SensorAPI_I2Cx_Read(uint8_t subaddress, uint8_t *pBuffer, uint16_t ReadNumbr, void *intf_ptr)
{
uint8_t dev_addr = *(uint8_t*)intf_ptr;
uint16_t DevAddress = dev_addr << 1;
// send register address
HAL_I2C_Master_Transmit(&hi2c1, DevAddress, &subaddress, 1, BUS_TIMEOUT);
HAL_I2C_Master_Receive(&hi2c1, DevAddress, pBuffer, ReadNumbr, BUS_TIMEOUT);
return 0;
}
int8_t SensorAPI_I2Cx_Write(uint8_t subaddress, uint8_t *pBuffer, uint16_t WriteNumbr, void *intf_ptr)
{
uint8_t dev_addr = *(uint8_t*)intf_ptr;
uint16_t DevAddress = dev_addr << 1;
GTXBuffer[0] = subaddress;
memcpy(>XBuffer[1], pBuffer, WriteNumbr);
// send register address
HAL_I2C_Master_Transmit(&hi2c1, DevAddress, GTXBuffer, WriteNumbr+1, BUS_TIMEOUT);
return 0;
}
void delay_us(uint32_t period, void *intf_ptr)
{
uint32_t i;
while(period--)
{
for(i = 0; i < 84; i++)
{
;
}
}
}
This is how I am assigning the function pointers to the struct.
uint8_t i2c_bus = 0x68;
bmi2.intf_ptr = &i2c_bus;
bmi2.intf = BMI2_I2C_INTF;
bmi2.read = (bmi2_read_fptr_t)SensorAPI_I2Cx_Read;
bmi2.write = (bmi2_write_fptr_t)SensorAPI_I2Cx_Write;
bmi2.delay_us = (bmi2_delay_fptr_t)DWT_Delay;
From debugging, it is failing at the null pointer check. Clearly, my struct initialization is incorrect. Are there any bare metal STM32 examples I could use for reference?
Solved! Go to Solution.
02-17-2022 03:13 PM
02-18-2022 03:15 AM
Thanks. I used your code for further troubleshooting. My struct is initialized properly as shown in the snapshot below.
But after bmi270_init is called, it looks like this:
It returns back to normal upon returning. I think it has something to do with scope
02-18-2022 02:20 PM
Thanks for the code sample. I finally got it working.