-
Notifications
You must be signed in to change notification settings - Fork 1
/
adt74xx.c
358 lines (323 loc) · 10.4 KB
/
adt74xx.c
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
/*
* Copyright (c) 2022, IoTSharp Development Team
*
* SPDX-License-Identifier: LGPLv2.1
*
* Change Logs:
* Date Author Notes
* 2022-06-15 maikebing the first version
*/
#include <string.h>
#define DBG_ENABLE
#define DBG_SECTION_NAME "adt74xx"
#define DBG_LEVEL DBG_LOG
#define DBG_COLOR
#include <rtdbg.h>
#include "adt74xx.h"
adt74xx_device_t adt74xx_init(const char *i2c_bus_name, rt_uint8_t adt74xx_addr)
{
adt74xx_device_t dev;
RT_ASSERT(i2c_bus_name);
dev = rt_calloc(1, sizeof(struct adt74xx_device));
if (dev == RT_NULL)
{
LOG_E("Can't allocate memory for adt74xx device on '%s' ", i2c_bus_name);
return RT_NULL;
}
if (adt74xx_addr == adt74xx_I2CADDR_DEFAULT)
{
dev->adt74xx_addr = adt74xx_addr;
}
else
{
LOG_E("Illegal adt74xx address:'%x'", adt74xx_addr);
rt_free(dev);
return RT_NULL;
}
dev->i2c = rt_i2c_bus_device_find(i2c_bus_name);
if (dev->i2c == RT_NULL)
{
LOG_E("Can't find adt74xx_addr device on '%s' ", i2c_bus_name);
rt_free(dev);
return RT_NULL;
}
dev->lock = rt_mutex_create("mutex_adt74xx_addr", RT_IPC_FLAG_FIFO);
if (dev->lock == RT_NULL)
{
LOG_E("Can't create mutex for adt74xx_addr device on '%s' ", i2c_bus_name);
rt_free(dev);
return RT_NULL;
}
// clear the status register
adt74xx_clear_status(dev);
// read config
adt74xx_read_config(dev);
return dev;
}
void adt74xx_deinit(adt74xx_device_t dev)
{
RT_ASSERT(dev);
rt_mutex_delete(dev->lock);
rt_free(dev);
}
// 写单个寄存器
//reg: 寄存器地址
//data: 数据
// 返回值: 0, 正常 / -1, 错误代码
static rt_err_t adt74xx_write_reg(adt74xx_device_t dev, rt_uint8_t reg, rt_uint8_t data)
{
struct rt_i2c_msg msgs;
rt_uint8_t buf[2] = { reg, data };
msgs.addr = dev->adt74xx_addr; /* 从机地址 */
msgs.flags = RT_I2C_WR; /* 写标志 */
msgs.buf = buf; /* 发送数据指针 */
msgs.len = 2;
if (rt_i2c_transfer(dev->i2c, &msgs, 1) == 1)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
// 读取寄存器数据
//reg: 要读取的寄存器地址
//len: 要读取的数据字节数
//buf: 读取到的数据存储区
// 返回值: 0, 正常 / -1, 错误代码
static rt_err_t adt74xx_read_reg(adt74xx_device_t dev, rt_uint8_t reg, rt_uint8_t *buf, rt_uint8_t len)
{
struct rt_i2c_msg msgs[2];
msgs[0].addr = dev->adt74xx_addr; /* 从机地址 */
msgs[0].flags = RT_I2C_WR; /* 写标志 */
msgs[0].buf = ® /* 从机寄存器地址 */
msgs[0].len = 1; /* 发送数据字节数 */
msgs[1].addr = dev->adt74xx_addr; /* 从机地址 */
msgs[1].flags = RT_I2C_RD; /* 读标志 */
msgs[1].buf = buf; /* 读取数据指针 */
msgs[1].len = len; /* 读取数据字节数 */
if (rt_i2c_transfer(dev->i2c, msgs, 2) == 2)
{
return RT_EOK;
}
else
{
return -RT_ERROR;
}
}
rt_err_t adt74xx_clear_status(adt74xx_device_t dev)
{
dev->status.word = 0x00;
//dev->status.status_bits.RDY=1;
return adt74xx_write_reg(dev, adt74xx_REG_STATUS, dev->status.word);
}
rt_err_t adt74xx_softreset(adt74xx_device_t dev)
{
rt_err_t ret = adt74xx_write_reg(dev, adt74xx_REG_SWRST, 0x00);
rt_thread_mdelay(10);
return ret;
}
rt_err_t adt74xx_check(adt74xx_device_t dev)
{
rt_uint8_t buf[2] = { 0 };
rt_err_t ret = RT_EOK;
if (adt74xx_read_reg(dev, adt74xx_REG_ID, (rt_uint8_t*) &buf, 1) == RT_EOK)
{
dev->id.word = buf[0];
//dev->id.id_word==0xc8;
if ((buf[0] & 0xF8) == 0xC8) // dev->id=
{
return RT_EOK;
}
}
return ret;
}
rt_err_t adt74xx_read_status(adt74xx_device_t dev)
{
rt_uint8_t buf[2] = { 0 };
rt_err_t ret = RT_ERROR;
if (adt74xx_read_reg(dev, adt74xx_REG_STATUS, (rt_uint8_t*) &buf, 1) == RT_EOK)
{
dev->status.word = buf[0];
ret = RT_EOK;
}
return ret;
}
rt_err_t adt74xx_read_config(adt74xx_device_t dev)
{
rt_uint8_t buf[2] = { 0 };
rt_err_t ret = RT_EOK;
if (adt74xx_read_reg(dev, adt74xx_REG_STATUS, (rt_uint8_t*) &buf, 1) == RT_EOK)
{
dev->config.word = buf[0];
}
return ret;
}
static rt_uint16_t swapShort16(rt_uint16_t shortValue)
{
return ((shortValue & 0x00FF) << 8) | ((shortValue & 0xFF00) >> 8);
}
rt_err_t adt74xx_read_temperature(adt74xx_device_t dev)
{
rt_err_t ret = RT_ERROR;
RT_ASSERT(dev);
rt_uint16_t temp = 0;
rt_uint16_t temp1 = 0;
float temp_c = 0;
if (adt74xx_read_reg(dev, adt74xx_REG_TEMPMSB, (rt_uint8_t*) &temp1, 2) == RT_EOK)
{
temp=swapShort16(temp1);
//rt_kprintf("adt74xx_read_temperature:%d res=%d\n",temp,dev->config.config_bits.res);
if(dev->config.config_bits.res) {
if(temp & 0x8000)
/*! Negative temperature */
temp_c = (float)((rt_int32_t)temp - 65536) / 128;
else
/*! Positive temperature */
temp_c = (float)temp / 128;
} else {
temp >>= 3;
if(temp & 0x1000)
/*! Negative temperature */
temp_c = (float)((rt_int32_t)temp - 8192) / 16;
else
/*! Positive temperature */
temp_c = (float)temp / 16;
}
dev->temperature = temp_c;
ret = RT_EOK;
}
return ret;
}
/**
* This function is exported to MSH commands list
* Usage example:
* - sht3x probe i2c1 pu : initialize sht3x device on i2c1 bus with address pin pulled up(i2c address 0x45)
* - sht3x probe i2c1: initialize sht3x device one i2c1 bus with address pin pulled down by default(i2c address 0x44)
* - sht3x read: read and print temperature and humidity from previously initialized sht3x
*/
void adt74xx(int argc, char *argv[])
{
static adt74xx_device_t dev = RT_NULL;
rt_uint8_t adt_addr = adt74xx_I2CADDR_DEFAULT;
if (argc > 1)
{
if (!strcmp(argv[1], "probe"))
{
if (argc >= 3)
{
/* initialize the sensor when first probe */
if (!dev || strcmp(dev->i2c->parent.parent.name, argv[2]))
{
/* deinit the old device */
if (dev)
{
rt_kprintf("Deinit adt74xx");
adt74xx_deinit(dev);
}
// no else needed here
if (argc > 3)
{
if (!strcmp("pu", argv[3]))
{
adt_addr = adt74xx_I2CADDR_DEFAULT;
}
else
{
rt_kprintf("Illegal adt74xx address, using 0x44 by default\n");
adt_addr = adt74xx_I2CADDR_DEFAULT; // pulled down by default: 0x48
}
}
// no else needed here
dev = adt74xx_init(argv[2], adt_addr);
if (!dev)
{
rt_kprintf("adt74xx probe failed, check input args\n");
}
else
{
rt_kprintf("adt74xx probed, addr:0x%x\n", adt_addr);
}
}
}
else
{
rt_kprintf("adt74xx probe <i2c dev name> - probe sensor by given name\n");
}
}
else if (!strcmp(argv[1], "read"))
{
if (dev)
{
/* read the sensor data */
if (adt74xx_read_temperature(dev) == RT_EOK)
{
rt_kprintf("adt74xx temperature: %d.%d \n", (int) dev->temperature,
(int) (dev->temperature * 100) % 100);
}
else
{
rt_kprintf("Can't read adt74xx temperature: ");
}
}
else
{
rt_kprintf("Please using 'adt74xx probe <i2c dev name> <pu/pd>' first\n");
}
}
else if (!strcmp(argv[1], "status"))
{
if (dev)
{
if (adt74xx_read_status(dev) == RT_EOK)
{
rt_kprintf("adt74xx status:\n");
rt_kprintf("\tT_low:\t%d\t- \n", dev->status.status_bits.T_low);
rt_kprintf("\tT_high:\t%d\t-\n", dev->status.status_bits.T_high);
rt_kprintf("\tT_crit:\t%d\n", dev->status.status_bits.T_crit);
rt_kprintf("\tRDY:\t%d\n", dev->status.status_bits.RDY);
}
else
{
rt_kprintf("adt74xx status not read\n");
}
}
else
{
rt_kprintf("Please using 'adt74xx probe <i2c dev name> <pu/pd>' first\n");
}
}
else if (!strcmp(argv[1], "reset"))
{
if (dev)
{
if (adt74xx_softreset(dev) == RT_EOK)
{
rt_kprintf("adt74xx reset cmd sent\n");
}
else
{
rt_kprintf("adt74xx reset cmd not sent\n");
}
}
else
{
rt_kprintf("Please using 'adt74xx probe <i2c dev name> <pu/pd>' first\n");
}
}
else
{
rt_kprintf("Unknown command. Enter 'adt74xx' for help\n");
}
}
else
{
rt_kprintf("Usage:\n");
rt_kprintf("\tadt74xx probe <i2c dev name> <pu/pd> -- probe sensor by i2c dev name and pull config\n");
rt_kprintf("\tadt74xx read -- read sensor adt74xx data\n");
rt_kprintf("\tadt74xx status -- status register of adt74xx\n");
rt_kprintf("\tadt74xx reset -- send soft reset command to adt74xx\n");
}
}
MSH_CMD_EXPORT(adt74xx, adt74xx sensor);