fix: 修复放电功能相关的若干问题

1. 修改apps/chrg/applications/utils/chrg_sink.c:
   - 调整寄存器数组长度,新增继电器控制相关的初始化配置
   - 在放电初始化前先关闭所有功率板继电器
2. 修改apps/chrg/applications/thread/chrg_north.c:
   - 修复变量类型不匹配问题,将uint8_t改为rt_uint8_t
   - 新增参数合法性校验逻辑,避免空指针和越界访问
   - 新增电压状态标记,仅在电压有效后才将低压视为故障
   - 修复无符号整数溢出问题,避免电压差值计算异常
   - 优化电流异常时的状态机切换逻辑
   - 非工作状态下重置所有状态标记
This commit is contained in:
yhf
2026-07-18 23:01:25 +08:00
parent 1b72b7bb78
commit ee76d8782d
3 changed files with 167 additions and 11 deletions
+32 -9
View File
@@ -207,11 +207,17 @@ void chrg_source_setting(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
void chrg_sink_parse(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
struct chrg_switch_t *pSW, struct chrg_north_t *pNOR)
{
rt_uint16_t volt_mv = ((frame_data[4] << 8) | frame_data[5])*10;
static uint8_t volt_low[TOTAL_SOU_CHS] = {0};
static uint8_t no_curr[TOTAL_SOU_CHS] = {0};
pSW->VZ = ((frame_data[14] << 8) | frame_data[15]);
pSW->VF = ((frame_data[16] << 8) | frame_data[17]);
rt_uint16_t volt_mv = ((frame_data[4] << 8) | frame_data[5]) * 10;
static rt_uint8_t volt_low[TOTAL_SOU_CHS] = {0};
static rt_uint8_t no_curr[TOTAL_SOU_CHS] = {0};
static rt_uint8_t volt_set_ok[TOTAL_SOU_CHS] = {0};
if ((idx >= TOTAL_SOU_CHS) || (pSW == RT_NULL)) {
return;
}
pSW->VZ = ((frame_data[14] << 8) | frame_data[15]);
pSW->VF = ((frame_data[16] << 8) | frame_data[17]);
#if DEBUG_NORTH
LOG_D("Sink[%d] real volt: %d mV", idx, volt_mv);
@@ -220,25 +226,42 @@ void chrg_sink_parse(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
if(pSW->sink.On_work == 1 && pSW->sink.test.test_mode == 0) {
if (volt_mv <= WORK_MIN_VOLT) { // 电压过低,禁止进入工作状态机
// volt_low[idx]++;
/* Treat low voltage as a fault only after voltage was once valid. */
if (volt_set_ok[idx] == 1) {
if (++volt_low[idx] >= 5) {
volt_set_ok[idx] = 0;
no_curr[idx] = 0;
pSW->sink.work_mode = SINK_WORK_LOW;
}
}
// if(volt_low[idx]>=5) pSW->sink.work_on_step = SINK_WORK_LOW; // 复位工作状态机
}
else{ // 电压稳定时执行工作状态机(继电器/工作模式切换)
volt_low[idx] = 0;
volt_set_ok[idx] = 1;
pSW->change_flag = 0;
if((pSW->sink.pro_loadv - volt_mv) <= 2000){
/* Avoid unsigned underflow when actual voltage is above target. */
if ((rt_uint32_t)volt_mv + 2000 >= pSW->sink.pro_loadv) {
pSW->sink.work_mode = SINK_WORK_RUNING;
if(pSW->Now_current<=200){
if(no_curr[idx]++>=5){
/* Voltage is normal but current is still zero: retry from WAIT. */
if (pSW->Now_current <= 200) {
if (++no_curr[idx] >= 5) {
pSW->sink.work_mode = SINK_WORK_WAIT;
no_curr[idx] = 0;
}
}
else no_curr[idx] = 0;
else {
no_curr[idx] = 0;
}
} else {
no_curr[idx] = 0;
}
}
}
else{
volt_low[idx] = 0;
no_curr[idx] = 0;
volt_set_ok[idx] = 0;
// pSW->sink.work_on_step = SINK_WORK_STOP; // 复位工作状态机
}
chrg_sink_work(idx,pSW);