From f47b6e84e5ca3d5a7a5351e950b865e2ca6d7c32 Mon Sep 17 00:00:00 2001 From: Bill Richardson Date: Wed, 26 Mar 2014 10:31:04 -0700 Subject: Make battery_is_present() tristate: YES, NO, NOT_SURE In most cases we can't actually know whether a battery is present until we've been able to talk to it. This adds that NOT_SURE case. BUG=none BRANCH=ToT TEST=none Nothing uses this case yet, and the only time that battery_is_present() is called is when we have hardware to detect the battery (which always returns YES or NO). This is just preparation for charge_state_v2, which will need the NOT_SURE case for trickle charging. Change-Id: Ic5793de080529d50c98860450a021a1abae168db Signed-off-by: Bill Richardson Reviewed-on: https://chromium-review.googlesource.com/191782 Reviewed-by: Randall Spangler --- board/samus/board.c | 10 ++++++++-- common/battery.c | 5 +++-- common/charge_state_v1.c | 2 +- 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. -- cgit v1.2.1