Added a wait until the NVM copy was complete.

This commit is contained in:
Bosch Sensortec 2019-09-13 13:10:43 +02:00 committed by Kevin Goveas
parent 47596cdf7f
commit bdf4e573cb
7 changed files with 2800 additions and 2772 deletions

View File

@ -7,9 +7,9 @@ The sensor driver package includes bme280.c, bme280.h and bme280_defs.h files.
## Version
File | Version | Date
--------------|---------|------------
bme280.c | 3.3.6 | 08 Mar 2019
bme280.h | 3.3.6 | 08 Mar 2019
bme280_defs.h | 3.3.6 | 08 Mar 2019
bme280.c | 3.3.7 | 26 Aug 2019
bme280.h | 3.3.7 | 26 Aug 2019
bme280_defs.h | 3.3.7 | 26 Aug 2019
## Integration details
* Integrate bme280.h, bme280_defs.h and bme280.c file in to the project.
@ -280,19 +280,16 @@ int8_t user_i2c_write(uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint1
/*
* Data on the bus should be like
* |------------+----------------------|
* | I2C action | Data |
* |------------+----------------------|
* | Start | - |
* | Write | (reg_addr) |
* | Write | (reg_data[0]) |
* | Write | (reg_addr + 1) |
* | Write | (reg_data[1]) |
* | Write | (....) |
* | Write | (reg_addr + len - 1) |
* | Write | (reg_data[len - 1]) |
* | Stop | - |
* |------------+----------------------|
* |------------+---------------------|
* | I2C action | Data |
* |------------+---------------------|
* | Start | - |
* | Write | (reg_addr) |
* | Write | (reg_data[0]) |
* | Write | (....) |
* | Write | (reg_data[len - 1]) |
* | Stop | - |
* |------------+---------------------|
*/
return rslt;

View File

@ -40,8 +40,8 @@
* patent rights of the copyright holder.
*
* File bme280.c
* Date 08 Mar 2019
* Version 3.3.6
* Date 26 Aug 2019
* Version 3.3.7
*
*/
@ -632,9 +632,11 @@ int8_t bme280_soft_reset(const struct bme280_dev *dev)
{
int8_t rslt;
uint8_t reg_addr = BME280_RESET_ADDR;
uint8_t status_reg = 0;
uint8_t try_run = 5;
/* 0xB6 is the soft reset command */
uint8_t soft_rst_cmd = 0xB6;
uint8_t soft_rst_cmd = BME280_SOFT_RESET_COMMAND;
/* Check for null pointer in the device structure*/
rslt = null_ptr_check(dev);
@ -645,8 +647,22 @@ int8_t bme280_soft_reset(const struct bme280_dev *dev)
/* Write the soft reset command in the sensor */
rslt = bme280_set_regs(&reg_addr, &soft_rst_cmd, 1, dev);
/* As per data sheet, startup time is 2 ms. */
dev->delay_ms(2);
if (rslt == BME280_OK)
{
/* If NVM not copied yet, Wait for NVM to copy */
do
{
/* As per data sheet - Table 1, startup time is 2 ms. */
dev->delay_ms(2);
rslt = bme280_get_regs(BME280_STATUS_REG_ADDR, &status_reg, 1, dev);
} while ((rslt == BME280_OK) && (try_run--) && (status_reg & BME280_STATUS_IM_UPDATE));
if (status_reg & BME280_STATUS_IM_UPDATE)
{
rslt = BME280_E_NVM_COPY_FAILED;
}
}
}
return rslt;

View File

@ -40,8 +40,8 @@
* patent rights of the copyright holder.
*
* @file bme280.h
* @date 08 Mar 2019
* @version 3.3.6
* @date 26 Aug 2019
* @version 3.3.7
* @brief
*
*/

View File

@ -40,8 +40,8 @@
* patent rights of the copyright holder.
*
* @file bme280_defs.h
* @date 08 Mar 2019
* @version 3.3.6
* @date 26 Aug 2019
* @version 3.3.7
* @brief
*
*/
@ -148,6 +148,7 @@
#define BME280_E_INVALID_LEN INT8_C(-3)
#define BME280_E_COMM_FAIL INT8_C(-4)
#define BME280_E_SLEEP_MODE_FAIL INT8_C(-5)
#define BME280_E_NVM_COPY_FAILED INT8_C(-6)
/**\name API warning codes */
#define BME280_W_INVALID_OSR_MACRO INT8_C(1)
@ -221,7 +222,7 @@
#define BME280_OVERSAMPLING_16X UINT8_C(0x05)
/**\name Standby duration selection macros */
#define BME280_STANDBY_TIME_0_5_MS (0x00)
#define BME280_STANDBY_TIME_0_5_MS (0x00)
#define BME280_STANDBY_TIME_62_5_MS (0x01)
#define BME280_STANDBY_TIME_125_MS (0x02)
#define BME280_STANDBY_TIME_250_MS (0x03)
@ -237,6 +238,10 @@
#define BME280_FILTER_COEFF_8 (0x03)
#define BME280_FILTER_COEFF_16 (0x04)
#define BME280_STATUS_REG_ADDR (0xF3)
#define BME280_SOFT_RESET_COMMAND (0xB6)
#define BME280_STATUS_IM_UPDATE (0x01)
/*!
* @brief Interface selection Enums
*/

View File

@ -1,161 +1,171 @@
/*
Linux userspace test code, simple and mose code directy from the doco.
compile like this: gcc linux_userspace.c ../bme280.c -I ../ -o bme280
tested: Raspberry Pi.
Use like: ./bme280 /dev/i2c-0
*/
* Linux userspace test code, simple and mose code directy from the doco.
* compile like this: gcc linux_userspace.c ../bme280.c -I ../ -o bme280
* tested: Raspberry Pi.
* Use like: ./bme280 /dev/i2c-0
*/
#include "bme280.h"
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#ifdef __KERNEL__
#include <linux/i2c-dev.h>
#include <sys/ioctl.h>
#endif
#include <sys/types.h>
#include <fcntl.h>
#include "bme280.h"
int fd;
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
void user_delay_ms(uint32_t period);
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len);
void print_sensor_data(struct bme280_data *comp_data);
int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev);
int8_t user_i2c_read(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
if (write(fd, &reg_addr, sizeof(reg_addr)) < sizeof(reg_addr))
return BME280_E_COMM_FAIL;
if (read(fd, data, len) < len)
return BME280_E_COMM_FAIL;
return BME280_OK;
write(fd, &reg_addr, 1);
read(fd, data, len);
return 0;
}
void user_delay_ms(uint32_t period)
{
usleep(period * 1000);
usleep(period * 1000);
}
int8_t user_i2c_write(uint8_t id, uint8_t reg_addr, uint8_t *data, uint16_t len)
{
int8_t *buf;
buf = malloc(len + 1);
buf[0] = reg_addr;
memcpy(buf + 1, data, len);
if (write(fd, buf, len + 1) < len)
return BME280_E_COMM_FAIL;
free(buf);
int8_t *buf;
buf = malloc(len + 1);
buf[0] = reg_addr;
memcpy(buf + 1, data, len);
if (write(fd, buf, len + 1) < len)
return BME280_E_COMM_FAIL;
free(buf);
return BME280_OK;
}
void print_sensor_data(struct bme280_data *comp_data)
{
float temp, press, hum;
float temp, press, hum;
#ifdef BME280_FLOAT_ENABLE
temp = comp_data->temperature;
press = 0.01 * comp_data->pressure;
hum = comp_data->humidity;
temp = comp_data->temperature;
press = 0.01 * comp_data->pressure;
hum = comp_data->humidity;
#else
#ifdef BME280_64BIT_ENABLE
temp = 0.01f * comp_data->temperature;
press = 0.0001f * comp_data->pressure;
hum = 1.0f / 1024.0f * comp_data->humidity;
temp = 0.01f * comp_data->temperature;
press = 0.0001f * comp_data->pressure;
hum = 1.0f / 1024.0f * comp_data->humidity;
#else
temp = 0.01f * comp_data->temperature;
press = 0.01f * comp_data->pressure;
hum = 1.0f / 1024.0f * comp_data->humidity;
temp = 0.01f * comp_data->temperature;
press = 0.01f * comp_data->pressure;
hum = 1.0f / 1024.0f * comp_data->humidity;
#endif
#endif
printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
printf("%0.2lf deg C, %0.2lf hPa, %0.2lf%%\n", temp, press, hum);
}
int8_t stream_sensor_data_forced_mode(struct bme280_dev *dev)
{
int8_t rslt;
uint8_t settings_sel;
struct bme280_data comp_data;
int8_t rslt;
uint8_t settings_sel;
struct bme280_data comp_data;
/* Recommended mode of operation: Indoor navigation */
dev->settings.osr_h = BME280_OVERSAMPLING_1X;
dev->settings.osr_p = BME280_OVERSAMPLING_16X;
dev->settings.osr_t = BME280_OVERSAMPLING_2X;
dev->settings.filter = BME280_FILTER_COEFF_16;
/* Recommended mode of operation: Indoor navigation */
dev->settings.osr_h = BME280_OVERSAMPLING_1X;
dev->settings.osr_p = BME280_OVERSAMPLING_16X;
dev->settings.osr_t = BME280_OVERSAMPLING_2X;
dev->settings.filter = BME280_FILTER_COEFF_16;
settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
settings_sel = BME280_OSR_PRESS_SEL | BME280_OSR_TEMP_SEL | BME280_OSR_HUM_SEL | BME280_FILTER_SEL;
rslt = bme280_set_sensor_settings(settings_sel, dev);
rslt = bme280_set_sensor_settings(settings_sel, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to set sensor settings (code %+d).", rslt);
return rslt;
}
printf("Temperature, Pressure, Humidity\n");
/* Continuously stream sensor data */
while (1)
{
rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
break;
}
/* Wait for the measurement to complete and print data @25Hz */
dev->delay_ms(40);
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
break;
}
print_sensor_data(&comp_data);
}
return rslt;
printf("Temperature, Pressure, Humidity\n");
/* Continuously stream sensor data */
while (1)
{
rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to set sensor mode (code %+d).", rslt);
break;
}
/* Wait for the measurement to complete and print data @25Hz */
dev->delay_ms(40);
rslt = bme280_get_sensor_data(BME280_ALL, &comp_data, dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to get sensor data (code %+d).", rslt);
break;
}
print_sensor_data(&comp_data);
}
return rslt;
}
int main(int argc, char* argv[])
{
struct bme280_dev dev;
int8_t rslt = BME280_OK;
struct bme280_dev dev;
int8_t rslt = BME280_OK;
if (argc < 2)
{
fprintf(stderr, "Missing argument for i2c bus.\n");
exit(1);
}
if (argc < 2)
{
fprintf(stderr, "Missing argument for i2c bus.\n");
exit(1);
}
// make sure to select BME280_I2C_ADDR_PRIM
// or BME280_I2C_ADDR_SEC as needed
dev.dev_id =
// make sure to select BME280_I2C_ADDR_PRIM
// or BME280_I2C_ADDR_SEC as needed
dev.dev_id =
#if 1
BME280_I2C_ADDR_PRIM
BME280_I2C_ADDR_PRIM
#else
BME280_I2C_ADDR_SEC
BME280_I2C_ADDR_SEC
#endif
;
;
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_ms = user_delay_ms;
dev.intf = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_ms = user_delay_ms;
if ((fd = open(argv[1], O_RDWR)) < 0)
{
fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
exit(1);
}
if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)
{
fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
if ((fd = open(argv[1], O_RDWR)) < 0)
{
fprintf(stderr, "Failed to open the i2c bus %s\n", argv[1]);
exit(1);
}
#ifdef __KERNEL__
if (ioctl(fd, I2C_SLAVE, dev.dev_id) < 0)
{
fprintf(stderr, "Failed to acquire bus access and/or talk to slave.\n");
exit(1);
}
#endif
rslt = bme280_init(&dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
exit(1);
}
rslt = stream_sensor_data_forced_mode(&dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
exit(1);
}
return 0;
rslt = bme280_init(&dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to initialize the device (code %+d).\n", rslt);
exit(1);
}
rslt = stream_sensor_data_forced_mode(&dev);
if (rslt != BME280_OK)
{
fprintf(stderr, "Failed to stream sensor data (code %+d).\n", rslt);
exit(1);
}
return 0;
}

View File

@ -40,8 +40,8 @@
* patent rights of the copyright holder.
*
* File bme280_selftest.c
* Date 08 Mar 2019
* Version 3.3.6
* Date 26 Aug 2019
* Version 3.3.7
*
*/

View File

@ -40,8 +40,8 @@
* patent rights of the copyright holder.
*
* File bme280_selftest.h
* Date 08 Mar 2019
* Version 3.3.6
* Date 26 Aug 2019
* Version 3.3.7
*
*/