diff options
-rw-r--r-- | board/samus/board.c | 10 | ||||
-rw-r--r-- | common/battery.c | 5 | ||||
-rw-r--r-- | common/charge_state_v1.c | 2 | ||||
-rw-r--r-- | include/battery.h | 10 |
4 files changed, 18 insertions, 9 deletions
diff --git a/board/samus/board.c b/board/samus/board.c index 68f3f787a7..cfab3e9f16 100644 --- a/board/samus/board.c +++ b/board/samus/board.c @@ -8,6 +8,7 @@ #include "adc.h" #include "adc_chip.h" #include "backlight.h" +#include "battery.h" #include "capsense.h" #include "common.h" #include "driver/temp_sensor/tmp006.h" @@ -321,8 +322,13 @@ struct keyboard_scan_config keyscan_config = { /** * Physical check of battery presence. */ -int battery_is_present(void) +enum battery_present battery_is_present(void) { - return adc_read_channel(ADC_CH_BAT_TEMP) < (9 * ADC_READ_MAX / 10); + /* + * This pin has a pullup, so if it's not completely pegged there's + * something attached. Probably a battery. + */ + int analog_val = adc_read_channel(ADC_CH_BAT_TEMP); + return analog_val < (9 * ADC_READ_MAX / 10) ? BP_YES : BP_NO; } #endif diff --git a/common/battery.c b/common/battery.c index 7f318646a3..1f8b757929 100644 --- a/common/battery.c +++ b/common/battery.c @@ -21,9 +21,10 @@ /** * Physical detection of battery. */ -int battery_is_present(void) +enum battery_present battery_is_present(void) { - return (gpio_get_level(CONFIG_BATTERY_PRESENT_GPIO) == 0); + /* The GPIO is low when the battery is present */ + return gpio_get_level(CONFIG_BATTERY_PRESENT_GPIO) ? BP_NO : BP_YES; } #endif diff --git a/common/charge_state_v1.c b/common/charge_state_v1.c index 53c1dda5d2..9f1040654f 100644 --- a/common/charge_state_v1.c +++ b/common/charge_state_v1.c @@ -250,7 +250,7 @@ static int state_common(struct charge_state_context *ctx) #if defined(CONFIG_BATTERY_PRESENT_CUSTOM) || \ defined(CONFIG_BATTERY_PRESENT_GPIO) - if (!battery_is_present()) { + if (battery_is_present() == BP_NO) { curr->error |= F_BATTERY_NOT_CONNECTED; return curr->error; } diff --git a/include/battery.h b/include/battery.h index 7072b522ef..202148559e 100644 --- a/include/battery.h +++ b/include/battery.h @@ -100,11 +100,13 @@ void battery_vendor_params(struct batt_params *batt); /** * Check for presence of battery. * - * @return non-zero if the battery is present. Note that the - * battery may not be responding on the i2c interface if it - * is deeply discharged. + * @return Whether there is a battery attached or not, or if we can't tell. */ -int battery_is_present(void); +enum battery_present { + BP_NO = 0, + BP_YES = 1, + BP_NOT_SURE, +} battery_is_present(void); /** * Get battery mode. |