fix(battery): detect TP4056 charging and full states on new board
This commit is contained in:
@@ -12,13 +12,18 @@ SOURCE = ROOT / "components/platform/src/platform_battery_driver.c"
|
||||
|
||||
def initializer():
|
||||
source = SOURCE.read_text()
|
||||
start = source.index("esp_err_t platform_battery_init(void)")
|
||||
body = source.index("{", start)
|
||||
end, depth = body + 1, 1
|
||||
while depth:
|
||||
depth += (source[end] == "{") - (source[end] == "}")
|
||||
end += 1
|
||||
return source[start:end]
|
||||
parts = []
|
||||
for signature in ["static esp_err_t configure_charge_inputs_locked(void)",
|
||||
"esp_err_t platform_battery_read_charge_inputs(",
|
||||
"esp_err_t platform_battery_init(void)"]:
|
||||
start = source.index(signature)
|
||||
body = source.index("{", start)
|
||||
end, depth = body + 1, 1
|
||||
while depth:
|
||||
depth += (source[end] == "{") - (source[end] == "}")
|
||||
end += 1
|
||||
parts.append(source[start:end])
|
||||
return "\n".join(parts)
|
||||
|
||||
|
||||
STUBS = r'''
|
||||
@@ -26,6 +31,7 @@ STUBS = r'''
|
||||
#include <stdbool.h>
|
||||
#include <stdint.h>
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
typedef int esp_err_t;
|
||||
typedef int adc_unit_t;
|
||||
typedef int adc_channel_t;
|
||||
@@ -58,7 +64,10 @@ typedef struct { int unit_id, ulp_mode; } adc_oneshot_unit_init_cfg_t;
|
||||
typedef struct { int atten, bitwidth; } adc_oneshot_chan_cfg_t;
|
||||
static adc_oneshot_unit_handle_t s_adc_handle;
|
||||
static adc_channel_t s_adc_channel;
|
||||
static bool s_initialized, locked;
|
||||
static bool s_initialized, s_inputs_initialized, locked;
|
||||
#define GPIO_IS_VALID_GPIO(n) ((n) >= 0 && (n) < GPIO_NUM_MAX)
|
||||
typedef struct { int charge_level, full_level; bool valid; } platform_battery_charge_inputs_t;
|
||||
static int gpio_get_level(int gpio) { return gpio == CONFIG_TQ_BATTERY_FULL_GPIO; }
|
||||
static int fail_lock, gpio_error, mapping_error, unit_error, channel_error, calibration_error;
|
||||
static int gpio_calls, unit_calls, delete_calls, calibration_calls;
|
||||
static int actual_unit, actual_channel;
|
||||
@@ -103,7 +112,7 @@ static esp_err_t adc_oneshot_del_unit(adc_oneshot_unit_handle_t handle) {
|
||||
TESTS = r'''
|
||||
static void reset(void) {
|
||||
assert(!locked);
|
||||
s_initialized = false; s_adc_handle = NULL;
|
||||
s_initialized = s_inputs_initialized = false; s_adc_handle = NULL;
|
||||
fail_lock = gpio_error = mapping_error = unit_error = channel_error = calibration_error = 0;
|
||||
gpio_calls = unit_calls = delete_calls = calibration_calls = 0;
|
||||
}
|
||||
@@ -113,7 +122,7 @@ int main(void) {
|
||||
assert(s_initialized && !locked);
|
||||
assert(actual_unit == EXPECTED_UNIT && actual_channel == EXPECTED_CHANNEL);
|
||||
assert(s_adc_channel == EXPECTED_CHANNEL && calibration_calls == 1);
|
||||
assert(charge_mask == (UINT64_C(1) << CONFIG_TQ_BATTERY_CHARGE_GPIO));
|
||||
assert(charge_mask == ((UINT64_C(1) << CONFIG_TQ_BATTERY_CHARGE_GPIO) | (UINT64_C(1) << CONFIG_TQ_BATTERY_FULL_GPIO)));
|
||||
assert(platform_battery_init() == ESP_OK && unit_calls == 1 && gpio_calls == 1);
|
||||
reset(); fail_lock = 1;
|
||||
assert(platform_battery_init() == ESP_ERR_TIMEOUT && !s_initialized && !unit_calls);
|
||||
@@ -131,8 +140,15 @@ int main(void) {
|
||||
reset(); calibration_error = ESP_ERR_NOT_SUPPORTED;
|
||||
assert(platform_battery_init() == calibration_error && !locked && !s_initialized);
|
||||
assert(delete_calls == 1 && s_adc_handle == NULL);
|
||||
platform_battery_charge_inputs_t inputs = {0};
|
||||
assert(platform_battery_read_charge_inputs(&inputs) == ESP_OK);
|
||||
assert(inputs.valid && inputs.charge_level == 0 && inputs.full_level == 1);
|
||||
assert(!s_initialized); /* 数字输入不依赖ADC校准 */
|
||||
calibration_error = ESP_OK;
|
||||
assert(platform_battery_init() == ESP_OK && s_initialized);
|
||||
assert(platform_battery_read_charge_inputs(NULL) == ESP_ERR_INVALID_ARG);
|
||||
fail_lock = 1; inputs.valid = true;
|
||||
assert(platform_battery_read_charge_inputs(&inputs) == ESP_ERR_TIMEOUT && !inputs.valid);
|
||||
puts("PASS: battery ADC unit/channel propagation, idempotence, bounded lock failure, cleanup and retry");
|
||||
}
|
||||
'''
|
||||
@@ -154,6 +170,7 @@ def main():
|
||||
"-fsanitize=address,undefined", "-fno-omit-frame-pointer",
|
||||
f"-DCONFIG_TQ_BATTERY_ADC_GPIO={gpio}",
|
||||
f"-DCONFIG_TQ_BATTERY_CHARGE_GPIO={charge_gpio}",
|
||||
"-DCONFIG_TQ_BATTERY_FULL_GPIO=8",
|
||||
f"-DEXPECTED_UNIT={unit}", f"-DEXPECTED_CHANNEL={channel}",
|
||||
str(source), "-o", str(binary),
|
||||
], check=True)
|
||||
|
||||
Reference in New Issue
Block a user