📄 drv_xmodem.c
字号:
#ifndef __XMODEM_C__
#define __XMODEM_C__
#include "vxWorks.h"
#include "config.h" /* board support configuration header */
#include "Typedef.h"
#include "../Bsp.h"
#include "Drv_Crc.h"
#include "Drv_Xmodem.h"
//在主机中加载跑不起来可能是因为命令行把字符收过去了。可以测试一下
_U32 Load_xModemDownLoad ( _U32 startaddr,_U32 *pdwLength)
{
_U32 iReturn;
/*====================================================================*/
/* Initialize the related variables */
/*====================================================================*/
g_DecTimer = 0 ;
iRepeatTime = 0 ;
ucLastSeqNo = 0 ;
iDownLoadState = START_OF_HEADER ;
szDram = (_U8 *)startaddr;
ulLength = 0 ;
gucSOHReceived = 0 ;
iReturn = 0 ;
InitCrcTable();
Drv_Print ( "\n\r Please run command \"send file\" of HyperTerminal..." ) ;
/*====================================================================*/
/* FSM execute looply until the state is END */
/*====================================================================*/
while ( (iDownLoadState != END) && (!iReturn) )
{
iReturn = DownLoadFSM[ iDownLoadState ] ( ) ;
if ( iDownLoadState == END )
{
g_DecTimer = 0 ;
iRepeatTime = 0 ;
ucLastSeqNo = 0 ;
}
}
if (ulLength == 0)
{
return FAILURE;
}
*pdwLength = ulLength;
return iReturn;
}
/*========================================================================*/
/* */
/* Function Name : StartOfHeader */
/* Function Func. : The Function for the state START_OF_HEADER */
/* Input Parameter : none */
/* Output Parameter : none */
/* Return : int */
/* 0 : success else failure */
/* Call Functions : */
/* 1. InChar ( ) */
/* 2. OutChar ( ) */
/* Called By : */
/* 1. xModemDownLoad ( ) */
/* */
/*========================================================================*/
_U32 Load_xModemStartOfHeader ( void )
{
_U8 data ;
_U32 ret ;
int i;
if ( !iRepeatTime )
{
/*================================================================*/
/* if firstly go the state START_OF_HEADER, */
/* then send NAK to tell the sender that the receiver have */
/* been ready, and set the timeout timer */
/*================================================================*/
data = OUTCC ;
ret = Drv_DbgSerialOutChar(data);
g_DecTimer = WAITFORSOH ;
iRepeatTime ++ ;
return SUCCESS ;
}
/*====================================================================*/
/* receive the data, if the data is : */
/* SOH ---- indicate the start of Xmodem packet */
/* EOT ---- indicate the end of transfer */
/* CAN ---- transmiter end the transmition */
/* other ---- discard */
/*====================================================================*/
ret = Drv_DbgSerialInChar(&data);
if ( !ret )
{
switch ( data )
{
case SOH :
gucSOHReceived = 1 ;
g_DecTimer = 0 ;
iRepeatTime = 0 ;
iDownLoadState = SEQUENCE_NO ;
return SUCCESS ;
case EOT :
data = ACK ;
ret = Drv_DbgSerialOutChar(data);
g_DecTimer = 0 ;
iRepeatTime = 0 ;
iDownLoadState = END ;
return SUCCESS ;
case CAN :
g_DecTimer = 0 ;
iRepeatTime = 0 ;
iDownLoadState = END ;
return FAILURE;
case 0x03 : /* 'Ctrl+c' exit to reboot TA */
g_DecTimer = 0 ;
iRepeatTime = 0 ;
iDownLoadState = END ;
Drv_Print ( "\n\r Download stoped! now rebooting system...");
for(i=0;i<5000;i++)
{
}
return FAILURE;
default :
break ;
}
}
/*====================================================================*/
/* If timeout , then retry , if retry time is 10, then error */
/*====================================================================*/
if ( !g_DecTimer )
{
if ( (iRepeatTime < MAXREPEATTIME_FOR_FIRST_FRAME)&&(gucSOHReceived == 0) )
{
data = OUTCC ;
ret = Drv_DbgSerialOutChar(data);
iRepeatTime ++ ;
g_DecTimer = WAITFORSOH ;
return SUCCESS ;
}
else
if( (iRepeatTime < MAXREPEATTIME_FOR_FIRST_FRAME)&&(gucSOHReceived == 1) )
{
if( !(iRepeatTime%5) )
{
data = NAK ;
ret = Drv_DbgSerialOutChar(data);
g_DecTimer = WAITFORSOH ;
}
iRepeatTime ++ ;
return SUCCESS ;
}
else
{
g_DecTimer = 0 ;
iRepeatTime = 0 ;
iDownLoadState = END ;
return FAILURE;
}
}
return SUCCESS ;
}
/*========================================================================*/
/* */
/* Function Name : SequenceNo */
/* Function Func. : The Function for the state SEQUNCE_NO */
/* Created By : YanJunQing */
/* Creation Date : 1997, 1 */
/* Input Parameter : none */
/* Output Parameter : none */
/* Return : int */
/* 0 : success else failure */
/* Call Functions : */
/* 1. InChar ( ) */
/* Called By : */
/* 1. xModemDownLoad ( ) */
/* */
/*========================================================================*/
_U32 Load_xModemSequenceNo ( void )
{
_U8 data ;
_U32 ret ;
/*====================================================================*/
/* if firstly go to the state SEQUNCE_NO, then set the timout timer */
/*====================================================================*/
if ( !iRepeatTime )
{
g_DecTimer = WAITFORDATA ;
iRepeatTime ++ ;
return SUCCESS ;
}
/*====================================================================*/
/* if can receive the data , then go to the state COMP_OF_SEQ_NO */
/*====================================================================*/
ret = Drv_DbgSerialInChar(&data);
if ( !ret )
{
XModemPacket.ucSeqNo = data ;
g_DecTimer = 0 ;
iRepeatTime = 0 ;
iDownLoadState = COMP_OF_SEQ_NO ;
return SUCCESS ;
}
/*====================================================================*/
/* if timeout, then retry, */
/* if retry time is 10, then search the SOH again */
/*====================================================================*/
if ( !g_DecTimer )
{
if ( iRepeatTime < MAXREPEATTIME )
{
g_DecTimer = WAITFORDATA ;
iRepeatTime ++ ;
return SUCCESS ;
}
else
{
data = NAK ;
ret = Drv_DbgSerialOutChar(data);
g_DecTimer = 0 ;
iRepeatTime = 0 ;
iDownLoadState = START_OF_HEADER ;
return FAILURE;
}
}
return SUCCESS;
}
/*========================================================================*/
/* */
/* Function Name : CompOfSeqNo */
/* Function Func. : The Function for the state COMP_OF_SEQ_NO */
/* Input Parameter : none */
/* Output Parameter : none */
/* Return : int */
/* 0 : success else failure */
/* Call Functions : */
/* 1. InChar ( ) */
/* 2. OutChar ( ) */
/* 3. Purge ( ) */
/* Called By : */
/* 1. xModemDownLoad ( ) */
/* */
/*========================================================================*/
_U32 Load_xModemCompOfSeqNo ( void )
{
_U8 data ;
_U32 ret ;
/*====================================================================*/
/* if firstly go to the state COMP_OF_SEQ_NO , */
/* then set the timeout timer */
/*====================================================================*/
if ( !iRepeatTime )
{
g_DecTimer = WAITFORDATA ;
iRepeatTime ++ ;
return SUCCESS ;
}
/*====================================================================*/
/* if receive the correct compensate of sequence no , */
/* then if the sequence no is : */
/* the last sequence no : send ACK , search the SOH again */
/* the expexted sequence no : begin to receive data */
/* other case : send CAN, end the transfer */
/* else clear the COM ports, search the SOH again */
/*====================================================================*/
ret = Drv_DbgSerialInChar(&data);
if ( !ret )
{
if ( CompensateTo255( data ) == XModemPacket.ucSeqNo )
{
if ( XModemPacket.ucSeqNo == ucLastSeqNo )
{
Load_xModemPurge ( ) ;
data = ACK ;
ret = Drv_DbgSerialOutChar(data);
g_DecTimer = 0 ;
iRepeatTime = 0 ;
iDownLoadState = START_OF_HEADER ;
return SUCCESS ;
}
else if ( ( XModemPacket.ucSeqNo == 0 && ucLastSeqNo == MAXSEQNO )
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -