-
Notifications
You must be signed in to change notification settings - Fork 1
/
bq25180.c
316 lines (257 loc) · 7.42 KB
/
bq25180.c
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
/*
* SPDX-FileCopyrightText: 2022 Kyunghwan Kwon <[email protected]>
*
* SPDX-License-Identifier: MIT
*/
#include "bq25180.h"
#include "bq25180_overrides.h"
#include <string.h>
#if !defined(assert)
#define assert(exp)
#endif
#if !defined(MIN)
#define MIN(a, b) (((a) > (b))? (b) : (a))
#endif
#define MIN_BAT_REG_mV 3500U
#define MAX_BAT_REG_mV 4650U
#define MIN_BAT_UNDERVOLTAGE_mV 2000U
#define MAX_BAT_UNDERVOLTAGE_mV 3000U
#define MIN_IN_CURR_mA 5U
#define MAX_IN_CURR_mA 1000U
enum registers {
STAT0 = 0, /* Charger Status */
STAT1, /* Charger Status and Faults */
FLAG0, /* Charger Flag Registers */
VBAT_CTRL, /* Battery Voltage Control */
ICHG_CTRL, /* Fast Charge Current Control */
CHARGECTRL0, /* Charger Control 0 */
CHARGECTRL1, /* Charger Control 1 */
IC_CTRL, /* IC Control */
TMR_ILIM, /* Timer and Input Current Limit Control */
SHIP_RST, /* Shipmode, Reset and Pushbutton Control */
SYS_REG, /* SYS Regulation Voltage Control */
TS_CONTROL, /* TS Control */
MASK_ID, /* MASK and Device ID */
};
static bool write_reg(uint8_t reg, uint8_t val)
{
return bq25180_write(BQ25180_DEVICE_ADDRESS, reg, &val, 1) >= 0;
}
static bool read_reg(uint8_t reg, uint8_t *p)
{
return bq25180_read(BQ25180_DEVICE_ADDRESS, reg, p, 1) >= 0;
}
static void set_reg(uint8_t reg, uint8_t bit, uint8_t mask, uint8_t val)
{
uint8_t tmp;
read_reg(reg, &tmp);
tmp = tmp & (uint8_t)~(mask << bit);
tmp = tmp | (uint8_t)(val << bit);
write_reg(reg, tmp);
}
static void set_interrupts(uint8_t mask, uint8_t enable)
{
if (mask & BQ25180_INTR_CHARGING_STATUS) {
set_reg(CHARGECTRL1, 2, 1, !enable); /* CHG_STATUS_INT_MASK */
}
if (mask & BQ25180_INTR_CURRENT_LIMIT) {
set_reg(CHARGECTRL1, 1, 1, !enable); /* ILIM_INT_MASK */
}
if (mask & BQ25180_INTR_VDPM) {
set_reg(CHARGECTRL1, 0, 1, !enable); /* VDPM_INT_MASK */
}
if (mask & BQ25180_INTR_THERMAL_FAULT) {
set_reg(MASK_ID, 7, 1, !enable); /* TS_INT_MASK */
}
if (mask & BQ25180_INTR_THERMAL_REGULATION) {
set_reg(MASK_ID, 6, 1, !enable); /* TREG_INT_MASK */
}
if (mask & BQ25180_INTR_BATTERY_RANGE) {
set_reg(MASK_ID, 5, 1, !enable); /* BAT_INT_MASK */
}
if (mask & BQ25180_INTR_POWER_ERROR) {
set_reg(MASK_ID, 4, 1, !enable); /* PG_INT_MASK */
}
}
void bq25180_reset(bool hardware_reset)
{
if (hardware_reset) {
set_reg(SHIP_RST, 5, 3, 3); /* EN_RST_SHIP */
} else {
set_reg(SHIP_RST, 7, 1, 1); /* REG_RST */
}
}
bool bq25180_read_event(struct bq25180_event *p)
{
uint8_t val;
assert(p != NULL);
if (!read_reg(FLAG0, &val)) {
return false;
}
memset(p, 0, sizeof(*p));
p->battery_overcurrent = val & 1U; /* BAT_OCP_FAULT */
p->battery_undervoltage = (val >> 1) & 1U; /* BUVLO_FAULT_FLAG */
p->input_overvoltage = (val >> 2) & 1U; /* VIN_OVP_FAULT_FLAG */
p->thermal_regulation = (val >> 3) & 1U; /* THERMREG_ACTIVE_FLAG */
p->vindpm_fault = (val >> 4) & 1U; /* VINDPM_ACTIVE_FLAG */
p->vdppm_fault = (val >> 5) & 1U; /* VDPPM_ACTIVE_FLAG */
p->ilim_fault = (val >> 6) & 1U; /* ILIM_ACTIVE_FLAG */
p->battery_thermal_fault = (val >> 7) & 1U; /* TS_FAULT */
return true;
}
bool bq25180_read_state(struct bq25180_state *p)
{
uint8_t val0, val1;
assert(p != NULL);
if (!read_reg(STAT0, &val0) || !read_reg(STAT1, &val1)) {
return false;
}
memset(p, 0, sizeof(*p));
p->vin_good = val0 & 1U; /* VIN_PGOOD_STAT */
p->thermal_regulation_active = (val0 >> 1) & 1U; /* THERMREG_ACTIVE_STAT */
p->vindpm_active = (val0 >> 2) & 1U; /* VINDPM_ACTIVE_STAT */
p->vdppm_active = (val0 >> 3) & 1U; /* VDPPM_ACTIVE_STAT */
p->ilim_active = (val0 >> 4) & 1U; /* ILIM_ACTIVE_STAT */
p->charging_status = (val0 >> 5) & 3U; /* CHG_STAT */
p->tsmr_open = (val0 >> 7) & 1U; /* TS_OPEN_STAT */
p->wake2_raised = val1 & 1U; /* WAKE2_FLAG */
p->wake1_raised = (val1 >> 1) & 1U; /* WAKE1_FLAG */
p->safety_timer_fault = (val1 >> 2) & 1U; /* SAFETY_TMR_FAULT_FLAG */
p->ts_status = (val1 >> 3) & 3U; /* TS_STAT */
p->battery_undervoltage_active = (val1 >> 6) & 1U; /* BUVLO_START */
p->vin_overvoltage_active = (val1 >> 7) & 1U; /* VIN_OVP_STAT */
return true;
}
void bq25180_enable_battery_charging(bool enable)
{
set_reg(ICHG_CTRL, 7, 1, !enable); /* CHG_DIS */
}
void bq25180_set_safety_timer(enum bq25180_safety_timer opt)
{
/* TODO: support IC_CTRL.2XTMR_EN */
set_reg(IC_CTRL, 2, 3, (uint8_t)opt); /* SAFETY_TIMER */
}
void bq25180_set_watchdog_timer(enum bq25180_watchdog opt)
{
/* TODO: support SYS_REG.WATCHDOG_15S_ENABLE */
set_reg(IC_CTRL, 0, 3, (uint8_t)opt); /* WATCHDOG_SEL */
}
void bq25180_set_battery_regulation_voltage(uint16_t millivoltage)
{
assert(millivoltage >= MIN_BAT_REG_mV &&
millivoltage <= MAX_BAT_REG_mV);
uint8_t reg = (uint8_t)((millivoltage - MIN_BAT_REG_mV) / 10);
write_reg(VBAT_CTRL, reg);
}
void bq25180_set_battery_discharge_current(
enum bq25180_bat_discharge_current opt)
{
set_reg(CHARGECTRL1, 6, 3, (uint8_t)opt); /* IBAT_OCP */
}
void bq25180_set_battery_under_voltage(uint16_t millivoltage)
{
uint8_t val;
assert(millivoltage >= MIN_BAT_UNDERVOLTAGE_mV &&
millivoltage <= MAX_BAT_UNDERVOLTAGE_mV);
if (millivoltage > 2800) {
val = 2;
} else if (millivoltage > 2600) {
val = 3;
} else if (millivoltage > 2400) {
val = 4;
} else if (millivoltage > 2200) {
val = 5;
} else if (millivoltage > 2000) {
val = 6;
} else {
val = 7;
}
set_reg(CHARGECTRL1, 3, 7, val); /* UVLO */
}
void bq25180_set_precharge_threshold(uint16_t millivoltage)
{
uint8_t val = 0;
if (millivoltage <= 2800) {
val = 1;
}
set_reg(IC_CTRL, 6, 1, val); /* VLOWV_SEL */
}
void bq25180_set_precharge_current(bool double_termination_current)
{
set_reg(CHARGECTRL0, 6, 1, !double_termination_current); /* IPRECHG */
}
void bq25180_set_fastcharge_current(uint16_t milliampere)
{
assert(milliampere >= MIN_IN_CURR_mA && milliampere <= MAX_IN_CURR_mA);
uint8_t val = (uint8_t)(milliampere - MIN_IN_CURR_mA);
if (milliampere > 35) {
/* NOTE: 36mA to 39mA not in the range.
* See the datasheet: Table 8-13. */
val = MIN((uint8_t)(milliampere / 10 + 27), 127/*1000mA*/);
}
set_reg(ICHG_CTRL, 0, 0x7f, val); /* ICHG */
}
void bq25180_set_termination_current(uint8_t pct)
{
uint8_t val = 0;
if (pct >= 20) {
val = 3;
} else if (pct >= 10) {
val = 2;
} else if (pct >= 5) {
val = 1;
}
set_reg(CHARGECTRL0, 4, 3, val); /* ITERM */
}
void bq25180_enable_vindpm(enum bq25180_vindpm opt)
{
set_reg(CHARGECTRL0, 2, 3, (uint8_t)opt); /* VINDPM */
}
void bq25180_enable_dppm(bool enable)
{
set_reg(SYS_REG, 0, 1, !enable); /* VDPPM_DIS */
}
void bq25180_set_input_current(uint16_t milliampere)
{
uint8_t val = 0;
if (milliampere >= 1100) {
val = 7;
} else if (milliampere >= 700) {
val = 6;
} else if (milliampere >= 500) {
val = 5;
} else if (milliampere >= 400) {
val = 4;
} else if (milliampere >= 300) {
val = 3;
} else if (milliampere >= 200) {
val = 2;
} else if (milliampere >= 100) {
val = 1;
}
set_reg(TMR_ILIM, 0, 7, val); /* ILIM */
}
void bq25180_set_sys_source(enum bq25180_sys_source source)
{
set_reg(SYS_REG, 2, 3, (uint8_t)source); /* SYS_MODE */
}
void bq25180_set_sys_voltage(enum bq25180_sys_regulation val)
{
set_reg(SYS_REG, 5, 7, (uint8_t)val); /* SYS_REG_CTRL */
}
void bq25180_enable_thermal_protection(bool enable)
{
set_reg(IC_CTRL, 7, 1, enable); /* TS_EN */
}
void bq25180_enable_push_button(bool enable)
{
set_reg(SHIP_RST, 0, 1, enable); /* EN_PUSH */
}
void bq25180_enable_interrupt(uint8_t mask)
{
set_interrupts(mask, 1);
}
void bq25180_disable_interrupt(uint8_t mask)
{
set_interrupts(mask, 0);
}