2018-05-28 11:17:01 +00:00
|
|
|
/*
|
2018-07-16 14:39:19 +00:00
|
|
|
* Copyright (c) 2018, Texas Instruments Incorporated - http://www.ti.com/
|
2018-05-28 11:17:01 +00:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the copyright holder nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived
|
|
|
|
* from this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
|
|
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
|
|
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
|
|
* OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
/**
|
2018-07-16 14:39:19 +00:00
|
|
|
* \addtogroup sensortag-bmp-sensor
|
2018-05-28 11:17:01 +00:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
2018-07-16 14:39:19 +00:00
|
|
|
* Driver for the Sensortag BMP280 Altimeter / Pressure Sensor
|
|
|
|
* \author
|
|
|
|
* Edvard Pettersen <e.pettersen@ti.com>
|
2018-05-28 11:17:01 +00:00
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "lib/sensors.h"
|
|
|
|
#include "sys/ctimer.h"
|
2018-05-31 15:17:07 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-24 15:24:06 +00:00
|
|
|
#include "board-conf.h"
|
|
|
|
#include "bmp-280-sensor.h"
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-05-31 15:17:07 +00:00
|
|
|
#include <Board.h>
|
2018-07-16 14:39:19 +00:00
|
|
|
|
2018-05-31 15:17:07 +00:00
|
|
|
#include <ti/drivers/I2C.h>
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include <stdbool.h>
|
2018-05-28 11:17:01 +00:00
|
|
|
#include <stdint.h>
|
|
|
|
#include <string.h>
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#define DEBUG 0
|
|
|
|
#if DEBUG
|
|
|
|
#define PRINTF(...) printf(__VA_ARGS__)
|
|
|
|
#else
|
|
|
|
#define PRINTF(...)
|
|
|
|
#endif
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-24 15:24:06 +00:00
|
|
|
/*
|
|
|
|
* Disable the entire file if sensors are disabled, as it could potentially
|
|
|
|
* create compile errors with missing defines from either the Board file or
|
|
|
|
* configuration defines.
|
|
|
|
*/
|
|
|
|
#if BOARD_SENSORS_ENABLE
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-05-31 15:17:07 +00:00
|
|
|
#ifndef Board_BMP280_ADDR
|
2018-07-25 16:16:27 +00:00
|
|
|
#error "Board file doesn't define I2C address Board_BMP280_ADDR"
|
2018-05-31 15:17:07 +00:00
|
|
|
#endif
|
|
|
|
/* Sensor I2C address */
|
|
|
|
#define BMP280_I2C_ADDRESS Board_BMP280_ADDR
|
2018-05-28 11:17:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Registers */
|
|
|
|
#define ADDR_CALIB 0x88
|
|
|
|
#define ADDR_PROD_ID 0xD0
|
|
|
|
#define ADDR_RESET 0xE0
|
|
|
|
#define ADDR_STATUS 0xF3
|
|
|
|
#define ADDR_CTRL_MEAS 0xF4
|
|
|
|
#define ADDR_CONFIG 0xF5
|
|
|
|
#define ADDR_PRESS_MSB 0xF7
|
|
|
|
#define ADDR_PRESS_LSB 0xF8
|
|
|
|
#define ADDR_PRESS_XLSB 0xF9
|
|
|
|
#define ADDR_TEMP_MSB 0xFA
|
|
|
|
#define ADDR_TEMP_LSB 0xFB
|
|
|
|
#define ADDR_TEMP_XLSB 0xFC
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Reset values */
|
|
|
|
#define VAL_PROD_ID 0x58
|
|
|
|
#define VAL_RESET 0x00
|
|
|
|
#define VAL_STATUS 0x00
|
|
|
|
#define VAL_CTRL_MEAS 0x00
|
|
|
|
#define VAL_CONFIG 0x00
|
|
|
|
#define VAL_PRESS_MSB 0x80
|
|
|
|
#define VAL_PRESS_LSB 0x00
|
|
|
|
#define VAL_TEMP_MSB 0x80
|
|
|
|
#define VAL_TEMP_LSB 0x00
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Test values */
|
|
|
|
#define VAL_RESET_EXECUTE 0xB6
|
|
|
|
#define VAL_CTRL_MEAS_TEST 0x55
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Misc. */
|
|
|
|
#define MEAS_DATA_SIZE 6
|
|
|
|
#define CALIB_DATA_SIZE 24
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#define RES_OFF 0
|
|
|
|
#define RES_ULTRA_LOW_POWER 1
|
|
|
|
#define RES_LOW_POWER 2
|
|
|
|
#define RES_STANDARD 3
|
|
|
|
#define RES_HIGH 5
|
|
|
|
#define RES_ULTRA_HIGH 6
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Bit fields in CTRL_MEAS register */
|
|
|
|
#define PM_OFF 0
|
|
|
|
#define PM_FORCED 1
|
|
|
|
#define PM_NORMAL 3
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#define OSRST(v) ((v) << 5)
|
|
|
|
#define OSRSP(v) ((v) << 2)
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-05-31 15:17:07 +00:00
|
|
|
typedef struct {
|
2018-05-28 11:17:01 +00:00
|
|
|
uint16_t dig_t1;
|
2018-07-25 16:16:27 +00:00
|
|
|
int16_t dig_t2;
|
|
|
|
int16_t dig_t3;
|
2018-05-28 11:17:01 +00:00
|
|
|
uint16_t dig_p1;
|
2018-07-25 16:16:27 +00:00
|
|
|
int16_t dig_p2;
|
|
|
|
int16_t dig_p3;
|
|
|
|
int16_t dig_p4;
|
|
|
|
int16_t dig_p5;
|
|
|
|
int16_t dig_p6;
|
|
|
|
int16_t dig_p7;
|
|
|
|
int16_t dig_p8;
|
|
|
|
int16_t dig_p9;
|
2018-05-31 15:17:07 +00:00
|
|
|
} BMP_280_Calibration;
|
2018-05-28 11:17:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-05-31 15:17:07 +00:00
|
|
|
static BMP_280_Calibration calib_data;
|
2018-05-28 11:17:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-16 14:39:19 +00:00
|
|
|
static I2C_Handle i2c_handle;
|
2018-05-28 11:17:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-05-31 15:17:07 +00:00
|
|
|
typedef enum {
|
|
|
|
SENSOR_STATUS_DISABLED,
|
|
|
|
SENSOR_STATUS_INITIALISED,
|
|
|
|
SENSOR_STATUS_NOT_READY,
|
|
|
|
SENSOR_STATUS_READY
|
|
|
|
} SENSOR_STATUS;
|
2018-05-28 11:17:01 +00:00
|
|
|
|
2018-05-31 15:17:07 +00:00
|
|
|
static volatile SENSOR_STATUS sensor_status = SENSOR_STATUS_DISABLED;
|
2018-05-28 11:17:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/* Wait SENSOR_STARTUP_DELAY clock ticks for the sensor to be ready - ~80ms */
|
|
|
|
#define SENSOR_STARTUP_DELAY 3
|
|
|
|
|
|
|
|
static struct ctimer startup_timer;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2018-05-31 15:17:07 +00:00
|
|
|
notify_ready(void *unused)
|
2018-05-28 11:17:01 +00:00
|
|
|
{
|
2018-05-31 15:17:07 +00:00
|
|
|
(void)unused;
|
|
|
|
|
|
|
|
sensor_status = SENSOR_STATUS_READY;
|
2018-05-28 11:17:01 +00:00
|
|
|
sensors_changed(&bmp_280_sensor);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-05-31 15:17:07 +00:00
|
|
|
static bool
|
|
|
|
i2c_write_read(void *writeBuf, size_t writeCount, void *readBuf, size_t readCount)
|
2018-05-28 11:17:01 +00:00
|
|
|
{
|
2018-05-31 15:17:07 +00:00
|
|
|
I2C_Transaction i2cTransaction = {
|
|
|
|
.writeBuf = writeBuf,
|
|
|
|
.writeCount = writeCount,
|
|
|
|
.readBuf = readBuf,
|
|
|
|
.readCount = readCount,
|
|
|
|
.slaveAddress = BMP280_I2C_ADDRESS,
|
|
|
|
};
|
|
|
|
|
2018-07-16 14:39:19 +00:00
|
|
|
return I2C_transfer(i2c_handle, &i2cTransaction);
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
2018-05-31 15:17:07 +00:00
|
|
|
#define i2c_write(writeBuf, writeCount) i2c_write_read(writeBuf, writeCount, NULL, 0)
|
|
|
|
#define i2c_read(readBuf, readCount) i2c_write_read(NULL, 0, readBuf, readCount)
|
2018-05-28 11:17:01 +00:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2018-07-16 14:39:19 +00:00
|
|
|
* \brief Initalise the sensor.
|
|
|
|
* \return Boolean Value descibing whether initialization were
|
|
|
|
* successful or not.
|
|
|
|
* \retval true Successful initialization
|
|
|
|
* \retval false Error during initialization
|
2018-05-28 11:17:01 +00:00
|
|
|
*/
|
2018-05-31 15:17:07 +00:00
|
|
|
static bool
|
2018-05-28 11:17:01 +00:00
|
|
|
init(void)
|
|
|
|
{
|
2018-07-23 15:49:18 +00:00
|
|
|
if(i2c_handle) {
|
2018-05-31 15:17:07 +00:00
|
|
|
return true;
|
|
|
|
}
|
2018-05-28 11:17:01 +00:00
|
|
|
|
2018-05-31 15:17:07 +00:00
|
|
|
I2C_Params i2cParams;
|
|
|
|
I2C_Params_init(&i2cParams);
|
2018-07-16 14:39:19 +00:00
|
|
|
|
2018-05-31 15:17:07 +00:00
|
|
|
i2cParams.transferMode = I2C_MODE_BLOCKING;
|
|
|
|
i2cParams.bitRate = I2C_400kHz;
|
2018-05-28 11:17:01 +00:00
|
|
|
|
2018-07-16 14:39:19 +00:00
|
|
|
i2c_handle = I2C_open(Board_I2C0, &i2cParams);
|
2018-07-23 15:49:18 +00:00
|
|
|
if(i2c_handle == NULL) {
|
2018-05-31 15:17:07 +00:00
|
|
|
return false;
|
|
|
|
}
|
2018-05-28 11:17:01 +00:00
|
|
|
|
2018-05-31 15:17:07 +00:00
|
|
|
uint8_t reset_data[] = { ADDR_RESET, VAL_RESET_EXECUTE };
|
|
|
|
|
|
|
|
uint8_t calib_reg = ADDR_CALIB;
|
2018-07-16 14:39:19 +00:00
|
|
|
/* Read and store calibration data */
|
2018-05-31 15:17:07 +00:00
|
|
|
return i2c_write_read(&calib_reg, sizeof(calib_reg), &calib_data, sizeof(calib_data))
|
2018-07-25 16:16:27 +00:00
|
|
|
/* then reset the sensor */
|
|
|
|
&& i2c_write(reset_data, sizeof(reset_data));
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2018-07-16 14:39:19 +00:00
|
|
|
* \brief Enable/disable measurements.
|
|
|
|
* \param enable Enable if true; else, disable.
|
|
|
|
* \return Boolean Value descibing whether initialization were
|
|
|
|
* successful or not.
|
|
|
|
* \retval true Successful initialization
|
|
|
|
* \retval false Error during initialization
|
2018-05-28 11:17:01 +00:00
|
|
|
*/
|
2018-05-31 15:17:07 +00:00
|
|
|
static bool
|
2018-05-28 11:17:01 +00:00
|
|
|
enable_sensor(bool enable)
|
|
|
|
{
|
2018-05-31 15:17:07 +00:00
|
|
|
uint8_t val = (enable)
|
|
|
|
? PM_FORCED | OSRSP(1) | OSRST(1)
|
|
|
|
: PM_OFF;
|
2018-05-28 11:17:01 +00:00
|
|
|
|
2018-05-31 15:17:07 +00:00
|
|
|
uint8_t ctrl_meas_data[] = { ADDR_CTRL_MEAS, val };
|
|
|
|
return i2c_write(&ctrl_meas_data, sizeof(ctrl_meas_data));
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2018-07-16 14:39:19 +00:00
|
|
|
* \brief Read temperature and pressure data.
|
|
|
|
* \param data Pointer to a buffer where temperature and pressure will be
|
2018-08-31 10:15:22 +00:00
|
|
|
* written.
|
|
|
|
* \param count Number of byes to read.
|
2018-07-16 14:39:19 +00:00
|
|
|
* \return Boolean Value descibing whether initialization were
|
|
|
|
* successful or not.
|
|
|
|
* \retval true Successful initialization
|
|
|
|
* \retval false Error during initialization
|
2018-05-28 11:17:01 +00:00
|
|
|
*/
|
|
|
|
static bool
|
2018-05-31 15:17:07 +00:00
|
|
|
read_data(uint8_t *data, size_t count)
|
2018-05-28 11:17:01 +00:00
|
|
|
{
|
2018-05-31 15:17:07 +00:00
|
|
|
uint8_t press_msb_reg = ADDR_PRESS_MSB;
|
|
|
|
return i2c_write_read(&press_msb_reg, sizeof(press_msb_reg), data, count);
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2018-07-16 14:39:19 +00:00
|
|
|
* \brief Convert raw data to values in degrees C (temp) and Pascal
|
|
|
|
* (pressure).
|
|
|
|
* \param data Pointer to a buffer that holds raw sensor data.
|
|
|
|
* \param temp Pointer to a variable where the converted temperature will
|
|
|
|
* be written.
|
2018-05-28 11:17:01 +00:00
|
|
|
* \param press Pointer to a variable where the converted pressure will be
|
2018-07-16 14:39:19 +00:00
|
|
|
* written.
|
2018-05-28 11:17:01 +00:00
|
|
|
*/
|
|
|
|
static void
|
|
|
|
convert(uint8_t *data, int32_t *temp, uint32_t *press)
|
|
|
|
{
|
2018-05-31 15:17:07 +00:00
|
|
|
BMP_280_Calibration *p = &calib_data;
|
2018-05-28 11:17:01 +00:00
|
|
|
|
|
|
|
/* Pressure */
|
2018-05-31 15:17:07 +00:00
|
|
|
const int32_t upress = (int32_t)(
|
2018-07-25 16:16:27 +00:00
|
|
|
(((uint32_t)data[0]) << 12) |
|
|
|
|
(((uint32_t)data[1]) << 4) |
|
|
|
|
(((uint32_t)data[2]) >> 4)
|
|
|
|
);
|
2018-05-28 11:17:01 +00:00
|
|
|
/* Temperature */
|
2018-05-31 15:17:07 +00:00
|
|
|
const int32_t utemp = (int32_t)(
|
2018-07-25 16:16:27 +00:00
|
|
|
(((uint32_t)data[3]) << 12) |
|
|
|
|
(((uint32_t)data[4]) << 4) |
|
|
|
|
(((uint32_t)data[5]) >> 4)
|
|
|
|
);
|
2018-05-28 11:17:01 +00:00
|
|
|
|
|
|
|
/* Compensate temperature */
|
2018-07-25 16:16:27 +00:00
|
|
|
int32_t v_x1_u32r = (((utemp >> 3) - ((int32_t)p->dig_t1 << 1)) * (int32_t)p->dig_t2) >> 11;
|
|
|
|
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;
|
2018-05-31 15:17:07 +00:00
|
|
|
|
|
|
|
const uint32_t t_fine = v_x1_u32r + v_x2_u32r;
|
|
|
|
const int32_t temperature = (t_fine * 5 + 128) >> 8;
|
2018-05-28 11:17:01 +00:00
|
|
|
*temp = temperature;
|
|
|
|
|
|
|
|
/* Compensate pressure */
|
2018-05-31 15:17:07 +00:00
|
|
|
v_x1_u32r = ((int32_t)t_fine >> 1) - (int32_t)64000;
|
2018-07-25 16:16:27 +00:00
|
|
|
v_x2_u32r = (((v_x1_u32r >> 2) * (v_x1_u32r >> 2)) >> 11) * (int32_t)p->dig_p6;
|
|
|
|
v_x2_u32r = ((v_x1_u32r * (int32_t)p->dig_p5) << 1) + v_x2_u32r;
|
2018-05-31 15:17:07 +00:00
|
|
|
v_x2_u32r = (v_x2_u32r >> 2) + ((int32_t)p->dig_p4 << 16);
|
2018-07-25 16:16:27 +00:00
|
|
|
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;
|
|
|
|
v_x1_u32r = ((32768 + v_x1_u32r) * (int32_t)p->dig_p1) >> 15;
|
2018-05-28 11:17:01 +00:00
|
|
|
|
2018-07-23 15:49:18 +00:00
|
|
|
if(v_x1_u32r == 0) {
|
2018-05-31 15:17:07 +00:00
|
|
|
/* Avoid exception caused by division by zero */
|
|
|
|
*press = 0;
|
|
|
|
return;
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
uint32_t pressure = (((uint32_t)((int32_t)1048576 - upress)) - (v_x2_u32r >> 12)) * 3125;
|
2018-07-23 15:49:18 +00:00
|
|
|
if((int32_t)pressure < 0) {
|
2018-05-31 15:17:07 +00:00
|
|
|
pressure = (pressure << 1) / (uint32_t)v_x1_u32r;
|
2018-05-28 11:17:01 +00:00
|
|
|
} else {
|
|
|
|
pressure = (pressure / (uint32_t)v_x1_u32r) * 2;
|
|
|
|
}
|
|
|
|
|
2018-07-25 16:16:27 +00:00
|
|
|
v_x1_u32r = (((int32_t)(((pressure >> 3) * (pressure >> 3)) >> 13)) * (int32_t)p->dig_p9) >> 12;
|
|
|
|
v_x2_u32r = ((int32_t)(pressure >> 2) * (int32_t)p->dig_p8) >> 13;
|
|
|
|
pressure = (uint32_t)(((v_x1_u32r + v_x2_u32r + p->dig_p7) >> 4) + (int32_t)pressure);
|
2018-05-28 11:17:01 +00:00
|
|
|
|
|
|
|
*press = pressure;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2018-07-16 14:39:19 +00:00
|
|
|
* \brief Returns a reading from the sensor.
|
|
|
|
* \param type Parameter of type BMP_280_SENSOR_TYPE, choosing between either
|
|
|
|
* measuring temperature or pressure.
|
|
|
|
* \return Sensor data of either Temperature (centi degrees C) or
|
|
|
|
* Pressure (Pascal).
|
2018-05-28 11:17:01 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
value(int type)
|
|
|
|
{
|
|
|
|
int32_t temp = 0;
|
|
|
|
uint32_t pres = 0;
|
|
|
|
|
2018-07-23 15:49:18 +00:00
|
|
|
if(sensor_status != SENSOR_STATUS_READY) {
|
2018-05-31 15:17:07 +00:00
|
|
|
PRINTF("Sensor disabled or starting up (%d)\n", sensor_status);
|
|
|
|
return BMP_280_READING_ERROR;
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
|
2018-05-31 15:17:07 +00:00
|
|
|
/* A buffer for the raw reading from the sensor */
|
|
|
|
uint8_t sensor_value[MEAS_DATA_SIZE];
|
2018-05-28 11:17:01 +00:00
|
|
|
|
2018-07-23 15:49:18 +00:00
|
|
|
switch(type) {
|
2018-05-31 15:17:07 +00:00
|
|
|
case BMP_280_SENSOR_TYPE_TEMP:
|
|
|
|
case BMP_280_SENSOR_TYPE_PRESS:
|
|
|
|
memset(sensor_value, 0, MEAS_DATA_SIZE);
|
2018-07-23 15:49:18 +00:00
|
|
|
if(!read_data(sensor_value, MEAS_DATA_SIZE)) {
|
2018-05-31 15:17:07 +00:00
|
|
|
return BMP_280_READING_ERROR;
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
PRINTF("val: %02x%02x%02x %02x%02x%02x\n",
|
|
|
|
sensor_value[0], sensor_value[1], sensor_value[2],
|
|
|
|
sensor_value[3], sensor_value[4], sensor_value[5]);
|
|
|
|
|
|
|
|
convert(sensor_value, &temp, &pres);
|
|
|
|
|
|
|
|
if(type == BMP_280_SENSOR_TYPE_TEMP) {
|
2018-05-31 15:17:07 +00:00
|
|
|
return (int)temp;
|
2018-05-28 11:17:01 +00:00
|
|
|
} else if(type == BMP_280_SENSOR_TYPE_PRESS) {
|
2018-05-31 15:17:07 +00:00
|
|
|
return (int)pres;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
2018-05-31 15:17:07 +00:00
|
|
|
|
|
|
|
default:
|
|
|
|
PRINTF("Invalid BMP 208 Sensor Type\n");
|
|
|
|
return BMP_280_READING_ERROR;
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2018-07-16 14:39:19 +00:00
|
|
|
* \brief Configuration function for the BMP280 sensor.
|
|
|
|
* \param type Activate, enable or disable the sensor. See below
|
|
|
|
* \param enable Disable sensor if 0; else, enable sensor otherwise.
|
|
|
|
* When type == SENSORS_HW_INIT we turn on the hardware.
|
|
|
|
* When type == SENSORS_ACTIVE and enable==1 we enable the sensor.
|
|
|
|
* When type == SENSORS_ACTIVE and enable==0 we disable the sensor.
|
2018-05-28 11:17:01 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
configure(int type, int enable)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case SENSORS_HW_INIT:
|
2018-07-23 15:49:18 +00:00
|
|
|
if(init()) {
|
2018-05-31 15:17:07 +00:00
|
|
|
enable_sensor(false);
|
|
|
|
sensor_status = SENSOR_STATUS_INITIALISED;
|
|
|
|
} else {
|
|
|
|
sensor_status = SENSOR_STATUS_DISABLED;
|
|
|
|
}
|
2018-05-28 11:17:01 +00:00
|
|
|
break;
|
2018-05-31 15:17:07 +00:00
|
|
|
|
2018-05-28 11:17:01 +00:00
|
|
|
case SENSORS_ACTIVE:
|
|
|
|
/* Must be initialised first */
|
2018-07-23 15:49:18 +00:00
|
|
|
if(sensor_status == SENSOR_STATUS_DISABLED) {
|
2018-05-31 15:17:07 +00:00
|
|
|
break;
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
2018-07-23 15:49:18 +00:00
|
|
|
if(enable) {
|
2018-05-31 15:17:07 +00:00
|
|
|
enable_sensor(true);
|
2018-05-28 11:17:01 +00:00
|
|
|
ctimer_set(&startup_timer, SENSOR_STARTUP_DELAY, notify_ready, NULL);
|
2018-05-31 15:17:07 +00:00
|
|
|
sensor_status = SENSOR_STATUS_NOT_READY;
|
2018-05-28 11:17:01 +00:00
|
|
|
} else {
|
|
|
|
ctimer_stop(&startup_timer);
|
2018-05-31 15:17:07 +00:00
|
|
|
enable_sensor(false);
|
|
|
|
sensor_status = SENSOR_STATUS_INITIALISED;
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
break;
|
2018-05-31 15:17:07 +00:00
|
|
|
|
2018-05-28 11:17:01 +00:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2018-05-31 15:17:07 +00:00
|
|
|
return sensor_status;
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
2018-07-16 14:39:19 +00:00
|
|
|
* \brief Returns the status of the sensor.
|
|
|
|
* \param type SENSORS_ACTIVE or SENSORS_READY.
|
|
|
|
* \return Current status of the sensor.
|
2018-05-28 11:17:01 +00:00
|
|
|
*/
|
|
|
|
static int
|
|
|
|
status(int type)
|
|
|
|
{
|
|
|
|
switch(type) {
|
|
|
|
case SENSORS_ACTIVE:
|
|
|
|
case SENSORS_READY:
|
2018-05-31 15:17:07 +00:00
|
|
|
return sensor_status;
|
2018-05-28 11:17:01 +00:00
|
|
|
default:
|
2018-07-16 14:39:19 +00:00
|
|
|
return SENSOR_STATUS_DISABLED;
|
2018-05-28 11:17:01 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
SENSORS_SENSOR(bmp_280_sensor, "BMP280", value, configure, status);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-07-24 15:24:06 +00:00
|
|
|
#endif /* BOARD_SENSORS_ENABLE */
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2018-05-28 11:17:01 +00:00
|
|
|
/** @} */
|