LibDriver HLW8032
Loading...
Searching...
No Matches
driver_hlw8032.h
Go to the documentation of this file.
1
36
37#ifndef DRIVER_HLW8032_H
38#define DRIVER_HLW8032_H
39
40#include <stdio.h>
41#include <stdint.h>
42#include <string.h>
43
44#ifdef __cplusplus
45extern "C"{
46#endif
47
53
57#ifndef HLW8032_VOLTAGE_COEFFICIENT
58 #define HLW8032_VOLTAGE_COEFFICIENT 1.88f
59#endif
60
64#ifndef HLW8032_CURRENT_COEFFICIENT
65 #define HLW8032_CURRENT_COEFFICIENT 1.0f
66#endif
67
71#ifndef HLW8032_VOLTAGE_K1
72 #define HLW8032_VOLTAGE_K1 2.045f
73#endif
74
78#ifndef HLW8032_CURRENT_K2
79 #define HLW8032_CURRENT_K2 1.0f
80#endif
81
85#ifndef HLW8032_POWER_K3
86 #define HLW8032_POWER_K3 2.045f
87#endif
88
93
102
113
124
146
150typedef struct hlw8032_handle_s
151{
152 uint8_t (*uart_init)(void);
153 uint8_t (*uart_deinit)(void);
154 uint16_t (*uart_read)(uint8_t *buf, uint16_t len);
155 uint8_t (*uart_flush)(void);
156 void (*delay_ms)(uint32_t ms);
157 void (*debug_print)(const char *const fmt, ...);
158 uint8_t inited;
159 uint8_t mode;
164 float power_k3;
165 uint8_t is_first;
166 int64_t pf_counter;
168 uint8_t pf_last_ovf;
170
186
190
197
204#define DRIVER_HLW8032_LINK_INIT(HANDLE, STRUCTURE) memset(HANDLE, 0, sizeof(STRUCTURE))
205
212#define DRIVER_HLW8032_LINK_UART_INIT(HANDLE, FUC) (HANDLE)->uart_init = FUC
213
220#define DRIVER_HLW8032_LINK_UART_DEINIT(HANDLE, FUC) (HANDLE)->uart_deinit = FUC
221
228#define DRIVER_HLW8032_LINK_UART_READ(HANDLE, FUC) (HANDLE)->uart_read = FUC
229
236#define DRIVER_HLW8032_LINK_UART_FLUSH(HANDLE, FUC) (HANDLE)->uart_flush = FUC
237
244#define DRIVER_HLW8032_LINK_DELAY_MS(HANDLE, FUC) (HANDLE)->delay_ms = FUC
245
252#define DRIVER_HLW8032_LINK_DEBUG_PRINT(HANDLE, FUC) (HANDLE)->debug_print = FUC
253
257
264
273uint8_t hlw8032_info(hlw8032_info_t *info);
274
285uint8_t hlw8032_init(hlw8032_handle_t *handle);
286
297uint8_t hlw8032_deinit(hlw8032_handle_t *handle);
298
312uint8_t hlw8032_read(hlw8032_handle_t *handle, hlw8032_data_t *data);
313
326
338uint8_t hlw8032_get_mode(hlw8032_handle_t *handle, hlw8032_mode_t *mode);
339
350uint8_t hlw8032_quantity_electricity_export(hlw8032_handle_t *handle, int64_t *quantity_electricity_counter);
351
364uint8_t hlw8032_quantity_electricity_import(hlw8032_handle_t *handle, int64_t quantity_electricity_counter);
365
377uint8_t hlw8032_set_sample_voltage_coefficient(hlw8032_handle_t *handle, float coeff);
378
390uint8_t hlw8032_get_sample_voltage_coefficient(hlw8032_handle_t *handle, float *coeff);
391
403uint8_t hlw8032_set_sample_current_coefficient(hlw8032_handle_t *handle, float coeff);
404
416uint8_t hlw8032_get_sample_current_coefficient(hlw8032_handle_t *handle, float *coeff);
417
429uint8_t hlw8032_set_transformer_voltage_k1(hlw8032_handle_t *handle, float coeff);
430
442uint8_t hlw8032_get_transformer_voltage_k1(hlw8032_handle_t *handle, float *coeff);
443
455uint8_t hlw8032_set_transformer_current_k2(hlw8032_handle_t *handle, float coeff);
456
468uint8_t hlw8032_get_transformer_current_k2(hlw8032_handle_t *handle, float *coeff);
469
481uint8_t hlw8032_set_transformer_power_k3(hlw8032_handle_t *handle, float coeff);
482
494uint8_t hlw8032_get_transformer_power_k3(hlw8032_handle_t *handle, float *coeff);
495
499
506
520uint8_t hlw8032_get_buffer(hlw8032_handle_t *handle, char *buf, uint16_t len, uint32_t ms);
521
525
529
530#ifdef __cplusplus
531}
532#endif
533
534#endif
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
hlw8032_update_status_t
hlw8032 update status enumeration definition
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
hlw8032_status_t
hlw8032 status 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
@ HLW8032_UPDATE_STATUS_CURRENT_UPDATED
@ HLW8032_UPDATE_STATUS_PF_OVERFLOW
@ HLW8032_UPDATE_STATUS_POWER_UPDATED
@ HLW8032_UPDATE_STATUS_VOLTAGE_UPDATED
@ HLW8032_MODE_SAMPLE
@ HLW8032_MODE_TRANSFORMER
@ HLW8032_STATUS_POWER_OVERFLOW
@ HLW8032_STATUS_VOLTAGE_OVERFLOW
@ HLW8032_STATUS_REGISTER_INVALID
@ HLW8032_STATUS_CURRENT_OVERFLOW
uint8_t hlw8032_get_buffer(hlw8032_handle_t *handle, char *buf, uint16_t len, uint32_t ms)
get buffer
hlw8032 data structure definition
float quantity_electricity_kwh
uint32_t voltage_parameter_raw
uint32_t current_parameter_raw
uint32_t power_parameter_raw
hlw8032 handle structure definition
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)
uint8_t(* uart_init)(void)
hlw8032 information structure definition
uint32_t driver_version
char manufacturer_name[32]