refactor: 重构南向充电控制与参数管理逻辑
1. apps/chrg/applications/thread/chrg_south.c: 新增支持YM_CAN应答类型处理 2. apps/chrg/applications/utils/chrg_source.h: 移除冗余的max_vol、protective_curr字段 3. apps/chrg/applications/utils/chrg_sink.h: 新增OCP测试状态枚举与注释 4. apps/chrg/applications/thread/chrg_comm.h: 移除废弃的sink_proto_diff_t结构体及相关声明 5. apps/chrg/applications/thread/chrg_roll_sou.h/.c: 重构南向升级逻辑,新增帧缓存与安全发送机制 6. apps/chrg/applications/utils/chrg_eload.c: 重构Ymodem应答队列,优化并发访问处理 7. apps/chrg/applications/utils/chrg_sink.c: 新增OCP测试功能实现与状态切换逻辑 8. apps/chrg/applications/utils/chrg_source.c: 替换protective_curr为vc.set_current,移除冗余enable标志 9. apps/chrg/applications/thread/chrg_north.c: 重构电源电压控制逻辑,简化快充输出流程 10. apps/chrg/applications/thread/chrg_comm.c: 全面重构Ymodem升级协议与参数差异处理逻辑
This commit is contained in:
@@ -27,100 +27,6 @@
|
||||
#define SOURCE_VOLT_ERROR_MV 200U
|
||||
#define SOURCE_VOLT_RETRY_FRAMES 3U
|
||||
|
||||
/*****************************************************************
|
||||
函数名称: chrg_source_voltage_control
|
||||
函数描述: 统一处理Source目标电压、误差判断和状态机切换
|
||||
输入参数: idx:当前通道 pSW:通道控制结构体 volt_mv:目标电压(mV)
|
||||
curr_ma:目标电流(mA)
|
||||
输出参数: 更新source.vc及source.work_mode
|
||||
返回说明: 0:状态处理成功 <0:参数错误或命令提交失败
|
||||
其它说明: 目标电压变化时立即进入RUNING;实际电压与目标值的绝对误差
|
||||
小于等于200mV时进入WAIT;连续3次超过误差范围时重发
|
||||
*****************************************************************/
|
||||
static int chrg_source_voltage_control(eIDX_SOU_CH idx,
|
||||
struct chrg_switch_t *pSW,
|
||||
rt_uint16_t volt_mv,
|
||||
rt_uint16_t curr_ma)
|
||||
{
|
||||
rt_uint16_t volt_error;
|
||||
static rt_uint16_t last_volt_mv[TOTAL_SOU_CHS] = {0};
|
||||
static rt_uint8_t last_volt_valid[TOTAL_SOU_CHS] = {0};
|
||||
static rt_uint8_t last_protocol[TOTAL_SOU_CHS] = {0};
|
||||
static rt_uint8_t volt_retry_count[TOTAL_SOU_CHS] = {0};
|
||||
rt_bool_t need_set = RT_FALSE;
|
||||
int ret;
|
||||
|
||||
if ((idx >= TOTAL_SOU_CHS) || (pSW == RT_NULL)) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
/* 保存本次目标值,RUNING状态只使用其中的目标电压下发命令。 */
|
||||
pSW->source.vc.set_voltage = volt_mv;
|
||||
pSW->source.vc.set_current = curr_ma;
|
||||
|
||||
if (!pSW->source.On_Flag) {
|
||||
/* 关机后清除历史缓存,下一次启动必须重新设置目标电压。 */
|
||||
last_volt_valid[idx] = 0;
|
||||
volt_retry_count[idx] = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* 使用绝对误差,避免实际电压低于目标值时发生无符号下溢。 */
|
||||
volt_error = (pSW->Now_voltage >= volt_mv) ?
|
||||
(pSW->Now_voltage - volt_mv) :
|
||||
(volt_mv - pSW->Now_voltage);
|
||||
|
||||
if (!last_volt_valid[idx] ||
|
||||
(last_protocol[idx] != (rt_uint8_t)pSW->source.protocol) ||
|
||||
(volt_mv != last_volt_mv[idx])) {
|
||||
/* 首次设置、协议切换或目标变化时立即下发新电压。 */
|
||||
need_set = RT_TRUE;
|
||||
} else if (volt_error <= SOURCE_VOLT_ERROR_MV) {
|
||||
/* 实际电压误差不超过200mV即可视为设置成功。 */
|
||||
volt_retry_count[idx] = 0;
|
||||
} else {
|
||||
/* 给功率板三个反馈周期的稳定时间,避免每帧重复写电压。 */
|
||||
if (volt_retry_count[idx] < SOURCE_VOLT_RETRY_FRAMES) {
|
||||
volt_retry_count[idx]++;
|
||||
}
|
||||
if (volt_retry_count[idx] >= SOURCE_VOLT_RETRY_FRAMES) {
|
||||
need_set = RT_TRUE;
|
||||
}
|
||||
}
|
||||
|
||||
pSW->source.work_mode = need_set ?
|
||||
SOURCE_WORK_RUNING : SOURCE_WORK_WAIT;
|
||||
ret = chrg_source_work_state(idx, pSW);
|
||||
if ((ret == 0) && need_set) {
|
||||
/* 只有命令成功提交后才更新缓存,失败时下一次继续重试。 */
|
||||
last_volt_mv[idx] = volt_mv;
|
||||
last_protocol[idx] = (rt_uint8_t)pSW->source.protocol;
|
||||
last_volt_valid[idx] = 1;
|
||||
volt_retry_count[idx] = 0;
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
函数名称: chrg_source_standard_output
|
||||
函数描述: 使用COM配置值驱动标准协议电源输出
|
||||
输入参数: idx:当前通道 pSW:通道控制结构体
|
||||
输出参数: 更新source.vc及source.work_mode
|
||||
返回说明: 无返回值
|
||||
其它说明: 外部协议值0x20映射为PRO_NONE;本函数不解析北向协议返回
|
||||
数据,目标电压和保护电流直接使用COM配置值
|
||||
*****************************************************************/
|
||||
void chrg_source_standard_output(eIDX_SOU_CH idx, struct chrg_switch_t *pSW)
|
||||
{
|
||||
if (pSW == RT_NULL) {
|
||||
return;
|
||||
}
|
||||
|
||||
(void)chrg_source_voltage_control(idx, pSW,
|
||||
pSW->source.max_vol, pSW->source.protective_curr);
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
函数名称: chrg_source_parse
|
||||
函数描述: 按电源协议类型处理目标电压并驱动Source状态机
|
||||
@@ -128,9 +34,8 @@ void chrg_source_standard_output(eIDX_SOU_CH idx, struct chrg_switch_t *pSW)
|
||||
pNOR:北向总控结构体
|
||||
输出参数: 更新电压、电流、source.work_mode及电压重试计数
|
||||
返回说明: -
|
||||
其它说明: 标准协议不解析frame_data,直接使用COM配置值;快充协议读取
|
||||
frame_data[8~11]中的协商目标值。新目标立即进入RUNING,
|
||||
实际误差连续3帧超过200mV时重发
|
||||
其它说明: 标准协议只在本函数中识别,不使用北向返回值控制输出;快充协议
|
||||
读取frame_data[8~11]中的协商目标值,并统一交给RUNING状态比较
|
||||
*****************************************************************/
|
||||
void chrg_source_parse(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
|
||||
struct chrg_switch_t *pSW, struct chrg_north_t *pNOR)
|
||||
@@ -142,19 +47,22 @@ void chrg_source_parse(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
|
||||
return;
|
||||
}
|
||||
|
||||
/* 标准协议使用COM设定值,不读取北向协议返回帧。 */
|
||||
/* 标准协议仍在北向解析入口处判断,但输出已经由INIT/COM直接完成。
|
||||
* 不读取北向返回电压,也不在这里重复下发南向电压。 */
|
||||
if (pSW->source.protocol == PRO_NONE) {
|
||||
chrg_source_standard_output(idx, pSW);
|
||||
return;
|
||||
}
|
||||
|
||||
if (frame_data == RT_NULL) {
|
||||
if ((frame_data == RT_NULL) || !pSW->source.On_Flag) {
|
||||
return;
|
||||
}
|
||||
volt_mv = (frame_data[8] << 8) | frame_data[9];
|
||||
curr_ma = (frame_data[10] << 8) | frame_data[11];
|
||||
/* 快充只负责解析北向协商结果,后续控制逻辑与标准协议共用。 */
|
||||
(void)chrg_source_voltage_control(idx, pSW, volt_mv, curr_ma);
|
||||
pSW->source.vc.set_voltage = volt_mv;
|
||||
pSW->source.vc.set_current = curr_ma;
|
||||
/* 北向只负责提取本次返回值;新旧电压比较放在RUNING状态完成。 */
|
||||
pSW->source.work_mode = SOURCE_WORK_RUNING;
|
||||
(void)chrg_source_work_state(idx, pSW);
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
@@ -170,13 +78,10 @@ void chrg_source_setting(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
|
||||
struct chrg_switch_t *pSW, struct chrg_north_t *pNOR)
|
||||
{
|
||||
//执行输出调整
|
||||
if (pSW->source.On_Flag == 1) {
|
||||
if(pSW->source.protocol == PRO_NONE){//标准下输出设定值
|
||||
chrg_source_standard_output(idx, pSW);
|
||||
}
|
||||
else{ //快充部分按协商结果输出
|
||||
if (pSW->source.On_Flag == 1) {
|
||||
if (pSW->source.protocol != PRO_NONE) {
|
||||
/* 标准协议不依赖北向返回值;快充协议继续解析协商结果。 */
|
||||
chrg_source_parse(frame_data, idx, pSW, pNOR);
|
||||
// rt_kprintf("set_end\r\n");
|
||||
}
|
||||
}else if(pSW->source.On_Flag == 0){
|
||||
//chrg_source_stop(frame_data,pSW,idx,&pNOR->enable[idx]);
|
||||
|
||||
Reference in New Issue
Block a user