39 #include "contiki-conf.h"
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
76 #define VAL_RESET 0x00
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
91 #define CALIB_DATA_SIZE 24
94 #define RES_ULTRA_LOW_POWER 1
95 #define RES_LOW_POWER 2
96 #define RES_STANDARD 3
98 #define RES_ULTRA_HIGH 6
105 #define OSRST(v) ((v) << 5)
106 #define OSRSP(v) ((v) << 2)
108 typedef struct bmp_280_calibration {
122 } bmp_280_calibration_t;
124 static uint8_t calibration_data[CALIB_DATA_SIZE];
126 #define SENSOR_STATUS_DISABLED 0
127 #define SENSOR_STATUS_INITIALISED 1
128 #define SENSOR_STATUS_NOT_READY 2
129 #define SENSOR_STATUS_READY 3
131 static int enabled = SENSOR_STATUS_DISABLED;
134 #define SENSOR_DATA_BUF_SIZE 6
136 static uint8_t sensor_value[SENSOR_DATA_BUF_SIZE];
139 #define SENSOR_STARTUP_DELAY 3
141 static struct ctimer startup_timer;
144 notify_ready(
void *not_used)
146 enabled = SENSOR_STATUS_READY;
147 sensors_changed(&bmp_280_sensor);
171 val = VAL_RESET_EXECUTE;
190 val = PM_FORCED | OSRSP(1) | OSRST(1);
227 convert(uint8_t *data, int32_t *temp, uint32_t *press)
229 int32_t utemp, upress;
230 bmp_280_calibration_t *p = (bmp_280_calibration_t *)calibration_data;
237 upress = (int32_t)((((uint32_t)(data[0])) << 12)
238 | (((uint32_t)(data[1])) << 4) | ((uint32_t)data[2] >> 4));
241 utemp = (int32_t)((((uint32_t)(data[3])) << 12) | (((uint32_t)(data[4])) << 4)
242 | ((uint32_t)data[5] >> 4));
245 v_x1_u32r = ((((utemp >> 3) - ((int32_t)p->dig_t1 << 1)))
246 * ((int32_t)p->dig_t2)) >> 11;
247 v_x2_u32r = (((((utemp >> 4) - ((int32_t)p->dig_t1))
248 * ((utemp >> 4) - ((int32_t)p->dig_t1))) >> 12)
249 * ((int32_t)p->dig_t3))
251 p->t_fine = v_x1_u32r + v_x2_u32r;
252 temperature = (p->t_fine * 5 + 128) >> 8;
256 v_x1_u32r = (((int32_t)p->t_fine) >> 1) - (int32_t)64000;
257 v_x2_u32r = (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 11)
258 * ((int32_t)p->dig_p6);
259 v_x2_u32r = v_x2_u32r + ((v_x1_u32r * ((int32_t)p->dig_p5)) << 1);
260 v_x2_u32r = (v_x2_u32r >> 2) + (((int32_t)p->dig_p4) << 16);
262 (((p->dig_p3 * (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 13)) >> 3)
263 + ((((int32_t)p->dig_p2) * v_x1_u32r) >> 1)) >> 18;
264 v_x1_u32r = ((((32768 + v_x1_u32r)) * ((int32_t)p->dig_p1)) >> 15);
270 pressure = (((uint32_t)(((int32_t)1048576) - upress) - (v_x2_u32r >> 12)))
272 if(pressure < 0x80000000) {
273 pressure = (pressure << 1) / ((uint32_t)v_x1_u32r);
275 pressure = (pressure / (uint32_t)v_x1_u32r) * 2;
278 v_x1_u32r = (((int32_t)p->dig_p9)
279 * ((int32_t)(((pressure >> 3) * (pressure >> 3)) >> 13))) >> 12;
280 v_x2_u32r = (((int32_t)(pressure >> 2)) * ((int32_t)p->dig_p8)) >> 13;
281 pressure = (uint32_t)((int32_t)pressure
282 + ((v_x1_u32r + v_x2_u32r + p->dig_p7) >> 4));
299 if(enabled != SENSOR_STATUS_READY) {
300 PRINTF(
"Sensor disabled or starting up (%d)\n", enabled);
301 return CC26XX_SENSOR_READING_ERROR;
304 if((type != BMP_280_SENSOR_TYPE_TEMP) && type != BMP_280_SENSOR_TYPE_PRESS) {
305 PRINTF(
"Invalid type\n");
306 return CC26XX_SENSOR_READING_ERROR;
308 memset(sensor_value, 0, SENSOR_DATA_BUF_SIZE);
313 return CC26XX_SENSOR_READING_ERROR;
316 PRINTF(
"val: %02x%02x%02x %02x%02x%02x\n",
317 sensor_value[0], sensor_value[1], sensor_value[2],
318 sensor_value[3], sensor_value[4], sensor_value[5]);
320 convert(sensor_value, &temp, &pres);
322 if(type == BMP_280_SENSOR_TYPE_TEMP) {
324 }
else if(type == BMP_280_SENSOR_TYPE_PRESS) {
345 case SENSORS_HW_INIT:
346 enabled = SENSOR_STATUS_INITIALISED;
352 if(enabled == SENSOR_STATUS_DISABLED) {
353 return SENSOR_STATUS_DISABLED;
357 ctimer_set(&startup_timer, SENSOR_STARTUP_DELAY, notify_ready,
NULL);
358 enabled = SENSOR_STATUS_NOT_READY;
362 enabled = SENSOR_STATUS_INITIALISED;
387 return SENSOR_STATUS_DISABLED;
static bool read_data(uint8_t *data)
Read temperature and pressure data.
Header file with macros which rename TI CC26xxware functions.
void sensor_common_set_error_data(uint8_t *buf, uint8_t len)
Fill a result buffer with dummy error data.
static void enable_sensor(bool enable)
Enable/disable measurements.
static void init(void)
Initalise the sensor.
void board_i2c_select(uint8_t new_interface, uint8_t address)
Select an I2C slave.
static int status(int type)
Returns the status of the sensor.
Header file for the callback timer
#define NULL
The null pointer.
static int value(int type)
Returns a reading from the sensor.
Header file for the Sensortag-CC26xx Common sensor utilities.
Header file for the Sensortag-CC26xx I2C Driver.
static int configure(int type, int enable)
Configuration function for the BMP280 sensor.
Header file for the Sensortag-CC26xx BMP280 Altimeter / Pressure Sensor.
bool sensor_common_read_reg(uint8_t addr, uint8_t *buf, uint8_t len)
Reads a sensor's register over I2C.
void ctimer_set(struct ctimer *c, clock_time_t t, void(*f)(void *), void *ptr)
Set a callback timer.
bool sensor_common_write_reg(uint8_t addr, uint8_t *buf, uint8_t len)
Write to a sensor's register over I2C.
void ctimer_stop(struct ctimer *c)
Stop a pending callback timer.
static void convert(uint8_t *data, int32_t *temp, uint32_t *press)
Convert raw data to values in degrees C (temp) and Pascal (pressure)