|
22 | 22 | #include <pbdrv/adc.h> |
23 | 23 | #include <pbdrv/charger.h> |
24 | 24 | #include <pbdrv/gpio.h> |
25 | | -#if PBDRV_CONFIG_CHARGER_MP2639A_MODE_PWM | PBDRV_CONFIG_CHARGER_MP2639A_ISET_PWM |
26 | 25 | #include <pbdrv/pwm.h> |
27 | | -#endif |
28 | | -#if PBDRV_CONFIG_CHARGER_MP2639A_CHG_RESISTOR_LADDER |
29 | 26 | #include <pbdrv/resistor_ladder.h> |
30 | | -#endif |
| 27 | +#include <pbdrv/usb.h> |
31 | 28 | #include <pbio/error.h> |
32 | 29 | #include <pbio/util.h> |
33 | 30 |
|
@@ -70,7 +67,13 @@ pbdrv_charger_status_t pbdrv_charger_get_status(void) { |
70 | 67 | return pbdrv_charger_status; |
71 | 68 | } |
72 | 69 |
|
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) { |
74 | 77 | #if PBDRV_CONFIG_CHARGER_MP2639A_ISET_PWM |
75 | 78 |
|
76 | 79 | // 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) { |
116 | 119 | mode_pin_is_low = enable; |
117 | 120 | } |
118 | 121 |
|
| 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 | + |
119 | 148 | /** |
120 | 149 | * Gets the current CHG signal status (inverted compared to /CHG pin state). |
121 | 150 | */ |
@@ -174,6 +203,11 @@ PROCESS_THREAD(pbdrv_charger_mp2639a_process, ev, data) { |
174 | 203 | PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(&timer)); |
175 | 204 | etimer_restart(&timer); |
176 | 205 |
|
| 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 | + |
177 | 211 | chg_samples[chg_index] = read_chg(); |
178 | 212 |
|
179 | 213 | if (mode_pin_is_low) { |
|
0 commit comments