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

📄 lintmr.c

📁 基于摩托罗拉(现在飞思卡尔)系列MC68HC908单片机开发车辆ECU系统!
💻 C
字号:
#define LINTMR_C
/******************************************************************************
*
*       Copyright (C) 2003 Motorola, Inc.
*       All Rights Reserved
*
* Filename:     $RCSfile: lintmr.c,v $
* Author:       $Author: ttz778 $
* Locker:       $Locker:  $
* State:        $State: Exp $
* Revision:     $Revision: 1.0 $
*
* Functions:    LIN Timeout management module 
*
* History:      Use the RCS command log to display revision history
*               information.
*
* Description:  
*
* Notes:        Protocol timeouts -- only for Master node
*
******************************************************************************/

#include <linbase.h>

/****************************************************************************
 * All common-purpose RAM variables shall be declared here.
 ***************************************************************************/

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_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 */
 
/****************************************************************************
 * 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
 *              HC08 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(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(CW08) */

#if defined(COSMIC08)

#define LIN_COUNTER16ADD(reg1, addvalue) \
    /*  reg1 += addvalue; */ \
    _asm("xref	_"#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(COSMIC08) */

/***************************************************************************
 * 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
 *              HC08 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(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(CW08) */

#if defined(COSMIC08)

#define LIN_COUNTER16ADD2(reg1, reg2, addvalue) \
    /*  reg1 = reg2 + addvalue; */ \
    _asm("xref	_"#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(COSMIC08) */

/***************************************************************************
 * 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)
{
    /* at first, disable channel 0 interrupt */
    LIN_TASC0 &= ~LIN_TM_CHXIE;

    /* at second, set timeout */
    LIN_LengthCh0Tick = LIN_MTO_Ticks[timeoutIndex];
    /* TACH0 = TACNT + LIN_LengthCh0Tick */
    LIN_COUNTER16ADD2(_LIN_TACH0, _LIN_TACNT, LIN_LengthCh0Tick);
 
    /* clear interrupt flag and enable Channel 0 interrupt */
    LIN_TASC0 = (LIN_TASC0 & ~(LIN_TM_CHXF)) | LIN_TM_CHXIE;
 
    /* 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 */
    LIN_TASC0 &= ~LIN_TM_CHXIE;
}

/***************************************************************************
 * Function :   LIN_ISR_Timer0
 *
 * Description: Timer interrupt routine. 
 *              Used for:   LIN_MaxFrameTime, 
 *                          LIN_WakeupDelimiterTime
 *                          timeouts maintenance.
 *              Also used for:  break generation.
 *                 
 * Returns:     none
 *
 * Notes:       HC08GR16 - mapped to TIM A Channel 0 vector
 *
 **************************************************************************/
LIN_INTERRUPT LIN_ISR_Timer0( 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 */
        LIN_TASC0 = (LIN_TASC0 & ~(LIN_TM_CHXF | LIN_TM_CHXIE));
        LIN_Timeout();
    }
    else
    {
        /* timeout continued */

        /* arm timer Channel 0 for next compare */
        /* this is TACH0 = TACNT + LIN_LengthCh0Tick */
        LIN_COUNTER16ADD(_LIN_TACH0, LIN_LengthCh0Tick); 
        /* clear interrupt flag and enable Channel 0 interrupt */
        LIN_TASC0 = (LIN_TASC0 & ~(LIN_TM_CHXF)) | LIN_TM_CHXIE; 
    }

    LIN_DBG_CLR_PORT_0;
    LIN_DBG_CLR_PORT_4;
}


#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 + -