LibDriver AHT30
Loading...
Searching...
No Matches
driver_aht30.c
Go to the documentation of this file.
1
36
37#include "driver_aht30.h"
38
42#define CHIP_NAME "ASAIR AHT30"
43#define MANUFACTURER_NAME "ASAIR"
44#define SUPPLY_VOLTAGE_MIN 2.2f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 0.60f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define AHT30_ADDRESS 0x70
55
66static uint8_t a_aht30_iic_read(aht30_handle_t *handle, uint8_t *data, uint16_t len)
67{
68 if (handle->iic_read_cmd(AHT30_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_aht30_iic_write(aht30_handle_t *handle, uint8_t *data, uint16_t len)
89{
90 if (handle->iic_write_cmd(AHT30_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_aht30_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_aht30_jh_reset_reg(aht30_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_aht30_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_aht30_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_aht30_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("aht30: 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("aht30: 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("aht30: 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("aht30: 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("aht30: 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("aht30: 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_aht30_iic_read(handle, &status, 1) != 0) /* read the status */
233 {
234 handle->debug_print("aht30: 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_aht30_jh_reset_reg(handle, 0x1B) != 0) /* reset the 0x1B */
242 {
243 handle->debug_print("aht30: 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_aht30_jh_reset_reg(handle, 0x1C) != 0) /* reset the 0x1C */
249 {
250 handle->debug_print("aht30: 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_aht30_jh_reset_reg(handle, 0x1E) != 0) /* reset the 0x1E */
256 {
257 handle->debug_print("aht30: 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("aht30: 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 aht30_read_temperature_humidity(aht30_handle_t *handle, uint32_t *temperature_raw, float *temperature_s,
318 uint32_t *humidity_raw, uint8_t *humidity_s)
319{
320 uint8_t buf[7];
321
322 if (handle == NULL) /* check handle */
323 {
324 return 2; /* return error */
325 }
326 if (handle->inited != 1) /* check handle initialization */
327 {
328 return 3; /* return error */
329 }
330
331 buf[0] = 0xAC; /* set the addr */
332 buf[1] = 0x33; /* set 0x33 */
333 buf[2] = 0x00; /* set 0x00 */
334 if (a_aht30_iic_write(handle, buf, 3) != 0) /* write the command */
335 {
336 handle->debug_print("aht30: sent command failed.\n"); /* sent command failed */
337
338 return 1; /* return error */
339 }
340 handle->delay_ms(85); /* delay 85ms */
341 if (a_aht30_iic_read(handle, buf, 7) != 0) /* read data */
342 {
343 handle->debug_print("aht30: read data failed.\n"); /* read data failed */
344
345 return 1; /* return error */
346 }
347 if ((buf[0] & 0x80) != 0) /* check busy */
348 {
349 handle->debug_print("aht30: data is not ready.\n"); /* data is not ready */
350
351 return 4; /* return error */
352 }
353 if (a_aht30_calc_crc(buf, 6) != buf[6]) /* check the crc */
354 {
355 handle->debug_print("aht30: crc is error.\n"); /* crc is error */
356
357 return 5; /* return error */
358 }
359
360 *humidity_raw = (((uint32_t)buf[1]) << 16) |
361 (((uint32_t)buf[2]) << 8) |
362 (((uint32_t)buf[3]) << 0); /* set the humidity */
363 *humidity_raw = (*humidity_raw) >> 4; /* right shift 4 */
364 *humidity_s = (uint8_t)((float)(*humidity_raw)
365 / 1048576.0f * 100.0f); /* convert the humidity */
366 *temperature_raw = (((uint32_t)buf[3]) << 16) |
367 (((uint32_t)buf[4]) << 8) |
368 (((uint32_t)buf[5]) << 0); /* set the temperature */
369 *temperature_raw = (*temperature_raw) & 0xFFFFF; /* cut the temperature part */
370 *temperature_s = (float)(*temperature_raw)
371 / 1048576.0f * 200.0f
372 - 50.0f; /* right shift 4 */
373
374 return 0; /* success return 0 */
375}
376
391uint8_t aht30_read_temperature(aht30_handle_t *handle, uint32_t *temperature_raw, float *temperature_s)
392{
393 uint8_t buf[7];
394
395 if (handle == NULL) /* check handle */
396 {
397 return 2; /* return error */
398 }
399 if (handle->inited != 1) /* check handle initialization */
400 {
401 return 3; /* return error */
402 }
403
404 buf[0] = 0xAC; /* set the addr */
405 buf[1] = 0x33; /* set 0x33 */
406 buf[2] = 0x00; /* set 0x00 */
407 if (a_aht30_iic_write(handle, buf, 3) != 0) /* write the command */
408 {
409 handle->debug_print("aht30: sent command failed.\n"); /* sent command failed */
410
411 return 1; /* return error */
412 }
413 handle->delay_ms(85); /* delay 85ms */
414 if (a_aht30_iic_read(handle, buf, 7) != 0) /* read data */
415 {
416 handle->debug_print("aht30: read data failed.\n"); /* read data failed */
417
418 return 1; /* return error */
419 }
420 if ((buf[0] & 0x80) != 0) /* check busy */
421 {
422 handle->debug_print("aht30: data is not ready.\n"); /* data is not ready */
423
424 return 4; /* return error */
425 }
426 if (a_aht30_calc_crc(buf, 6) != buf[6]) /* check the crc */
427 {
428 handle->debug_print("aht30: crc is error.\n"); /* crc is error */
429
430 return 5; /* return error */
431 }
432
433 *temperature_raw = (((uint32_t)buf[3]) << 16) |
434 (((uint32_t)buf[4]) << 8) |
435 (((uint32_t)buf[5]) << 0); /* set the temperature */
436 *temperature_raw = (*temperature_raw) & 0xFFFFF; /* cut the temperature part */
437 *temperature_s = (float)(*temperature_raw)
438 / 1048576.0f * 200.0f
439 - 50.0f; /* right shift 4 */
440
441 return 0; /* success return 0 */
442}
443
458uint8_t aht30_read_humidity(aht30_handle_t *handle, uint32_t *humidity_raw, uint8_t *humidity_s)
459{
460 uint8_t buf[7];
461
462 if (handle == NULL) /* check handle */
463 {
464 return 2; /* return error */
465 }
466 if (handle->inited != 1) /* check handle initialization */
467 {
468 return 3; /* return error */
469 }
470
471 buf[0] = 0xAC; /* set the addr */
472 buf[1] = 0x33; /* set 0x33 */
473 buf[2] = 0x00; /* set 0x00 */
474 if (a_aht30_iic_write(handle, buf, 3) != 0) /* write the command */
475 {
476 handle->debug_print("aht30: sent command failed.\n"); /* sent command failed */
477
478 return 1; /* return error */
479 }
480 handle->delay_ms(85); /* delay 85ms */
481 if (a_aht30_iic_read(handle, buf, 7) != 0) /* read data */
482 {
483 handle->debug_print("aht30: read data failed.\n"); /* read data failed */
484
485 return 1; /* return error */
486 }
487 if ((buf[0] & 0x80) != 0) /* check busy */
488 {
489 handle->debug_print("aht30: data is not ready.\n"); /* data is not ready */
490
491 return 4; /* return error */
492 }
493 if (a_aht30_calc_crc(buf, 6) != buf[6]) /* check the crc */
494 {
495 handle->debug_print("aht30: crc is error.\n"); /* crc is error */
496
497 return 5; /* return error */
498 }
499
500 *humidity_raw = (((uint32_t)buf[1]) << 16) |
501 (((uint32_t)buf[2]) << 8) |
502 (((uint32_t)buf[3]) << 0); /* set the humidity */
503 *humidity_raw = (*humidity_raw) >> 4; /* right shift 4 */
504 *humidity_s = (uint8_t)((float)(*humidity_raw)
505 / 1048576.0f * 100.0f); /* convert the humidity */
506
507 return 0; /* success return 0 */
508}
509
521uint8_t aht30_get_status(aht30_handle_t *handle, uint8_t *status)
522{
523 if (handle == NULL) /* check handle */
524 {
525 return 2; /* return error */
526 }
527 if (handle->inited != 1) /* check handle initialization */
528 {
529 return 3; /* return error */
530 }
531
532 if (a_aht30_iic_read(handle, status, 1) != 0) /* read data */
533 {
534 handle->debug_print("aht30: read data failed.\n"); /* read data failed */
535
536 return 1; /* return error */
537 }
538
539 return 0; /* success return 0 */
540}
541
554uint8_t aht30_set_reg(aht30_handle_t *handle, uint8_t *buf, uint16_t len)
555{
556 if (handle == NULL) /* check handle */
557 {
558 return 2; /* return error */
559 }
560 if (handle->inited != 1) /* check handle initialization */
561 {
562 return 3; /* return error */
563 }
564
565 if (a_aht30_iic_write(handle, buf, len) != 0) /* write data */
566 {
567 return 1; /* return error */
568 }
569 else
570 {
571 return 0; /* success return 0 */
572 }
573}
574
587uint8_t aht30_get_reg(aht30_handle_t *handle, uint8_t *buf, uint16_t len)
588{
589 if (handle == NULL) /* check handle */
590 {
591 return 2; /* return error */
592 }
593 if (handle->inited != 1) /* check handle initialization */
594 {
595 return 3; /* return error */
596 }
597
598 if (a_aht30_iic_read(handle, buf, len) != 0) /* read data */
599 {
600 return 1; /* return error */
601 }
602 else
603 {
604 return 0; /* success return 0 */
605 }
606}
607
617{
618 if (info == NULL) /* check handle */
619 {
620 return 2; /* return error */
621 }
622
623 memset(info, 0, sizeof(aht30_info_t)); /* initialize aht30 info structure */
624 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
625 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
626 strncpy(info->interface, "IIC", 8); /* copy interface name */
627 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
628 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
629 info->max_current_ma = MAX_CURRENT; /* set maximum current */
630 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
631 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
632 info->driver_version = DRIVER_VERSION; /* set driver version */
633
634 return 0; /* success return 0 */
635}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define AHT30_ADDRESS
chip address definition
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver aht30 header file
uint8_t aht30_deinit(aht30_handle_t *handle)
close the chip
uint8_t aht30_read_humidity(aht30_handle_t *handle, uint32_t *humidity_raw, uint8_t *humidity_s)
read the humidity data
uint8_t aht30_read_temperature(aht30_handle_t *handle, uint32_t *temperature_raw, float *temperature_s)
read the temperature
uint8_t aht30_init(aht30_handle_t *handle)
initialize the chip
uint8_t aht30_read_temperature_humidity(aht30_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 aht30_handle_s aht30_handle_t
aht30 handle structure definition
uint8_t aht30_info(aht30_info_t *info)
get chip's information
uint8_t aht30_get_status(aht30_handle_t *handle, uint8_t *status)
get status
struct aht30_info_s aht30_info_t
aht30 information structure definition
uint8_t aht30_get_reg(aht30_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
uint8_t aht30_set_reg(aht30_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]