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
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, ®_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, ®_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
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,