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.
Diff: BME280.cpp
- Revision:
- 2:4ed2f08d0eff
- Parent:
- 1:d766e0ac711f
- Child:
- 3:b3835216cc88
--- a/BME280.cpp Sat Nov 07 12:05:49 2015 +0000 +++ b/BME280.cpp Sat Nov 07 12:23:29 2015 +0000 @@ -47,7 +47,7 @@ extern Serial ser; -BME280::BME280(void) : m_pI2C(NULL), m_address(0x00), m_fine_temp(0x1F3E6), m_mode(MODE_SLEEP) { +BME280::BME280(void) : m_pI2C(NULL), m_address(0x00), m_fine_temp(0x1F3E6), m_mode(MODE_SLEEP),m_bOk(false) { // initialized m_fine_temp to 25C } @@ -56,12 +56,14 @@ } bool BME280::init(I2C& i2c, uint8_t address) { + m_bOk = false; m_pI2C = &i2c; m_address = address<<1; m_fine_temp = 0x1F3E6; if (!reset()) return false; if (read_chip_id()!=0x60) return false; if (!read_calibration()) return false; + m_bOk = true; return true; } @@ -140,19 +142,10 @@ bool BME280::raw_data(int32_t& rawT, int32_t& rawP, int32_t& rawH) { if (m_pI2C==NULL) return false; uint8_t data[8]; - uint8_t ok = 0x00; - m_pI2C->start(); - ok = (ok<<1)|m_pI2C->write(m_address); - ok = (ok<<1)|m_pI2C->write(REG_PRESS_MSB); - m_pI2C->start(); - ok = (ok<<1)|m_pI2C->write(m_address|1); - for (int i=0; i<7; i++) data[i] = m_pI2C->read(1); - data[7] = m_pI2C->read(0); - m_pI2C->stop(); - if (ok!=0x7) { - rawT = rawP = rawH = 0x00; + if (!burst_read(data,8,REG_PRESS_MSB)) { + rawT = rawP = rawH = 0; return false; - } + } rawP = (int32_t(data[0])<<12)|(int32_t(data[1])<<4)|(int32_t(data[2])>>4); rawT = (int32_t(data[3])<<12)|(int32_t(data[4])<<4)|(int32_t(data[5])>>4); rawH = (int32_t(data[6])<<8)|int32_t(data[7]); @@ -174,19 +167,9 @@ } bool BME280::read_calibration_lo(void) { + if (m_pI2C==NULL) return false; uint8_t data[VAL_CALIB00_25_SIZE]; - if (m_pI2C==NULL) return false; - uint8_t ok = 0x00; - m_pI2C->start(); - ok = (ok<<1)|m_pI2C->write(m_address); - ok = (ok<<1)|m_pI2C->write(REG_CALIB00_25_BASE); - m_pI2C->start(); - ok = (ok<<1)|m_pI2C->write(m_address|1); - for (int n=0; n<VAL_CALIB00_25_SIZE-1; n++) data[n] = m_pI2C->read(1); - data[VAL_CALIB00_25_SIZE-1] = m_pI2C->read(0); - m_pI2C->stop(); - if (ok!=0x7) return false; - + if (!burst_read(data,VAL_CALIB00_25_SIZE,REG_CALIB00_25_BASE)) return false; m_calib.dig_T1 = conv_u16(data,0,1); // 0x88:89 m_calib.dig_T2 = conv_s16(data,2,3); // 0x8A:8B m_calib.dig_T3 = conv_s16(data,4,5); // 0x8C:8D @@ -205,19 +188,9 @@ } bool BME280::read_calibration_hi(void) { + if (m_pI2C==NULL) return false; uint8_t data[VAL_CALIB26_41_SIZE]; - if (m_pI2C==NULL) return false; - uint8_t ok = 0x00; - m_pI2C->start(); - ok = (ok<<1)|m_pI2C->write(m_address); - ok = (ok<<1)|m_pI2C->write(REG_CALIB26_41_BASE); - m_pI2C->start(); - ok = (ok<<1)|m_pI2C->write(m_address|1); - for (int n=0; n<VAL_CALIB26_41_SIZE-1; n++) data[n] = m_pI2C->read(1); - data[VAL_CALIB26_41_SIZE-1] = m_pI2C->read(0); - m_pI2C->stop(); - if (ok!=0x7) return false; - + if (!burst_read(data,VAL_CALIB26_41_SIZE,REG_CALIB26_41_BASE)) return false; m_calib.dig_H2 = conv_s16(data,0,1); // 0xE1:E2 m_calib.dig_H3 = data[2]; // 0xE3 m_calib.dig_H4 = int16_t((uint16_t(data[3])<<8)|uint16_t((data[4]&0xF)<<4))>>4; // 0xE4:E5[3:0] @@ -267,6 +240,7 @@ // ======================================== LOW LEVEL API ======================================== bool BME280::reset(void) { + m_bOk = false; if (write8(REG_RESET,VAL_RESET)) { wait_ms(2); m_mode = MODE_SLEEP; @@ -315,4 +289,24 @@ const BME280::calib_t& BME280::calib(void) const { return m_calib; +} + +bool BME280::burst_read(uint8_t* data, uint8_t nBytes, uint8_t reg) { + if (m_pI2C==NULL) return false; + if (nBytes==0) return false; + if (data==NULL) return false; + uint8_t ok = 0x00; + m_pI2C->start(); + ok = (ok<<1)|m_pI2C->write(m_address); + ok = (ok<<1)|m_pI2C->write(reg); + m_pI2C->start(); + ok = (ok<<1)|m_pI2C->write(m_address|1); + for (int i=0; i<nBytes-1; i++) data[i] = m_pI2C->read(1); + data[nBytes-1] = m_pI2C->read(0); + m_pI2C->stop(); + if (ok!=0x7) { + memset(data,0,nBytes); + return false; + } + return true; } \ No newline at end of file