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

📄 lintmr.c

📁 基于FREESCALE单片机9S08DZ60的LIN从节点的应用层源代码.LIN是通过UART串口来实现的.开发环境为CODEWARRIOR FOR HC08
💻 C
字号:
#define LINTMR_C
/******************************************************************************
*                                                       
*       Copyright (C) 2005 Freescale Semiconductor, Inc.
*       All Rights Reserved								              
*														                            
* Filename:     lintmr.c                
*														                            
* Revision:      										                    
*														                            
* Functions:    LIN Timeout management module 
*												                            
* Description:  
*
* Notes:        Protocol timeouts -- only for Master node
*
******************************************************************************/

#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_WORD LIN_IdleTimeout;          /* counter for idle timeout */

#if !defined(LINAPI_1_0)

#if defined(MASTER)

static LIN_BYTE LIN_ProtocolTimeout;      /* counter for protocol timeout */
LIN_BYTE  LIN_TmpTPMStatus;               /* byte to clear TPM status  */
LIN_WORD LIN_LengthCh0Tick;               /* length of   one timer Channel 1 overflow tick;
                                             the LIN_LengthCh0Tick is not declared as static due to 
                                             it used as symbol in assembler macro, but Cosmic compiler
                                             eliminate symbol references to static variables */

#if defined(S08) && defined(CW08)
#pragma DATA_SEG DEFAULT
#endif /* defined(S08) && defined(CW08) */ 
/****************************************************************************
 * End of common-purpose RAM variables declaration
 ***************************************************************************/


 /***************************************************************************
 * Function :   LIN_COUNTER16ADD
 *
 * Description: Add 16-bit value to 16-bit MCU register and put the result
 *              into this register.
 *                 
 * Returns:     none
 *
 * Notes:       The specific access order shall to be used during
 *              S08 16-bit counter-related register: high byte first.
 *              Compilers used reverce access order, so specific routine
 *              shall be used to proper counter maintenance.
 *
 **************************************************************************/
#if defined(S08) && defined(CW08)

#define LIN_COUNTER16ADD(reg1, addvalue) \
    /*  reg1 += addvalue; */ \
    asm lda reg1##L; \
    asm add addvalue:1; \
    asm tax; \
    asm lda reg1##H; \
    asm adc addvalue; \
    asm sta reg1##H; \
    asm stx reg1##L; 

#endif /* defined(S08) && defined(CW08) */



/***************************************************************************
 * Function :   LIN_COUNTER16ADD2   
 *
 * Description: Add 16-bit value to 16-bit MCU register and put the result
 *              into another 16-bit MCU register.
 *                 
 * Returns:     none
 *
 * Notes:       The specific access order shall to be used during
 *              S08 16-bit counter-related register: high byte first.
 *              Compilers used reverce access order, so specific routine
 *              shall be used to proper counter maintenance.
 *
 **************************************************************************/
#if defined(S08) && defined(CW08)

#define LIN_COUNTER16ADD2(reg1, reg2, addvalue) \
    /*  reg1 = reg2 + addvalue; */ \
    asm ldhx reg2; \
    asm txa; \
    asm add addvalue:1; \
    asm tax; \
    asm pshh; \
    asm pula; \
    asm adc addvalue; \
    asm sta reg1##H; \
    asm stx reg1##L;

#endif /* defined(S08) && defined(CW08) */

/***************************************************************************
 * Function :   LIN_SetTimeout
 *
 * Description: Set protocol timeout.
 *                  - disable channel 0 interrupt
 *                  - set timeout
 *                  - enable channel 0 interrupt
 *                  - set LIN_ProtocolTimeout value
 *
 * Returns:     none
 *
 * Notes:       none
 *
 **************************************************************************/
void LIN_SetTimeout(LIN_BYTE timeoutIndex)
{

#if defined(S08DZ60)
    /* at first, disable channel interrupt */
    LIN_TPMChanSC &= ~LIN_TM_ChanSC_CHnIE;

    /* at second, set timeout */
    LIN_LengthCh0Tick = LIN_MTO_Ticks[timeoutIndex];
    LIN_TPMChanV = LIN_TPMCNT + LIN_LengthCh0Tick; 
    //LIN_COUNTER16ADD2(LIN_TPMChanV, LIN_TPMCNT, LIN_LengthCh0Tick);

    /* clear interrupt flag and enable Channel interrupt */
    LIN_TmpTPMStatus = LIN_TPMChanSC;
    LIN_TPMChanSC = (LIN_TPMChanSC & ~(LIN_TM_ChanSC_CHnF)) | LIN_TM_ChanSC_CHnIE;
    
#endif /* defined(S08DZ60) */

   
    /* set LIN_ProtocolTimeout value for this timeout */
    LIN_ProtocolTimeout = LIN_MTO_Counter[timeoutIndex];
}

/***************************************************************************
 * Function :   LIN_CancelTimeout
 *
 * Description: Cancel protocol timeout
 *
 * Returns:     none
 *
 * Notes:       none
 *
 **************************************************************************/
void LIN_CancelTimeout( void )
{
    /* disable channel 0 interrupt */
#if defined(S08DZ60)
    LIN_TPMChanSC &= ~LIN_TM_ChanSC_CHnIE;
#endif /* defined(S08DZ60) */
}

/***************************************************************************
 * Function :   LIN_ISR_Timer0
 *
 * Description: Timer interrupt routine. 
 *              Used for:   LIN_MaxFrameTime, 
 *                          LIN_WakeupDelimiterTime
 *                          timeouts maintenance.
 *              Also used for:  break generation.
 *                 
 * Returns:     none
 *
 * Notes:       S08DZ60 - mapped to TPM x Channel y vector (defined in configuration)
 *
 **************************************************************************/
LIN_INTERRUPT LIN_ISR_Timer( void ) 
{
    LIN_DBG_SET_PORT_0;
    LIN_DBG_SET_PORT_4;

    --LIN_ProtocolTimeout;

    if ( LIN_ProtocolTimeout == 0)
    {   
        /* timeout expired */

        /* clear interrupt flag and disable channel 0 interrupt */
#if defined(S08DZ60)   
    LIN_TmpTPMStatus =  LIN_TPMChanSC;
    LIN_TPMChanSC    = (LIN_TPMChanSC & ~(LIN_TM_ChanSC_CHnF | LIN_TM_ChanSC_CHnIE)) ;
#endif /* defined(S08DZ60) */
        
        LIN_Timeout();
    }
    else
    {
        /* timeout continued */

        /* arm timer Channel 0 for next compare */
#if defined(S08DZ60)
        /* this is LIN_TPMChanV = LIN_TPMChanV + LIN_LengthCh0Tick */
        LIN_TPMChanV = LIN_TPMChanV + LIN_LengthCh0Tick;
        //LIN_COUNTER16ADD(LIN_TPMChanV, LIN_LengthCh0Tick); 
        /* clear interrupt flag and enable Channel interrupt */
        LIN_TmpTPMStatus =  LIN_TPMChanSC;
        LIN_TPMChanSC    = (LIN_TPMChanSC & ~(LIN_TM_ChanSC_CHnF )) | LIN_TM_ChanSC_CHnIE;
#endif /* defined(S08DZ60) */


    }

    LIN_DBG_CLR_PORT_0;
    LIN_DBG_CLR_PORT_4;
}

#define LIN_MAXMODULO   0xFFFFu
/**************************************************************************
 * Function :   LIN_TimerInit
 *
 * Description: Timer Module Initialization
 *
 * Returns:     none
 *
 * Notes:       none
 *
 **************************************************************************/
void LIN_TimerInit( void ) 
{
LIN_BYTE  LIN_TmpTPMStatus;

    LIN_TmpTPMStatus = LIN_TPMSC;
    LIN_TPMSC = 0;                                     /* clear prescaler and disable the timer */           
    LIN_TPMSC |= (LIN_TM_SC_PR & LIN_CfgConst.LIN_TimerPrescaler);  /* set prescaler */             
    LIN_TPMMOD = LIN_MAXMODULO;                                  /* set maximum modulo value */  
    
    /* clear interrupt flag and disable interrupt channel 0;                                    
       channel 0 performs output compare with not using pin */                                        
    LIN_TmpTPMStatus = LIN_TPMChanSC;
    LIN_TPMChanSC = (LIN_TPMChanSC & (~(LIN_TM_ChanSC_CHnF | LIN_TM_ChanSC_CHnIE| LIN_TM_ChanSC_MSnB | LIN_TM_ChanSC_ELSnB | LIN_TM_ChanSC_ELSnA))) | LIN_TM_ChanSC_MSnA;                                    
 
    /* enable timer and select the TPM clock source*/                                                                          
    LIN_TPMSC |= (LIN_TM_SC_CLK & LIN_CfgConst.LIN_TimerCLK);
}

#endif /* defined(MASTER) */


/***************************************************************************
 * Function :   LIN_IdleClock
 *
 * Description: User time clock for Idle timeout.
 *              Update "no-bus-activity" condition counter
 *              and check, if the condition meets.
 *                 
 * Returns:     none
 *
 * Notes:       1. API Service call       
 *
 **************************************************************************/
void LIN_IdleClock( void ) 
{
    LIN_BYTE        intMask;

    LIN_DBG_SET_PORT_7;

    intMask = LIN_DisableInt();         /* Disable interrupts -- do we really need it? */

    if ( LIN_IdleTimeout != 0  )
        {
            --LIN_IdleTimeout;
        }

    LIN_EnableInt(intMask);             /* enable interrupts */    

    LIN_DBG_CLR_PORT_7;
}

#endif /* !defined(LINAPI_1_0) */

/***************************************************************************
 * Function :   LIN_SetIdleTimeout
 *
 * Description: Set/reset LIN_IdleTimeout variable from constant
 *                 
 * Returns:     none
 *
 * Notes:       
 *
 **************************************************************************/
void LIN_SetIdleTimeout( void )
{
    LIN_IdleTimeout = LIN_CfgConst.LIN_IdleTimeoutClocks;
}

⌨️ 快捷键说明

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