Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Revision 4:a1c46ef057a6, committed 2019-09-03
- Comitter:
- mcm
- Date:
- Tue Sep 03 15:59:50 2019 +0000
- Parent:
- 3:ab857b70346e
- Commit message:
- The driver was completed and tested ( NUCLEO-L152RE ), it works as expected.
Changed in this revision
BME280.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/BME280.h Tue Sep 03 15:36:35 2019 +0000 +++ b/BME280.h Tue Sep 03 15:59:50 2019 +0000 @@ -81,10 +81,10 @@ #include "mbed.h" #include "BME280.h" -BME680 myBME280 ( I2C_SDA, I2C_SCL, 400000 ); +BME280 myBME280 ( I2C_SDA, I2C_SCL, 400000 ); Serial pc ( USBTX, USBRX ); -DigitalOut myled ( LED1 ); +DigitalOut myled ( LED1 ); Ticker newReading; uint32_t myState = 0; @@ -100,12 +100,36 @@ //@brief FUNCTION FOR APPLICATION MAIN ENTRY. int main() { + uint8_t settings_sel; + struct bme280_dev dev; + struct bme280_data comp_data; + int8_t rslt = BME280_OK; + pc.baud ( 115200 ); myled = 1; wait(3); myled = 0; + // Configure the device I2C interface + dev.dev_id = BME280_I2C_ADDR_PRIM; + dev.intf = BME280_I2C_INTF; + dev.read = user_i2c_read; + dev.write = user_i2c_write; + dev.delay_ms = user_delay_ms; + + rslt = myBME280.bme280_init(&dev); + + // Configure the device in Force mode + 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; + + rslt = myBME280.bme280_set_sensor_settings(settings_sel, &dev); + newReading.attach( &changeDATA, 1 ); // the address of the function to be attached ( changeDATA ) and the interval ( 1s ) @@ -117,8 +141,19 @@ myled = 1; if ( myState == 1 ) { + // Trigger a new sample + rslt = myBME280.bme280_set_sensor_mode(BME280_FORCED_MODE, &dev); - myState = 0; // Reset the variable + // Wait for the measurement to complete and print data @25Hz + dev.delay_ms(40); + + // Get the data + rslt = myBME280.bme280_get_sensor_data(BME280_ALL, &comp_data, &dev); + + // Transmit result over the UART + pc.printf( "T: %ld C | P: %ld Pa | RH: %ld %%\r\n", ( comp_data.temperature / 100 ), comp_data.pressure, ( comp_data.humidity / 1024 ) ); + + myState = 0; // Reset the variable } myled = 0; @@ -126,78 +161,74 @@ } - - - // @brief changeDATA ( void ) - // - // @details It changes myState variable - // - // @param[in] N/A - // - // @param[out] N/A. - // - // - // @return N/A.. - // - // - // @author Manuel Caballero - // @date 03/September/2019 - // @version 03/September/2019 The ORIGIN - // @pre N/A - // @warning N/A. +// @brief changeDATA ( void ) +// +// @details It changes myState variable +// +// @param[in] N/A +// +// @param[out] N/A. +// +// +// @return N/A.. +// +// +// @author Manuel Caballero +// @date 03/September/2019 +// @version 03/September/2019 The ORIGIN +// @pre N/A +// @warning N/A. void changeDATA ( void ) { myState = 1; } - - // @brief user_delay_ms ( uint32_t ) - // - // @details Return control or wait, for a period amount of milliseconds - // - // @param[in] period: Delay in milliseconds. - // - // @param[out] N/A. - // - // - // @return N/A.. - // - // - // @author Manuel Caballero - // @date 03/September/2019 - // @version 03/September/2019 The ORIGIN - // @pre This is a Bosh pointer function adapted to our system. - // @warning N/A. +// @brief user_delay_ms ( uint32_t ) +// +// @details Return control or wait, for a period amount of milliseconds +// +// @param[in] period: Delay in milliseconds. +// +// @param[out] N/A. +// +// +// @return N/A.. +// +// +// @author Manuel Caballero +// @date 03/September/2019 +// @version 03/September/2019 The ORIGIN +// @pre This is a Bosh pointer function adapted to our system. +// @warning N/A. void user_delay_ms ( uint32_t period ) { - // Return control or wait, - // for a period amount of milliseconds + // Return control or wait, + // for a period amount of milliseconds wait_ms ( period ); } - - // @brief user_i2c_read ( uint8_t , uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) - // - // @details It adapts I2C reading functionality. - // - // @param[in] dev_id: I2C address. - // @param[in] reg_addr: Register to be read. - // @param[in] len: How many bytes to read. - // - // @param[out] reg_data: Result. - // - // - // @return Status of user_i2c_read. - // - // - // @author Manuel Caballero - // @date 03/September/2019 - // @version 03/September/2019 The ORIGIN - // @pre This is a Bosh pointer function adapted to our system. - // @warning N/A. +// @brief user_i2c_read ( uint8_t , uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) +// +// @details It adapts I2C reading functionality. +// +// @param[in] dev_id: I2C address. +// @param[in] reg_addr: Register to be read. +// @param[in] len: How many bytes to read. +// +// @param[out] reg_data: Result. +// +// +// @return Status of user_i2c_read. +// +// +// @author Manuel Caballero +// @date 03/September/2019 +// @version 03/September/2019 The ORIGIN +// @pre This is a Bosh pointer function adapted to our system. +// @warning N/A. int8_t user_i2c_read ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) { int8_t rslt = 0; // Return 0 for Success, non-zero for failure @@ -205,48 +236,57 @@ // The parameter dev_id can be used as a variable to store the I2C address of the device - // Data on the bus should be like - // |------------+---------------------| - // | I2C action | Data | - // |------------+---------------------| - // | Start | - | - // | Write | (reg_addr) | - // | Stop | - | - // | Start | - | - // | Read | (reg_data[0]) | - // | Read | (....) | - // | Read | (reg_data[len - 1]) | - // | Stop | - | - // |------------+---------------------| + // Data on the bus should be like + // |------------+---------------------| + // | I2C action | Data | + // |------------+---------------------| + // | Start | - | + // | Write | (reg_addr) | + // | Stop | - | + // | Start | - | + // | Read | (reg_data[0]) | + // | Read | (....) | + // | Read | (reg_data[len - 1]) | + // | Stop | - | + // |------------+---------------------| + // Read data + uint32_t aux = 0; + aux = myBME280._i2c.write ( dev_id, (char*)®_addr, 1, true ); + aux = myBME280._i2c.read ( dev_id, (char*)®_data[0], len ); + + + if ( aux == 0 ) { + rslt = 0; + } else { + rslt = 0xFF; + } return rslt; } - - - // @brief user_i2c_write ( uint8_t , uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) - // - // @details It adapts I2C writing functionality. - // - // @param[in] dev_id: I2C address. - // @param[in] reg_addr: Register to be read. - // @param[out] reg_data: Data to be written. - // @param[in] len: How many bytes to read. - // - // @param[out] N/A. - // - // - // @return Status of user_i2c_write. - // - // - // @author Manuel Caballero - // @date 03/September/2019 - // @version 03/September/2019 The ORIGIN - // @pre This is a Bosh pointer function adapted to our system. - // @warning N/A. +// @brief user_i2c_write ( uint8_t , uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) +// +// @details It adapts I2C writing functionality. +// +// @param[in] dev_id: I2C address. +// @param[in] reg_addr: Register to be read. +// @param[out] reg_data: Data to be written. +// @param[in] len: How many bytes to read. +// +// @param[out] N/A. +// +// +// @return Status of user_i2c_write. +// +// +// @author Manuel Caballero +// @date 03/September/2019 +// @version 03/September/2019 The ORIGIN +// @pre This is a Bosh pointer function adapted to our system. +// @warning N/A. int8_t user_i2c_write ( uint8_t dev_id, uint8_t reg_addr, uint8_t *reg_data, uint16_t len ) { int8_t rslt = 0; // Return 0 for Success, non-zero for failure @@ -266,7 +306,25 @@ // | Stop | - | // |------------+---------------------| + uint32_t aux = 0; + char cmd[64] = { 0 }; + uint32_t i = 0; + // Prepare the data to be sent + cmd[0] = reg_addr; + for ( i = 1; i <= len; i++ ) { + cmd[i] = reg_data[i - 1]; + } + + // Write data + aux = myBME280._i2c.write ( dev_id, &cmd[0], len + 1, false ); + + + if ( aux == 0 ) { + rslt = 0; + } else { + rslt = 0xFF; + } return rslt; } @@ -525,7 +583,7 @@ * @retval Compensated pressure data in double. */ double compensate_pressure(const struct bme280_uncomp_data *uncomp_data, - const struct bme280_calib_data *calib_data); + const struct bme280_calib_data *calib_data); /*! * @brief This internal API is used to compensate the raw humidity data and @@ -538,7 +596,7 @@ * @retval Compensated humidity data in double. */ double compensate_humidity(const struct bme280_uncomp_data *uncomp_data, - const struct bme280_calib_data *calib_data); + const struct bme280_calib_data *calib_data); /*! * @brief This internal API is used to compensate the raw temperature data and @@ -551,7 +609,7 @@ * @retval Compensated temperature data in double. */ double compensate_temperature(const struct bme280_uncomp_data *uncomp_data, - struct bme280_calib_data *calib_data); + struct bme280_calib_data *calib_data); #else @@ -566,7 +624,7 @@ * @retval Compensated temperature data in integer. */ int32_t compensate_temperature(const struct bme280_uncomp_data *uncomp_data, - struct bme280_calib_data *calib_data); + struct bme280_calib_data *calib_data); /*! * @brief This internal API is used to compensate the raw pressure data and @@ -579,7 +637,7 @@ * @retval Compensated pressure data in integer. */ uint32_t compensate_pressure(const struct bme280_uncomp_data *uncomp_data, - const struct bme280_calib_data *calib_data); + const struct bme280_calib_data *calib_data); /*! * @brief This internal API is used to compensate the raw humidity data and @@ -592,7 +650,7 @@ * @retval Compensated humidity data in integer. */ uint32_t compensate_humidity(const struct bme280_uncomp_data *uncomp_data, - const struct bme280_calib_data *calib_data); + const struct bme280_calib_data *calib_data); #endif @@ -633,8 +691,8 @@ * @retval zero -> Success / +ve value -> Warning / -ve value -> Error */ int8_t set_osr_settings(uint8_t desired_settings, - const struct bme280_settings *settings, - const struct bme280_dev *dev); + const struct bme280_settings *settings, + const struct bme280_dev *dev); /*! * @brief This API sets the pressure and/or temperature oversampling settings @@ -648,8 +706,8 @@ * @retval zero -> Success / +ve value -> Warning / -ve value -> Error */ int8_t set_osr_press_temp_settings(uint8_t desired_settings, - const struct bme280_settings *settings, - const struct bme280_dev *dev); + const struct bme280_settings *settings, + const struct bme280_dev *dev); /*! * @brief This internal API fills the pressure oversampling settings provided by @@ -683,8 +741,8 @@ * @retval zero -> Success / +ve value -> Warning / -ve value -> Error */ int8_t set_filter_standby_settings(uint8_t desired_settings, - const struct bme280_settings *settings, - const struct bme280_dev *dev); + const struct bme280_settings *settings, + const struct bme280_dev *dev); /*! * @brief This internal API fills the filter settings provided by the user