Updated to v3.2.0

This commit is contained in:
Bosch Sensortec 2017-06-23 10:08:13 +02:00
parent cf40d00b0b
commit 4dbd10a74b
6 changed files with 1949 additions and 4367 deletions

204
README.md
View File

@ -1,64 +1,152 @@
# BME280 sensor API
## Introduction
This package contains the Bosch Sensortec's BME280 pressure sensor driver (sensor API)
CONTENTS OF THIS FILE
=======================
* Introduction
* Version
* Integration details
* Driver files information
* Supported sensor interface
* Copyright
The sensor driver package includes bme280.c, bme280.h and bme280_defs.h files.
INTRODUCTION
===============
- This package contains the Bosch Sensortec MEMS humidity sensor driver(sensor API)
- The sensor driver package includes bme280.h, bme280.c and bme280_support.c files
## Version
File | Version | Date
-----|---------|-----
bme280.c | 3.2.0 | 21 Mar 2017
bme280.h | 3.2.0 | 21 Mar 2017
bme280_defs.h | 3.2.0 | 21 Mar 2017
## Integration details
* Integrate bme280.h, bme280_defs.h and bme280.c file in to the project.
* Include the bme280.h file in your code like below.
``` c
#include "bme280.h"
```
## File information
* bme280_defs.h : This header file has the constants, macros and datatype declarations.
* bme280.h : This header file contains the declarations of the sensor driver APIs.
* bme280.c : This source file contains the definitions of the sensor driver APIs.
## Supported sensor interfaces
* SPI 4-wire
* I2C
SPI 3-wire is currently not supported in the API.
## Usage guide
### Initializing the sensor
To initialize the sensor, user need to create a device structure. User can do this by
creating an instance of the structure bme280_dev. After creating the device strcuture, user
need to fill in the various parameters as shown below.
#### Example for SPI 4-Wire
``` c
struct bme280_dev dev;
int8_t rslt = BME280_OK;
/* Sensor_0 interface over SPI with native chip select line */
dev.id = 0;
dev.interface = BME280_SPI_INTF;
dev.read = user_spi_read;
dev.write = user_spi_write;
dev.delay_ms = user_delay_ms;
rslt = bme280_init(&dev);
```
#### Example for I2C
``` c
struct bme280_dev dev;
int8_t rslt = BME280_OK;
dev.id = BME280_I2C_ADDR_PRIM;
dev.interface = BME280_I2C_INTF;
dev.read = user_i2c_read;
dev.write = user_i2c_write;
dev.delay_ms = user_delay_ms;
rslt = bme280_init(&dev);
```
Regarding compensation functions for temperature,pressure and humidity we have two implementations.
1) Double precision floating point version
2) Integer version
By default, integer version is used in the API. If user needs double version, user has to
enable FLOATING_POINT_REPRESENTATION macro in bme280_defs.h file.
In integer compensation functions, we also have below two implementations for pressure.
1) For 32 bit machine.
2) For 64 bit machine.
By default, 64 bit variant is used in the API. If user wants 32 bit variant, user can disable the
macro MACHINE_64_BIT in bme280_defs.h file.
### Get sensor data
#### Get sensor data in forced mode
``` c
int8_t get_sensor_data_forced_mode(struct bme280_dev *dev)
{
int8_t rslt;
uint8_t settings_sel;
struct bme280_data comp_data;
/* Continuously get the sensor data */
while (1) {
dev->settings.osr_h = BME280_OVERSAMPLING_4X;
dev->settings.osr_p = BME280_OVERSAMPLING_4X;
dev->settings.osr_t = BME280_OVERSAMPLING_4X;
settings_sel = BME280_OSR_PRESS_SEL|BME280_OSR_TEMP_SEL|BME280_OSR_HUM_SEL;
rslt = bme280_set_sensor_settings(settings_sel, dev);
rslt = bme280_set_sensor_mode(BME280_FORCED_MODE, dev);
/* Give some delay for the sensor to go into force mode */
dev->delay_ms(5);
rslt = bme280_get_sensor_data(BME280_PRESS | BME280_HUM | BME280_TEMP, &comp_data, dev);
print_sensor_data(&comp_data);
}
return rslt;
}
void print_sensor_data(struct bme280_data *comp_data)
{
#ifdef FLOATING_POINT_REPRESENTATION
printf("%0.2f\t\t%0.2f\t\t%0.2f\t\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#else
printf("%ld\t\t%ld\t\t%ld\t\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#endif
}
```
##### Get sensor data in normal mode
``` c
int8_t get_sensor_data_normal_mode(struct bme280_dev *dev)
{
int8_t rslt;
uint8_t settings_sel;
struct bme280_data comp_data;
dev->settings.osr_h = BME280_OVERSAMPLING_4X;
dev->settings.osr_p = BME280_OVERSAMPLING_4X;
dev->settings.osr_t = BME280_OVERSAMPLING_4X;
settings_sel = BME280_OSR_PRESS_SEL|BME280_OSR_TEMP_SEL|BME280_OSR_HUM_SEL;
rslt = bme280_set_sensor_settings(settings_sel, dev);
rslt = bme280_set_sensor_mode(BME280_NORMAL_MODE, dev);
/* Give some delay for the sensor to go into normal mode */
dev->delay_ms(5);
VERSION
=========
- Version of bme280 sensor driver is:
* bme280.c - V2.0.5
* bme280.h - V2.0.5
* bme280_support.c - V1.0.6
while (1) {
rslt = bme280_get_sensor_data(BME280_PRESS | BME280_HUM | BME280_TEMP, &comp_data, dev);
print_sensor_data(&comp_data);
}
INTEGRATION DETAILS
=====================
- Integrate bme280.h and bme280.c file in to your project.
- The bme280_support.c file contains only examples for API use cases, so it is not required to integrate into project.
return rslt;
}
DRIVER FILES INFORMATION
===========================
bme280.h
-----------
* This header file has the register address definition, constant definitions, data type definition and supported sensor driver calls declarations.
void print_sensor_data(struct bme280_data *comp_data)
{
#ifdef FLOATING_POINT_REPRESENTATION
printf("%0.2f\t\t%0.2f\t\t%0.2f\t\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#else
printf("%ld\t\t%ld\t\t%ld\t\n",comp_data->temperature, comp_data->pressure, comp_data->humidity);
#endif
}
```
bme280.c
------------
* This file contains the implementation for the sensor driver APIs.
bme280_support.c
----------------------
* This file shall be used as an user guidance, here you can find samples of
* Initialize the sensor with I2C/SPI communication
- Add your code to the SPI and/or I2C bus read and bus write functions.
- Return value can be chosen by yourself
- API just passes that value to your application code
- Add your code to the delay function
- Change I2C address accordingly in bme280.h
* Power mode configuration of the sensor
* Get and set functions usage
* Reading the sensor read out data
SUPPORTED SENSOR INTERFACE
====================================
- This humidity sensor driver supports SPI and I2C interfaces
COPYRIGHT
===========
- Copyright (C) 2013 - 2016 Bosch Sensortec GmbH
## Copyright (C) 2016 - 2017 Bosch Sensortec GmbH

3348
bme280.c

File diff suppressed because it is too large Load Diff

1903
bme280.h

File diff suppressed because it is too large Load Diff

372
bme280_defs.h Normal file
View File

@ -0,0 +1,372 @@
/**
* Copyright (C) 2016 - 2017 Bosch Sensortec GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of the
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
*
* @file bme280_defs.h
* @date 21 Mar 2017
* @version 3.2.0
* @brief
*
*/
/*! @file bme280_defs.h
@brief Sensor driver for BME280 sensor */
/*!
* @defgroup BME280 SENSOR API
* @brief
* @{*/
#ifndef BME280_DEFS_H_
#define BME280_DEFS_H_
/********************************************************/
/* header includes */
#ifdef __KERNEL__
#include <linux/types.h>
#else
#include <stdint.h>
#include <stdio.h>
#endif
#ifdef __KERNEL__
#if (LONG_MAX) > 0x7fffffff
#define __have_long64 1
#elif (LONG_MAX) == 0x7fffffff
#define __have_long32 1
#endif
#if !defined(UINT8_C)
#define INT8_C(x) x
#if (INT_MAX) > 0x7f
#define UINT8_C(x) x
#else
#define UINT8_C(x) x##U
#endif
#endif
#if !defined(UINT16_C)
#define INT16_C(x) x
#if (INT_MAX) > 0x7fff
#define UINT16_C(x) x
#else
#define UINT16_C(x) x##U
#endif
#endif
#if !defined(INT32_C) && !defined(UINT32_C)
#if __have_long32
#define INT32_C(x) x##L
#define UINT32_C(x) x##UL
#else
#define INT32_C(x) x
#define UINT32_C(x) x##U
#endif
#endif
#if !defined(INT64_C) && !defined(UINT64_C)
#if __have_long64
#define INT64_C(x) x##L
#define UINT64_C(x) x##UL
#else
#define INT64_C(x) x##LL
#define UINT64_C(x) x##ULL
#endif
#endif
#endif
/**@}*/
/**\name C standard macros */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#else
#define NULL ((void *) 0)
#endif
#endif
/* #define FLOATING_POINT_REPRESENTATION */
#define MACHINE_64_BIT
#ifndef TRUE
#define TRUE UINT8_C(1)
#endif
#ifndef FALSE
#define FALSE UINT8_C(0)
#endif
/**\name I2C addresses */
#define BME280_I2C_ADDR_PRIM UINT8_C(0x76)
#define BME280_I2C_ADDR_SEC UINT8_C(0x77)
/**\name BME280 chip identifier */
#define BME280_CHIP_ID UINT8_C(0x60)
/**\name Register Address */
#define BME280_CHIP_ID_ADDR UINT8_C(0xD0)
#define BME280_RESET_ADDR UINT8_C(0xE0)
#define BME280_TEMP_PRESS_CALIB_DATA_ADDR UINT8_C(0x88)
#define BME280_HUMIDITY_CALIB_DATA_ADDR UINT8_C(0xE1)
#define BME280_PWR_CTRL_ADDR UINT8_C(0xF4)
#define BME280_CTRL_HUM_ADDR UINT8_C(0xF2)
#define BME280_CTRL_MEAS_ADDR UINT8_C(0xF4)
#define BME280_CONFIG_ADDR UINT8_C(0xF5)
#define BME280_DATA_ADDR UINT8_C(0xF7)
/**\name API success code */
#define BME280_OK INT8_C(0)
/**\name API error codes */
#define BME280_E_NULL_PTR INT8_C(-1)
#define BME280_E_DEV_NOT_FOUND INT8_C(-2)
#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)
/**\name API warning codes */
#define BME280_W_INVALID_OSR_MACRO UINT8_C(1)
/**\name Macros related to size */
#define BME280_TEMP_PRESS_CALIB_DATA_LEN UINT8_C(26)
#define BME280_HUMIDITY_CALIB_DATA_LEN UINT8_C(7)
#define BME280_P_T_H_DATA_LEN UINT8_C(8)
/**\name Sensor power modes */
#define BME280_SLEEP_MODE UINT8_C(0x00)
#define BME280_FORCED_MODE UINT8_C(0x01)
#define BME280_NORMAL_MODE UINT8_C(0x03)
/**\name Macro to combine two 8 bit data's to form a 16 bit data */
#define BME280_CONCAT_BYTES(msb, lsb) (((uint16_t)msb << 8) | (uint16_t)lsb)
#define BME280_SET_BITS(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | \
((data << bitname##_POS) & bitname##_MSK))
#define BME280_SET_BITS_POS_0(reg_data, bitname, data) \
((reg_data & ~(bitname##_MSK)) | \
(data & bitname##_MSK))
#define BME280_GET_BITS(reg_data, bitname) ((reg_data & (bitname##_MSK)) >> \
(bitname##_POS))
#define BME280_GET_BITS_POS_0(reg_data, bitname) (reg_data & (bitname##_MSK))
/**\name Macros for bit masking */
#define BME280_SENSOR_MODE_MSK UINT8_C(0x03)
#define BME280_SENSOR_MODE_POS UINT8_C(0x00)
#define BME280_CTRL_HUM_MSK UINT8_C(0x07)
#define BME280_CTRL_HUM_POS UINT8_C(0x00)
#define BME280_CTRL_PRESS_MSK UINT8_C(0x1C)
#define BME280_CTRL_PRESS_POS UINT8_C(0x02)
#define BME280_CTRL_TEMP_MSK UINT8_C(0xE0)
#define BME280_CTRL_TEMP_POS UINT8_C(0x05)
#define BME280_FILTER_MSK UINT8_C(0x1C)
#define BME280_FILTER_POS UINT8_C(0x02)
#define BME280_STANDBY_MSK UINT8_C(0xE0)
#define BME280_STANDBY_POS UINT8_C(0x05)
/**\name Sensor component selection macros
These values are internal for API implementation. Don't relate this to
data sheet.*/
#define BME280_PRESS UINT8_C(1)
#define BME280_TEMP UINT8_C(1 << 1)
#define BME280_HUM UINT8_C(1 << 2)
#define BME280_ALL UINT8_C(0x07)
/**\name Settings selection macros */
#define BME280_OSR_PRESS_SEL UINT8_C(1)
#define BME280_OSR_TEMP_SEL UINT8_C(1 << 1)
#define BME280_OSR_HUM_SEL UINT8_C(1 << 2)
#define BME280_FILTER_SEL UINT8_C(1 << 3)
#define BME280_STANDBY_SEL UINT8_C(1 << 4)
#define BME280_ALL_SETTINGS_SEL UINT8_C(0x1F)
/**\name Oversampling macros */
#define BME280_NO_OVERSAMPLING UINT8_C(0x00)
#define BME280_OVERSAMPLING_1X UINT8_C(0x01)
#define BME280_OVERSAMPLING_2X UINT8_C(0x02)
#define BME280_OVERSAMPLING_4X UINT8_C(0x03)
#define BME280_OVERSAMPLING_8X UINT8_C(0x04)
#define BME280_OVERSAMPLING_16X UINT8_C(0x05)
/**\name Standby duration selection macros */
#define BME280_STANDBY_TIME_1_MS (0x00)
#define BME280_STANDBY_TIME_62_5_MS (0x01)
#define BME280_STANDBY_TIME_125_MS (0x02)
#define BME280_STANDBY_TIME_250_MS (0x03)
#define BME280_STANDBY_TIME_500_MS (0x04)
#define BME280_STANDBY_TIME_1000_MS (0x05)
#define BME280_STANDBY_TIME_10_MS (0x06)
#define BME280_STANDBY_TIME_20_MS (0x07)
/**\name Filter coefficient selection macros */
#define BME280_FILTER_COEFF_OFF (0x00)
#define BME280_FILTER_COEFF_2 (0x01)
#define BME280_FILTER_COEFF_4 (0x02)
#define BME280_FILTER_COEFF_8 (0x03)
#define BME280_FILTER_COEFF_16 (0x04)
/*!
* @brief Interface selection Enums
*/
enum bme280_intf {
/*! SPI interface */
BME280_SPI_INTF,
/*! I2C interface */
BME280_I2C_INTF
};
/*!
* @brief Type definitions
*/
typedef int8_t (*bme280_com_fptr_t)(uint8_t dev_id, uint8_t reg_addr,
uint8_t *data, uint16_t len);
typedef void (*bme280_delay_fptr_t)(uint32_t period);
/*!
* @brief Calibration data
*/
struct bme280_calib_data {
/**
* @ Trim Variables
*/
/**@{*/
uint16_t dig_T1;
int16_t dig_T2;
int16_t dig_T3;
uint16_t dig_P1;
int16_t dig_P2;
int16_t dig_P3;
int16_t dig_P4;
int16_t dig_P5;
int16_t dig_P6;
int16_t dig_P7;
int16_t dig_P8;
int16_t dig_P9;
uint8_t dig_H1;
int16_t dig_H2;
uint8_t dig_H3;
int16_t dig_H4;
int16_t dig_H5;
int8_t dig_H6;
int32_t t_fine;
/**@}*/
};
/*!
* @brief bme280 sensor structure which comprises of temperature, pressure and
* humidity data
*/
#ifdef FLOATING_POINT_REPRESENTATION
struct bme280_data {
/*! Compensated pressure */
double pressure;
/*! Compensated temperature */
double temperature;
/*! Compensated humidity */
double humidity;
};
#else
struct bme280_data {
/*! Compensated pressure */
uint32_t pressure;
/*! Compensated temperature */
int32_t temperature;
/*! Compensated humidity */
uint32_t humidity;
};
#endif /* BME280_USE_FLOATING_POINT */
/*!
* @brief bme280 sensor structure which comprises of uncompensated temperature,
* pressure and humidity data
*/
struct bme280_uncomp_data {
/*! un-compensated pressure */
uint32_t pressure;
/*! un-compensated temperature */
uint32_t temperature;
/*! un-compensated humidity */
uint32_t humidity;
};
/*!
* @brief bme280 sensor settings structure which comprises of mode,
* oversampling and filter settings.
*/
struct bme280_settings {
/*! pressure oversampling */
uint8_t osr_p;
/*! temperature oversampling */
uint8_t osr_t;
/*! humidity oversampling */
uint8_t osr_h;
/*! filter coefficient */
uint8_t filter;
/*! standby time */
uint8_t standby_time;
};
/*!
* @brief bme280 device structure
*/
struct bme280_dev {
/*! Chip Id */
uint8_t chip_id;
/*! Device Id */
uint8_t id;
/*! SPI/I2C interface */
enum bme280_intf interface;
/*! Read function pointer */
bme280_com_fptr_t read;
/*! Write function pointer */
bme280_com_fptr_t write;
/*! Delay function pointer */
bme280_delay_fptr_t delay_ms;
/*! Trim data */
struct bme280_calib_data calib_data;
/*! Sensor settings */
struct bme280_settings settings;
};
#endif /* BME280_DEFS_H_ */
/** @}*/
/** @}*/

View File

@ -1,464 +0,0 @@
/*
****************************************************************************
* Copyright (C) 2015 - 2016 Bosch Sensortec GmbH
*
* bme280_support.c
* Date: 2016/07/04
* Revision: 1.0.6 $
*
* Usage: Sensor Driver support file for BME280 sensor
*
****************************************************************************
* License:
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* Neither the name of the copyright holder nor the names of the
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL COPYRIGHT HOLDER
* OR CONTRIBUTORS BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
* OR CONSEQUENTIAL DAMAGES(INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
*
* The information provided is believed to be accurate and reliable.
* The copyright holder assumes no responsibility
* for the consequences of use
* of such information nor for any infringement of patents or
* other rights of third parties which may result from its use.
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
**************************************************************************/
/*---------------------------------------------------------------------------*/
/* Includes*/
/*---------------------------------------------------------------------------*/
#include "bme280.h"
#define BME280_API
/*Enable the macro BME280_API to use this support file */
/*----------------------------------------------------------------------------*
* The following functions are used for reading and writing of
* sensor data using I2C or SPI communication
*----------------------------------------------------------------------------*/
#ifdef BME280_API
/* \Brief: The function is used as I2C bus read
* \Return : Status of the I2C read
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be read
* \param reg_data : This data read from the sensor, which is hold in an array
* \param cnt : The no of byte of data to be read
*/
s8 BME280_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
/* \Brief: The function is used as I2C bus write
* \Return : Status of the I2C write
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be written
* \param reg_data : It is a value hold in the array,
* will be used for write the value into the register
* \param cnt : The no of byte of data to be write
*/
s8 BME280_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
/* \Brief: The function is used as SPI bus write
* \Return : Status of the SPI write
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be written
* \param reg_data : It is a value hold in the array,
* will be used for write the value into the register
* \param cnt : The no of byte of data to be write
*/
s8 BME280_SPI_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
/* \Brief: The function is used as SPI bus read
* \Return : Status of the SPI read
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be read
* \param reg_data : This data read from the sensor, which is hold in an array
* \param cnt : The no of byte of data to be read */
s8 BME280_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt);
/*
* \Brief: SPI/I2C init routine
*/
s8 I2C_routine(void);
s8 SPI_routine(void);
#endif
/********************End of I2C/SPI function declarations***********************/
/* Brief : The delay routine
* \param : delay in ms
*/
void BME280_delay_msek(u32 msek);
/* This function is an example for reading sensor data
* \param: None
* \return: communication result
*/
s32 bme280_data_readout_template(void);
/*----------------------------------------------------------------------------*
* struct bme280_t parameters can be accessed by using bme280
* bme280_t having the following parameters
* Bus write function pointer: BME280_WR_FUNC_PTR
* Bus read function pointer: BME280_RD_FUNC_PTR
* Delay function pointer: delay_msec
* I2C address: dev_addr
* Chip id of the sensor: chip_id
*---------------------------------------------------------------------------*/
struct bme280_t bme280;
/* This function is an example for reading sensor data
* \param: None
* \return: communication result
*/
s32 bme280_data_readout_template(void)
{
/* The variable used to assign the standby time*/
u8 v_stand_by_time_u8 = BME280_INIT_VALUE;
/* The variable used to read uncompensated temperature*/
s32 v_data_uncomp_temp_s32 = BME280_INIT_VALUE;
/* The variable used to read uncompensated pressure*/
s32 v_data_uncomp_pres_s32 = BME280_INIT_VALUE;
/* The variable used to read uncompensated pressure*/
s32 v_data_uncomp_hum_s32 = BME280_INIT_VALUE;
/* The variable used to read compensated temperature*/
s32 v_comp_temp_s32[2] = {BME280_INIT_VALUE, BME280_INIT_VALUE};
/* The variable used to read compensated pressure*/
u32 v_comp_press_u32[2] = {BME280_INIT_VALUE, BME280_INIT_VALUE};
/* The variable used to read compensated humidity*/
u32 v_comp_humidity_u32[2] = {BME280_INIT_VALUE, BME280_INIT_VALUE};
/* result of communication results*/
s32 com_rslt = ERROR;
/*********************** START INITIALIZATION ************************/
/* Based on the user need configure I2C or SPI interface.
* It is example code to explain how to use the bme280 API*/
#ifdef BME280_API
I2C_routine();
/*SPI_routine();*/
#endif
/*--------------------------------------------------------------------------*
* This function used to assign the value/reference of
* the following parameters
* I2C address
* Bus Write
* Bus read
* Chip id
*-------------------------------------------------------------------------*/
com_rslt = bme280_init(&bme280);
/* For initialization it is required to set the mode of
* the sensor as "NORMAL"
* data acquisition/read/write is possible in this mode
* by using the below API able to set the power mode as NORMAL*/
/* Set the power mode as NORMAL*/
com_rslt += bme280_set_power_mode(BME280_NORMAL_MODE);
/* For reading the pressure, humidity and temperature data it is required to
* set the OSS setting of humidity, pressure and temperature
* The "BME280_CTRLHUM_REG_OSRSH" register sets the humidity
* data acquisition options of the device.
* changes to this registers only become effective after a write operation to
* "BME280_CTRLMEAS_REG" register.
* In the code automated reading and writing of "BME280_CTRLHUM_REG_OSRSH"
* register first set the "BME280_CTRLHUM_REG_OSRSH" and then read and write
* the "BME280_CTRLMEAS_REG" register in the function*/
com_rslt += bme280_set_oversamp_humidity(BME280_OVERSAMP_1X);
/* set the pressure oversampling*/
com_rslt += bme280_set_oversamp_pressure(BME280_OVERSAMP_2X);
/* set the temperature oversampling*/
com_rslt += bme280_set_oversamp_temperature(BME280_OVERSAMP_4X);
/*--------------------------------------------------------------------------*/
/*------------------------------------------------------------------------*
************************* START GET and SET FUNCTIONS DATA ****************
*---------------------------------------------------------------------------*/
/* This API used to Write the standby time of the sensor input
* value have to be given
* Normal mode comprises an automated perpetual cycling between an (active)
* Measurement period and an (inactive) standby period.
* The standby time is determined by the contents of the register t_sb.
* Standby time can be set using BME280_STANDBYTIME_125_MS.
* Usage Hint : bme280_set_standbydur(BME280_STANDBYTIME_125_MS)*/
com_rslt += bme280_set_standby_durn(BME280_STANDBY_TIME_1_MS);
/* This API used to read back the written value of standby time*/
com_rslt += bme280_get_standby_durn(&v_stand_by_time_u8);
/*-----------------------------------------------------------------*
************************* END GET and SET FUNCTIONS ****************
*------------------------------------------------------------------*/
/************************* END INITIALIZATION *************************/
/*------------------------------------------------------------------*
************ START READ UNCOMPENSATED PRESSURE, TEMPERATURE
AND HUMIDITY DATA ********
*---------------------------------------------------------------------*/
/* API is used to read the uncompensated temperature*/
com_rslt += bme280_read_uncomp_temperature(&v_data_uncomp_temp_s32);
/* API is used to read the uncompensated pressure*/
com_rslt += bme280_read_uncomp_pressure(&v_data_uncomp_pres_s32);
/* API is used to read the uncompensated humidity*/
com_rslt += bme280_read_uncomp_humidity(&v_data_uncomp_hum_s32);
/* API is used to read the uncompensated temperature,pressure
and humidity data */
com_rslt += bme280_read_uncomp_pressure_temperature_humidity(
&v_data_uncomp_temp_s32, &v_data_uncomp_pres_s32, &v_data_uncomp_hum_s32);
/*--------------------------------------------------------------------*
************ END READ UNCOMPENSATED PRESSURE AND TEMPERATURE********
*-------------------------------------------------------------------------*/
/*------------------------------------------------------------------*
************ START READ COMPENSATED PRESSURE, TEMPERATURE
AND HUMIDITY DATA ********
*---------------------------------------------------------------------*/
/* API is used to compute the compensated temperature*/
v_comp_temp_s32[0] = bme280_compensate_temperature_int32(
v_data_uncomp_temp_s32);
/* API is used to compute the compensated pressure*/
v_comp_press_u32[0] = bme280_compensate_pressure_int32(
v_data_uncomp_pres_s32);
/* API is used to compute the compensated humidity*/
v_comp_humidity_u32[0] = bme280_compensate_humidity_int32(
v_data_uncomp_hum_s32);
/* API is used to read the compensated temperature, humidity and pressure*/
com_rslt += bme280_read_pressure_temperature_humidity(
&v_comp_press_u32[1], &v_comp_temp_s32[1], &v_comp_humidity_u32[1]);
/*--------------------------------------------------------------------*
************ END READ COMPENSATED PRESSURE, TEMPERATURE AND HUMIDITY ********
*-------------------------------------------------------------------------*/
/*-----------------------------------------------------------------------*
************************* START DE-INITIALIZATION ***********************
*-------------------------------------------------------------------------*/
/* For de-initialization it is required to set the mode of
* the sensor as "SLEEP"
* the device reaches the lowest power consumption only
* In SLEEP mode no measurements are performed
* All registers are accessible
* by using the below API able to set the power mode as SLEEP*/
/* Set the power mode as SLEEP*/
com_rslt += bme280_set_power_mode(BME280_SLEEP_MODE);
/*---------------------------------------------------------------------*
************************* END DE-INITIALIZATION **********************
*---------------------------------------------------------------------*/
return com_rslt;
}
#ifdef BME280_API
#define SPI_READ 0x80
#define SPI_WRITE 0x7F
#define BME280_DATA_INDEX 1
#define BME280_ADDRESS_INDEX 2
/*--------------------------------------------------------------------------*
* The following function is used to map the I2C bus read, write, delay and
* device address with global structure bme280
*-------------------------------------------------------------------------*/
s8 I2C_routine(void) {
/*--------------------------------------------------------------------------*
* By using bme280 the following structure parameter can be accessed
* Bus write function pointer: BME280_WR_FUNC_PTR
* Bus read function pointer: BME280_RD_FUNC_PTR
* Delay function pointer: delay_msec
* I2C address: dev_addr
*--------------------------------------------------------------------------*/
bme280.bus_write = BME280_I2C_bus_write;
bme280.bus_read = BME280_I2C_bus_read;
bme280.dev_addr = BME280_I2C_ADDRESS2;
bme280.delay_msec = BME280_delay_msek;
return BME280_INIT_VALUE;
}
/*---------------------------------------------------------------------------*
* The following function is used to map the SPI bus read, write and delay
* with global structure bme280
*--------------------------------------------------------------------------*/
s8 SPI_routine(void) {
/*--------------------------------------------------------------------------*
* By using bme280 the following structure parameter can be accessed
* Bus write function pointer: BME280_WR_FUNC_PTR
* Bus read function pointer: BME280_RD_FUNC_PTR
* Delay function pointer: delay_msec
*--------------------------------------------------------------------------*/
bme280.bus_write = BME280_SPI_bus_write;
bme280.bus_read = BME280_SPI_bus_read;
bme280.delay_msec = BME280_delay_msek;
return BME280_INIT_VALUE;
}
/************** I2C/SPI buffer length ******/
#define I2C_BUFFER_LEN 8
#define SPI_BUFFER_LEN 5
/*-------------------------------------------------------------------*
* This is a sample code for read and write the data by using I2C/SPI
* Use either I2C or SPI based on your need
* The device address defined in the bme280.h file
*-----------------------------------------------------------------------*/
/* \Brief: The function is used as I2C bus write
* \Return : Status of the I2C write
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be written
* \param reg_data : It is a value hold in the array,
* will be used for write the value into the register
* \param cnt : The no of byte of data to be write
*/
s8 BME280_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BME280_INIT_VALUE;
u8 array[I2C_BUFFER_LEN];
u8 stringpos = BME280_INIT_VALUE;
array[BME280_INIT_VALUE] = reg_addr;
for (stringpos = BME280_INIT_VALUE; stringpos < cnt; stringpos++) {
array[stringpos + BME280_DATA_INDEX] = *(reg_data + stringpos);
}
/*
* Please take the below function as your reference for
* write the data using I2C communication
* "IERROR = I2C_WRITE_STRING(DEV_ADDR, array, cnt+1)"
* add your I2C write function here
* iError is an return value of I2C read function
* Please select your valid return value
* In the driver SUCCESS defined as 0
* and FAILURE defined as -1
* Note :
* This is a full duplex operation,
* The first read data is discarded, for that extra write operation
* have to be initiated. For that cnt+1 operation done in the I2C write string function
* For more information please refer data sheet SPI communication:
*/
return (s8)iError;
}
/* \Brief: The function is used as I2C bus read
* \Return : Status of the I2C read
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be read
* \param reg_data : This data read from the sensor, which is hold in an array
* \param cnt : The no of data byte of to be read
*/
s8 BME280_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BME280_INIT_VALUE;
u8 array[I2C_BUFFER_LEN] = {BME280_INIT_VALUE};
u8 stringpos = BME280_INIT_VALUE;
array[BME280_INIT_VALUE] = reg_addr;
/* Please take the below function as your reference
* for read the data using I2C communication
* add your I2C rad function here.
* "IERROR = I2C_WRITE_READ_STRING(DEV_ADDR, ARRAY, ARRAY, 1, CNT)"
* iError is an return value of SPI write function
* Please select your valid return value
* In the driver SUCCESS defined as 0
* and FAILURE defined as -1
*/
for (stringpos = BME280_INIT_VALUE; stringpos < cnt; stringpos++) {
*(reg_data + stringpos) = array[stringpos];
}
return (s8)iError;
}
/* \Brief: The function is used as SPI bus read
* \Return : Status of the SPI read
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, will data is going to be read
* \param reg_data : This data read from the sensor, which is hold in an array
* \param cnt : The no of byte of data to be read
*/
s8 BME280_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError=BME280_INIT_VALUE;
u8 array[SPI_BUFFER_LEN]={0,};
u8 stringpos;
/* For the SPI mode only 7 bits of register addresses are used.
The MSB of register address is declared the bit what functionality it is
read/write (read as 1/write as BME280_INIT_VALUE)*/
array[BME280_INIT_VALUE] = reg_addr|SPI_READ;/*read routine is initiated register address is mask with 0x80*/
/*
* Please take the below function as your reference for
* read the data using SPI communication
* " IERROR = SPI_READ_WRITE_STRING(ARRAY, ARRAY, CNT+1)"
* add your SPI read function here
* iError is an return value of SPI read function
* Please select your valid return value
* In the driver SUCCESS defined as 0
* and FAILURE defined as -1
* Note :
* This is a full duplex operation,
* The first read data is discarded, for that extra write operation
* have to be initiated. For that cnt+1 operation done in the SPI read
* and write string function
* For more information please refer data sheet SPI communication:
*/
for (stringpos = BME280_INIT_VALUE; stringpos < cnt; stringpos++) {
*(reg_data + stringpos) = array[stringpos+BME280_DATA_INDEX];
}
return (s8)iError;
}
/* \Brief: The function is used as SPI bus write
* \Return : Status of the SPI write
* \param dev_addr : The device address of the sensor
* \param reg_addr : Address of the first register, where data is to be written
* \param reg_data : It is a value hold in the array,
* will be used for write the value into the register
* \param cnt : The no of byte of data to be write
*/
s8 BME280_SPI_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt)
{
s32 iError = BME280_INIT_VALUE;
u8 array[SPI_BUFFER_LEN * BME280_ADDRESS_INDEX];
u8 stringpos = BME280_INIT_VALUE;
u8 index = BME280_INIT_VALUE;
for (stringpos = BME280_INIT_VALUE; stringpos < cnt; stringpos++) {
/* the operation of (reg_addr++)&0x7F done as per the
SPI communication protocol specified in the data sheet*/
index = stringpos * BME280_ADDRESS_INDEX;
array[index] = (reg_addr++) & SPI_WRITE;
array[index + BME280_DATA_INDEX] = *(reg_data + stringpos);
}
/* Please take the below function as your reference
* for write the data using SPI communication
* add your SPI write function here.
* "IERROR = SPI_WRITE_STRING(ARRAY, CNT*2)"
* iError is an return value of SPI write function
* Please select your valid return value
* In the driver SUCCESS defined as 0
* and FAILURE defined as -1
*/
return (s8)iError;
}
/* Brief : The delay routine
* \param : delay in ms
*/
void BME280_delay_msek(u32 msek)
{
/*Here you can write your own delay routine*/
}
#endif

25
changelog.md Normal file
View File

@ -0,0 +1,25 @@
# Change Log
All notable changes to BME280 Sensor API will be documented in this file.
## v3.2.0, 21 Mar 2017
### Changed
- API for putting sensor into sleep mode changed.
- Pressure, Temperature out of range data clipped.
- 64 bit pressure compensation changed.
## v3.1.0, 8 Mar 2017
### Added
- Device settings APIs.
- Compensations functions, double datatype for Pressure, Temperature and Humidity.
- Compensations functions, integer datatype 64bit for Pressure.
### Changed
- Internal functions related to power mode.
## v3.0.0, 17 Feb 2017
### Added
Below functionalities are supported
- Init.
- Soft reset.
- Power mode.
- Compensated functions, integer datatype for Pressure, Temperature and Humidity.
- Get sensor data.