aboutsummaryrefslogtreecommitdiffstats
path: root/os/various/devices_lib/sensors/mcp9808.c
blob: 4b22ea9f342dcda7a56bb7906c2d7b0a876d9785 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
    MCP9808 for ChibiOS/RT - Copyright (C) 2016 Stephane D'Alu

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
*/

#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
_apply_config(MCP9808_drv *drv) {
    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));
}

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; /* power up default */
    drv->state       = SENSOR_INIT;
}

msg_t
MCP9808_check(MCP9808_drv *drv) {
    uint16_t manuf, device;

    msg_t msg;
    if (((msg = i2c_reg_recv16_be(MCP9808_REG_MANUF_ID,  &manuf )) < MSG_OK) ||
	((msg = i2c_reg_recv16_be(MCP9808_REG_DEVICE_ID, &device)) < MSG_OK))
	return msg;
    
    if ((manuf != MCP9808_MANUF_ID) || (device != MCP9808_DEVICE_ID))
	return SENSOR_NOTFOUND;

    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);
    return _apply_config(drv);
}

msg_t
MCP9808_stop(MCP9808_drv *drv) {
    drv->cfg |= (MCP9808_REG_CONFIG_SHUTDOWN);
    return _apply_config(drv);
}

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, "OOPS");
    return 0;
}

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 == SENSOR_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);    
}