balancer5  2.0.0.0
balancer5.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 BALANCER5_H
36 #define BALANCER5_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 
53 // -------------------------------------------------------------- PUBLIC MACROS
63 #define BALANCER5_MAP_MIKROBUS( cfg, mikrobus ) \
64  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
65  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
66  cfg.pg = MIKROBUS( mikrobus, MIKROBUS_AN ); \
67  cfg.pss = MIKROBUS( mikrobus, MIKROBUS_RST ); \
68  cfg.cd = MIKROBUS( mikrobus, MIKROBUS_CS ); \
69  cfg.int_pin = MIKROBUS( mikrobus, MIKROBUS_INT )
70 
76 #define BALANCER5_RETVAL uint8_t
77 
78 #define BALANCER5_OK 0x00
79 #define BALANCER5_INIT_ERROR 0xFF
80 
86 #define BALANCER5_REG_CELL_V_LIMIT 0x00
87 #define BALANCER5_REG_CHARGE_CURR_LIMIT 0x01
88 #define BALANCER5_REG_INPUT_V_LIMIT 0x02
89 #define BALANCER5_REG_INPUT_CURR_LIMIT 0x03
90 #define BALANCER5_REG_PRECHARGE_N_TERMINATION_CTRL 0x04
91 #define BALANCER5_REG_CHARGER_CTRL_1 0x05
92 #define BALANCER5_REG_CHARGER_CTRL_2 0x06
93 #define BALANCER5_REG_CHARGER_CTRL_3 0x07
94 #define BALANCER5_REG_CHARGER_CTRL_4 0x08
95 #define BALANCER5_REG_ICO_CURR_LIMIT 0x0A
96 #define BALANCER5_REG_CHARGER_STATUS_1 0x0B
97 #define BALANCER5_REG_CHARGER_STATUS_2 0x0C
98 #define BALANCER5_REG_NTC_STATUS 0x0D
99 #define BALANCER5_REG_FAULT_STATUS 0x0E
100 #define BALANCER5_REG_CHARGER_FLAG_1 0x0F
101 #define BALANCER5_REG_CHARGER_FLAG_2 0x10
102 #define BALANCER5_REG_FAULT_FLAG 0x11
103 #define BALANCER5_REG_CHARGER_MASK_1 0x12
104 #define BALANCER5_REG_CHARGER_MASK_2 0x13
105 #define BALANCER5_REG_FAULT_MASK 0x14
106 #define BALANCER5_REG_ADC_CTRL 0x15
107 #define BALANCER5_REG_ADC_FUNCTION_DISABLE 0x16
108 #define BALANCER5_REG_IBUS_ADC1 0x17
109 #define BALANCER5_REG_IBUS_ADC0 0x18
110 #define BALANCER5_REG_ICHG_ADC1 0x19
111 #define BALANCER5_REG_ICHG_ADC0 0x1A
112 #define BALANCER5_REG_VBUS_ADC1 0x1B
113 #define BALANCER5_REG_VBUS_ADC0 0x1C
114 #define BALANCER5_REG_VBAT_ADC1 0x1D
115 #define BALANCER5_REG_VBAT_ADC0 0x1E
116 #define BALANCER5_REG_VCELLTOP_ADC1 0x1F
117 #define BALANCER5_REG_VCELLTOP_ADC0 0x20
118 #define BALANCER5_REG_TS_ADC1 0x21
119 #define BALANCER5_REG_TS_ADC0 0x22
120 #define BALANCER5_REG_TDIE_ADC1 0x23
121 #define BALANCER5_REG_TDIE_ADC0 0x24
122 #define BALANCER5_REG_PART_INFO 0x25
123 #define BALANCER5_REG_VCELLBOT_ADC1 0x26
124 #define BALANCER5_REG_VCELLBOT_ADC0 0x27
125 #define BALANCER5_REG_CELL_BALANCING_CTRL1 0x28
126 #define BALANCER5_REG_CELL_BALANCING_CTRL2 0x29
127 #define BALANCER5_REG_CELL_BALANCING_STATUS_N_CNTRL 0x2A
128 #define BALANCER5_REG_CELL_BALANCING_FLAG 0x2B
129 #define BALANCER5_REG_CELL_BALANCING_MASK 0x2C
130 
136 #define BALANCER5_ERROR_ID 0xAA
137 #define BALANCER5_SUCCESSFUL 0xFF
138 
144 #define BALANCER5_DEVICE_ID 0x29
145 
150 #define BALANCER5_PIN_STATUS_HIGH 1
151 #define BALANCER5_PIN_STATUS_LOW 0
152 
157 #define BALANCER5_CHARGE_OFF 1
158 #define BALANCER5_CHARGE_ON 0
159 
164 #define BALANCER5_ADC_CTRL_ENABLE 0x80
165 #define BALANCER5_ADC_CTRL_DISABLE 0x00
166 #define BALANCER5_ADC_CTRL_CONT_CONV 0x00
167 #define BALANCER5_ADC_CTRL_ONE_SHOT_CONV 0x40
168 #define BALANCER5_ADC_CTRL_15BIT_RES 0x00
169 #define BALANCER5_ADC_CTRL_14BIT_RES 0x10
170 #define BALANCER5_ADC_CTRL_13BIT_RES 0x20
171 #define BALANCER5_ADC_CTRL_12BIT_RES 0x30
172 
177 #define BALANCER5_CS1_IINDPM_NORMAL 0x00
178 #define BALANCER5_CS1_IINDPM_IN_REGULATION 0x40
179 #define BALANCER5_CS1_VINDPM_NORMAL 0x00
180 #define BALANCER5_CS1_VINDPM_IN_REGULATION 0x20
181 #define BALANCER5_CS1_IC_NORMAL 0x00
182 #define BALANCER5_CS1_IC_IN_THERMAL_REGULATION 0x10
183 #define BALANCER5_CS1_WD_NORMAL 0x00
184 #define BALANCER5_CS1_WD_TIMER_EXPIRED 0x08
185 #define BALANCER5_CS1_NOT_CHARGING 0x00
186 #define BALANCER5_CS1_TRICKLE_CHARGE 0x01
187 #define BALANCER5_CS1_PRE_CHARGE 0x02
188 #define BALANCER5_CS1_FAST_CHARGE 0x03
189 #define BALANCER5_CS1_TAPER_CHARGE 0x04
190 #define BALANCER5_CS1_TOP_OFF_TIMER_CHARGE 0x05
191 #define BALANCER5_CS1_CHARGE_TERMINATION 0x06
192 
197 #define BALANCER5_CS2_POWER_GOOD 0x80
198 #define BALANCER5_CS2_POWER_NOT_GOOD 0x00
199 #define BALANCER5_CS2_NO_INPUT 0x00
200 #define BALANCER5_CS2_USB_HOST_SDP 0x10
201 #define BALANCER5_CS2_USB_CDP 0x20
202 #define BALANCER5_CS2_ADAPTER 0x30
203 #define BALANCER5_CS2_POORSRC 0x40
204 #define BALANCER5_CS2_UNKNOWN_ADAPTER 0x50
205 #define BALANCER5_CS2_NON_STANDARD_ADAPTER 0x60
206 #define BALANCER5_CS2_ICO_DISABLED 0x00
207 #define BALANCER5_CS2_ICO_OPTIMIZATION_IN_PROGRESS 0x02
208 #define BALANCER5_CS2_MAX_INPUT 0x04
209 
214 #define BALANCER5_SLAVE_ADDRESS 0x6A
215  // End group macro
219 // --------------------------------------------------------------- PUBLIC TYPES
228 typedef struct
229 {
230  // Output pins
231 
232  digital_out_t cd;
233 
234 
235  // Input pins
236 
237  digital_in_t pg;
238  digital_in_t pss;
239  digital_in_t int_pin;
240 
241  // Modules
242 
243  i2c_master_t i2c;
244 
245  // ctx variable
246 
247  uint8_t slave_address;
248 
249 } balancer5_t;
250 
254 typedef struct
255 {
256  // Communication gpio pins
257 
258  pin_name_t scl;
259  pin_name_t sda;
260 
261  // Additional gpio pins
262 
263  pin_name_t pg;
264  pin_name_t pss;
265  pin_name_t cd;
266  pin_name_t int_pin;
267 
268  // static variable
269 
270  uint32_t i2c_speed;
271  uint8_t i2c_address;
272 
274  // End types group
276 
277 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
278 
284 #ifdef __cplusplus
285 extern "C"{
286 #endif
287 
297 
306 
320 
331 void balancer5_generic_write ( balancer5_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
332 
344 void balancer5_generic_read ( balancer5_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
345 
356 uint8_t balancer5_read_data( balancer5_t *ctx, uint8_t reg_addr );
357 
358 
368 void balancer5_write_data ( balancer5_t *ctx, uint8_t reg_addr, uint8_t write_data );
369 
378 void balancer5_charge ( balancer5_t *ctx, uint8_t state );
379 
390 
401 
412 
423 
424 
425 #ifdef __cplusplus
426 }
427 #endif
428 #endif // _BALANCER5_H_
429  // End public_function group
432 
433 // ------------------------------------------------------------------------- END
BALANCER5_RETVAL
#define BALANCER5_RETVAL
Definition: balancer5.h:76
balancer5_t::pss
digital_in_t pss
Definition: balancer5.h:238
balancer5_cfg_t
Click configuration structure definition.
Definition: balancer5.h:255
balancer5_read_data
uint8_t balancer5_read_data(balancer5_t *ctx, uint8_t reg_addr)
Generic function for reading one Byte data from registar.
balancer5_t
Click ctx object definition.
Definition: balancer5.h:229
balancer5_cfg_t::scl
pin_name_t scl
Definition: balancer5.h:258
balancer5_cfg_t::sda
pin_name_t sda
Definition: balancer5.h:259
balancer5_cfg_t::cd
pin_name_t cd
Definition: balancer5.h:265
balancer5_write_data
void balancer5_write_data(balancer5_t *ctx, uint8_t reg_addr, uint8_t write_data)
Generic function for writing one Byte data to registar.
balancer5_cfg_t::pss
pin_name_t pss
Definition: balancer5.h:264
balancer5_cfg_t::int_pin
pin_name_t int_pin
Definition: balancer5.h:266
balancer5_t::pg
digital_in_t pg
Definition: balancer5.h:237
balancer5_get_int_status
uint8_t balancer5_get_int_status(balancer5_t *ctx)
Gets state of the int pin.
balancer5_check_id
uint8_t balancer5_check_id(balancer5_t *ctx)
Checks if device ID is good.
balancer5_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: balancer5.h:270
balancer5_generic_write
void balancer5_generic_write(balancer5_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
balancer5_cfg_setup
void balancer5_cfg_setup(balancer5_cfg_t *cfg)
Config Object Initialization function.
balancer5_cfg_t::i2c_address
uint8_t i2c_address
Definition: balancer5.h:271
balancer5_default_cfg
void balancer5_default_cfg(balancer5_t *ctx)
Click Default Configuration function.
balancer5_t::slave_address
uint8_t slave_address
Definition: balancer5.h:247
balancer5_init
BALANCER5_RETVAL balancer5_init(balancer5_t *ctx, balancer5_cfg_t *cfg)
Initialization function.
balancer5_cfg_t::pg
pin_name_t pg
Definition: balancer5.h:263
balancer5_t::int_pin
digital_in_t int_pin
Definition: balancer5.h:239
balancer5_get_power_good_status
uint8_t balancer5_get_power_good_status(balancer5_t *ctx)
Gets state of the pg pin on an.
balancer5_generic_read
void balancer5_generic_read(balancer5_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
balancer5_get_power_source_status
uint8_t balancer5_get_power_source_status(balancer5_t *ctx)
Gets state of the psel pin on rst.
balancer5_charge
void balancer5_charge(balancer5_t *ctx, uint8_t state)
Function for setting charging status.
balancer5_t::i2c
i2c_master_t i2c
Definition: balancer5.h:243
balancer5_t::cd
digital_out_t cd
Definition: balancer5.h:232