📄 mac_timer.c
字号:
/*******************************************************************************************************
* *
* ********** *
* ************ *
* *** *** *
* *** +++ *** *
* *** + + *** *
* *** + CHIPCON CC2420 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, 2004 *
*******************************************************************************************************
* 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 8 MHz crystal oscillator! *
*******************************************************************************************************
* Compiler: AVR-GCC *
* Target platform: CC2420DB, CC2420 + any ATMEGA MCU *
*******************************************************************************************************
* The revision history is located at the bottom of this file *
*******************************************************************************************************/
#pragma SFR
#pragma NOP
#pragma STOP
#pragma HALT
#pragma DI
#pragma EI
#pragma section @@DATA time at 0xEB00///
#pragma interrupt INTTM000 MD_INTTM000 //SIGNAL(SIG_OUTPUT_COMPARE1A)
#pragma interrupt INTTM001 MD_INTTM001 //SIGNAL(SIG_INPUT_CAPTURE1)
///#pragma interrupt INTTM011 MD_INTTM011 //SIGNAL(SIG_OUTPUT_COMPARE1C)
#pragma interrupt INTTM51 MD_INTTM51 //SIGNAL(SIG_OUTPUT_COMPARE1C)
///#pragma interrupt INTTM51 MD_INTTM50 //SIGNAL(SIG_OUTPUT_COMPARE1C)
#include "mac_headers.h"
//-------------------------------------------------------------------------------------------------------
// Callback table and other timer-related variables
MAC_CALLBACK_INFO pMtimCallbacks[MAC_CALLBACK_COUNT];
__sreg MAC_TIMER_INFO mtimInfo;///
__sreg INT32 volatile test;////
char str1[4];///
char str2[32][4];///
char str[32];
INT8 volatile COMA;
INT8 volatile INTP;
INT8 array1[32];
///INT8 array2[128][128];
//-------------------------------------------------------------------------------------------------------
int UART_StartTx(char *buffer);///
//-------------------------------------------------------------------------------------------------------
// void mtimInit(void)
//
// DESCRIPTION:
// Initializes Timer 1 of the ATMEGA128/64 for interrupts at every backoff slot.
//-------------------------------------------------------------------------------------------------------
void mtimInit(void) {
UINT8 n;
// Initialize the callback table / queue
for (n = 0; n < MAC_CALLBACK_COUNT; n++) {
pMtimCallbacks[n].occupied = FALSE;
}
mtimInfo.nextCallback = NO_CALLBACK;
// Reset time stamps and counters
mtimInfo.bosCounter = 0;
mtimInfo.captureBosCounter = 0;
mtimInfo.captureTcnt = 0;
mtimInfo.captureTime = 0;
mtimInfo.bosCounterAdjustTime = 0;
// Configure the timer registers
///TIMER1_INIT(TIMER1_CAPTURE_NOISE_CANCELLER | TIMER1_CAPTURE_ON_POSEDGE);
///TIMER1_SET_COMPA_VALUE(MAC_TIMER_OVERFLOW_VALUE);
///TIMER1_SET_COMPB_VALUE(MAC_TIMER_BACKOFF_SLOT_OFFSET);
///TIMER3_INIT(TIMER3_CAPTURE_NOISE_CANCELLER | TIMER3_CAPTURE_ON_POSEDGE);///
///TIMER3_INIT( 0x00, BM(7)|BM(5), BM(3)|BM(2) );///
TIMER3_INIT( 0x00, BM(7)|BM(5), 0x00);///BM(7)|BM(5),
TIMER3_SET_COMPA_VALUE(MAC_TIMER_OVERFLOW_VALUE );///
TIMER3_SET_COMPB_VALUE(MAC_TIMER_BACKOFF_SLOT_OFFSET);///
///TIMER1_INIT(0x01, BM(6)|BM(5), BM(2));///start
TIMER1_INIT(0x01, BM(6)|BM(5), 0x00);///BM(6)|BM(5),
///DISABLE_T3_COMPB_INT();
///DISABLE_T1_COMPC_INT();
///DISABLE_SFD_CAPTURE_INT();
///DISABLE_FIFOP_INT();
///TIMER50_INIT(BM(1),0x00,BM(3)|BM(2));///
TIMER51_INIT(BM(1),0x00,BM(3)|BM(2));///
} // mtimInit
//-------------------------------------------------------------------------------------------------------
// BOOL mtimSetCallback(VFPTR pFunc, INT32 timeout)
//
// DESCRIPTION:
// Activates a timed callback. The callback will happen at [timeout] backoff slots from now.
// Note: There can only be MAC_CALLBACK_COUNT active callbacks.
//
// PARAMETERS:
// VFPTR pFunc
// A pointer to the callback function
// INT32 timeout
// The number of backoff slots to count before calling pFunc(). If this value is negative, the
// function will not be executed.
//
// RETURN VALUE:
// BOOL
// TRUE: OK
// FALSE: Too many active callbacks (>MAC_CALLBACK_COUNT) or invalid timeout value
//-------------------------------------------------------------------------------------------------------
BOOL mtimSetCallback(VFPTR pFunc, INT32 timeout) {
UINT8 newCallback, prevCallback, n;
MAC_CALLBACK_INFO *pNewCallback;
// Special timeout values
if (timeout < 0) {
return FALSE;
} else if (timeout == 0) {
pFunc();
return TRUE;
}
// Locate an available entry in the callback table
DISABLE_GLOBAL_INT();
for (newCallback = 0; newCallback < MAC_CALLBACK_COUNT; newCallback++) {
if (!pMtimCallbacks[newCallback].occupied) goto foundIndex;
}
// None available :(
ENABLE_GLOBAL_INT();
return FALSE;
foundIndex:
pNewCallback = &pMtimCallbacks[newCallback];
// No other active callbacks...
if (mtimInfo.nextCallback == NO_CALLBACK) {
mtimInfo.nextCallback = newCallback;
pNewCallback->prevCallback = NO_CALLBACK;
pNewCallback->nextCallback = NO_CALLBACK;
// Locate insertion point...
} else {
// First timeout?
if (timeout <= pMtimCallbacks[mtimInfo.nextCallback].timeout) {
// Insert before!
pNewCallback->prevCallback = NO_CALLBACK;
pNewCallback->nextCallback = mtimInfo.nextCallback;
pMtimCallbacks[mtimInfo.nextCallback].timeout -= timeout;
pMtimCallbacks[mtimInfo.nextCallback].prevCallback = newCallback;
mtimInfo.nextCallback = newCallback;
// Search through the linked list...
} else {
prevCallback = mtimInfo.nextCallback;
n = pMtimCallbacks[prevCallback].nextCallback;
timeout -= pMtimCallbacks[prevCallback].timeout;
while ((timeout > pMtimCallbacks[n].timeout) && (n != NO_CALLBACK)) {
timeout -= pMtimCallbacks[n].timeout;
prevCallback = n;
n = pMtimCallbacks[prevCallback].nextCallback;
}
if (n != NO_CALLBACK) {
pMtimCallbacks[n].timeout -= timeout;
pMtimCallbacks[n].prevCallback = newCallback;
}
pNewCallback->prevCallback = prevCallback;
pNewCallback->nextCallback = n;
pMtimCallbacks[prevCallback].nextCallback = newCallback;
}
}
// Always set...
pNewCallback->pFunc = pFunc;
pNewCallback->timeout = timeout;
pNewCallback->occupied = TRUE;
ENABLE_GLOBAL_INT();
return TRUE;
} // mtimSetCallback
//-------------------------------------------------------------------------------------------------------
// void mtimeCancelCallback(void *pFunc)
//
// DESCRIPTION:
// Cancels a callback. One entry of pFunc will be removed.
//
// PARAMETERS:
// void *pFunc
// A pointer to the callback function to be removed
//
// RETURN VALUE:
// BOOL
// A match was found and removed
//-------------------------------------------------------------------------------------------------------
BOOL mtimCancelCallback(void *pFunc) {
UINT8 n, prevIndex, nextIndex;
DISABLE_GLOBAL_INT();
// Search through the whole array
for (n = 0; n < MAC_CALLBACK_COUNT; n++) {
if ((pFunc == pMtimCallbacks[n].pFunc) && pMtimCallbacks[n].occupied) {
// Deactivate the entry
pMtimCallbacks[n].occupied = FALSE;
// Get the indexes of the previous and the next entries
prevIndex = pMtimCallbacks[n].prevCallback;
nextIndex = pMtimCallbacks[n].nextCallback;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -