From: Ezhil Mani R Date: Mon, 4 Jul 2016 12:53:20 +0000 (+0530) Subject: Chip Id Retry functionality added. X-Git-Url: http://git.itanic.dy.fi/?p=BME280_driver;a=commitdiff_plain;h=28982a1627d72f9398a119918e403e928592c7b2 Chip Id Retry functionality added. --- diff --git a/README.md b/README.md index 7092310..02ba2e5 100644 --- a/README.md +++ b/README.md @@ -16,9 +16,9 @@ INTRODUCTION VERSION ========= - Version of bme280 sensor driver is: - * bme280.c - V2.0.4 - * bme280.h - V2.0.4 - * bme280_support.c - V1.0.5 + * bme280.c - V2.0.5 + * bme280.h - V2.0.5 + * bme280_support.c - V1.0.6 INTEGRATION DETAILS ===================== @@ -55,7 +55,7 @@ SUPPORTED SENSOR INTERFACE COPYRIGHT =========== - - Copyright (C) 2013 - 2015 Bosch Sensortec GmbH + - Copyright (C) 2013 - 2016 Bosch Sensortec GmbH diff --git a/bme280.c b/bme280.c index 7680242..f1bc1ae 100644 --- a/bme280.c +++ b/bme280.c @@ -1,10 +1,10 @@ /* **************************************************************************** -* Copyright (C) 2013 - 2015 Bosch Sensortec GmbH +* Copyright (C) 2015 - 2016 Bosch Sensortec GmbH * * bme280.c -* Date: 2015/03/27 -* Revision: 2.0.4(Pressure and Temperature compensation code revision is 1.1 +* Date: 2016/07/04 +* Revision: 2.0.5(Pressure and Temperature compensation code revision is 1.1 * and Humidity compensation code revision is 1.0) * * Usage: Sensor Driver file for BME280 sensor @@ -84,17 +84,35 @@ BME280_RETURN_FUNCTION_TYPE bme280_init(struct bme280_t *bme280) /* used to return the communication result*/ BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR; u8 v_data_u8 = BME280_INIT_VALUE; + u8 v_chip_id_read_count = BME280_CHIP_ID_READ_COUNT; - p_bme280 = bme280; /* assign BME280 ptr */ - com_rslt = p_bme280->BME280_BUS_READ_FUNC(p_bme280->dev_addr, - BME280_CHIP_ID_REG, &v_data_u8, - BME280_GEN_READ_WRITE_DATA_LENGTH); - /* read Chip Id */ + p_bme280 = bme280; + + while (v_chip_id_read_count > 0) { + + /* read Chip Id */ + com_rslt = p_bme280->BME280_BUS_READ_FUNC(p_bme280->dev_addr, + BME280_CHIP_ID_REG, &v_data_u8, + BME280_GEN_READ_WRITE_DATA_LENGTH); + /* Check for the correct chip id */ + if (v_data_u8 == BME280_CHIP_ID) + break; + v_chip_id_read_count--; + /* Delay added concerning the low speed of power up system to + facilitate the proper reading of the chip ID */ + p_bme280->delay_msec(BME280_REGISTER_READ_DELAY); + } + /*assign chip ID to the global structure*/ p_bme280->chip_id = v_data_u8; + /*com_rslt status of chip ID read*/ + com_rslt = (v_chip_id_read_count == BME280_INIT_VALUE) ? + BME280_CHIP_ID_READ_FAIL : BME280_CHIP_ID_READ_SUCCESS; - com_rslt += bme280_get_calib_param(); - /* readout bme280 calibparam structure */ + if (com_rslt == BME280_CHIP_ID_READ_SUCCESS) { + /* readout bme280 calibparam structure */ + com_rslt += bme280_get_calib_param(); + } return com_rslt; } /*! @@ -1824,7 +1842,7 @@ s32 *v_uncom_temperature_s32, s32 *v_uncom_humidity_s32) /* used to return the communication result*/ BME280_RETURN_FUNCTION_TYPE com_rslt = ERROR; u8 v_data_u8 = BME280_INIT_VALUE; - u8 v_waittime_u8r = BME280_INIT_VALUE; + u8 v_waittime_u8 = BME280_INIT_VALUE; u8 v_prev_pow_mode_u8 = BME280_INIT_VALUE; u8 v_mode_u8r = BME280_INIT_VALUE; u8 pre_ctrl_config_value = BME280_INIT_VALUE; @@ -1874,8 +1892,8 @@ s32 *v_uncom_temperature_s32, s32 *v_uncom_humidity_s32) BME280_CTRL_MEAS_REG, &v_mode_u8r, BME280_GEN_READ_WRITE_DATA_LENGTH); } - bme280_compute_wait_time(&v_waittime_u8r); - p_bme280->delay_msec(v_waittime_u8r); + bme280_compute_wait_time(&v_waittime_u8); + p_bme280->delay_msec(v_waittime_u8); /* read the force-mode value of pressure temperature and humidity*/ com_rslt += @@ -2033,7 +2051,7 @@ double bme280_compensate_pressure_double(s32 v_uncom_pressure_s32) ((double)p_bme280->cal_param.dig_P1); pressure = 1048576.0 - (double)v_uncom_pressure_s32; /* Avoid exception caused by division by zero */ - if (v_x1_u32 != BME280_INIT_VALUE) + if ((v_x1_u32 > 0) || (v_x1_u32 < 0)) pressure = (pressure - (v_x2_u32 / 4096.0)) * 6250.0 / v_x1_u32; else return BME280_INVALID_DATA; @@ -2062,7 +2080,7 @@ double bme280_compensate_humidity_double(s32 v_uncom_humidity_s32) double var_h = BME280_INIT_VALUE; var_h = (((double)p_bme280->cal_param.t_fine) - 76800.0); - if (var_h != BME280_INIT_VALUE) + if ((var_h > 0) || (var_h < 0)) var_h = (v_uncom_humidity_s32 - (((double)p_bme280->cal_param.dig_H4) * 64.0 + ((double)p_bme280->cal_param.dig_H5) / 16384.0 * var_h))* @@ -2204,9 +2222,9 @@ BME280_RETURN_FUNCTION_TYPE bme280_compute_wait_time(u8 >> BME280_SHIFT_BIT_POSITION_BY_01_BIT) + ((1 << p_bme280->oversamp_humidity) >> BME280_SHIFT_BIT_POSITION_BY_01_BIT)) - + (p_bme280->oversamp_pressure ? + + ((p_bme280->oversamp_pressure > 0) ? T_SETUP_PRESSURE_MAX : 0) + - (p_bme280->oversamp_humidity ? + ((p_bme280->oversamp_humidity > 0) ? T_SETUP_HUMIDITY_MAX : 0) + 15) / 16; return com_rslt; } diff --git a/bme280.h b/bme280.h index 49817ab..e2c6e86 100644 --- a/bme280.h +++ b/bme280.h @@ -1,13 +1,13 @@ /** \mainpage * **************************************************************************** -* Copyright (C) 2013 - 2015 Bosch Sensortec GmbH +* Copyright (C) 2015 - 2016 Bosch Sensortec GmbH * * File : bme280.h * -* Date : 2015/03/27 +* Date : 2016/07/04 * -* Revision : 2.0.4(Pressure and Temperature compensation code revision is 1.1 +* Revision : 2.0.5(Pressure and Temperature compensation code revision is 1.1 * and Humidity compensation code revision is 1.0) * * Usage: Sensor Driver for BME280 sensor @@ -95,7 +95,7 @@ typedef u_int64_t u64;/**< used for unsigned 64bit */ * These definition uses for define the C * standard version data types ***********************************************************/ -# if !defined(__STDC_VERSION__) +# if defined(__STDC_VERSION__) /************************************************ * compiler is C11 C standard @@ -179,7 +179,7 @@ could not be found, or 64 bit integers are not supported in your environment. #warning The API will only offer 32 bit pressure calculation.This will \ slightly impede accuracy(noise of ~1 pascal RMS will be added to output). #warning If 64 bit integers are supported on your platform, \ -please set s64 manually and "#define(BMPE80_64BITSUPPORT_PRESENT)" manually. +please set s64 manually and "#define(BME280_64BITSUPPORT_PRESENT)" manually. #endif /*unsigned integer types*/ @@ -310,7 +310,7 @@ define the data types manually #endif #endif /********************************************/ -/**\name ENABLE FLATING OUTPUT */ +/**\name ENABLE FLOATING OUTPUT */ /**************************************/ /*! * @brief If the user wants to support floating point calculations, please set @@ -438,7 +438,7 @@ BME280_BUS_RD_PARAM_TYPE to function calls used inside the API #define BME280_NULL (0) #define BME280_RETURN_FUNCTION_TYPE s8 /* shift definitions*/ -#define BME280_SHIFT_BIT_POSITION_BY_01_BIT (1) +#define BME280_SHIFT_BIT_POSITION_BY_01_BIT (1) #define BME280_SHIFT_BIT_POSITION_BY_02_BITS (2) #define BME280_SHIFT_BIT_POSITION_BY_03_BITS (3) #define BME280_SHIFT_BIT_POSITION_BY_04_BITS (4) @@ -469,8 +469,9 @@ BME280_BUS_RD_PARAM_TYPE to function calls used inside the API #define BME280_TEMPERATURE_DATA_LENGTH (3) #define BME280_PRESSURE_DATA_LENGTH (3) #define BME280_ALL_DATA_FRAME_LENGTH (8) -#define BME280_INIT_VALUE (0) -#define BME280_INVALID_DATA (0) +#define BME280_INIT_VALUE (0) +#define BME280_CHIP_ID_READ_COUNT (5) +#define BME280_INVALID_DATA (0) /****************************************************/ /**\name ERROR CODE DEFINITIONS */ @@ -480,6 +481,14 @@ BME280_BUS_RD_PARAM_TYPE to function calls used inside the API #define E_BME280_COMM_RES ((s8)-1) #define E_BME280_OUT_OF_RANGE ((s8)-2) #define ERROR ((s8)-1) +#define BME280_CHIP_ID_READ_FAIL ((s8)-1) +#define BME280_CHIP_ID_READ_SUCCESS ((u8)0) + +/****************************************************/ +/**\name CHIP ID DEFINITIONS */ +/***************************************************/ +#define BME280_CHIP_ID (0x60) + /****************************************************/ /**\name I2C ADDRESS DEFINITIONS */ /***************************************************/ @@ -778,9 +787,10 @@ FOR PRESSURE AND TEMPERATURE DATA */ s8 (*bus_read)(u8, u8,\ u8 *, u8) -#define BME280_MDELAY_DATA_TYPE u16 +#define BME280_MDELAY_DATA_TYPE u32 -#define BME280_3MS_DELAY 3 +#define BME280_3MS_DELAY (3) +#define BME280_REGISTER_READ_DELAY (1) /**************************************************************/ /**\name STRUCTURE DEFINITIONS */ /**************************************************************/ @@ -1700,5 +1710,5 @@ s32 v_uncom_pressure_s32); * */ BME280_RETURN_FUNCTION_TYPE bme280_compute_wait_time(u8 -*v_delaytime_u8r); +*v_delaytime_u8); #endif diff --git a/bme280_support.c b/bme280_support.c index 6703903..b15fa0d 100644 --- a/bme280_support.c +++ b/bme280_support.c @@ -1,10 +1,10 @@ /* **************************************************************************** -* Copyright (C) 2014 - 2015 Bosch Sensortec GmbH +* Copyright (C) 2015 - 2016 Bosch Sensortec GmbH * * bme280_support.c -* Date: 2015/03/27 -* Revision: 1.0.5 $ +* Date: 2016/07/04 +* Revision: 1.0.6 $ * * Usage: Sensor Driver support file for BME280 sensor * @@ -54,6 +54,8 @@ /*---------------------------------------------------------------------------*/ #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 @@ -127,17 +129,18 @@ 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_tem_s32 = BME280_INIT_VALUE; + 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 real temperature*/ - s32 v_actual_temp_s32 = BME280_INIT_VALUE; - /* The variable used to read real pressure*/ - u32 v_actual_press_u32 = BME280_INIT_VALUE; - /* The variable used to read real humidity*/ - u32 v_actual_humity_u32 = 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; @@ -146,9 +149,9 @@ s32 bme280_data_readout_template(void) /*********************** 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 + #ifdef BME280_API I2C_routine(); - /*SPI_routine(); */ + /*SPI_routine();*/ #endif /*--------------------------------------------------------------------------* * This function used to assign the value/reference of @@ -208,7 +211,7 @@ s32 bme280_data_readout_template(void) AND HUMIDITY DATA ******** *---------------------------------------------------------------------*/ /* API is used to read the uncompensated temperature*/ - com_rslt += bme280_read_uncomp_temperature(&v_data_uncomp_tem_s32); + 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); @@ -219,32 +222,32 @@ AND HUMIDITY DATA ******** /* API is used to read the uncompensated temperature,pressure and humidity data */ com_rslt += bme280_read_uncomp_pressure_temperature_humidity( - &v_data_uncomp_tem_s32, &v_data_uncomp_pres_s32, &v_data_uncomp_hum_s32); + &v_data_uncomp_temp_s32, &v_data_uncomp_pres_s32, &v_data_uncomp_hum_s32); /*--------------------------------------------------------------------* ************ END READ UNCOMPENSATED PRESSURE AND TEMPERATURE******** *-------------------------------------------------------------------------*/ /*------------------------------------------------------------------* -************ START READ TRUE PRESSURE, TEMPERATURE +************ START READ COMPENSATED PRESSURE, TEMPERATURE AND HUMIDITY DATA ******** *---------------------------------------------------------------------*/ - /* API is used to read the true temperature*/ - /* Input value as uncompensated temperature and output format*/ - com_rslt += bme280_compensate_temperature_int32(v_data_uncomp_tem_s32); + /* 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 read the true pressure*/ - /* Input value as uncompensated pressure */ - com_rslt += bme280_compensate_pressure_int32(v_data_uncomp_pres_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 read the true humidity*/ - /* Input value as uncompensated humidity and output format*/ - com_rslt += bme280_compensate_H_int32(v_data_uncomp_hum_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 true temperature, humidity and pressure*/ + /* API is used to read the compensated temperature, humidity and pressure*/ com_rslt += bme280_read_pressure_temperature_humidity( - &v_actual_press_u32, &v_actual_temp_s32, &v_actual_humity_u32); + &v_comp_press_u32[1], &v_comp_temp_s32[1], &v_comp_humidity_u32[1]); /*--------------------------------------------------------------------* -************ END READ TRUE PRESSURE, TEMPERATURE AND HUMIDITY ******** +************ END READ COMPENSATED PRESSURE, TEMPERATURE AND HUMIDITY ******** *-------------------------------------------------------------------------*/ /*-----------------------------------------------------------------------* @@ -265,9 +268,10 @@ return com_rslt; } #ifdef BME280_API -#define MASK_DATA1 0xFF -#define MASK_DATA2 0x80 -#define MASK_DATA3 0x7F +#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 @@ -331,12 +335,12 @@ s8 BME280_I2C_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) u8 stringpos = BME280_INIT_VALUE; array[BME280_INIT_VALUE] = reg_addr; for (stringpos = BME280_INIT_VALUE; stringpos < cnt; stringpos++) { - array[stringpos + BME280_ONE_U8X] = *(reg_data + 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)" + * "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 @@ -389,12 +393,12 @@ s8 BME280_I2C_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) 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]={MASK_DATA1}; + 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|MASK_DATA2;/*read routine is initiated register address is mask with 0x80*/ + 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 @@ -403,7 +407,7 @@ s8 BME280_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) * 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 + * and FAILURE defined as -1 * Note : * This is a full duplex operation, * The first read data is discarded, for that extra write operation @@ -412,7 +416,7 @@ s8 BME280_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) * For more information please refer data sheet SPI communication: */ for (stringpos = BME280_INIT_VALUE; stringpos < cnt; stringpos++) { - *(reg_data + stringpos) = array[stringpos+BME280_ONE_U8X]; + *(reg_data + stringpos) = array[stringpos+BME280_DATA_INDEX]; } return (s8)iError; } @@ -420,7 +424,7 @@ s8 BME280_SPI_bus_read(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_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 @@ -428,14 +432,15 @@ s8 BME280_SPI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) 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_TWO_U8X]; + 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: because it ensure the - BME280_INIT_VALUE and 1 of the given value - It is done only for 8bit operation*/ - array[stringpos * BME280_TWO_U8X] = (reg_addr++) & MASK_DATA3; - array[stringpos * BME280_TWO_U8X + BME280_ONE_U8X] = *(reg_data + 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 @@ -444,7 +449,7 @@ s8 BME280_SPI_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 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 + * and FAILURE defined as -1 */ return (s8)iError; }