📄 timer.c
字号:
/*
===============================================================================
// TEXAS INSTRUMENTS INCORPORATED PROPRIETARY INFORMATION
//
// Property of Texas Instruments
// For Unrestricted Internal Use Only
// Unauthorized reproduction and/or distribution is strictly prohibited.
// This product is protected under copyright law and trade secret law
// as an unpublished work.
// Created 1999, (C) Copyright 1999 Texas Instruments. All rights reserved.
//
//
// Filename : timer.c
//
// Description : arm9 Timers functions (ostimer1, ostimer2, ostimer3,
// wdgtimer)
//
// Project : omap3
//
// Author : Daniel Blanc dblanc@tif.ti.com
// Modified for omap3 by Arnaud Balmelle (a-balmelle@ti.com)
// - Added OSTIMER3 for OMAP3
//
===============================================================================
*/
#include "test.h"
#include "global_types.h"
#include "result.h"
#include "intvecs.h"
#include "test.h"
#include "timer.h"
#include "clkrst.h"
#include "interrupt_mapping.h"
#include "inth2.h"
//-------------------------------------------------------------------------------
// Be carreful to use after DisableWdg
//-------------------------------------------------------------------------------
// NAME : TIME_SetupTimer -
// DESCRIPTION : Setup the timer configuration -
// SYNOPSYS : void ConfigureTimer ( TIMER_NAME_t TimerName,
// UWORD32 LoadValue, -
// TIMER_MODE_t TimerMode, -
// TIMER_SCALER_t TimerScaler) -
// PARAMETERS : -
// TimerName = Precise on which timer we work
// OSTIMER1
// OSTIMER2
// OSTIMER3
// WDGTIMER
//
// -
// LoadValue = Timer setup value is loaded when timer passes through 0 -
// or when it starts -
// -
// TimerMode = Specify the timer mode (only used for watchdog timer) -
// -TIMER_ONESHOT_MODE => the timer decrements from the loaded value to zero -
// and then stops. -
// -TIMER_AUTORELOAD_MODE => the timer restarts when it passes through zero -
//
// -TimerScaler => TIMER_PRE_SCALE_2
// TIMER_PRE_SCALE_4
// TIMER_PRE_SCALE_8
// TIMER_PRE_SCALE_16
// TIMER_PRE_SCALE_32
// TIMER_PRE_SCALE_64
// TIMER_PRE_SCALE_128
// TIMER_PRE_SCALE_256
// -
//-for watchdog timer------------------------------------------------------------
//-------------------------------------------------------------------------------
// TimerScaler = Prescale value used to define the interrupt period Tint: -
// Tint = Tclk *(LoadValue+1) * 2**(TimerScaler+1) -
// Fclk=928 Khz => Tclk=1/Fclk=1.08 Us -
// |--------------------------------------------------| -
// | LoadValue | ClockScale = 0 | ClockScale = 7 | -
// |------------|------------------|------------------| -
// | 0000 | 2.156 us | 275.8 us | -
// | FFFF | 141.2 ms | 18.078 s | -
// |--------------------------------------------------| -
// -
//-for OS timers-----------------------------------------------------------------
//-------------------------------------------------------------------------------
// TimerScaler = Prescale value used to define the interrupt period Tint: -
// Tint = Tclk *(LoadValue+1) * 2**(TimerScaler+1) -
// Fclk=100 Mhz => Tclk=1/Fclk=10 ns -
// |-------------------------------------------------------|
// | LoadValue | ClockScale = 0 | ClockScale = 7 |
// |-----------------|------------------|------------------|
// | 0000:0000 | 20 ns | 2.56 us |
// | FFFF:FFFF | 85.9 s | 11000 s |<=> 3h3'20''
// |-------------------------------------------------------|
//--------------------------------------------------------------------------------
// -
// RETURN VALUE: OK or NOT_OK if problem -
// LIMITATIONS : To prevent from some undefined results, must not be called -
// when the timer is running -
//-------------------------------------------------------------------------------
BOOL TIME_SetupTimer(TIMER_NAME_t TimerName, UWORD32 LoadValue, TIMER_MODE_t TimerMode, TIMER_SCALER_t TimerScale)
{
// Force to "Stop_Timer" and Reset Prescale_clock_Timer_value
UWORD32 cntl;
switch(TimerName)
{
case OSTIMER1 :
{
cntl=ClearBit( OSTIMER1_CNTL_SUP_REG, (OSTIMER_ST_MASK | OSTIMER_PTV_MASK) );
// Set AutoReload/OneShot timer Mode
if (TimerMode==TIMER_AUTORELOAD_MODE)
{
SetBit(cntl,OSTIMER_AR_MASK);
}
else
{
ClearBit(cntl,OSTIMER_AR_MASK);
}
// Set Pre-scale clock timer value
SetBit(cntl , (TimerScale << OSTIMER_PTV_BITPOS));
// Commit Update into Control Register
OSTIMER1_CNTL_SUP_REG = cntl;
// Load the timer value
OSTIMER1_LOAD_REG = LoadValue;
break;
}
case OSTIMER2 :
{
cntl=ClearBit( OSTIMER2_CNTL_SUP_REG, (OSTIMER_ST_MASK | OSTIMER_PTV_MASK) );
// Set AutoReload/OneShot timer Mode
if (TimerMode==TIMER_AUTORELOAD_MODE)
{
SetBit(cntl,OSTIMER_AR_MASK);
}
else
{
ClearBit(cntl,OSTIMER_AR_MASK);
}
// Set Pre-scale clock timer value
SetBit(cntl , (TimerScale << OSTIMER_PTV_BITPOS));
// Commit Update into Control Register
OSTIMER2_CNTL_SUP_REG = cntl;
// Load the timer value
OSTIMER2_LOAD_REG = LoadValue;
break;
}
case OSTIMER3 :
{
cntl=ClearBit( OSTIMER3_CNTL_SUP_REG, (OSTIMER_ST_MASK | OSTIMER_PTV_MASK) );
// Set AutoReload/OneShot timer Mode
if (TimerMode==TIMER_AUTORELOAD_MODE)
{
SetBit(cntl,OSTIMER_AR_MASK);
}
else
{
ClearBit(cntl,OSTIMER_AR_MASK);
}
// Set Pre-scale clock timer value
SetBit(cntl , (TimerScale << OSTIMER_PTV_BITPOS));
// Commit Update into Control Register
OSTIMER3_CNTL_SUP_REG = cntl;
// Load the timer value
OSTIMER3_LOAD_REG = LoadValue;
break;
}
case WDGTIMER :
{
UWORD16 cntl=ClearBit( WDGTIMER_CNTL_SUP_REG, (WDGTIMER_ST_MASK | WDGTIMER_PTV_MASK) );
// Set AutoReload/OneShot timer Mode
if (TimerMode==TIMER_AUTORELOAD_MODE)
{
SetBit(cntl,WDGTIMER_AR_MASK);
}
else
{
ClearBit(cntl,WDGTIMER_AR_MASK);
}
// Set Pre-scale clock timer value
SetBit(cntl , (TimerScale << WDGTIMER_PTV_BITPOS));
// Commit Update into Control Register
WDGTIMER_CNTL_SUP_REG = cntl;
// Load the timer value
WDGTIMER_LOAD_REG = (UWORD32)LoadValue;
break;
}
default :
{
return((BOOL)NOT_OK);
}
}
return((BOOL)OK);
}
//---------------------------------------------------------------------
// NAME : TIME_LoadTimerValue -
// DESCRIPTION : Write the Setup Counter value into Load Register -
// SYNOPSYS : void TIME_LoadTimerValue(TIMER_NAME_t WhichTimer,
// UNSIGNED32 CounterValue); -
// PARAMETERS :
// WhichTimer = Timer name to load -
// OSTIMER1
// OSTIMER2
// OSTIMER3
// WDGTIMER
//
// CountValue = Timer setup value ti load
// -
// RETURN VALUE: OK or NOT_OK
// LIMITATIONS : To prevent from some undefined results, must not be -
// called when the timer is running -
// The new value from the Load Register is written into -
// the Timer when passing through zero or when starting -
//---------------------------------------------------------------------
BOOL TIME_LoadTimerValue(TIMER_NAME_t WhichTimer, UWORD32 CounterValue)
{
switch (WhichTimer)
{
case OSTIMER1 :
{
OSTIMER1_LOAD_REG = CounterValue;
break;
}
case OSTIMER2 :
{
OSTIMER2_LOAD_REG = CounterValue;
break;
}
case OSTIMER3 :
{
OSTIMER3_LOAD_REG = CounterValue;
break;
}
case WDGTIMER :
{
WDGTIMER_LOAD_REG = (UWORD16)CounterValue;
break;
}
default :
{
return((BOOL)NOT_OK);
}
}
return((BOOL)OK);
}
//---------------------------------------------------------------------
// NAME : TIME_ReadTimerValue -
// DESCRIPTION : Read the value into the timer Register -
// SYNOPSYS : UWORD32 TIME_ReadTimerValue(TIMER_NAME_t WhichTimer); -
// PARAMETERS :
// WhichTimer = Timer name to read -
// OSTIMER1
// OSTIMER2
// OSTIMER3
// WDGTIMER
// -
// RETURN VALUE: Value of the register
// LIMITATIONS : None -
//---------------------------------------------------------------------
UWORD32 TIME_ReadTimerValue(TIMER_NAME_t WhichTimer)
{
switch (WhichTimer)
{
case OSTIMER1 :
{
return(OSTIMER1_READ_REG);
}
case OSTIMER2 :
{
return(OSTIMER2_READ_REG);
}
case OSTIMER3 :
{
return(OSTIMER3_READ_REG);
}
case WDGTIMER :
{
return((UWORD32)WDGTIMER_READ_REG);
}
default :
{
return(ERROR32_VAL);
}
}
}
//---------------------------------------------------------------------
// NAME : TIME_ReadLoadTimerValue -
// DESCRIPTION : Read the Setup Counter value into Load Register -
// SYNOPSYS : UWORD32 TIME_ReadTimerValue(TIMER_NAME_t WhichTimer); -
// PARAMETERS :
// WhichTimer = Timer name to read -
// OSTIMER1
// OSTIMER2
// OSTIMER3
// WDGTIMER
// -
// RETURN VALUE: Value of the register
// -
//---------------------------------------------------------------------
UWORD32 TIME_ReadLoadTimerValue(TIMER_NAME_t WhichTimer)
{
switch (WhichTimer)
{
case OSTIMER1 :
{
return(OSTIMER1_READ_LOAD_REG);
}
case OSTIMER2 :
{
return(OSTIMER2_READ_LOAD_REG);
}
case OSTIMER3 :
{
return(OSTIMER3_READ_LOAD_REG);
}
case WDGTIMER :
{
return((UWORD32)WDGTIMER_READ_REG);//wdg timerhas not the same structure than ostimer
}
default :
{
return(ERROR32_VAL);
}
}
}
//---------------------------------------------------------------------
// NAME : TIME_ReadPtv
// DESCRIPTION : Read the PTV value in cntl register
// SYNOPSYS : UWORD32 TIME_ReadPtv(TIMER_NAME_t WhichTimer); -
// PARAMETERS :
// WhichTimer = Timer name to read -
// OSTIMER1
// OSTIMER2
// OSTIMER3
// WDGTIMER
// -
// RETURN VALUE: Value of the PTV
// LIMITATIONS : None -
//---------------------------------------------------------------------
UWORD32 TIME_ReadPtv(TIMER_NAME_t WhichTimer)
{
switch (WhichTimer)
{
case OSTIMER1 :
{
return((OSTIMER1_CNTL_USR_REG & OSTIMER_PTV_MASK)>>OSTIMER_PTV_BITPOS);
}
case OSTIMER2 :
{
return((OSTIMER2_CNTL_USR_REG & OSTIMER_PTV_MASK)>>OSTIMER_PTV_BITPOS);
}
case OSTIMER3 :
{
return((OSTIMER3_CNTL_USR_REG & OSTIMER_PTV_MASK)>>OSTIMER_PTV_BITPOS);
}
case WDGTIMER :
{
return((UWORD32)((WDGTIMER_CNTL_USR_REG & WDGTIMER_PTV_MASK)>>OSTIMER_PTV_BITPOS));
}
default :
{
return(ERROR32_VAL);
}
}
}
//------------------------------------------------------------------
// NAME : TIME_StartTimer -
// TIME_StopTimer
// DESCRIPTION : Start and Stop the timer -
// SYNOPSYS : void TIME_StartTimer(void); -
// void TIME_StopTimer (void); -
// PARAMETERS : TIMER_NAME_t TimerName -
// OSTIMER1
// OSTIMER2
// OSTIMER3
// WDGTIMER
// RETURN VALUE: None -
// LIMITATIONS : None -
//------------------------------------------------------------------
BOOL TIME_StartTimer(TIMER_NAME_t TimerName)
{
switch (TimerName)
{
case OSTIMER1 :
{
SetBit(OSTIMER1_CNTL_SUP_REG,OSTIMER_ST_MASK);
break;
}
case OSTIMER2 :
{
SetBit(OSTIMER2_CNTL_SUP_REG,OSTIMER_ST_MASK);
break;
}
case OSTIMER3 :
{
SetBit(OSTIMER3_CNTL_SUP_REG,OSTIMER_ST_MASK);
break;
}
case WDGTIMER :
{
SetBit(WDGTIMER_CNTL_SUP_REG,WDGTIMER_ST_MASK);
break;
}
default :
{
return((BOOL)NOT_OK);
}
}
return((BOOL)OK);
}
BOOL TIME_StopTimer(TIMER_NAME_t TimerName)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -