Bosch Sensortec Community

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

    [BME280] bme280_init() error

    [BME280] bme280_init() error

    honoju
    Member

    Hello,

    I am using a BME280 sensor with STM32L476RG microcontroler.

    I am having trouble with the bme280_init() function.

    In this function, I pass the first null_ptr_check() so dev , dev->read , dev->write and dev->delay_us  are different from NULL.

    But just after that when I enter the bme280_get_regs() function, I don't pass the null_ptr_check() whereas my device structure has not changed.

    I investigated the matter with a debugger and I found out dev->intf_ptr has changed from BME280_SPI_INTF to BME280_I2C_INT, which I don't understand as there was no redefinition of the device structure.

    Could someone help me?

    Thanks

    13 REPLIES 13

    Yes, that is exactly the situation. I don't quite understanding what you are asking me to do. If you want me to the debug step by step and print the value rslt after bme280_get_regs(), it is -1. Is that what you asked for?

    Hello Minhwan,

    Thanks for the reply, the error value ef rslt is -1 which is according to bme280_defs.h the null pointer error and that's what I don't understand as I passed the first null pointer checkand it was okay and there was no redefinition of the device structure after

    Minhwan
    Community Moderator
    Community Moderator

    Hello Honoju, 

     

    Let'd do step by step.

    I believe that you already put your read and write function ( I2C or SPI. ) 

    But, please check that you put your function like below. 

     

    In case of SPI

    struct bme280_dev dev;
    int8_t rslt = BME280_OK;
    
    /* Sensor_0 interface over SPI with native chip select line */
    uint8_t dev_addr = 0;
    
    dev.intf_ptr = &dev_addr;
    dev.intf = BME280_SPI_INTF;
    dev.read = user_spi_read;
    dev.write = user_spi_write;
    dev.delay_ms = user_delay_ms;
    
    rslt = bme280_init(&dev);

     

    In case of I2C

    struct bme280_dev dev;
    int8_t rslt = BME280_OK;
    uint8_t dev_addr = BME280_I2C_ADDR_PRIM;
    
    dev.intf_ptr = &dev_addr;
    dev.intf = BME280_I2C_INTF;
    dev.read = user_i2c_read;
    dev.write = user_i2c_write;
    dev.delay_ms = user_delay_ms;
    
    rslt = bme280_init(&dev);

     

    i2c spi function type should be followed below url. 

    https://github.com/BoschSensortec/BME280_driver

     

    And, if you feel all settings are okay, but still there is same issue, you need to capture your data log using like logic analyzer whether MCU can really communicate with sensor. 

    You can upload your data log if you have any questions for data log. 

    Thanks,  

    Hello,

    As I am using 3 sensors, I modified the initialisation code as follows:

    #define NB_BME 3
    
    struct bme280_dev dev0, dev1, dev2; 
    struct bme280_dev dev[NB_BME];
    uint8_t dev_addr[NB_BME] ={0,1,2}; 
    uint8_t i;
    
      dev[0] = dev0;
      dev[1] = dev1;
      dev[2] = dev2;
    
      for(i=0;i<NB_BME;i++)
      {
    	  //déclaration instance capteurs
    	  dev[i].intf_ptr = &dev_addr[i];           
    	  dev[i].intf = BME280_SPI_INTF;         
    	  dev[i].read = user_spi_read;
    	  dev[i].write = user_spi_write;
    	  dev[i].delay_us = user_delay_ms;
    
              rslt = bme280_init(&dev[i]);
    }

     

    And my read/write functions are as follows (I'm using STM32 HAL driver SPI function)

    int8_t user_spi_read(uint8_t reg_addr, uint8_t *reg_data, uint32_t len, void *intf_ptr)
    {
    	int8_t rslt = 0;
    
            /*---resetting the CS pin of the corresponding sensor (starts SPI comm)---*/
    	if(intf_ptr == &dev_addr[0])
    		HAL_GPIO_WritePin(CS1_GPIO_Port, CS1_Pin, GPIO_PIN_RESET);
    	if(intf_ptr == &dev_addr[1])
    		HAL_GPIO_WritePin(CS2_GPIO_Port, CS2_Pin, GPIO_PIN_RESET);
    	if(intf_ptr == &dev_addr[2])
    		HAL_GPIO_WritePin(CS3_GPIO_Port, CS3_Pin, GPIO_PIN_RESET);
             /*--------------------------------------------------------------------------------------------------------------*/
    
    	// sending the address to read
            HAL_SPI_Transmit(&hspi1, &reg_addr, 1, HAL_MAX_DELAY);
    
           // receiving the data from the adress read
    	HAL_SPI_Receive(&hspi1, reg_data, len, HAL_MAX_DELAY);
    
            /*---setting the CS pin of the corresponding sensor (stops SPI comm)---*/
    	if(intf_ptr == &dev_addr[0])
    		HAL_GPIO_WritePin(CS1_GPIO_Port, CS1_Pin, GPIO_PIN_SET);
    	if(intf_ptr == &dev_addr[1])
    		HAL_GPIO_WritePin(CS2_GPIO_Port, CS2_Pin, GPIO_PIN_SET);
    	if(intf_ptr == &dev_addr[2])
    		HAL_GPIO_WritePin(CS3_GPIO_Port, CS3_Pin, GPIO_PIN_SET);
             /*-----------------------------------------------------------------------------------------------------------*/
    	
             return rslt;
    
    }
    
    int8_t user_spi_write(uint8_t reg_addr, const uint8_t *reg_data, uint32_t len, void *intf_ptr)
    {
    
    	int8_t rslt = 0;
    
            /*---resetting the CS pin of the corresponding sensor (starts SPI comm)---*/
    	if(intf_ptr == &dev_addr[0])
    		HAL_GPIO_WritePin(CS1_GPIO_Port, CS1_Pin, GPIO_PIN_RESET);
    	if(intf_ptr == &dev_addr[1])
    		HAL_GPIO_WritePin(CS2_GPIO_Port, CS2_Pin, GPIO_PIN_RESET);
    	if(intf_ptr == &dev_addr[2])
    		HAL_GPIO_WritePin(CS3_GPIO_Port, CS3_Pin, GPIO_PIN_RESET);
          /*---------------------------------------------------------------------------------------------------------------*/
    
    	//sending the address where to write
            HAL_SPI_Transmit(&hspi1, &reg_addr, 1, HAL_MAX_DELAY);
           //sending the data to write
            HAL_SPI_Transmit(&hspi1, (uint8_t*)reg_data, len, HAL_MAX_DELAY);
    
    	/*---setting the CS pin of the corresponding sensor (stops SPI comm)---*/
            if(intf_ptr == &dev_addr[0])
    		HAL_GPIO_WritePin(CS1_GPIO_Port, CS1_Pin, GPIO_PIN_SET);
    	if(intf_ptr == &dev_addr[1])
    		HAL_GPIO_WritePin(CS2_GPIO_Port, CS2_Pin, GPIO_PIN_SET);
    	if(intf_ptr == &dev_addr[2])
    		HAL_GPIO_WritePin(CS3_GPIO_Port, CS3_Pin, GPIO_PIN_SET);
            /*-----------------------------------------------------------------------------------------------------------*/
    	
            return rslt;
    }
    
    void user_delay_ms(uint32_t period, void *intf_ptr)
    {
      	HAL_Delay(period);
    }

     

    I wonder if the code modification due to the usage of multiple BME280 sensors is the source of problem. The application works well with one BME280 sensor.

    Thanks for your help

     

    Minhwan
    Community Moderator
    Community Moderator

    Hello Honoju, 

     

    Mutiple sensors should be fine if you handle well. And your code seems okay, but can't say just checking your code. 

    Could you capture your logic data analyzer with just one sensor? 

    I'd like to check whether you can communicate with BME280 properly or not. 

    Thanks, 

    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