vcpmonitor  2.0.0.0
vcpmonitor.h
Go to the documentation of this file.
1 /*
2  * MikroSDK - MikroE Software Development Kit
3  * Copyright© 2020 MikroElektronika d.o.o.
4  *
5  * Permission is hereby granted, free of charge, to any person
6  * obtaining a copy of this software and associated documentation
7  * files (the "Software"), to deal in the Software without restriction,
8  * including without limitation the rights to use, copy, modify, merge,
9  * publish, distribute, sublicense, and/or sell copies of the Software,
10  * and to permit persons to whom the Software is furnished to do so,
11  * subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be
14  * included in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
21  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
22  * OR OTHER DEALINGS IN THE SOFTWARE.
23  */
24 
33 // ----------------------------------------------------------------------------
34 
35 #ifndef VCPMONITOR_H
36 #define VCPMONITOR_H
37 
38 #include "mikrosdk_version.h"
39 
40 #ifdef __GNUC__
41 #if mikroSDK_GET_VERSION < 20800ul
42 #include "rcu_delays.h"
43 #else
44 #include "delays.h"
45 #endif
46 #endif
47 
48 #include "drv_digital_out.h"
49 #include "drv_digital_in.h"
50 #include "drv_i2c_master.h"
51 
52 // -------------------------------------------------------------- PUBLIC MACROS
62 #define VCPMONITOR_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
64  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
65  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
66 
72 #define VCPMONITOR_RETVAL uint8_t
73 
74 #define VCPMONITOR_OK 0x00
75 #define VCPMONITOR_INIT_ERROR 0xFF
76 
82 #define VCPMONITOR_REG_CONFIGURATION 0x00
83 #define VCPMONITOR_REG_CURRENT_DATA 0x01
84 #define VCPMONITOR_REG_BUS_VOLTAGE_DATA 0x02
85 #define VCPMONITOR_REG_POWER_DATA 0x03
86 #define VCPMONITOR_REG_MASK_ENABLE 0x06
87 #define VCPMONITOR_REG_ALERT_LIMIT 0x07
88 #define VCPMONITOR_REG_MANUFACTURE_ID 0xFE
89 #define VCPMONITOR_REG_DIE_ID 0xFF
90 
96 #define VCPMONITOR_SLAVE_ADDR_GND_GND 0x40
97 #define VCPMONITOR_SLAVE_ADDR_GND_VS 0x41
98 #define VCPMONITOR_SLAVE_ADDR_GND_SDA 0x42
99 #define VCPMONITOR_SLAVE_ADDR_GND_SCL 0x43
100 #define VCPMONITOR_SLAVE_ADDR_VS_GND 0x44
101 #define VCPMONITOR_SLAVE_ADDR_VS_VS 0x45
102 #define VCPMONITOR_SLAVE_ADDR_VS_SDA 0x46
103 #define VCPMONITOR_SLAVE_ADDR_VS_SCL 0x47
104 #define VCPMONITOR_SLAVE_ADDR_SDA_GND 0x48
105 #define VCPMONITOR_SLAVE_ADDR_SDA_VS 0x49
106 #define VCPMONITOR_SLAVE_ADDR_SDA_SDA 0x4A
107 #define VCPMONITOR_SLAVE_ADDR_SDA_SCL 0x4B
108 #define VCPMONITOR_SLAVE_ADDR_SCL_GND 0x4C
109 #define VCPMONITOR_SLAVE_ADDR_SCL_VS 0x4D
110 #define VCPMONITOR_SLAVE_ADDR_SCL_SDA 0x4E
111 #define VCPMONITOR_SLAVE_ADDR_SCL_SCL 0x4F
112 
118 #define VCPMONITOR_DEF_MANUFACTURE_ID 0x5449
119 #define VCPMONITOR_DEF_DIE_ID 0x2270
120 #define VCPMONITOR_DEF_CONFIGURATION 0x6127
121 
128 #define VCPMONITOR_CFG_RESET_ON 0x8000
129 #define VCPMONITOR_CFG_RESET_OFF 0x0000
130 #define VCPMONITOR_CFG_NUM_OF_AVG_1 0x0000
131 #define VCPMONITOR_CFG_NUM_OF_AVG_4 0x0200
132 #define VCPMONITOR_CFG_NUM_OF_AVG_16 0x0400
133 #define VCPMONITOR_CFG_NUM_OF_AVG_64 0x0600
134 #define VCPMONITOR_CFG_NUM_OF_AVG_128 0x0800
135 #define VCPMONITOR_CFG_NUM_OF_AVG_256 0x0A00
136 #define VCPMONITOR_CFG_NUM_OF_AVG_512 0x0C00
137 #define VCPMONITOR_CFG_NUM_OF_AVG_1024 0x0E00
138 #define VCPMONITOR_CFG_VBUSCT_140us 0x0000
139 #define VCPMONITOR_CFG_VBUSCT_204us 0x0040
140 #define VCPMONITOR_CFG_VBUSCT_332us 0x0080
141 #define VCPMONITOR_CFG_VBUSCT_588us 0x00C0
142 #define VCPMONITOR_CFG_VBUSCT_1p1ms 0x0100
143 #define VCPMONITOR_CFG_VBUSCT_2p116ms 0x0140
144 #define VCPMONITOR_CFG_VBUSCT_4p156ms 0x0180
145 #define VCPMONITOR_CFG_VBUSCT_8p244ms 0x01C0
146 #define VCPMONITOR_CFG_ISHCT_140us 0x0000
147 #define VCPMONITOR_CFG_ISHCT_204us 0x0008
148 #define VCPMONITOR_CFG_ISHCT_332us 0x0010
149 #define VCPMONITOR_CFG_ISHCT_588us 0x0018
150 #define VCPMONITOR_CFG_ISHCT_1p1ms 0x0020
151 #define VCPMONITOR_CFG_ISHCT_2p116ms 0x0028
152 #define VCPMONITOR_CFG_ISHCT_4p156ms 0x0030
153 #define VCPMONITOR_CFG_ISHCT_8p244ms 0x0038
154 #define VCPMONITOR_CFG_MODE_POWER_DOWN 0x0000
155 #define VCPMONITOR_CFG_MODE_CURRENT_TRIG 0x0001
156 #define VCPMONITOR_CFG_MODE_VOLTAGE_TRIG 0x0002
157 #define VCPMONITOR_CFG_MODE_CURR_VOLT_TRIG 0x0003
158 #define VCPMONITOR_CFG_MODE_CURRENT_CONT 0x0005
159 #define VCPMONITOR_CFG_MODE_VOLTAGE_CONT 0x0006
160 #define VCPMONITOR_CFG_MODE_CURR_VOLT_CONT 0x0007
161 
168 #define CURRENT_CONSTANT 1.25
169 #define VOLTAGE_CONSTANT 1.25
170 #define POWER_CONSTANT 10
171 
177 // Mask Enable register
178 #define VCPMONITOR_MASK_OCL_DISABLE 0x0000
179 #define VCPMONITOR_MASK_UCL_ENABLE 0x4000
180 #define VCPMONITOR_MASK_UCL_DISABLE 0x0000
181 #define VCPMONITOR_MASK_BOL_ENABLE 0x2000
182 #define VCPMONITOR_MASK_BOL_DISABLE 0x0000
183 #define VCPMONITOR_MASK_BUL_ENABLE 0x1000
184 #define VCPMONITOR_MASK_OCL_ENABLE 0x8000
185 #define VCPMONITOR_MASK_BUL_DISABLE 0x0000
186 #define VCPMONITOR_MASK_POL_ENABLE 0x0800
187 #define VCPMONITOR_MASK_POL_DISABLE 0x0000
188 #define VCPMONITOR_MASK_CNVR_ENABLE 0x0400
189 #define VCPMONITOR_MASK_CNVR_DISABLE 0x0000
190 #define VCPMONITOR_MASK_AFF_ENABLE 0x0010
191 #define VCPMONITOR_MASK_AFF_DISABLE 0x0000
192 #define VCPMONITOR_MASK_CVRF_ENABLE 0x0008
193 #define VCPMONITOR_MASK_CVRF_DISABLE 0x0000
194 #define VCPMONITOR_MASK_OVF_ENABLE 0x0004
195 #define VCPMONITOR_MASK_OVF_DISABLE 0x0000
196 #define VCPMONITOR_MASK_APOL_ENABLE 0x0002
197 #define VCPMONITOR_MASK_APOL_DISABLE 0x0000
198 #define VCPMONITOR_MASK_LEN_ENABLE 0x0001
199 #define VCPMONITOR_MASK_LEN_DISABLE 0x0000
200  // End group macro
203 // --------------------------------------------------------------- PUBLIC TYPES
212 typedef struct
213 {
214  // Input pins
215 
216  digital_in_t int_pin;
217 
218  // Modules
219 
220  i2c_master_t i2c;
221 
222  // ctx variable
223 
224  uint8_t slave_address;
225 
226 } vcpmonitor_t;
227 
231 typedef struct
232 {
233  // Communication gpio pins
234 
235  pin_name_t scl;
236  pin_name_t sda;
237 
238  // Additional gpio pins
239 
240  pin_name_t int_pin;
241 
242  // static variable
243 
244  uint32_t i2c_speed;
245  uint8_t i2c_address;
246 
248  // End types group
250 
251 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
252 
258 #ifdef __cplusplus
259 extern "C"{
260 #endif
261 
271 
281 
290 
301 void vcpmonitor_generic_write ( vcpmonitor_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
302 
313 void vcpmonitor_generic_read ( vcpmonitor_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
314 
324 void vcpmonitor_write_data ( vcpmonitor_t *ctx, uint8_t reg, uint16_t tx_data );
325 
335 int16_t vcpmonitor_read_data ( vcpmonitor_t *ctx, uint8_t reg_addr );
336 
347 
358 
369 
380 
392 uint8_t vcpmonitor_get_id_value ( vcpmonitor_t *ctx, uint16_t *manufacture_id, uint16_t* did_id );
393 
394 
395 #ifdef __cplusplus
396 }
397 #endif
398 #endif // _VCPMONITOR_H_
399  // End public_function group
402 
403 // ------------------------------------------------------------------------- END
vcpmonitor_get_voltage
float vcpmonitor_get_voltage(vcpmonitor_t *ctx)
Reads voltage data in mV.
vcpmonitor_get_current
float vcpmonitor_get_current(vcpmonitor_t *ctx)
Reads current data in mA.
vcpmonitor_init
VCPMONITOR_RETVAL vcpmonitor_init(vcpmonitor_t *ctx, vcpmonitor_cfg_t *cfg)
Initialization function.
VCPMONITOR_RETVAL
#define VCPMONITOR_RETVAL
Definition: vcpmonitor.h:72
vcpmonitor_get_power
float vcpmonitor_get_power(vcpmonitor_t *ctx)
Reads power data in mW.
vcpmonitor_t::i2c
i2c_master_t i2c
Definition: vcpmonitor.h:220
vcpmonitor_write_data
void vcpmonitor_write_data(vcpmonitor_t *ctx, uint8_t reg, uint16_t tx_data)
Writes data to register.
vcpmonitor_get_id_value
uint8_t vcpmonitor_get_id_value(vcpmonitor_t *ctx, uint16_t *manufacture_id, uint16_t *did_id)
Gets Manufacture ID and Device ID.
vcpmonitor_cfg_setup
void vcpmonitor_cfg_setup(vcpmonitor_cfg_t *cfg)
Config Object Initialization function.
vcpmonitor_generic_write
void vcpmonitor_generic_write(vcpmonitor_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
vcpmonitor_cfg_t
Click configuration structure definition.
Definition: vcpmonitor.h:232
vcpmonitor_get_interrupt_state
uint8_t vcpmonitor_get_interrupt_state(vcpmonitor_t *ctx)
Gets Interrupt pin state.
vcpmonitor_cfg_t::int_pin
pin_name_t int_pin
Definition: vcpmonitor.h:240
vcpmonitor_t
Click ctx object definition.
Definition: vcpmonitor.h:213
vcpmonitor_cfg_t::sda
pin_name_t sda
Definition: vcpmonitor.h:236
vcpmonitor_t::int_pin
digital_in_t int_pin
Definition: vcpmonitor.h:216
vcpmonitor_t::slave_address
uint8_t slave_address
Definition: vcpmonitor.h:224
vcpmonitor_cfg_t::i2c_address
uint8_t i2c_address
Definition: vcpmonitor.h:245
vcpmonitor_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: vcpmonitor.h:244
vcpmonitor_cfg_t::scl
pin_name_t scl
Definition: vcpmonitor.h:235
vcpmonitor_read_data
int16_t vcpmonitor_read_data(vcpmonitor_t *ctx, uint8_t reg_addr)
Reads data from register.
vcpmonitor_default_cfg
void vcpmonitor_default_cfg(vcpmonitor_t *ctx)
Click Default Configuration function.
vcpmonitor_generic_read
void vcpmonitor_generic_read(vcpmonitor_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.