printf("hp4570n_set_low_chrg_ipre_mA(30) ret:%d\n",ret);\r
\r
ret = hp4570n_set_chrg_ichg_mA(260);\r
- printf("hp4570n_set_chrg_ichg_mA(400) ret:%d\n",ret);\r
+ printf("hp4570n_set_chrg_ichg_mA(260) ret:%d\n",ret);\r
\r
ret = hp4570n_set_chrg_iterm_mA(30);\r
printf("hp4570n_set_chrg_iterm_mA(30) ret:%d\n",ret);\r
if(hp4570n_identify(HP4570N) == SUCCESS)\r
id = 0x2;\r
return id;\r
+}\r
+\r
+void hp4570n_reset(void)\r
+{\r
+ printf("hp4570n reset\n");\r
+ hp4570n_reg_rst();\r
+ // Add initialization code here, such as configuring registers\r
+ hp4570n_set_ceb_en(true);\r
+\r
+ // int ret = hp4570n_set_chrg_vbat_mV(4700.0);\r
+ int ret = hp4570n_set_chrg_vbat_mV(4420.0);\r
+ printf("hp4570n_set_chrg_vbat_mV ret:%d\n",ret);\r
+\r
+ ret = hp4570n_set_high_chrg_ipre_mA(30);\r
+ printf("hp4570n_set_high_chrg_ipre_mA(30) ret:%d\n",ret);\r
+\r
+ ret = hp4570n_set_low_chrg_ipre_mA(30);\r
+ printf("hp4570n_set_low_chrg_ipre_mA(30) ret:%d\n",ret);\r
+\r
+ ret = hp4570n_set_chrg_ichg_mA(260);\r
+ printf("hp4570n_set_chrg_ichg_mA(400) ret:%d\n",ret);\r
+\r
+ ret = hp4570n_set_chrg_iterm_mA(30);\r
+ printf("hp4570n_set_chrg_iterm_mA(30) ret:%d\n",ret);\r
+\r
+ ret = hp4570n_set_iindpm_mA(1000);\r
+ printf("hp4570n_set_iindpm_mA(1000) ret:%d\n",ret);\r
+\r
+ ret = hp4570n_set_high_chrg_idead_mA(29);\r
+ printf("hp4570n_set_high_chrg_idead_mA(29) ret:%d\n",ret);\r
+\r
+ ret = hp4570n_set_low_chrg_idead_mA(29);\r
+ printf("hp4570n_set_low_chrg_idead_mA(29) ret:%d\n",ret);\r
+\r
+ hp4570n_set_rst_vsys_wvbus_en(false);\r
}
\ No newline at end of file
void hp4570n_set_ipre_4x_en(bool en);//Pre-charge current X4 enable control\r
void hp4570n_set_ideadbat_4x_en(bool en);//dead battary charge current X4 enable control\r
uint8_t hp4570n_get_id(void);\r
+\r
+void hp4570n_reset(void);\r
+\r
#endif // HP4570N_CHARGER_H
\ No newline at end of file
printf("[%s] SET fail\n", __func__);
return ret;
}
- printf("[%s] ret=0x%x\n", __func__,ret);
+ // printf("[%s] ret=0x%x\n", __func__,ret);
return ret;
}
+void sgm41562_wdt_rst(void)
+{
+ (void)sgm41562_ctrl_setting(REG_BIT_WD_RST, true);
+}
static int sgm41562_get_state(
struct sgm41562_state *state)
\r
void sgm41562_read_all_reg(void);\r
\r
+void sgm41562_wdt_rst(void);\r
+\r
#endif /* _SGM41562_CHARGER_H */\r
#include "custom_utils.h"
#define AVG_VOLTAGE_COUNT 7
+/** 滑动平均:仅接受该区间内的采样;历史槽中若出现非 0 且越界视为异常并清空环形缓冲 */
+#define BAT_AVG_SAMPLE_VOL_MIN (3.25f)
+#define BAT_AVG_SAMPLE_VOL_MAX (4.45f)
#pragma pack(1)
typedef struct
return ERRCODE_SUCC;
}
+/** 非正视为空槽(轮转时写 0);正数且在区间外则为异常历史 */
+static bool bat_vol_history_has_out_of_band(void)
+{
+ for (int i = 0; i < AVG_VOLTAGE_COUNT; i++) {
+ float v = bat_vol.history_data[i];
+ if (v <= 0.0f) {
+ continue;
+ }
+ if (v < BAT_AVG_SAMPLE_VOL_MIN || v > BAT_AVG_SAMPLE_VOL_MAX) {
+ return true;
+ }
+ }
+ return false;
+}
+
static void get_avg_voltage(float *vol)
{
uint32_t current_time = 0;
sample_time = current_time - history_time;
history_time = current_time;
- if(sample_time >= 10 && *vol >= 3.25 && *vol <= 4.45)
+ if(sample_time >= 10 && *vol >= BAT_AVG_SAMPLE_VOL_MIN && *vol <= BAT_AVG_SAMPLE_VOL_MAX)
{
sample_time = 0;
if(bat_vol.vol_num == AVG_VOLTAGE_COUNT)
if(bat_vol.vol_num > 0)
{
+ if (bat_vol_history_has_out_of_band()) {
+ PRINT("[battery] history out of band [%.2f,%.2f], reset avg ring\r\n",
+ (double)BAT_AVG_SAMPLE_VOL_MIN, (double)BAT_AVG_SAMPLE_VOL_MAX);
+ memset_s(&bat_vol, sizeof(BAT_VOL_T), 0, sizeof(BAT_VOL_T));
+ bat_vol.right = -1;
+ return;
+ }
+
if(bat_vol.vol_num > 2)
{
int valid_idx = 0;
for (int i = 0; i < AVG_VOLTAGE_COUNT; i++)
{
- if (bat_vol.history_data[i] >= 3.25 && 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)
{
valid_data[valid_idx++] = bat_vol.history_data[i];
sum_vol+=bat_vol.history_data[i];
bat_vol.avg_vol = sum_vol / valid_idx;
}
- if(sum_vol != bat_vol.sum_vol)
- {
- printf("sum_vol:%.3f != bat_vol.sum_vol:%.3f\n",sum_vol,bat_vol.sum_vol);
- // bat_vol.sum_vol = sum_vol;
- memset_s(&bat_vol,sizeof(BAT_VOL_T),0,sizeof(BAT_VOL_T));
- bat_vol.right = -1;
- }
-
}
else {
bat_vol.avg_vol = bat_vol.sum_vol / bat_vol.vol_num;
#define BAT_FILE_PATH "user/userdata/bat_data.bin"
-#define CHARGE_1C (260.0f)
+#define CHARGE_1C_MA (260u)
+#define CHARGE_02C_MA ((CHARGE_1C_MA * 2u) / 10u) /* 52 */
+#define CHARGE_05C_MA ((CHARGE_1C_MA) / 2u) /* 130 */
bool batteryservice_get_voltage_by_channel(float *voltage, uint8_t channel);
battery_info_t *get_current_battery_info(void);
static uint8_t g_check_count = 0;
+#define WDT_KICK_INTERVAL_SEC 30u
+static uint8_t g_charge_wdt_sec_counter = 0;
+
+
static void battery_check_timer_callback(void* data);
errcode_t charger_plug_event_notify(uint8_t plugged);
errcode_t charger_plug_event_ui_sendmsg_toUI(uint8_t plugged);
// printf("bat_temp:%f\n",bat_temp);
// bat_temp = -10;
- float charge_mA = 0;
+ uint16_t charge_mA = 0;
if(bat_temp >= BATTERY_DISABLE_CHARGE_HI_TEMP_VALUE || bat_temp <= BATTERY_DISABLE_CHARGE_LOW_TEMP_VALUE)
{
if(bat_temp >= 0 && bat_temp < 15)
{
#ifdef ENABLE_HP4570
- charge_mA = 0.2 * CHARGE_1C;
- // hp4570_set_chrg_ichg_mA(0.2 * CHARGE_1C);
+ charge_mA = CHARGE_02C_MA;
#endif
}
else if((bat_temp >= 15 && bat_temp < 25) || (bat_temp >= 45 && bat_temp < 60))
{
#ifdef ENABLE_HP4570
- // hp4570_set_chrg_ichg_mA(0.5 * CHARGE_1C);
- charge_mA = 0.5 * CHARGE_1C;
+ charge_mA = CHARGE_05C_MA;
#endif
}
else if(bat_temp >= 25 && bat_temp < 45)
{
#ifdef ENABLE_HP4570
- // hp4570_set_chrg_ichg_mA(CHARGE_1C);
- charge_mA = CHARGE_1C;
+ charge_mA = CHARGE_1C_MA;
#endif
}
if(charge_mA != last_charge_mA)
{
- last_charge_mA = (uint16_t)charge_mA;
+ last_charge_mA = charge_mA;
// printf("last_charge_mA : %d\n",last_charge_mA);
#ifdef ENABLE_HP4570
- hp4570n_set_chrg_ichg_mA(charge_mA);
+ hp4570n_set_chrg_ichg_mA(last_charge_mA);
#endif
}
if (mail.comm.event == BATTERY_CMD_CHECK_VOLTAGE) {
+
+ // if (get_charge_stat() == 1)
+ {
+ g_charge_wdt_sec_counter++;
+ if (g_charge_wdt_sec_counter >= WDT_KICK_INTERVAL_SEC)
+ {
+ g_charge_wdt_sec_counter = 0;
+#if defined(ENABLE_HP4570)
+ hp4570n_wdt_rst();
+ if (hp4570n_read_status_wdt_fault())
+ {
+ PRINT("[battery] HP4570 WDT fault, hp4570n_reset()\r\n");
+ hp4570n_reset();
+ }
+#elif defined(ENABLE_SGM41562)
+ sgm41562_wdt_rst();
+#endif
+ }
+ }
+ // else {
+ // g_charge_wdt_sec_counter = 0;
+ // }
+
+
g_check_count++;
// PRINT("BATTERY_CMD_CHECK_VOLTAGE g_check_count %d\r\n",g_check_count);
if(last_battery_charge_enable_status == true){
charger_plug_event_ui_sendmsg_toUI(1);
// 记录一次充电线插入时刻的电池数据,用于后续统计充电时间
battery_save_data();
+#ifdef ENABLE_HP4570
+ hp4570n_reset();
+#endif
}else if (mail.comm.event == BATTERY_CMD_CHARGE_UNPLUG) {
// PRINT("[battery][service] charger_plug_event_ui_sendmsg_toUI unplugged \n");
oncharging_status = 0;