-
Notifications
You must be signed in to change notification settings - Fork 1
/
bq25180.h
346 lines (308 loc) · 8.84 KB
/
bq25180.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/*
* SPDX-FileCopyrightText: 2022 Kyunghwan Kwon <[email protected]>
*
* SPDX-License-Identifier: MIT
*/
#ifndef LIBMCU_BQ25180_H
#define LIBMCU_BQ25180_H
#if defined(__cplusplus)
extern "C" {
#endif
#include <stdbool.h>
#include <stdint.h>
#include <stddef.h>
#define BQ25180_DEVICE_ADDRESS 0x6A /* 7-bit addressing only */
enum bq25180_sys_source {
BQ25180_SYS_SRC_VIN_VBAT, /**< Powered from VIN if present or VBAT */
BQ25180_SYS_SRC_VBAT, /**< Powered from VBAT only, even if VIN present */
BQ25180_SYS_SRC_NONE_FLOATING, /**< Disconnected and left floating */
BQ25180_SYS_SRC_NONE_PULLDOWN, /**< Disconnected with pulldown */
};
enum bq25180_sys_regulation {
BQ25180_SYS_REG_VBAT, /**< VBAT + 225 mV (3.8 V minimum) */
BQ25180_SYS_REG_V4_4, /**< 4.4V */
BQ25180_SYS_REG_V4_5, /**< 4.5V */
BQ25180_SYS_REG_V4_6, /**< 4.6V */
BQ25180_SYS_REG_V4_7, /**< 4.7V */
BQ25180_SYS_REG_V4_8, /**< 4.8V */
BQ25180_SYS_REG_V4_9, /**< 4.9V */
BQ25180_SYS_REG_PASS_THROUGH, /**< Pass through */
};
enum bq25180_bat_discharge_current {
BQ25180_BAT_DISCHAGE_500mA,
BQ25180_BAT_DISCHAGE_1000mA,
BQ25180_BAT_DISCHAGE_1500mA,
BQ25180_BAT_DISCHAGE_DISABLE,
};
enum bq25180_safety_timer {
BQ25180_SAFETY_3H, /**< 3 hour fast charge */
BQ25180_SAFETY_6H, /**< 6 hour fast charge */
BQ25180_SAFETY_12H, /**< 12 hour fast charge */
BQ25180_SAFETY_DISABLE, /**< disable safty timer */
};
enum bq25180_watchdog {
BQ25180_WDT_DEFAULT, /**< 160s hardware reset */
BQ25180_WDT_160_SEC, /**< 160s hardware reset */
BQ25180_WDT_40_SEC, /**< 40s hardware reset */
BQ25180_WDT_DISABLE, /**< Disable watchdog function */
};
enum bq25180_vindpm {
BQ25180_VINDPM_4200mV,
BQ25180_VINDPM_4500mV,
BQ25180_VINDPM_4700mV,
BQ25180_VINDPM_DISABLE,
};
enum bq25180_intr {
BQ25180_INTR_CHARGING_STATUS = 0x01,
BQ25180_INTR_CURRENT_LIMIT = 0x02,
BQ25180_INTR_VDPM = 0x04,
BQ25180_INTR_THERMAL_FAULT = 0x08,
BQ25180_INTR_THERMAL_REGULATION = 0x10,
BQ25180_INTR_BATTERY_RANGE = 0x20,
BQ25180_INTR_POWER_ERROR = 0x40,
BQ25180_INTR_ALL = (
BQ25180_INTR_CHARGING_STATUS |
BQ25180_INTR_CURRENT_LIMIT |
BQ25180_INTR_VDPM |
BQ25180_INTR_THERMAL_FAULT |
BQ25180_INTR_THERMAL_REGULATION |
BQ25180_INTR_BATTERY_RANGE |
BQ25180_INTR_POWER_ERROR
),
};
struct bq25180_event {
uint8_t battery_overcurrent : 1;
uint8_t battery_undervoltage : 1;
uint8_t input_overvoltage : 1;
uint8_t thermal_regulation : 1;
uint8_t vindpm_fault : 1;
uint8_t vdppm_fault : 1;
uint8_t ilim_fault : 1;
uint8_t battery_thermal_fault: 1;
};
struct bq25180_state {
uint16_t vin_good : 1;
uint16_t thermal_regulation_active : 1;
uint16_t vindpm_active : 1;
uint16_t vdppm_active : 1;
uint16_t ilim_active : 1;
uint16_t charging_status : 2;
uint16_t tsmr_open : 1;
uint16_t wake2_raised : 1;
uint16_t wake1_raised : 1;
uint16_t safety_timer_fault : 1;
uint16_t ts_status : 2;
uint16_t battery_undervoltage_active: 1;
uint16_t vin_overvoltage_active : 1;
};
/**
* @brief Reset the system
*
* The device will reset all of the registers to the defaults. A hardware reset
* to completely powercycle the system.
*
* @param[in] hardware_reset hardware reset if true while soft reset if false
*
* @note A hardware or software reset will cancel the pending shipmode request.
*/
void bq25180_reset(bool hardware_reset);
/**
* @brief Read the device events
*
* @param[in] p @ref bq25180_event
*
* @return true on success or false
*/
bool bq25180_read_event(struct bq25180_event *p);
/**
* @brief Read the device state
*
* @param[in] p @ref bq25180_state
*
* @return true on success or false
*/
bool bq25180_read_state(struct bq25180_state *p);
/**
* @brief Enable or disable battery charging
*
* @param[in] enable battery charging to be enabled if true or false to be
* disabled
*/
void bq25180_enable_battery_charging(bool enable);
/**
* @brief Set the safety time
*
* If charging has not terminated before the programmed safety time, charging
* is disabled.
*
* When the safety timer is active, changing the safety timer duration resets
* the safety timer.
*
* 6 hour by default on reset.
*
* @param[in] opt one of @ref bq25180_safety_timer
*/
void bq25180_set_safety_timer(enum bq25180_safety_timer opt);
/**
* @brief Set the watchdog time
*
* Once the initial transaction is received, the watchdog timer is started. The
* watchdog timer is reset by any transaction by the host using the I2C
* interface. If the watchdog timer expires without a reset from the I2C
* interface, all charger parameters registers are reset to the default values.
*
* 160 sec by default on reset.
*
* @param[in] opt one of @ref bq25180_watchdog
*/
void bq25180_set_watchdog_timer(enum bq25180_watchdog opt);
/**
* @brief Set the battery regulation target
*
* The charging current starts tapering off once the battery voltage approches
* the battery regulation target.
*
* 4200mV by default on reset.
*
* @param[in] millivoltage from 3500mV up to 4650mV
*/
void bq25180_set_battery_regulation_voltage(uint16_t millivoltage);
/**
* @brief Set the battery discharge current limit
*
* @ref BQ25180_BAT_DISCHAGE_500mA by default on reset.
*
* @param[in] opt one of @ref bq25180_bat_discharge_current
*/
void bq25180_set_battery_discharge_current(
enum bq25180_bat_discharge_current opt);
/**
* @brief Set battery undervoltage lockout falling threshold
*
* When voltage at the battery drops below the configured, the BAT and SYS path
* gets disengaged.
*
* The below levels are supported:
* - 3000mV
* - 2800mV
* - 2600mV
* - 2400mV
* - 2200mV
* - 2000mV
*
* 3000mV by default on reset.
*
* @param[in] millivoltage from 2000mV up to 3000mV
*/
void bq25180_set_battery_under_voltage(uint16_t millivoltage);
/**
* @brief Set the precharge voltage threshold
*
* 3000mV by default on reset.
*
* @param[in] millivoltage either of 2800mV or 3000mV only
*/
void bq25180_set_precharge_threshold(uint16_t millivoltage);
/**
* @brief Set the precharge current
*
* The precharge current is the same to the termination current by default on
* reset.
*
* @param[in] double_termination_current double of termination current if true
* or same to the termination current if false
*/
void bq25180_set_precharge_current(bool double_termination_current);
/**
* @brief Set the maximum charge current level
*
* 10mA by default on reset.
*
* @param[in] milliampere from 5mA up to 1000mA
*/
void bq25180_set_fastcharge_current(uint16_t milliampere);
/**
* @brief Set the termination current
*
* 10% by default on reset.
*
* @param[in] pct percentage of the maximum charge current level
*/
void bq25180_set_termination_current(uint8_t pct);
/**
* @brief Enable or disable Input Voltage Based Dynamic Power Management
*
* This sets input voltage threshold to keep the input voltage higher than
* configured.
*
* This feature is disbled by default on reset.
*
* @param[in] opt @ref bq25180_vindpm
*/
void bq25180_enable_vindpm(enum bq25180_vindpm opt);
/**
* @brief Enable of disable Dynamic Power Path Management Mode
*
* This feature is enabled by default on reset.
*
* @param[in] enable true to enable, false to disable
*/
void bq25180_enable_dppm(bool enable);
/**
* @brief Set the maximum input current
*
* The input DPM loop reduces input current if the sum of the charging and load
* currents exceeds the preset maximum input current.
*
* 500mA by default on reset.
*
* @param[in] milliampere from 50mA up to 1100mA
*/
void bq25180_set_input_current(uint16_t milliampere);
/**
* @brief Set regulated system voltage source
*
* This sets how SYS is powered in any state, except SHIPMODE.
*
* @ref BQ25180_SYS_SRC_VIN_VBAT by default on reset.
*
* @param[in] source one of @ref bq25180_sys_source
*/
void bq25180_set_sys_source(enum bq25180_sys_source source);
/**
* @brief Set SYS regulation voltgage
*
* @ref BQ25180_SYS_REG_V4_4 by default on reset.
*
* @param[in] val one of @ref bq25180_sys_regulation
*/
void bq25180_set_sys_voltage(enum bq25180_sys_regulation val);
/**
* @brief Enable or disable thermal protection
*
* @param[in] enable enable if true or disable if false
*/
void bq25180_enable_thermal_protection(bool enable);
/**
* @brief Enable or disable push button on battery only
*
* @param[in] enable enable if true or disable if false
*/
void bq25180_enable_push_button(bool enable);
/**
* @brief Enable interrupts
*
* @param[in] mask combined interrupt mask @ref bq25180_intr
*/
void bq25180_enable_interrupt(uint8_t mask);
/**
* @brief Disable interrupts
*
* @param[in] mask combined interrupt mask @ref bq25180_intr
*/
void bq25180_disable_interrupt(uint8_t mask);
/* TODO: Implement bq25180_shutdown_mode(void) */
#if defined(__cplusplus)
}
#endif
#endif /* LIBMCU_BQ25180_H */