diff options
| author | Joel Challis | 2025-08-17 02:14:48 +0200 |
|---|---|---|
| committer | GitHub | 2025-08-17 02:14:48 +0200 |
| commit | cc696a2ae838a9639335ca8eb3cb3b794c06bc33 (patch) | |
| tree | 901b54bda536acb5503c6cf924b0f30bca1a174e /drivers/battery/battery_adc.c | |
| parent | f29d8117bf877a4df1f88f40e0131f4465748540 (diff) | |
Refactor battery driver (#25550)
Diffstat (limited to 'drivers/battery/battery_adc.c')
| -rw-r--r-- | drivers/battery/battery_adc.c | 25 |
1 files changed, 13 insertions, 12 deletions
diff --git a/drivers/battery/battery_adc.c b/drivers/battery/battery_adc.c index cf0e69cb48..145265b5db 100644 --- a/drivers/battery/battery_adc.c +++ b/drivers/battery/battery_adc.c @@ -1,23 +1,24 @@ // Copyright 2025 QMK // SPDX-License-Identifier: GPL-2.0-or-later +#include "battery_driver.h" #include "analog.h" #include "gpio.h" -#ifndef BATTERY_PIN -# error("BATTERY_PIN not configured!") +#ifndef BATTERY_ADC_PIN +# error("BATTERY_ADC_PIN not configured!") #endif -#ifndef BATTERY_REF_VOLTAGE_MV -# define BATTERY_REF_VOLTAGE_MV 3300 +#ifndef BATTERY_ADC_REF_VOLTAGE_MV +# define BATTERY_ADC_REF_VOLTAGE_MV 3300 #endif -#ifndef BATTERY_VOLTAGE_DIVIDER_R1 +#ifndef BATTERY_ADC_VOLTAGE_DIVIDER_R1 # define BATTERY_VOLTAGE_DIVIDER_R1 100 #endif -#ifndef BATTERY_VOLTAGE_DIVIDER_R2 -# define BATTERY_VOLTAGE_DIVIDER_R2 100 +#ifndef BATTERY_ADC_VOLTAGE_DIVIDER_R2 +# define BATTERY_ADC_VOLTAGE_DIVIDER_R2 100 #endif // TODO: infer from adc config? @@ -26,16 +27,16 @@ #endif void battery_driver_init(void) { - gpio_set_pin_input(BATTERY_PIN); + gpio_set_pin_input(BATTERY_ADC_PIN); } uint16_t battery_driver_get_mv(void) { - uint32_t raw = analogReadPin(BATTERY_PIN); + uint32_t raw = analogReadPin(BATTERY_ADC_PIN); - uint32_t bat_mv = raw * BATTERY_REF_VOLTAGE_MV / (1 << BATTERY_ADC_RESOLUTION); + uint32_t bat_mv = raw * BATTERY_ADC_REF_VOLTAGE_MV / (1 << BATTERY_ADC_RESOLUTION); -#if BATTERY_VOLTAGE_DIVIDER_R1 > 0 && BATTERY_VOLTAGE_DIVIDER_R2 > 0 - bat_mv = bat_mv * (BATTERY_VOLTAGE_DIVIDER_R1 + BATTERY_VOLTAGE_DIVIDER_R2) / BATTERY_VOLTAGE_DIVIDER_R2; +#if BATTERY_VOLTAGE_DIVIDER_R1 > 0 && BATTERY_ADC_VOLTAGE_DIVIDER_R2 > 0 + bat_mv = bat_mv * (BATTERY_VOLTAGE_DIVIDER_R1 + BATTERY_ADC_VOLTAGE_DIVIDER_R2) / BATTERY_ADC_VOLTAGE_DIVIDER_R2; #endif return bat_mv; |