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

📄 linprot.c

📁 基于FREESCALE单片机9S08DZ60的LIN从节点的应用层源代码.LIN是通过UART串口来实现的.开发环境为CODEWARRIOR FOR HC08
💻 C
📖 第 1 页 / 共 2 页
字号:

            if (LIN_ChkSum != LIN_TmpSCIByte )
            { 
                /* bit error in checksum */
                LIN_Error();		            /* Tx error */
            }
            else
            {
                /* Message transfered succesfuly so update flags -- for Freescale API only */

#if !defined(LINAPI_1_0)
                /* NB: LIN_MSG_UPDATED set only with LIN_MSG_NOCHANGE */
                
                if ( ( LinMsgStatus[LIN_SaveIdx] & LIN_MSG_UPDATED ) == 0 )
                {
                    /* msg has not been changed since start of transmission */
                    LinMsgStatus[LIN_SaveIdx] = LIN_MSG_OK;
                }

                /* if msg has been changed since start of transmission then we
                   keep current state (LIN_MSG_UPDATED | LIN_MSG_NOCHANGE)      */

#endif /* !defined(LINAPI_1_0) */

                /* For LIN API: message state is already LIN_MSG_OK -- for any type of message */

                LIN_EndOfFrame();
            }
        }
    }

    /**********************************************************************/
    /***       MASTER & SLAVE :   LIN_FLAG_SLEEP                ***/
    /**********************************************************************/ 
    else if (LIN_StateFlags & LIN_FLAG_SLEEP)
    {
        if ((LIN_TmpSCIByte == WAKEUP_BYTE) || (LIN_TmpSCIByte == 0xC0) || (LIN_TmpSCIByte == 0x00))
        {
            LIN_SetIdleTimeout();                /* Set idle timeout again */

#if defined(SLAVE)
            LIN_StateFlags = LIN_FLAG_IGNORE;
#endif /* defined(SLAVE) */

#if defined(MASTER)
            LIN_SetTimeout(LIN_CfgConst.LIN_to_wakeup_delimiter);                    
            LIN_StateFlags = LIN_FLAG_WAKEUP_TIMEOUT;
#endif /* defined(MASTER) */
        }  
    }


#if defined(MASTER)
    /**********************************************************************/
    /***       MASTER : LIN_FLAG_IGNORE_DATA                            ***/
    /**********************************************************************/
    else if ( LIN_StateFlags & LIN_FLAG_IGNORE_DATA )
    {
        ++LIN_BufCounter;
        
        if (LIN_BufCounter > LIN_MsgLength)         /* NB: LIN_MsgLength = 1..8 */
        {   
            LIN_EndOfFrame();
        }
    }

    
    /**********************************************************************/
    /***       MASTER:   LIN_FLAG_SEND_HEADER                           ***/
    /**********************************************************************/
    else if (LIN_StateFlags & LIN_FLAG_SEND_HEADER)
    {
        if ( LIN_BufCounter == 0 )
        {   
            /* after SYNC byte sended */
        
            if ( LIN_TmpSCIByte == LIN_SYNC_FIELD )
            {
                ++LIN_BufCounter;
                /* send ID byte */
                LIN_SCDRPut(LIN_ProtBuf[LIN_BufCounter]);
            }
            else if ( LIN_TmpSCIByte != 0 )
            { 
                /* bit error in SYNC byte */
                LIN_Error();                /* Tx error */
            }
            /* If LIN_TmpSCIByte == 0 it may be Rx interrupt after break,
               so we must ignore it */
        }
        else
        {
            /* after ID byte sended */

            if ( LIN_TmpSCIByte != LIN_ProtBuf[LIN_BufCounter] )
            { 
                /* bit error in ID byte */
                LIN_Error();                /* Tx error */
            }
            else
            {   
                /* header has been transfered so process message ID */
    
                LIN_DBG_SET_PORT_6;

                /* LIN_SaveIdentifier was set in LIN_RequestMsg() */

                /* We must count bytes in IGNORE_DATA state also,
                   so we always need LIN_BufCounter */
                LIN_BufCounter  = 0;

                /* LIN_MsgLength is already set -- in LIN_RequestMsg */

                /* Check what we should do with this message */
                tmp = LinLookupTable[LIN_SaveIdentifier];
                /* Now tmp contains ID and msg flags */

                LIN_SaveIdx = tmp & LIN_MSG_INDEX_MASK;

                if ( !(tmp & LIN_MSG_NOT_IGNORED) )
                {
                    /* We did not find this id */
                    LIN_StateFlags = LIN_FLAG_IGNORE_DATA;
                }
                else if ( tmp & LIN_MSG_SEND ) /* if we probably send the message (SEND or SEND_UPDATED) */
                {   
                    /* check do we really should send data */

                    if ( (   ( LinMsgStatus[LIN_SaveIdx] & LIN_MSG_NODATA  ) != 0      ) ||
                         ( ( ( tmp & LIN_MSG_SEND_UPDATED                  ) != 0 ) &&
                           ( ( LinMsgStatus[LIN_SaveIdx] & LIN_MSG_UPDATED ) == 0 )    )   )
                    {
                        /* the data is not initialized  or
                           we should send only updated data but the data has not updated */
                        /* -> we ignore this message and don't change msg state */
                        /* NB: Tmaxframe (Rx) error should occure */

                        LIN_StateFlags = LIN_FLAG_IGNORE_DATA;
                    }
                    else
                    {   /* we should send data for this message */

                        /* new state of the msg */
                        if ( ( tmp & LIN_MSG_SEND_UPDATED ) != 0 )
                        {
                            /* if we are sending only updated data then we should reset
                               updated state of msg at all */
                            LinMsgStatus[LIN_SaveIdx] = LIN_MSG_OK;
                        }
                        else
                        {     
                            /* if we are sending msg always then we should clear updated flag,
                               so LIN_MSG_NOCHANGE flag can be keep */
                            LinMsgStatus[LIN_SaveIdx] &= ~(LINStatusType)LIN_MSG_UPDATED;
                        }
                        
                        LIN_CopyToBuf(LinMsgBuf[LIN_SaveIdx]);
                
                        LIN_SCDRPut(LIN_ProtBuf[0]);   /* Send first byte of buffer */

                        LIN_StateFlags  = LIN_FLAG_SEND_DATA;
                        LIN_ChkSum      = LIN_ProtBuf[0];
                    }
                }
                else    
                {   /* we receive data from this message */

                    LIN_StateFlags = LIN_FLAG_RECEIVE_DATA;        
                    LIN_ChkSum = 0;
                }

                LIN_DBG_CLR_PORT_6;
            }
        }
    }
#endif /* defined(MASTER) */

    
#if defined(SLAVE)
    /**********************************************************************/
    /***       SLAVE:   LIN_FLAG_RECEIVE_SYNC                           ***/
    /**********************************************************************/
    else if (LIN_StateFlags & LIN_FLAG_RECEIVE_SYNC)
    {
        if (LIN_TmpSCIByte != LIN_SYNC_FIELD )
        {
            LIN_Error();		/* Rx */
        }
        else
        {
            LIN_StateFlags = LIN_FLAG_RECEIVE_ID;
        }
    }


    /**********************************************************************/
    /***       SLAVE:   LIN_FLAG_RECEIVE_ID                             ***/
    /**********************************************************************/
    else if (LIN_StateFlags & LIN_FLAG_RECEIVE_ID)
    {
        /* header has been received so process message ID */

        LIN_DBG_SET_PORT_6;

        /* Cut the parity bits off and search message */
        tmp = LinLookupTable[LIN_TmpSCIByte & LIN_MSG_INDEX_MASK];
        /* Now tmp contains ID and msg flags */

        LIN_SaveIdx = tmp & LIN_MSG_INDEX_MASK;
        
        if ( ( !(tmp & LIN_MSG_NOT_IGNORED) ) || ( LinMsgId[LIN_SaveIdx] != LIN_TmpSCIByte ) )
        {
            /* We did not find this id or parity did not match. */
            /* NB: we don't store information about Parity Error ! */
            LIN_EndOfFrame();
        }
        else 
        {   
            LIN_BufCounter  = 0;
            LIN_MsgLength = LinMsgLen[LIN_SaveIdx];

            if ( tmp & LIN_MSG_SEND ) /* if we probably send the message (SEND or SEND_UPDATED) */
            {   
                /* check do we really should send data */

                if ( (   ( LinMsgStatus[LIN_SaveIdx] & LIN_MSG_NODATA  ) != 0      ) ||
                     ( ( ( tmp & LIN_MSG_SEND_UPDATED                  ) != 0 ) &&
                       ( ( LinMsgStatus[LIN_SaveIdx] & LIN_MSG_UPDATED ) == 0 )    )   )
                {
                    /* the data is not initialized  or
                       we should send only updated data but the data has not updated */
                    /* -> we ignore this message and don't change msg state */
                    /* NB: Tmaxframe (Rx) error should occure */

                    LIN_EndOfFrame();
                }
                else
                {   /* we should send data for this message */

                    /* set new state of the msg */

#if defined(LINAPI_1_0)

                    /* for LIN API: any transmitted message always go to state LIN_MSG_OK */
                    LinMsgStatus[LIN_SaveIdx] = LIN_MSG_OK;

#else   /* !defined(LINAPI_1_0) */

                    /* for Freescale API: LIN_SEND and LIN_SEND_UPDATED messages processed differently */

                    if ( ( tmp & LIN_MSG_SEND_UPDATED ) != 0 )
                    {
                        /* if we are sending only updated data then we should reset
                           updated state of msg at all */
                        LinMsgStatus[LIN_SaveIdx] = LIN_MSG_OK;
                    }
                    else
                    {     
                        /* if we are sending msg always then we should clear updated flag,
                           so LIN_MSG_NOCHANGE flag can be keep */
                        LinMsgStatus[LIN_SaveIdx] &= ~(LINStatusType)LIN_MSG_UPDATED;
                    }

#endif  /* !defined(LINAPI_1_0) */

                    LIN_CopyToBuf(LinMsgBuf[LIN_SaveIdx]);
        
                    LIN_SCDRPut(LIN_ProtBuf[0]);   /* Send first byte of buffer */

                    LIN_StateFlags  = LIN_FLAG_SEND_DATA;
                    LIN_ChkSum      = LIN_ProtBuf[0];
                }
            }

            else    
            {   /* we receive data from this message */

                LIN_StateFlags = LIN_FLAG_RECEIVE_DATA;        
                LIN_ChkSum = 0;
            }
        }

        LIN_DBG_CLR_PORT_6;
    }
#endif /* defined(SLAVE) */

    /* Ignored: */
    /**********************************************************************/
    /***       MASTER & SLAVE : LIN_FLAG_IGNORE,                        ***/
    /***                        LIN_FLAG_SLEEP, LIN_FLAG_WAKEUP_TIMEOUT ***/
    /**********************************************************************/
    /***       MASTER  :        LIN_FLAG_SEND_BREAK                     ***/
    /***                        Wait for timeout.                       ***/
    /**********************************************************************/
    /***       LIN_API :        LIN_FLAG_DISCONNECT, LIN_FLAG_NOINIT    ***/
    /**********************************************************************/
    /* NB: in Sleep mode LIN_FLAG_IGNORE and LIN_FLAG_SLEEP flags are set */
    /* For LIN API -- no LIN_FLAG_SLEEP                                   */
    /**********************************************************************/
}


/***************************************************************************
 * Function :   LIN_FrameError
 *
 * Description: Frame Error interrupt 
 * 
 * Returns:     none
 *
 * Notes:       MASTER: no parameters
 *              SLAVE:  input parameter
 *                      LIN_FRAMEERROR  -- frame error like break;
 *                      LIN_NORMALBREAK -- normal break received.
 *              Slave should ignore break in sleep mode
 **************************************************************************/

#if defined(MASTER)
void LIN_FrameError( void )
{
    /**********************************************************************/
    /***       MASTER:   LIN_FLAG_SEND_BREAK                            ***/
    /**********************************************************************/
    if (LIN_StateFlags & LIN_FLAG_SEND_BREAK)           
    {
        LIN_SetIdleTimeout();           /* valid break transmited -> set idle timeout */
 
        /* Set MaxFrame timeout depended on the message size */
        LIN_SetTimeout(LIN_MsgLength);

        LIN_StateFlags = LIN_FLAG_SEND_HEADER;

        /* Send Synchronization field */
        LIN_SCDRPut(LIN_SYNC_FIELD);
        /* LIN_ProtBuf[1] and BufCounter has been prepared in LIN_RequestMsg() */
    } 
    else if (LIN_StateFlags & ( LIN_FLAG_SEND_HEADER  | LIN_FLAG_SEND_DATA | 
                                LIN_FLAG_RECEIVE_DATA | LIN_FLAG_IGNORE_DATA ))
    {
        LIN_Error();        /* set Tx or Rx error for previous message */
    }
    /* in all other states -- ignoring */
}
#endif /* defined(MASTER) */

#if defined(SLAVE)
void LIN_FrameError( LIN_BYTE breakType )
{
        LIN_SetIdleTimeout();           /* break received -> set idle timeout */

        if ( (LIN_StateFlags & ( LIN_FLAG_RECEIVE_SYNC | LIN_FLAG_RECEIVE_ID | 
                                 LIN_FLAG_RECEIVE_DATA | LIN_FLAG_SEND_DATA ))&& 
                               ( breakType == LIN_FRAMEERROR ) )
        {
    
            LIN_Error();                /* set Tx or Rx error for previous message and goto IGNORE */
        } 
        else if  ( breakType == LIN_NORMALBREAK )
        {
          
            /* IGNORE or WAKEUP states -> goto RECEIVE_55 */
            LIN_StateFlags = LIN_FLAG_RECEIVE_SYNC;
        }         
}
#endif /* defined(SLAVE) */


#if defined(MASTER)
/***************************************************************************
 * Function :   LIN_Timeout
 *
 * Description: Timeout interrupt 
 * 
 * Returns:     none
 *
 * Notes:       only for Master node
 *
 **************************************************************************/
void LIN_Timeout( void )
{
    if ( LIN_StateFlags & ( LIN_FLAG_SEND_HEADER | LIN_FLAG_SEND_DATA | 
                            LIN_FLAG_RECEIVE_DATA | LIN_FLAG_IGNORE_DATA))
    {
        LIN_Error();        /* Rx or Tx error */
    }

    else if (LIN_StateFlags & LIN_FLAG_WAKEUP_TIMEOUT )
    {
                            /* Wakeup delimiter timeout expired */
        LIN_StateFlags = LIN_FLAG_IGNORE;
    } 

#if defined(LIN_DBG_CHECK_INTERNAL_ERROR)
    else
    {
        
        LIN_InternalError   = LIN_ERROR_6;
        while (1)
        {}
    }
#endif /* defined(LIN_DBG_CHECK_INTERNAL_ERROR) */

}
#endif /* defined(MASTER) */


⌨️ 快捷键说明

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