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
32 extern "C"{
33 #endif
34 
35 #include "drv_digital_out.h"
36 #include "drv_digital_in.h"
37 #include "drv_i2c_master.h"
38 
59 #define PWRMETER3_REG_EEPROM_0B 0x0B
60 #define PWRMETER3_REG_EEPROM_0C 0x0C
61 #define PWRMETER3_REG_EEPROM_0D 0x0D
62 #define PWRMETER3_REG_EEPROM_0E 0x0E
63 #define PWRMETER3_REG_EEPROM_0F 0x0F
64 
72 #define PWRMETER3_REG_SHADOW_1B 0x1B
73 #define PWRMETER3_REG_SHADOW_1C 0x1C
74 #define PWRMETER3_REG_SHADOW_1D 0x1D
75 #define PWRMETER3_REG_SHADOW_1E 0x1E
76 #define PWRMETER3_REG_SHADOW_1F 0x1F
77 
82 #define PWRMETER3_REG_VOLATILE_20 0x20
83 #define PWRMETER3_REG_VOLATILE_21 0x21
84 #define PWRMETER3_REG_VOLATILE_22 0x22
85 #define PWRMETER3_REG_VOLATILE_25 0x25
86 #define PWRMETER3_REG_VOLATILE_26 0x26
87 #define PWRMETER3_REG_VOLATILE_27 0x27
88 #define PWRMETER3_REG_VOLATILE_28 0x28
89 #define PWRMETER3_REG_VOLATILE_29 0x29
90 #define PWRMETER3_REG_VOLATILE_2A 0x2A
91 #define PWRMETER3_REG_VOLATILE_2C 0x2C
92 #define PWRMETER3_REG_VOLATILE_2D 0x2D
93 #define PWRMETER3_REG_VOLATILE_2F 0x2F
94 #define PWRMETER3_REG_VOLATILE_30 0x30
95  // pwrmeter3_reg
97 
112 #define PWRMETER3_CUST_ACCESS_CODE 0x4F70656Eul
113 #define PWRMETER3_CUST_ACCESS_CODE_CLEAR 0x00000000ul
114 
119 #define PWRMETER3_VEVENT_CYCS_MASK 0x0000003Ful
120 #define PWRMETER3_OVERVREG_MASK 0x00003F00ul
121 #define PWRMETER3_UNDERVREG_MASK 0x000FC000ul
122 #define PWRMETER3_DELAYCNT_SEL_MASK 0x00100000ul
123 #define PWRMETER3_HALFCYCLC_EN_MASK 0x00200000ul
124 #define PWRMETER3_SQUAREWAVE_EN_MASK 0x00400000ul
125 #define PWRMETER3_ZEROCROSSCHANSEL_MASK 0x00800000ul
126 #define PWRMETER3_ZEROCROSSEDGESEL_MASK 0x01000000ul
127 #define PWRMETER3_ECC_MASK 0xFC000000ul
128 #define PWRMETER3_OVERVREG_MAX 63
129 #define PWRMETER3_UNDERVREG_MAX 63
130 #define PWRMETER3_OVERVREG_3 3
131 #define PWRMETER3_UNDERVREG_1 1
132 
137 #define PWRMETER3_I2C_SLV_ADDR_MASK 0x000001FCul
138 #define PWRMETER3_I2C_DIS_SLV_ADDR 0x00000200ul
139 #define PWRMETER3_DIO_0_SEL_MASK 0x00000C00ul
140 #define PWRMETER3_DIO_1_SEL_MASK 0x00003000ul
141 #define PWRMETER3_N_MASK 0x00FFC000ul
142 #define PWRMETER3_BYPASS_N_EN_MASK 0x01000000ul
143 #define PWRMETER3_DIO_0_SEL_ZC 0
144 #define PWRMETER3_DIO_0_SEL_OV 1
145 #define PWRMETER3_DIO_0_SEL_UV 2
146 #define PWRMETER3_DIO_0_SEL_OV_UV 3
147 #define PWRMETER3_DIO_1_SEL_OCF 0
148 #define PWRMETER3_DIO_1_SEL_UV 1
149 #define PWRMETER3_DIO_1_SEL_OV 2
150 #define PWRMETER3_DIO_1_SEL_OV_UV_OCF 3
151 #define PWRMETER3_BYPASS_N_DISABLE 0
152 #define PWRMETER3_BYPASS_N_ENABLE 1
153 #define PWRMETER3_MAX_NUM_SAMPLES 1023u
154 
159 #define PWRMETER3_RSENSE_OHM 2000.0f
160 #define PWRMETER3_AC_RDIV_OHM 2000000.0f
161 #define PWRMETER3_DC_RDIV_OHM 4000000.0f
162 #define PWRMETER3_ADC_FSCALE 55000u
163 #define PWRMETER3_DELTA_VIN_MAX_mV 250
164 #define PWRMETER3_mV_TO_V 1000u
165 #define PWRMETER3_MAX_CURRENT 90
166 #define PWRMETER3_DEF_AVG_SAMPLES 500u
167 
173 #define PWRMETER3_DEVICE_ADDRESS_0 0x60
174 #define PWRMETER3_DEVICE_ADDRESS_1 0x61
175 #define PWRMETER3_DEVICE_ADDRESS_2 0x64
176 #define PWRMETER3_DEVICE_ADDRESS_3 0x65
177  // pwrmeter3_set
179 
194 #define PWRMETER3_MAP_MIKROBUS( cfg, mikrobus ) \
195  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
196  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
197  cfg.dio1 = MIKROBUS( mikrobus, MIKROBUS_AN ); \
198  cfg.en = MIKROBUS( mikrobus, MIKROBUS_RST ); \
199  cfg.dio0 = MIKROBUS( mikrobus, MIKROBUS_INT )
200  // pwrmeter3_map // pwrmeter3
203 
208 typedef struct
209 {
210  // Output pins
211  digital_out_t en;
213  // Input pins
214  digital_in_t dio1;
215  digital_in_t dio0;
217  // Modules
218  i2c_master_t i2c;
220  // I2C slave address
221  uint8_t slave_address;
223  uint8_t bypass_n_en;
225 } pwrmeter3_t;
226 
231 typedef struct
232 {
233  pin_name_t scl;
234  pin_name_t sda;
236  pin_name_t dio1;
237  pin_name_t dio0;
238  pin_name_t en;
240  uint32_t i2c_speed;
241  uint8_t i2c_address;
244 
249 typedef enum
250 {
252  PWRMETER3_ERROR = -1
253 
255 
272 
287 
301 
315 err_t pwrmeter3_write_register ( pwrmeter3_t *ctx, uint8_t reg, uint32_t data_in );
316 
330 err_t pwrmeter3_read_register ( pwrmeter3_t *ctx, uint8_t reg, uint32_t *data_out );
331 
341 
351 
361 
371 
383 
395 
409 err_t pwrmeter3_set_bypass_n_state ( pwrmeter3_t *ctx, uint8_t state );
410 
423 err_t pwrmeter3_set_num_samples ( pwrmeter3_t *ctx, uint16_t num_samples );
424 
439 err_t pwrmeter3_set_dio0_sel ( pwrmeter3_t *ctx, uint8_t dio_sel );
440 
455 err_t pwrmeter3_set_dio1_sel ( pwrmeter3_t *ctx, uint8_t dio_sel );
456 
468 err_t pwrmeter3_set_ovrms_threshold ( pwrmeter3_t *ctx, uint8_t ovrms );
469 
481 err_t pwrmeter3_set_uvrms_threshold ( pwrmeter3_t *ctx, uint8_t uvrms );
482 
495 err_t pwrmeter3_read_rms ( pwrmeter3_t *ctx, float *v_rms, float *i_rms );
496 
511 err_t pwrmeter3_read_average_rms ( pwrmeter3_t *ctx, float *v_rms, float *i_rms, uint16_t num_avg );
512 
513 #ifdef __cplusplus
514 }
515 #endif
516 #endif // PWRMETER3_H
517  // pwrmeter3
519 
520 // ------------------------------------------------------------------------ END
pwrmeter3_cfg_t::dio1
pin_name_t dio1
Definition: pwrmeter3.h:236
pwrmeter3_set_bypass_n_state
err_t pwrmeter3_set_bypass_n_state(pwrmeter3_t *ctx, uint8_t state)
PWR Meter 3 set bypass n state function.
pwrmeter3_init
err_t pwrmeter3_init(pwrmeter3_t *ctx, pwrmeter3_cfg_t *cfg)
PWR Meter 3 initialization function.
pwrmeter3_set_ovrms_threshold
err_t pwrmeter3_set_ovrms_threshold(pwrmeter3_t *ctx, uint8_t ovrms)
PWR Meter 3 set ovrms threshold function.
PWRMETER3_OK
@ PWRMETER3_OK
Definition: pwrmeter3.h:251
pwrmeter3_get_dio1_pin
uint8_t pwrmeter3_get_dio1_pin(pwrmeter3_t *ctx)
PWR Meter 3 get dio1 pin function.
pwrmeter3_cfg_t::dio0
pin_name_t dio0
Definition: pwrmeter3.h:237
pwrmeter3_set_dio1_sel
err_t pwrmeter3_set_dio1_sel(pwrmeter3_t *ctx, uint8_t dio_sel)
PWR Meter 3 set dio1 sel function.
pwrmeter3_get_dio0_pin
uint8_t pwrmeter3_get_dio0_pin(pwrmeter3_t *ctx)
PWR Meter 3 get dio0 pin function.
pwrmeter3_read_register
err_t pwrmeter3_read_register(pwrmeter3_t *ctx, uint8_t reg, uint32_t *data_out)
PWR Meter 3 read register function.
pwrmeter3_cfg_t::scl
pin_name_t scl
Definition: pwrmeter3.h:233
pwrmeter3_t::bypass_n_en
uint8_t bypass_n_en
Definition: pwrmeter3.h:223
pwrmeter3_set_dio0_sel
err_t pwrmeter3_set_dio0_sel(pwrmeter3_t *ctx, uint8_t dio_sel)
PWR Meter 3 set dio0 sel function.
pwrmeter3_cfg_t::i2c_address
uint8_t i2c_address
Definition: pwrmeter3.h:241
pwrmeter3_allow_write_access
err_t pwrmeter3_allow_write_access(pwrmeter3_t *ctx)
PWR Meter 3 allow write access function.
pwrmeter3_default_cfg
err_t pwrmeter3_default_cfg(pwrmeter3_t *ctx)
PWR Meter 3 default configuration function.
pwrmeter3_t
PWR Meter 3 Click context object.
Definition: pwrmeter3.h:209
pwrmeter3_set_num_samples
err_t pwrmeter3_set_num_samples(pwrmeter3_t *ctx, uint16_t num_samples)
PWR Meter 3 set num samples function.
pwrmeter3_deny_write_access
err_t pwrmeter3_deny_write_access(pwrmeter3_t *ctx)
PWR Meter 3 deny write access function.
pwrmeter3_cfg_t::en
pin_name_t en
Definition: pwrmeter3.h:238
pwrmeter3_disable_device
void pwrmeter3_disable_device(pwrmeter3_t *ctx)
PWR Meter 3 disable device function.
PWRMETER3_ERROR
@ PWRMETER3_ERROR
Definition: pwrmeter3.h:252
pwrmeter3_cfg_setup
void pwrmeter3_cfg_setup(pwrmeter3_cfg_t *cfg)
PWR Meter 3 configuration object setup function.
pwrmeter3_return_value_t
pwrmeter3_return_value_t
PWR Meter 3 Click return value data.
Definition: pwrmeter3.h:250
pwrmeter3_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: pwrmeter3.h:240
pwrmeter3_enable_device
void pwrmeter3_enable_device(pwrmeter3_t *ctx)
PWR Meter 3 enable device function.
pwrmeter3_write_register
err_t pwrmeter3_write_register(pwrmeter3_t *ctx, uint8_t reg, uint32_t data_in)
PWR Meter 3 write register function.
pwrmeter3_t::en
digital_out_t en
Definition: pwrmeter3.h:211
pwrmeter3_read_average_rms
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.
pwrmeter3_t::dio1
digital_in_t dio1
Definition: pwrmeter3.h:214
pwrmeter3_t::i2c
i2c_master_t i2c
Definition: pwrmeter3.h:218
pwrmeter3_t::dio0
digital_in_t dio0
Definition: pwrmeter3.h:215
pwrmeter3_t::slave_address
uint8_t slave_address
Definition: pwrmeter3.h:221
pwrmeter3_cfg_t
PWR Meter 3 Click configuration object.
Definition: pwrmeter3.h:232
pwrmeter3_set_uvrms_threshold
err_t pwrmeter3_set_uvrms_threshold(pwrmeter3_t *ctx, uint8_t uvrms)
PWR Meter 3 set uvrms threshold function.
pwrmeter3_cfg_t::sda
pin_name_t sda
Definition: pwrmeter3.h:234
pwrmeter3_read_rms
err_t pwrmeter3_read_rms(pwrmeter3_t *ctx, float *v_rms, float *i_rms)
PWR Meter 3 read rms function.