refactor(chrg_north): 重构北向通信处理逻辑,增加配置版本校验机制

1.  apps/chrg/applications/thread/chrg_comm.c: 新增通道配置版本递增逻辑,避免旧回码干扰新配置
2.  apps/chrg/applications/thread/chrg_north.h: 更新config_version注释,补充用途说明
3.  apps/chrg/applications/thread/chrg_north.c: 重构帧头解析函数,新增缓冲区操作辅助函数和通道切换复位逻辑
4.  apps/chrg/applications/thread/chrg_roll_nor.c:
    - 新增超时、重发等宏定义
    - 重构协议设置函数,增加返回值处理
    - 新增回码匹配校验、邮箱清理、等待响应等辅助函数
    - 重构自动请求逻辑,增加重试和配置版本校验
    - 简化线程入口处理逻辑,统一回码释放流程
5.  apps/chrg/codex_build.log: 记录本次编译的代码尺寸变化
This commit is contained in:
wrh
2026-08-05 09:15:39 +08:00
parent f21492fa50
commit d9a8c359c0
182 changed files with 1265 additions and 82144 deletions
+76 -31
View File
@@ -2336,7 +2336,8 @@ static void chrg_comm_get_param_diff(eIDX_ID role,
static eMBException chrg_comm_apply_sink_effect(rt_uint8_t ch,
struct chrg_switch_t *pReal, const struct chrg_switch_t *pShadow)
{
rt_bool_t pro_change;
rt_bool_t protocol_or_cc_change;
rt_bool_t protocol_param_change;
rt_uint8_t curr_test;
rt_uint8_t curr_test_old;
rt_uint8_t old_cv_mode = pReal->CV_mode;
@@ -2346,10 +2347,11 @@ static eMBException chrg_comm_apply_sink_effect(rt_uint8_t ch,
chrg_param_diff_t diff_info;
chrg_comm_get_param_diff(ID_SINK, pReal, pShadow, &diff_info);
/* Sink以下属性变化时需要从CC开始重新执行北向协议配置链路。 */
pro_change = diff_info.protocol_changed || diff_info.group_changed ||
diff_info.pro_voltage_changed || diff_info.pro_current_changed ||
diff_info.cc_changed || diff_info.cv_changed;
/* 协议类型或CC线变化:需卸载后从CC开始重新配置北向协议链路 */
protocol_or_cc_change = diff_info.protocol_changed || diff_info.cc_changed;
/* 同协议下组号/协议电压/协议电流变化:卸载后重发协议,不重设CC */
protocol_param_change = diff_info.group_changed ||
diff_info.pro_voltage_changed || diff_info.pro_current_changed;
rt_memcpy(pReal, pShadow, sizeof(struct chrg_switch_t));
if (sink_work == 0x01) {
@@ -2374,9 +2376,12 @@ static eMBException chrg_comm_apply_sink_effect(rt_uint8_t ch,
pReal->sink.On_work = 1;
pReal->sub = IDX_SET_SINK_CCLINE;
pReal->sink.work_mode = SINK_WORK_INIT;
} else if (pro_change) {
} else if (protocol_or_cc_change) {
pReal->sub = IDX_SET_SINK_CCLINE;
pReal->sink.work_mode = SINK_WORK_WAIT;
pReal->sink.work_mode = SINK_WORK_PROTOCOL_UNLOAD;
} else if (protocol_param_change) {
pReal->sub = IDX_SET_SINK_PD;
pReal->sink.work_mode = SINK_WORK_PROTOCOL_UNLOAD;
} else if (chrg_sink_diff_send_reg((eIDX_SOU_CH)ch,
&diff_info, pReal) != 0) {
pReal->CV_mode = old_cv_mode;
@@ -2406,7 +2411,13 @@ static eMBException chrg_comm_apply_source_effect(rt_uint8_t ch,
struct chrg_switch_t *pReal, const struct chrg_switch_t *pShadow)
{
chrg_param_diff_t diff_info;
rt_bool_t protocol_config_changed;
rt_bool_t cc_changed;
rt_bool_t north_config_changed;
rt_bool_t protocol_switch_requested;
rt_bool_t old_is_pd;
rt_bool_t new_is_pd;
rt_bool_t need_cc_close;
struct vc_pd_t old_pd_set;
eSRC_PRO old_protocol = pReal->source.protocol;
rt_uint8_t old_cc_set = pReal->source.CC_Set;
@@ -2420,15 +2431,23 @@ static eMBException chrg_comm_apply_source_effect(rt_uint8_t ch,
rt_uint16_t source_work = pShadow->source.Now_State;
rt_uint16_t regs[2];
rt_uint16_t vals[2];
rt_uint16_t clear_reg;
rt_uint16_t clear_val;
rt_uint16_t target_curr;
rt_uint8_t count = 0;
chrg_comm_get_param_diff(ID_SOURCE, pReal, pShadow, &diff_info);
rt_memcpy(&old_pd_set, &pReal->source.pd_set, sizeof(old_pd_set));
north_config_changed = diff_info.protocol_changed ||
diff_info.cc_changed || diff_info.pd_changed;
/* 标准协议的电压电流直接交给功率板,不因pd_set数值变化重发0xD0。 */
protocol_config_changed = diff_info.protocol_changed ||
((pShadow->source.protocol != PRO_NONE) && diff_info.pd_changed);
cc_changed = diff_info.cc_changed;
north_config_changed = protocol_config_changed || cc_changed;
protocol_switch_requested = north_config_changed;
old_is_pd = (old_protocol == PRO_PD0PSS) ||
(old_protocol == PRO_PD31AVS);
new_is_pd = (pShadow->source.protocol == PRO_PD0PSS) ||
(pShadow->source.protocol == PRO_PD31AVS);
need_cc_close = (new_is_pd && north_config_changed) ||
(old_is_pd && !new_is_pd && diff_info.protocol_changed);
target_curr = pShadow->source.pd_set.mm.max_cur;
/* 影子区只拥有COM配置;北向解析产生的运行目标和回读数据继续由pReal持有。 */
@@ -2442,30 +2461,42 @@ static eMBException chrg_comm_apply_source_effect(rt_uint8_t ch,
if (source_work == 0x01) {
if (pReal->source.On_Flag == 0) {
/* 新命令要求运行时直接置位,不依赖旧工作状态。 */
pReal->sub = IDX_SET_SOURCE_CC_SET;
/*
* 停止状态启动不需要先断开CC,直接从设置协议开始;
* 收到0xD0应答并等待保护时间后,再连接目标CC。
*/
chrg_roll_nor_source_switch_reset(ch);
pReal->sub = IDX_SET_SOURCE_PD;
pReal->source.On_Flag = 1;
pReal->source.work_mode = SOURCE_WORK_INIT;
return (chrg_source_work_state(ch, pReal) == 0) ?
MB_EX_NONE : MB_EX_SLAVE_BUSY;
if (chrg_source_work_state(ch, pReal) != 0) {
goto source_effect_submit_failed;
}
return MB_EX_NONE;
}
/* 协议、CC或任一pd_set字段变化时,重新执行北向配置链路。 */
/*
* PD配置或CC发生变化,以及从PD切换到非PD时,先断开CC;
* 其它协议配置变化直接从0xD0开始。
*/
if (north_config_changed) {
pReal->sub = IDX_SET_SOURCE_CC_SET;
chrg_roll_nor_source_switch_reset(ch);
if (need_cc_close) {
pReal->sub = IDX_SET_SOURCE_CC_CLOSE;
chrg_roll_nor_source_cc_close_register(ch);
} else {
pReal->sub = IDX_SET_SOURCE_PD;
}
}
pReal->source.work_mode = SOURCE_WORK_WAIT;
if (diff_info.protocol_changed || diff_info.cc_changed) {
/* 协议或CC切换前先清零南向输出,帮助功率板退出旧协议状态,
* 后续北向轮询再按CC、协议、VC顺序完成新协议握手。 */
clear_reg = REG_VOLTAGE_OUT + 1;
clear_val = 0;
if (chrg_sou_com_batch_submit((eIDX_SOU_CH)ch,
&clear_reg, &clear_val, 1) != 0) {
goto source_effect_submit_failed;
}
pReal->source.set_volt = 0;
if (protocol_switch_requested) {
/*
* COM只进入协议切换状态;南向输出清零统一由Source状态函数执行。
* sub已在上方确定,北向轮询线程执行本状态并清零成功后,继续
* CC_CLOSE或协议设置,不需要COM直接发送任何清零命令。
*/
pReal->source.work_mode = SOURCE_WORK_PROTOCOL_SWITCH;
}
if (pReal->source.protocol != PRO_NONE) {
@@ -2474,11 +2505,11 @@ static eMBException chrg_comm_apply_source_effect(rt_uint8_t ch,
}
/* 标准协议的模式和电流可直接提交,电压只能由RUNING状态写入。 */
if (diff_info.protocol_changed || diff_info.cv_changed) {
if (!protocol_switch_requested && diff_info.cv_changed) {
regs[count] = REG_MODE;
vals[count++] = pReal->CV_mode;
}
if (diff_info.protocol_changed || diff_info.current_changed) {
if (!protocol_switch_requested && diff_info.current_changed) {
regs[count] = REG_CURRENT_OUT + 1;
vals[count++] = target_curr;
}
@@ -2491,11 +2522,18 @@ static eMBException chrg_comm_apply_source_effect(rt_uint8_t ch,
/* COM只更新标准协议配置并保持WAIT;下一帧北向解析负责比较
* pd_set目标与set_volt,再决定是否进入RUNING写电压。 */
pReal->source.set_curr = target_curr;
pReal->source.work_mode = SOURCE_WORK_WAIT;
if (!protocol_switch_requested) {
pReal->source.work_mode = SOURCE_WORK_WAIT;
}
return MB_EX_NONE;
}
pReal->source.On_Flag = 0;
if (pReal->source.On_Flag == 0) {
chrg_roll_nor_source_switch_reset(ch);
pReal->source.work_mode = SOURCE_WORK_WAIT;
return MB_EX_NONE;
}
chrg_roll_nor_source_switch_reset(ch);
pReal->source.work_mode = SOURCE_WORK_STOP;
return (chrg_source_work_state(ch, pReal) == 0) ?
MB_EX_NONE : MB_EX_SLAVE_BUSY;
@@ -2556,8 +2594,15 @@ static eMBException chrg_mb_write_sys(rt_uint8_t ch, rt_uint16_t reg_offset,
old_id = pReal->id;
new_id = pSW->id;
g_shadow_dirty[ch] = 0;
/*
* 使每次COM参数生效拥有独立版本。北向线程等待回码期间如果版本变化,
* 必须放弃旧请求,防止旧回码把新配置的sub错误推进到下一步。
*/
pNOR->config_version[ch]++;
if (old_id != new_id) {
/* 角色切换时清除旧Source协议切换的等待上下文。 */
chrg_roll_nor_source_switch_reset(ch);
rt_memcpy(pReal, pSW, sizeof(struct chrg_switch_t));
if (new_id == ID_SOURCE) {
pReal->source.work_mode = SOURCE_WORK_SWITCH;