Skip to content

Commit f49d482

Browse files
committed
pbio/drv/charger: Move charge enable logic.
The charger module should be in charge of charging, not the battery module. This ensures that the battery status getter is synchronized with the actual battery state. Before this commit, the battery could be charging before it was reflected in the charging state. This will also give us more control to disable charging under certain charger error state conditions in the next commit.
1 parent 77384ec commit f49d482

File tree

3 files changed

+39
-41
lines changed

3 files changed

+39
-41
lines changed

lib/pbio/drv/charger/charger_mp2639a.c

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,9 @@
2222
#include <pbdrv/adc.h>
2323
#include <pbdrv/charger.h>
2424
#include <pbdrv/gpio.h>
25-
#if PBDRV_CONFIG_CHARGER_MP2639A_MODE_PWM | PBDRV_CONFIG_CHARGER_MP2639A_ISET_PWM
2625
#include <pbdrv/pwm.h>
27-
#endif
28-
#if PBDRV_CONFIG_CHARGER_MP2639A_CHG_RESISTOR_LADDER
2926
#include <pbdrv/resistor_ladder.h>
30-
#endif
27+
#include <pbdrv/usb.h>
3128
#include <pbio/error.h>
3229
#include <pbio/util.h>
3330

@@ -70,7 +67,13 @@ pbdrv_charger_status_t pbdrv_charger_get_status(void) {
7067
return pbdrv_charger_status;
7168
}
7269

73-
void pbdrv_charger_enable(bool enable, pbdrv_charger_limit_t limit) {
70+
/**
71+
* Enables or disables the charger.
72+
*
73+
* @param enable True to enable the charger, false to disable.
74+
* @param limit The current limit to set. Only used on some platforms.
75+
*/
76+
static void pbdrv_charger_enable(bool enable, pbdrv_charger_limit_t limit) {
7477
#if PBDRV_CONFIG_CHARGER_MP2639A_ISET_PWM
7578

7679
// Set the current limit (ISET) based on the type of charger attached.
@@ -116,6 +119,32 @@ void pbdrv_charger_enable(bool enable, pbdrv_charger_limit_t limit) {
116119
mode_pin_is_low = enable;
117120
}
118121

122+
/**
123+
* Enables the charger if USB is connected, otherwise disables charger.
124+
*/
125+
static void pbdrv_charger_enable_if_usb_connected(void) {
126+
pbdrv_usb_bcd_t bcd = pbdrv_usb_get_bcd();
127+
bool enable = bcd != PBDRV_USB_BCD_NONE;
128+
pbdrv_charger_limit_t limit;
129+
130+
// This battery charger chip will automatically monitor VBUS and
131+
// limit the current if the VBUS voltage starts to drop, so these limits
132+
// are a bit looser than they could be.
133+
switch (bcd) {
134+
case PBDRV_USB_BCD_NONE:
135+
limit = PBDRV_CHARGER_LIMIT_NONE;
136+
break;
137+
case PBDRV_USB_BCD_STANDARD_DOWNSTREAM:
138+
limit = PBDRV_CHARGER_LIMIT_STD_MAX;
139+
break;
140+
default:
141+
limit = PBDRV_CHARGER_LIMIT_CHARGING;
142+
break;
143+
}
144+
145+
pbdrv_charger_enable(enable, limit);
146+
}
147+
119148
/**
120149
* Gets the current CHG signal status (inverted compared to /CHG pin state).
121150
*/
@@ -174,6 +203,11 @@ PROCESS_THREAD(pbdrv_charger_mp2639a_process, ev, data) {
174203
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer));
175204
etimer_restart(&timer);
176205

206+
// Enable charger chip based on USB state. We don't need to disable it
207+
// on charger fault since the chip will automatically disable itself.
208+
// If we disable it here we can't detect the fault condition.
209+
pbdrv_charger_enable_if_usb_connected();
210+
177211
chg_samples[chg_index] = read_chg();
178212

179213
if (mode_pin_is_low) {

lib/pbio/include/pbdrv/charger.h

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -72,13 +72,6 @@ pbio_error_t pbdrv_charger_get_current_now(uint16_t *current);
7272
*/
7373
pbdrv_charger_status_t pbdrv_charger_get_status(void);
7474

75-
/**
76-
* Enables or disables charging.
77-
* @param [in] enable True to enable charging or false for discharging.
78-
* @param [in] limit The current limit for the charging rate.
79-
*/
80-
void pbdrv_charger_enable(bool enable, pbdrv_charger_limit_t limit);
81-
8275
#else
8376

8477
static inline pbio_error_t pbdrv_charger_get_current_now(uint16_t *current) {
@@ -90,9 +83,6 @@ static inline pbdrv_charger_status_t pbdrv_charger_get_status(void) {
9083
return PBDRV_CHARGER_STATUS_FAULT;
9184
}
9285

93-
static inline void pbdrv_charger_enable(bool enable, pbdrv_charger_limit_t limit) {
94-
}
95-
9686
#endif
9787

9888
#endif // _PBDRV_CHARGER_H_

lib/pbio/sys/battery.c

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -99,32 +99,6 @@ void pbsys_battery_poll(void) {
9999
} else if (avg_battery_voltage >= battery_ok_mv) {
100100
pbsys_status_clear(PBIO_PYBRICKS_STATUS_BATTERY_LOW_VOLTAGE_WARNING);
101101
}
102-
103-
// REVISIT: we should be able to make this event driven rather than polled
104-
#if PBDRV_CONFIG_CHARGER
105-
106-
pbdrv_usb_bcd_t bcd = pbdrv_usb_get_bcd();
107-
bool enable = bcd != PBDRV_USB_BCD_NONE;
108-
pbdrv_charger_limit_t limit;
109-
110-
// REVISIT: The only current battery charger chip will automatically monitor
111-
// VBUS and limit the current if the VBUS voltage starts to drop, so these
112-
// limits are a bit looser than they could be.
113-
switch (bcd) {
114-
case PBDRV_USB_BCD_NONE:
115-
limit = PBDRV_CHARGER_LIMIT_NONE;
116-
break;
117-
case PBDRV_USB_BCD_STANDARD_DOWNSTREAM:
118-
limit = PBDRV_CHARGER_LIMIT_STD_MAX;
119-
break;
120-
default:
121-
limit = PBDRV_CHARGER_LIMIT_CHARGING;
122-
break;
123-
}
124-
125-
pbdrv_charger_enable(enable, limit);
126-
127-
#endif // PBDRV_CONFIG_CHARGER
128102
}
129103

130104
/**

0 commit comments

Comments
 (0)