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

📄 linsci.c

📁 飞思卡尔车身控制技术研讨会资料 里面有LIN的主从节点源程序
💻 C
字号:
#define LINSCI_C
/******************************************************************************
*                                                       
*       Copyright (C) 2005 Freescale Semiconductor, Inc.
*       All Rights Reserved								              
*														                            
* Filename:     linsci.c                
*														                            
* Revision:      										                    
*														                            
* Functions:    SCI management module
*												                            
* Description:
*
* Notes:        
*
******************************************************************************/

#include <linbase.h>

#pragma MESSAGE DISABLE C4200 /* WARNING C4200: other segment than in previous declaration */

/****************************************************************************
 * All common-purpose RAM variables shall be declared here.
 ***************************************************************************/
/****************************************************************************
 * Some of the variables (more often used) can be declared using LIN_ZPAGE
 * modifier.
 * It require to perform zero page RAM placement optimisation.
 ***************************************************************************/
#if defined(S08) && defined(CW08)
#pragma DATA_SEG SHORT ZeroSeg
#endif /* defined(S08) && defined(CW08) */

LIN_ZPAGE LIN_BYTE  LIN_TmpSCIStatus;   /* byte to clear SCI status, and to receive byte   */

#if defined(S08) && defined(CW08)
#pragma DATA_SEG DEFAULT
#endif /* defined(S08) && defined(CW08) */

#if defined(LINAPI_1_0)

/***************************************************************************
 * Function :   l_ifc_init_sci0
 *
 * Description: Initialize SCI to work with LIN bus. 
 *                  Set specified baud rate;
 *                  Disable all SCI interrupts;
 *                  Enable Tx and RX pins
 *
 * Returns:     none
 *
 * Notes:       LIN API service
 *
 **************************************************************************/

void l_ifc_init_sci0( void )
{
    LIN_BYTE    intMask;

    LIN_DBG_SET_PORT_7;
    intMask = LIN_DisableInt();     /* disable interrupts */

    /* Disconnect SCI and set initial state -- this is abort any activity */
    LIN_StateFlags = LIN_FLAG_IGNORE | LIN_FLAG_DISCONNECT;
    
    /* ??? SCI not disabled while initialization */
#if defined(S08DZ60) 
    LIN_SCISetBaudRate(LIN_CfgConst.LIN_BaudRate);

    /* one start bit, eight data bits, one stop bit */
    LIN_SCCR1 = 0;                            
    /* enable transmitter & receiver and disable all SCI interrupts */
    LIN_SCCR2 = LIN_SCCR2_TE | LIN_SCCR2_RE;  

    /* SCI flags clearing and interrupts enables in ifc_connect */
#endif   /* defined(S08DZ60) */

    LIN_EnableInt(intMask);         /* enable interrupts */
    LIN_DBG_CLR_PORT_7;
}


/***************************************************************************
 * Function :   l_ifc_connect_sci0
 *
 * Description: Connect SCI -- clear all SCI flags,
 *              enable SCI interrupts, reset idle timeout.
 *
 * Returns:     0   -- success
 *              ~0  -- called before l_ifc_init()
 *
 * Notes:       LIN API.
 *              If already connected then do nothing.
 *
 **************************************************************************/
l_bool l_ifc_connect_sci0( void )
{
    l_irqmask   intMask;
    l_bool      ret;

    ret = 0;    /* suppose that connecting OK */

    LIN_DBG_SET_PORT_7;
    intMask = LIN_DisableInt();     /* disable interrupts */

    /* if l_ifc_connect called before l_ifc_init then return error */
    if ( (LIN_StateFlags & LIN_FLAG_NOINIT) != 0 )
    {
        ret = ~0;
    }

    /* we are after l_ifc_init */

    /* if an interrupt occurs here and call l_sys_init() then we in LIN_FLAG_NOINIT
       so we need interrupt disabling -- OPTIM */

    /* if interface is already connected then do nothing */
    else if ( (LIN_StateFlags & LIN_FLAG_DISCONNECT) != 0 )
    {
        /* we are in disconnected state -> connect */

        /* connect SCI hardware */
#if defined(S08DZ60)
                                        /* clear SCI status */
        LIN_TmpSCIByte = LIN_SCSR1;
        LIN_TmpSCIByte = LIN_SCDR;

        LIN_SCCR2 |= LIN_SCCR2_RIE;     /* enable RX complited interrupt */
#endif /* defined(S08DZ60) */

        /* start to wait break */
        LIN_StateFlags = LIN_FLAG_IGNORE;

        /* reset idle timeout */
        LIN_SetIdleTimeout();               
    }

    LIN_EnableInt(intMask);         /* enable interrupts */
    LIN_DBG_CLR_PORT_7;

    return ret;
}

/***************************************************************************
 * Function :   l_ifc_disconnect_sci0
 *
 * Description: Disconnect SCI -- disable SCI interrupts.
 *
 * Returns:     0   -- success
 *              ~0  -- called before l_ifc_init()
 *
 * Notes:       LIN API.
 *              If already disconnected then do nothing.
 *
 **************************************************************************/
l_bool l_ifc_disconnect_sci0( void )
{
    l_irqmask   intMask;
    l_bool      ret;

    ret = 0;    /* suppose that disconnecting OK */

    LIN_DBG_SET_PORT_7;
    intMask = LIN_DisableInt();     /* disable interrupts */

    /* if l_ifc_disconnect called before l_ifc_init then return error */
    if ( (LIN_StateFlags & LIN_FLAG_NOINIT) != 0 )
    {
        ret = ~0;
    }

    /* we are after l_ifc_init */

    /* if an interrupt occurs here and call l_sys_init() then we in LIN_FLAG_NOINIT
       so we need interrupt disabling -- OPTIM */

    /* if interface is already disconnected then do nothing */
    else if ( (LIN_StateFlags & LIN_FLAG_DISCONNECT) == 0 )
    {
        /* we are in connected state -> disconnect */

        /* disconnect SCI hardware */
#if defined(S08DZ60)
        /* enable transmitter and receiver, disable all SCI interrupts */
        LIN_SCCR2 = LIN_SCCR2_TE | LIN_SCCR2_RE;  
#endif /* defined(S08DZ60) */

        /* set disconnected state */
        LIN_StateFlags = LIN_FLAG_IGNORE | LIN_FLAG_DISCONNECT;
    }

    LIN_EnableInt(intMask);         /* enable interrupts */
    LIN_DBG_CLR_PORT_7;

    return ret;
}


#else /* !defined(LINAPI_1_0) */


/***************************************************************************
 * Function :   LIN_SCIInit
 *
 * Description: Initialize SCI to work with LIN bus. 
 *                  Set specified baud rate;
 *                  Disable all SCI interrupts;
 *                  Enable Tx and RX pins
 *                  Enable Rx interrupt
 *
 * Returns:     none
 *
 * Notes:       1. SLAVE, MASTER node
 *
 **************************************************************************/
void LIN_SCIInit( void )
{
    /* ??? SCI not disabled while initialization */
#if defined(S08DZ60)
                                    /* initialization order correct */
    LIN_SCISetBaudRate(LIN_CfgConst.LIN_BaudRate);

    /* one start bit, eight data bits, one stop bit */
    LIN_SCCR1 = 0;                            
    /* enable transmitter & receiver and disable all SCI interrupts */
    LIN_SCCR2 = LIN_SCCR2_TE | LIN_SCCR2_RE;  

#if defined (MASTER)
    LIN_SCSR2 |= LIN_SCSR2_BRK13;  /* init sending 13 bit break with M=0*/
#endif   /* defined(MASTER) */       
                             
																	  /* clear SCI status */
    LIN_TmpSCIByte = LIN_SCSR1;
    LIN_TmpSCIByte = LIN_SCDR;

    LIN_SCCR2 |= LIN_SCCR2_RIE;    /* enable RX complete interrupt */
    LIN_SCCR3 |= LIN_SCCR3_FEIE;   /* Enable Frame Error interrupt */
#endif   /* defined(S08DZ60) */
}

#endif  /* !defined(LINAPI_1_0) */

/***************************************************************************
 * Function :   LIN_SCISetBaudRate
 *
 * Description: Programm SCI to particular baud rate
 *
 * Returns:     none
 *
 * Notes:       none
 *
 **************************************************************************/
void LIN_SCISetBaudRate( LIN_WORD baudRate )
{
    /* set the baud rate divider  */
#if defined(S08DZ60)    
    LIN_SCBDH = (LIN_BYTE)((baudRate >> 8) & 0x001f);               
    LIN_SCBDL = (LIN_BYTE)(baudRate & 0x00ff);
#endif  /* defined(S08DZ60) */  
     
}

/****************************************************************************/
/****************************************************************************/
/***                     Interrupt Driven Routines                        ***/
/****************************************************************************/
/****************************************************************************/
#if defined(LINAPI_1_0)

/***************************************************************************
 * Function :   l_ifc_tx_sci0
 *
 * Description: SCI transmit processing
 *
 * Returns:     none
 *
 * Notes:       1. Tx enable interrupt disabled forever
 *                 Tx completed interrupt enabled only while send wakeup symbol 
 *              2. LIN API function
 *
 **************************************************************************/
void l_ifc_tx_sci0( void )
{
    LIN_DBG_SET_PORT_0;

    if ( (LIN_StateFlags & LIN_FLAG_DISCONNECT) == 0 )
    {   
        /* work only if SCI connected */

        LIN_DBG_SET_PORT_2;

#if defined(S08DZ60)
        LIN_SCCR2  &= ~( LIN_SCCR2_TCIE );    /* Disable Tx completed */
#endif  /* defined(S08DZ60) */        

        LIN_TxCompleted();

        LIN_DBG_CLR_PORT_2;
    }

    LIN_DBG_CLR_PORT_0;
}

/***************************************************************************
 * Function :   l_ifc_rx_sci0
 *
 * Description: SCI receive and SCI error processing
 *
 * Returns:     none
 *
 * Notes:       1. Only for LIN API
 *              2. Set bit 1 in debug port while working.
 *              3. Rx interrupt enabled forever
 *
 **************************************************************************/
void l_ifc_rx_sci0( void )
{
    LIN_DBG_SET_PORT_0;

    if ( (LIN_StateFlags & LIN_FLAG_DISCONNECT) == 0 )
    {   
        /* work only if SCI connected */

#if defined(S08DZ60)
        LIN_TmpSCIStatus = LIN_SCSR1;
        LIN_TmpSCIByte   = LIN_SCDR;                  /* clear all flags */
#endif  /* defined(S08DZ60) */
        
#if defined(S08DZ60)
        if ( LIN_TmpSCIStatus & LIN_SCSR1_FE ) 
        {
            if ( LIN_TmpSCIByte == 0 )      /* Check SCI  RX data bit if 0 so count next 
                                               bittime to receive 11 bittime break else frame error */          
            {
#endif  /* defined(S08DZ60) */              

                /* Normal Break */

                LIN_DBG_SET_PORT_5;

                LIN_FrameError(LIN_NORMALBREAK);

                LIN_DBG_CLR_PORT_5;
            }
            else
            {

#if defined(S08DZ60)
                LIN_SCCR2 &= ~LIN_SCCR2_RE;    /* NB: disable and enable SCI to prevent */
                                                /*     distinction next zero level bit as start bit */
#endif  /* defined(S08DZ60) */                                                 
                                                
                LIN_DBG_SET_PORT_3;         /* indicate frame error recognized */

                LIN_FrameError(LIN_FRAMEERROR);                  

                LIN_DBG_CLR_PORT_3;

#if defined(S08DZ60)                
                LIN_SCCR2 |= LIN_SCCR2_RE;     /* enable SCI Rx */
#endif  /* defined(S08DZ60) */
            }                                      
        }

        else
        {
            /* SCI Receive */

            LIN_DBG_SET_PORT_1;

            LIN_RxCompleted();
        
            LIN_DBG_CLR_PORT_1;
        }
    }

    LIN_DBG_CLR_PORT_0;
}


#else /* defined(LINAPI_1_0) */
/***************************************************************************
 * Function :   LIN_ISR_SCI_Receive
 *
 * Description: SCI receive interrupt
 *
 * Returns:     none
 *
 * Notes:       1. Set bit 1 in debug port while working.
 *              2. Rx interrupt enabled forever
 *              3. For Freescale API only
 *
 **************************************************************************/
LIN_INTERRUPT LIN_ISR_SCI_Receive ( void )
{
    LIN_DBG_SET_PORT_0;
    LIN_DBG_SET_PORT_1;

#if defined(S08DZ60)
    LIN_TmpSCIStatus = LIN_SCSR1;
    LIN_TmpSCIByte   = LIN_SCDR;                  /* clear all flags */
#endif  /* defined(S08DZ60) */
		
    LIN_RxCompleted();
        
    LIN_DBG_CLR_PORT_1;
    LIN_DBG_CLR_PORT_0;
}

/***************************************************************************
 * Function :   LIN_ISR_SCI_Error
 *
 * Description: SCI error interrupt
 *                        
 * Returns:     none
 *
 * Notes:       1. Set bit 1 in debug port while working.
 *              2. For Freescale API only
 * 
 **************************************************************************/
LIN_INTERRUPT LIN_ISR_SCI_Error ( void )
{
    LIN_DBG_SET_PORT_4;

    LIN_DBG_SET_PORT_0;

#if defined(S08DZ60)
    LIN_TmpSCIStatus = LIN_SCSR1;
    LIN_TmpSCIByte   = LIN_SCDR;                  /* clear all flags */
#endif  /* defined(S08DZ60) */

#if defined(MASTER)

    LIN_DBG_SET_PORT_3;             /* indicate frame error recognized */

    LIN_FrameError();

    LIN_DBG_CLR_PORT_3;

   
#endif /* defined(MASTER) */

#if defined(SLAVE)

#if defined(S08DZ60)
        if ( LIN_TmpSCIByte == 0 )      /* Check SCI  RX data bit if 0 so count next 
                                           bittime to receive 11 bittime break else frame error */          
#endif  /* defined(S08DZ60) */ 
				{ 
        LIN_DBG_SET_PORT_5;

        LIN_FrameError(LIN_NORMALBREAK);

        LIN_DBG_CLR_PORT_5;
    }
    else
    {
        LIN_DBG_SET_PORT_3;         /* indicate frame error recognized */

        LIN_FrameError(LIN_FRAMEERROR);                  

        LIN_DBG_CLR_PORT_3;

    }                                      
#endif /* defined(SLAVE) */

    LIN_DBG_CLR_PORT_0;
}

#endif /* defined(LINAPI_1_0) */

⌨️ 快捷键说明

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