charger2  2.0.0.0
charger2.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 CHARGER2_H
36 #define CHARGER2_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 CHARGER2_MAP_MIKROBUS( cfg, mikrobus ) \
63  cfg.scl = MIKROBUS( mikrobus, MIKROBUS_SCL ); \
64  cfg.sda = MIKROBUS( mikrobus, MIKROBUS_SDA ); \
65 
71 #define CHARGER2_RETVAL uint8_t
72 
73 #define CHARGER2_OK 0x00
74 #define CHARGER2_INIT_ERROR 0xFF
75 
81 #define R_SHUNT 0.05
82 
88 #define CHARGER2_REG_MODE 0x00
89 #define CHARGER2_REG_CONTROL 0x01
90 #define CHARGER2_REG_CHARGE_LOW 0x02
91 #define CHARGER2_REG_CHARGE_HIGH 0x03
92 #define CHARGER2_REG_COUNTER_LOW 0x04
93 #define CHARGER2_REG_COUNTER_HIGH 0x05
94 #define CHARGER2_REG_CURRENT_LOW 0x06
95 #define CHARGER2_REG_CURRENT_HIGH 0x07
96 #define CHARGER2_REG_VOLTAGE_LOW 0x08
97 #define CHARGER2_REG_VOLTAGE_HIGH 0x09
98 #define CHARGER2_REG_TEMPERATURE_LOW 0x0A
99 #define CHARGER2_REG_TEMPERATURE_HIGH 0x0B
100 #define CHARGER2_REG_ID0 0x18
101 #define CHARGER2_REG_ID1 0x19
102 #define CHARGER2_REG_ID2 0x1A
103 #define CHARGER2_REG_ID3 0x1B
104 #define CHARGER2_REG_ID4 0x1C
105 #define CHARGER2_REG_ID5 0x1D
106 #define CHARGER2_REG_ID6 0x1E
107 #define CHARGER2_REG_ID7 0x1F
108 #define CHARGER2_REG_RAM0 0x20
109 #define CHARGER2_REG_RAM1 0x21
110 #define CHARGER2_REG_RAM2 0x22
111 #define CHARGER2_REG_RAM3 0x23
112 #define CHARGER2_REG_RAM4 0x24
113 #define CHARGER2_REG_RAM5 0x25
114 #define CHARGER2_REG_RAM6 0x26
115 #define CHARGER2_REG_RAM7 0x27
116 #define CHARGER2_REG_RAM8 0x28
117 #define CHARGER2_REG_RAM9 0x29
118 #define CHARGER2_REG_RAM10 0x2A
119 #define CHARGER2_REG_RAM11 0x2B
120 #define CHARGER2_REG_RAM12 0x2C
121 #define CHARGER2_REG_RAM13 0x2D
122 #define CHARGER2_REG_RAM14 0x2E
123 #define CHARGER2_REG_RAM15 0x2F
124 #define CHARGER2_REG_RAM16 0x30
125 #define CHARGER2_REG_RAM17 0x31
126 #define CHARGER2_REG_RAM18 0x32
127 #define CHARGER2_REG_RAM19 0x33
128 #define CHARGER2_REG_RAM20 0x34
129 #define CHARGER2_REG_RAM21 0x35
130 #define CHARGER2_REG_RAM22 0x36
131 #define CHARGER2_REG_RAM23 0x37
132 #define CHARGER2_REG_RAM24 0x38
133 #define CHARGER2_REG_RAM25 0x39
134 #define CHARGER2_REG_RAM26 0x3A
135 #define CHARGER2_REG_RAM27 0x3B
136 #define CHARGER2_REG_RAM28 0x3C
137 #define CHARGER2_REG_RAM29 0x3D
138 #define CHARGER2_REG_RAM30 0x3E
139 #define CHARGER2_REG_RAM31 0x3F
140 
146 #define CHARGER2_AUTO_DETECT 0x00
147 #define CHARGER2_EXTERNAL_CLOCK 0x01
148 #define CHARGER2_14_BITS_RESOLUTION 0x00
149 #define CHARGER2_13_BITS_RESOLUTION 0x02
150 #define CHARGER2_12_BITS_RESOLUTION 0x04
151 #define CHARGER2_CALIBRATE_ADC 0x08
152 #define CHARGER2_STANDBY_MODE 0x00
153 #define CHARGER2_OPERATING_MODE 0x10
154 
160 #define CHARGER2_IO0_OUTPUT_LOW 0x00
161 #define CHARGER2_IO0_OUTPUT_OPEN 0x01
162 #define CHARGER2_RESET_CHARGE_ACCUM_AND_COUNTER 0x02
163 #define CHARGER2_RELEASE_SOFT_RESET 0x00
164 #define CHARGER2_ASSERT_SOFT_RESET 0x10
165 
171 #define CHARGER2_RESET_COUNTER_MODE 0x00
172 #define CHARGER2_SOFT_RESET_MODE 0x01
173 #define CHARGER2_CURRENT_CYCLE_CHECK 0x00
174 #define CHARGER2_VOLTAGE_TEMP_CYCLE_CHECK 0x01
175 #define CHARGER2_GAS_GAUGE_CHARGE_DATA 0x00
176 #define CHARGER2_CONV_NUMBER 0x01
177 #define CHARGER2_SHUNT_VOLTAGE 0x02
178 #define CHARGER2_BATTERY_VOLTAGE 0x03
179 #define CHARGER2_TEMPERATURE 0x04
180 #define CHARGER2_BATTERY_CURRENT 0x05
181  // End group macro
184 // --------------------------------------------------------------- PUBLIC TYPES
193 typedef struct
194 {
195  // Modules
196 
197  i2c_master_t i2c;
198 
199  // ctx variable
200 
201  uint8_t slave_address;
202 
203  uint8_t unit_mode;
204 
205 } charger2_t;
206 
210 typedef struct
211 {
212  // Communication gpio pins
213 
214  pin_name_t scl;
215  pin_name_t sda;
216 
217  // static variable
218 
219  uint32_t i2c_speed;
220  uint8_t i2c_address;
221 
223  // End types group
225 // ----------------------------------------------- PUBLIC FUNCTION DECLARATIONS
226 
232 #ifdef __cplusplus
233 extern "C"{
234 #endif
235 
245 
254 
265 void charger2_generic_write ( charger2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
266 
277 void charger2_generic_read ( charger2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len );
278 
288 uint8_t charger2_write_reg ( charger2_t *ctx, uint8_t register_address, uint8_t transfer_data );
289 
300 uint8_t charger2_read_reg ( charger2_t *ctx, uint8_t register_address, uint8_t *value_out, uint8_t n_bytes );
301 
311 uint8_t charger2_read_data ( charger2_t *ctx, uint8_t data_mode, uint32_t *data_out );
312 
321 uint8_t charger2_reset ( charger2_t *ctx, uint8_t reset_mode );
322 
332 uint8_t charger2_check_conv_cycle( charger2_t *ctx, uint8_t conv_mode );
333 
334 #ifdef __cplusplus
335 }
336 #endif
337 #endif // _CHARGER2_H_
338  // End public_function group
341 
342 // ------------------------------------------------------------------------- END
charger2_read_reg
uint8_t charger2_read_reg(charger2_t *ctx, uint8_t register_address, uint8_t *value_out, uint8_t n_bytes)
Read reg function.
charger2_read_data
uint8_t charger2_read_data(charger2_t *ctx, uint8_t data_mode, uint32_t *data_out)
Data Read function.
charger2_cfg_t
Click configuration structure definition.
Definition: charger2.h:211
charger2_cfg_t::scl
pin_name_t scl
Definition: charger2.h:214
charger2_cfg_t::i2c_address
uint8_t i2c_address
Definition: charger2.h:220
charger2_generic_read
void charger2_generic_read(charger2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic read function.
charger2_generic_write
void charger2_generic_write(charger2_t *ctx, uint8_t reg, uint8_t *data_buf, uint8_t len)
Generic write function.
charger2_t::i2c
i2c_master_t i2c
Definition: charger2.h:197
charger2_write_reg
uint8_t charger2_write_reg(charger2_t *ctx, uint8_t register_address, uint8_t transfer_data)
Write reg function.
charger2_t::unit_mode
uint8_t unit_mode
Definition: charger2.h:203
charger2_cfg_t::i2c_speed
uint32_t i2c_speed
Definition: charger2.h:219
charger2_check_conv_cycle
uint8_t charger2_check_conv_cycle(charger2_t *ctx, uint8_t conv_mode)
Conversion Cycle Check function.
charger2_cfg_t::sda
pin_name_t sda
Definition: charger2.h:215
charger2_t::slave_address
uint8_t slave_address
Definition: charger2.h:201
charger2_t
Click ctx object definition.
Definition: charger2.h:194
charger2_init
CHARGER2_RETVAL charger2_init(charger2_t *ctx, charger2_cfg_t *cfg)
Initialization function.
charger2_reset
uint8_t charger2_reset(charger2_t *ctx, uint8_t reset_mode)
Reset function.
CHARGER2_RETVAL
#define CHARGER2_RETVAL
Definition: charger2.h:71
charger2_cfg_setup
void charger2_cfg_setup(charger2_cfg_t *cfg)
Config Object Initialization function.