aboutsummaryrefslogtreecommitdiff
path: root/lib/chibios-contrib/os/various/devices_lib/sensors/mcp9808.h
diff options
context:
space:
mode:
Diffstat (limited to 'lib/chibios-contrib/os/various/devices_lib/sensors/mcp9808.h')
-rw-r--r--lib/chibios-contrib/os/various/devices_lib/sensors/mcp9808.h204
1 files changed, 204 insertions, 0 deletions
diff --git a/lib/chibios-contrib/os/various/devices_lib/sensors/mcp9808.h b/lib/chibios-contrib/os/various/devices_lib/sensors/mcp9808.h
new file mode 100644
index 000000000..857f2f98b
--- /dev/null
+++ b/lib/chibios-contrib/os/various/devices_lib/sensors/mcp9808.h
@@ -0,0 +1,204 @@
1/*
2 MCP9808 for ChibiOS/RT - Copyright (C) 2016 Stephane D'Alu
3
4 Licensed under the Apache License, Version 2.0 (the "License");
5 you may not use this file except in compliance with the License.
6 You may obtain a copy of the License at
7
8 http://www.apache.org/licenses/LICENSE-2.0
9
10 Unless required by applicable law or agreed to in writing, software
11 distributed under the License is distributed on an "AS IS" BASIS,
12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 See the License for the specific language governing permissions and
14 limitations under the License.
15*/
16
17#ifndef _SENSOR_MCP9808_H_
18#define _SENSOR_MCP9808_H_
19
20#include <math.h>
21#include "i2c_helpers.h"
22#include "sensor.h"
23
24/*===========================================================================*/
25/* Driver constants. */
26/*===========================================================================*/
27
28#define MCP9808_CONTINUOUS_ACQUISITION_SUPPORTED TRUE
29
30
31#define MCP9808_I2CADDR_FIXED 0x18
32
33/**
34 * @brief Time necessary for the sensor to boot
35 */
36#define MCP9808_BOOTUP_TIME 0
37
38/**
39 * @brief Time necessary for the sensor to start
40 */
41#define MCP9808_STARTUP_TIME 0
42
43/*===========================================================================*/
44/* Driver pre-compile time settings. */
45/*===========================================================================*/
46
47/*===========================================================================*/
48/* Derived constants and error checks. */
49/*===========================================================================*/
50
51#define MCP9808_I2CADDR_DEFAULT MCP9808_I2CADDR_FIXED
52
53/*===========================================================================*/
54/* Driver data structures and types. */
55/*===========================================================================*/
56
57/**
58 * @brief Different possible resolution
59 */
60typedef enum {
61 RES_2 = 0x00, /**< @brief Resolution of 1/2 = 0.5 */
62 RES_4 = 0x01, /**< @brief Resolution of 1/4 = 0.25 */
63 RES_8 = 0x10, /**< @brief Resolution of 1/8 = 0.125 */
64 RES_16 = 0x11, /**< @brief Resolution of 1/16 = 0.0625 */
65} MCP9808_resolution_t;
66
67/**
68 * @brief MCP9808 configuration structure.
69 */
70typedef struct {
71 I2CHelper i2c; /* keep it first */
72} MCP9808_config;
73
74/**
75 * @brief MCP9808 configuration structure.
76 */
77typedef struct {
78 MCP9808_config *config;
79 sensor_state_t state;
80 MCP9808_resolution_t resolution;
81 uint16_t cfg;
82} MCP9808_drv;
83
84/**
85 * @brief MCP9808 measure reading
86 */
87typedef struct {
88 float temperature;
89} MCP9808_measure;
90
91/*===========================================================================*/
92/* Driver macros. */
93/*===========================================================================*/
94
95
96/*===========================================================================*/
97/* External declarations. */
98/*===========================================================================*/
99
100/**
101 * @brief Initialize the sensor driver
102 */
103void
104MCP9808_init(MCP9808_drv *drv,
105 MCP9808_config *config);
106
107/**
108 * @brief Check that the sensor is really present
109 */
110msg_t
111MCP9808_check(MCP9808_drv *drv);
112
113/**
114 * @brief Start the sensor
115 */
116msg_t
117MCP9808_start(MCP9808_drv *drv);
118
119/**
120 * @brief Stop the sensor
121 *
122 * @details If the sensor support it, it will be put in low energy mode.
123 */
124msg_t
125MCP9808_stop(MCP9808_drv *drv);
126
127/**
128 * @brief Control the MCP9809 resolution.
129 */
130msg_t
131MCP9808_setResolution(MCP9808_drv *drv,
132 MCP9808_resolution_t res);
133
134/**
135 * @brief Time in milli-seconds necessary for acquiring a naw measure
136 *
137 * @returns
138 * unsigned int time in millis-seconds
139 */
140unsigned int
141MCP9808_getAcquisitionTime(MCP9808_drv *drv);
142
143/**
144 * @brief Trigger a mesure acquisition
145 */
146static inline msg_t
147MCP9808_startMeasure(MCP9808_drv *drv) {
148 (void)drv;
149 return MSG_OK;
150}
151
152/**
153 * @brief Read the newly acquiered measure
154 *
155 * @note According the the sensor design the measure read
156 * can be any value acquired after the acquisition time
157 * and the call to readMeasure.
158 */
159msg_t
160MCP9808_readMeasure(MCP9808_drv *drv,
161 float *temperature);
162
163
164/**
165 * @brief Read temperature and humidity
166 *
167 * @details According to the sensor specification/configuration
168 * (see #MCP9808_CONTINUOUS_ACQUISITION_SUPPORTED),
169 * if the sensor is doing continuous measurement
170 * it's value will be requested and returned immediately.
171 * Otherwise a measure is started, the necessary amount of
172 * time for acquiring the value is spend sleeping (not spinning),
173 * and finally the measure is read.
174 *
175 * @note In continuous measurement mode, if you just started
176 * the sensor, you will need to wait getAcquisitionTime()
177 * in addition to the usual getStartupTime()
178
179 * @note If using several sensors, it is better to start all the
180 * measure together, wait for the sensor having the longuest
181 * aquisition time, and finally read all the values
182 */
183msg_t
184MCP9808_readTemperature(MCP9808_drv *drv,
185 float *temperature);
186
187/**
188 * @brief Return the temperature value in °C.
189 *
190 * @note Prefere readTemperature(), if you need better error handling.
191 *
192 * @return The temperature in °C
193 * @retval float humidity percent
194 * @retval NAN on failure
195 */
196static inline float
197MCP9808_getTemperature(MCP9808_drv *drv) {
198 float temperature = NAN;
199 MCP9808_readTemperature(drv, &temperature);
200 return temperature;
201}
202
203#endif
204