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
54#define AHT20_ADDRESS 0x70
66static uint8_t a_aht20_iic_read(
aht20_handle_t *handle, uint8_t *data, uint16_t len)
88static uint8_t a_aht20_iic_write(
aht20_handle_t *handle, uint8_t *data, uint16_t len)
107static uint8_t a_aht20_calc_crc(uint8_t *data, uint8_t len)
113 for (
byte = 0;
byte < len;
byte++)
116 for (i = 8; i > 0; --i)
118 if ((crc & 0x80) != 0)
120 crc = (crc << 1) ^ 0x31;
141static uint8_t a_aht20_jh_reset_reg(
aht20_handle_t *handle, uint8_t addr)
149 if (a_aht20_iic_write(handle, buf, 3) != 0)
154 if (a_aht20_iic_read(handle, regs, 3) != 0)
159 buf[0] = 0xB0 | addr;
162 if (a_aht20_iic_write(handle, buf, 3) != 0)
202 handle->
debug_print(
"aht20: iic_deinit is null.\n");
208 handle->
debug_print(
"aht20: iic_read_cmd is null.\n");
214 handle->
debug_print(
"aht20: iic_write_cmd is null.\n");
232 if (a_aht20_iic_read(handle, &status, 1) != 0)
234 handle->
debug_print(
"aht20: read status failed.\n");
239 if ((status & 0x18) != 0x18)
241 if (a_aht20_jh_reset_reg(handle, 0x1B) != 0)
248 if (a_aht20_jh_reset_reg(handle, 0x1C) != 0)
255 if (a_aht20_jh_reset_reg(handle, 0x1E) != 0)
292 handle->
debug_print(
"aht20: iic deinit failed.\n");
318 uint32_t *humidity_raw, uint8_t *humidity_s)
335 if (a_aht20_iic_write(handle, buf, 3) != 0)
337 handle->
debug_print(
"aht20: sent command failed.\n");
342 if (a_aht20_iic_read(handle, &status, 1) != 0)
344 handle->
debug_print(
"aht20: read status failed.\n");
348 if ((status & 0x80) == 0)
350 if (a_aht20_iic_read(handle, buf, 7) != 0)
356 if (a_aht20_calc_crc(buf, 6) != buf[6])
363 *humidity_raw = (((uint32_t)buf[1]) << 16) |
364 (((uint32_t)buf[2]) << 8) |
365 (((uint32_t)buf[3]) << 0);
366 *humidity_raw = (*humidity_raw) >> 4;
367 *humidity_s = (uint8_t)((
float)(*humidity_raw)
368 / 1048576.0f * 100.0f);
369 *temperature_raw = (((uint32_t)buf[3]) << 16) |
370 (((uint32_t)buf[4]) << 8) |
371 (((uint32_t)buf[5]) << 0);
372 *temperature_raw = (*temperature_raw) & 0xFFFFF;
373 *temperature_s = (float)(*temperature_raw)
374 / 1048576.0f * 200.0f
381 handle->
debug_print(
"aht20: data is not ready.\n");
418 if (a_aht20_iic_write(handle, buf, 3) != 0)
420 handle->
debug_print(
"aht20: sent command failed.\n");
425 if (a_aht20_iic_read(handle, &status, 1) != 0)
427 handle->
debug_print(
"aht20: read status failed.\n");
431 if ((status & 0x80) == 0)
433 if (a_aht20_iic_read(handle, buf, 7) != 0)
439 if (a_aht20_calc_crc(buf, 6) != buf[6])
446 *temperature_raw = (((uint32_t)buf[3]) << 16) |
447 (((uint32_t)buf[4]) << 8) |
448 (((uint32_t)buf[5]) << 0);
449 *temperature_raw = (*temperature_raw) & 0xFFFFF;
450 *temperature_s = (float)(*temperature_raw)
451 / 1048576.0f * 200.0f
458 handle->
debug_print(
"aht20: data is not ready.\n");
495 if (a_aht20_iic_write(handle, buf, 3) != 0)
497 handle->
debug_print(
"aht20: sent command failed.\n");
502 if (a_aht20_iic_read(handle, &status, 1) != 0)
504 handle->
debug_print(
"aht20: read status failed.\n");
508 if ((status & 0x80) == 0)
510 if (a_aht20_iic_read(handle, buf, 7) != 0)
516 if (a_aht20_calc_crc(buf, 6) != buf[6])
523 *humidity_raw = (((uint32_t)buf[1]) << 16) |
524 (((uint32_t)buf[2]) << 8) |
525 (((uint32_t)buf[3]) << 0);
526 *humidity_raw = (*humidity_raw) >> 4;
527 *humidity_s = (uint8_t)((
float)(*humidity_raw)
528 / 1048576.0f * 100.0f);
534 handle->
debug_print(
"aht20: data is not ready.\n");
563 if (a_aht20_iic_write(handle, buf, len) != 0)
596 if (a_aht20_iic_read(handle, buf, len) != 0)
#define SUPPLY_VOLTAGE_MAX
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
#define AHT20_ADDRESS
chip address definition
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 supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v