pwrmeter 2.0.0.0
pwrmeter.h
Go to the documentation of this file.
1/*
2 * MikroSDK - MikroE Software Development Kit
3 * Copyright© 2020 MikroElektronika d.o.o.
4 *
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22 * OR OTHER DEALINGS IN THE SOFTWARE.
23 */
24
33// ----------------------------------------------------------------------------
34
35#ifndef PWRMETER_H
36#define PWRMETER_H
37
42#ifdef PREINIT_SUPPORTED
43#include "preinit.h"
44#endif
45
46#ifdef MikroCCoreVersion
47 #if MikroCCoreVersion >= 1
48 #include "delays.h"
49 #endif
50#endif
51
52#include "drv_digital_out.h"
53#include "drv_digital_in.h"
54#include "drv_uart.h"
55
56
57// -------------------------------------------------------------- PUBLIC MACROS
67#define PWRMETER_MAP_MIKROBUS( cfg, mikrobus ) \
68 cfg.tx_pin = MIKROBUS( mikrobus, MIKROBUS_TX ); \
69 cfg.rx_pin = MIKROBUS( mikrobus, MIKROBUS_RX ); \
70 cfg.en = MIKROBUS( mikrobus, MIKROBUS_CS )
77#define PWRMETER_RETVAL uint8_t
78#define PWRMETER_P const uint8_t*
79
80#define PWRMETER_OK 0x00
81#define PWRMETER_INIT_ERROR 0xFF
88#define PWRMETER_ERROR_COMMAND_OR_ELEMENT 0xFF
89#define PWRMETER_ERROR_START_OR_END_COMMAND 0x8F
90#define PWRMETER_ERROR_NEXT_ELEMENT 0x4F
91#define PWRMETER_ERROR_CURRENT_ELEMENT 0x2F
92#define PWRMETER_NO_ERROR 0x00
99#define PWRMETER_HEADER_BYTE 0xA5
106#define PWRMETER_REG_READ_COMM 0x4E
107#define PWRMETER_REG_WRITE_COMM 0x4D
108#define PWRMETER_SET_ADDR_POINTER_COMM 0x41
109#define PWRMETER_SAVE_TO_FLASH_COMM 0x53
110#define PWRMETER_EEPROM_READ_COMM 0x42
111#define PWRMETER_EEPROM_WRITE_COMM 0x50
112#define PWRMETER_EEPROM_ERASE_COMM 0x4F
113#define PWRMETER_GAIN_CALIB_COMM 0x5A
114#define PWRMETER_REACTIVE_GAIN_CALIB_COMM 0x7A
115#define PWRMETER_FREQ_CALIB_COMM 0x76
116#define PWRMETER_SAVE_ENERGY_TO_EEPROM_COMM 0x45
117
118
123#define PWRMETER_SUCCESSFUL 0x06
124#define PWRMETER_COMMAND_FAIL 0x15
125#define PWRMETER_CHECKSUM_FAIL 0x51
126#define PWRMETER_ADDRESS_FAIL 0x00
127#define PWRMETER_NBYTES_FAIL 0x01
128#define PWRMETER_PAGE_NUM_FAIL 0x02
129
134#define PWRMETER_16BIT_DATA 0x00
135#define PWRMETER_32BIT_DATA 0x01
136
141#define PWRMETER_DCMODE_MASK 0x01
142#define PWRMETER_DCCURR_SIGN_MASK 0x02
143#define PWRMETER_DCVOLT_SIGN_MASK 0x04
144#define PWRMETER_PR_SIGN_MASK 0x08
145#define PWRMETER_PA_SIGN_MASK 0x10
146
151#define PWRMETER_ENABLE 0x00
152#define PWRMETER_DISABLE 0x01
153
158#define PWRMETER_INSTR_POINTER_REG 0x0000
159#define PWRMETER_SYS_STATUS_REG 0x0002
160#define PWRMETER_SYS_VERSION_REG 0x0004
161#define PWRMETER_VOLT_RMS_REG 0x0006
162#define PWRMETER_LINE_FREQ_REG 0x0008
163#define PWRMETER_THERM_VOLT_REG 0x000A
164#define PWRMETER_PWR_FACTOR_REG 0x000C
165#define PWRMETER_CURR_RMS_REG 0x000E
166#define PWRMETER_ACTIVE_PWR_REG 0x0012
167#define PWRMETER_REACTIVE_PWR_REG 0x0016
168#define PWRMETER_APPARENT_PWR_REG 0x001A
169#define PWRMETER_IMPORT_ACTIVE_E_COUNT_REG 0x001E
170#define PWRMETER_EXPORT_ACTIVE_E_COUNT_REG 0x0026
171#define PWRMETER_IMPORT_REACTIVE_E_COUNT_REG 0x002E
172#define PWRMETER_EXPORT_REACTIVE_E_COUNT_REG 0x0036
173#define PWRMETER_MIN_RECORD_1_REG 0x003E
174#define PWRMETER_MIN_RECORD_2_REG 0x0042
175#define PWRMETER_MAX_RECORD_1_REG 0x0046
176#define PWRMETER_MAX_RECORD_2_REG 0x004A
177#define PWRMETER_CALIB_DELIMIT_REG 0x004E
178#define PWRMETER_GAIN_CURR_RMS_REG 0x0050
179#define PWRMETER_GAIN_VOLT_RMS_REG 0x0052
180#define PWRMETER_GAIN_ACTIVE_PWR_REG 0x0054
181#define PWRMETER_GAIN_REACTIVE_PWR_REG 0x0056
182#define PWRMETER_OFFSET_CURR_RMS_REG 0x005A
183#define PWRMETER_OFFSET_ACTIVE_PWR_REG 0x005C
184#define PWRMETER_OFFSET_REACTIVE_PWR_REG 0x005E
185#define PWRMETER_GAIN_LINE_FREQ_REG 0x0060
186#define PWRMETER_PHASE_COMP_REG 0x0062
187#define PWRMETER_VOLT_DROP_COMP_REG 0x0064
188#define PWRMETER_INCAP_CURR_COMP_REG 0x0066
189#define PWRMETER_RANGE_VDROP_COMP_REG 0x0068
190#define PWRMETER_DC_GAIN_CURR_RMS_REG 0x006C
191#define PWRMETER_DC_GAIN_VOLT_RMS_REG 0x006E
192#define PWRMETER_DC_GAIN_ACTIVE_PWR_REG 0x0070
193#define PWRMETER_DC_OFFSET_CURR_RMS_REG 0x0072
194#define PWRMETER_DC_OFFSET_AVTIVE_PWR_REG 0x0074
195#define PWRMETER_OFFCAL_MSB_REG 0x007A
196#define PWRMETER_OFFCAL_CH0_REG 0x007C
197#define PWRMETER_OFFCAL_CH1_REG 0x007E
198#define PWRMETER_TEMP_POS_COMP_FREQ_REG 0x0080
199#define PWRMETER_TEMP_NEG_COMP_FREQ_REG 0x0082
200#define PWRMETER_TEMP_POS_COMP_CURR_REG 0x0084
201#define PWRMETER_TEMP_NEG_COMP_CURR_REG 0x0086
202#define PWRMETER_TEMP_POS_COMP_PWR_REG 0x0088
203#define PWRMETER_TEMP_NEG_COMP_PWR_REG 0x008A
204#define PWRMETER_SYS_CONFIG_REG 0x0094
205#define PWRMETER_EVENT_CONFIG_REG 0x0098
206#define PWRMETER_RANGE_REG 0x009C
207#define PWRMETER_CALIB_CURR_REG 0x00A0
208#define PWRMETER_CALIB_VOLT_REG 0x00A4
209#define PWRMETER_CALIB_PWR_ACTIVE_REG 0x00A6
210#define PWRMETER_CALIB_PWR_REACTIVE_REG 0x00AA
211#define PWRMETER_APP_PWR_DIV_DIG_REG 0x00BE
212#define PWRMETER_ACCUM_INTER_PAR_REG 0x00C0
213#define PWRMETER_PWM_PERIOD_REG 0x00C2
214#define PWRMETER_PWM_DUTY_CYCLE_REG 0x00C4
215#define PWRMETER_MIN_MAX_POINTER1_REG 0x00C6
216#define PWRMETER_MIN_MAX_POINTER2_REG 0x00C8
217#define PWRMETER_LINE_FREQ_REF_REG 0x00CA
218#define PWRMETER_THERM_VOLT_CALIB_REG 0x00CC
219#define PWRMETER_VOLT_SAG_LIMIT_REG 0x00CE
220#define PWRMETER_VOLT_SURGE_LIMIT_REG 0x00D0
221#define PWRMETER_OVER_CURR_LIMIT_REG 0x00D2
222#define PWRMETER_OVER_PWR_LIMIT_REG 0x00D6
223#define PWRMETER_OVER_TEMP_LIMIT_REG 0x00DA
224#define PWRMETER_VOLT_LOW_THRESHOLD_REG 0x00DC
225#define PWRMETER_VOLT_HIGH_THRESHOLD_REG 0x00DE
226#define PWRMETER_NO_LOAD_THRESHOLD_REG 0x00E0
227
232#define PWRMETER_VOLT_GAIN_32 0x28000000
233#define PWRMETER_VOLT_GAIN_16 0x20000000
234#define PWRMETER_VOLT_GAIN_8 0x18000000
235#define PWRMETER_VOLT_GAIN_4 0x10000000
236#define PWRMETER_VOLT_GAIN_2 0x08000000
237#define PWRMETER_VOLT_GAIN_1 0x00000000
238#define PWRMETER_CURR_GAIN_32 0x05000000
239#define PWRMETER_CURR_GAIN_16 0x04000000
240#define PWRMETER_CURR_GAIN_8 0x03000000
241#define PWRMETER_CURR_GAIN_4 0x02000000
242#define PWRMETER_CURR_GAIN_2 0x01000000
243#define PWRMETER_CURR_GAIN_1 0x00000000
244#define PWRMETER_UART_BR_1200 0x0000E000
245#define PWRMETER_UART_BR_2400 0x0000C000
246#define PWRMETER_UART_BR_4800 0x0000A000
247#define PWRMETER_UART_BR_9600 0x00008000
248#define PWRMETER_UART_BR_19200 0x00006000
249#define PWRMETER_UART_BR_38400 0x00004000
250#define PWRMETER_UART_BR_57600 0x00002000
251#define PWRMETER_UART_BR_115200 0x00000000
252#define PWRMETER_ZCD_INVERTED 0x00001000
253#define PWRMETER_ZCD_OUTPUT_DISABLED 0x00000400
254#define PWRMETER_SINGLE_WIRE_EN 0x00000100
255#define PWRMETER_TEMP_COMP_EN 0x00000080
256#define PWRMETER_BOTH_RESET 0x00000060
257#define PWRMETER_VOLT_RESET 0x00000040
258#define PWRMETER_CURR_RESET 0x00000020
259#define PWRMETER_BOTH_SHUTDOWN 0x00000018
260#define PWRMETER_VOLT_SHUTDOWN 0x00000010
261#define PWRMETER_CURR_SHUTDOWN 0x00000008
262#define PWRMETER_INT_VOLT_REF_DISABLED 0x00000004
263#define PWRMETER_PWM_TURNED_ON 0x00000002
264#define PWRMETER_ENERGY_ON 0x00000001
265
270#define DRV_RX_BUFFER_SIZE 500
273 // End group macro
274// --------------------------------------------------------------- PUBLIC TYPES
282typedef struct
283{
284 // Output pins
285
286 digital_out_t en;
287
288 // Modules
289
290 uart_t uart;
291
292 char uart_rx_buffer[ DRV_RX_BUFFER_SIZE ];
293 char uart_tx_buffer[ DRV_RX_BUFFER_SIZE ];
294
295} pwrmeter_t;
296
300typedef struct
301{
302 // Communication gpio pins
303
304 pin_name_t rx_pin;
305 pin_name_t tx_pin;
306
307 // Additional gpio pins
308
309 pin_name_t en;
310
311 // static variable
312
313 uint32_t baud_rate; // Clock speed.
315 uart_data_bits_t data_bit; // Data bits.
316 uart_parity_t parity_bit; // Parity bit.
317 uart_stop_bits_t stop_bit; // Stop bits.
318
320
324typedef uint8_t pwrmeter_error_t;
325
326 // End types group
327// ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
328
334#ifdef __cplusplus
335extern "C"{
336#endif
337
347
357
366
377void pwrmeter_generic_write ( pwrmeter_t *ctx, char *data_buf, uint16_t len );
378
391int32_t pwrmeter_generic_read ( pwrmeter_t *ctx, char *data_buf, uint16_t max_len );
392
405PWRMETER_RETVAL pwrmeter_read_reg_bytes ( pwrmeter_t *ctx, uint16_t reg_addr, uint8_t n_bytes, uint8_t *data_out );
406
418PWRMETER_RETVAL pwrmeter_read_reg_word ( pwrmeter_t *ctx, uint16_t register_addr, uint16_t *data_out );
419
431PWRMETER_RETVAL pwrmeter_read_reg_dword ( pwrmeter_t *ctx, uint16_t register_addr, uint32_t *data_out );
432
445PWRMETER_RETVAL pwrmeter_read_reg_signed ( pwrmeter_t *ctx, uint16_t register_addr, uint8_t data_mode, int32_t *data_out );
446
459PWRMETER_RETVAL pwrmeter_write_reg_bytes ( pwrmeter_t *ctx, uint16_t reg_addr, uint8_t n_bytes, uint8_t *data_in );
460
472PWRMETER_RETVAL pwrmeter_write_reg_word ( pwrmeter_t *ctx, uint16_t register_addr, uint16_t data_in );
473
485PWRMETER_RETVAL pwrmeter_write_reg_dword ( pwrmeter_t *ctx, uint16_t register_addr, uint32_t data_in );
486
499PWRMETER_RETVAL pwrmeter_write_reg_signed ( pwrmeter_t *ctx, uint16_t register_addr, uint8_t data_mode, int32_t data_in );
500
511PWRMETER_RETVAL pwrmeter_send_command ( pwrmeter_t *ctx, uint8_t command_byte );
512
524PWRMETER_RETVAL pwrmeter_read_page_eeprom ( pwrmeter_t *ctx, uint8_t page_num, uint16_t *data_out );
525
537PWRMETER_RETVAL pwrmeter_write_page_eeprom ( pwrmeter_t *ctx, uint8_t page_num, uint16_t *data_in );
538
550PWRMETER_RETVAL pwrmeter_get_status ( pwrmeter_t *ctx, uint8_t *status_data );
551
560void pwrmeter_enable ( pwrmeter_t *ctx, uint8_t state );
561
562#ifdef __cplusplus
563}
564#endif
565#endif // _PWRMETER_H_
566
567 // End public_function group
569
570// ------------------------------------------------------------------------- END
#define DRV_RX_BUFFER_SIZE
Definition pwrmeter.h:270
#define PWRMETER_RETVAL
Definition pwrmeter.h:77
void pwrmeter_generic_write(pwrmeter_t *ctx, char *data_buf, uint16_t len)
Generic write function.
PWRMETER_RETVAL pwrmeter_read_reg_bytes(pwrmeter_t *ctx, uint16_t reg_addr, uint8_t n_bytes, uint8_t *data_out)
Bytes Read function.
PWRMETER_RETVAL pwrmeter_read_page_eeprom(pwrmeter_t *ctx, uint8_t page_num, uint16_t *data_out)
EEPROM Read function.
PWRMETER_RETVAL pwrmeter_read_reg_word(pwrmeter_t *ctx, uint16_t register_addr, uint16_t *data_out)
WORD Read function.
PWRMETER_RETVAL pwrmeter_read_reg_dword(pwrmeter_t *ctx, uint16_t register_addr, uint32_t *data_out)
DWORD Read function.
PWRMETER_RETVAL pwrmeter_write_reg_word(pwrmeter_t *ctx, uint16_t register_addr, uint16_t data_in)
WORD Write function.
PWRMETER_RETVAL pwrmeter_write_reg_bytes(pwrmeter_t *ctx, uint16_t reg_addr, uint8_t n_bytes, uint8_t *data_in)
Bytes Write function.
void pwrmeter_cfg_setup(pwrmeter_cfg_t *cfg)
Config Object Initialization function.
PWRMETER_RETVAL pwrmeter_write_reg_dword(pwrmeter_t *ctx, uint16_t register_addr, uint32_t data_in)
DWORD Write function.
PWRMETER_RETVAL pwrmeter_get_status(pwrmeter_t *ctx, uint8_t *status_data)
Status Get function.
PWRMETER_RETVAL pwrmeter_read_reg_signed(pwrmeter_t *ctx, uint16_t register_addr, uint8_t data_mode, int32_t *data_out)
Signed Data Read function.
void pwrmeter_enable(pwrmeter_t *ctx, uint8_t state)
Enable function.
PWRMETER_RETVAL pwrmeter_write_page_eeprom(pwrmeter_t *ctx, uint8_t page_num, uint16_t *data_in)
EEPROM Write function.
PWRMETER_RETVAL pwrmeter_write_reg_signed(pwrmeter_t *ctx, uint16_t register_addr, uint8_t data_mode, int32_t data_in)
Signed Data Write function.
void pwrmeter_default_cfg(pwrmeter_t *ctx)
Click Default Configuration function.
int32_t pwrmeter_generic_read(pwrmeter_t *ctx, char *data_buf, uint16_t max_len)
Generic read function.
PWRMETER_RETVAL pwrmeter_init(pwrmeter_t *ctx, pwrmeter_cfg_t *cfg)
Initialization function.
PWRMETER_RETVAL pwrmeter_send_command(pwrmeter_t *ctx, uint8_t command_byte)
Command Send function.
uint8_t pwrmeter_error_t
Error type.
Definition pwrmeter.h:324
Click configuration structure definition.
Definition pwrmeter.h:301
uint32_t baud_rate
Definition pwrmeter.h:313
bool uart_blocking
Definition pwrmeter.h:314
uart_data_bits_t data_bit
Definition pwrmeter.h:315
pin_name_t tx_pin
Definition pwrmeter.h:305
pin_name_t rx_pin
Definition pwrmeter.h:304
uart_stop_bits_t stop_bit
Definition pwrmeter.h:317
pin_name_t en
Definition pwrmeter.h:309
uart_parity_t parity_bit
Definition pwrmeter.h:316
Click ctx object definition.
Definition pwrmeter.h:283
uart_t uart
Definition pwrmeter.h:290
digital_out_t en
Definition pwrmeter.h:286