📄 osal_timers.c
字号:
/*********************************************************************
Filename: OSAL_Timers.c
Revised: $Date: 2006-10-27 16:25:55 -0700 (Fri, 27 Oct 2006) $
Revision: $Revision: 12442 $
Description: OSAL Timer definition and manipulation functions.
Copyright (c) 2006 by Texas Instruments, Inc.
All Rights Reserved. Permission to use, reproduce, copy, prepare
derivative works, modify, distribute, perform, display or sell this
software and/or its documentation for any purpose is prohibited
without the express written consent of Texas Instruments, Inc.
*********************************************************************/
/*********************************************************************
* INCLUDES
*/
#include "ZComDef.h"
#include "OnBoard.h"
#include "OSAL.h"
#include "OSAL_Timers.h"
#include "hal_timer.h"
#include "hal_led.h"
/*********************************************************************
* MACROS
*/
/*********************************************************************
* CONSTANTS
*/
/*********************************************************************
* TYPEDEFS
*/
typedef struct
{
void *next;
UINT16 timeout;
UINT16 event_flag;
byte task_id;
} osalTimerRec_t;
/*********************************************************************
* GLOBAL VARIABLES
*/
osalTimerRec_t *timerHead;
uint32 tmr_count; // Amount of time per tick - in micro-sec
uint16 tmr_decr_time; // Decr_Time for system timer
byte timerActive; // Flag if hw timer active
/*********************************************************************
* EXTERNAL VARIABLES
*/
/*********************************************************************
* EXTERNAL FUNCTIONS
*/
/*********************************************************************
* LOCAL VARIABLES
*/
// Milliseconds since last reboot
static uint32 osal_systemClock;
/*********************************************************************
* LOCAL FUNCTION PROTOTYPES
*/
osalTimerRec_t *osalAddTimer( byte task_id, UINT16 event_flag, UINT16 timeout );
osalTimerRec_t *osalFindTimer( byte task_id, uint16 event_flag );
void osalDeleteTimer( osalTimerRec_t *rmTimer );
static void osalTimerUpdate( uint16 time );
void osal_timer_activate( byte turn_on );
void osal_timer_hw_setup( byte turn_on );
void osal_set_timer_interrupt( byte turn_on );
void osal_retune_timers( void );
/*********************************************************************
* FUNCTIONS
*********************************************************************/
/*********************************************************************
* @fn osalTimerInit
*
* @brief Initialization for the OSAL Timer System.
*
* @param none
*
* @return
*/
void osalTimerInit( void )
{
// Initialize the rollover modulo
tmr_count = TICK_TIME;
tmr_decr_time = TIMER_DECR_TIME;
// Initialize the system timer
osal_timer_activate( false );
timerActive = false;
osal_systemClock = 0;
}
/*********************************************************************
* @fn osalAddTimer
*
* @brief Add a timer to the timer list.
* Ints must be disabled.
*
* @param task_id
* @param event_flag
* @param timeout
*
* @return osalTimerRec_t * - pointer to newly created timer
*/
osalTimerRec_t * osalAddTimer( byte task_id, UINT16 event_flag, UINT16 timeout )
{
osalTimerRec_t *newTimer;
osalTimerRec_t *srchTimer;
// Look for an existing timer first
newTimer = osalFindTimer( task_id, event_flag );
if ( newTimer )
{
// Timer is found - update it.
newTimer->timeout = timeout;
return ( newTimer );
}
else
{
// New Timer
newTimer = osal_mem_alloc( sizeof( osalTimerRec_t ) );
if ( newTimer )
{
// Fill in new timer
newTimer->task_id = task_id;
newTimer->event_flag = event_flag;
newTimer->timeout = timeout;
newTimer->next = (void *)NULL;
// Does the timer list already exist
if ( timerHead == NULL )
{
// Start task list
timerHead = newTimer;
}
else
{
// Add it to the end of the timer list
srchTimer = timerHead;
// Stop at the last record
while ( srchTimer->next )
srchTimer = srchTimer->next;
// Add to the list
srchTimer->next = newTimer;
}
return ( newTimer );
}
else
return ( (osalTimerRec_t *)NULL );
}
}
/*********************************************************************
* @fn osalFindTimer
*
* @brief Find a timer in a timer list.
* Ints must be disabled.
*
* @param task_id
* @param event_flag
*
* @return osalTimerRec_t *
*/
osalTimerRec_t *osalFindTimer( byte task_id, uint16 event_flag )
{
osalTimerRec_t *srchTimer;
// Head of the timer list
srchTimer = timerHead;
// Stop when found or at the end
while ( srchTimer )
{
if ( srchTimer->event_flag == event_flag &&
srchTimer->task_id == task_id )
break;
// Not this one, check another
srchTimer = srchTimer->next;
}
return ( srchTimer );
}
/*********************************************************************
* @fn osalDeleteTimer
*
* @brief Delete a timer from a timer list.
* Ints must be disabled.
*
* @param table
* @param rmTimer
*
* @return none
*/
void osalDeleteTimer( osalTimerRec_t *rmTimer )
{
osalTimerRec_t *srchTimer;
// Does the timer list really exist
if ( (timerHead != NULL) && rmTimer )
{
// Add it to the end of the timer list
srchTimer = timerHead;
// First element?
if ( srchTimer == rmTimer )
{
timerHead = rmTimer->next;
osal_mem_free( rmTimer );
}
else
{
// Stop when found or at the end
while ( srchTimer->next && srchTimer->next != rmTimer )
srchTimer = srchTimer->next;
// Found?
if ( srchTimer->next == rmTimer )
{
// Fix pointers
srchTimer->next = rmTimer->next;
// Deallocate the timer struct memory
osal_mem_free( rmTimer );
}
}
}
}
/*********************************************************************
* @fn osal_start_timer
*
* @brief
*
* This function is called to start a timer to expire in n mSecs.
* When the timer expires, the calling task will get the specified event.
*
* @param UINT16 event_id - event to be notified with
* @param UNINT16 timeout_value - in milliseconds.
*
* @return ZSUCCESS, NO_TIMER_AVAIL, INVALID_EVENT_ID or INVALID_TIMEOUT_VALUE
*/
byte osal_start_timer( UINT16 event_id, UINT16 timeout_value )
{
return osal_start_timerEx( osal_self(), event_id, timeout_value );
}
/*********************************************************************
* @fn osal_start_timerEx
*
* @brief
*
* This function is called to start a timer to expire in n mSecs.
* When the timer expires, the calling task will get the specified event.
*
* @param byte taskID - task id to set timer for
* @param UINT16 event_id - event to be notified with
* @param UNINT16 timeout_value - in milliseconds.
*
* @return ZSUCCESS, or NO_TIMER_AVAIL.
*/
byte osal_start_timerEx( byte taskID, UINT16 event_id, UINT16 timeout_value )
{
halIntState_t intState;
osalTimerRec_t *newTimer;
HAL_ENTER_CRITICAL_SECTION( intState ); // Hold off interrupts.
// Add timer
newTimer = osalAddTimer( taskID, event_id, timeout_value );
if ( newTimer )
{
#ifdef POWER_SAVING
// Update timer registers
osal_retune_timers();
(void)timerActive;
#endif
// Does the timer need to be started?
if ( timerActive == FALSE )
{
osal_timer_activate( TRUE );
}
}
HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts.
return ( (newTimer != NULL) ? ZSUCCESS : NO_TIMER_AVAIL );
}
/*********************************************************************
* @fn osal_stop_timer
*
* @brief
*
* This function is called to stop a timer that has already been started.
* If ZSUCCESS, the function will cancel the timer and prevent the event
* associated with the timer from being set for the calling task.
*
* @param UINT16 event_id - identifier of the timer that is to be stopped
*
* @return ZSUCCESS or INVALID_EVENT_ID
*/
byte osal_stop_timer( UINT16 event_id )
{
return osal_stop_timerEx( osal_self(), event_id );
}
/*********************************************************************
* @fn osal_stop_timerEx
*
* @brief
*
* This function is called to stop a timer that has already been started.
* If ZSUCCESS, the function will cancel the timer and prevent the event
* associated with the timer from being set for the calling task.
*
* @param byte task_id - task id of timer to stop
* @param UINT16 event_id - identifier of the timer that is to be stopped
*
* @return ZSUCCESS or INVALID_EVENT_ID
*/
byte osal_stop_timerEx( byte task_id, UINT16 event_id )
{
halIntState_t intState;
osalTimerRec_t *foundTimer;
HAL_ENTER_CRITICAL_SECTION( intState ); // Hold off interrupts.
// Find the timer to stop
foundTimer = osalFindTimer( task_id, event_id );
if ( foundTimer )
{
osalDeleteTimer( foundTimer );
#ifdef POWER_SAVING
osal_retune_timers();
#endif
}
HAL_EXIT_CRITICAL_SECTION( intState ); // Re-enable interrupts.
return ( (foundTimer != NULL) ? ZSUCCESS : INVALID_EVENT_ID );
}
/*********************************************************************
* @fn osal_get_timeoutEx
*
* @brief
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -