Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 
    SOLVED

    BMI270 Initialization fail with STM32

    BMI270 Initialization fail with STM32

    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(&GTXBuffer[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?

    3 REPLIES 3

    BSTRobin
    Community Moderator
    Community Moderator

    Hello embedded_newbie,

    You could refer the exmaple code on STM32 attached.

    Thanks. I used your code for further troubleshooting. My struct is initialized properly as shown in the snapshot below. 

    embedded_newbie_0-1645150283860.png

    But after bmi270_init is called, it looks like this: 

    embedded_newbie_1-1645150354142.png

     

    It returns back to normal upon returning. I think it has something to do with scope

    embedded_newbie_2-1645150487567.png

     

    Thanks for the code sample. I finally got it working.

    Icon--AD-black-48x48Icon--address-consumer-data-black-48x48Icon--appointment-black-48x48Icon--back-left-black-48x48Icon--calendar-black-48x48Icon--center-alignedIcon--Checkbox-checkIcon--clock-black-48x48Icon--close-black-48x48Icon--compare-black-48x48Icon--confirmation-black-48x48Icon--dealer-details-black-48x48Icon--delete-black-48x48Icon--delivery-black-48x48Icon--down-black-48x48Icon--download-black-48x48Ic-OverlayAlertIcon--externallink-black-48x48Icon-Filledforward-right_adjustedIcon--grid-view-black-48x48IC_gd_Check-Circle170821_Icons_Community170823_Bosch_Icons170823_Bosch_Icons170821_Icons_CommunityIC-logout170821_Icons_Community170825_Bosch_Icons170821_Icons_CommunityIC-shopping-cart2170821_Icons_CommunityIC-upIC_UserIcon--imageIcon--info-i-black-48x48Icon--left-alignedIcon--Less-minimize-black-48x48Icon-FilledIcon--List-Check-grennIcon--List-Check-blackIcon--List-Cross-blackIcon--list-view-mobile-black-48x48Icon--list-view-black-48x48Icon--More-Maximize-black-48x48Icon--my-product-black-48x48Icon--newsletter-black-48x48Icon--payment-black-48x48Icon--print-black-48x48Icon--promotion-black-48x48Icon--registration-black-48x48Icon--Reset-black-48x48Icon--right-alignedshare-circle1Icon--share-black-48x48Icon--shopping-bag-black-48x48Icon-shopping-cartIcon--start-play-black-48x48Icon--store-locator-black-48x48Ic-OverlayAlertIcon--summary-black-48x48tumblrIcon-FilledvineIc-OverlayAlertwhishlist