#include #include "ql_type.h" #include "ql_data_call.h" #include "ql_rtos.h" #include "ql_application.h" #include #include "sockets.h" #include "ql_uart.h" #include "ql_func.h" #include "netdb.h" #include "ql_sim.h" #include "ql_dev.h" #include "ql_wtd.h" #include "ql_fs.h" #include "ql_fota.h" #include "ql_sim.h" #include "ql_ping.h" #include "ql_nw.h" #include "ql_atcmd.h" #include "app_inc.h" #include "ql_uart.h" #include "ql_application.h" #include "ql_log.h" #include "ql_adc.h" #include "ql_power.h" #include "hal_api.h" // 电量阈值定义 #define BAT_VOLTAGE_100 4200 #define BAT_VOLTAGE_90 4125 #define BAT_VOLTAGE_80 4050 #define BAT_VOLTAGE_70 3950 #define BAT_VOLTAGE_60 3850 #define BAT_VOLTAGE_50 3775 #define BAT_VOLTAGE_40 3700 #define BAT_VOLTAGE_30 3650 #define BAT_VOLTAGE_20 3600 #define BAT_VOLTAGE_10 3550 #define BAT_VOLTAGE_MIN 3500 #define BAT_LOW_WARN_MV 3550 #define BAT_LOW_PROBE_EXIT_MV 3600 #define BAT_SHUTDOWN_MV 3500 #define BAT_VALID_MIN_MV 2500 #define BAT_VALID_MAX_MV 5000 #define BAT_SHUTDOWN_CONFIRM_COUNT 3 /* * 原理图中电池负极经 R28(0.33R) 接地,电池侧通过 R35 接 ADC0。 * 充电电流流过 R28 时 ADC0 出现正向压降;放电时 ADC0 接近 0mV。 * 8mV 约对应 24mA,3mV 作为退出阈值,滞回可避免零点噪声抖动。 */ #define BAT_CHARGE_ADC_ENTER_MV 8 #define BAT_CHARGE_ADC_EXIT_MV 3 #define BAT_CHARGE_CONFIRM_COUNT 3 #define BAT_CHARGE_ADC_FAIL_COUNT 3 unsigned short g_vbat_vol = 0; unsigned short vbat_sample[6] = {0}; // 修改:样本数改为6 unsigned char battery_first_update_flag = 0; static unsigned short g_vbat_vol_last = 0; static bat_level_t g_bat_level_last = BAT_LEVEL_100; static unsigned char g_bat_level_valid = 0; static unsigned char g_low_voltage_count = 0; static unsigned char g_low_probe_active = 0; static unsigned char g_low_power_warned = 0; static int g_vbus_last_state = -1; static int g_charge_adc_state = 0; static unsigned char g_charge_adc_enter_count = 0; static unsigned char g_charge_adc_exit_count = 0; static unsigned char g_charge_adc_fail_count = 0; bat_level_t calc_battery_level(uint16_t voltage_mv) { if (voltage_mv >= BAT_VOLTAGE_90) return BAT_LEVEL_100; else if (voltage_mv >= BAT_VOLTAGE_80) return BAT_LEVEL_90; else if (voltage_mv >= BAT_VOLTAGE_70) return BAT_LEVEL_80; else if (voltage_mv >= BAT_VOLTAGE_60) return BAT_LEVEL_70; else if (voltage_mv >= BAT_VOLTAGE_50) return BAT_LEVEL_60; else if (voltage_mv >= BAT_VOLTAGE_40) return BAT_LEVEL_50; else if (voltage_mv >= BAT_VOLTAGE_30) return BAT_LEVEL_40; else if (voltage_mv >= BAT_VOLTAGE_20) return BAT_LEVEL_30; else if (voltage_mv >= BAT_VOLTAGE_10) return BAT_LEVEL_20; else if (voltage_mv >= BAT_VOLTAGE_MIN) return BAT_LEVEL_10; else return BAT_LEVEL_0; } bat_level_t battery_get_level(void) { return g_bat_level_valid ? g_bat_level_last : calc_battery_level(g_vbat_vol); } static int update_external_power_state(void) { int vbus_state = ql_get_vbus_state() ? 1 : 0; int adc_valid = 0; unsigned short adc0_mv = 0; if (vbus_state != g_vbus_last_state) { printf("[BAT][VBUS] state changed: %d -> %d\r\n", g_vbus_last_state, vbus_state); g_vbus_last_state = vbus_state; } if (ql_adc_read(ADC_CHANNEL_ADC0, &adc0_mv) == ADC_READ_SUCCESS) { adc_valid = 1; g_charge_adc_fail_count = 0; if (!g_charge_adc_state) { g_charge_adc_exit_count = 0; if (adc0_mv >= BAT_CHARGE_ADC_ENTER_MV) { if (g_charge_adc_enter_count < BAT_CHARGE_CONFIRM_COUNT) { g_charge_adc_enter_count++; } if (g_charge_adc_enter_count >= BAT_CHARGE_CONFIRM_COUNT) { g_charge_adc_state = 1; g_charge_adc_enter_count = 0; printf("[BAT][CHARGE] ADC0 charging detected: %dmV\r\n", adc0_mv); } } else { g_charge_adc_enter_count = 0; } } else { g_charge_adc_enter_count = 0; if (adc0_mv <= BAT_CHARGE_ADC_EXIT_MV) { if (g_charge_adc_exit_count < BAT_CHARGE_CONFIRM_COUNT) { g_charge_adc_exit_count++; } if (g_charge_adc_exit_count >= BAT_CHARGE_CONFIRM_COUNT) { g_charge_adc_state = 0; g_charge_adc_exit_count = 0; printf("[BAT][CHARGE] ADC0 charging removed: %dmV\r\n", adc0_mv); } } else { g_charge_adc_exit_count = 0; } } } else { g_charge_adc_enter_count = 0; g_charge_adc_exit_count = 0; if (g_charge_adc_fail_count < BAT_CHARGE_ADC_FAIL_COUNT) { g_charge_adc_fail_count++; } if (g_charge_adc_fail_count >= BAT_CHARGE_ADC_FAIL_COUNT) { g_charge_adc_state = 0; } } { int external_power = vbus_state || g_charge_adc_state; printf("[BAT][POWER] vbus=%d, adc0=%d, adc_valid=%d, " "adc_charge=%d, external=%d\r\n", vbus_state, adc0_mv, adc_valid, g_charge_adc_state, external_power); if (external_power) { g_low_voltage_count = 0; g_low_probe_active = 0; g_low_power_warned = 0; } return external_power; } } static bat_level_t limit_level_jump(bat_level_t raw_level, int external_power) { bat_level_t result = g_bat_level_last; if (!g_bat_level_valid) { g_bat_level_valid = 1; g_bat_level_last = raw_level; printf("[BAT] first level initialized: %d\r\n", raw_level); return raw_level; } if (external_power) { if (raw_level > g_bat_level_last) { result = (raw_level >= g_bat_level_last + BAT_LEVEL_10) ? (bat_level_t)(g_bat_level_last + BAT_LEVEL_10) : raw_level; } } else { if (raw_level < g_bat_level_last) { result = (raw_level <= g_bat_level_last - BAT_LEVEL_10) ? (bat_level_t)(g_bat_level_last - BAT_LEVEL_10) : raw_level; } } printf("[BAT] level update: last=%d raw=%d result=%d external=%d\r\n", g_bat_level_last, raw_level, result, external_power); g_bat_level_last = result; return result; } static void battery_check_critical_voltage(unsigned short voltage_mv, int external_power) { if (external_power) { g_low_voltage_count = 0; g_low_probe_active = 0; return; } if (voltage_mv < BAT_LOW_WARN_MV) { g_low_probe_active = 1; } else if (voltage_mv >= BAT_LOW_PROBE_EXIT_MV) { g_low_probe_active = 0; } if (voltage_mv < BAT_SHUTDOWN_MV) { if (g_low_voltage_count < BAT_SHUTDOWN_CONFIRM_COUNT) { g_low_voltage_count++; } printf("[BAT] critical voltage:%d, confirm:%d/%d\r\n", voltage_mv, g_low_voltage_count, BAT_SHUTDOWN_CONFIRM_COUNT); if (g_low_voltage_count >= BAT_SHUTDOWN_CONFIRM_COUNT) { printf("[BAT] critical voltage confirmed, poweroff now\r\n"); app_play_tips_file(15); ql_rtos_task_sleep_s(5); ql_power_down(1); } } else { g_low_voltage_count = 0; } } /* 修改后的 battery_check:分阶段采样,采样窗口为6次 */ void battery_check(void) { static unsigned int call_count = 0; // 总调用计数 static unsigned char sample_idx = 0; // 当前已采集样本数 (0~5) static unsigned short sample_buf[6] = {0}; // 本地缓冲(也可用全局 vbat_sample,此处直接用局部) int external_power = update_external_power_state(); call_count++; /* 判断本次是否执行ADC采样 */ unsigned char do_sample = 0; if (call_count <= 24) { // 开机2分钟内 (24次*5s=120s) do_sample = 1; } else if (g_low_probe_active) { do_sample = 1; // 低电风险时每5秒快速确认 } else { // 2分钟后,每36次调用采样一次 (36*5s=180s=3分钟) if ((call_count - 25) % 36 == 0) { do_sample = 1; } } if (!do_sample) { return; // 本次不采样,直接返回 } /* ----- 执行一次ADC采样 ----- */ u16 vbat_vol = 0; ql_adc_onff(1); ql_rtos_task_sleep_ms(5); ql_adc_onff(0); vbat_vol = ql_get_battery_vol(); printf("[BAT][SAMPLE] voltage=%d, external=%d, low_probe=%d\r\n", vbat_vol, external_power, g_low_probe_active); if (vbat_vol < BAT_VALID_MIN_MV || vbat_vol > BAT_VALID_MAX_MV) { printf("[BAT] invalid voltage sample ignored:%d\r\n", vbat_vol); g_low_voltage_count = 0; return; } battery_check_critical_voltage(vbat_vol, external_power); sample_buf[sample_idx] = vbat_vol; sample_idx++; /* 当收集满6个样本时进行平均处理 */ if (sample_idx == 6) { unsigned short max = sample_buf[0], min = sample_buf[0]; unsigned short j = 0, vol_all = 0; for (j = 1; j < 6; j++) { if (max < sample_buf[j]) max = sample_buf[j]; if (min > sample_buf[j]) min = sample_buf[j]; } for (j = 0; j < 6; j++) { vol_all += sample_buf[j]; } vol_all = vol_all - min - max; vbat_vol = vol_all / 4; // 6个样本去掉最大最小,剩余4个平均 /* 非接电状态下,电压不允许上升 */ if (!external_power) { if (g_vbat_vol_last > 0 && vbat_vol > g_vbat_vol_last) { printf("[BAT] voltage rise blocked: %d -> %d (external=0)\r\n", g_vbat_vol_last, vbat_vol); vbat_vol = g_vbat_vol_last; } } g_vbat_vol = vbat_vol; g_vbat_vol_last = vbat_vol; printf("vbat_vol:%d, external:%d\r\n", g_vbat_vol, external_power); /* 计算电量等级并限制跳变 */ bat_level_t raw_level = calc_battery_level(g_vbat_vol); bat_level_t level = limit_level_jump(raw_level, external_power); printf("[BAT] raw_level:%d, limited_level:%d, external:%d\r\n", raw_level, level, external_power); if (battery_first_update_flag == 0) { battery_first_update_flag = 1; } /* 极低电关机由原始电压独立处理,普通低电提示只播放一次 */ if (!external_power && level <= BAT_LEVEL_10) { if (!g_low_power_warned) { g_low_power_warned = 1; app_play_tips_file(14); } } else if (external_power || level >= BAT_LEVEL_20) { g_low_power_warned = 0; } sample_idx = 0; // 重置样本索引,准备下一轮采集 } /* 若未满6个,则本次只存储,不进行平均,直接返回 */ }