⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drv_xmodem.c

📁 vxWorks上的modem实现
💻 C
📖 第 1 页 / 共 2 页
字号:
                              || XModemPacket.ucSeqNo == ( ucLastSeqNo + 1 ) )
            {
                g_DecTimer = 0 ;
                iRepeatTime = 0 ;
                iCurrentData = 0 ;
                iDownLoadState = DATA ;

                return SUCCESS;
            }
            else
            {
                Load_xModemPurge (  ) ;

                data = CAN ;
                ret  = Drv_DbgSerialOutChar(data);

                data = CAN ;
                ret  = Drv_DbgSerialOutChar(data);

                iDownLoadState = END ;

                return FAILURE;
            }
        }
        else
        {
            Load_xModemPurge (  ) ;

            data = NAK ;
            ret  = Drv_DbgSerialOutChar(data);

            g_DecTimer = 0 ;
            iRepeatTime = 0 ;

            iDownLoadState = START_OF_HEADER ;

            return FAILURE ;
        }
    }

    /*====================================================================*/
    /*    if timout, 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    : Data                                             */
/*    Function Func.   : The Function for the state DATA                  */
/*    Input Parameter  : none                                             */
/*    Output Parameter : none                                             */
/*    Return           : int                                              */
/*                       0 : success else failure                         */
/*    Call Functions   :                                                  */
/*                       1. InChar  (  )                                  */
/*                       2. OutChar (  )                                  */
/*    Called By        :                                                  */
/*                       1. xModemDownLoad (  )                           */
/*                                                                        */
/*========================================================================*/
_U32 Load_xModemData ( void )
{
    _U8 data ;
    _U32 ret ;

    /*====================================================================*/
    /*    if firstly go to the state DATA, then set the timeout timer     */
    /*====================================================================*/
    if ( !iRepeatTime )
    {
        g_DecTimer = WAITFORDATA ;
        iRepeatTime ++ ;

        return SUCCESS ;
    }

    /*====================================================================*/
    /*    if can receive data, then record the data                       */
    /*    if received data's no is MAXDATANO, then receive the CRC        */
    /*    else continue receive data                                      */
    /*====================================================================*/
    ret = Drv_DbgSerialInChar(&data);
    if ( !ret )
    {
        XModemPacket.ucData[ iCurrentData ++ ] = data ;

        g_DecTimer = 0 ;
        iRepeatTime = 0 ;

        if ( iCurrentData == MAXDATANO )
        {
            iCurrentCRC = 0;
            iDownLoadState = C_R_C ;
        }

        return SUCCESS ;
    }

    /*====================================================================*/
    /*    if timeout, then retry,                                         */
    /*    if retry time is 10, then send C , search the SOH again         */
    /*====================================================================*/
    if ( !g_DecTimer )
    {
        if ( iRepeatTime < MAXREPEATTIME )
        {
            g_DecTimer = WAITFORDATA ;
            iRepeatTime ++ ;

            return SUCCESS ;
        }
        else
        {
            g_DecTimer = 0 ;
            iRepeatTime = 0 ;

            data = NAK ;
            ret  = Drv_DbgSerialOutChar(data);

            iDownLoadState = START_OF_HEADER ;

            return FAILURE;
        }
    }

    return SUCCESS ;
}

/*========================================================================*/
/*                                                                        */
/*    Function Name    : CRC                                              */
/*    Function Func.   : The Function for the state C_R_C                 */
/*    Input Parameter  : none                                             */
/*    Output Parameter : none                                             */
/*    Return           : int                                              */
/*                       0 : success else failure                         */
/*    Call Functions   :                                                  */
/*                       1. InChar  (  )                                  */
/*                       2. OutChar (  )                                  */
/*                       3. CalCRC   (  )                                 */
/*    Called By        :                                                  */
/*                       1. xModemDownLoad (  )                           */
/*                                                                        */
/*========================================================================*/
_U32 Load_xModemCRC ( void )
{
    _U8  data ;
    _U16 uCRC ;
    _U32  ret ;

    /*====================================================================*/
    /*    if firstly go to the state C_R_C, then set the timeout timer    */
    /*====================================================================*/
    if ( !iRepeatTime )
    {
        g_DecTimer = WAITFORDATA ;
        iRepeatTime ++ ;

        return SUCCESS ;
    }

    /*====================================================================*/
    /*    if can receive two data, then record and cmpare with CRC        */
    /*    calculated , if equal , then transfer ok, else have error,      */
    /*    send C,  search the SOH again                                   */
    /*====================================================================*/
    ret = Drv_DbgSerialInChar(&data);
    if ( !ret )
    {
        #if BIG_ENDIAN
        XModemPacket.ucCRC[ iCurrentCRC++ ] = data ;
        #else
        int iCRC ;

        iCRC = MAXCRCNO - iCurrentCRC - 1 ;
        iCurrentCRC ++ ;
        XModemPacket.ucCRC[ iCRC ] = data ;
        #endif

        g_DecTimer = 0 ;
        iRepeatTime = 0 ;

        if ( iCurrentCRC == MAXCRCNO )
        {
            memcpy ( ( _U8 * )&uCRC, ( _U8 * ) XModemPacket.ucCRC, MAXCRCNO ) ;

            if ( uCRC != CalcCrc ( XModemPacket.ucData, MAXDATANO ) )
            {
                data = NAK ;
                ret  = Drv_DbgSerialOutChar(data);
            }
            else
            {
                if( ulLength >= PROGRAM_MAX_LENGTH - MAXDATANO )
                {
                    iDownLoadState = END ;
                    ulLength = PROGRAM_MAX_LENGTH + 128 ;

                    /*返回正确实际上是为了在外部函数对长度进行判断之后提示长度溢出*/
                    return OK ;
                }
                memcpy ( szDram, ( _U8 * ) XModemPacket.ucData, MAXDATANO ) ;
                szDram += MAXDATANO ;
                ulLength += MAXDATANO ;

                data = ACK ;
                ret  = Drv_DbgSerialOutChar(data);

                ucLastSeqNo = XModemPacket.ucSeqNo ;

            }

            iDownLoadState = START_OF_HEADER ;
        }

        return SUCCESS ;
    }

    /*====================================================================*/
    /*    if timeout, then retry,                                         */
    /*    if retry time is 10, then send the C, search the SOH again      */
    /*====================================================================*/
    if ( !g_DecTimer )
    {
        if ( iRepeatTime < MAXREPEATTIME )
        {
            g_DecTimer = WAITFORDATA ;
            iRepeatTime ++ ;

            return SUCCESS ;
        }
        else
        {
            g_DecTimer = 0 ;
            iRepeatTime = 0 ;

            data = NAK ;
            ret  = Drv_DbgSerialOutChar(data);

            iDownLoadState = START_OF_HEADER ;

            return FAILURE;
        }
    }

    return SUCCESS ;
}

/*========================================================================*/
/*                                                                        */
/*    Function Name    : End                                              */
/*    Function Func.   : The Function for the state END                   */
/*    Input Parameter  : none                                             */
/*    Output Parameter : none                                             */
/*    Return           : int                                              */
/*                       0 : success else failure                         */
/*    Call Functions   : none                                             */
/*    Called By        :                                                  */
/*                       1. xModemDownLoad (  )                           */
/*                                                                        */
/*========================================================================*/
_U32 Load_xModemEnd ( void )
{
    return SUCCESS ;
}

/*========================================================================*/
/*                                                                        */
/*    Function Name    : Purge                                            */
/*    Function Func.   : Clear the COM port by receive data until no data */
/*    Input Parameter  : none                                             */
/*    Output Parameter : none                                             */
/*    Return           : void                                             */
/*    Call Functions   :                                                  */
/*                       1. InChar (  )                                   */
/*    Called By        :                                                  */
/*                       1. CompOfSeqNo (  )                              */
/*                                                                        */
/*========================================================================*/
void Load_xModemPurge ( void )
{
    _U8 data ;
    _U32 ret ;

    do
    {
        ret = Drv_DbgSerialInChar(&data);
    }
    while ( ret  ) ;

    return ;
}

#endif

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -