42#define CHIP_NAME "HLW TECHNOLOGY HLW8032"
43#define MANUFACTURER_NAME "HLW TECHNOLOGY"
44#define SUPPLY_VOLTAGE_MIN 4.5f
45#define SUPPLY_VOLTAGE_MAX 5.5f
46#define MAX_CURRENT 4.0f
47#define TEMPERATURE_MIN -40.0f
48#define TEMPERATURE_MAX 85.0f
49#define DRIVER_VERSION 1000
73 handle->
debug_print(
"hlw8032: uart_init is null.\n");
79 handle->
debug_print(
"hlw8032: uart_deinit is null.\n");
85 handle->
debug_print(
"hlw8032: uart_read is null.\n");
91 handle->
debug_print(
"hlw8032: uart_flush is null.\n");
97 handle->
debug_print(
"hlw8032: delay_ms is null.\n");
104 handle->
debug_print(
"hlw8032: uart init failed.\n");
146 handle->
debug_print(
"hlw8032: uart deinit failed.\n");
192 handle->
debug_print(
"hlw8032: uart flush failed.\n");
197 len = handle->
uart_read((uint8_t *)buf, 48);
200 handle->
debug_print(
"hlw8032: uart read failed.\n");
206 for (i = 0; i < 48; i++)
208 if ((i != 0) && (buf[i] == 0x5A) && ((i + 23) < 48))
211 for (j = 0; j < 21; j++)
213 check_sum += buf[i - 1 + 2 + j];
215 if (buf[i - 1 + 23] != (check_sum & 0xFF))
231 data->
status = buf[offset + 0];
233 ((uint32_t)buf[offset + 3] << 8) |
234 ((uint32_t)buf[offset + 4] << 0);
235 data->
voltage_raw = ((uint32_t)buf[offset + 5] << 16) |
236 ((uint32_t)buf[offset + 6] << 8) |
237 ((uint32_t)buf[offset + 7] << 0);
239 ((uint32_t)buf[offset + 9] << 8) |
240 ((uint32_t)buf[offset + 10] << 0);
241 data->
current_raw = ((uint32_t)buf[offset + 11] << 16) |
242 ((uint32_t)buf[offset + 12] << 8) |
243 ((uint32_t)buf[offset + 13] << 0);
245 ((uint32_t)buf[offset + 15] << 8) |
246 ((uint32_t)buf[offset + 16] << 0);
247 data->
power_raw = ((uint32_t)buf[offset + 17] << 16) |
248 ((uint32_t)buf[offset + 18] << 8) |
249 ((uint32_t)buf[offset + 19] << 0);
251 data->
pf_counter = ((uint16_t)buf[offset + 21] << 8) |
252 ((uint16_t)buf[offset + 22] << 0);
255 handle->
debug_print(
"hlw8032: data is invalid.\n");
284 if (handle->
mode != 0)
339 (1.0 / ((
double)(handle->
power_k3))) *
340 1000000000.0 * 3600.0;
402 1000000000.0 * 3600.0;
431 *quantity_electricity_counter = handle->
pf_counter;
455 uint8_t update_status;
473 handle->
debug_print(
"hlw8032: uart flush failed.\n");
478 len = handle->
uart_read((uint8_t *)buf, 48);
481 handle->
debug_print(
"hlw8032: uart read failed.\n");
487 for (i = 0; i < 48; i++)
489 if ((i != 0) && (buf[i] == 0x5A) && ((i + 23) < 48))
492 for (j = 0; j < 21; j++)
494 check_sum += buf[i - 1 + 2 + j];
496 if (buf[i - 1 + 23] != (check_sum & 0xFF))
513 pf_counter = ((uint16_t)buf[offset + 21] << 8) |
514 ((uint16_t)buf[offset + 22] << 0);
515 update_status = buf[offset + 20];
519 handle->
pf_counter = quantity_electricity_counter;
546 handle->
mode = (uint8_t)mode;
878 handle->
debug_print(
"hlw8032: uart flush failed.\n");
883 l = handle->
uart_read((uint8_t *)buf, len);
886 handle->
debug_print(
"hlw8032: uart read failed.\n");
#define SUPPLY_VOLTAGE_MAX
#define MANUFACTURER_NAME
#define SUPPLY_VOLTAGE_MIN
#define CHIP_NAME
chip information definition
driver hlw8032 header file
uint8_t hlw8032_set_transformer_current_k2(hlw8032_handle_t *handle, float coeff)
set transformer current k2
uint8_t hlw8032_read(hlw8032_handle_t *handle, hlw8032_data_t *data)
read the data
uint8_t hlw8032_get_mode(hlw8032_handle_t *handle, hlw8032_mode_t *mode)
get mode
uint8_t hlw8032_deinit(hlw8032_handle_t *handle)
close the chip
uint8_t hlw8032_get_sample_current_coefficient(hlw8032_handle_t *handle, float *coeff)
get sample current coefficient
uint8_t hlw8032_get_transformer_voltage_k1(hlw8032_handle_t *handle, float *coeff)
get transformer voltage k1
uint8_t hlw8032_init(hlw8032_handle_t *handle)
initialize the chip
uint8_t hlw8032_set_mode(hlw8032_handle_t *handle, hlw8032_mode_t mode)
set mode
struct hlw8032_info_s hlw8032_info_t
hlw8032 information structure definition
uint8_t hlw8032_info(hlw8032_info_t *info)
get chip's information
uint8_t hlw8032_set_transformer_voltage_k1(hlw8032_handle_t *handle, float coeff)
set transformer voltage k1
uint8_t hlw8032_get_transformer_current_k2(hlw8032_handle_t *handle, float *coeff)
get transformer current k2
uint8_t hlw8032_get_sample_voltage_coefficient(hlw8032_handle_t *handle, float *coeff)
get sample voltage coefficient
uint8_t hlw8032_quantity_electricity_export(hlw8032_handle_t *handle, int64_t *quantity_electricity_counter)
quantity electricity export
struct hlw8032_handle_s hlw8032_handle_t
hlw8032 handle structure definition
uint8_t hlw8032_get_transformer_power_k3(hlw8032_handle_t *handle, float *coeff)
get transformer power k3
uint8_t hlw8032_quantity_electricity_import(hlw8032_handle_t *handle, int64_t quantity_electricity_counter)
quantity electricity import
uint8_t hlw8032_set_transformer_power_k3(hlw8032_handle_t *handle, float coeff)
set transformer power k3
uint8_t hlw8032_set_sample_voltage_coefficient(hlw8032_handle_t *handle, float coeff)
set sample voltage coefficient
hlw8032_mode_t
hlw8032 mode enumeration definition
uint8_t hlw8032_set_sample_current_coefficient(hlw8032_handle_t *handle, float coeff)
set sample current coefficient
struct hlw8032_data_s hlw8032_data_t
hlw8032 data structure definition
#define HLW8032_CURRENT_COEFFICIENT
hlw8032 current coefficient definition
#define HLW8032_VOLTAGE_COEFFICIENT
hlw8032 voltage coefficient definition
#define HLW8032_CURRENT_K2
hlw8032 current k2 definition
#define HLW8032_VOLTAGE_K1
hlw8032 voltage k1 definition
#define HLW8032_POWER_K3
hlw8032 power k3 definition
uint8_t hlw8032_get_buffer(hlw8032_handle_t *handle, char *buf, uint16_t len, uint32_t ms)
get buffer
float quantity_electricity_kwh
uint32_t voltage_parameter_raw
float effective_current_a
uint32_t current_parameter_raw
float effective_voltage_v
uint32_t power_parameter_raw
uint8_t(* uart_flush)(void)
void(* delay_ms)(uint32_t ms)
uint8_t(* uart_deinit)(void)
void(* debug_print)(const char *const fmt,...)
uint16_t(* uart_read)(uint8_t *buf, uint16_t len)
float current_coefficient
uint8_t(* uart_init)(void)
float voltage_coefficient
float supply_voltage_max_v
char manufacturer_name[32]
float supply_voltage_min_v