diff options
author | Stephane D'Alu <sdalu@sdalu.com> | 2016-02-07 18:54:54 +0100 |
---|---|---|
committer | Stephane D'Alu <sdalu@sdalu.com> | 2016-02-07 18:54:54 +0100 |
commit | 7ecdfd4386abe1bd27a92da87a5a487a946c1165 (patch) | |
tree | f8cf678b4c4805ed6f876dc1bfd19f0dab82105c /os/various/devices_lib | |
parent | 12992db45cf71b0b729644452e2eb1966eb7a575 (diff) | |
download | ChibiOS-Contrib-7ecdfd4386abe1bd27a92da87a5a487a946c1165.tar.gz ChibiOS-Contrib-7ecdfd4386abe1bd27a92da87a5a487a946c1165.tar.bz2 ChibiOS-Contrib-7ecdfd4386abe1bd27a92da87a5a487a946c1165.zip |
mcp9808 temperature sensor
Diffstat (limited to 'os/various/devices_lib')
-rw-r--r-- | os/various/devices_lib/sensors/mcp9808/mcp9808.c | 200 | ||||
-rw-r--r-- | os/various/devices_lib/sensors/mcp9808/mcp9808.h | 196 |
2 files changed, 396 insertions, 0 deletions
diff --git a/os/various/devices_lib/sensors/mcp9808/mcp9808.c b/os/various/devices_lib/sensors/mcp9808/mcp9808.c new file mode 100644 index 0000000..df4d440 --- /dev/null +++ b/os/various/devices_lib/sensors/mcp9808/mcp9808.c @@ -0,0 +1,200 @@ + +#define I2C_HELPERS_AUTOMATIC_DRV TRUE + +#include "hal.h" +#include "i2c_helpers.h" +#include "mcp9808.h" + +// http://www.mouser.com/ds/2/268/25095A-15487.pdf +// http://ww1.microchip.com/downloads/en/DeviceDoc/25095A.pdf + + +/*===========================================================================*/ +/* Driver local definitions. */ +/*===========================================================================*/ + +/* I2C Register */ +#define MCP9808_REG_CONFIG 0x01 +#define MCP9808_REG_UPPER_TEMP 0x02 +#define MCP9808_REG_LOWER_TEMP 0x03 +#define MCP9808_REG_CRIT_TEMP 0x04 +#define MCP9808_REG_AMBIENT_TEMP 0x05 +#define MCP9808_REG_MANUF_ID 0x06 +#define MCP9808_REG_DEVICE_ID 0x07 +#define MCP9808_REG_RESOLUTION 0x08 + +/* Config */ +#define MCP9808_REG_CONFIG_SHUTDOWN 0x0100 +#define MCP9808_REG_CONFIG_CRITLOCKED 0x0080 +#define MCP9808_REG_CONFIG_WINLOCKED 0x0040 +#define MCP9808_REG_CONFIG_INTCLR 0x0020 +#define MCP9808_REG_CONFIG_ALERTSTAT 0x0010 +#define MCP9808_REG_CONFIG_ALERTCTRL 0x0008 +#define MCP9808_REG_CONFIG_ALERTSEL 0x0002 +#define MCP9808_REG_CONFIG_ALERTPOL 0x0002 +#define MCP9808_REG_CONFIG_ALERTMODE 0x0001 + +/* Device Id */ +#define MCP9808_MANUF_ID 0x0054 +#define MCP9808_DEVICE_ID 0x0400 + +/* Resolution */ +#define MCP9808_RES_2 0x00 /* 1/2 = 0.5 */ +#define MCP9808_RES_4 0x01 /* 1/4 = 0.25 */ +#define MCP9808_RES_8 0x10 /* 1/8 = 0.125 */ +#define MCP9808_RES_16 0x11 /* 1/16 = 0.0625 */ + +/* Time in milli-seconds */ +#define MCP9808_DELAY_ACQUIRE_RES_2 30 +#define MCP9808_DELAY_ACQUIRE_RES_4 65 +#define MCP9808_DELAY_ACQUIRE_RES_8 130 +#define MCP9808_DELAY_ACQUIRE_RES_16 250 + +/*===========================================================================*/ +/* Driver exported variables. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local variables and types. */ +/*===========================================================================*/ + +/*===========================================================================*/ +/* Driver local functions. */ +/*===========================================================================*/ + +static inline msg_t +_decode_measure(MCP9808_drv *drv, + uint16_t val, float *temperature) { + + /* Temperature */ + if (temperature) { + float temp = val & 0x0fff; + if (val & 0x1000) temp -= 0x1000; + + float factor = 16.0F; + switch(drv->resolution) { + case RES_2 : factor = 2.0F; break; + case RES_4 : factor = 4.0F; break; + case RES_8 : factor = 8.0F; break; + case RES_16: factor = 16.0F; break; + } + + *temperature = temp / factor; + } + + /* Ok */ + return MSG_OK; +} + + +/*===========================================================================*/ +/* Driver exported functions. */ +/*===========================================================================*/ + +void +MCP9808_init(MCP9808_drv *drv, MCP9808_config *config) { + drv->config = config; + drv->cfg = 0; + drv->resolution = RES_16; + drv->state = MCP9808_INIT; +} + +msg_t +MCP9808_check(MCP9808_drv *drv) { + msg_t msg = -10; + uint16_t val; + + if ((msg = i2c_reg_recv16_be(MCP9808_REG_MANUF_ID, &val)) < MSG_OK) + return msg; + if (val != MCP9808_MANUF_ID) + return -2; + + if ((msg = i2c_reg_recv16_be(MCP9808_REG_DEVICE_ID, &val)) < MSG_OK) + return msg; + if (val != MCP9808_DEVICE_ID) + return -2; + + return MSG_OK; +} + +msg_t +MCP9808_setResolution(MCP9808_drv *drv, MCP9808_resolution_t res) { + struct __attribute__((packed)) { + uint8_t reg; + uint8_t resolution; + } tx = { MCP9808_REG_RESOLUTION, res }; + + msg_t msg; + if ((msg = i2c_send((uint8_t*)&tx, sizeof(tx))) < MSG_OK) + return msg; + + drv->resolution = res; + return MSG_OK; +} + + +msg_t +MCP9808_start(MCP9808_drv *drv) { + drv->cfg &= ~(MCP9808_REG_CONFIG_SHUTDOWN); + + struct __attribute__((packed)) { + uint8_t reg; + uint16_t conf; + } tx = { MCP9808_REG_CONFIG, cpu_to_be16(drv->cfg) }; + + return i2c_send((uint8_t*)&tx, sizeof(tx)) >= 0; +} + +msg_t +MCP9808_stop(MCP9808_drv *drv) { + drv->cfg |= (MCP9808_REG_CONFIG_SHUTDOWN); + + struct __attribute__((packed)) { + uint8_t reg; + uint16_t conf; + } tx = { MCP9808_REG_CONFIG, cpu_to_be16(drv->cfg) }; + + return i2c_send((uint8_t*)&tx, sizeof(tx)) >= 0; +} + + + +unsigned int +MCP9808_getAcquisitionTime(MCP9808_drv *drv) { + switch(drv->resolution) { + case RES_2 : return MCP9808_DELAY_ACQUIRE_RES_2; + case RES_4 : return MCP9808_DELAY_ACQUIRE_RES_4; + case RES_8 : return MCP9808_DELAY_ACQUIRE_RES_8; + case RES_16: return MCP9808_DELAY_ACQUIRE_RES_16; + } + osalDbgAssert(false, "programming error"); + return -1; +} + +msg_t +MCP9808_readMeasure(MCP9808_drv *drv, + float *temperature) { + + msg_t msg; + uint16_t val; + + if ((msg = i2c_reg_recv16_be(MCP9808_REG_AMBIENT_TEMP, &val)) < MSG_OK) + return msg; + + return _decode_measure(drv, val, temperature); +} + + +msg_t +MCP9808_readTemperature(MCP9808_drv *drv, + float *temperature) { + osalDbgAssert(drv->state == MCP9808_STARTED, "invalid state"); + + msg_t msg; + uint16_t val; + + if ((msg = i2c_reg_recv16_be(MCP9808_REG_AMBIENT_TEMP, &val)) < MSG_OK) + return msg; + + return _decode_measure(drv, val, temperature); +} diff --git a/os/various/devices_lib/sensors/mcp9808/mcp9808.h b/os/various/devices_lib/sensors/mcp9808/mcp9808.h new file mode 100644 index 0000000..74540b4 --- /dev/null +++ b/os/various/devices_lib/sensors/mcp9808/mcp9808.h @@ -0,0 +1,196 @@ +/* + MCP9808 for ChibiOS/RT - Copyright (C) 2016 Stephane D'Alu +*/ + +#ifndef _SENSOR_MCP9808_H_ +#define _SENSOR_MCP9808_H_ + +#include <math.h> +#include <stdbool.h> +#include "i2c_helpers.h" + + +#define MCP9808_I2CADDR_DEFAULT 0x18 + +typedef enum { + RES_2 = 0x00, /* 1/2 = 0.5 */ + RES_4 = 0x01, /* 1/4 = 0.25 */ + RES_8 = 0x10, /* 1/8 = 0.125 */ + RES_16 = 0x11, /* 1/16 = 0.0625 */ +} MCP9808_resolution_t; + + + +#define MCP9808_CONTINUOUS_ACQUISITION_SUPPORTED TRUE + +/** + * @brief MCP9808 configuration structure. + */ +typedef struct { + I2CHelper i2c; /* keep it first */ +} MCP9808_config; + + +/** + * @brief Driver state machine possible states. + */ +typedef enum __attribute__ ((__packed__)) { + MCP9808_UNINIT = 0, /**< Not initialized. */ + MCP9808_INIT = 1, /**< Initialized. */ + MCP9808_STARTED = 2, /**< Started. */ + MCP9808_MEASURING = 4, /**< Measuring. */ + MCP9808_READY = 3, /**< Ready. */ + MCP9808_STOPPED = 5, /**< Stopped. */ + MCP9808_ERROR = 6, /**< Error. */ +} MCP9808_state_t; + + +/** + * @brief MCP9808 configuration structure. + */ +typedef struct { + MCP9808_config *config; + MCP9808_state_t state; + MCP9808_resolution_t resolution; + uint16_t cfg; +} MCP9808_drv; + +/** + * @brief Initialize the sensor driver + */ +void +MCP9808_init(MCP9808_drv *drv, + MCP9808_config *config); + +/** + * @brief Check that the sensor is really present + */ +msg_t +MCP9808_check(MCP9808_drv *drv); + +/** + * @brief Start the sensor + */ +msg_t +MCP9808_start(MCP9808_drv *drv); + +/** + * @brief Stop the sensor + * + * @details If the sensor support it, it will be put in low energy mode. + */ +msg_t +MCP9808_stop(MCP9808_drv *drv); + + + +/** + * @brief Control the MCP9809 resolution. + */ +msg_t +MCP9808_setResolution(MCP9808_drv *drv, + MCP9808_resolution_t res); + + +/** + * @brief Time necessary for the sensor to boot + * + * @returns + * unsigned int time in millis-seconds + */ + +static inline unsigned int +MCP9808_getBootupTime(MCP9808_drv *drv) { + (void)drv; + return 15; +}; + +/** + * @brief Time necessary the sensor to for starting + * + * @returns + * unsigned int time in millis-seconds + */ +static inline unsigned int +MCP9808_getStartupTime(MCP9808_drv *drv) { + (void)drv; + return 0; +}; + +/** + * @brief Time in milli-seconds necessary for acquiring a naw measure + * + * @returns + * unsigned int time in millis-seconds + */ +unsigned int +MCP9808_getAcquisitionTime(MCP9808_drv *drv); + +/** + * @brief Trigger a mesure acquisition + */ +static inline msg_t +MCP9808_startMeasure(MCP9808_drv *drv) { +} + +/** + * @brief Read the newly acquiered measure + * + * @note According the the sensor design the measure read + * can be any value acquired after the acquisition time + * and the call to readMeasure. + */ +msg_t +MCP9808_readMeasure(MCP9808_drv *drv, + float *temperature); + + +/** + * @brief Read temperature and humidity + * + * @details According to the sensor specification/configuration + * (see #MCP9808_CONTINUOUS_ACQUISITION_SUPPORTED), + * if the sensor is doing continuous measurement + * it's value will be requested and returned immediately. + * Otherwise a measure is started, the necessary amount of + * time for acquiring the value is spend sleeping (not spinning), + * and finally the measure is read. + * + * @note In continuous measurement mode, if you just started + * the sensor, you will need to wait getAcquisitionTime() + * in addition to the usual getStartupTime() + + * @note If using several sensors, it is better to start all the + * measure together, wait for the sensor having the longuest + * aquisition time, and finally read all the values + */ +msg_t +MCP9808_readTemperature(MCP9808_drv *drv, + float *temperature); + +/** + * @brief Return the temperature value in °C. + * + * @details Use readTemperatureHumidity() for returning the humidity value. + * + * @note Prefere readTemperatureHumidity(), if you need both temperature + * and humidity, or if you need better error handling. + * + * @returns + * float humidity percent + * NAN on failure + */ +static inline float +MCP9808_getTemperature(MCP9808_drv *drv) { + float temperature = NAN; + MCP9808_readTemperature(drv, &temperature); + return temperature; +} + + + + + + +#endif + |