LibDriver AHT25
Loading...
Searching...
No Matches
driver_aht25.c
Go to the documentation of this file.
1
36
37#include "driver_aht25.h"
38
42#define CHIP_NAME "ASAIR AHT25"
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 80.0f
49#define DRIVER_VERSION 1000
50
54#define AHT25_ADDRESS 0x70
55
66static uint8_t a_aht25_iic_read(aht25_handle_t *handle, uint8_t *data, uint16_t len)
67{
68 if (handle->iic_read_cmd(AHT25_ADDRESS, data, len) != 0) /* read the register */
69 {
70 return 1; /* return error */
71 }
72
73 return 0; /* success return 0 */
74}
75
86static uint8_t a_aht25_iic_write(aht25_handle_t *handle, uint8_t *data, uint16_t len)
87{
88 if (handle->iic_write_cmd(AHT25_ADDRESS, data, len) != 0) /* write the register */
89 {
90 return 1; /* return error */
91 }
92
93 return 0; /* success return 0 */
94}
95
103static uint8_t a_aht25_calc_crc(uint8_t *data, uint8_t len)
104{
105 uint8_t i;
106 uint8_t byte;
107 uint8_t crc = 0xFF;
108
109 for (byte = 0; byte < len; byte++) /* len times */
110 {
111 crc ^= data[byte]; /* xor byte */
112 for (i = 8; i > 0; --i) /* one byte */
113 {
114 if ((crc & 0x80) != 0) /* if high*/
115 {
116 crc = (crc << 1) ^ 0x31; /* xor 0x31 */
117 }
118 else
119 {
120 crc = crc << 1; /* skip */
121 }
122 }
123 }
124
125 return crc; /* return crc */
126}
127
137static uint8_t a_aht25_jh_reset_reg(aht25_handle_t *handle, uint8_t addr)
138{
139 uint8_t buf[3];
140 uint8_t regs[3];
141
142 buf[0] = addr; /* set the addr */
143 buf[1] = 0x00; /* set 0x00 */
144 buf[2] = 0x00; /* set 0x00 */
145 if (a_aht25_iic_write(handle, buf, 3) != 0) /* write the command */
146 {
147 return 1; /* return error */
148 }
149 handle->delay_ms(5); /* delay 5ms */
150 if (a_aht25_iic_read(handle, regs, 3) != 0) /* read regs */
151 {
152 return 1; /* return error */
153 }
154 handle->delay_ms(10); /* delay 10ms */
155 buf[0] = 0xB0 | addr; /* set addr */
156 buf[1] = regs[1]; /* set regs[1] */
157 buf[2] = regs[2]; /* set regs[2] */
158 if (a_aht25_iic_write(handle, buf, 3) != 0) /* write the data */
159 {
160 return 1; /* return error */
161 }
162
163 return 0; /* success return 0 */
164}
165
179{
180 uint8_t status;
181
182 if (handle == NULL) /* check handle */
183 {
184 return 2; /* return error */
185 }
186 if (handle->debug_print == NULL) /* check debug_print */
187 {
188 return 3; /* return error */
189 }
190 if (handle->iic_init == NULL) /* check iic_init */
191 {
192 handle->debug_print("aht25: iic_init is null.\n"); /* iic_init is null */
193
194 return 3; /* return error */
195 }
196 if (handle->iic_deinit == NULL) /* check iic_deinit */
197 {
198 handle->debug_print("aht25: iic_deinit is null.\n"); /* iic_deinit is null */
199
200 return 3; /* return error */
201 }
202 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
203 {
204 handle->debug_print("aht25: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
205
206 return 3; /* return error */
207 }
208 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
209 {
210 handle->debug_print("aht25: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
211
212 return 3; /* return error */
213 }
214 if (handle->delay_ms == NULL) /* check delay_ms */
215 {
216 handle->debug_print("aht25: delay_ms is null.\n"); /* delay_ms is null */
217
218 return 3; /* return error */
219 }
220
221 if (handle->iic_init() != 0) /* iic init */
222 {
223 handle->debug_print("aht25: iic init failed.\n"); /* iic init failed */
224
225 return 1; /* return error */
226 }
227 handle->delay_ms(500); /* wait for 500 ms */
228 if (a_aht25_iic_read(handle, &status, 1) != 0) /* read the status */
229 {
230 handle->debug_print("aht25: read status failed.\n"); /* read status failed */
231 (void)handle->iic_deinit(); /* close the iic */
232
233 return 4; /* return error */
234 }
235 if ((status & 0x18) != 0x18) /* check the status */
236 {
237 if (a_aht25_jh_reset_reg(handle, 0x1B) != 0) /* reset the 0x1B */
238 {
239 handle->debug_print("aht25: reset reg failed.\n"); /* reset reg failed */
240 (void)handle->iic_deinit(); /* close the iic */
241
242 return 5; /* return error */
243 }
244 if (a_aht25_jh_reset_reg(handle, 0x1C) != 0) /* reset the 0x1C */
245 {
246 handle->debug_print("aht25: reset reg failed.\n"); /* reset reg failed */
247 (void)handle->iic_deinit(); /* close the iic */
248
249 return 5; /* return error */
250 }
251 if (a_aht25_jh_reset_reg(handle, 0x1E) != 0) /* reset the 0x1E */
252 {
253 handle->debug_print("aht25: reset reg failed.\n"); /* reset reg failed */
254 (void)handle->iic_deinit(); /* close the iic */
255
256 return 5; /* return error */
257 }
258 }
259 handle->delay_ms(10); /* delay 10ms */
260 handle->inited = 1; /* flag finish initialization */
261
262 return 0; /* success return 0 */
263}
264
276{
277 if (handle == NULL) /* check handle */
278 {
279 return 2; /* return error */
280 }
281 if (handle->inited != 1) /* check handle initialization */
282 {
283 return 3; /* return error */
284 }
285
286 if (handle->iic_deinit() != 0) /* iic deinit */
287 {
288 handle->debug_print("aht25: iic deinit failed.\n"); /* iic deinit failed */
289
290 return 1; /* return error */
291 }
292 handle->inited = 0; /* set closed flag */
293
294 return 0; /* success return 0 */
295}
296
313uint8_t aht25_read_temperature_humidity(aht25_handle_t *handle, uint32_t *temperature_raw, float *temperature_s,
314 uint32_t *humidity_raw, uint8_t *humidity_s)
315{
316 uint8_t status;
317 uint8_t buf[7];
318
319 if (handle == NULL) /* check handle */
320 {
321 return 2; /* return error */
322 }
323 if (handle->inited != 1) /* check handle initialization */
324 {
325 return 3; /* return error */
326 }
327
328 buf[0] = 0xAC; /* set the addr */
329 buf[1] = 0x33; /* set 0x33 */
330 buf[2] = 0x00; /* set 0x00 */
331 if (a_aht25_iic_write(handle, buf, 3) != 0) /* write the command */
332 {
333 handle->debug_print("aht25: sent command failed.\n"); /* sent command failed */
334
335 return 1; /* return error */
336 }
337 handle->delay_ms(85); /* delay 85ms */
338 if (a_aht25_iic_read(handle, &status, 1) != 0) /* read the status */
339 {
340 handle->debug_print("aht25: read status failed.\n"); /* read status failed */
341
342 return 1; /* return error */
343 }
344 if ((status & 0x80) == 0) /* check the status */
345 {
346 if (a_aht25_iic_read(handle, buf, 7) != 0) /* read data */
347 {
348 handle->debug_print("aht25: read data failed.\n"); /* read data failed */
349
350 return 1; /* return error */
351 }
352 if (a_aht25_calc_crc(buf, 6) != buf[6]) /* check the crc */
353 {
354 handle->debug_print("aht25: crc is error.\n"); /* crc is error */
355
356 return 5; /* return error */
357 }
358
359 *humidity_raw = (((uint32_t)buf[1]) << 16) |
360 (((uint32_t)buf[2]) << 8) |
361 (((uint32_t)buf[3]) << 0); /* set the humidity */
362 *humidity_raw = (*humidity_raw) >> 4; /* right shift 4 */
363 *humidity_s = (uint8_t)((float)(*humidity_raw)
364 / 1048576.0f * 100.0f); /* convert the humidity */
365 *temperature_raw = (((uint32_t)buf[3]) << 16) |
366 (((uint32_t)buf[4]) << 8) |
367 (((uint32_t)buf[5]) << 0); /* set the temperature */
368 *temperature_raw = (*temperature_raw) & 0xFFFFF; /* cut the temperature part */
369 *temperature_s = (float)(*temperature_raw)
370 / 1048576.0f * 200.0f
371 - 50.0f; /* right shift 4 */
372
373 return 0; /* success return 0 */
374 }
375 else
376 {
377 handle->debug_print("aht25: data is not ready.\n"); /* data is not ready */
378
379 return 4; /* return error */
380 }
381}
382
397uint8_t aht25_read_temperature(aht25_handle_t *handle, uint32_t *temperature_raw, float *temperature_s)
398{
399 uint8_t status;
400 uint8_t buf[7];
401
402 if (handle == NULL) /* check handle */
403 {
404 return 2; /* return error */
405 }
406 if (handle->inited != 1) /* check handle initialization */
407 {
408 return 3; /* return error */
409 }
410
411 buf[0] = 0xAC; /* set the addr */
412 buf[1] = 0x33; /* set 0x33 */
413 buf[2] = 0x00; /* set 0x00 */
414 if (a_aht25_iic_write(handle, buf, 3) != 0) /* write the command */
415 {
416 handle->debug_print("aht25: sent command failed.\n"); /* sent command failed */
417
418 return 1; /* return error */
419 }
420 handle->delay_ms(85); /* delay 85ms */
421 if (a_aht25_iic_read(handle, &status, 1) != 0) /* read the status */
422 {
423 handle->debug_print("aht25: read status failed.\n"); /* read status failed */
424
425 return 1; /* return error */
426 }
427 if ((status & 0x80) == 0) /* check the status */
428 {
429 if (a_aht25_iic_read(handle, buf, 7) != 0) /* read data */
430 {
431 handle->debug_print("aht25: read data failed.\n"); /* read data failed */
432
433 return 1; /* return error */
434 }
435 if (a_aht25_calc_crc(buf, 6) != buf[6]) /* check the crc */
436 {
437 handle->debug_print("aht25: crc is error.\n"); /* crc is error */
438
439 return 5; /* return error */
440 }
441
442 *temperature_raw = (((uint32_t)buf[3]) << 16) |
443 (((uint32_t)buf[4]) << 8) |
444 (((uint32_t)buf[5]) << 0); /* set the temperature */
445 *temperature_raw = (*temperature_raw) & 0xFFFFF; /* cut the temperature part */
446 *temperature_s = (float)(*temperature_raw)
447 / 1048576.0f * 200.0f
448 - 50.0f; /* right shift 4 */
449
450 return 0; /* success return 0 */
451 }
452 else
453 {
454 handle->debug_print("aht25: data is not ready.\n"); /* data is not ready */
455
456 return 4; /* return error */
457 }
458}
459
474uint8_t aht25_read_humidity(aht25_handle_t *handle, uint32_t *humidity_raw, uint8_t *humidity_s)
475{
476 uint8_t status;
477 uint8_t buf[7];
478
479 if (handle == NULL) /* check handle */
480 {
481 return 2; /* return error */
482 }
483 if (handle->inited != 1) /* check handle initialization */
484 {
485 return 3; /* return error */
486 }
487
488 buf[0] = 0xAC; /* set the addr */
489 buf[1] = 0x33; /* set 0x33 */
490 buf[2] = 0x00; /* set 0x00 */
491 if (a_aht25_iic_write(handle, buf, 3) != 0) /* write the command */
492 {
493 handle->debug_print("aht25: sent command failed.\n"); /* sent command failed */
494
495 return 1; /* return error */
496 }
497 handle->delay_ms(85); /* delay 85ms */
498 if (a_aht25_iic_read(handle, &status, 1) != 0) /* read the status */
499 {
500 handle->debug_print("aht25: read status failed.\n"); /* read status failed */
501
502 return 1; /* return error */
503 }
504 if ((status & 0x80) == 0) /* check the status */
505 {
506 if (a_aht25_iic_read(handle, buf, 7) != 0) /* read data */
507 {
508 handle->debug_print("aht25: read data failed.\n"); /* read data failed */
509
510 return 1; /* return error */
511 }
512 if (a_aht25_calc_crc(buf, 6) != buf[6]) /* check the crc */
513 {
514 handle->debug_print("aht25: crc is error.\n"); /* crc is error */
515
516 return 5; /* return error */
517 }
518
519 *humidity_raw = (((uint32_t)buf[1]) << 16) |
520 (((uint32_t)buf[2]) << 8) |
521 (((uint32_t)buf[3]) << 0); /* set the humidity */
522 *humidity_raw = (*humidity_raw) >> 4; /* right shift 4 */
523 *humidity_s = (uint8_t)((float)(*humidity_raw)
524 / 1048576.0f * 100.0f); /* convert the humidity */
525
526 return 0; /* success return 0 */
527 }
528 else
529 {
530 handle->debug_print("aht25: data is not ready.\n"); /* data is not ready */
531
532 return 4; /* return error */
533 }
534}
535
548uint8_t aht25_set_reg(aht25_handle_t *handle, uint8_t *buf, uint16_t len)
549{
550 if (handle == NULL) /* check handle */
551 {
552 return 2; /* return error */
553 }
554 if (handle->inited != 1) /* check handle initialization */
555 {
556 return 3; /* return error */
557 }
558
559 if (a_aht25_iic_write(handle, buf, len) != 0) /* write data */
560 {
561 return 1; /* return error */
562 }
563
564 return 0; /* success return 0 */
565}
566
579uint8_t aht25_get_reg(aht25_handle_t *handle, uint8_t *buf, uint16_t len)
580{
581 if (handle == NULL) /* check handle */
582 {
583 return 2; /* return error */
584 }
585 if (handle->inited != 1) /* check handle initialization */
586 {
587 return 3; /* return error */
588 }
589
590 if (a_aht25_iic_read(handle, buf, len) != 0) /* read data */
591 {
592 return 1; /* return error */
593 }
594
595 return 0; /* success return 0 */
596}
597
607{
608 if (info == NULL) /* check handle */
609 {
610 return 2; /* return error */
611 }
612
613 memset(info, 0, sizeof(aht25_info_t)); /* initialize aht25 info structure */
614 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
615 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
616 strncpy(info->interface, "IIC", 8); /* copy interface name */
617 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
618 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
619 info->max_current_ma = MAX_CURRENT; /* set maximum current */
620 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
621 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
622 info->driver_version = DRIVER_VERSION; /* set driver version */
623
624 return 0; /* success return 0 */
625}
#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 AHT25_ADDRESS
chip address definition
#define DRIVER_VERSION
driver aht25 header file
uint8_t aht25_info(aht25_info_t *info)
get chip's information
struct aht25_info_s aht25_info_t
aht25 information structure definition
uint8_t aht25_init(aht25_handle_t *handle)
initialize the chip
uint8_t aht25_read_temperature_humidity(aht25_handle_t *handle, uint32_t *temperature_raw, float *temperature_s, uint32_t *humidity_raw, uint8_t *humidity_s)
read the temperature and humidity data
uint8_t aht25_deinit(aht25_handle_t *handle)
close the chip
uint8_t aht25_read_humidity(aht25_handle_t *handle, uint32_t *humidity_raw, uint8_t *humidity_s)
read the humidity data
struct aht25_handle_s aht25_handle_t
aht25 handle structure definition
uint8_t aht25_read_temperature(aht25_handle_t *handle, uint32_t *temperature_raw, float *temperature_s)
read the temperature
uint8_t aht25_get_reg(aht25_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
uint8_t aht25_set_reg(aht25_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]