LibDriver AHT10
Loading...
Searching...
No Matches
driver_aht10.c
Go to the documentation of this file.
1
36
37#include "driver_aht10.h"
38
42#define CHIP_NAME "ASAIR AHT10"
43#define MANUFACTURER_NAME "ASAIR"
44#define SUPPLY_VOLTAGE_MIN 1.8f
45#define SUPPLY_VOLTAGE_MAX 3.6f
46#define MAX_CURRENT 10.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
50
54#define AHT10_ADDRESS 0x70
55
66static uint8_t a_aht10_iic_read(aht10_handle_t *handle, uint8_t *data, uint16_t len)
67{
68 if (handle->iic_read_cmd(AHT10_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_aht10_iic_write(aht10_handle_t *handle, uint8_t *data, uint16_t len)
87{
88 if (handle->iic_write_cmd(AHT10_ADDRESS, data, len) != 0) /* write the register */
89 {
90 return 1; /* return error */
91 }
92
93 return 0; /* success return 0 */
94}
95
109{
110 uint8_t status;
111 uint8_t soft_reset = 0xBA;
112
113 if (handle == NULL) /* check handle */
114 {
115 return 2; /* return error */
116 }
117 if (handle->debug_print == NULL) /* check debug_print */
118 {
119 return 3; /* return error */
120 }
121 if (handle->iic_init == NULL) /* check iic_init */
122 {
123 handle->debug_print("aht10: iic_init is null.\n"); /* iic_init is null */
124
125 return 3; /* return error */
126 }
127 if (handle->iic_deinit == NULL) /* check iic_deinit */
128 {
129 handle->debug_print("aht10: iic_deinit is null.\n"); /* iic_deinit is null */
130
131 return 3; /* return error */
132 }
133 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
134 {
135 handle->debug_print("aht10: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
136
137 return 3; /* return error */
138 }
139 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
140 {
141 handle->debug_print("aht10: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
142
143 return 3; /* return error */
144 }
145 if (handle->delay_ms == NULL) /* check delay_ms */
146 {
147 handle->debug_print("aht10: delay_ms is null.\n"); /* delay_ms is null */
148
149 return 3; /* return error */
150 }
151
152 if (handle->iic_init() != 0) /* iic init */
153 {
154 handle->debug_print("aht10: iic init failed.\n"); /* iic init failed */
155
156 return 1; /* return error */
157 }
158 handle->delay_ms(50); /* wait for 50 ms */
159 if (a_aht10_iic_write(handle, &soft_reset, 1) != 0) /* write cmd */
160 {
161 handle->debug_print("aht10: reset failed.\n"); /* reset failed */
162 (void)handle->iic_deinit(); /* close the iic */
163
164 return 4; /* return error */
165 }
166 handle->delay_ms(50); /* wait for 50 ms */
167 if (a_aht10_iic_read(handle, &status, 1) != 0) /* read the status */
168 {
169 handle->debug_print("aht10: read status failed.\n"); /* read status failed */
170 (void)handle->iic_deinit(); /* close the iic */
171
172 return 4; /* return error */
173 }
174 if ((status & (1 << 3)) == 0) /* check the status */
175 {
176 uint8_t cmd[] = {0xE1, 0x08, 0x00};
177
178 if (a_aht10_iic_write(handle, cmd, 3) != 0) /* write cmd */
179 {
180 handle->debug_print("aht10: init failed.\n"); /* init failed */
181 (void)handle->iic_deinit(); /* close the iic */
182
183 return 5; /* return error */
184 }
185 handle->delay_ms(50); /* delay 50ms */
186 }
187 handle->inited = 1; /* flag finish initialization */
188
189 return 0; /* success return 0 */
190}
191
203{
204 if (handle == NULL) /* check handle */
205 {
206 return 2; /* return error */
207 }
208 if (handle->inited != 1) /* check handle initialization */
209 {
210 return 3; /* return error */
211 }
212
213 if (handle->iic_deinit() != 0) /* iic deinit */
214 {
215 handle->debug_print("aht10: iic deinit failed.\n"); /* iic deinit failed */
216
217 return 1; /* return error */
218 }
219 handle->inited = 0; /* set closed flag */
220
221 return 0; /* success return 0 */
222}
223
239uint8_t aht10_read_temperature_humidity(aht10_handle_t *handle, uint32_t *temperature_raw, float *temperature_s,
240 uint32_t *humidity_raw, uint8_t *humidity_s)
241{
242 uint8_t buf[6];
243
244 if (handle == NULL) /* check handle */
245 {
246 return 2; /* return error */
247 }
248 if (handle->inited != 1) /* check handle initialization */
249 {
250 return 3; /* return error */
251 }
252
253 buf[0] = 0xAC; /* set the addr */
254 buf[1] = 0x33; /* set 0x33 */
255 buf[2] = 0x00; /* set 0x00 */
256 if (a_aht10_iic_write(handle, buf, 3) != 0) /* write the command */
257 {
258 handle->debug_print("aht10: sent command failed.\n"); /* sent command failed */
259
260 return 1; /* return error */
261 }
262 handle->delay_ms(85); /* delay 85ms */
263 if (a_aht10_iic_read(handle, buf, 6) != 0) /* read data */
264 {
265 handle->debug_print("aht10: read data failed.\n"); /* read data failed */
266
267 return 1; /* return error */
268 }
269 if ((buf[0] & 0x80) == 0) /* check the status */
270 {
271 *humidity_raw = (((uint32_t)buf[1]) << 16) |
272 (((uint32_t)buf[2]) << 8) |
273 (((uint32_t)buf[3]) << 0); /* set the humidity */
274 *humidity_raw = (*humidity_raw) >> 4; /* right shift 4 */
275 *humidity_s = (uint8_t)((float)(*humidity_raw)
276 / 1048576.0f * 100.0f); /* convert the humidity */
277 *temperature_raw = (((uint32_t)buf[3]) << 16) |
278 (((uint32_t)buf[4]) << 8) |
279 (((uint32_t)buf[5]) << 0); /* set the temperature */
280 *temperature_raw = (*temperature_raw) & 0xFFFFF; /* cut the temperature part */
281 *temperature_s = (float)(*temperature_raw)
282 / 1048576.0f * 200.0f
283 - 50.0f; /* right shift 4 */
284
285 return 0; /* success return 0 */
286 }
287 else
288 {
289 handle->debug_print("aht10: data is not ready.\n"); /* data is not ready */
290
291 return 4; /* return error */
292 }
293}
294
308uint8_t aht10_read_temperature(aht10_handle_t *handle, uint32_t *temperature_raw, float *temperature_s)
309{
310 uint8_t buf[6];
311
312 if (handle == NULL) /* check handle */
313 {
314 return 2; /* return error */
315 }
316 if (handle->inited != 1) /* check handle initialization */
317 {
318 return 3; /* return error */
319 }
320
321 buf[0] = 0xAC; /* set the addr */
322 buf[1] = 0x33; /* set 0x33 */
323 buf[2] = 0x00; /* set 0x00 */
324 if (a_aht10_iic_write(handle, buf, 3) != 0) /* write the command */
325 {
326 handle->debug_print("aht10: sent command failed.\n"); /* sent command failed */
327
328 return 1; /* return error */
329 }
330 handle->delay_ms(85); /* delay 85ms */
331 if (a_aht10_iic_read(handle, buf, 6) != 0) /* read data */
332 {
333 handle->debug_print("aht10: read data failed.\n"); /* read data failed */
334
335 return 1; /* return error */
336 }
337 if ((buf[0] & 0x80) == 0) /* check the status */
338 {
339 *temperature_raw = (((uint32_t)buf[3]) << 16) |
340 (((uint32_t)buf[4]) << 8) |
341 (((uint32_t)buf[5]) << 0); /* set the temperature */
342 *temperature_raw = (*temperature_raw) & 0xFFFFF; /* cut the temperature part */
343 *temperature_s = (float)(*temperature_raw)
344 / 1048576.0f * 200.0f
345 - 50.0f; /* right shift 4 */
346
347 return 0; /* success return 0 */
348 }
349 else
350 {
351 handle->debug_print("aht10: data is not ready.\n"); /* data is not ready */
352
353 return 4; /* return error */
354 }
355}
356
370uint8_t aht10_read_humidity(aht10_handle_t *handle, uint32_t *humidity_raw, uint8_t *humidity_s)
371{
372 uint8_t buf[6];
373
374 if (handle == NULL) /* check handle */
375 {
376 return 2; /* return error */
377 }
378 if (handle->inited != 1) /* check handle initialization */
379 {
380 return 3; /* return error */
381 }
382
383 buf[0] = 0xAC; /* set the addr */
384 buf[1] = 0x33; /* set 0x33 */
385 buf[2] = 0x00; /* set 0x00 */
386 if (a_aht10_iic_write(handle, buf, 3) != 0) /* write the command */
387 {
388 handle->debug_print("aht10: sent command failed.\n"); /* sent command failed */
389
390 return 1; /* return error */
391 }
392 handle->delay_ms(85); /* delay 85ms */
393 if (a_aht10_iic_read(handle, buf, 6) != 0) /* read data */
394 {
395 handle->debug_print("aht10: read data failed.\n"); /* read data failed */
396
397 return 1; /* return error */
398 }
399 if ((buf[0] & 0x80) == 0) /* check the status */
400 {
401 *humidity_raw = (((uint32_t)buf[1]) << 16) |
402 (((uint32_t)buf[2]) << 8) |
403 (((uint32_t)buf[3]) << 0); /* set the humidity */
404 *humidity_raw = (*humidity_raw) >> 4; /* right shift 4 */
405 *humidity_s = (uint8_t)((float)(*humidity_raw)
406 / 1048576.0f * 100.0f); /* convert the humidity */
407
408 return 0; /* success return 0 */
409 }
410 else
411 {
412 handle->debug_print("aht10: data is not ready.\n"); /* data is not ready */
413
414 return 4; /* return error */
415 }
416}
417
430uint8_t aht10_set_reg(aht10_handle_t *handle, uint8_t *buf, uint16_t len)
431{
432 if (handle == NULL) /* check handle */
433 {
434 return 2; /* return error */
435 }
436 if (handle->inited != 1) /* check handle initialization */
437 {
438 return 3; /* return error */
439 }
440
441 if (a_aht10_iic_write(handle, buf, len) != 0) /* write data */
442 {
443 return 1; /* return error */
444 }
445 else
446 {
447 return 0; /* success return 0 */
448 }
449}
450
463uint8_t aht10_get_reg(aht10_handle_t *handle, uint8_t *buf, uint16_t len)
464{
465 if (handle == NULL) /* check handle */
466 {
467 return 2; /* return error */
468 }
469 if (handle->inited != 1) /* check handle initialization */
470 {
471 return 3; /* return error */
472 }
473
474 if (a_aht10_iic_read(handle, buf, len) != 0) /* read data */
475 {
476 return 1; /* return error */
477 }
478 else
479 {
480 return 0; /* success return 0 */
481 }
482}
483
493{
494 if (info == NULL) /* check handle */
495 {
496 return 2; /* return error */
497 }
498
499 memset(info, 0, sizeof(aht10_info_t)); /* initialize aht10 info structure */
500 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
501 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
502 strncpy(info->interface, "IIC", 8); /* copy interface name */
503 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
504 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
505 info->max_current_ma = MAX_CURRENT; /* set maximum current */
506 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
507 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
508 info->driver_version = DRIVER_VERSION; /* set driver version */
509
510 return 0; /* success return 0 */
511}
#define AHT10_ADDRESS
chip address definition
#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
driver aht10 header file
struct aht10_handle_s aht10_handle_t
aht10 handle structure definition
struct aht10_info_s aht10_info_t
aht10 information structure definition
uint8_t aht10_read_temperature(aht10_handle_t *handle, uint32_t *temperature_raw, float *temperature_s)
read the temperature
uint8_t aht10_init(aht10_handle_t *handle)
initialize the chip
uint8_t aht10_read_humidity(aht10_handle_t *handle, uint32_t *humidity_raw, uint8_t *humidity_s)
read the humidity data
uint8_t aht10_deinit(aht10_handle_t *handle)
close the chip
uint8_t aht10_read_temperature_humidity(aht10_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 aht10_info(aht10_info_t *info)
get chip's information
uint8_t aht10_set_reg(aht10_handle_t *handle, uint8_t *buf, uint16_t len)
set the chip register
uint8_t aht10_get_reg(aht10_handle_t *handle, uint8_t *buf, uint16_t len)
get 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]