LibDriver AHT20
Loading...
Searching...
No Matches
driver_aht20.c
Go to the documentation of this file.
1
36
37#include "driver_aht20.h"
38
42#define CHIP_NAME "ASAIR AHT20"
43#define MANUFACTURER_NAME "ASAIR"
44#define SUPPLY_VOLTAGE_MIN 2.2f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 0.98f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define AHT20_ADDRESS 0x70
55
66static uint8_t a_aht20_iic_read(aht20_handle_t *handle, uint8_t *data, uint16_t len)
67{
68 if (handle->iic_read_cmd(AHT20_ADDRESS, data, len) != 0) /* read the register */
69 {
70 return 1; /* return error */
71 }
72 else
73 {
74 return 0; /* success return 0 */
75 }
76}
77
88static uint8_t a_aht20_iic_write(aht20_handle_t *handle, uint8_t *data, uint16_t len)
89{
90 if (handle->iic_write_cmd(AHT20_ADDRESS, data, len) != 0) /* write the register */
91 {
92 return 1; /* return error */
93 }
94 else
95 {
96 return 0; /* success return 0 */
97 }
98}
99
107static uint8_t a_aht20_calc_crc(uint8_t *data, uint8_t len)
108{
109 uint8_t i;
110 uint8_t byte;
111 uint8_t crc = 0xFF;
112
113 for (byte = 0; byte < len; byte++) /* len times */
114 {
115 crc ^= data[byte]; /* xor byte */
116 for (i = 8; i > 0; --i) /* one byte */
117 {
118 if ((crc & 0x80) != 0) /* if high*/
119 {
120 crc = (crc << 1) ^ 0x31; /* xor 0x31 */
121 }
122 else
123 {
124 crc = crc << 1; /* skip */
125 }
126 }
127 }
128
129 return crc; /* return crc */
130}
131
141static uint8_t a_aht20_jh_reset_reg(aht20_handle_t *handle, uint8_t addr)
142{
143 uint8_t buf[3];
144 uint8_t regs[3];
145
146 buf[0] = addr; /* set the addr */
147 buf[1] = 0x00; /* set 0x00 */
148 buf[2] = 0x00; /* set 0x00 */
149 if (a_aht20_iic_write(handle, buf, 3) != 0) /* write the command */
150 {
151 return 1; /* return error */
152 }
153 handle->delay_ms(5); /* delay 5ms */
154 if (a_aht20_iic_read(handle, regs, 3) != 0) /* read regs */
155 {
156 return 1; /* return error */
157 }
158 handle->delay_ms(10); /* delay 10ms */
159 buf[0] = 0xB0 | addr; /* set addr */
160 buf[1] = regs[1]; /* set regs[1] */
161 buf[2] = regs[2]; /* set regs[2] */
162 if (a_aht20_iic_write(handle, buf, 3) != 0) /* write the data */
163 {
164 return 1; /* return error */
165 }
166
167 return 0; /* success return 0 */
168}
169
183{
184 uint8_t status;
185
186 if (handle == NULL) /* check handle */
187 {
188 return 2; /* return error */
189 }
190 if (handle->debug_print == NULL) /* check debug_print */
191 {
192 return 3; /* return error */
193 }
194 if (handle->iic_init == NULL) /* check iic_init */
195 {
196 handle->debug_print("aht20: iic_init is null.\n"); /* iic_init is null */
197
198 return 3; /* return error */
199 }
200 if (handle->iic_deinit == NULL) /* check iic_deinit */
201 {
202 handle->debug_print("aht20: iic_deinit is null.\n"); /* iic_deinit is null */
203
204 return 3; /* return error */
205 }
206 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
207 {
208 handle->debug_print("aht20: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
209
210 return 3; /* return error */
211 }
212 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
213 {
214 handle->debug_print("aht20: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
215
216 return 3; /* return error */
217 }
218 if (handle->delay_ms == NULL) /* check delay_ms */
219 {
220 handle->debug_print("aht20: delay_ms is null.\n"); /* delay_ms is null */
221
222 return 3; /* return error */
223 }
224
225 if (handle->iic_init() != 0) /* iic init */
226 {
227 handle->debug_print("aht20: iic init failed.\n"); /* iic init failed */
228
229 return 1; /* return error */
230 }
231 handle->delay_ms(500); /* wait for 500 ms */
232 if (a_aht20_iic_read(handle, &status, 1) != 0) /* read the status */
233 {
234 handle->debug_print("aht20: read status failed.\n"); /* read status failed */
235 (void)handle->iic_deinit(); /* close the iic */
236
237 return 4; /* return error */
238 }
239 if ((status & 0x18) != 0x18) /* check the status */
240 {
241 if (a_aht20_jh_reset_reg(handle, 0x1B) != 0) /* reset the 0x1B */
242 {
243 handle->debug_print("aht20: reset reg failed.\n"); /* reset reg failed */
244 (void)handle->iic_deinit(); /* close the iic */
245
246 return 5; /* return error */
247 }
248 if (a_aht20_jh_reset_reg(handle, 0x1C) != 0) /* reset the 0x1C */
249 {
250 handle->debug_print("aht20: reset reg failed.\n"); /* reset reg failed */
251 (void)handle->iic_deinit(); /* close the iic */
252
253 return 5; /* return error */
254 }
255 if (a_aht20_jh_reset_reg(handle, 0x1E) != 0) /* reset the 0x1E */
256 {
257 handle->debug_print("aht20: reset reg failed.\n"); /* reset reg failed */
258 (void)handle->iic_deinit(); /* close the iic */
259
260 return 5; /* return error */
261 }
262 }
263 handle->delay_ms(10); /* delay 10ms */
264 handle->inited = 1; /* flag finish initialization */
265
266 return 0; /* success return 0 */
267}
268
280{
281 if (handle == NULL) /* check handle */
282 {
283 return 2; /* return error */
284 }
285 if (handle->inited != 1) /* check handle initialization */
286 {
287 return 3; /* return error */
288 }
289
290 if (handle->iic_deinit() != 0) /* iic deinit */
291 {
292 handle->debug_print("aht20: iic deinit failed.\n"); /* iic deinit failed */
293
294 return 1; /* return error */
295 }
296 handle->inited = 0; /* set closed flag */
297
298 return 0; /* success return 0 */
299}
300
317uint8_t aht20_read_temperature_humidity(aht20_handle_t *handle, uint32_t *temperature_raw, float *temperature_s,
318 uint32_t *humidity_raw, uint8_t *humidity_s)
319{
320 uint8_t status;
321 uint8_t buf[7];
322
323 if (handle == NULL) /* check handle */
324 {
325 return 2; /* return error */
326 }
327 if (handle->inited != 1) /* check handle initialization */
328 {
329 return 3; /* return error */
330 }
331
332 buf[0] = 0xAC; /* set the addr */
333 buf[1] = 0x33; /* set 0x33 */
334 buf[2] = 0x00; /* set 0x00 */
335 if (a_aht20_iic_write(handle, buf, 3) != 0) /* write the command */
336 {
337 handle->debug_print("aht20: sent command failed.\n"); /* sent command failed */
338
339 return 1; /* return error */
340 }
341 handle->delay_ms(85); /* delay 85ms */
342 if (a_aht20_iic_read(handle, &status, 1) != 0) /* read the status */
343 {
344 handle->debug_print("aht20: read status failed.\n"); /* read status failed */
345
346 return 1; /* return error */
347 }
348 if ((status & 0x80) == 0) /* check the status */
349 {
350 if (a_aht20_iic_read(handle, buf, 7) != 0) /* read data */
351 {
352 handle->debug_print("aht20: read data failed.\n"); /* read data failed */
353
354 return 1; /* return error */
355 }
356 if (a_aht20_calc_crc(buf, 6) != buf[6]) /* check the crc */
357 {
358 handle->debug_print("aht20: crc is error.\n"); /* crc is error */
359
360 return 5; /* return error */
361 }
362
363 *humidity_raw = (((uint32_t)buf[1]) << 16) |
364 (((uint32_t)buf[2]) << 8) |
365 (((uint32_t)buf[3]) << 0); /* set the humidity */
366 *humidity_raw = (*humidity_raw) >> 4; /* right shift 4 */
367 *humidity_s = (uint8_t)((float)(*humidity_raw)
368 / 1048576.0f * 100.0f); /* convert the humidity */
369 *temperature_raw = (((uint32_t)buf[3]) << 16) |
370 (((uint32_t)buf[4]) << 8) |
371 (((uint32_t)buf[5]) << 0); /* set the temperature */
372 *temperature_raw = (*temperature_raw) & 0xFFFFF; /* cut the temperature part */
373 *temperature_s = (float)(*temperature_raw)
374 / 1048576.0f * 200.0f
375 - 50.0f; /* right shift 4 */
376
377 return 0; /* success return 0 */
378 }
379 else
380 {
381 handle->debug_print("aht20: data is not ready.\n"); /* data is not ready */
382
383 return 4; /* return error */
384 }
385}
386
401uint8_t aht20_read_temperature(aht20_handle_t *handle, uint32_t *temperature_raw, float *temperature_s)
402{
403 uint8_t status;
404 uint8_t buf[7];
405
406 if (handle == NULL) /* check handle */
407 {
408 return 2; /* return error */
409 }
410 if (handle->inited != 1) /* check handle initialization */
411 {
412 return 3; /* return error */
413 }
414
415 buf[0] = 0xAC; /* set the addr */
416 buf[1] = 0x33; /* set 0x33 */
417 buf[2] = 0x00; /* set 0x00 */
418 if (a_aht20_iic_write(handle, buf, 3) != 0) /* write the command */
419 {
420 handle->debug_print("aht20: sent command failed.\n"); /* sent command failed */
421
422 return 1; /* return error */
423 }
424 handle->delay_ms(85); /* delay 85ms */
425 if (a_aht20_iic_read(handle, &status, 1) != 0) /* read the status */
426 {
427 handle->debug_print("aht20: read status failed.\n"); /* read status failed */
428
429 return 1; /* return error */
430 }
431 if ((status & 0x80) == 0) /* check the status */
432 {
433 if (a_aht20_iic_read(handle, buf, 7) != 0) /* read data */
434 {
435 handle->debug_print("aht20: read data failed.\n"); /* read data failed */
436
437 return 1; /* return error */
438 }
439 if (a_aht20_calc_crc(buf, 6) != buf[6]) /* check the crc */
440 {
441 handle->debug_print("aht20: crc is error.\n"); /* crc is error */
442
443 return 5; /* return error */
444 }
445
446 *temperature_raw = (((uint32_t)buf[3]) << 16) |
447 (((uint32_t)buf[4]) << 8) |
448 (((uint32_t)buf[5]) << 0); /* set the temperature */
449 *temperature_raw = (*temperature_raw) & 0xFFFFF; /* cut the temperature part */
450 *temperature_s = (float)(*temperature_raw)
451 / 1048576.0f * 200.0f
452 - 50.0f; /* right shift 4 */
453
454 return 0; /* success return 0 */
455 }
456 else
457 {
458 handle->debug_print("aht20: data is not ready.\n"); /* data is not ready */
459
460 return 4; /* return error */
461 }
462}
463
478uint8_t aht20_read_humidity(aht20_handle_t *handle, uint32_t *humidity_raw, uint8_t *humidity_s)
479{
480 uint8_t status;
481 uint8_t buf[7];
482
483 if (handle == NULL) /* check handle */
484 {
485 return 2; /* return error */
486 }
487 if (handle->inited != 1) /* check handle initialization */
488 {
489 return 3; /* return error */
490 }
491
492 buf[0] = 0xAC; /* set the addr */
493 buf[1] = 0x33; /* set 0x33 */
494 buf[2] = 0x00; /* set 0x00 */
495 if (a_aht20_iic_write(handle, buf, 3) != 0) /* write the command */
496 {
497 handle->debug_print("aht20: sent command failed.\n"); /* sent command failed */
498
499 return 1; /* return error */
500 }
501 handle->delay_ms(85); /* delay 85ms */
502 if (a_aht20_iic_read(handle, &status, 1) != 0) /* read the status */
503 {
504 handle->debug_print("aht20: read status failed.\n"); /* read status failed */
505
506 return 1; /* return error */
507 }
508 if ((status & 0x80) == 0) /* check the status */
509 {
510 if (a_aht20_iic_read(handle, buf, 7) != 0) /* read data */
511 {
512 handle->debug_print("aht20: read data failed.\n"); /* read data failed */
513
514 return 1; /* return error */
515 }
516 if (a_aht20_calc_crc(buf, 6) != buf[6]) /* check the crc */
517 {
518 handle->debug_print("aht20: crc is error.\n"); /* crc is error */
519
520 return 5; /* return error */
521 }
522
523 *humidity_raw = (((uint32_t)buf[1]) << 16) |
524 (((uint32_t)buf[2]) << 8) |
525 (((uint32_t)buf[3]) << 0); /* set the humidity */
526 *humidity_raw = (*humidity_raw) >> 4; /* right shift 4 */
527 *humidity_s = (uint8_t)((float)(*humidity_raw)
528 / 1048576.0f * 100.0f); /* convert the humidity */
529
530 return 0; /* success return 0 */
531 }
532 else
533 {
534 handle->debug_print("aht20: data is not ready.\n"); /* data is not ready */
535
536 return 4; /* return error */
537 }
538}
539
552uint8_t aht20_set_reg(aht20_handle_t *handle, uint8_t *buf, uint16_t len)
553{
554 if (handle == NULL) /* check handle */
555 {
556 return 2; /* return error */
557 }
558 if (handle->inited != 1) /* check handle initialization */
559 {
560 return 3; /* return error */
561 }
562
563 if (a_aht20_iic_write(handle, buf, len) != 0) /* write data */
564 {
565 return 1; /* return error */
566 }
567 else
568 {
569 return 0; /* success return 0 */
570 }
571}
572
585uint8_t aht20_get_reg(aht20_handle_t *handle, uint8_t *buf, uint16_t len)
586{
587 if (handle == NULL) /* check handle */
588 {
589 return 2; /* return error */
590 }
591 if (handle->inited != 1) /* check handle initialization */
592 {
593 return 3; /* return error */
594 }
595
596 if (a_aht20_iic_read(handle, buf, len) != 0) /* read data */
597 {
598 return 1; /* return error */
599 }
600 else
601 {
602 return 0; /* success return 0 */
603 }
604}
605
615{
616 if (info == NULL) /* check handle */
617 {
618 return 2; /* return error */
619 }
620
621 memset(info, 0, sizeof(aht20_info_t)); /* initialize aht20 info structure */
622 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
623 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
624 strncpy(info->interface, "IIC", 8); /* copy interface name */
625 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
626 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
627 info->max_current_ma = MAX_CURRENT; /* set maximum current */
628 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
629 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
630 info->driver_version = DRIVER_VERSION; /* set driver version */
631
632 return 0; /* success return 0 */
633}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
#define AHT20_ADDRESS
chip address definition
driver aht20 header file
uint8_t aht20_read_humidity(aht20_handle_t *handle, uint32_t *humidity_raw, uint8_t *humidity_s)
read the humidity data
uint8_t aht20_info(aht20_info_t *info)
get chip's information
uint8_t aht20_deinit(aht20_handle_t *handle)
close the chip
uint8_t aht20_init(aht20_handle_t *handle)
initialize the chip
uint8_t aht20_read_temperature(aht20_handle_t *handle, uint32_t *temperature_raw, float *temperature_s)
read the temperature
uint8_t aht20_read_temperature_humidity(aht20_handle_t *handle, uint32_t *temperature_raw, float *temperature_s, uint32_t *humidity_raw, uint8_t *humidity_s)
read the temperature and humidity data
struct aht20_info_s aht20_info_t
aht20 information structure definition
struct aht20_handle_s aht20_handle_t
aht20 handle structure definition
uint8_t aht20_get_reg(aht20_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
uint8_t aht20_set_reg(aht20_handle_t *handle, uint8_t *buf, uint16_t len)
set the chip register
void(* delay_ms)(uint32_t ms)
void(* debug_print)(const char *const fmt,...)
uint8_t(* iic_init)(void)
uint8_t(* iic_read_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
uint8_t(* iic_deinit)(void)
uint8_t(* iic_write_cmd)(uint8_t addr, uint8_t *buf, uint16_t len)
float temperature_max
float supply_voltage_max_v
uint32_t driver_version
float temperature_min
float max_current_ma
char manufacturer_name[32]
float supply_voltage_min_v
char interface[8]
char chip_name[32]