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
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
53
54
55
76#define POWERMONITOR_REG_CONFIG 0x00
77#define POWERMONITOR_REG_ADCCONFIG 0x01
78#define POWERMONITOR_REG_SHUNT_CAL 0x02
79#define POWERMONITOR_REG_SHUNT_TEMPCO 0x03
80#define POWERMONITOR_REG_VSHUNT 0x04
81#define POWERMONITOR_REG_VBUS 0x05
82#define POWERMONITOR_REG_DIETEMP 0x06
83#define POWERMONITOR_REG_CURRENT 0x07
84#define POWERMONITOR_REG_POWER 0x08
85#define POWERMONITOR_REG_ENERGY 0x09
86#define POWERMONITOR_REG_CHARGE 0x0A
87#define POWERMONITOR_REG_DIAG_ALR 0x0B
88#define POWERMONITOR_REG_SOVL 0x0C
89#define POWERMONITOR_REG_SUVL 0x0D
90#define POWERMONITOR_REG_BOVL 0x0E
91#define POWERMONITOR_REG_BUVL 0x0F
92#define POWERMONITOR_REG_TEMP_LIMIT 0x10
93#define POWERMONITOR_REG_PWR_LIMIT 0x11
94#define POWERMONITOR_REG_MANUFACTURER_ID 0x3E
95#define POWERMONITOR_REG_DEVICE_ID 0x3F
96
97 // powermonitor_reg
98
113#define POWERMONITOR_RST_RESET_BIT 0x80
114#define POWERMONITOR_RSTACC_NORMAL_OPERATION 0x00
115#define POWERMONITOR_RSTACC_CLEARS_ENERGY_CHARGE_REG 0x01
116#define POWERMONITOR_CONVDLY_0_s 0x00
117#define POWERMONITOR_TEMPCOMP_DISABLED 0x00
118#define POWERMONITOR_TEMPCOMP_ENABLED 0x01
119#define POWERMONITOR_ADCRANGE_163_84_mV 0x00
120#define POWERMONITOR_ADCRANGE_40_96_mV 0x01
121#define POWERMONITOR_MODE_SINGLE_SHOT_SHUTDOWN 0x00
122#define POWERMONITOR_MODE_SINGLE_SHOT_BUS_VOLTAGE 0x01
123
124#define POWERMONITOR_MODE_SINGLE_SHOT_BUS_VTG 0x01
125#define POWERMONITOR_MODE_SINGLE_SHOT_SHUNT_VTG 0x02
126#define POWERMONITOR_MODE_SINGLE_SHOT_SHUNT_BUS_VTG 0x03
127#define POWERMONITOR_MODE_SINGLE_TEMPERATURE 0x04
128#define POWERMONITOR_MODE_SINGLE_SHOT_TEMP_BUS_VTG 0x05
129#define POWERMONITOR_MODE_SINGLE_SHOT_TEMP_SHUNT_VTG 0x06
130#define POWERMONITOR_MODE_SINGLE_SHOT_TEMP_SHUNT_BUS_VTG 0x07
131#define POWERMONITOR_MODE_CONTINUOUS_SHOT_BUS_VOLTAGE 0x08
132#define POWERMONITOR_MODE_CONTINUOUS_SHOT_BUS_VTG 0x09
133#define POWERMONITOR_MODE_CONTINUOUS_SHOT_SHUNT_VTG 0x0A
134#define POWERMONITOR_MODE_CONTINUOUS_SHOT_SHUNT_BUS_VTG 0x0B
135#define POWERMONITOR_MODE_CONTINUOUS_TEMPERATURE 0x0C
136#define POWERMONITOR_MODE_CONTINUOUS_SHOT_TEMP_BUS_VTG 0x0D
137#define POWERMONITOR_MODE_CONTINUOUS_SHOT_TEMP_SHUNT_VTG 0x0E
138#define POWERMONITOR_MODE_CONTINUOUS_SHOT_TEMP_SHUNT_BUS_VTG 0x0F
139
140#define POWERMONITOR_VBUSCT_CONVERSION_TIME_50_nS 0x00
141#define POWERMONITOR_VBUSCT_CONVERSION_TIME_84_nS 0x01
142#define POWERMONITOR_VBUSCT_CONVERSION_TIME_150_nS 0x02
143#define POWERMONITOR_VBUSCT_CONVERSION_TIME_280_nS 0x03
144#define POWERMONITOR_VBUSCT_CONVERSION_TIME_540_nS 0x04
145#define POWERMONITOR_VBUSCT_CONVERSION_TIME_1052_nS 0x05
146#define POWERMONITOR_VBUSCT_CONVERSION_TIME_2074_nS 0x06
147#define POWERMONITOR_VBUSCT_CONVERSION_TIME_4120_nS 0x07
148
149#define POWERMONITOR_VSHCT_CONVERSION_TIME_50_nS 0x00
150#define POWERMONITOR_VSHCT_CONVERSION_TIME_84_nS 0x01
151#define POWERMONITOR_VSHCT_CONVERSION_TIME_150_nS 0x02
152#define POWERMONITOR_VSHCT_CONVERSION_TIME_280_nS 0x03
153#define POWERMONITOR_VSHCT_CONVERSION_TIME_540_nS 0x04
154#define POWERMONITOR_VSHCT_CONVERSION_TIME_1052_nS 0x05
155#define POWERMONITOR_VSHCT_CONVERSION_TIME_2074_nS 0x06
156#define POWERMONITOR_VSHCT_CONVERSION_TIME_4120_nS 0x07
157
158#define POWERMONITOR_VTCT_CONVERSION_TIME_50_nS 0x00
159#define POWERMONITOR_VTCT_CONVERSION_TIME_84_nS 0x01
160#define POWERMONITOR_VTCT_CONVERSION_TIME_150_nS 0x02
161#define POWERMONITOR_VTCT_CONVERSION_TIME_280_nS 0x03
162#define POWERMONITOR_VTCT_CONVERSION_TIME_540_nS 0x04
163#define POWERMONITOR_VTCT_CONVERSION_TIME_1052_nS 0x05
164#define POWERMONITOR_VTCT_CONVERSION_TIME_2074_nS 0x06
165#define POWERMONITOR_VTCT_CONVERSION_TIME_4120_nS 0x07
166
167#define POWERMONITOR_AVG_AVERAGING_COUNT_1 0x00
168#define POWERMONITOR_AVG_AVERAGING_COUNT_4 0x01
169#define POWERMONITOR_AVG_AVERAGING_COUNT_16 0x02
170#define POWERMONITOR_AVG_AVERAGING_COUNT_64 0x03
171#define POWERMONITOR_AVG_AVERAGING_COUNT_128 0x04
172#define POWERMONITOR_AVG_AVERAGING_COUNT_256 0x05
173#define POWERMONITOR_AVG_AVERAGING_COUNT_512 0x06
174#define POWERMONITOR_AVG_AVERAGING_COUNT_1024 0x07
175
181#define POWERMONITOR_SET_DEV_ADDR_GND_GND 0x40
182#define POWERMONITOR_SET_DEV_ADDR_GND_VS 0x41
183#define POWERMONITOR_SET_DEV_ADDR_GND_SDA 0x42
184#define POWERMONITOR_SET_DEV_ADDR_GND_SCL 0x43
185#define POWERMONITOR_SET_DEV_ADDR_VS_GND 0x44
186#define POWERMONITOR_SET_DEV_ADDR_VS_VS 0x45
187#define POWERMONITOR_SET_DEV_ADDR_VS_SDA 0x46
188#define POWERMONITOR_SET_DEV_ADDR_VS_SCL 0x47
189#define POWERMONITOR_SET_DEV_ADDR_SDA_GND 0x48
190#define POWERMONITOR_SET_DEV_ADDR_SDA_VS 0x49
191#define POWERMONITOR_SET_DEV_ADDR_SDA_SDA 0x4A
192#define POWERMONITOR_SET_DEV_ADDR_SDA_SCL 0x4B
193#define POWERMONITOR_SET_DEV_ADDR_SCL_GND 0x4C
194#define POWERMONITOR_SET_DEV_ADDR_SCL_VS 0x4D
195#define POWERMONITOR_SET_DEV_ADDR_SCL_SDA 0x4E
196#define POWERMONITOR_SET_DEV_ADDR_SCL_SCL 0x4F
197
198 // powermonitor_set
199
214#define POWERMONITOR_MAP_MIKROBUS( cfg, mikrobus ) \
215 cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
216 cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
217 cfg.alr = MIKROBUS( mikrobus, MIKROBUS_INT )
218
219 // powermonitor_map
220 // powermonitor
221
226typedef struct
227{
228 // Input pins
229 digital_in_t alr;
231 // Modules
232 i2c_master_t i2c;
234 // I2C slave address
237 float shunt;
238
240
245typedef struct
246{
247 pin_name_t scl;
248 pin_name_t sda;
250 pin_name_t alr;
252 uint32_t i2c_speed;
253 uint8_t i2c_address;
256
261typedef struct
262{
263 uint8_t rstacc;
264 uint8_t convdly;
265 uint8_t tempcomp;
266 uint8_t adcrange;
267
269
274typedef struct
275{
276 uint8_t mode;
277 uint8_t vbusct;
278 uint8_t vshct;
279 uint8_t vtct;
280 uint8_t avg;
281
283
294
313
331
348
366err_t powermonitor_generic_write ( powermonitor_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len );
367
385err_t powermonitor_generic_read ( powermonitor_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len );
386
403
421
439
457
475
494
513
533
553
571err_t powermonitor_get_vshunt ( powermonitor_t *ctx, float *vshunt );
572
590err_t powermonitor_get_vbus ( powermonitor_t *ctx, float *vbus );
591
609err_t powermonitor_get_temperature ( powermonitor_t *ctx, float *temperature );
610
628err_t powermonitor_get_current ( powermonitor_t *ctx, float *current );
629
647err_t powermonitor_get_power ( powermonitor_t *ctx, float *power );
648
666err_t powermonitor_get_energy_data ( powermonitor_t *ctx, uint8_t *energy_data );
667
685err_t powermonitor_get_energy ( powermonitor_t *ctx, float *energy );
686
706err_t powermonitor_get_id ( powermonitor_t *ctx, uint8_t *manufacturer_id, uint16_t *die_id, uint8_t *rev_id );
707
725
726#ifdef __cplusplus
727}
728#endif
729#endif // POWERMONITOR_H
730
731 // powermonitor
732
733// ------------------------------------------------------------------------ END
err_t powermonitor_get_energy_data(powermonitor_t *ctx, uint8_t *energy_data)
Power Monitor get energy data function.
err_t powermonitor_generic_write(powermonitor_t *ctx, uint8_t reg, uint8_t *tx_buf, uint8_t tx_len)
Power Monitor I2C writing function.
err_t powermonitor_get_configuration(powermonitor_t *ctx, powermonitor_dev_cfg_t *cfg_data)
Power Monitor get configuration function.
void powermonitor_cfg_setup(powermonitor_cfg_t *cfg)
Power Monitor configuration object setup function.
err_t powermonitor_soft_reset(powermonitor_t *ctx)
Power Monitor software reset function.
err_t powermonitor_set_adc_config(powermonitor_t *ctx, powermonitor_adc_cfg_t adc_cfg)
Power Monitor set ADC configuration function.
err_t powermonitor_get_temperature(powermonitor_t *ctx, float *temperature)
Power Monitor get temperature function.
err_t powermonitor_get_adc_configu(powermonitor_t *ctx, powermonitor_adc_cfg_t *adc_cfg)
Power Monitor get ADC configuration function.
err_t powermonitor_set_configuration(powermonitor_t *ctx, powermonitor_dev_cfg_t cfg_data)
Power Monitor set configuration function.
err_t powermonitor_set_shunt_overvoltage_threshold(powermonitor_t *ctx, float sovl)
Power Monitor set shunt overvoltage threshold function.
err_t powermonitor_get_alert(powermonitor_t *ctx)
Power Monitor get alert function.
err_t powermonitor_set_bus_undervoltage_threshold(powermonitor_t *ctx, float buvl)
Power Monitor set bus undervoltage threshold function.
err_t powermonitor_get_vbus(powermonitor_t *ctx, float *vbus)
Power Monitor get bus voltage function.
err_t powermonitor_get_energy(powermonitor_t *ctx, float *energy)
Power Monitor get energy function.
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.
err_t powermonitor_get_current(powermonitor_t *ctx, float *current)
Power Monitor get current function.
err_t powermonitor_init(powermonitor_t *ctx, powermonitor_cfg_t *cfg)
Power Monitor initialization function.
err_t powermonitor_generic_read(powermonitor_t *ctx, uint8_t reg, uint8_t *rx_buf, uint8_t rx_len)
Power Monitor I2C reading function.
err_t powermonitor_get_power(powermonitor_t *ctx, float *power)
Power Monitor get power function.
err_t powermonitor_set_shunt_undervoltage_threshold(powermonitor_t *ctx, float suvl)
Power Monitor set shunt undervoltage threshold function.
err_t powermonitor_set_bus_overvoltage_threshold(powermonitor_t *ctx, float bovl)
Power Monitor set bus overvoltage threshold function.
err_t powermonitor_get_vshunt(powermonitor_t *ctx, float *vshunt)
Power Monitor get shunt voltage function.
err_t powermonitor_default_cfg(powermonitor_t *ctx)
Power Monitor default configuration function.
powermonitor_return_value_t
Power Monitor Click return value data.
Definition powermonitor.h:289
@ POWERMONITOR_OK
Definition powermonitor.h:290
@ POWERMONITOR_ERROR
Definition powermonitor.h:291
Power Monitor Click ADC configuration object.
Definition powermonitor.h:275
uint8_t avg
Definition powermonitor.h:280
uint8_t mode
Definition powermonitor.h:276
uint8_t vtct
Definition powermonitor.h:279
uint8_t vbusct
Definition powermonitor.h:277
uint8_t vshct
Definition powermonitor.h:278
Power Monitor Click configuration object.
Definition powermonitor.h:246
pin_name_t alr
Definition powermonitor.h:250
uint32_t i2c_speed
Definition powermonitor.h:252
pin_name_t scl
Definition powermonitor.h:247
pin_name_t sda
Definition powermonitor.h:248
uint8_t i2c_address
Definition powermonitor.h:253
Power Monitor Click device settings object.
Definition powermonitor.h:262
uint8_t tempcomp
Definition powermonitor.h:265
uint8_t convdly
Definition powermonitor.h:264
uint8_t adcrange
Definition powermonitor.h:266
uint8_t rstacc
Definition powermonitor.h:263
Power Monitor Click context object.
Definition powermonitor.h:227
digital_in_t alr
Definition powermonitor.h:229
float shunt
Definition powermonitor.h:237
i2c_master_t i2c
Definition powermonitor.h:232
uint8_t slave_address
Definition powermonitor.h:235