Fixed typo in standby duration macro

This commit is contained in:
Bosch Sensortec 2019-03-08 13:21:49 +01:00 committed by Kevin Goveas
parent a50e719ded
commit a78b67c249
6 changed files with 1054 additions and 847 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.4 | 14 Feb 2018
bme280.h | 3.3.4 | 14 Feb 2018
bme280_defs.h | 3.3.4 | 14 Feb 2018
bme280.c | 3.3.5 | 08 Nov 2018
bme280.h | 3.3.5 | 08 Nov 2018
bme280_defs.h | 3.3.5 | 08 Nov 2018
## Integration details
* Integrate bme280.h, bme280_defs.h and bme280.c file in to the project.

1346
bme280.c

File diff suppressed because it is too large Load Diff

View File

@ -1,5 +1,5 @@
/**
* Copyright (C) 2016 - 2017 Bosch Sensortec GmbH
* Copyright (C) 2018 - 2019 Bosch Sensortec GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -39,17 +39,20 @@
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
*
* @file bme280.h
* @date 14 Feb 2018
* @version 3.3.4
* @file bme280.h
* @date 08 Mar 2019
* @version 3.3.6
* @brief
*
*/
/*! @file bme280.h
@brief Sensor driver for BME280 sensor */
* @brief Sensor driver for BME280 sensor
*/
/*!
* @defgroup BME280 SENSOR API
* @{*/
*/
#ifndef BME280_H_
#define BME280_H_
@ -112,7 +115,7 @@ int8_t bme280_get_regs(uint8_t reg_addr, uint8_t *reg_data, uint16_t len, const
* desired settings. User can do OR operation of these macros for configuring
* multiple settings.
*
* Macros | Functionality
* Macros | Functionality
* -----------------------|----------------------------------------------
* BME280_OSR_PRESS_SEL | To set pressure oversampling.
* BME280_OSR_TEMP_SEL | To set temperature oversampling.
@ -151,8 +154,7 @@ int8_t bme280_get_sensor_settings(struct bme280_dev *dev);
* @return Result of API execution status
* @retval zero -> Success / +ve value -> Warning / -ve value -> Error
*/
int8_t bme280_set_sensor_mode(uint8_t sensor_mode,
const struct bme280_dev *dev);
int8_t bme280_set_sensor_mode(uint8_t sensor_mode, const struct bme280_dev *dev);
/*!
* @brief This API gets the power mode of the sensor.
@ -230,8 +232,10 @@ void bme280_parse_sensor_data(const uint8_t *reg_data, struct bme280_uncomp_data
* @return Result of API execution status.
* @retval zero -> Success / -ve value -> Error
*/
int8_t bme280_compensate_data(uint8_t sensor_comp, const struct bme280_uncomp_data *uncomp_data,
struct bme280_data *comp_data, struct bme280_calib_data *calib_data);
int8_t bme280_compensate_data(uint8_t sensor_comp,
const struct bme280_uncomp_data *uncomp_data,
struct bme280_data *comp_data,
struct bme280_calib_data *calib_data);
#ifdef __cplusplus
}

View File

@ -1,5 +1,5 @@
/**
* Copyright (C) 2016 - 2017 Bosch Sensortec GmbH
* Copyright (C) 2018 - 2019 Bosch Sensortec GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -39,19 +39,21 @@
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
*
* @file bme280_defs.h
* @date 14 Feb 2018
* @version 3.3.4
* @file bme280_defs.h
* @date 08 Mar 2019
* @version 3.3.6
* @brief
*
*/
/*! @file bme280_defs.h
@brief Sensor driver for BME280 sensor */
* @brief Sensor driver for BME280 sensor
*/
/*!
* @defgroup BME280 SENSOR API
* @brief
* @{*/
*/
#ifndef BME280_DEFS_H_
#define BME280_DEFS_H_
@ -66,42 +68,43 @@
#endif
/********************************************************/
/*! @name Common macros */
/*! @name Common macros */
/********************************************************/
#if !defined(UINT8_C) && !defined(INT8_C)
#define INT8_C(x) S8_C(x)
#define UINT8_C(x) U8_C(x)
#define INT8_C(x) S8_C(x)
#define UINT8_C(x) U8_C(x)
#endif
#if !defined(UINT16_C) && !defined(INT16_C)
#define INT16_C(x) S16_C(x)
#define UINT16_C(x) U16_C(x)
#define INT16_C(x) S16_C(x)
#define UINT16_C(x) U16_C(x)
#endif
#if !defined(INT32_C) && !defined(UINT32_C)
#define INT32_C(x) S32_C(x)
#define UINT32_C(x) U32_C(x)
#define INT32_C(x) S32_C(x)
#define UINT32_C(x) U32_C(x)
#endif
#if !defined(INT64_C) && !defined(UINT64_C)
#define INT64_C(x) S64_C(x)
#define UINT64_C(x) U64_C(x)
#define INT64_C(x) S64_C(x)
#define UINT64_C(x) U64_C(x)
#endif
/**@}*/
/**\name C standard macros */
#ifndef NULL
#ifdef __cplusplus
#define NULL 0
#define NULL 0
#else
#define NULL ((void *) 0)
#define NULL ((void *) 0)
#endif
#endif
/********************************************************/
#ifndef BME280_FLOAT_ENABLE
/* #define BME280_FLOAT_ENABLE */
#endif
@ -112,173 +115,176 @@
#endif
#ifndef TRUE
#define TRUE UINT8_C(1)
#define TRUE UINT8_C(1)
#endif
#ifndef FALSE
#define FALSE UINT8_C(0)
#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)
#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)
#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)
#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)
#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)
#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 INT8_C(1)
#define BME280_W_INVALID_OSR_MACRO INT8_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)
#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)
#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_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))
((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))
((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))
#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_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_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_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_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_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)
#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)
* 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)
#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)
#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)
#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)
#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)
#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
/*! 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 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;
/**@}*/
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;
/**@}*/
};
/*!
@ -286,22 +292,28 @@ struct bme280_calib_data {
* humidity data
*/
#ifdef BME280_FLOAT_ENABLE
struct bme280_data {
/*! Compensated pressure */
double pressure;
/*! Compensated temperature */
double temperature;
/*! Compensated humidity */
double humidity;
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;
struct bme280_data
{
/*! Compensated pressure */
uint32_t pressure;
/*! Compensated temperature */
int32_t temperature;
/*! Compensated humidity */
uint32_t humidity;
};
#endif /* BME280_USE_FLOATING_POINT */
@ -309,52 +321,68 @@ struct bme280_data {
* @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;
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;
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 dev_id;
/*! SPI/I2C interface */
enum bme280_intf intf;
/*! 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;
struct bme280_dev
{
/*! Chip Id */
uint8_t chip_id;
/*! Device Id */
uint8_t dev_id;
/*! SPI/I2C interface */
enum bme280_intf intf;
/*! 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,5 +1,5 @@
/**\mainpage
* Copyright (C) 2016 - 2017 Bosch Sensortec GmbH
* Copyright (C) 2018 - 2019 Bosch Sensortec GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -39,20 +39,20 @@
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
*
* File bme280_selftest.c
* Date 21 Nov 2017
* Version 1.0.0
* File bme280_selftest.c
* Date 08 Mar 2019
* Version 3.3.6
*
*/
#include "bme280_selftest.h"
#define BME280_CRC_DATA_ADDR UINT8_C(0xE8)
#define BME280_CRC_DATA_LEN UINT8_C(1)
#define BME280_CRC_CALIB1_ADDR UINT8_C(0x88)
#define BME280_CRC_CALIB1_LEN UINT8_C(26)
#define BME280_CRC_CALIB2_ADDR UINT8_C(0xE1)
#define BME280_CRC_CALIB2_LEN UINT8_C(7)
#define BME280_CRC_DATA_ADDR UINT8_C(0xE8)
#define BME280_CRC_DATA_LEN UINT8_C(1)
#define BME280_CRC_CALIB1_ADDR UINT8_C(0x88)
#define BME280_CRC_CALIB1_LEN UINT8_C(26)
#define BME280_CRC_CALIB2_ADDR UINT8_C(0xE1)
#define BME280_CRC_CALIB2_LEN UINT8_C(7)
/*!
* @brief This API calculates the CRC
@ -75,36 +75,44 @@ static uint8_t crc_calculate(uint8_t *mem_values, uint8_t mem_length);
*/
int8_t bme280_crc_selftest(const struct bme280_dev *dev)
{
int8_t rslt;
uint8_t reg_addr;
uint8_t reg_data[64];
int8_t rslt;
uint8_t reg_addr;
uint8_t reg_data[64];
uint8_t stored_crc = 0;
uint8_t calculated_crc = 0;
uint8_t stored_crc = 0;
uint8_t calculated_crc = 0;
/* Read stored crc value from register */
reg_addr = BME280_CRC_DATA_ADDR;
rslt = bme280_get_regs(reg_addr, reg_data, BME280_CRC_DATA_LEN, dev);
if (rslt == BME280_OK)
{
stored_crc = reg_data[0];
/* Read stored crc value from register */
reg_addr = BME280_CRC_DATA_ADDR;
rslt = bme280_get_regs(reg_addr, reg_data, BME280_CRC_DATA_LEN, dev);
if (rslt == BME280_OK) {
stored_crc = reg_data[0];
/* Calculated CRC value with calibration register */
reg_addr = BME280_CRC_CALIB1_ADDR;
rslt = bme280_get_regs(reg_addr, &reg_data[0], BME280_CRC_CALIB1_LEN, dev);
if (rslt == BME280_OK) {
reg_addr = BME280_CRC_CALIB2_ADDR;
rslt = bme280_get_regs(reg_addr, &reg_data[BME280_CRC_CALIB1_LEN], BME280_CRC_CALIB2_LEN, dev);
if (rslt == BME280_OK) {
calculated_crc = crc_calculate(reg_data, BME280_CRC_CALIB1_LEN + BME280_CRC_CALIB2_LEN);
/* Validate CRC */
if (stored_crc == calculated_crc)
rslt = BME280_OK;
else
rslt = BME280_W_SELF_TEST_FAIL;
}
}
}
/* Calculated CRC value with calibration register */
reg_addr = BME280_CRC_CALIB1_ADDR;
rslt = bme280_get_regs(reg_addr, &reg_data[0], BME280_CRC_CALIB1_LEN, dev);
if (rslt == BME280_OK)
{
reg_addr = BME280_CRC_CALIB2_ADDR;
rslt = bme280_get_regs(reg_addr, &reg_data[BME280_CRC_CALIB1_LEN], BME280_CRC_CALIB2_LEN, dev);
if (rslt == BME280_OK)
{
calculated_crc = crc_calculate(reg_data, BME280_CRC_CALIB1_LEN + BME280_CRC_CALIB2_LEN);
return rslt;
/* Validate CRC */
if (stored_crc == calculated_crc)
{
rslt = BME280_OK;
}
else
{
rslt = BME280_W_SELF_TEST_FAIL;
}
}
}
}
return rslt;
}
/*!
@ -118,24 +126,30 @@ int8_t bme280_crc_selftest(const struct bme280_dev *dev)
*/
static uint8_t crc_calculate(uint8_t *mem_values, uint8_t mem_length)
{
uint32_t crc_reg = 0xFF;
uint8_t polynomial = 0x1D;
uint8_t bitNo, index;
uint8_t din = 0;
uint32_t crc_reg = 0xFF;
uint8_t polynomial = 0x1D;
uint8_t bitNo, index;
uint8_t din = 0;
for (index = 0; index < mem_length; index++) {
for (bitNo = 0; bitNo < 8; bitNo++) {
if (((crc_reg & 0x80) > 0) ^ ((mem_values[index] & 0x80) > 0))
din = 1;
else
din = 0;
for (index = 0; index < mem_length; index++)
{
for (bitNo = 0; bitNo < 8; bitNo++)
{
if (((crc_reg & 0x80) > 0) != ((mem_values[index] & 0x80) > 0))
{
din = 1;
}
else
{
din = 0;
}
/* Truncate 8th bit for crc_reg and mem_values */
crc_reg = (uint32_t)((crc_reg & 0x7F) << 1);
mem_values[index] = (uint8_t)((mem_values[index] & 0x7F) << 1);
crc_reg = (uint32_t)(crc_reg ^ (polynomial * din));
}
}
/* Truncate 8th bit for crc_reg and mem_values */
crc_reg = (uint32_t)((crc_reg & 0x7F) << 1);
mem_values[index] = (uint8_t)((mem_values[index] & 0x7F) << 1);
crc_reg = (uint32_t)(crc_reg ^ (polynomial * din));
}
}
return (uint8_t)(crc_reg ^ 0xFF);
return (uint8_t)(crc_reg ^ 0xFF);
}

View File

@ -1,5 +1,5 @@
/**\mainpage
* Copyright (C) 2016 - 2017 Bosch Sensortec GmbH
* Copyright (C) 2018 - 2019 Bosch Sensortec GmbH
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
@ -39,16 +39,16 @@
* No license is granted by implication or otherwise under any patent or
* patent rights of the copyright holder.
*
* File bme280_selftest.h
* Date 21 Nov 2017
* Version 1.0.0
* File bme280_selftest.h
* Date 08 Mar 2019
* Version 3.3.6
*
*/
/*!
/*!
* @addtogroup bme280_selftest
* @brief
* @{*/
*/
#ifndef BME280_SELFTEST_H_
#define BME280_SELFTEST_H_
@ -57,12 +57,11 @@
/*! CPP guard */
#ifdef __cplusplus
extern "C"
{
extern "C" {
#endif
/**\name API warning code */
#define BME280_W_SELF_TEST_FAIL INT8_C(2)
#define BME280_W_SELF_TEST_FAIL INT8_C(2)
/*!
* @brief This API reads the stored CRC and then compare with calculated CRC
@ -74,12 +73,10 @@ extern "C"
*/
int8_t bme280_crc_selftest(const struct bme280_dev *dev);
/*! CPP guard */
#ifdef __cplusplus
}
#endif
#endif /* BME280_SELFTEST_H_ */
/** @}*/