41 #include "lib/sensors.h" 50 #include <ti/drivers/I2C.h> 58 #define PRINTF(...) printf(__VA_ARGS__) 68 #if BOARD_SENSORS_ENABLE 70 #ifndef Board_BMP280_ADDR 71 #error "Board file doesn't define I2C address Board_BMP280_ADDR" 74 #define BMP280_I2C_ADDRESS Board_BMP280_ADDR 77 #define ADDR_CALIB 0x88 78 #define ADDR_PROD_ID 0xD0 79 #define ADDR_RESET 0xE0 80 #define ADDR_STATUS 0xF3 81 #define ADDR_CTRL_MEAS 0xF4 82 #define ADDR_CONFIG 0xF5 83 #define ADDR_PRESS_MSB 0xF7 84 #define ADDR_PRESS_LSB 0xF8 85 #define ADDR_PRESS_XLSB 0xF9 86 #define ADDR_TEMP_MSB 0xFA 87 #define ADDR_TEMP_LSB 0xFB 88 #define ADDR_TEMP_XLSB 0xFC 91 #define VAL_PROD_ID 0x58 92 #define VAL_RESET 0x00 93 #define VAL_STATUS 0x00 94 #define VAL_CTRL_MEAS 0x00 95 #define VAL_CONFIG 0x00 96 #define VAL_PRESS_MSB 0x80 97 #define VAL_PRESS_LSB 0x00 98 #define VAL_TEMP_MSB 0x80 99 #define VAL_TEMP_LSB 0x00 102 #define VAL_RESET_EXECUTE 0xB6 103 #define VAL_CTRL_MEAS_TEST 0x55 106 #define SENSOR_DATA_BUF_SIZE 6 109 #define RES_ULTRA_LOW_POWER 1 110 #define RES_LOW_POWER 2 111 #define RES_STANDARD 3 113 #define RES_ULTRA_HIGH 6 120 #define OSRST(v) ((v) << 5) 121 #define OSRSP(v) ((v) << 2) 136 } BMP_280_Calibration;
138 static BMP_280_Calibration calib_data;
140 static I2C_Handle i2c_handle;
143 SENSOR_STATUS_DISABLED,
144 SENSOR_STATUS_INITIALISED,
145 SENSOR_STATUS_NOT_READY,
149 static volatile SENSOR_STATUS sensor_status = SENSOR_STATUS_DISABLED;
152 #define SENSOR_STARTUP_DELAY 3 154 static struct ctimer startup_timer;
161 sensor_status = SENSOR_STATUS_READY;
175 bool rv_write_read, rv_write;
180 sensor_status = SENSOR_STATUS_DISABLED;
184 uint8_t reset_data[] = { ADDR_RESET, VAL_RESET_EXECUTE };
186 uint8_t calib_reg = ADDR_CALIB;
189 &calib_reg,
sizeof(calib_reg),
190 &calib_data,
sizeof(calib_data));
192 rv_write =
i2c_arch_write(i2c_handle, BMP280_I2C_ADDRESS, reset_data,
197 return rv_write_read && rv_write;
213 uint8_t val = (enable)
214 ? PM_FORCED | OSRSP(1) | OSRST(1)
217 uint8_t ctrl_meas_data[] = { ADDR_CTRL_MEAS, val };
222 sensor_status = SENSOR_STATUS_DISABLED;
226 rv =
i2c_arch_write(i2c_handle, BMP280_I2C_ADDRESS, &ctrl_meas_data,
227 sizeof(ctrl_meas_data));
247 uint8_t press_msb_reg = ADDR_PRESS_MSB;
252 sensor_status = SENSOR_STATUS_DISABLED;
257 sizeof(press_msb_reg), data, count);
274 convert(uint8_t *data, int32_t *temp, uint32_t *press)
276 BMP_280_Calibration *p = &calib_data;
279 const int32_t upress = (int32_t)(
280 (((uint32_t)data[0]) << 12) |
281 (((uint32_t)data[1]) << 4) |
282 (((uint32_t)data[2]) >> 4)
285 const int32_t utemp = (int32_t)(
286 (((uint32_t)data[3]) << 12) |
287 (((uint32_t)data[4]) << 4) |
288 (((uint32_t)data[5]) >> 4)
292 int32_t v_x1_u32r = (((utemp >> 3) - ((int32_t)p->dig_t1 << 1)) * (int32_t)p->dig_t2) >> 11;
293 int32_t v_x2_u32r = (((((utemp >> 4) - (int32_t)p->dig_t1) * ((utemp >> 4) - (int32_t)p->dig_t1)) >> 12) * (int32_t)p->dig_t3) >> 14;
295 const uint32_t t_fine = v_x1_u32r + v_x2_u32r;
296 const int32_t temperature = (t_fine * 5 + 128) >> 8;
300 v_x1_u32r = ((int32_t)t_fine >> 1) - (int32_t)64000;
301 v_x2_u32r = (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 11) * (int32_t)p->dig_p6;
302 v_x2_u32r = ((v_x1_u32r * (int32_t)p->dig_p5) << 1) + v_x2_u32r;
303 v_x2_u32r = (v_x2_u32r >> 2) + ((int32_t)p->dig_p4 << 16);
304 v_x1_u32r = ((((((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 13) * p->dig_p3) >> 3) + (((int32_t)p->dig_p2 * v_x1_u32r) >> 1)) >> 18;
305 v_x1_u32r = ((32768 + v_x1_u32r) * (int32_t)p->dig_p1) >> 15;
313 uint32_t pressure = (((uint32_t)((int32_t)1048576 - upress)) - (v_x2_u32r >> 12)) * 3125;
314 if((int32_t)pressure < 0) {
315 pressure = (pressure << 1) / (uint32_t)v_x1_u32r;
317 pressure = (pressure / (uint32_t)v_x1_u32r) * 2;
320 v_x1_u32r = (((int32_t)(((pressure >> 3) * (pressure >> 3)) >> 13)) * (int32_t)p->dig_p9) >> 12;
321 v_x2_u32r = ((int32_t)(pressure >> 2) * (int32_t)p->dig_p8) >> 13;
322 pressure = (uint32_t)(((v_x1_u32r + v_x2_u32r + p->dig_p7) >> 4) + (int32_t)pressure);
341 uint8_t sensor_value[SENSOR_DATA_BUF_SIZE];
343 if(sensor_status != SENSOR_STATUS_READY) {
344 PRINTF(
"Sensor disabled or starting up (%d)\n", sensor_status);
345 return BMP_280_READING_ERROR;
349 case BMP_280_SENSOR_TYPE_TEMP:
350 case BMP_280_SENSOR_TYPE_PRESS:
351 memset(sensor_value, 0, SENSOR_DATA_BUF_SIZE);
352 if(!
read_data(sensor_value, SENSOR_DATA_BUF_SIZE)) {
353 return BMP_280_READING_ERROR;
356 PRINTF(
"val: %02x%02x%02x %02x%02x%02x\n",
357 sensor_value[0], sensor_value[1], sensor_value[2],
358 sensor_value[3], sensor_value[4], sensor_value[5]);
360 convert(sensor_value, &temp, &pres);
362 if(type == BMP_280_SENSOR_TYPE_TEMP) {
364 }
else if(type == BMP_280_SENSOR_TYPE_PRESS) {
371 PRINTF(
"Invalid BMP 208 Sensor Type\n");
372 return BMP_280_READING_ERROR;
388 case SENSORS_HW_INIT:
391 sensor_status = SENSOR_STATUS_INITIALISED;
393 sensor_status = SENSOR_STATUS_DISABLED;
399 if(sensor_status == SENSOR_STATUS_DISABLED) {
405 sensor_status = SENSOR_STATUS_NOT_READY;
409 sensor_status = SENSOR_STATUS_INITIALISED;
416 return sensor_status;
430 return sensor_status;
432 return SENSOR_STATUS_DISABLED;
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
static int status(int type)
Returns the status of the sensor.
static volatile uint64_t count
Num.
static void convert(uint8_t *data, int32_t *temp, uint32_t *press)
Convert raw data to values in degrees C (temp) and Pascal (pressure).
static bool enable_sensor(bool enable)
Enable/disable measurements.
static bool init(void)
Initalise the sensor.
Header file for the Sensortag BMP280 Altimeter / Pressure Sensor.
static int value(int type)
Returns a reading from the sensor.
static bool read_data(uint8_t *data, size_t count)
Read temperature and pressure data.
Implementation of the I2C HAL driver for CC13xx/CC26xx.
bool i2c_arch_write_read(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *wbuf, size_t wcount, void *rbuf, size_t rcount)
Setup and peform an I2C transaction.
static bool i2c_arch_write(I2C_Handle i2c_handle, uint_least8_t slave_addr, void *wbuf, size_t wcount)
Perform a write-only I2C transaction.
Header file for the callback timer
static int configure(int type, int enable)
Configuration function for the BMP280 sensor.
void i2c_arch_release(I2C_Handle i2c_handle)
Release the I2C Peripheral for other modules to use.
I2C_Handle i2c_arch_acquire(uint_least8_t index)
Open and lock the I2C Peripheral for use.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
static void notify_ready(void *unused)
Callback when sensor is ready to read data from.
Header file with definitions related to the sensors on the Sensortags.
const struct sensors_sensor bmp_280_sensor
Exports a global symbol to be used by the sensor API.