diff options
-rw-r--r-- | board/flapjack/battery.c | 24 | ||||
-rw-r--r-- | board/flapjack/board.c | 16 | ||||
-rw-r--r-- | board/flapjack/board.h | 9 |
3 files changed, 29 insertions, 20 deletions
diff --git a/board/flapjack/battery.c b/board/flapjack/battery.c index 6ba167b3fe..f7d5afc1ef 100644 --- a/board/flapjack/battery.c +++ b/board/flapjack/battery.c @@ -150,35 +150,19 @@ static const struct { }, }; -static struct { - enum battery_type type; - int expect_mv; -} const batteries[] = { +static const struct mv_to_id batteries[] = { { BATTERY_C18_ATL, 900 }, /* 100K */ { BATTERY_C19_ATL, 576 }, /* 47K */ }; BUILD_ASSERT(ARRAY_SIZE(batteries) < BATTERY_COUNT); -#define MARGIN_MV 56 /* Simply assume 1800/16/2 */ - static enum battery_type batt_type = BATTERY_UNKNOWN; static void board_get_battery_type(void) { - int mv; - int i; - - mv = adc_read_channel(ADC_BATT_ID); - if (mv == ADC_READ_ERROR) - mv = adc_read_channel(ADC_BATT_ID); - - for (i = 0; i < ARRAY_SIZE(batteries); i++) { - if (ABS(mv - batteries[i].expect_mv) < MARGIN_MV) { - batt_type = batteries[i].type; - break; - } - } - + int id = board_read_id(ADC_BATT_ID, batteries, ARRAY_SIZE(batteries)); + if (id != ADC_READ_ERROR) + batt_type = id; CPRINTS("Battery Type: %d", batt_type); } DECLARE_HOOK(HOOK_INIT, board_get_battery_type, HOOK_PRIO_FIRST); diff --git a/board/flapjack/board.c b/board/flapjack/board.c index 23f42c1cd4..862a6c12a4 100644 --- a/board/flapjack/board.c +++ b/board/flapjack/board.c @@ -54,6 +54,22 @@ uint16_t board_version; uint8_t oem; uint32_t sku; +int board_read_id(enum adc_channel ch, const struct mv_to_id *table, int size) +{ + int mv = adc_read_channel(ch); + int i; + + if (mv == ADC_READ_ERROR) + mv = adc_read_channel(ch); + + for (i = 0; i < size; i++) { + if (ABS(mv - table[i].median_mv) < ADC_MARGIN_MV) + return table[i].id; + } + + return ADC_READ_ERROR; +} + static void board_setup_panel(void) { uint8_t channel; diff --git a/board/flapjack/board.h b/board/flapjack/board.h index 1c41659577..ebe887d3cb 100644 --- a/board/flapjack/board.h +++ b/board/flapjack/board.h @@ -279,6 +279,15 @@ void emmc_cmd_interrupt(enum gpio_signal signal); void board_reset_pd_mcu(void); +#define ADC_MARGIN_MV 56 /* Simply assume 1800/16/2 */ + +struct mv_to_id { + int id; + int median_mv; +}; + +int board_read_id(enum adc_channel, const struct mv_to_id *table, int size); + #endif /* !__ASSEMBLER__ */ #endif /* __CROS_EC_BOARD_H */ |