@@ -1,7 +1,7 @@
/****************************************************************************
文件名称 : chrg_sink.c
完成日期 :
当前版本号 : V1.1.14
当前版本号 : V1.1.19
主要功能 : 北向数据sink请求数据填充及发送。
版本历史 : 创建原始版本
说明 :
@@ -41,10 +41,12 @@ static rt_tick_t g_sink_unload_query_tick[TOTAL_SOU_CHS] = {0};
static rt_uint8_t g_sink_unload_confirm_count [ TOTAL_SOU_CHS ] = { 0 } ;
/* 记录各通道短路测试最近一次WORK_START的发送时间。 */
static rt_tick_t g_sink_short_start_tick [ TOTAL_SOU_CHS ] = { 0 } ;
# if SINK_VON_VOLTAGE_SOURCE == SINK_VON_VOLTAGE_FROM_PROTOCOL
/* 只消费新的协议板0x20电压回码,避免同一帧重复计入稳定次数。 */
/* 只消费新的V_On电压采样,避免同一帧重复计入稳定确认。 */
static rt_uint32_t g_sink_von_voltage_seq [ TOTAL_SOU_CHS ] = { 0 } ;
# endif
/* 协议切换期间清零,避免旧电压样本累计成启动确认。 */
static rt_uint8_t g_sink_von_stable_count [ TOTAL_SOU_CHS ] = { 0 } ;
/* 自动停止带载后的恢复确认计数,独立于首次握手的电压确认计数。 */
static rt_uint8_t g_sink_load_restart_confirm_count [ TOTAL_SOU_CHS ] = { 0 } ;
enum {
SINK_UNLOAD_NEXT_RUNNING = 0 ,
@@ -447,9 +449,15 @@ static int chrg_sink_work_load_stop(eIDX_SOU_CH idx,
if ( ( idx > = TOTAL_SOU_CHS ) | | ( pSW = = RT_NULL ) ) {
return - 1 ;
}
/* 自动停止优先于同通道残留启动批次,禁止STOP后继续发送WORK_START。 */
if ( chrg_sou_com_batch_cancel ( idx ) ! = 0 ) {
return - 1 ;
}
if ( chrg_sou_com_batch_submit ( idx , & reg , & val , 1U ) ! = 0 ) {
return - 1 ;
}
g_sink_von_stable_count [ idx ] = 0U ;
g_sink_load_restart_confirm_count [ idx ] = 0U ;
pSW - > sink . work_mode = SINK_WORK_LOAD_STANDBY ;
return 0 ;
}
@@ -665,15 +673,23 @@ static int chrg_sink_work_switch(eIDX_SOU_CH idx, struct chrg_switch_t *pSW)
*****************************************************************/
static int chrg_sink_work_protocol_unload ( eIDX_SOU_CH idx , struct chrg_switch_t * pSW )
{
rt_uint16_t reg = REG_WORK ;
rt_uint16_t value = WORK_STOP ;
rt_uint16_t regs [ 2 ] = { REG_WORK , Eload_Start_V_ON } ;
rt_uint16_t values [ 2 ] ;
if ( chrg_sou_com_batch_submit ( idx , & reg , & value , 1U ) ! = 0 ) {
if ( ( idx > = TOTAL_SOU_CHS ) | | ( pSW = = RT_NULL ) ) {
return - 1 ;
}
values [ 0 ] = WORK_STOP ;
values [ 1 ] = ( pSW - > sink . volt_on < = 500U ) ? 500U : pSW - > sink . volt_on ;
if ( chrg_sou_com_batch_submit ( idx , regs , values , 2U ) ! = 0 ) {
return - 1 ;
}
/* WORK_STOP回码只确认命令接收;还需连续读取两帧合格的实际电流。 */
chrg_sink_unload_confirm_reset ( idx ) ;
g_sink_von_stable_count [ idx ] = 0U ;
g_sink_load_restart_confirm_count [ idx ] = 0U ;
pSW - > sink . work_mode = SINK_WORK_UNLOAD_WAIT ;
return 0 ;
}
@@ -683,31 +699,35 @@ static int chrg_sink_work_protocol_unload(eIDX_SOU_CH idx, struct chrg_switch_t
函数描述: 执行协议或CC完整重握手前的南向安全断开和副继电器恢复
输入参数: idx:南向功率板通道 pSW:当前通道控制结构体
输出参数: 命令提交成功后进入SINK_WORK_HANDSHAKE_WAIT
返回说明: 0:四 条命令提交成功 <0:参数或命令提交失败
其它说明: 命令顺序固定为WORK_STOP、CURRENT=1000mA、ALL_REL_OFF、FU_REL _ON;
WORK_STOP已关闭电子负载,1000mA仅为功率板停止阶段保存的电流设定值 ;
返回说明: 0:两 条命令提交成功 <0:参数或命令提交失败
其它说明: 命令顺序固定为WORK_STOP、Eload_Start_V _ON;
WORK_STOP已关闭电子负载,V_ON随后更新到功率板 ;
主副继电器全部断开并确认回码后再单独打开副继电器;
北向Sink继电器保持接通,低电流确认完成后才允许重新握手
*****************************************************************/
static int chrg_sink_work_handshake_prepare ( eIDX_SOU_CH idx ,
struct chrg_switch_t * pSW )
{
rt_uint16_t reg = REG_WORK ;
rt_uint16_t value = WORK_STOP ;
rt_uint16_t regs [ 2 ] = { REG_WORK , Eload_Start_V_ON } ;
rt_uint16_t values [ 2 ] ;
if ( ( idx > = TOTAL_SOU_CHS ) | | ( pSW = = RT_NULL ) ) {
return - 1 ;
}
values [ 0 ] = WORK_STOP ;
values [ 1 ] = ( pSW - > sink . volt_on < = 500U ) ? 500U : pSW - > sink . volt_on ;
/* 新CC/协议事务必须抢占本通道尚未发送的旧启动和设定命令。 */
if ( chrg_sou_com_batch_cancel ( idx ) ! = 0 ) {
return - 1 ;
}
if ( chrg_sou_com_batch_submit ( idx , & reg , & value , 1U ) ! = 0 ) {
if ( chrg_sou_com_batch_submit ( idx , regs , values , 2U ) ! = 0 ) {
return - 1 ;
}
/* 协议或CC切换先停止电子负载,连续2次确认低电流后再操作继电器。 */
chrg_sink_unload_confirm_reset ( idx ) ;
g_sink_von_stable_count [ idx ] = 0U ;
g_sink_load_restart_confirm_count [ idx ] = 0U ;
pSW - > sink . work_mode = SINK_WORK_HANDSHAKE_WAIT ;
return 0 ;
}
@@ -719,7 +739,7 @@ static int chrg_sink_work_handshake_prepare(eIDX_SOU_CH idx,
full_handshake:RT_TRUE为完整重握手 RT_FALSE为协议参数软卸载
输出参数: 确认成功后将sink.work_mode切换为SINK_WORK_WAIT
返回说明: 0:继续等待或确认成功 <0:状态查询或优先量测请求失败
其它说明: 软卸载只 要求WORK_STOP收到匹配回码后建立量测基线;完整重握手
其它说明: 软卸载要求WORK_STOP和V_ON 收到匹配回码后建立量测基线;完整重握手
还要求CURRENT=1000mA、ALL_REL_OFF和FU_REL_ON回码成功,但保持北向Sink继电器接通
*****************************************************************/
static int chrg_sink_work_current_wait ( eIDX_SOU_CH idx ,
@@ -831,13 +851,14 @@ static int chrg_sink_TEST_DYNA_ON(rt_uint8_t ch, struct chrg_switch_t *pSW)
*****************************************************************/
static int chrg_sink_TEST_SHORT_ON ( rt_uint8_t ch , struct chrg_switch_t * pSW )
{
rt_uint16_t regs [ 3 ] = { ModbusRTU_ShortTest_Hold_Time , ModbusRTU_ShortTest_CMD_MODE_FLAG , REG_WORK } ;
rt_uint16_t vals [ 3 ] = { 0 , 0 , 0 } ;
rt_uint16_t regs [ 4 ] = { Eload_Start_V_ON , ModbusRTU_ShortTest_Hold_Time ,
ModbusRTU_ShortTest_CMD_MODE_FLAG , REG_WORK } ;
rt_uint16_t vals [ 4 ] = { SINK_SHORT_TEST_VON_MV , 0 , 0 , 0 } ;
/* 外部工程固定短路测试保持60秒且不带载。 */
vals [ 0 ] = 60000 ;
vals [ 1 ] = ModbusRTU_DYNA_CMD_MODE_RUN_NO_CURR ;
vals [ 2 ] = WORK_START ;
if ( chrg_sou_com_batch_submit ( ( eIDX_SOU_CH ) ch , regs , vals , 3 ) = = 0 ) {
vals [ 1 ] = 60000 ;
vals [ 2 ] = ModbusRTU_DYNA_CMD_MODE_RUN_NO_CURR ;
vals [ 3 ] = WORK_START ;
if ( chrg_sou_com_batch_submit ( ( eIDX_SOU_CH ) ch , regs , vals , 4 ) = = 0 ) {
g_sink_short_start_tick [ ch ] = rt_tick_get ( ) ;
return 0 ;
}
@@ -897,10 +918,10 @@ static rt_uint16_t chrg_sink_von_voltage_get(const struct chrg_switch_t *pSW)
int chrg_sink_volt_judge ( eIDX_SOU_CH idx , struct chrg_switch_t * pSW )
{
rt_uint8_t stable_confirm_count ;
rt_uint32_t voltage_seq ;
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 } ;
static rt_uint8_t volt_stable_count [ TOTAL_SOU_CHS ] = { 0 } ;
static rt_tick_t running_start_tick [ TOTAL_SOU_CHS ] = { 0 } ;
rt_uint16_t von_voltage ;
@@ -908,18 +929,32 @@ int chrg_sink_volt_judge(eIDX_SOU_CH idx, struct chrg_switch_t *pSW)
return - 1 ;
}
# if SINK_VON_VOLTAGE_SOURCE == SINK_VON_VOLTAGE_FROM_PROTOCOL
if ( pSW - > Sink_voltage_seq = = g_sink_von_voltage_seq [ idx ] ) {
voltage_seq = pSW - > Sink_voltage_seq ;
# else
voltage_seq = chrg_eload_get_sample_seq ( idx ) ;
# endif
if ( voltage_seq = = g_sink_von_voltage_seq [ idx ] ) {
return chrg_sink_work ( idx , pSW ) ;
}
g_sink_von_voltage_seq [ idx ] = voltage_seq ;
/* 卸载和协议重配尚未完成时,旧电压只能用于推进安全流程,不能触发WORK_START。 */
if ( ( pSW - > sink . work_mode = = SINK_WORK_UNLOAD_WAIT ) | |
( pSW - > sink . work_mode = = SINK_WORK_HANDSHAKE_WAIT ) | |
( ( pSW - > sink . work_mode ! = SINK_WORK_RUNING ) & &
( pSW - > sink . work_mode ! = SINK_WORK_LOAD_STANDBY ) & &
( pSW - > sub ! = IDX_GET_SINK_VC ) ) ) {
g_sink_von_stable_count [ idx ] = 0U ;
g_sink_load_restart_confirm_count [ idx ] = 0U ;
return chrg_sink_work ( idx , pSW ) ;
}
g_sink_von_voltage_seq [ idx ] = pSW - > Sink_voltage_seq ;
# endif
von_voltage = chrg_sink_von_voltage_get ( pSW ) ;
stable_confirm_count = chrg_sink_voltage_confirm_count (
pSW - > sink . pro_loadv ) ;
if ( ( pSW - > sink . On_work = = 1 ) & & ( pSW - > sink . test . test_mode = = 0 ) ) {
if ( von_voltage < = WORK_MIN_VOLT ) {
volt_stable_count [ idx ] = 0 ;
g_sink_von_stable_count [ idx ] = 0U ;
g_sink_load_restart_confirm_count [ idx ] = 0U ;
if ( volt_set_ok [ idx ] = = 1 & & + + volt_low [ idx ] > = 50 ) {
volt_set_ok [ idx ] = 0 ;
no_curr [ idx ] = 0 ;
@@ -933,47 +968,58 @@ int chrg_sink_volt_judge(eIDX_SOU_CH idx, struct chrg_switch_t *pSW)
pSW - > change_flag = 0 ;
if ( pSW - > sink . work_mode = = SINK_WORK_LOW_MONITOR ) {
/* 副继电器恢复后首次重新检测到输入电压,只启动协议重握手。 */
volt_stable_count [ idx ] = 0 ;
g_sink_von_stable_count [ idx ] = 0U ;
no_curr [ idx ] = 0 ;
pSW - > sink . work_mode = SINK_WORK_WAIT ;
} else if ( ( pSW - > sink . work_mode = = SINK_WORK_LOW ) | |
( pSW - > sink . work_mode = = SINK_WORK_LOW_AUX_WAIT ) ) {
/* 掉电安全批次完成前,不得进入RUNING。 */
volt_stable_count [ idx ] = 0 ;
g_sink_von_stable_count [ idx ] = 0U ;
no_curr [ idx ] = 0 ;
} else if ( von_voltage > pSW - > sink . volt_on ) {
if ( ( pSW - > sink . work_mode ! = SINK_WORK_RUNING ) & &
( pSW - > sink . work_mode ! = SINK_WORK_LOAD_STANDBY ) ) {
if ( volt_stable_count [ idx ] < stable_confirm_count ) {
volt_stable_count [ idx ] + + ;
if ( g_sink_von_stable_count [ idx ] < stable_confirm_count ) {
g_sink_von_stable_count [ idx ] + + ;
}
if ( volt_stable_count [ idx ] > = stable_confirm_count ) {
if ( g_sink_von_stable_count [ idx ] > = stable_confirm_count ) {
/* 连续达到当前目标对应的确认帧数后进入RUNING。 */
volt_stable_count [ idx ] = 0 ;
g_sink_von_stable_count [ idx ] = 0U ;
pSW - > sink . work_mode = SINK_WORK_RUNING ;
running_start_tick [ idx ] = rt_tick_get ( ) ;
no_curr [ idx ] = 0 ;
}
} else if ( pSW - > sink . work_mode = = SINK_WORK_LOAD_STANDBY ) {
volt_stable_count [ idx ] = 0 ;
no_curr [ idx ] = 0 ;
if ( chrg_sink_load_should_start ( pSW ) ) {
/* 因输入电压不足停止带载后,电压恢复时重新进入稳定确认 。 */
pSW - > sink . work_mode = SINK_WORK_WAIT ;
/* 自动停止后只接受新的电压采样;达到恢复确认帧数才允许重启 。 */
if ( g_sink_load_restart_confirm_count [ idx ] <
SINK_LOAD_RESTART_CONFIRM_COUNT ) {
g_sink_load_restart_confirm_count [ idx ] + + ;
}
if ( g_sink_load_restart_confirm_count [ idx ] > =
SINK_LOAD_RESTART_CONFIRM_COUNT ) {
g_sink_load_restart_confirm_count [ idx ] = 0U ;
pSW - > sink . work_mode = SINK_WORK_RUNING ;
running_start_tick [ idx ] = rt_tick_get ( ) ;
}
} else {
g_sink_load_restart_confirm_count [ idx ] = 0U ;
}
} else if ( ( pSW - > Now_current > 200 ) | | ( pSW - > sink . loadc < 200 ) ) {
volt_stable_count [ idx ] = 0 ;
g_sink_von_stable_count [ idx ] = 0U ;
no_curr [ idx ] = 0 ;
} else if ( ( rt_tick_t ) ( rt_tick_get ( ) - running_start_tick [ idx ] ) > =
rt_tick_from_millisecond ( SINK_CURRENT_START_GRACE_MS ) ) {
volt_stable_count [ idx ] = 0 ;
g_sink_von_stable_count [ idx ] = 0U ;
if ( + + no_curr [ idx ] > = 2 ) {
pSW - > sink . work_mode = SINK_WORK_WAIT ;
no_curr [ idx ] = 0 ;
}
}
} else {
volt_stable_count [ idx ] = 0 ;
g_sink_von_stable_count [ idx ] = 0U ;
g_sink_load_restart_confirm_count [ idx ] = 0U ;
no_curr [ idx ] = 0 ;
/* 配置电压源低于负载设置的V_On时,立即停止电子负载。 */
if ( pSW - > sink . work_mode = = SINK_WORK_RUNING ) {
@@ -985,7 +1031,7 @@ int chrg_sink_volt_judge(eIDX_SOU_CH idx, struct chrg_switch_t *pSW)
volt_low [ idx ] = 0 ;
no_curr [ idx ] = 0 ;
volt_set_ok [ idx ] = 0 ;
volt_stable_count [ idx ] = 0 ;
g_sink_von_stable_count [ idx ] = 0U ;
}
return chrg_sink_work ( idx , pSW ) ;
}