📄 mac_timer.h
字号:
/*******************************************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC2430 INTEGRATED 802.15.4 MAC AND PHY *
* *** + + *** Low-Level Timing Functions *
* *** +++ *** *
* *** *** *
* ************ *
* ********** *
* *
*******************************************************************************************************
* CONFIDENTIAL *
* The use of this file is restricted by the signed MAC software license agreement. *
* *
* Copyright Chipcon AS, 2005 *
*******************************************************************************************************
* This module contains the MAC callback timer, which is used to handle timer events, and start the *
* execution of tasks. handles. The timer generates a T1.COMPA interrupt at every *
* 320 usecs = 20 symbols = 1 backoff slot. *
* *
* Command strobes that need to be aligned with backoff slot boundary must use the WAIT_FOR_BOUNADRY() *
* macro. *
* *
* NOTE: These functions are meant to be used with an 16MHz crystal oscillator! *
*******************************************************************************************************/
#ifndef MAC_TIMER_H
#define MAC_TIMER_H
/*******************************************************************************************************
*******************************************************************************************************
************************** CONSTANTS AND MACROS **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// The timer overflow value
#define CLOCK_SPEED_MHZ 8 // MHz
#define MAC_SYMBOL_DURATION 16 // us
#define MAC_TIMER_OVERFLOW_VALUE (CLOCK_SPEED_MHZ * MAC_SYMBOL_DURATION * aUnitBackoffPeriod - 1)
// Backoff slot boundary offset (1/4 into the timer tick interval)
#define MAC_TIMER_BACKOFF_SLOT_OFFSET ((MAC_TIMER_OVERFLOW_VALUE + 1) / 4)
// Waits for the next backoff slot boandary (global interrupts should be turned off!)
#define WAIT_FOR_BOUNDARY() while (!(TIFR & BM(OCF1B)))
// The number of callbacks available in the pool
#define MAC_CALLBACK_COUNT 12
// Callback queue terminator
#define NO_CALLBACK 0xFF
//-------------------------------------------------------------------------------------------------------
#define T2_STOP() (T2CNF &= ~0x01)
#define T2_SET_COUNTER_DELTA_DELAY(w) T2_SET_COUNTER(w)
#define T2_SET_COUNTER(w) \
do { \
T2TLD = LOBYTE(w); \
T2THD = HIBYTE(w); \
} while (0)
#define T2_SET_OVERFLOW_COUNTER(dw) \
do { \
T2OF0 = LOBYTE(LOWORD(dw)); \
T2OF1 = HIBYTE(LOWORD(dw)); \
T2OF2 = LOBYTE(HIWORD(dw)); \
} while (0)
#define T2_SET_OVERFLOW_COUNTER_COMPARE_VALUE(dw) \
do { \
T2PEROF0 = LOBYTE(LOWORD(dw)); \
T2PEROF1 = HIBYTE(LOWORD(dw)); \
T2PEROF2 = (T2PEROF2 & ~0x0F) | (LOBYTE(HIWORD(dw)) & 0x0F); \
} while (0)
#define T2_SET_COUNTER_RELOAD_VALUE(w) \
do { \
T2CAPLPL = LOBYTE(w); \
T2CAPHPH = HIBYTE(w); \
} while (0)
#define T2_SET_COUNTER_COMPARE_VALUE(b,lsb) \
do { \
T2CMP = (BYTE) b; \
if (lsb) { \
T2CNF |= 0x08; \
} else { \
T2CNF &= ~0x08; \
} \
} while (0)
#define T2_START_NOSYNC() \
do { \
T2CNF &= ~0x02; \
T2CNF |= 0x01; \
} while (0)
#define T2_START_SYNC() \
do { \
T2CNF |= 0x02; \
T2CNF |= 0x01; \
} while (0)
#define T2_CLEAR_COUNTER_OVERFLOW_FLAG() (T2CNF &= ~0x40)
#define T2_ENABLE_COUNTER_OVERFLOW_FLAG() (T2PEROF2 |= 0x40)
#define T2_DISABLE_COUNTER_OVERFLOW_FLAG() (T2PEROF2 &= ~0x40)
#define CLEAR_TIMER2_INT() (T2IF = 0)
#define ENABLE_TIMER2_INT() (T2IE = 1)
#define DISABLE_TIMER2_INT() (T2IE = 0)
UINT32 T2_GET_OVERFLOW_COUNTER(void);
UINT32 T2_GET_CAPTURED_COUNTER(void);
UINT32 T2_GET_CAPTURED_OVERFLOW_COUNTER(void);
UINT32 T2_GET_SLEEP_TIMER(void);
#define T2_IS_CAP_ACTIVE() (!(T2CNF & TIMER2_OVERFLOW_COMPARE_INTERRUPT_BM))
#define T2_SET_CAP_ACTIVE() (T2CNF &= ~TIMER2_OVERFLOW_COMPARE_INTERRUPT_BM)
/*******************************************************************************************************
*******************************************************************************************************
************************** MODULE DATA **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// Internal module data
// Callback table (Implemented in mac_timer.a51)
typedef struct {
UINT32 timeout;
BYTE nextCallback;
BOOL occupied;
VFPTR pFunc;
} MAC_CALLBACK_INFO;
// Backoff slot counter
typedef struct {
UINT32 captureTime;
UINT32 captureBosCounter;
UINT16 captureTcnt;
UINT8 beaconDuration;
} MAC_TIMER_INFO;
extern MAC_TIMER_INFO mtimInfo;
//-------------------------------------------------------------------------------------------------------
typedef struct {
INT16 counter;
UINT32 overflowCounter;
} TIMESTAMP;
/*******************************************************************************************************
*******************************************************************************************************
************************** FUNCTION PROTOTYPES **************************
*******************************************************************************************************
*******************************************************************************************************/
//-------------------------------------------------------------------------------------------------------
// void mtimStartSync (void)
//
// DESCRIPTION:
// Start Timer2 in synchronous mode. The routine waits until Timer2 has started
//-------------------------------------------------------------------------------------------------------
ROOT void mtimStartSync (void);
//-------------------------------------------------------------------------------------------------------
// Callback timer setup (timeout in backoff slots (rounded downwards)
ROOT void mtimInit(BOOL synchronousStart);
ROOT BOOL mtimSetCallback(VFPTR pFunc, INT32 timeout);
ROOT BOOL mtimCancelCallback(void (*pFunc)());
ROOT void mtimAlignWithBeacon(TIMESTAMP *pTimestamp);
//-------------------------------------------------------------------------------------------------------
#endif
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -