Bosch Sensortec Community

    cancel
    Showing results for 
    Search instead for 
    Did you mean: 

    Problem Reading data via SPI - BME680 STM32.

    Problem Reading data via SPI - BME680 STM32.

    giwed
    New Poster

    Hi,

    I am implementing the BME680 on a project. I am trying it with the STM32F407 dev board using SPI1. 
    I can not read any data, could you help me? I really don't know what am i missing.
    - Drivers: https://github.com/BoschSensortec/BME680_driver

    Code Snippet: 

     void main(){
    bme680_start(&gas_sensor);
    uint16_t meas_period = 180;
    bme680_get_profile_dur(&meas_period, &gas_sensor);
    HAL_Delay(meas_period);

    while(1){

    bme680_get_sensor_data(&data, &gas_sensor);
    t680 = (uint32_t) data.temperature / 100.0;
    h680 = (uint32_t) data.humidity / 1000.0;
    p680 = (uint32_t) data.pressure / 1000.0;

    // Trigger the next measurement if you would like to read data out continuously
    if(gas_sensor.power_mode == BME680_FORCED_MODE) bme680_set_sensor_mode(&gas_sensor);
    HAL_GPIO_TogglePin(GPIOD,GPIO_PIN_14);//toggle led

    HAL_Delay(meas_period);

    }

     

    /* ----- USER FUNCTIONS ------ */

    void bme680_start(struct bme680_dev * gas_sensor){

    /* You may assign a chip select identifier to be handled later */
    gas_sensor->dev_id = 0;
    gas_sensor->intf = BME680_SPI_INTF;
    gas_sensor->read = user_spi_read;
    gas_sensor->write = user_spi_write;
    gas_sensor->delay_ms = user_delay_ms;
    gas_sensor->amb_temp = 20;
    int8_t rslt = BME680_OK;
    rslt = bme680_init(gas_sensor);


    /* Set the temperature, pressure and humidity settings */
    gas_sensor->tph_sett.os_hum = BME680_OS_2X;
    gas_sensor->tph_sett.os_pres = BME680_OS_4X;
    gas_sensor->tph_sett.os_temp = BME680_OS_8X;
    gas_sensor->tph_sett.filter = BME680_FILTER_SIZE_3;
    /* Set the remaining gas sensor settings and link the heating profile */
    gas_sensor->gas_sett.run_gas = BME680_ENABLE_GAS_MEAS;
    /* Create a ramp heat waveform in 3 steps */
    gas_sensor->gas_sett.heatr_temp = 320; /* degree Celsius */
    gas_sensor->gas_sett.heatr_dur = 150; /* milliseconds */

    /* Select the power mode */
    /* Must be set before writing the sensor configuration */
    gas_sensor->power_mode = BME680_FORCED_MODE;

    /* Set the required sensor settings needed */
    uint8_t set_required_settings;
    set_required_settings = BME680_OST_SEL | BME680_OSP_SEL | BME680_OSH_SEL | BME680_GAS_SENSOR_SEL | BME680_FILTER_SEL;


    /* Set the desired sensor configuration */
    rslt = bme680_set_sensor_settings(set_required_settings,gas_sensor);

     


    /* Set the power mode */
    rslt = bme680_set_sensor_mode(gas_sensor);
    }

     

    void user_delay_ms(uint32_t period)
    {
    HAL_Delay(period);
    }

    int8_t user_spi_read(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
    {
    int8_t rslt = 0;
    dev_id = 0;


    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET); //CS low
    HAL_SPI_Transmit(&hspi1, &reg_addr, 1, 100);
    HAL_SPI_Receive(&hspi1, reg_data, len, 100);
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_SET); //cs high

     

    return rslt;
    }

    int8_t user_spi_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len)
    {
    int8_t rslt = 0;
    dev_id = 0;
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_RESET); //cs low
    HAL_SPI_Transmit(&hspi1, &reg_addr, 1, 150);
    HAL_SPI_Transmit(&hspi1, reg_data, len, 150);
    HAL_GPIO_WritePin(GPIOA,GPIO_PIN_3,GPIO_PIN_SET); //cs high

    return rslt;
    }

     


    This image show the results:

    giwed_0-1608658066646.png

    meas_period = 183ms, t680 = 42949672, p680 = 22

     

     

     

     

     

    5 REPLIES 5

    BSTRobin
    Community Moderator
    Community Moderator

    Hello giwed,

    For your SPI read and write functio, they are no correct. You could refer the following reference code on STM32 and test it again.

    uint8_t GTXBuffer[512], GRXBuffer[512];

    /*******************************************************************************
    * Function Name : SPI_Read
    * Description : Write data into slave device.
    * Input : I2C1 or I2C2, slave_address7, subaddress, Write Number
    * Output : None
    * Return : number of bytes transmitted
    *******************************************************************************/
    int8_t SensorAPI_SPIx_Read(uint8_t slave_address7, uint8_t subaddress, uint8_t *pBuffer, uint16_t ReadNumbr)
    {
    slave_address7 = slave_address7;
    GTXBuffer[0] = subaddress | 0x80;

    HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // NSS low

    HAL_SPI_TransmitReceive(&SPI_HANDLE, GTXBuffer, GRXBuffer, ReadNumbr+1, BUS_TIMEOUT); // timeout 1000msec;
    while(SPI_HANDLE.State == HAL_SPI_STATE_BUSY); // wait for xmission complete

    HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // NSS high
    memcpy(pBuffer, GRXBuffer+1, ReadNumbr);

    return 0;
    }

    int8_t SensorAPI_SPIx_Write(uint8_t slave_address7, uint8_t subaddress, uint8_t *pBuffer, uint16_t WriteNumbr)
    {
    slave_address7 = slave_address7;
    GTXBuffer[0] = subaddress & 0x7F;
    memcpy(&GTXBuffer[1], pBuffer, WriteNumbr);

    HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_RESET); // NSS low

    //HAL_SPI_TransmitReceive(&hspi2, pTxData, pRxData, WriteNumbr+1, BUS_TIMEOUT); // send register address + write data
    HAL_SPI_Transmit(&SPI_HANDLE, GTXBuffer, WriteNumbr+1, BUS_TIMEOUT); // send register address + write data
    while(SPI_HANDLE.State == HAL_SPI_STATE_BUSY); // wait for xmission complete

    HAL_GPIO_WritePin(SPI_CS_GPIO_Port, SPI_CS_Pin, GPIO_PIN_SET); // NSS high

    return 0;
    }

    Thanks it works.
    Anyway I am receving wrong data:

    giwed_0-1609251942459.png

    The temperature doesn't make any sense (77 in decimal). Humidity = 0. Im missing something..

     

     

    BSTRobin
    Community Moderator
    Community Moderator

    Hello giwed,

    Which example code you used?

    There was reference code in github. You could refer it.

    https://github.com/BoschSensortec/BME68x-Sensor-API/blob/master/examples/forced_mode/forced_mode.c

    Spoiler
    Hi 

    Can you share me SPI code ,just to refer what changes you make with code . 
    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