40#include "lib/sensors.h"
53#define PRINTF(...) printf(__VA_ARGS__)
58#define BMP280_I2C_ADDRESS 0x77
61#define ADDR_CALIB 0x88
62#define ADDR_PROD_ID 0xD0
63#define ADDR_RESET 0xE0
64#define ADDR_STATUS 0xF3
65#define ADDR_CTRL_MEAS 0xF4
66#define ADDR_CONFIG 0xF5
67#define ADDR_PRESS_MSB 0xF7
68#define ADDR_PRESS_LSB 0xF8
69#define ADDR_PRESS_XLSB 0xF9
70#define ADDR_TEMP_MSB 0xFA
71#define ADDR_TEMP_LSB 0xFB
72#define ADDR_TEMP_XLSB 0xFC
75#define VAL_PROD_ID 0x58
77#define VAL_STATUS 0x00
78#define VAL_CTRL_MEAS 0x00
79#define VAL_CONFIG 0x00
80#define VAL_PRESS_MSB 0x80
81#define VAL_PRESS_LSB 0x00
82#define VAL_TEMP_MSB 0x80
83#define VAL_TEMP_LSB 0x00
86#define VAL_RESET_EXECUTE 0xB6
87#define VAL_CTRL_MEAS_TEST 0x55
90#define MEAS_DATA_SIZE 6
93#define RES_ULTRA_LOW_POWER 1
94#define RES_LOW_POWER 2
97#define RES_ULTRA_HIGH 6
104#define OSRST(v) ((v) << 5)
105#define OSRSP(v) ((v) << 2)
107typedef struct bmp_280_calibration {
121} bmp_280_calibration_t;
123static bmp_280_calibration_t calibration_data;
125#define SENSOR_STATUS_DISABLED 0
126#define SENSOR_STATUS_INITIALISED 1
127#define SENSOR_STATUS_NOT_READY 2
128#define SENSOR_STATUS_READY 3
130static int enabled = SENSOR_STATUS_DISABLED;
133#define SENSOR_DATA_BUF_SIZE 6
135static uint8_t sensor_value[SENSOR_DATA_BUF_SIZE];
138#define SENSOR_STARTUP_DELAY 3
140static struct ctimer startup_timer;
145 enabled = SENSOR_STATUS_READY;
168 sizeof(calibration_data));
171 val = VAL_RESET_EXECUTE;
188 val = PM_FORCED | OSRSP(1) | OSRST(1);
225convert(uint8_t *data, int32_t *temp, uint32_t *press)
227 int32_t utemp, upress;
228 bmp_280_calibration_t *p = &calibration_data;
235 upress = (int32_t)((((uint32_t)(data[0])) << 12)
236 | (((uint32_t)(data[1])) << 4) | ((uint32_t)data[2] >> 4));
239 utemp = (int32_t)((((uint32_t)(data[3])) << 12) | (((uint32_t)(data[4])) << 4)
240 | ((uint32_t)data[5] >> 4));
243 v_x1_u32r = ((((utemp >> 3) - ((int32_t)p->dig_t1 << 1)))
244 * ((int32_t)p->dig_t2)) >> 11;
245 v_x2_u32r = (((((utemp >> 4) - ((int32_t)p->dig_t1))
246 * ((utemp >> 4) - ((int32_t)p->dig_t1))) >> 12)
247 * ((int32_t)p->dig_t3))
249 p->t_fine = v_x1_u32r + v_x2_u32r;
250 temperature = (p->t_fine * 5 + 128) >> 8;
254 v_x1_u32r = (((int32_t)p->t_fine) >> 1) - (int32_t)64000;
255 v_x2_u32r = (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 11)
256 * ((int32_t)p->dig_p6);
257 v_x2_u32r = v_x2_u32r + ((v_x1_u32r * ((int32_t)p->dig_p5)) << 1);
258 v_x2_u32r = (v_x2_u32r >> 2) + (((int32_t)p->dig_p4) << 16);
260 (((p->dig_p3 * (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 13)) >> 3)
261 + ((((int32_t)p->dig_p2) * v_x1_u32r) >> 1)) >> 18;
262 v_x1_u32r = ((((32768 + v_x1_u32r)) * ((int32_t)p->dig_p1)) >> 15);
268 pressure = (((uint32_t)(((int32_t)1048576) - upress) - (v_x2_u32r >> 12)))
270 if(pressure < 0x80000000) {
271 pressure = (pressure << 1) / ((uint32_t)v_x1_u32r);
273 pressure = (pressure / (uint32_t)v_x1_u32r) * 2;
276 v_x1_u32r = (((int32_t)p->dig_p9)
277 * ((int32_t)(((pressure >> 3) * (pressure >> 3)) >> 13))) >> 12;
278 v_x2_u32r = (((int32_t)(pressure >> 2)) * ((int32_t)p->dig_p8)) >> 13;
279 pressure = (uint32_t)((int32_t)pressure
280 + ((v_x1_u32r + v_x2_u32r + p->dig_p7) >> 4));
297 if(enabled != SENSOR_STATUS_READY) {
298 PRINTF(
"Sensor disabled or starting up (%d)\n", enabled);
299 return CC26XX_SENSOR_READING_ERROR;
302 if((type != BMP_280_SENSOR_TYPE_TEMP) && type != BMP_280_SENSOR_TYPE_PRESS) {
303 PRINTF(
"Invalid type\n");
304 return CC26XX_SENSOR_READING_ERROR;
306 memset(sensor_value, 0, SENSOR_DATA_BUF_SIZE);
311 return CC26XX_SENSOR_READING_ERROR;
314 PRINTF(
"val: %02x%02x%02x %02x%02x%02x\n",
315 sensor_value[0], sensor_value[1], sensor_value[2],
316 sensor_value[3], sensor_value[4], sensor_value[5]);
318 convert(sensor_value, &temp, &pres);
320 if(type == BMP_280_SENSOR_TYPE_TEMP) {
322 }
else if(type == BMP_280_SENSOR_TYPE_PRESS) {
343 case SENSORS_HW_INIT:
344 enabled = SENSOR_STATUS_INITIALISED;
350 if(enabled == SENSOR_STATUS_DISABLED) {
351 return SENSOR_STATUS_DISABLED;
356 enabled = SENSOR_STATUS_NOT_READY;
360 enabled = SENSOR_STATUS_INITIALISED;
385 return SENSOR_STATUS_DISABLED;
Header file for the Sensortag I2C Driver.
Header file for the callback timer.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
static void init(void)
Initalise the sensor.
const struct sensors_sensor bmp_280_sensor
Exports a global symbol to be used by the sensor API.
static int value(int type)
Returns a reading from the sensor.
static bool read_data(uint8_t *data)
Read temperature and pressure data.
static int status(int type)
Returns the status of the sensor.
static void enable_sensor(bool enable)
Enable/disable measurements.
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 int configure(int type, int enable)
Configuration function for the BMP280 sensor.
void board_i2c_select(uint8_t new_interface, uint8_t address)
Select an I2C slave.
void sensor_common_set_error_data(uint8_t *buf, uint8_t len)
Fill a result buffer with dummy error data.
bool sensor_common_read_reg(uint8_t addr, uint8_t *buf, uint8_t len)
Reads a sensor's register over I2C.
bool sensor_common_write_reg(uint8_t addr, uint8_t *buf, uint8_t len)
Write to a sensor's register over I2C.
static void notify_ready(void *unused)
Callback when sensor is ready to read data from.
Header file for the Sensortag Common sensor utilities.
Header file for the Sensortag BMP280 Altimeter / Pressure Sensor.
Header file with macros which rename TI CC26xxware functions.