Merge #3413 into fd272 from refs/changes/3413/head 3416/target/1
author彭磊 <1624952103@qq.com>
Fri, 22 May 2026 09:17:31 +0000 (09:17 +0000)
committer袁清梦 <3449750381@qq.com>
Fri, 22 May 2026 09:17:31 +0000 (09:17 +0000)
优化充电ic参数设置DPM 优化满电判断(补)

* refs/changes/3413/head: (2 commits squashed)

  - 优化充电ic参数设置DPM 优化满电判断(补)

  - 优化充电ic参数设置DPM 优化满电判断(补充)

Signed-off-by: aliyun6865282430 <1624952103@qq.com>
Merged-by: Caisking <3449750381@qq.com>
link: https://codeup.aliyun.com/64191456b9581d62e8f81ac0/FD230/change/3413
code/sdk/zhihui/services/battery/service/battery_driver.cpp

index e10a88b..30b4f61 100644 (file)
@@ -85,7 +85,7 @@ typedef enum {
 #define AVG_VOLTAGE_COUNT         7
 /** 滑动平均:仅接受该区间内的采样;历史槽中若出现非 0 且越界视为异常并清空环形缓冲 */
 #define BAT_AVG_SAMPLE_VOL_MIN    (3.25f)
-#define BAT_AVG_SAMPLE_VOL_MAX    (4.45f)
+#define BAT_AVG_SAMPLE_VOL_MAX    (4.55f)
 
 #pragma pack(1)
 typedef struct
@@ -580,7 +580,7 @@ void battery_save_vol(void)
     for(int i = 0; i < bat_vol.vol_num;i++)
     {
         printf("history_data[%d]:%.3f\n",i,bat_vol.history_data[i]);
-        if(bat_vol.history_data[i] < 3.3 || bat_vol.history_data[i] > 4.45)
+        if(bat_vol.history_data[i] < BAT_AVG_SAMPLE_VOL_MIN || bat_vol.history_data[i] > BAT_AVG_SAMPLE_VOL_MAX)
             bat_vol.history_data[i] = 0;
     }
 
@@ -613,7 +613,7 @@ void battery_restore_vol(void)
     if(bat_vol.vol_num < 0 || bat_vol.vol_num > AVG_VOLTAGE_COUNT ||
        bat_vol.right < -1 || bat_vol.right >= AVG_VOLTAGE_COUNT ||
        bat_vol.left < -1 || bat_vol.left >= AVG_VOLTAGE_COUNT ||
-       bat_vol.sum_vol < bat_vol.vol_num * 3.25 || bat_vol.sum_vol > (bat_vol.vol_num * 4.45))
+       bat_vol.sum_vol < bat_vol.vol_num * BAT_AVG_SAMPLE_VOL_MIN || bat_vol.sum_vol > (bat_vol.vol_num * BAT_AVG_SAMPLE_VOL_MAX))
     {
         memset_s(&bat_vol,sizeof(BAT_VOL_T),0,sizeof(BAT_VOL_T));
         bat_vol.right = -1;
@@ -631,7 +631,7 @@ void battery_restore_vol(void)
     // for(int i = 0; i < AVG_VOLTAGE_COUNT;i++)
     // {
     //     printf("history_data[%d]:%.3f\n",i,bat_vol.history_data[i]);
-    //     if(bat_vol.history_data[i] < 3.3 || bat_vol.history_data[i] > 4.45)
+    //     if(bat_vol.history_data[i] < BAT_AVG_SAMPLE_VOL_MIN || bat_vol.history_data[i] > BAT_AVG_SAMPLE_VOL_MAX)
     //     {
     //         bat_vol.history_data[i] = 0;
     //     }
@@ -766,6 +766,56 @@ static uint8_t battery_get_power_percentage(void)
     return percent;
 }
 
+#ifdef ENABLE_HP4570
+
+static bool hp4570_check_charge_done(float voltage, int *percent)
+{
+    static float s_hp4570_chg_done_last_v = -1.0f;
+    static uint8_t s_hp4570_chg_done_flat_cnt = 0;
+    const float k_chg_done_floor_v = HP4570_CHG_DONE_MIN_VOLTAGE_V;
+    const float k_flat_eps_v = 0.0015f; /* 噪声内视为“未升” */
+
+    if (!get_charge_stat() || voltage < k_chg_done_floor_v) {
+        s_hp4570_chg_done_last_v = -1.0f;
+        s_hp4570_chg_done_flat_cnt = 0;
+        return false;
+    }
+    if (!hp4570n_read_chrg_stat_chrg_done()) {
+        s_hp4570_chg_done_last_v = -1.0f;
+        s_hp4570_chg_done_flat_cnt = 0;
+        return false;
+    }
+
+    if (s_hp4570_chg_done_last_v < 0.0f) {
+        s_hp4570_chg_done_last_v = voltage;
+        s_hp4570_chg_done_flat_cnt = 0;
+    } else {
+        if (voltage <= s_hp4570_chg_done_last_v + k_flat_eps_v) {
+            if (s_hp4570_chg_done_flat_cnt < 250) {
+                s_hp4570_chg_done_flat_cnt++;
+            }
+        } else {
+            s_hp4570_chg_done_flat_cnt = 0;
+        }
+        s_hp4570_chg_done_last_v = voltage;
+    }
+
+    if (s_hp4570_chg_done_flat_cnt >= HP4570_CHG_DONE_FLAT_STREAK && history_percent < 100) {
+        history_percent++;
+        *percent = history_percent;
+        printf("[电池计算] HP4570 CHG_DONE+>=%.1fV 连续电压不升 cnt=%u(>=%u) -> 累加 %% hist=%d\n",
+            HP4570_CHG_DONE_MIN_VOLTAGE_V,
+            (unsigned int)s_hp4570_chg_done_flat_cnt, (unsigned int)HP4570_CHG_DONE_FLAT_STREAK, history_percent);
+        if (history_percent >= 100) {
+            s_hp4570_chg_done_flat_cnt = 0;
+            s_hp4570_chg_done_last_v = -1.0f;
+        }
+        return true;
+    }
+    return false;
+}
+#endif
+
 /**
  * 输入电池电压值 
  * 根据 battery_percent_map 计算并返回 电池电量百分比
@@ -913,46 +963,8 @@ static int battery_get_percent_by_voltage(float voltage)
     }
 
 #ifdef ENABLE_HP4570
-    /* CHG_DONE 且电压≥HP4570_CHG_DONE_MIN_VOLTAGE_V:连续 HP4570_CHG_DONE_FLAT_STREAK 次采样电压不升,再每次仅 +1% 直至 100 */
-    {
-        static float s_hp4570_chg_done_last_v = -1.0f;
-        static uint8_t s_hp4570_chg_done_flat_cnt = 0;
-        const float k_chg_done_floor_v = HP4570_CHG_DONE_MIN_VOLTAGE_V;
-        const float k_flat_eps_v = 0.0015f; /* 噪声内视为“未升” */
-
-        if (!get_charge_stat() || voltage < k_chg_done_floor_v) {
-            s_hp4570_chg_done_last_v = -1.0f;
-            s_hp4570_chg_done_flat_cnt = 0;
-        } else if (hp4570n_read_chrg_stat_chrg_done()) {
-            if (s_hp4570_chg_done_last_v < 0.0f) {
-                s_hp4570_chg_done_last_v = voltage;
-                s_hp4570_chg_done_flat_cnt = 0;
-            } else {
-                if (voltage <= s_hp4570_chg_done_last_v + k_flat_eps_v) {
-                    if (s_hp4570_chg_done_flat_cnt < 250) {
-                        s_hp4570_chg_done_flat_cnt++;
-                    }
-                } else {
-                    s_hp4570_chg_done_flat_cnt = 0;
-                }
-                s_hp4570_chg_done_last_v = voltage;
-            }
-            if (s_hp4570_chg_done_flat_cnt >= HP4570_CHG_DONE_FLAT_STREAK && history_percent < 100) {
-                history_percent++;
-                percent = history_percent;
-                printf("[电池计算] HP4570 CHG_DONE+>=%.1fV 连续电压不升 cnt=%u(>=%u) -> 累加 %% hist=%d\n",
-                    HP4570_CHG_DONE_MIN_VOLTAGE_V,
-                    (unsigned int)s_hp4570_chg_done_flat_cnt, (unsigned int)HP4570_CHG_DONE_FLAT_STREAK, history_percent);
-                if (history_percent >= 100) {
-                    s_hp4570_chg_done_flat_cnt = 0;
-                    s_hp4570_chg_done_last_v = -1.0f;
-                }
-                return percent;
-            }
-        } else {
-            s_hp4570_chg_done_last_v = -1.0f;
-            s_hp4570_chg_done_flat_cnt = 0;
-        }
+    if (hp4570_check_charge_done(voltage, &percent)) {
+        return percent;
     }
 #endif