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:42:49 2017 +0000
Revision:
4:ed587a5331e4
Parent:
3:4622b90b4657
Child:
5:64852447275f
Fixed transfer bug

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