powermonitor  2.0.0.0
powermonitor.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 POWERMONITOR_H
29 #define POWERMONITOR_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 
39 
40 
41 
62 #define POWERMONITOR_REG_CONFIG 0x00
63 #define POWERMONITOR_REG_ADCCONFIG 0x01
64 #define POWERMONITOR_REG_SHUNT_CAL 0x02
65 #define POWERMONITOR_REG_SHUNT_TEMPCO 0x03
66 #define POWERMONITOR_REG_VSHUNT 0x04
67 #define POWERMONITOR_REG_VBUS 0x05
68 #define POWERMONITOR_REG_DIETEMP 0x06
69 #define POWERMONITOR_REG_CURRENT 0x07
70 #define POWERMONITOR_REG_POWER 0x08
71 #define POWERMONITOR_REG_ENERGY 0x09
72 #define POWERMONITOR_REG_CHARGE 0x0A
73 #define POWERMONITOR_REG_DIAG_ALR 0x0B
74 #define POWERMONITOR_REG_SOVL 0x0C
75 #define POWERMONITOR_REG_SUVL 0x0D
76 #define POWERMONITOR_REG_BOVL 0x0E
77 #define POWERMONITOR_REG_BUVL 0x0F
78 #define POWERMONITOR_REG_TEMP_LIMIT 0x10
79 #define POWERMONITOR_REG_PWR_LIMIT 0x11
80 #define POWERMONITOR_REG_MANUFACTURER_ID 0x3E
81 #define POWERMONITOR_REG_DEVICE_ID 0x3F
82  // powermonitor_reg
84 
99 #define POWERMONITOR_RST_RESET_BIT 0x80
100 #define POWERMONITOR_RSTACC_NORMAL_OPERATION 0x00
101 #define POWERMONITOR_RSTACC_CLEARS_ENERGY_CHARGE_REG 0x01
102 #define POWERMONITOR_CONVDLY_0_s 0x00
103 #define POWERMONITOR_TEMPCOMP_DISABLED 0x00
104 #define POWERMONITOR_TEMPCOMP_ENABLED 0x01
105 #define POWERMONITOR_ADCRANGE_163_84_mV 0x00
106 #define POWERMONITOR_ADCRANGE_40_96_mV 0x01
107 #define POWERMONITOR_MODE_SINGLE_SHOT_SHUTDOWN 0x00
108 #define POWERMONITOR_MODE_SINGLE_SHOT_BUS_VOLTAGE 0x01
109 
110 #define POWERMONITOR_MODE_SINGLE_SHOT_BUS_VTG 0x01
111 #define POWERMONITOR_MODE_SINGLE_SHOT_SHUNT_VTG 0x02
112 #define POWERMONITOR_MODE_SINGLE_SHOT_SHUNT_BUS_VTG 0x03
113 #define POWERMONITOR_MODE_SINGLE_TEMPERATURE 0x04
114 #define POWERMONITOR_MODE_SINGLE_SHOT_TEMP_BUS_VTG 0x05
115 #define POWERMONITOR_MODE_SINGLE_SHOT_TEMP_SHUNT_VTG 0x06
116 #define POWERMONITOR_MODE_SINGLE_SHOT_TEMP_SHUNT_BUS_VTG 0x07
117 #define POWERMONITOR_MODE_CONTINUOUS_SHOT_BUS_VOLTAGE 0x08
118 #define POWERMONITOR_MODE_CONTINUOUS_SHOT_BUS_VTG 0x09
119 #define POWERMONITOR_MODE_CONTINUOUS_SHOT_SHUNT_VTG 0x0A
120 #define POWERMONITOR_MODE_CONTINUOUS_SHOT_SHUNT_BUS_VTG 0x0B
121 #define POWERMONITOR_MODE_CONTINUOUS_TEMPERATURE 0x0C
122 #define POWERMONITOR_MODE_CONTINUOUS_SHOT_TEMP_BUS_VTG 0x0D
123 #define POWERMONITOR_MODE_CONTINUOUS_SHOT_TEMP_SHUNT_VTG 0x0E
124 #define POWERMONITOR_MODE_CONTINUOUS_SHOT_TEMP_SHUNT_BUS_VTG 0x0F
125 
126 #define POWERMONITOR_VBUSCT_CONVERSION_TIME_50_nS 0x00
127 #define POWERMONITOR_VBUSCT_CONVERSION_TIME_84_nS 0x01
128 #define POWERMONITOR_VBUSCT_CONVERSION_TIME_150_nS 0x02
129 #define POWERMONITOR_VBUSCT_CONVERSION_TIME_280_nS 0x03
130 #define POWERMONITOR_VBUSCT_CONVERSION_TIME_540_nS 0x04
131 #define POWERMONITOR_VBUSCT_CONVERSION_TIME_1052_nS 0x05
132 #define POWERMONITOR_VBUSCT_CONVERSION_TIME_2074_nS 0x06
133 #define POWERMONITOR_VBUSCT_CONVERSION_TIME_4120_nS 0x07
134 
135 #define POWERMONITOR_VSHCT_CONVERSION_TIME_50_nS 0x00
136 #define POWERMONITOR_VSHCT_CONVERSION_TIME_84_nS 0x01
137 #define POWERMONITOR_VSHCT_CONVERSION_TIME_150_nS 0x02
138 #define POWERMONITOR_VSHCT_CONVERSION_TIME_280_nS 0x03
139 #define POWERMONITOR_VSHCT_CONVERSION_TIME_540_nS 0x04
140 #define POWERMONITOR_VSHCT_CONVERSION_TIME_1052_nS 0x05
141 #define POWERMONITOR_VSHCT_CONVERSION_TIME_2074_nS 0x06
142 #define POWERMONITOR_VSHCT_CONVERSION_TIME_4120_nS 0x07
143 
144 #define POWERMONITOR_VTCT_CONVERSION_TIME_50_nS 0x00
145 #define POWERMONITOR_VTCT_CONVERSION_TIME_84_nS 0x01
146 #define POWERMONITOR_VTCT_CONVERSION_TIME_150_nS 0x02
147 #define POWERMONITOR_VTCT_CONVERSION_TIME_280_nS 0x03
148 #define POWERMONITOR_VTCT_CONVERSION_TIME_540_nS 0x04
149 #define POWERMONITOR_VTCT_CONVERSION_TIME_1052_nS 0x05
150 #define POWERMONITOR_VTCT_CONVERSION_TIME_2074_nS 0x06
151 #define POWERMONITOR_VTCT_CONVERSION_TIME_4120_nS 0x07
152 
153 #define POWERMONITOR_AVG_AVERAGING_COUNT_1 0x00
154 #define POWERMONITOR_AVG_AVERAGING_COUNT_4 0x01
155 #define POWERMONITOR_AVG_AVERAGING_COUNT_16 0x02
156 #define POWERMONITOR_AVG_AVERAGING_COUNT_64 0x03
157 #define POWERMONITOR_AVG_AVERAGING_COUNT_128 0x04
158 #define POWERMONITOR_AVG_AVERAGING_COUNT_256 0x05
159 #define POWERMONITOR_AVG_AVERAGING_COUNT_512 0x06
160 #define POWERMONITOR_AVG_AVERAGING_COUNT_1024 0x07
161 
167 #define POWERMONITOR_SET_DEV_ADDR_GND_GND 0x40
168 #define POWERMONITOR_SET_DEV_ADDR_GND_VS 0x41
169 #define POWERMONITOR_SET_DEV_ADDR_GND_SDA 0x42
170 #define POWERMONITOR_SET_DEV_ADDR_GND_SCL 0x43
171 #define POWERMONITOR_SET_DEV_ADDR_VS_GND 0x44
172 #define POWERMONITOR_SET_DEV_ADDR_VS_VS 0x45
173 #define POWERMONITOR_SET_DEV_ADDR_VS_SDA 0x46
174 #define POWERMONITOR_SET_DEV_ADDR_VS_SCL 0x47
175 #define POWERMONITOR_SET_DEV_ADDR_SDA_GND 0x48
176 #define POWERMONITOR_SET_DEV_ADDR_SDA_VS 0x49
177 #define POWERMONITOR_SET_DEV_ADDR_SDA_SDA 0x4A
178 #define POWERMONITOR_SET_DEV_ADDR_SDA_SCL 0x4B
179 #define POWERMONITOR_SET_DEV_ADDR_SCL_GND 0x4C
180 #define POWERMONITOR_SET_DEV_ADDR_SCL_VS 0x4D
181 #define POWERMONITOR_SET_DEV_ADDR_SCL_SDA 0x4E
182 #define POWERMONITOR_SET_DEV_ADDR_SCL_SCL 0x4F
183  // powermonitor_set
185 
200 #define POWERMONITOR_MAP_MIKROBUS( cfg, mikrobus ) \
201  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
202  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
203  cfg.alr = MIKROBUS( mikrobus, MIKROBUS_INT )
204  // powermonitor_map // powermonitor
207 
212 typedef struct
213 {
214  // Input pins
215  digital_in_t alr;
217  // Modules
218  i2c_master_t i2c;
220  // I2C slave address
221  uint8_t slave_address;
223  float shunt;
224 
226 
231 typedef struct
232 {
233  pin_name_t scl;
234  pin_name_t sda;
236  pin_name_t alr;
238  uint32_t i2c_speed;
239  uint8_t i2c_address;
242 
247 typedef struct
248 {
249  uint8_t rstacc;
250  uint8_t convdly;
251  uint8_t tempcomp;
252  uint8_t adcrange;
253 
255 
260 typedef struct
261 {
262  uint8_t mode;
263  uint8_t vbusct;
264  uint8_t vshct;
265  uint8_t vtct;
266  uint8_t avg;
267 
269 
274 typedef enum
275 {
277  POWERMONITOR_ERROR = -1
278 
280 
299 
317 
334 
352 err_t powermonitor_generic_write ( powermonitor_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
353 
371 err_t powermonitor_generic_read ( powermonitor_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
372 
389 
407 
425 
443 
461 
480 
499 
519 
539 
557 err_t powermonitor_get_vshunt ( powermonitor_t *ctx, float *vshunt );
558 
576 err_t powermonitor_get_vbus ( powermonitor_t *ctx, float *vbus );
577 
595 err_t powermonitor_get_temperature ( powermonitor_t *ctx, float *temperature );
596 
614 err_t powermonitor_get_current ( powermonitor_t *ctx, float *current );
615 
633 err_t powermonitor_get_power ( powermonitor_t *ctx, float *power );
634 
652 err_t powermonitor_get_energy_data ( powermonitor_t *ctx, uint8_t *energy_data );
653 
671 err_t powermonitor_get_energy ( powermonitor_t *ctx, float *energy );
672 
692 err_t powermonitor_get_id ( powermonitor_t *ctx, uint8_t *manufacturer_id, uint16_t *die_id, uint8_t *rev_id );
693 
711 
712 #ifdef __cplusplus
713 }
714 #endif
715 #endif // POWERMONITOR_H
716  // powermonitor
718 
719 // ------------------------------------------------------------------------ END
POWERMONITOR_OK
Definition: powermonitor.h:275
powermonitor_set_adc_config
err_t powermonitor_set_adc_config(powermonitor_t *ctx, powermonitor_adc_cfg_t adc_cfg)
Power Monitor set ADC configuration function.
powermonitor_soft_reset
err_t powermonitor_soft_reset(powermonitor_t *ctx)
Power Monitor software reset function.
powermonitor_generic_write
err_t powermonitor_generic_write(powermonitor_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len)
Power Monitor I2C writing function.
powermonitor_return_value_t
powermonitor_return_value_t
Power Monitor Click return value data.
Definition: powermonitor.h:273
powermonitor_get_alert
err_t powermonitor_get_alert(powermonitor_t *ctx)
Power Monitor get alert function.
powermonitor_adc_cfg_t
Power Monitor Click ADC configuration object.
Definition: powermonitor.h:259
powermonitor_set_configuration
err_t powermonitor_set_configuration(powermonitor_t *ctx, powermonitor_dev_cfg_t cfg_data)
Power Monitor set configuration function.
powermonitor_cfg_setup
void powermonitor_cfg_setup(powermonitor_cfg_t *cfg)
Power Monitor configuration object setup function.
powermonitor_get_configuration
err_t powermonitor_get_configuration(powermonitor_t *ctx, powermonitor_dev_cfg_t *cfg_data)
Power Monitor get configuration function.
powermonitor_get_power
err_t powermonitor_get_power(powermonitor_t *ctx, float *power)
Power Monitor get power function.
powermonitor_get_adc_configu
err_t powermonitor_get_adc_configu(powermonitor_t *ctx, powermonitor_adc_cfg_t *adc_cfg)
Power Monitor get ADC configuration function.
POWERMONITOR_ERROR
Definition: powermonitor.h:276
powermonitor_cfg_t
Power Monitor Click configuration object.
Definition: powermonitor.h:230
powermonitor_set_shunt_undervoltage_threshold
err_t powermonitor_set_shunt_undervoltage_threshold(powermonitor_t *ctx, float suvl)
Power Monitor set shunt undervoltage threshold function.
powermonitor_get_energy
err_t powermonitor_get_energy(powermonitor_t *ctx, float *energy)
Power Monitor get energy function.
powermonitor_get_vbus
err_t powermonitor_get_vbus(powermonitor_t *ctx, float *vbus)
Power Monitor get bus voltage function.
powermonitor_generic_read
err_t powermonitor_generic_read(powermonitor_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len)
Power Monitor I2C reading function.
powermonitor_get_vshunt
err_t powermonitor_get_vshunt(powermonitor_t *ctx, float *vshunt)
Power Monitor get shunt voltage function.
powermonitor_set_bus_overvoltage_threshold
err_t powermonitor_set_bus_overvoltage_threshold(powermonitor_t *ctx, float bovl)
Power Monitor set bus overvoltage threshold function.
powermonitor_dev_cfg_t
Power Monitor Click device settings object.
Definition: powermonitor.h:246
powermonitor_init
err_t powermonitor_init(powermonitor_t *ctx, powermonitor_cfg_t *cfg)
Power Monitor initialization function.
powermonitor_get_energy_data
err_t powermonitor_get_energy_data(powermonitor_t *ctx, uint8_t *energy_data)
Power Monitor get energy data function.
powermonitor_default_cfg
err_t powermonitor_default_cfg(powermonitor_t *ctx)
Power Monitor default configuration function.
powermonitor_set_shunt_overvoltage_threshold
err_t powermonitor_set_shunt_overvoltage_threshold(powermonitor_t *ctx, float sovl)
Power Monitor set shunt overvoltage threshold function.
powermonitor_set_bus_undervoltage_threshold
err_t powermonitor_set_bus_undervoltage_threshold(powermonitor_t *ctx, float buvl)
Power Monitor set bus undervoltage threshold function.
powermonitor_get_current
err_t powermonitor_get_current(powermonitor_t *ctx, float *current)
Power Monitor get current function.
powermonitor_get_id
err_t powermonitor_get_id(powermonitor_t *ctx, uint8_t *manufacturer_id, uint16_t *die_id, uint8_t *rev_id)
Power Monitor get device ID function.
powermonitor_t
Power Monitor Click context object.
Definition: powermonitor.h:211
powermonitor_get_temperature
err_t powermonitor_get_temperature(powermonitor_t *ctx, float *temperature)
Power Monitor get temperature function.