powermonitor2  2.1.0.0
powermonitor2.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 POWERMONITOR2_H
29 #define POWERMONITOR2_H
30 
31 #ifdef __cplusplus
32 extern "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 POWERMONITOR2_REG_CONFIGURATION 0x00
74 #define POWERMONITOR2_REG_SHUNT_VOLTAGE 0x01
75 #define POWERMONITOR2_REG_BUS_VOLTAGE 0x02
76 #define POWERMONITOR2_REG_POWER 0x03
77 #define POWERMONITOR2_REG_CURRENT 0x04
78 #define POWERMONITOR2_REG_CALIBRATION 0x05
79  // powermonitor2_reg
81 
96 #define POWERMONITOR2_CONFIGURATION_RST 0x8000
97 #define POWERMONITOR2_CONFIGURATION_BRNG_16V 0x0000
98 #define POWERMONITOR2_CONFIGURATION_BRNG_32V 0x2000
99 #define POWERMONITOR2_CONFIGURATION_BRNG_MASK 0x2000
100 #define POWERMONITOR2_CONFIGURATION_PGA_DIV_1 0x0000
101 #define POWERMONITOR2_CONFIGURATION_PGA_DIV_2 0x0800
102 #define POWERMONITOR2_CONFIGURATION_PGA_DIV_4 0x1000
103 #define POWERMONITOR2_CONFIGURATION_PGA_DIV_8 0x1800
104 #define POWERMONITOR2_CONFIGURATION_PGA_MASK 0x1800
105 #define POWERMONITOR2_CONFIGURATION_BADC_84_US 0x0000
106 #define POWERMONITOR2_CONFIGURATION_BADC_148_US 0x0080
107 #define POWERMONITOR2_CONFIGURATION_BADC_276_US 0x0100
108 #define POWERMONITOR2_CONFIGURATION_BADC_532_US 0x0180
109 #define POWERMONITOR2_CONFIGURATION_BADC_1_06_MS 0x0480
110 #define POWERMONITOR2_CONFIGURATION_BADC_2_13_MS 0x0500
111 #define POWERMONITOR2_CONFIGURATION_BADC_4_26_MS 0x0580
112 #define POWERMONITOR2_CONFIGURATION_BADC_8_51_MS 0x0600
113 #define POWERMONITOR2_CONFIGURATION_BADC_17_02_MS 0x0680
114 #define POWERMONITOR2_CONFIGURATION_BADC_34_05_MS 0x0700
115 #define POWERMONITOR2_CONFIGURATION_BADC_68_10_MS 0x0780
116 #define POWERMONITOR2_CONFIGURATION_BADC_MASK 0x0780
117 #define POWERMONITOR2_CONFIGURATION_SADC_84_US 0x0000
118 #define POWERMONITOR2_CONFIGURATION_SADC_148_US 0x0008
119 #define POWERMONITOR2_CONFIGURATION_SADC_276_US 0x0010
120 #define POWERMONITOR2_CONFIGURATION_SADC_532_US 0x0018
121 #define POWERMONITOR2_CONFIGURATION_SADC_1_06_MS 0x0048
122 #define POWERMONITOR2_CONFIGURATION_SADC_2_13_MS 0x0050
123 #define POWERMONITOR2_CONFIGURATION_SADC_4_26_MS 0x0058
124 #define POWERMONITOR2_CONFIGURATION_SADC_8_51_MS 0x0060
125 #define POWERMONITOR2_CONFIGURATION_SADC_17_02_MS 0x0068
126 #define POWERMONITOR2_CONFIGURATION_SADC_34_05_MS 0x0070
127 #define POWERMONITOR2_CONFIGURATION_SADC_68_10_MS 0x0078
128 #define POWERMONITOR2_CONFIGURATION_SADC_MASK 0x0078
129 #define POWERMONITOR2_CONFIGURATION_MODE_PDOWN 0x0000
130 #define POWERMONITOR2_CONFIGURATION_MODE_SHT_V_TRG 0x0001
131 #define POWERMONITOR2_CONFIGURATION_MODE_BUS_V_TRG 0x0002
132 #define POWERMONITOR2_CONFIGURATION_MODE_SHT_BUS_V_TRG 0x0003
133 #define POWERMONITOR2_CONFIGURATION_MODE_ADC_OFF 0x0004
134 #define POWERMONITOR2_CONFIGURATION_MODE_SHT_V_CONT 0x0005
135 #define POWERMONITOR2_CONFIGURATION_MODE_BUS_V_CONT 0x0006
136 #define POWERMONITOR2_CONFIGURATION_MODE_SHT_BUS_V_CONT 0x0007
137 #define POWERMONITOR2_CONFIGURATION_MODE_MASK 0x0007
138 
143 #define POWERMONITOR2_BUS_VOLTAGE_CNVR 0x0002
144 #define POWERMONITOR2_BUS_VOLTAGE_OVF 0x0001
145 
150 #define POWERMONITOR2_SHUNT_LSB_MV 0.01f
151 #define POWERMONITOR2_BUS_V_LSB_V 0.004f
152 #define POWERMONITOR2_POWER_LSB_W 0.02f
153 #define POWERMONITOR2_CURRENT_LSB_A 0.001f
154 #define POWERMONITOR2_CALIBRATION_FOR_SHUNT_0_01_OHM 4096
155 #define POWERMONITOR2_DEFAULT_NUM_CONV 20
156 
162 #define POWERMONITOR2_DEVICE_ADDRESS_3V3_0 0x40
163 #define POWERMONITOR2_DEVICE_ADDRESS_3V3_1 0x44
164 #define POWERMONITOR2_DEVICE_ADDRESS_5V_0 0x41
165 #define POWERMONITOR2_DEVICE_ADDRESS_5V_1 0x45
166  // powermonitor2_set
168 
183 #define POWERMONITOR2_MAP_MIKROBUS( cfg, mikrobus ) \
184  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
185  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA );
186  // powermonitor2_map // powermonitor2
189 
194 typedef struct
195 {
196  float shunt_mv;
197  float bus_v;
198  float current;
199  float power;
202 
207 typedef struct
208 {
209  // Modules
210  i2c_master_t i2c;
212  // I2C slave address
213  uint8_t slave_address;
215  uint8_t address_3v3;
216  uint8_t address_5v;
219 
224 typedef struct
225 {
226  pin_name_t scl;
227  pin_name_t sda;
229  uint32_t i2c_speed;
230  uint8_t i2c_address_3v3;
231  uint8_t i2c_address_5v;
234 
239 typedef enum
240 {
243 
245 
262 
277 
291 
303 err_t powermonitor2_set_address ( powermonitor2_t *ctx, uint8_t slave_address );
304 
317 err_t powermonitor2_write_reg ( powermonitor2_t *ctx, uint8_t reg, uint16_t data_in );
318 
331 err_t powermonitor2_read_reg ( powermonitor2_t *ctx, uint8_t reg, uint16_t *data_out );
332 
346 
361 err_t powermonitor2_read_data_avg ( powermonitor2_t *ctx, uint16_t num_conv, powermonitor2_data_t *data_out );
362 
363 #ifdef __cplusplus
364 }
365 #endif
366 #endif // POWERMONITOR2_H
367  // powermonitor2
369 
370 // ------------------------------------------------------------------------ END
POWERMONITOR2_ERROR
@ POWERMONITOR2_ERROR
Definition: powermonitor2.h:242
powermonitor2_data_t::bus_v
float bus_v
Definition: powermonitor2.h:197
powermonitor2_read_data_avg
err_t powermonitor2_read_data_avg(powermonitor2_t *ctx, uint16_t num_conv, powermonitor2_data_t *data_out)
Power Monitor 2 read data avg function.
powermonitor2_t::i2c
i2c_master_t i2c
Definition: powermonitor2.h:210
powermonitor2_cfg_t
Power Monitor 2 Click configuration object.
Definition: powermonitor2.h:225
powermonitor2_init
err_t powermonitor2_init(powermonitor2_t *ctx, powermonitor2_cfg_t *cfg)
Power Monitor 2 initialization function.
powermonitor2_read_reg
err_t powermonitor2_read_reg(powermonitor2_t *ctx, uint8_t reg, uint16_t *data_out)
Power Monitor 2 read reg function.
powermonitor2_t::slave_address
uint8_t slave_address
Definition: powermonitor2.h:213
powermonitor2_t
Power Monitor 2 Click context object.
Definition: powermonitor2.h:208
powermonitor2_write_reg
err_t powermonitor2_write_reg(powermonitor2_t *ctx, uint8_t reg, uint16_t data_in)
Power Monitor 2 write reg function.
powermonitor2_return_value_t
powermonitor2_return_value_t
Power Monitor 2 Click return value data.
Definition: powermonitor2.h:240
powermonitor2_t::address_5v
uint8_t address_5v
Definition: powermonitor2.h:216
powermonitor2_data_t::current
float current
Definition: powermonitor2.h:198
powermonitor2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: powermonitor2.h:229
powermonitor2_data_t::power
float power
Definition: powermonitor2.h:199
powermonitor2_read_data
err_t powermonitor2_read_data(powermonitor2_t *ctx, powermonitor2_data_t *data_out)
Power Monitor 2 read data function.
powermonitor2_cfg_t::i2c_address_5v
uint8_t i2c_address_5v
Definition: powermonitor2.h:231
powermonitor2_cfg_t::sda
pin_name_t sda
Definition: powermonitor2.h:227
POWERMONITOR2_OK
@ POWERMONITOR2_OK
Definition: powermonitor2.h:241
powermonitor2_data_t::shunt_mv
float shunt_mv
Definition: powermonitor2.h:196
powermonitor2_cfg_t::scl
pin_name_t scl
Definition: powermonitor2.h:226
powermonitor2_default_cfg
err_t powermonitor2_default_cfg(powermonitor2_t *ctx)
Power Monitor 2 default configuration function.
powermonitor2_t::address_3v3
uint8_t address_3v3
Definition: powermonitor2.h:215
powermonitor2_set_address
err_t powermonitor2_set_address(powermonitor2_t *ctx, uint8_t slave_address)
Power Monitor 2 set slave address function.
powermonitor2_cfg_t::i2c_address_3v3
uint8_t i2c_address_3v3
Definition: powermonitor2.h:230
powermonitor2_cfg_setup
void powermonitor2_cfg_setup(powermonitor2_cfg_t *cfg)
Power Monitor 2 configuration object setup function.
powermonitor2_data_t
Power Monitor 2 Click data object.
Definition: powermonitor2.h:195