Change to comply with mbed os 5.1, as the attach function as used has been deprecated

Dependents:   Barometer-Example LoRaWAN-demo-72-bootcamp

Fork of BMP085 by silabs-abakurs

BMP085 library supporting mbed os low power

Committer:
abouillot
Date:
Thu Mar 02 14:04:09 2017 +0000
Revision:
5:64852447275f
Parent:
4:ed587a5331e4
Fixed some display error in traces and made traces switchable with #define TRACE

Who changed what in which revision?

UserRevisionLine numberNew contents of line
asmellby 0:fb197da45ba8 1 #include "BMP085.h"
asmellby 0:fb197da45ba8 2
abouillot 5:64852447275f 3 #if defined (TRACE)
abouillot 3:4622b90b4657 4 extern Serial pc;
abouillot 5:64852447275f 5 #endif
abouillot 3:4622b90b4657 6
abouillot 3:4622b90b4657 7 void BMP085::init(void)
abouillot 3:4622b90b4657 8 {
asmellby 0:fb197da45ba8 9 _callback.attach(this, &BMP085::_callback_handler);
abouillot 3:4622b90b4657 10
asmellby 0:fb197da45ba8 11 _cal_data.ac1 = read_cal_register(BMP085_CAL_AC1);
asmellby 0:fb197da45ba8 12 _cal_data.ac2 = read_cal_register(BMP085_CAL_AC2);
asmellby 0:fb197da45ba8 13 _cal_data.ac3 = read_cal_register(BMP085_CAL_AC3);
asmellby 0:fb197da45ba8 14 _cal_data.ac4 = read_cal_register(BMP085_CAL_AC4);
asmellby 0:fb197da45ba8 15 _cal_data.ac5 = read_cal_register(BMP085_CAL_AC5);
asmellby 0:fb197da45ba8 16 _cal_data.ac6 = read_cal_register(BMP085_CAL_AC6);
asmellby 0:fb197da45ba8 17 _cal_data.b1 = read_cal_register(BMP085_CAL_B1);
asmellby 0:fb197da45ba8 18 _cal_data.b2 = read_cal_register(BMP085_CAL_B2);
asmellby 0:fb197da45ba8 19 _cal_data.mb = read_cal_register(BMP085_CAL_MB);
asmellby 0:fb197da45ba8 20 _cal_data.mc = read_cal_register(BMP085_CAL_MC);
asmellby 0:fb197da45ba8 21 _cal_data.md = read_cal_register(BMP085_CAL_MD);
asmellby 0:fb197da45ba8 22 }
asmellby 0:fb197da45ba8 23
abouillot 3:4622b90b4657 24
abouillot 3:4622b90b4657 25 int BMP085::transfer(char *tx, int tx_len, char *rx, int rx_len)
abouillot 3:4622b90b4657 26 {
abouillot 3:4622b90b4657 27 LowPowerTimeout delay;
abouillot 3:4622b90b4657 28
abouillot 5:64852447275f 29 #if defined (TRACE)
abouillot 3:4622b90b4657 30 pc.printf("transfer ");
abouillot 5:64852447275f 31 #endif
asmellby 0:fb197da45ba8 32 _state = BMP085_BUSY;
abouillot 4:ed587a5331e4 33 _i2c.transfer(BMP085_ADDR, tx, tx_len, rx, rx_len, _callback);
abouillot 5:64852447275f 34 // set the timeout timer to guard the transfer
abouillot 3:4622b90b4657 35 delay.attach(callback(this, &BMP085::_callback_handler_timeout), 2.0);
abouillot 3:4622b90b4657 36 while(_state == BMP085_BUSY) {
asmellby 0:fb197da45ba8 37 sleep();
asmellby 0:fb197da45ba8 38 }
abouillot 5:64852447275f 39 delay.attach(NULL, 0); // cancel timeout timer
abouillot 3:4622b90b4657 40 if (_state == BMP085_TIMEOUT) {
abouillot 5:64852447275f 41 #if defined (TRACE)
abouillot 4:ed587a5331e4 42 pc.printf("\ntimeout\n");
abouillot 5:64852447275f 43 #endif
abouillot 3:4622b90b4657 44 _i2c.abort_transfer();
abouillot 3:4622b90b4657 45 return -33;
abouillot 3:4622b90b4657 46 }
abouillot 5:64852447275f 47 #if defined (TRACE)
abouillot 5:64852447275f 48 pc.printf(" completed ");
abouillot 5:64852447275f 49 #endif
abouillot 3:4622b90b4657 50 return 0;
abouillot 3:4622b90b4657 51 }
abouillot 3:4622b90b4657 52
abouillot 3:4622b90b4657 53 int BMP085::wait(float wait)
abouillot 3:4622b90b4657 54 {
abouillot 3:4622b90b4657 55 LowPowerTimeout delay;
abouillot 3:4622b90b4657 56
abouillot 5:64852447275f 57 #if defined (TRACE)
abouillot 5:64852447275f 58 pc.printf(" bmpWait ");
abouillot 5:64852447275f 59 #endif
abouillot 3:4622b90b4657 60 _state = BMP085_BUSY;
abouillot 3:4622b90b4657 61 delay.attach(callback(this, &BMP085::_callback_handler_timeout), wait);
abouillot 3:4622b90b4657 62 while(_state == BMP085_BUSY) {
abouillot 3:4622b90b4657 63 sleep();
abouillot 3:4622b90b4657 64 }
abouillot 5:64852447275f 65 #if defined (TRACE)
abouillot 3:4622b90b4657 66 pc.printf("over ");
abouillot 5:64852447275f 67 #endif
abouillot 3:4622b90b4657 68 return 0;
abouillot 3:4622b90b4657 69 }
abouillot 3:4622b90b4657 70
abouillot 3:4622b90b4657 71
abouillot 3:4622b90b4657 72
abouillot 3:4622b90b4657 73 uint16_t BMP085::read_cal_register(BMP085_Register reg)
abouillot 3:4622b90b4657 74 {
abouillot 5:64852447275f 75 #if defined (TRACE)
abouillot 3:4622b90b4657 76 pc.printf("\nread_cal_register ");
abouillot 5:64852447275f 77 #endif
abouillot 3:4622b90b4657 78 _tx_buf[0] = reg;
abouillot 3:4622b90b4657 79 transfer((char*)_tx_buf, 1, (char*)_rx_buf, 2);
asmellby 0:fb197da45ba8 80 return (_rx_buf[0] << 8) | _rx_buf[1];
asmellby 0:fb197da45ba8 81 }
asmellby 0:fb197da45ba8 82
abouillot 3:4622b90b4657 83 uint16_t BMP085::read_uc_temperature(void)
abouillot 3:4622b90b4657 84 {
abouillot 5:64852447275f 85 #if defined (TRACE)
abouillot 5:64852447275f 86 pc.printf("\nread_uc_temperature ");
abouillot 5:64852447275f 87 #endif
asmellby 0:fb197da45ba8 88 _tx_buf[0] = BMP085_CTRL;
asmellby 0:fb197da45ba8 89 _tx_buf[1] = BMP085_CMD_READ_TEMP;
abouillot 3:4622b90b4657 90 transfer((char*)_tx_buf, 2, (char*)NULL, 0);
asmellby 0:fb197da45ba8 91
abouillot 3:4622b90b4657 92 wait(0.01);
asmellby 0:fb197da45ba8 93
asmellby 0:fb197da45ba8 94 _tx_buf[0] = BMP085_DATA_TEMP;
abouillot 3:4622b90b4657 95 transfer((char*)_tx_buf, 1, (char*)_rx_buf, 2);
asmellby 0:fb197da45ba8 96 return (_rx_buf[0] << 8) | _rx_buf[1];
asmellby 0:fb197da45ba8 97 }
asmellby 0:fb197da45ba8 98
abouillot 3:4622b90b4657 99 uint32_t BMP085::read_uc_pressure(void)
abouillot 3:4622b90b4657 100 {
abouillot 5:64852447275f 101 #if defined (TRACE)
abouillot 5:64852447275f 102 pc.printf("\nread_uc_pressure ");
abouillot 5:64852447275f 103 #endif
abouillot 5:64852447275f 104 _tx_buf[0] = BMP085_CTRL;
asmellby 0:fb197da45ba8 105 _tx_buf[1] = BMP085_CMD_READ_PRESSURE + ((uint8_t)_mode << 6);
abouillot 3:4622b90b4657 106 transfer((char*)_tx_buf, 2, (char*)NULL, 0);
asmellby 0:fb197da45ba8 107
abouillot 3:4622b90b4657 108 wait(0.02);
asmellby 0:fb197da45ba8 109
asmellby 0:fb197da45ba8 110 _tx_buf[0] = BMP085_DATA_PRESSURE;
abouillot 3:4622b90b4657 111 transfer((char*)_tx_buf, 1, (char*)_rx_buf, 3);
abouillot 3:4622b90b4657 112
abouillot 3:4622b90b4657 113 return ((_rx_buf[0] << 16) | (_rx_buf[1] << 8) | _rx_buf[2]) >> (8 - (uint8_t)_mode);
asmellby 0:fb197da45ba8 114 }
asmellby 0:fb197da45ba8 115
abouillot 3:4622b90b4657 116 void BMP085::measure()
abouillot 3:4622b90b4657 117 {
asmellby 0:fb197da45ba8 118 int t, p, ut, up, x1, x2, x3, b3, b5, b6;
asmellby 0:fb197da45ba8 119 uint32_t b4, b7;
asmellby 0:fb197da45ba8 120
asmellby 0:fb197da45ba8 121 ut = read_uc_temperature();
asmellby 0:fb197da45ba8 122
asmellby 0:fb197da45ba8 123 x1 = (ut - _cal_data.ac6) * _cal_data.ac5 / (1 << 15);
asmellby 0:fb197da45ba8 124 x2 = (int)_cal_data.mc * (1 << 11) / (x1 + _cal_data.md);
asmellby 0:fb197da45ba8 125 b5 = x1 + x2;
asmellby 0:fb197da45ba8 126 t = (b5 + 8) / (1 << 4);
asmellby 0:fb197da45ba8 127 _temperature = (float)t / 10.0;
abouillot 3:4622b90b4657 128
asmellby 0:fb197da45ba8 129 up = read_uc_pressure();
abouillot 3:4622b90b4657 130
asmellby 0:fb197da45ba8 131 b6 = b5 - 4000;
asmellby 0:fb197da45ba8 132 x1 = (_cal_data.b2 * (b6 * b6 / (1 << 12))) / (1 << 11);
asmellby 0:fb197da45ba8 133 x2 = _cal_data.ac2 * b6 / (1 << 11);
asmellby 0:fb197da45ba8 134 x3 = x1 + x2;
asmellby 0:fb197da45ba8 135 b3 = ((((unsigned int)_cal_data.ac1 * 4 + x3) << (int)_mode) + 2) / 4;
asmellby 0:fb197da45ba8 136 x1 = _cal_data.ac3 * b6 / (1 << 13);
asmellby 0:fb197da45ba8 137 x2 = (_cal_data.b1 * (b6 * b6 / (1 << 12))) / (1 << 16);
asmellby 0:fb197da45ba8 138 x3 = ((x1 + x2) + 2) / (1 << 2);
asmellby 0:fb197da45ba8 139 b4 = _cal_data.ac4 * (unsigned int)(x3 + 32768) / (1 << 15);
asmellby 0:fb197da45ba8 140 b7 = ((unsigned int)up - b3) * (50000 >> (int)_mode);
asmellby 0:fb197da45ba8 141 if (b7 < (unsigned int)0x80000000) {
asmellby 0:fb197da45ba8 142 p = (b7 * 2) / b4;
asmellby 0:fb197da45ba8 143 } else {
asmellby 0:fb197da45ba8 144 p = (b7 / b4) * 2;
asmellby 0:fb197da45ba8 145 }
asmellby 0:fb197da45ba8 146 x1 = (p / (1 << 8)) * (p / (1 << 8));
asmellby 0:fb197da45ba8 147 x1 = (x1 * 3038) / (1 << 16);
asmellby 0:fb197da45ba8 148 x2 = (-7357 * p) / (1 << 16);
asmellby 0:fb197da45ba8 149 p = p + (x1 + x2 + 3791) / (1 << 4);
asmellby 0:fb197da45ba8 150 _pressure = (float)p / 100.0;
asmellby 0:fb197da45ba8 151 }
asmellby 0:fb197da45ba8 152
abouillot 3:4622b90b4657 153 void BMP085::_callback_handler(int event)
abouillot 3:4622b90b4657 154 {
asmellby 0:fb197da45ba8 155 _state = BMP085_IDLE;
asmellby 0:fb197da45ba8 156 }
asmellby 0:fb197da45ba8 157
abouillot 3:4622b90b4657 158 void BMP085::_callback_handler_timeout()
abouillot 3:4622b90b4657 159 {
abouillot 5:64852447275f 160 #if defined (TRACE)
abouillot 5:64852447275f 161 pc.printf(" bmp timeout callback ");
abouillot 5:64852447275f 162 #endif
abouillot 3:4622b90b4657 163 _state = BMP085_TIMEOUT;
asmellby 0:fb197da45ba8 164 }