48#include "lib/sensors.h"
53#define PRINTF(...) printf(__VA_ARGS__)
58static uint8_t enabled = 0;
72} bmpx8x_calibration_values;
75 uint8_t oversampling_mode;
77 bmpx8x_calibration_values calib;
80static bmpx8x_config bmpx8x_values;
83bmpx8x_read_reg(uint8_t reg, uint8_t *buf, uint8_t num)
85 if((buf == NULL) || (num <= 0)) {
86 PRINTF(
"BMPx8x: invalid read values\n");
94 return BMPx8x_SUCCESS;
101bmpx8x_write_reg(uint8_t *buf, uint8_t num)
103 if((buf == NULL) || (num <= 0)) {
104 PRINTF(
"BMPx8x: invalid write values\n");
109 if(
i2c_burst_send(BMPx8x_ADDR, buf, num) == I2C_MASTER_ERR_NONE) {
110 return BMPx8x_SUCCESS;
116bmpx8x_read_calib(
void)
120 if(bmpx8x_read_reg(BMPx8x_AC1_CALIB, buf,
124 bmpx8x_values.calib.ac1 = ((buf[0] << 8) + buf[1]);
125 bmpx8x_values.calib.ac2 = ((buf[2] << 8) + buf[3]);
126 bmpx8x_values.calib.ac3 = ((buf[4] << 8) + buf[5]);
127 bmpx8x_values.calib.ac4 = ((buf[6] << 8) + buf[7]);
128 bmpx8x_values.calib.ac5 = ((buf[8] << 8) + buf[9]);
129 bmpx8x_values.calib.ac6 = ((buf[10] << 8) + buf[11]);
130 bmpx8x_values.calib.b1 = ((buf[12] << 8) + buf[13]);
131 bmpx8x_values.calib.b2 = ((buf[14] << 8) + buf[15]);
132 bmpx8x_values.calib.mb = ((buf[16] << 8) + buf[17]);
133 bmpx8x_values.calib.mc = ((buf[18] << 8) + buf[19]);
134 bmpx8x_values.calib.md = ((buf[20] << 8) + buf[21]);
136 return BMPx8x_SUCCESS;
139 PRINTF(
"BMPx8x: failed to read calibration\n");
144bmpx8x_read_uncompensated_pressure(int32_t *pressure)
150 buf[0] = BMPx8x_CTRL_REG;
152 switch(bmpx8x_values.oversampling_mode) {
153 case BMPx8x_MODE_ULTRA_LOW_POWER:
154 buf[1] = BMPx8x_CTRL_REG_PRESS_4_5MS;
155 delay = BMPx8x_DELAY_4_5MS;
157 case BMPx8x_MODE_STANDARD:
158 buf[1] = BMPx8x_CTRL_REG_PRESS_7_5MS;
159 delay = BMPx8x_DELAY_7_5MS;
161 case BMPx8x_MODE_HIGH_RES:
162 buf[1] = BMPx8x_CTRL_REG_PRESS_13_5MS;
163 delay = BMPx8x_DELAY_13_5MS;
165 case BMPx8x_MODE_ULTRA_HIGH_RES:
166 buf[1] = BMPx8x_CTRL_REG_PRESS_25_5MS;
167 delay = BMPx8x_DELAY_25_5MS;
173 if(bmpx8x_write_reg(buf, 2) == BMPx8x_SUCCESS) {
175 if(bmpx8x_read_reg(BMPx8x_DATA_MSB, buf, 3) == BMPx8x_SUCCESS) {
176 upres = (buf[0] << 16) + (buf[1] << 8) + buf[2];
177 *pressure = (upres >> (8 - bmpx8x_values.oversampling_mode));
178 return BMPx8x_SUCCESS;
185bmpx8x_read_uncompensated_temperature(int32_t *temp)
188 buf[0] = BMPx8x_CTRL_REG;
189 buf[1] = BMPx8x_CTRL_REG_TEMP;
191 if(bmpx8x_write_reg(buf, 2) == BMPx8x_SUCCESS) {
193 if(bmpx8x_read_reg(BMPx8x_DATA_MSB, buf, 2) == BMPx8x_SUCCESS) {
194 *temp = (int32_t)((buf[0] << 8) + buf[1]);
195 return BMPx8x_SUCCESS;
202bmpx8x_read_temperature(int16_t *temp)
207 if(bmpx8x_read_uncompensated_temperature(&ut) == BMPx8x_ERROR) {
211 x1 = ((int32_t)ut - (int32_t)bmpx8x_values.calib.ac6)
212 * (int32_t)bmpx8x_values.calib.ac5 >> 15;
213 x2 = ((int32_t)bmpx8x_values.calib.mc << 11) / (x1 + bmpx8x_values.calib.md);
214 bmpx8x_values.b5 = x1 + x2;
215 *temp = (int16_t)((bmpx8x_values.b5 + 8) >> 4);
216 return BMPx8x_SUCCESS;
220bmpx8x_read_pressure(int32_t *pressure)
224 int32_t x1, x2, b6, x3, b3, p;
227 if(bmpx8x_read_uncompensated_pressure(&up) == BMPx8x_ERROR) {
231 if(bmpx8x_read_uncompensated_temperature(&ut) == BMPx8x_ERROR) {
235 b6 = bmpx8x_values.b5 - 4000;
236 x1 = (bmpx8x_values.calib.b2 * (b6 * b6 >> 12)) >> 11;
237 x2 = bmpx8x_values.calib.ac2 * b6 >> 11;
239 b3 = ((((int32_t)bmpx8x_values.calib.ac1) * 4 + x3) + 2) >> 2;
241 x1 = (bmpx8x_values.calib.ac3 * b6) >> 13;
242 x2 = (bmpx8x_values.calib.b1 * ((b6 * b6) >> 12)) >> 16;
243 x3 = ((x1 + x2) + 2) >> 2;
244 b4 = (bmpx8x_values.calib.ac4 * ((uint32_t)(x3 + 32768))) >> 15;
245 b7 = ((uint32_t)up - b3) * 50000;
247 if(b7 < 0x80000000) {
253 x1 = (p >> 8) * (p >> 8);
254 x1 = (x1 * 3038) >> 16;
255 x2 = (-7357 * p) >> 16;
256 *pressure = (p + ((x1 + x2 + 3791) >> 4));
259 return BMPx8x_SUCCESS;
263configure(
int type,
int value)
265 if((type != BMPx8x_ACTIVE) && (type != BMPx8x_OVERSAMPLING)) {
266 PRINTF(
"BMPx8x: invalid start value\n");
270 if(type == BMPx8x_ACTIVE) {
272 i2c_init(I2C_SDA_PORT, I2C_SDA_PIN, I2C_SCL_PORT, I2C_SCL_PIN,
273 I2C_SCL_NORMAL_BUS_SPEED);
276 if(bmpx8x_read_calib() != BMPx8x_ERROR) {
277 PRINTF(
"BMPx8x: sensor started\n");
279 bmpx8x_values.oversampling_mode = BMPx8x_MODE_ULTRA_LOW_POWER;
280 return BMPx8x_SUCCESS;
283 PRINTF(
"BMPx8x: failed to enable\n");
287 return BMPx8x_SUCCESS;
289 }
else if(type == BMPx8x_OVERSAMPLING) {
290 if((value < BMPx8x_MODE_ULTRA_LOW_POWER) ||
291 (value > BMPx8x_MODE_ULTRA_HIGH_RES)) {
292 PRINTF(
"BMPx8x: invalid oversampling value\n");
295 bmpx8x_values.oversampling_mode = value;
296 return BMPx8x_SUCCESS;
314bmpx8x_read_sensor(int32_t *value, uint8_t type)
319 if(bmpx8x_read_temperature(&temp) != BMPx8x_SUCCESS) {
324 case BMPx8x_READ_PRESSURE:
325 return bmpx8x_read_pressure(value);
327 case BMPx8x_READ_TEMP:
328 *value = (int16_t) temp;
329 return BMPx8x_SUCCESS;
341 PRINTF(
"BMPx8x: sensor not started\n");
345 if((type != BMPx8x_READ_PRESSURE) && (type != BMPx8x_READ_TEMP)) {
346 PRINTF(
"BMPx8x: invalid read value\n");
350 if(bmpx8x_read_sensor(&value, type) == BMPx8x_SUCCESS) {
354 PRINTF(
"BMPx8x: fail to read\n");
358SENSORS_SENSOR(bmpx8x, BMPx8x_SENSOR, value, configure, status);
Header file for the external BMP085/BMP180 Sensor Driver.
Header file with register and macro declarations for the cc2538 GPIO module.
void clock_delay_usec(uint16_t dt)
Delay a given number of microseconds.
uint8_t i2c_burst_send(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to send multiple bytes to a slave.
uint8_t i2c_master_busy(void)
Return the busy state of I2C module.
void i2c_init(uint8_t port_sda, uint8_t pin_sda, uint8_t port_scl, uint8_t pin_scl, uint32_t bus_speed)
Initialize the I2C peripheral and pins.
void i2c_master_enable(void)
Enable master I2C module.
uint8_t i2c_burst_receive(uint8_t slave_addr, uint8_t *data, uint8_t len)
Perform all operations to receive multiple bytes from a slave.
uint8_t i2c_single_send(uint8_t slave_addr, uint8_t data)
Perform all operations to send a byte to a slave.
#define BMPx8x_CALIB_TABLE_SIZE
size in bytes
Implementation of a generic module controlling Zoul sensors.