docs: 更新修改日志并新增北向功率板升级计划文档
chore: 升级所有相关文件版本号至V1.1.39 feat: 实现北向事务隔离逻辑,新增通道切换清理和事务号匹配机制 feat: 新增北向请求开始函数,处理通道切换后的接收清理和事务号生成 perf: 调整北向回码等待超时时间,量测回码等待80ms,配置回码等待150ms refactor: 修改北向回码匹配函数,增加通道和事务号校验
This commit is contained in:
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
文件名称 : chrg_comm.c
|
||||
完成日期 :
|
||||
当前版本号 : V1.1.37
|
||||
当前版本号 : V1.1.39
|
||||
主要功能 : 实现对外通讯协议 MODBUS 服务端,以独立线程等待处理响应请求数据。
|
||||
版本历史 : 创建原始版本
|
||||
说明 :
|
||||
@@ -27,7 +27,7 @@
|
||||
#include "stm32f4xx_hal.h"
|
||||
|
||||
#define HOLD_REG_FIRMWARE_VERSION 0x0300U /* 协议板固件版本,只读 */
|
||||
#define CHRG_FIRMWARE_VERSION 0x0135U /* 当前协议板版本:V1.1.37 */
|
||||
#define CHRG_FIRMWARE_VERSION 0x0137U /* 当前协议板版本:V1.1.39 */
|
||||
|
||||
|
||||
rt_uint8_t ucDiscInBuf[(TOTAL_DISC_REGS+BITS_UCHAR-1)/BITS_UCHAR];
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
文件名称 : chrg_north.c
|
||||
完成日期 :
|
||||
当前版本号 : V1.1.24
|
||||
当前版本号 : V1.1.39
|
||||
主要功能 : 实现北向通信交互,包括接收命令解析,发送请求命令,切换通路。以独立线程处理。
|
||||
版本历史 : 创建原始版本
|
||||
说明 :
|
||||
@@ -332,6 +332,50 @@ int chrg_north_switch (eIDX_NOR_CH ch)
|
||||
chrgios[IDX_GPO_NOR_SW_B].base, (eIDX_CH)ch);
|
||||
}
|
||||
|
||||
#define NORTH_SWITCH_SETTLE_MS 2U
|
||||
#define NORTH_RX_RESET_TIMEOUT_MS 30U
|
||||
|
||||
/*
|
||||
* 开始一次北向请求:切换物理通道后先让接收线程清理旧事务,
|
||||
* 再为当前通道生成新的内部事务号,避免旧通道尾帧被误配给新请求。
|
||||
* 事务号和清理序号都不进入北向协议帧,只用于协议板内部并发隔离。
|
||||
*/
|
||||
rt_uint32_t chrg_north_request_begin(eIDX_NOR_CH ch)
|
||||
{
|
||||
struct chrg_north_t *pNOR = &chrgnorth;
|
||||
rt_uint32_t reset_request;
|
||||
rt_tick_t start_tick;
|
||||
|
||||
if ((ch >= TOTAL_NOR_CHS) || (pNOR->tty == RT_NULL)) {
|
||||
return 0U;
|
||||
}
|
||||
pNOR->idx = ch;
|
||||
/* MUX切换稳定后再请求接收线程清理,避免旧通道尾包残留到新请求。 */
|
||||
rt_thread_mdelay(NORTH_SWITCH_SETTLE_MS);
|
||||
pNOR->transaction_id[ch]++;
|
||||
if (pNOR->transaction_id[ch] == 0U) {
|
||||
pNOR->transaction_id[ch] = 1U;
|
||||
}
|
||||
/* 请求号采用非零序号;回绕时跳过0,便于区分未初始化状态。 */
|
||||
reset_request = pNOR->rx_reset_request + 1U;
|
||||
if (reset_request == 0U) {
|
||||
reset_request = 1U;
|
||||
}
|
||||
pNOR->rx_reset_request = reset_request;
|
||||
/* 接收线程观察到请求号变化后执行实际清理,发送线程只负责发起请求。 */
|
||||
(void)chrg_tty_break_recv(pNOR->tty);
|
||||
start_tick = rt_tick_get();
|
||||
while (pNOR->rx_reset_ack != reset_request) {
|
||||
if ((rt_tick_t)(rt_tick_get() - start_tick) >=
|
||||
rt_tick_from_millisecond(NORTH_RX_RESET_TIMEOUT_MS)) {
|
||||
LOG_W("north rx reset timeout: ch=%d", ch + 1U);
|
||||
return 0U;
|
||||
}
|
||||
rt_thread_mdelay(1);
|
||||
}
|
||||
return pNOR->transaction_id[ch];
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
函数名称: chrg_north_send
|
||||
函数描述: 北向通道数据发送
|
||||
@@ -670,6 +714,8 @@ static int chrg_north_pickup_frame (struct rt_ringbuffer *rb,
|
||||
}
|
||||
pFrame->pMB->length = pFrame->len_msg;
|
||||
pFrame->pMB->payload = RT_NULL;
|
||||
pFrame->pMB->channel = (rt_uint8_t)idx;
|
||||
pFrame->pMB->transaction_id = chrgnorth.transaction_id[idx];
|
||||
pFrame->pMB->payload = (rt_uint8_t *)rt_malloc(pFrame->len_msg);
|
||||
if (RT_NULL == pFrame->pMB->payload) {
|
||||
LOG_E("malloc buf failed.");
|
||||
@@ -711,6 +757,28 @@ static int chrg_north_pickup_frame (struct rt_ringbuffer *rb,
|
||||
return cnt;
|
||||
}
|
||||
|
||||
static void chrg_north_rx_reset_request_process(struct chrg_north_t *pNOR,
|
||||
struct chrg_tty_t *pTTY, eIDX_NOR_CH *pFrameIdx)
|
||||
{
|
||||
rt_uint8_t discard[32];
|
||||
|
||||
if ((pNOR == RT_NULL) || (pTTY == RT_NULL) ||
|
||||
(pFrameIdx == RT_NULL) ||
|
||||
(pNOR->rx_reset_ack == pNOR->rx_reset_request)) {
|
||||
return;
|
||||
}
|
||||
/*
|
||||
* 只由北向接收线程执行:在同一上下文中复位环形缓冲区、未完成帧,
|
||||
* 并排空UART驱动FIFO,避免与接收操作并发访问这些状态。
|
||||
*/
|
||||
rt_ringbuffer_reset(pNOR->rb);
|
||||
chrg_north_frame_reset(&pNOR->frame, RT_TRUE);
|
||||
while (rt_device_read(pTTY->dev, 0, discard, sizeof(discard)) > 0) {
|
||||
}
|
||||
*pFrameIdx = pNOR->idx;
|
||||
pNOR->rx_reset_ack = pNOR->rx_reset_request;
|
||||
}
|
||||
|
||||
/*****************************************************************
|
||||
函数名称: chrg_north_thread_entry
|
||||
函数描述: 北向通道数据接收线程体
|
||||
@@ -756,6 +824,10 @@ void chrg_north_thread_entry (void *data)
|
||||
}
|
||||
|
||||
while (1) {
|
||||
if (pNOR->rx_reset_ack != pNOR->rx_reset_request) {
|
||||
chrg_north_rx_reset_request_process(pNOR, pTTY, &frame_idx);
|
||||
continue;
|
||||
}
|
||||
if (pNOR->frame.findhead &&
|
||||
((rt_tick_t)(rt_tick_get() - pNOR->frame.last_tick) >=
|
||||
rt_tick_from_millisecond(NORTH_FRAME_TIMEOUT_MS))) {
|
||||
@@ -765,6 +837,11 @@ void chrg_north_thread_entry (void *data)
|
||||
|
||||
rt_memset(pNOR->rx_buf, 0, sizeof(pNOR->rx_buf));
|
||||
len = chrg_tty_recv(pTTY, pNOR->rx_buf, sizeof(pNOR->rx_buf));
|
||||
if (pNOR->rx_reset_ack != pNOR->rx_reset_request) {
|
||||
/* 切换请求到达期间读出的字节属于旧事务,整批丢弃。 */
|
||||
chrg_north_rx_reset_request_process(pNOR, pTTY, &frame_idx);
|
||||
continue;
|
||||
}
|
||||
if (len <= 0) {
|
||||
rt_thread_mdelay(1);
|
||||
continue;
|
||||
@@ -779,6 +856,7 @@ void chrg_north_thread_entry (void *data)
|
||||
rt_ringbuffer_reset(pNOR->rb);
|
||||
chrg_north_frame_reset(&pNOR->frame, RT_TRUE);
|
||||
frame_idx = pNOR->idx;
|
||||
continue;
|
||||
}
|
||||
|
||||
put_len = rt_ringbuffer_put(pNOR->rb, pNOR->rx_buf, len);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
文件名称 : chrg_north.h
|
||||
完成日期 :
|
||||
当前版本号 : V1.1.24
|
||||
当前版本号 : V1.1.39
|
||||
主要功能 : 实现北向通信交互,包括接收命令解析,发送请求命令,切换通路。以独立线程处理。
|
||||
版本历史 : 创建原始版本
|
||||
说明 :
|
||||
@@ -133,6 +133,11 @@ struct chrg_north_t {
|
||||
struct rt_ringbuffer *rb;
|
||||
struct pickup_info_t frame;
|
||||
rt_uint32_t config_version[TOTAL_NOR_CHS]; // COM配置版本,用于识别等待期间到达的旧回码
|
||||
/* 每个物理通道独立递增;仅在协议板内部标识一次北向请求。 */
|
||||
volatile rt_uint32_t transaction_id[TOTAL_NOR_CHS];
|
||||
/* 通道切换时由发送线程递增请求号,接收线程完成清理后写回确认号。 */
|
||||
volatile rt_uint32_t rx_reset_request;
|
||||
volatile rt_uint32_t rx_reset_ack;
|
||||
struct chrg_trim trim[TOTAL_NOR_CHS];
|
||||
rt_uint8_t rx_buf[SIZE_BUF_TTY];
|
||||
rt_uint8_t free_flag;
|
||||
@@ -178,6 +183,9 @@ extern void chrg_sink_parse(rt_uint8_t *frame_data, eIDX_SOU_CH idx,
|
||||
*****************************************************************/
|
||||
extern int chrg_north_switch (eIDX_NOR_CH ch);
|
||||
|
||||
/* 物理通道切换后清理旧接收数据并建立新事务,返回0表示准备失败。 */
|
||||
extern rt_uint32_t chrg_north_request_begin(eIDX_NOR_CH ch);
|
||||
|
||||
/*****************************************************************
|
||||
函数名称: chrg_north_send
|
||||
函数描述: 北向通道数据发送
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
文件名称 : chrg_roll_nor.c
|
||||
完成日期 :
|
||||
当前版本号 : V1.1.35
|
||||
当前版本号 : V1.1.39
|
||||
主要功能 : 实现北向通信轮巡请求命令。以独立线程处理。
|
||||
版本历史 : 创建原始版本
|
||||
说明 :
|
||||
@@ -27,7 +27,8 @@
|
||||
#define LOG_TAG "chrg.rollnor"
|
||||
#include <rtdbg.h>
|
||||
|
||||
#define NORTH_RESPONSE_TIMEOUT_MS 50U // 单次北向请求等待匹配回码的总时间
|
||||
#define NORTH_RESPONSE_TIMEOUT_MEASURE_MS 80U // 0x20量测回码等待时间,避免单通道阻塞过久
|
||||
#define NORTH_RESPONSE_TIMEOUT_CONFIG_MS 150U // 协议/CC配置回码等待时间,给功率板生效留出时间
|
||||
#define NORTH_REQUEST_MAX_ATTEMPTS 1U // 每次轮到通道只发送并等待一次,超时后留给下一轮
|
||||
#define NORTH_POLL_INTERVAL_MS 10U // 一个通道事务结束后到下一通道的轮询间隔
|
||||
#define SOURCE_PROTOCOL_WAIT_MS 100U // 每通道0xD0应答后的协议生效保护时间
|
||||
@@ -593,15 +594,17 @@ static rt_uint8_t chrg_roll_nor_expected_cmd(const struct chrg_switch_t *pSW)
|
||||
|
||||
/*****************************************************************
|
||||
函数名称: chrg_roll_nor_response_matches
|
||||
函数描述: 校验北向回码与当前请求的通道角色、命令和固定长度是否匹配
|
||||
函数描述: 校验北向回码与当前请求的通道、事务号、角色、命令和固定长度是否匹配
|
||||
输入参数: pMB:已通过north.c结构及CRC校验的回码 expected_idx:请求通道
|
||||
expected_id:请求角色 expected_cmd:请求命令
|
||||
expected_id:请求角色 expected_cmd:请求命令 transaction_id:请求内部事务号
|
||||
输出参数: -
|
||||
返回说明: 0:回码匹配 <0:参数错误或回码与请求不匹配
|
||||
其它说明: 本函数只判断请求归属,业务解析在配置版本复核通过后执行
|
||||
其它说明: 通道和事务号只用于协议板内部,不改变北向协议帧;本函数只判断请求归属,
|
||||
业务解析在配置版本复核通过后执行
|
||||
*****************************************************************/
|
||||
static int chrg_roll_nor_response_matches(const struct mb_msg_t *pMB,
|
||||
rt_uint8_t expected_idx, eIDX_ID expected_id, rt_uint8_t expected_cmd)
|
||||
rt_uint8_t expected_idx, eIDX_ID expected_id, rt_uint8_t expected_cmd,
|
||||
rt_uint32_t transaction_id)
|
||||
{
|
||||
if ((pMB == RT_NULL) || (pMB->payload == RT_NULL) ||
|
||||
(pMB->length < MIN_FRAME_LENGTH) ||
|
||||
@@ -616,6 +619,10 @@ static int chrg_roll_nor_response_matches(const struct mb_msg_t *pMB,
|
||||
pMB->payload[3], expected_cmd);
|
||||
return -2;
|
||||
}
|
||||
if ((pMB->channel != expected_idx) ||
|
||||
(pMB->transaction_id != transaction_id)) {
|
||||
return -5;
|
||||
}
|
||||
|
||||
if ((expected_cmd == SRC_CMD_GET_VOLTAGE) && (pMB->length != 21U)) {
|
||||
return -3;
|
||||
@@ -679,7 +686,7 @@ static void chrg_roll_nor_drain_mailbox(struct chrg_thread_t *pTN)
|
||||
*****************************************************************/
|
||||
static int chrg_roll_nor_wait_response(struct chrg_thread_t *pTN,
|
||||
rt_uint8_t expected_idx, eIDX_ID expected_id, rt_uint8_t expected_cmd,
|
||||
struct mb_msg_t **matched_response)
|
||||
rt_uint32_t transaction_id, struct mb_msg_t **matched_response)
|
||||
{
|
||||
rt_tick_t start_tick;
|
||||
rt_tick_t timeout_tick;
|
||||
@@ -694,7 +701,9 @@ static int chrg_roll_nor_wait_response(struct chrg_thread_t *pTN,
|
||||
}
|
||||
*matched_response = RT_NULL;
|
||||
start_tick = rt_tick_get();
|
||||
timeout_tick = rt_tick_from_millisecond(NORTH_RESPONSE_TIMEOUT_MS);
|
||||
timeout_tick = rt_tick_from_millisecond(
|
||||
(expected_cmd == SRC_CMD_GET_VOLTAGE) ?
|
||||
NORTH_RESPONSE_TIMEOUT_MEASURE_MS : NORTH_RESPONSE_TIMEOUT_CONFIG_MS);
|
||||
|
||||
while (1) {
|
||||
elapsed = (rt_tick_t)(rt_tick_get() - start_tick);
|
||||
@@ -711,7 +720,7 @@ static int chrg_roll_nor_wait_response(struct chrg_thread_t *pTN,
|
||||
}
|
||||
|
||||
if (chrg_roll_nor_response_matches(pMB, expected_idx,
|
||||
expected_id, expected_cmd) == 0) {
|
||||
expected_id, expected_cmd, transaction_id) == 0) {
|
||||
*matched_response = pMB;
|
||||
return 0;
|
||||
}
|
||||
@@ -841,6 +850,7 @@ static int chrg_roll_nor_auto_request(rt_uint8_t idx,
|
||||
rt_uint8_t expected_cmd = 0; // 本次请求期望收到的命令码
|
||||
rt_uint8_t max_attempts; // 本次请求允许的总发送次数
|
||||
eIDX_ID expected_id = ID_SINK; // 本次请求发送时的角色快照
|
||||
rt_uint32_t transaction_id = 0; // 本次请求内部事务号
|
||||
rt_uint32_t request_version = 0; // 本次请求发送时的COM配置版本
|
||||
int send_ret; // 当前命令的串口发送结果
|
||||
int wait_ret; // 当前命令的回码等待结果
|
||||
@@ -862,6 +872,7 @@ static int chrg_roll_nor_auto_request(rt_uint8_t idx,
|
||||
expected_id = pSW->id;
|
||||
expected_cmd = chrg_roll_nor_expected_cmd(pSW);
|
||||
request_version = pNOR->config_version[idx];
|
||||
transaction_id = pNOR->transaction_id[idx];
|
||||
/* 新事务发送前清理已结束或已超时事务遗留的邮箱回码。 */
|
||||
chrg_roll_nor_drain_mailbox(pTN);
|
||||
}
|
||||
@@ -875,7 +886,7 @@ static int chrg_roll_nor_auto_request(rt_uint8_t idx,
|
||||
|
||||
if (send_ret > 0) {
|
||||
wait_ret = chrg_roll_nor_wait_response(pTN, idx,
|
||||
expected_id, expected_cmd, &pMB);
|
||||
expected_id, expected_cmd, transaction_id, &pMB);
|
||||
if ((wait_ret == 0) && (pMB != RT_NULL)) {
|
||||
rt_mutex_take(pTHR->mutex, RT_WAITING_FOREVER);
|
||||
if ((pNOR->config_version[idx] != request_version) ||
|
||||
@@ -1047,8 +1058,12 @@ void chrg_roll_nor_thread_entry (void *data)
|
||||
continue;
|
||||
} else {
|
||||
chrg_led_flashing(IDX_LED2, TIMES_ONE, PULSE_TIME*2, PULSE_TIME*2);
|
||||
chrg_north_switch((eIDX_NOR_CH)idx);
|
||||
chrgnorth.idx = idx;
|
||||
if ((chrg_north_switch((eIDX_NOR_CH)idx) != 0) ||
|
||||
(chrg_north_request_begin((eIDX_NOR_CH)idx) == 0U)) {
|
||||
CHRG_CHANNEL_NEXT(index);
|
||||
rt_thread_mdelay(NORTH_POLL_INTERVAL_MS);
|
||||
continue;
|
||||
}
|
||||
pROLL->idx = idx;
|
||||
(void)chrg_roll_nor_auto_request(idx, pSW,
|
||||
pTHR, pTN, pNOR);
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
/****************************************************************************
|
||||
文件名称 : chrg_def.h
|
||||
完成日期 :
|
||||
当前版本号 : V1.1.37
|
||||
当前版本号 : V1.1.39
|
||||
主要功能 : 全局宏定义,常量,及结构体。
|
||||
版本历史 : 创建原始版本
|
||||
说明 :
|
||||
@@ -15,8 +15,8 @@
|
||||
/* CHRG application version. Keep this value synchronized with the changelog. */
|
||||
#define CHRG_VERSION_MAJOR 1U
|
||||
#define CHRG_VERSION_MINOR 1U
|
||||
#define CHRG_VERSION_PATCH 37U
|
||||
#define CHRG_VERSION_STRING "V1.1.37"
|
||||
#define CHRG_VERSION_PATCH 39U
|
||||
#define CHRG_VERSION_STRING "V1.1.39"
|
||||
|
||||
/*
|
||||
* Channel polling mode:
|
||||
@@ -114,6 +114,8 @@ struct vc_pd_t {
|
||||
struct mb_msg_t {
|
||||
rt_uint16_t length; // 数据长度
|
||||
rt_uint8_t *payload; // 数据区
|
||||
rt_uint8_t channel; // 接收物理通道(北向消息使用),用于匹配当前轮询通道
|
||||
rt_uint32_t transaction_id; // 北向请求内部事务号,不进入通信协议帧,仅用于丢弃旧回码
|
||||
};
|
||||
|
||||
struct pickup_info_t {
|
||||
|
||||
Reference in New Issue
Block a user