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:
Mon Jan 30 21:35:24 2017 +0000
Revision:
3:4622b90b4657
Parent:
2:dcd90bee9ac2
Child:
4:ed587a5331e4
Added timeout handling in communication; Factorized transfer and wait functions

Who changed what in which revision?

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