Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    Implementation of BMI160 Driver

    Implementation of BMI160 Driver

    Samfirex
    New Poster

    Hello everyone,

    I am currently trying to get the BMI160 Sensor to work. It is connected trough SPI to the main processor. I would like to use the Driver provided (https://github.com/BoschSensortec/BMI160_driver), but do not really know how to continue.

    In the readme file it is written that first I have to create a bmi160_dev struct. Then I have to initialise the.read and the .write functions. I do not really understand what is asked here. 

    Is that just the function one usually uses to read or write per SPI? As an argument I get the registry adress, the data and its length and then I just need to call some HAL_SPI_TRANSMIT or HAL_SPI_RECEIVE (now for STM32)?

    And one more question: Is the length the length of the transmitted data in bytes without the adress? (So if I have 16 bits -> 8 for the adress and write/read and 8 for the data the length would be 1?)

    Thank you very much for your help!

    Sincerely 

    Sam

    2 REPLIES 2

    _Julian_
    Member

    Hi Samfirex,

     

    the read and write functions require function pointers to function of yours that are able to read/write multiple bytes from/to the SPI.

    In the file bmi160_defs.h lines 714, 715 you can see how these functions must be defined

    typedef int8_t (*bmi160_com_fptr_t)(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len);
    typedef void (*bmi160_delay_fptr_t)(uint32_t period);

     

    You mentioned yourself the arguments the function must implement.

    These functions are then called in "bmi160_get_regs" or "bmi160_get_regs", e.g.:

    rslt = dev->read(dev->id, reg_addr, data, len);

     

    • The dev_addr is a pointer to the "struct bmi160_dev yourSensor" you have to initialize before. You do not really need that (well I didn't ;-))  you could get for example the chip select pin of your SPI device, if you wrote it in the struct before.
    • The reg_addr will be something out of the "bmi160_defs.h" file, whatever you want to read, write.
    • data and len: for write it will be a pointer to the data which has to be written to the bmi, for read it will be a pointer where the data read from the bmi can be stored, i.e. a buffer (don't worry about that, it is handled by the API, specifically the functions that call bmi160_get_regs). Len is the number of Bytes that are to be read/written.

    The function for SPI read and write you have to provide can look like this (for an Arduino environment)

    int8_t bmi_spiWriteBytes(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
    {
    	
    	SPISettings imuSpiSettings(8000000, MSBFIRST, SPI_MODE0);
    	SPI1.beginTransaction(imuSpiSettings);
    	
    	//csPin = dev_addr -> chip_id
    	digitalWrite(SS_IMU_BMI, LOW); // Initiate communication
    	
    	SPI1.transfer(reg_addr); // Send Address
    	
    	for (int i = 0; i < len; i++)
    	{
    		SPI1.transfer(*(data + i));  // Transfer the data
    	}
    	
    	digitalWrite(SS_IMU_BMI, HIGH); // Close communication
    	
    	
    	SPI1.endTransaction();
    	
    	return 0;
    }

     

    int8_t bmi_spiReadBytes(uint8_t dev_addr, uint8_t reg_addr, uint8_t *data, uint16_t len)
    {
    	
    	SPISettings imuSpiSettings(8000000, MSBFIRST, SPI_MODE0);
    	SPI1.beginTransaction(imuSpiSettings);
    	
    	
    	digitalWrite(SS_IMU_BMI, LOW); // Initiate communication
    	
    	SPI1.transfer(reg_addr); // Send Address
    	
    	for (int i = 0; i < len; i++)
    	{
    		*(data + i) = SPI1.transfer(0x00);  // request, read the data
    	}
    	
    	digitalWrite(SS_IMU_BMI, HIGH); // Close communication
    	
    	
    	SPI1.endTransaction();
    	
    	return 0;
    }

     

    You have to adapt these functions to match your controllers environment.

    These functions you have to provide to the struct as pointers:

    struct bmi160_dev sensor;
    
    sensor.id = 0;
    sensor.interface = BMI160_SPI_INTF;
    sensor.chip_id = SS_IMU_BMI;
    sensor.read = &bmi_spiReadBytes;	        // Function pointer to SPI read
    sensor.write = &bmi_spiWriteBytes;	// Function pointer to SPI write
    sensor.delay_ms = &delay;		        // Function pointer to delay(ms) function
    
    rslt = bmi160_init(&sensor);

     

    Then you should be all set and you can start using the functions from the API like "bmi160_get_sensor_data(...)".

    Hope it works for you.

    Greetings Julian

    shellywang
    Occasional Contributor

    1.Yes. The two functions must be initialized in order to read and write via SPI bus. You should also follow the SPI timing described in the BMI160 datasheet.

    2.If you were talking about the example "rslt = bmi160_get_regs(reg_addr, &data, len, &sensor);", the len (length) indicates how many bytes of data you have read from the BMI160 reg map.

    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