LibDriver AHT40
Loading...
Searching...
No Matches
driver_aht40.c
Go to the documentation of this file.
1
36
37#include "driver_aht40.h"
38
42#define CHIP_NAME "ASAIR AHT40"
43#define MANUFACTURER_NAME "ASAIR"
44#define SUPPLY_VOLTAGE_MIN 2.0f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 0.46f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 120.0f
49#define DRIVER_VERSION 1000
50
54#define AHT40_ADDRESS 0x88U
55
66static uint8_t a_aht40_iic_read(aht40_handle_t *handle, uint8_t *data, uint16_t len)
67{
68 if (handle->iic_read_cmd(AHT40_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_aht40_iic_write(aht40_handle_t *handle, uint8_t *data, uint16_t len)
87{
88 if (handle->iic_write_cmd(AHT40_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_aht40_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
141{
142 if (handle == NULL) /* check handle */
143 {
144 return 2; /* return error */
145 }
146 if (handle->debug_print == NULL) /* check debug_print */
147 {
148 return 3; /* return error */
149 }
150 if (handle->iic_init == NULL) /* check iic_init */
151 {
152 handle->debug_print("aht40: iic_init is null.\n"); /* iic_init is null */
153
154 return 3; /* return error */
155 }
156 if (handle->iic_deinit == NULL) /* check iic_deinit */
157 {
158 handle->debug_print("aht40: iic_deinit is null.\n"); /* iic_deinit is null */
159
160 return 3; /* return error */
161 }
162 if (handle->iic_read_cmd == NULL) /* check iic_read_cmd */
163 {
164 handle->debug_print("aht40: iic_read_cmd is null.\n"); /* iic_read_cmd is null */
165
166 return 3; /* return error */
167 }
168 if (handle->iic_write_cmd == NULL) /* check iic_write_cmd */
169 {
170 handle->debug_print("aht40: iic_write_cmd is null.\n"); /* iic_write_cmd is null */
171
172 return 3; /* return error */
173 }
174 if (handle->delay_ms == NULL) /* check delay_ms */
175 {
176 handle->debug_print("aht40: delay_ms is null.\n"); /* delay_ms is null */
177
178 return 3; /* return error */
179 }
180
181 if (handle->iic_init() != 0) /* iic init */
182 {
183 handle->debug_print("aht40: iic init failed.\n"); /* iic init failed */
184
185 return 1; /* return error */
186 }
187 handle->delay_ms(10); /* delay 10ms */
188 handle->inited = 1; /* flag finish initialization */
189
190 return 0; /* success return 0 */
191}
192
204{
205 if (handle == NULL) /* check handle */
206 {
207 return 2; /* return error */
208 }
209 if (handle->inited != 1) /* check handle initialization */
210 {
211 return 3; /* return error */
212 }
213
214 if (handle->iic_deinit() != 0) /* iic deinit */
215 {
216 handle->debug_print("aht40: iic deinit failed.\n"); /* iic deinit failed */
217
218 return 1; /* return error */
219 }
220 handle->inited = 0; /* set closed flag */
221
222 return 0; /* success return 0 */
223}
224
240uint8_t aht40_read_temperature_humidity(aht40_handle_t *handle, uint16_t *temperature_raw, float *temperature_s,
241 uint16_t *humidity_raw, float *humidity_s)
242{
243 uint8_t buf[6];
244
245 if (handle == NULL) /* check handle */
246 {
247 return 2; /* return error */
248 }
249 if (handle->inited != 1) /* check handle initialization */
250 {
251 return 3; /* return error */
252 }
253
254 buf[0] = 0xFD; /* set the command */
255 if (a_aht40_iic_write(handle, buf, 1) != 0) /* write the command */
256 {
257 handle->debug_print("aht40: sent command failed.\n"); /* sent command failed */
258
259 return 1; /* return error */
260 }
261 handle->delay_ms(85); /* delay 85ms */
262 if (a_aht40_iic_read(handle, buf, 6) != 0) /* read data */
263 {
264 handle->debug_print("aht40: read data failed.\n"); /* read data failed */
265
266 return 1; /* return error */
267 }
268 if (a_aht40_calc_crc(buf + 0, 2) != buf[2]) /* check the crc */
269 {
270 handle->debug_print("aht40: crc is error.\n"); /* crc is error */
271
272 return 4; /* return error */
273 }
274 if (a_aht40_calc_crc(buf + 3, 2) != buf[5]) /* check the crc */
275 {
276 handle->debug_print("aht40: crc is error.\n"); /* crc is error */
277
278 return 4; /* return error */
279 }
280
281 *humidity_raw = (((uint16_t)buf[3]) << 8) |
282 (((uint16_t)buf[4]) << 0); /* set the humidity */
283 *humidity_s = (float)(*humidity_raw) / 65535.0f *
284 125.0f - 6.0f; /* convert the humidity */
285 *temperature_raw = (((uint16_t)buf[0]) << 8) |
286 (((uint16_t)buf[1]) << 0); /* set the temperature */
287 *temperature_s = (float)(*temperature_raw) / 65535.0f *
288 175.0f - 45.0f; /* convert the temperature */
289
290 return 0; /* success return 0 */
291}
292
306uint8_t aht40_read_temperature(aht40_handle_t *handle, uint16_t *temperature_raw, float *temperature_s)
307{
308 uint8_t buf[6];
309
310 if (handle == NULL) /* check handle */
311 {
312 return 2; /* return error */
313 }
314 if (handle->inited != 1) /* check handle initialization */
315 {
316 return 3; /* return error */
317 }
318
319 buf[0] = 0xFD; /* set the command */
320 if (a_aht40_iic_write(handle, buf, 1) != 0) /* write the command */
321 {
322 handle->debug_print("aht40: sent command failed.\n"); /* sent command failed */
323
324 return 1; /* return error */
325 }
326 handle->delay_ms(85); /* delay 85ms */
327 if (a_aht40_iic_read(handle, buf, 6) != 0) /* read data */
328 {
329 handle->debug_print("aht40: read data failed.\n"); /* read data failed */
330
331 return 1; /* return error */
332 }
333 if (a_aht40_calc_crc(buf + 0, 2) != buf[2]) /* check the crc */
334 {
335 handle->debug_print("aht40: crc is error.\n"); /* crc is error */
336
337 return 4; /* return error */
338 }
339 if (a_aht40_calc_crc(buf + 3, 2) != buf[5]) /* check the crc */
340 {
341 handle->debug_print("aht40: crc is error.\n"); /* crc is error */
342
343 return 4; /* return error */
344 }
345
346 *temperature_raw = (((uint16_t)buf[0]) << 8) |
347 (((uint16_t)buf[1]) << 0); /* set the temperature */
348 *temperature_s = (float)(*temperature_raw) / 65535.0f *
349 175.0f - 45.0f; /* convert the temperature */
350
351 return 0; /* success return 0 */
352}
353
367uint8_t aht40_read_humidity(aht40_handle_t *handle, uint16_t *humidity_raw, float *humidity_s)
368{
369 uint8_t buf[6];
370
371 if (handle == NULL) /* check handle */
372 {
373 return 2; /* return error */
374 }
375 if (handle->inited != 1) /* check handle initialization */
376 {
377 return 3; /* return error */
378 }
379
380 buf[0] = 0xFD; /* set the command */
381 if (a_aht40_iic_write(handle, buf, 1) != 0) /* write the command */
382 {
383 handle->debug_print("aht40: sent command failed.\n"); /* sent command failed */
384
385 return 1; /* return error */
386 }
387 handle->delay_ms(85); /* delay 85ms */
388 if (a_aht40_iic_read(handle, buf, 6) != 0) /* read data */
389 {
390 handle->debug_print("aht40: read data failed.\n"); /* read data failed */
391
392 return 1; /* return error */
393 }
394 if (a_aht40_calc_crc(buf + 0, 2) != buf[2]) /* check the crc */
395 {
396 handle->debug_print("aht40: crc is error.\n"); /* crc is error */
397
398 return 4; /* return error */
399 }
400 if (a_aht40_calc_crc(buf + 3, 2) != buf[5]) /* check the crc */
401 {
402 handle->debug_print("aht40: crc is error.\n"); /* crc is error */
403
404 return 4; /* return error */
405 }
406
407 *humidity_raw = (((uint16_t)buf[3]) << 8) |
408 (((uint16_t)buf[4]) << 0); /* set the humidity */
409 *humidity_s = (float)(*humidity_raw) / 65535.0f *
410 125.0f - 6.0f; /* convert the humidity */
411
412 return 0; /* success return 0 */
413}
414
427uint8_t aht40_set_reg(aht40_handle_t *handle, uint8_t *buf, uint16_t len)
428{
429 if (handle == NULL) /* check handle */
430 {
431 return 2; /* return error */
432 }
433 if (handle->inited != 1) /* check handle initialization */
434 {
435 return 3; /* return error */
436 }
437
438 if (a_aht40_iic_write(handle, buf, len) != 0) /* write data */
439 {
440 return 1; /* return error */
441 }
442
443 return 0; /* success return 0 */
444}
445
458uint8_t aht40_get_reg(aht40_handle_t *handle, uint8_t *buf, uint16_t len)
459{
460 if (handle == NULL) /* check handle */
461 {
462 return 2; /* return error */
463 }
464 if (handle->inited != 1) /* check handle initialization */
465 {
466 return 3; /* return error */
467 }
468
469 if (a_aht40_iic_read(handle, buf, len) != 0) /* read data */
470 {
471 return 1; /* return error */
472 }
473
474 return 0; /* success return 0 */
475}
476
486{
487 if (info == NULL) /* check handle */
488 {
489 return 2; /* return error */
490 }
491
492 memset(info, 0, sizeof(aht40_info_t)); /* initialize aht40 info structure */
493 strncpy(info->chip_name, CHIP_NAME, 32); /* copy chip name */
494 strncpy(info->manufacturer_name, MANUFACTURER_NAME, 32); /* copy manufacturer name */
495 strncpy(info->interface, "IIC", 8); /* copy interface name */
496 info->supply_voltage_min_v = SUPPLY_VOLTAGE_MIN; /* set minimal supply voltage */
497 info->supply_voltage_max_v = SUPPLY_VOLTAGE_MAX; /* set maximum supply voltage */
498 info->max_current_ma = MAX_CURRENT; /* set maximum current */
499 info->temperature_max = TEMPERATURE_MAX; /* set minimal temperature */
500 info->temperature_min = TEMPERATURE_MIN; /* set maximum temperature */
501 info->driver_version = DRIVER_VERSION; /* set driver version */
502
503 return 0; /* success return 0 */
504}
#define MAX_CURRENT
#define SUPPLY_VOLTAGE_MAX
#define TEMPERATURE_MAX
#define MANUFACTURER_NAME
#define TEMPERATURE_MIN
#define SUPPLY_VOLTAGE_MIN
#define AHT40_ADDRESS
chip address definition
#define CHIP_NAME
chip information definition
#define DRIVER_VERSION
driver aht40 header file
struct aht40_info_s aht40_info_t
aht40 information structure definition
uint8_t aht40_init(aht40_handle_t *handle)
initialize the chip
uint8_t aht40_read_humidity(aht40_handle_t *handle, uint16_t *humidity_raw, float *humidity_s)
read the humidity data
uint8_t aht40_info(aht40_info_t *info)
get chip's information
struct aht40_handle_s aht40_handle_t
aht40 handle structure definition
uint8_t aht40_read_temperature(aht40_handle_t *handle, uint16_t *temperature_raw, float *temperature_s)
read the temperature
uint8_t aht40_read_temperature_humidity(aht40_handle_t *handle, uint16_t *temperature_raw, float *temperature_s, uint16_t *humidity_raw, float *humidity_s)
read the temperature and humidity data
uint8_t aht40_deinit(aht40_handle_t *handle)
close the chip
uint8_t aht40_get_reg(aht40_handle_t *handle, uint8_t *buf, uint16_t len)
get the chip register
uint8_t aht40_set_reg(aht40_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]