//------------------------------------------------------------------------------------------------
// Function Name
//    timerIntDisableInterrupts()
//
// Return Value : None
// Parameters   : None
//
// This function is used to disable all interrupts at the completion of a state. Using it saves
// a few bytes of code.
//-----------------------------------------------------------------------------------------------
void timerIntDisableInterrupts (void)
{
   // disable timer since no subsequent timeout is used
   DISABLE_MAC_TIMER_INTERRUPT();
   // clear Interrupt Enable 1 register
   timerIntSpiWriteReg(SI4432_INTERRUPT_ENABLE_1, 0x00);
   // clear Interrupt Enable 2 register
   timerIntSetEnable2 (0x00);
}
//------------------------------------------------------------------------------------------------
// Function Name
//    timerIntT0_ISR()
//
// Return Value : None
// Parameters   : None
//
// This is the Interrupt Service Routing for the T0 timer. The T0 timer is used for all MAC
// time outs and MAC timing events. The T0 time base uses SYSCLK/4 for all supported SYSCLK
// frequencies. The time outs are calculated using macros or calculated by the initialization
// or register write API functions. Since some time outs require long periods a 24-bit timer
// has been implemented using a global variable for the most significant byte. If the MSB of the
// timer (EZMacProTimerMSB) is non-zero, it will be decrement and the ISR will be called again
// when the timer overflows.
//
// This function disables the timer interrupts before executing the state machines. If a
// timeout event is to initiate another timeout event, the timerIntTimeout() function should
// be used.
//
// The Basic States (Wake-up, Receive, and Transmit) are implemented using if..else if bit
// tests for the corresponding bit in the master control register. The detailed TX and RX
// state machines are implemented in separate functions.
//
// Conditional compile time switches remove the TX or RX state machines for the RX only and
// and TX only builds.
//
// The timer interrupt should not call functions from other modules. This would create cause
// a multiple call to segment warning and result in poor RAM usage.
//
//-----------------------------------------------------------------------------------------------
INTERRUPT(timerIntT3_ISR, INTERRUPT_TIMER3)
{
    U8 state;

    if (EZMacProTimerMSB == 0)
    {
        DISABLE_MAC_TIMER_INTERRUPT();
        STOP_MAC_TIMER();
        CLEAR_MAC_TIMER_INTERRUPT();

        state = EZMacProReg.name.MSR & 0x0F;

        if (EZMacProReg.name.MSR == EZMAC_PRO_WAKE_UP)
        {//if the MAC is in Wake up state call the WakeUp function
            timerIntWakeUp();
        }
#ifndef RECEIVER_ONLY_OPERATION
        else if ((EZMacProReg.name.MSR & TX_STATE_BIT) == TX_STATE_BIT)
        {//if the MAC is in transmit state then call the transmit state machine
            timerIntTX_StateMachine(state);
        }
#endif
#ifndef TRANSMITTER_ONLY_OPERATION
        else if ((EZMacProReg.name.MSR & RX_STATE_BIT) == RX_STATE_BIT)
        {//if the MAC is in receive state then call the receiver
            timerIntRX_StateMachine(state);
        }
#endif
        else
        {
        }
    }
    else
    {
        CLEAR_MAC_TIMER_INTERRUPT();
        EZMacProTimerMSB--;
    }
}
/*
 * 该函数用于设置16-bit定时器
 *
 * time[15, 0]	-- 定时器溢出时间
 * time[31, 16]	-- 定时器溢出次数
 * timeout_cnt	-- 用来记录定时器溢出次数的变量, 在ISR中使用
 * */
void set_timeout_and_start_timer(U32 longTime)
{
    UU32 time;
    U16 timeout;

    DISABLE_MAC_TIMER_INTERRUPT();       // Disable Timer interrupt
    STOP_MAC_TIMER();                    // Stop Timer

    if (0 != is_mac_timer_inuse) {
        /* 用于检测定时器使用的冲突, 如果有冲突说明状态控制有异常, 暂时不做处理 */
    }

    is_mac_timer_inuse = 1;

    time.U32 = longTime;
    if (longTime > 65535) {
        EZMacProTimerMSB	= time.U16[MSB];
        timeout	= 65535;
    } else {
        EZMacProTimerMSB	= 0;
        timeout = time.U16[LSB];
    }
    EZMacProTimerLSB	= time.U16[LSB];

#if C51_SYNTAX_
    time.U16[LSB] 	= -time.U16[LSB];
    TIMER_LOW_BYTE 	= time.U8[b0];       // write LSB first
    TIMER_HIGH_BYTE = time.U8[b1];       // write MSB last
#else
    init_si4432_mac_timer(timeout);
#endif

    CLEAR_MAC_TIMER_INTERRUPT();         // Clear Timer interrupt
    START_MAC_TIMER();                   // Start Timer

    return;
}