Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    BMI323 SPI For stm32

    BMI323 SPI For stm32

    Noro
    New Poster

    Hello forum can you tell me how to intagrate API for stm32 spi please Thank You

    9 REPLIES 9

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Noro,

    You could refer attached example code runs on STM32F4.

    this is my function        

    void spiWriteRegister16(uint8_t reg, uint16_t value, GPIO_TypeDef *port, uint16_t cs) {
    uint8_t data[3];
    data[0] = reg;
    data[1] = (uint8_t)(value >> 8); // transmit MSB first
    data[2] = (uint8_t)(value & 0xFF); // transmit LSB second
    HAL_GPIO_WritePin(port, cs, GPIO_PIN_RESET); // make chip select low to enable transmission
    HAL_SPI_Transmit(&hspi1, data, 3, 10); // write data to register
    HAL_GPIO_WritePin(port, cs, GPIO_PIN_SET); // make chip select high at the end of transmission
    }

    uint16_t spiReadRegister16(uint8_t reg, GPIO_TypeDef *port, uint16_t cs) {
    uint8_t address = reg | 0x80; // read operation with auto-increment
    uint8_t data[4] = {0};
    HAL_GPIO_WritePin(port, cs, GPIO_PIN_RESET);
    HAL_SPI_Transmit(&hspi1, &address, 1, 1); // send address
    HAL_SPI_Receive(&hspi1, data, 4, 1); // read the data from device
    HAL_GPIO_WritePin(port, cs, GPIO_PIN_SET);
    return ((uint16_t)data[2] << 😎 | data[3];
    }
    void readAllAccel() {
    uint8_t address = 0x03;
    uint8_t txBuf[1] = {address | 0x80}; // read operation, MSB is 1
    uint8_t rxBuf[20] = {0}; // initialize to 0
    HAL_GPIO_WritePin(GPIOA, SPI_CS_Pin, GPIO_PIN_RESET); // make chip select low to enable transmission
    HAL_SPI_TransmitReceive(&hspi1, txBuf, rxBuf, 1, 10); // send address and receive data
    HAL_SPI_TransmitReceive(&hspi1, &txBuf[1], &rxBuf[1], 19, 10); // receive the rest of the data
    HAL_GPIO_WritePin(GPIOA, SPI_CS_Pin, GPIO_PIN_SET); // make chip select high at the end of transmission

    //Offset = 2 because the 2 first bytes are dummy (useless)
    int offset = 2;
    x = ((rxBuf[offset + 1] << 8 | (uint16_t)rxBuf[offset + 0])); //0x03
    y = ((rxBuf[offset + 3] << 8 | (uint16_t)rxBuf[offset + 2])); //0x04
    z = ((rxBuf[offset + 5] << 8 | (uint16_t)rxBuf[offset + 4])); //0x05
    gyr_x = (rxBuf[offset + 7] << 8 | (uint16_t)rxBuf[offset + 6]); //0x06
    gyr_y = (rxBuf[offset + 9] << 8 | (uint16_t)rxBuf[offset + 8]); //0x07
    gyr_z = (rxBuf[offset + 11] << 8 | (uint16_t)rxBuf[offset + 10]); //0x08
    temperature = (rxBuf[offset + 13] << 8 | (uint16_t)rxBuf[offset + 12]); //0x09
    }
    void softReset(){
    spiWriteRegister16(CMD, 0xDEAF,GPIOA,SPI_CS_Pin);
    HAL_Delay(50);
    }

     

    #define ACC_CONF 0x20 //Page 91
    #define GYR_CONF 0x21 //Page 93
    #define CMD 0x7E //Page 65
    #define GRAVITY_EARTH (9.80665f)
    uint16_t temperature = 0;
    int16_t x, y, z;
    int16_t gyr_x, gyr_y, gyr_z;

    init  device 

    softReset();
    spiWriteRegister16(ACC_CONF,0x4027,GPIOA,SPI_CS_Pin);
    spiWriteRegister16(GYR_CONF,0x760B,GPIOA,SPI_CS_Pin);

    please see if this is  correct Thanks

    BSTRobin
    Community Moderator
    Community Moderator

    Hi Noro,

    You SPI write, read function only supports data access that is 2 bytes long.
    BMI323 sensor API https://github.com/boschsensortec/BMI323-Sensor-API has implemented the function how to access sensor.
    You only need the appropriate read, write, delay_us interface, then you can access the sensor normally.

    Previous attached example code has demostrated how to use SPI to access BMI323 on STM32 for your reference.

    I connected as in your code sent, I don’t see anything after the ID

    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