|
|
|
@@ -26,6 +26,8 @@
|
|
|
|
|
#define SOU_MUTEX_TIMEOUT_MS 100
|
|
|
|
|
#define SOU_RESPONSE_TIMEOUT_MS 20
|
|
|
|
|
#define SOU_COM_MAX_ATTEMPTS 3
|
|
|
|
|
#define SOU_COM_BURST_MAX 10U
|
|
|
|
|
#define SOU_COM_RETRY_YIELD_MS 1U
|
|
|
|
|
#define SOU_REL_SWITCH_DELAY_MS 100U
|
|
|
|
|
#define SOU_CHANNEL_BIT(ch) ((rt_uint8_t)(1U << (ch)))
|
|
|
|
|
|
|
|
|
@@ -45,6 +47,8 @@ static rt_uint8_t g_sou_version_ready[TOTAL_SOU_CHS] = {0U};
|
|
|
|
|
* 回码错误或超时时游标保持不变,使同一条命令优先完成重发,避免破坏
|
|
|
|
|
* 本通道批次内的寄存器执行顺序。 */
|
|
|
|
|
static rt_uint8_t g_sou_com_batch_rr_next_ch = 0U;
|
|
|
|
|
static rt_uint8_t g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
|
|
|
static rt_uint8_t g_sou_com_batch_burst_count = 0U;
|
|
|
|
|
|
|
|
|
|
struct chrg_rollsou_t chrgrollsou = {
|
|
|
|
|
.run = RUN_AUTO,
|
|
|
|
@@ -535,6 +539,29 @@ static int sou_com_batch_next(rt_uint8_t *ch, rt_uint16_t *reg, rt_uint16_t *val
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
now = rt_tick_get();
|
|
|
|
|
/* 短路保活的WORK_START达到周期后优先下发,避免长批次挤占30ms保活窗口。 */
|
|
|
|
|
for (offset = 0; offset < TOTAL_SOU_CHS; offset++) {
|
|
|
|
|
idx = (rt_uint8_t)((g_sou_com_batch_rr_next_ch + offset) %
|
|
|
|
|
TOTAL_SOU_CHS);
|
|
|
|
|
sou_com_batch_ch_t *pBatch = &g_sou_com_batch.ch[idx];
|
|
|
|
|
if ((pBatch->pending != RT_TRUE) ||
|
|
|
|
|
(pBatch->cur_idx >= pBatch->reg_cnt) ||
|
|
|
|
|
(pBatch->reg_list[pBatch->cur_idx] != REG_WORK) ||
|
|
|
|
|
(pBatch->val_list[pBatch->cur_idx] != WORK_START) ||
|
|
|
|
|
(chrgnorth.sw[idx].id != ID_SINK) ||
|
|
|
|
|
(chrgnorth.sw[idx].sink.work_mode != SINK_WORK_ON_TEST_SHORT) ||
|
|
|
|
|
(chrgnorth.sw[idx].sink.test.test_mode != SINK_TEST_SHORT) ||
|
|
|
|
|
((pBatch->next_send_tick != 0U) &&
|
|
|
|
|
((rt_int32_t)(now - pBatch->next_send_tick) < 0))) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
pBatch->next_send_tick = 0U;
|
|
|
|
|
*ch = idx;
|
|
|
|
|
*reg = pBatch->reg_list[pBatch->cur_idx];
|
|
|
|
|
*value = pBatch->val_list[pBatch->cur_idx];
|
|
|
|
|
rt_mutex_release(pTHR->mutex);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
/* 从上一次成功命令的下一个通道开始查找,使多通道批次按
|
|
|
|
|
* "CH1一条 -> CH2一条 -> ..." 交替执行,而不是低编号通道独占总线。 */
|
|
|
|
|
for (offset = 0; offset < TOTAL_SOU_CHS; offset++) {
|
|
|
|
@@ -593,9 +620,6 @@ static int sou_com_batch_complete(rt_uint8_t ch, rt_uint16_t reg, rt_uint16_t va
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pBatch->cur_idx++;
|
|
|
|
|
/* 当前寄存器已收到匹配写回码。下一次调度从下一个通道开始;
|
|
|
|
|
* 当前通道的下一条命令会在其它就绪通道各执行一条后继续发送。 */
|
|
|
|
|
g_sou_com_batch_rr_next_ch = (rt_uint8_t)((ch + 1U) % TOTAL_SOU_CHS);
|
|
|
|
|
/* 暂时关闭副继电器与主继电器之间的固定延时;需要恢复时重新启用本段。 */
|
|
|
|
|
if ((reg == REG_REL_ON) && (value == ALL_REL_OFF) &&
|
|
|
|
|
(pBatch->cur_idx < pBatch->reg_cnt) &&
|
|
|
|
@@ -616,6 +640,19 @@ static int sou_com_batch_complete(rt_uint8_t ch, rt_uint16_t reg, rt_uint16_t va
|
|
|
|
|
pBatch->cur_idx = 0;
|
|
|
|
|
pBatch->reg_cnt = 0;
|
|
|
|
|
pBatch->next_send_tick = 0U;
|
|
|
|
|
g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
|
|
|
g_sou_com_batch_burst_count = 0U;
|
|
|
|
|
g_sou_com_batch_rr_next_ch = (rt_uint8_t)((ch + 1U) % TOTAL_SOU_CHS);
|
|
|
|
|
} else if (g_sou_com_batch_burst_ch != ch) {
|
|
|
|
|
g_sou_com_batch_burst_ch = ch;
|
|
|
|
|
g_sou_com_batch_burst_count = 1U;
|
|
|
|
|
g_sou_com_batch_rr_next_ch = ch;
|
|
|
|
|
} else if (++g_sou_com_batch_burst_count >= SOU_COM_BURST_MAX) {
|
|
|
|
|
g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
|
|
|
g_sou_com_batch_burst_count = 0U;
|
|
|
|
|
g_sou_com_batch_rr_next_ch = (rt_uint8_t)((ch + 1U) % TOTAL_SOU_CHS);
|
|
|
|
|
} else {
|
|
|
|
|
g_sou_com_batch_rr_next_ch = ch;
|
|
|
|
|
}
|
|
|
|
|
rt_mutex_release(pTHR->mutex);
|
|
|
|
|
return 0;
|
|
|
|
@@ -636,6 +673,12 @@ int chrg_sou_com_batch_cancel(eIDX_SOU_CH ch)
|
|
|
|
|
pBatch->cur_idx = 0;
|
|
|
|
|
pBatch->reg_cnt = 0;
|
|
|
|
|
pBatch->next_send_tick = 0U;
|
|
|
|
|
if (g_sou_com_batch_burst_ch == ch) {
|
|
|
|
|
g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
|
|
|
g_sou_com_batch_burst_count = 0U;
|
|
|
|
|
g_sou_com_batch_rr_next_ch =
|
|
|
|
|
(rt_uint8_t)((ch + 1U) % TOTAL_SOU_CHS);
|
|
|
|
|
}
|
|
|
|
|
/* 已取消事务对应的优先P1量测同样失效,后续状态会重新申请新量测。 */
|
|
|
|
|
g_sou_measure_once_pending_mask &= (rt_uint8_t)~SOU_CHANNEL_BIT(ch);
|
|
|
|
|
g_sou_measure_once_active_mask &= (rt_uint8_t)~SOU_CHANNEL_BIT(ch);
|
|
|
|
@@ -735,6 +778,23 @@ static void sou_com_command_failed(rt_uint8_t ch, rt_uint16_t reg, rt_uint16_t v
|
|
|
|
|
|
|
|
|
|
(*retry_count)++;
|
|
|
|
|
if (*retry_count < SOU_COM_MAX_ATTEMPTS) {
|
|
|
|
|
struct chrg_thread_t *pTHR = &chrgthr[IDX_THR_ROLL_SOU];
|
|
|
|
|
if (rt_mutex_take(pTHR->mutex,
|
|
|
|
|
rt_tick_from_millisecond(SOU_MUTEX_TIMEOUT_MS)) == RT_EOK) {
|
|
|
|
|
sou_com_batch_ch_t *pBatch = &g_sou_com_batch.ch[ch];
|
|
|
|
|
if ((pBatch->pending == RT_TRUE) &&
|
|
|
|
|
(pBatch->cur_idx < pBatch->reg_cnt) &&
|
|
|
|
|
(pBatch->reg_list[pBatch->cur_idx] == reg) &&
|
|
|
|
|
(pBatch->val_list[pBatch->cur_idx] == value)) {
|
|
|
|
|
pBatch->next_send_tick = rt_tick_get() +
|
|
|
|
|
rt_tick_from_millisecond(SOU_COM_RETRY_YIELD_MS);
|
|
|
|
|
g_sou_com_batch_rr_next_ch =
|
|
|
|
|
(rt_uint8_t)((ch + 1U) % TOTAL_SOU_CHS);
|
|
|
|
|
g_sou_com_batch_burst_ch = TOTAL_SOU_CHS;
|
|
|
|
|
g_sou_com_batch_burst_count = 0U;
|
|
|
|
|
}
|
|
|
|
|
rt_mutex_release(pTHR->mutex);
|
|
|
|
|
}
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
@@ -766,7 +826,7 @@ void chrg_roll_sou_thread_entry (void *data)
|
|
|
|
|
rt_uint8_t run = 0;
|
|
|
|
|
rt_uint8_t index = 0, idx = 0;
|
|
|
|
|
rt_uint8_t batch_ch = 0;
|
|
|
|
|
rt_uint8_t retry_count = 0;
|
|
|
|
|
rt_uint8_t retry_count[TOTAL_SOU_CHS] = {0};
|
|
|
|
|
rt_uint8_t command_ch = 0;
|
|
|
|
|
rt_uint8_t single_ch = 0;
|
|
|
|
|
rt_uint8_t single_part = 0;
|
|
|
|
@@ -796,7 +856,7 @@ void chrg_roll_sou_thread_entry (void *data)
|
|
|
|
|
version_probe = RT_FALSE;
|
|
|
|
|
response_part = MB_R_PART1;
|
|
|
|
|
if ((run != RUN_COM) && (run != RUN_UPDATA)) {
|
|
|
|
|
retry_count = 0;
|
|
|
|
|
rt_memset(retry_count, 0, sizeof(retry_count));
|
|
|
|
|
}
|
|
|
|
|
// rt_kprintf("------ROLL SOU START \n--------");
|
|
|
|
|
switch (run) {
|
|
|
|
@@ -855,7 +915,6 @@ void chrg_roll_sou_thread_entry (void *data)
|
|
|
|
|
{
|
|
|
|
|
int next_ret = sou_com_batch_next(&command_ch, &command_reg, &command_value);
|
|
|
|
|
if (next_ret <= 0) {
|
|
|
|
|
retry_count = 0;
|
|
|
|
|
rt_thread_mdelay(1);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
@@ -919,10 +978,10 @@ void chrg_roll_sou_thread_entry (void *data)
|
|
|
|
|
if (response_valid) {
|
|
|
|
|
if (run == RUN_COM) {
|
|
|
|
|
if (sou_com_batch_complete(batch_ch, command_reg, command_value) == 0) {
|
|
|
|
|
retry_count = 0;
|
|
|
|
|
retry_count[batch_ch] = 0;
|
|
|
|
|
} else {
|
|
|
|
|
sou_com_command_failed(batch_ch, command_reg, command_value,
|
|
|
|
|
"batch changed", &retry_count);
|
|
|
|
|
"batch changed", &retry_count[batch_ch]);
|
|
|
|
|
}
|
|
|
|
|
} else if (run != RUN_AUTO) {
|
|
|
|
|
sou_single_command_finish(run);
|
|
|
|
@@ -940,7 +999,7 @@ void chrg_roll_sou_thread_entry (void *data)
|
|
|
|
|
}
|
|
|
|
|
} else if (run == RUN_COM) {
|
|
|
|
|
sou_com_command_failed(batch_ch, command_reg, command_value,
|
|
|
|
|
"invalid response", &retry_count);
|
|
|
|
|
"invalid response", &retry_count[batch_ch]);
|
|
|
|
|
} else {
|
|
|
|
|
LOG_E("crc failed");
|
|
|
|
|
}
|
|
|
|
@@ -956,7 +1015,7 @@ void chrg_roll_sou_thread_entry (void *data)
|
|
|
|
|
}
|
|
|
|
|
if ((run == RUN_COM) && command_valid) {
|
|
|
|
|
sou_com_command_failed(batch_ch, command_reg, command_value,
|
|
|
|
|
"timeout", &retry_count);
|
|
|
|
|
"timeout", &retry_count[batch_ch]);
|
|
|
|
|
} else if (run != RUN_AUTO) {
|
|
|
|
|
sou_single_command_finish(run);
|
|
|
|
|
}
|
|
|
|
|