pwrmeter3 2.1.0.0
pwrmeter3.h
Go to the documentation of this file.
1/****************************************************************************
2** Copyright (C) 2020 MikroElektronika d.o.o.
3** Contact: https://www.mikroe.com/contact
4**
5** Permission is hereby granted, free of charge, to any person obtaining a copy
6** of this software and associated documentation files (the "Software"), to deal
7** in the Software without restriction, including without limitation the rights
8** to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9** copies of the Software, and to permit persons to whom the Software is
10** furnished to do so, subject to the following conditions:
11** The above copyright notice and this permission notice shall be
12** included in all copies or substantial portions of the Software.
13**
14** THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
16** OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
18** DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
19** OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
20** USE OR OTHER DEALINGS IN THE SOFTWARE.
21****************************************************************************/
22
28#ifndef PWRMETER3_H
29#define PWRMETER3_H
30
31#ifdef __cplusplus
32extern "C"{
33#endif
34
39#ifdef PREINIT_SUPPORTED
40#include "preinit.h"
41#endif
42
43#ifdef MikroCCoreVersion
44 #if MikroCCoreVersion >= 1
45 #include "delays.h"
46 #endif
47#endif
48
49#include "drv_digital_out.h"
50#include "drv_digital_in.h"
51#include "drv_i2c_master.h"
52
73#define PWRMETER3_REG_EEPROM_0B 0x0B
74#define PWRMETER3_REG_EEPROM_0C 0x0C
75#define PWRMETER3_REG_EEPROM_0D 0x0D
76#define PWRMETER3_REG_EEPROM_0E 0x0E
77#define PWRMETER3_REG_EEPROM_0F 0x0F
78
86#define PWRMETER3_REG_SHADOW_1B 0x1B
87#define PWRMETER3_REG_SHADOW_1C 0x1C
88#define PWRMETER3_REG_SHADOW_1D 0x1D
89#define PWRMETER3_REG_SHADOW_1E 0x1E
90#define PWRMETER3_REG_SHADOW_1F 0x1F
91
96#define PWRMETER3_REG_VOLATILE_20 0x20
97#define PWRMETER3_REG_VOLATILE_21 0x21
98#define PWRMETER3_REG_VOLATILE_22 0x22
99#define PWRMETER3_REG_VOLATILE_25 0x25
100#define PWRMETER3_REG_VOLATILE_26 0x26
101#define PWRMETER3_REG_VOLATILE_27 0x27
102#define PWRMETER3_REG_VOLATILE_28 0x28
103#define PWRMETER3_REG_VOLATILE_29 0x29
104#define PWRMETER3_REG_VOLATILE_2A 0x2A
105#define PWRMETER3_REG_VOLATILE_2C 0x2C
106#define PWRMETER3_REG_VOLATILE_2D 0x2D
107#define PWRMETER3_REG_VOLATILE_2F 0x2F
108#define PWRMETER3_REG_VOLATILE_30 0x30
109
110 // pwrmeter3_reg
111
126#define PWRMETER3_CUST_ACCESS_CODE 0x4F70656Eul
127#define PWRMETER3_CUST_ACCESS_CODE_CLEAR 0x00000000ul
128
133#define PWRMETER3_VEVENT_CYCS_MASK 0x0000003Ful
134#define PWRMETER3_OVERVREG_MASK 0x00003F00ul
135#define PWRMETER3_UNDERVREG_MASK 0x000FC000ul
136#define PWRMETER3_DELAYCNT_SEL_MASK 0x00100000ul
137#define PWRMETER3_HALFCYCLC_EN_MASK 0x00200000ul
138#define PWRMETER3_SQUAREWAVE_EN_MASK 0x00400000ul
139#define PWRMETER3_ZEROCROSSCHANSEL_MASK 0x00800000ul
140#define PWRMETER3_ZEROCROSSEDGESEL_MASK 0x01000000ul
141#define PWRMETER3_ECC_MASK 0xFC000000ul
142#define PWRMETER3_OVERVREG_MAX 63
143#define PWRMETER3_UNDERVREG_MAX 63
144#define PWRMETER3_OVERVREG_3 3
145#define PWRMETER3_UNDERVREG_1 1
146
151#define PWRMETER3_I2C_SLV_ADDR_MASK 0x000001FCul
152#define PWRMETER3_I2C_DIS_SLV_ADDR 0x00000200ul
153#define PWRMETER3_DIO_0_SEL_MASK 0x00000C00ul
154#define PWRMETER3_DIO_1_SEL_MASK 0x00003000ul
155#define PWRMETER3_N_MASK 0x00FFC000ul
156#define PWRMETER3_BYPASS_N_EN_MASK 0x01000000ul
157#define PWRMETER3_DIO_0_SEL_ZC 0
158#define PWRMETER3_DIO_0_SEL_OV 1
159#define PWRMETER3_DIO_0_SEL_UV 2
160#define PWRMETER3_DIO_0_SEL_OV_UV 3
161#define PWRMETER3_DIO_1_SEL_OCF 0
162#define PWRMETER3_DIO_1_SEL_UV 1
163#define PWRMETER3_DIO_1_SEL_OV 2
164#define PWRMETER3_DIO_1_SEL_OV_UV_OCF 3
165#define PWRMETER3_BYPASS_N_DISABLE 0
166#define PWRMETER3_BYPASS_N_ENABLE 1
167#define PWRMETER3_MAX_NUM_SAMPLES 1023u
168
173#define PWRMETER3_RSENSE_OHM 2000.0f
174#define PWRMETER3_AC_RDIV_OHM 2000000.0f
175#define PWRMETER3_DC_RDIV_OHM 4000000.0f
176#define PWRMETER3_ADC_FSCALE 55000u
177#define PWRMETER3_DELTA_VIN_MAX_mV 250
178#define PWRMETER3_mV_TO_V 1000u
179#define PWRMETER3_MAX_CURRENT 90
180#define PWRMETER3_DEF_AVG_SAMPLES 500u
181
187#define PWRMETER3_DEVICE_ADDRESS_0 0x60
188#define PWRMETER3_DEVICE_ADDRESS_1 0x61
189#define PWRMETER3_DEVICE_ADDRESS_2 0x64
190#define PWRMETER3_DEVICE_ADDRESS_3 0x65
191
192 // pwrmeter3_set
193
208#define PWRMETER3_MAP_MIKROBUS( cfg, mikrobus ) \
209 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
210 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
211 cfg.dio1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
212 cfg.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
213 cfg.dio0 = MIKROBUS( mikrobus, MIKROBUS_INT )
214
215 // pwrmeter3_map
216 // pwrmeter3
217
222typedef struct
223{
224 // Output pins
225 digital_out_t en;
227 // Input pins
228 digital_in_t dio1;
229 digital_in_t dio0;
231 // Modules
232 i2c_master_t i2c;
234 // I2C slave address
237 uint8_t bypass_n_en;
240
245typedef struct
246{
247 pin_name_t scl;
248 pin_name_t sda;
250 pin_name_t dio1;
251 pin_name_t dio0;
252 pin_name_t en;
254 uint32_t i2c_speed;
255 uint8_t i2c_address;
258
269
286
301
315
329err_t pwrmeter3_write_register ( pwrmeter3_t *ctx, uint8_t reg, uint32_t data_in );
330
344err_t pwrmeter3_read_register ( pwrmeter3_t *ctx, uint8_t reg, uint32_t *data_out );
345
355
365
375
385
397
409
423err_t pwrmeter3_set_bypass_n_state ( pwrmeter3_t *ctx, uint8_t state );
424
437err_t pwrmeter3_set_num_samples ( pwrmeter3_t *ctx, uint16_t num_samples );
438
453err_t pwrmeter3_set_dio0_sel ( pwrmeter3_t *ctx, uint8_t dio_sel );
454
469err_t pwrmeter3_set_dio1_sel ( pwrmeter3_t *ctx, uint8_t dio_sel );
470
482err_t pwrmeter3_set_ovrms_threshold ( pwrmeter3_t *ctx, uint8_t ovrms );
483
495err_t pwrmeter3_set_uvrms_threshold ( pwrmeter3_t *ctx, uint8_t uvrms );
496
509err_t pwrmeter3_read_rms ( pwrmeter3_t *ctx, float *v_rms, float *i_rms );
510
525err_t pwrmeter3_read_average_rms ( pwrmeter3_t *ctx, float *v_rms, float *i_rms, uint16_t num_avg );
526
527#ifdef __cplusplus
528}
529#endif
530#endif // PWRMETER3_H
531
532 // pwrmeter3
533
534// ------------------------------------------------------------------------ END
err_t pwrmeter3_set_num_samples(pwrmeter3_t *ctx, uint16_t num_samples)
PWR Meter 3 set num samples function.
err_t pwrmeter3_default_cfg(pwrmeter3_t *ctx)
PWR Meter 3 default configuration function.
uint8_t pwrmeter3_get_dio1_pin(pwrmeter3_t *ctx)
PWR Meter 3 get dio1 pin function.
err_t pwrmeter3_init(pwrmeter3_t *ctx, pwrmeter3_cfg_t *cfg)
PWR Meter 3 initialization function.
void pwrmeter3_cfg_setup(pwrmeter3_cfg_t *cfg)
PWR Meter 3 configuration object setup function.
err_t pwrmeter3_read_average_rms(pwrmeter3_t *ctx, float *v_rms, float *i_rms, uint16_t num_avg)
PWR Meter 3 read average rms function.
err_t pwrmeter3_set_uvrms_threshold(pwrmeter3_t *ctx, uint8_t uvrms)
PWR Meter 3 set uvrms threshold function.
void pwrmeter3_enable_device(pwrmeter3_t *ctx)
PWR Meter 3 enable device function.
err_t pwrmeter3_set_dio1_sel(pwrmeter3_t *ctx, uint8_t dio_sel)
PWR Meter 3 set dio1 sel function.
err_t pwrmeter3_read_register(pwrmeter3_t *ctx, uint8_t reg, uint32_t *data_out)
PWR Meter 3 read register function.
void pwrmeter3_disable_device(pwrmeter3_t *ctx)
PWR Meter 3 disable device function.
err_t pwrmeter3_set_dio0_sel(pwrmeter3_t *ctx, uint8_t dio_sel)
PWR Meter 3 set dio0 sel function.
err_t pwrmeter3_read_rms(pwrmeter3_t *ctx, float *v_rms, float *i_rms)
PWR Meter 3 read rms function.
err_t pwrmeter3_write_register(pwrmeter3_t *ctx, uint8_t reg, uint32_t data_in)
PWR Meter 3 write register function.
err_t pwrmeter3_set_ovrms_threshold(pwrmeter3_t *ctx, uint8_t ovrms)
PWR Meter 3 set ovrms threshold function.
err_t pwrmeter3_set_bypass_n_state(pwrmeter3_t *ctx, uint8_t state)
PWR Meter 3 set bypass n state function.
err_t pwrmeter3_deny_write_access(pwrmeter3_t *ctx)
PWR Meter 3 deny write access function.
uint8_t pwrmeter3_get_dio0_pin(pwrmeter3_t *ctx)
PWR Meter 3 get dio0 pin function.
err_t pwrmeter3_allow_write_access(pwrmeter3_t *ctx)
PWR Meter 3 allow write access function.
pwrmeter3_return_value_t
PWR Meter 3 Click return value data.
Definition pwrmeter3.h:264
@ PWRMETER3_ERROR
Definition pwrmeter3.h:266
@ PWRMETER3_OK
Definition pwrmeter3.h:265
PWR Meter 3 Click configuration object.
Definition pwrmeter3.h:246
uint32_t i2c_speed
Definition pwrmeter3.h:254
pin_name_t scl
Definition pwrmeter3.h:247
pin_name_t en
Definition pwrmeter3.h:252
pin_name_t sda
Definition pwrmeter3.h:248
pin_name_t dio0
Definition pwrmeter3.h:251
pin_name_t dio1
Definition pwrmeter3.h:250
uint8_t i2c_address
Definition pwrmeter3.h:255
PWR Meter 3 Click context object.
Definition pwrmeter3.h:223
digital_in_t dio1
Definition pwrmeter3.h:228
uint8_t bypass_n_en
Definition pwrmeter3.h:237
i2c_master_t i2c
Definition pwrmeter3.h:232
digital_out_t en
Definition pwrmeter3.h:225
uint8_t slave_address
Definition pwrmeter3.h:235
digital_in_t dio0
Definition pwrmeter3.h:229