更新部分变量
This commit is contained in:
parent
f0a7aa7916
commit
4dd350b388
52
tcl.c
52
tcl.c
@ -55,11 +55,10 @@ static devstatus_t curMode = R_MODE;
|
||||
static devstatus_t tarMode = R_MODE;
|
||||
volatile int isOK2Swt = 1; // 是否尝试切换到发送模式
|
||||
static devstatus_t tarDevMode = R_MODE; // 目标端工作模式
|
||||
static MultiTimer retryTimer, timeoutTimer; // 重试定时器、超时定时器
|
||||
static int rxNewDataFlag = 0;
|
||||
static MultiTimer swtModeTimer, retryTimer, timeoutTimer; // 重试定时器、超时定时器
|
||||
static int isNewLineRx = 0;
|
||||
static uint8_t lstLineRxData[MAX_FRAME_LENGTH]; // 透传缆最后一个接收到的数据
|
||||
static uint8_t lstLineRxDataLen = 0; // 透传缆最后一个接收到的数据长度
|
||||
static int isTimeOut = 0;
|
||||
static Node *userRxBuffList = NULL; // user端接收缓冲链表
|
||||
// static err_t lstState = ERR_OK;
|
||||
|
||||
@ -100,7 +99,7 @@ void lineTransmitData(uint8_t *data, uint8_t len)
|
||||
// 发送数据前后需初始化为发送模式
|
||||
initWorkStatus(T_MODE);
|
||||
// 发送数据
|
||||
|
||||
|
||||
// 发送完毕后需恢复为接收模式
|
||||
msdelay(len * 10);
|
||||
initWorkStatus(R_MODE);
|
||||
@ -111,8 +110,10 @@ void lineTransmitData(uint8_t *data, uint8_t len)
|
||||
// 2. 发送完成后切换为接收模式
|
||||
// 3. 通过透传缆串口接收回复的ACK
|
||||
// 4. ACK正常则发送成功,否则发送失败
|
||||
// 5. 发送成功则准备发送缓存队列中下一帧数据
|
||||
// 5. 发送成功反馈ACK,删除链表数据并准备发送链表中下一帧数据
|
||||
// 6. 发送失败则等待指定时间t后重发当前帧
|
||||
// 7. 重试次数达到上限则发送失败,删除链表数据并准备发送链表中下一帧数据
|
||||
// 8.
|
||||
|
||||
/// @brief 通过用户串口发送数据
|
||||
/// @param data 待发送数据
|
||||
@ -167,11 +168,11 @@ uint64_t getPlatformTicks(void)
|
||||
}
|
||||
|
||||
// Callback functions for the timers
|
||||
void retryTimerCallback(MultiTimer *timer, void *userData)
|
||||
void swtModeTimerCallback(MultiTimer *timer, void *userData)
|
||||
{
|
||||
isOK2Swt = 1;
|
||||
|
||||
printf("Timer 1 fired at %lu ms\n", getPlatformTicks());
|
||||
//提示保护期已过,收端可以发送数据
|
||||
printf("time of protect expired and can send data.\n");
|
||||
}
|
||||
|
||||
void timeOutTimerCallback(MultiTimer *timer, void *userData)
|
||||
@ -180,7 +181,12 @@ void timeOutTimerCallback(MultiTimer *timer, void *userData)
|
||||
|
||||
printf("Timer 1 fired at %lu ms\n", getPlatformTicks());
|
||||
}
|
||||
void reTryTimerCallback(MultiTimer *timer, void *userData)
|
||||
{
|
||||
userData = 1;
|
||||
|
||||
printf("Timer 1 fired at %lu ms\n", getPlatformTicks());
|
||||
}
|
||||
/// @brief 计算异或XOR校验
|
||||
/// @param data 待校验数据
|
||||
/// @param len 数据长度
|
||||
@ -256,7 +262,7 @@ void userRecDataCallback(uint8_t *data, uint8_t len)
|
||||
/// @param len
|
||||
void lineRecCallback(uint8_t *data, uint8_t len)
|
||||
{
|
||||
rxNewDataFlag = 1;
|
||||
isNewLineRx = 1;
|
||||
memcpy(lstLineRxData, data, len);
|
||||
lstLineRxDataLen = len;
|
||||
}
|
||||
@ -266,9 +272,9 @@ void lineRecCallback(uint8_t *data, uint8_t len)
|
||||
/// @return 返回ACK状态
|
||||
err_t getACK(int timeOut)
|
||||
{
|
||||
rxNewDataFlag = 0;
|
||||
uint8_t isTimeOut = 0;
|
||||
multiTimerStop(&timeoutTimer);
|
||||
multiTimerStart(&timeoutTimer, timeOut, timeOutTimerCallback, NULL); // Start timer
|
||||
multiTimerStart(&timeoutTimer, timeOut, timeOutTimerCallback, &isTimeOut); // Start timer
|
||||
while (1)
|
||||
{
|
||||
if (isTimeOut)
|
||||
@ -276,9 +282,10 @@ err_t getACK(int timeOut)
|
||||
return ERR_TIMEOUT;
|
||||
}
|
||||
|
||||
if (rxNewDataFlag)
|
||||
if (isNewLineRx)
|
||||
{
|
||||
rxNewDataFlag = 0;
|
||||
isNewLineRx = 0;
|
||||
//ACK为单个字节
|
||||
if (lstLineRxDataLen == 1 && lstLineRxData[0] == CMD_ACK_OK)
|
||||
{
|
||||
return ERR_OK;
|
||||
@ -319,25 +326,27 @@ void handleTR()
|
||||
multiTimerYield();
|
||||
|
||||
// 收到新数据
|
||||
if (rxNewDataFlag)
|
||||
if (isNewLineRx)
|
||||
{
|
||||
if (lstLineRxDataLen == 1)
|
||||
{
|
||||
/* code */
|
||||
/* 判断为无效,只有ACK为单个字节 */
|
||||
}
|
||||
else
|
||||
{
|
||||
if (chkDataValid(lstLineRxData, lstLineRxDataLen) == ERR_OK)
|
||||
{
|
||||
//校验通过
|
||||
lineTransmitData(CMD_ACK_OK, 1);
|
||||
userTransmitData(lstLineRxData, lstLineRxDataLen);
|
||||
//每次建立通信后10s内收端不能发起通信
|
||||
isOK2Swt = 0;
|
||||
multiTimerStop(&retryTimer);
|
||||
multiTimerStart(&retryTimer, 10*1000, retryTimerCallback, NULL); // Start timer
|
||||
multiTimerStart(&retryTimer, 10*1000, swtModeTimerCallback, NULL); // Start timer
|
||||
}
|
||||
else
|
||||
{
|
||||
//数据校验不过
|
||||
lineTransmitData(CMD_ACK_NOK, 1);
|
||||
}
|
||||
}
|
||||
@ -347,20 +356,21 @@ void handleTR()
|
||||
if (isOK2Swt && getListSize(userRxBuffList) > 0)
|
||||
{
|
||||
// 发送数据
|
||||
uint8_t reTryCnt = 0;
|
||||
err_t lstState = ERR_OK;
|
||||
while (userRxBuffList != NULL) // 遍历链表
|
||||
{
|
||||
static uint8_t isRetryTimeOut = 0;
|
||||
|
||||
if (lstState == ERR_NOK)
|
||||
{
|
||||
if (!isTimeOut)
|
||||
if (!isRetryTimeOut)
|
||||
{
|
||||
// 跳过后续代码进入下次循环
|
||||
continue;
|
||||
}
|
||||
else
|
||||
{
|
||||
isTimeOut = 0;
|
||||
isRetryTimeOut = 0;
|
||||
}
|
||||
}
|
||||
|
||||
@ -380,12 +390,12 @@ void handleTR()
|
||||
lstState = ERR_OK;
|
||||
userRxBuffList = userRxBuffList->next; // 正常则移动指针
|
||||
deleteFirstNode(&userRxBuffList);
|
||||
// userTransmitData(CMD_ACK_OK, 1);
|
||||
userTransmitData(ERR_OK, 1);
|
||||
}
|
||||
else
|
||||
{
|
||||
lstState = ERR_NOK;
|
||||
static uint8_t reTryCnt = 0;//当前重试次数
|
||||
reTryCnt++;
|
||||
if (reTryCnt > MAX_RETRY_CNT)
|
||||
{
|
||||
@ -399,7 +409,7 @@ void handleTR()
|
||||
}
|
||||
//失败的原因可能是数据碰撞,需要根据优先级等待不同的时间后进行重试
|
||||
multiTimerStop(&timeoutTimer);
|
||||
multiTimerStart(&timeoutTimer, time2Retry, timeOutTimerCallback, NULL); // Start timer
|
||||
multiTimerStart(&timeoutTimer, time2Retry, reTryTimerCallback, &isRetryTimeOut); // Start timer
|
||||
}
|
||||
}
|
||||
else // 无ACK
|
||||
|
Loading…
Reference in New Issue
Block a user