系统时钟不准确,久了会死机的疑问?

-小仙- 2009-10-13 11:54:03
大家好:
遇到了一个问题,向各位请教:
我在APP中用SetTimer(1,1000,NULL),然后在Ontimer做响应处理,目的是一秒钟一次操作,但我的系统Timer设置有问题: Timer时快时慢,而且过一段时间后,就会死机。不知道这是什么原因。-
---------------------
我google了一下:
http://www.cnemb.com/forum/read.php?tid=23044 这个帖子和我的问题一样,

我的BSP中是把Timer4预留给系统的,
这个是我的代码:
static DWORD dwPartialCurMSec = 0;		// Keep CPU-specific sub-millisecond leftover.
void
OEMIdle( DWORD dwIdleParam )
{
DWORD dwIdleMSec;
DWORD dwPrevMSec = *pCurMSec;

// Use for 64-bit math
ULARGE_INTEGER currIdle = { curridlelow, curridlehigh };

if ((int) (dwIdleMSec = dwReschedTime - dwPrevMSec) <= 0)
{
return; // already time to wakeup
}

// just idle till tick if profiling or running iltiming
if (bProfileTimerRunning || fIntrTime) // fIntrTime : Interrupt Latency timeing.
{
// idle till end of 'tick'
CPUEnterIdle(dwIdleParam);

// Update global idle time and return
currIdle.QuadPart += RESCHED_PERIOD;
curridlelow = currIdle.LowPart;
curridlehigh = currIdle.HighPart;

return;
}
RETAILMSG(1,(TEXT("OEMIdle ->RESCHED_PERIOD=%d \r\n"),RESCHED_PERIOD));
//
// Since OEMIdle( ) is being called in the middle of a normal reschedule
// period, CurMSec, dwPartialCurMSec, and CurTicks need to be updated accordingly.
// Once we reach this point, we must re-program the timer (if we ever did)
// because dwPartialCurMSec will be modified in the next function call.
//
CPUGetSysTimerCountElapsed(RESCHED_PERIOD, pCurMSec, &dwPartialCurMSec, pCurTicks);

if ((int) (dwIdleMSec -= *pCurMSec - dwPrevMSec) > 0)
{
dwPrevMSec = *pCurMSec;

//
// The system timer may not be capable of arbitrary timeouts. Get the
// CPU-specific highest possible timeout available.
//
dwIdleMSec = CPUGetSysTimerCountMax(dwIdleMSec);

//
// Set the timer to wake up much later than usual, if needed.
//
CPUSetSysTimerCount(dwIdleMSec);
CPUClearSysTimerIRQ( );

//
// Enable wakeup on any interrupt, then go to sleep.
//
// DEBUGMSG(1, (TEXT("OEMIDle \r\n")));
CPUEnterIdle(dwIdleParam);
INTERRUPTS_OFF( );

//
// We're awake! The wake-up ISR (or any other ISR) has already run.
//
if (dwPrevMSec != *pCurMSec)
{
//
// We completed the full period we asked to sleep. Update the counters.
//
*pCurMSec += (dwIdleMSec - RESCHED_PERIOD); // Subtract resched period, because ISR also incremented.
CurTicks.QuadPart += (dwIdleMSec - RESCHED_PERIOD) * dwReschedIncrement;

currIdle.QuadPart += dwIdleMSec;
} else {
//
// Some other interrupt woke us up before the full idle period was
// complete. Determine how much time has elapsed.
//
currIdle.QuadPart += CPUGetSysTimerCountElapsed(dwIdleMSec, pCurMSec, &dwPartialCurMSec, pCurTicks);
}
}

// Re-arm counters
CPUSetSysTimerCount(RESCHED_PERIOD);
CPUClearSysTimerIRQ( );

// Update global idle time
curridlelow = currIdle.LowPart;
curridlehigh = currIdle.HighPart;

return;
}

------------------------------------------------------------------------------------------------------
void
InitClock(void)
{
volatile PWMreg *s2440PWM =(PWMreg *)PWM_BASE;
volatile INTreg *s2440INT = (INTreg *)INT_BASE;
DWORD ttmp;


// Timer4 as OS tick and disable it first.
s2440INT->rINTMSK |= BIT_TIMER4; // Mask timer4 interrupt.
s2440INT->rSRCPND = BIT_TIMER4; // Clear pending bit
s2440INT->rINTPND = BIT_TIMER4;

// Operating clock : PCLK=101500000 (101.5 Mhz)
// IMPORTANT : MUST CHECK S2440.H DEFINITIONS !!!!
s2440PWM->rTCFG1 = 0x8080; //;;; SHL
s2440PWM->rTCFG0 &= ~(0xff << 8); /* Prescaler 1's Value */
s2440PWM->rTCFG0 |= (PRESCALER << 8); // prescaler value=15

RETAILMSG(1, (_T("InitClock_PRESCALER=%d\r\n"),PRESCALER));
/* Timer4 Divider field clear */
s2440PWM->rTCFG1 = 0x11111; // ;;; SHL
s2440PWM->rTCFG1 &= ~(0xf << 16);

#if( SYS_TIMER_DIVIDER == D2 )
s2440PWM->rTCFG1 |= (D1_2 << 16); /* 1/2 */
#elif ( SYS_TIMER_DIVIDER == D4 )
s2440PWM->rTCFG1 |= (D1_4 << 16); /* 1/4 */
#elif ( SYS_TIMER_DIVIDER == D8 )
s2440PWM->rTCFG1 |= (D1_8 << 16); /* 1/8 */
#elif ( SYS_TIMER_DIVIDER == D16 )
s2440PWM->rTCFG1 |= (D1_16 << 16); /* 1/16 */
#endif
s2440PWM->rTCNTB4 = RESCHED_INCREMENT; //((RESCHED_PERIOD * OEM_CLOCK_FREQ) / 1000)
ttmp = s2440PWM->rTCON & (~(0xf << 20));

s2440PWM->rTCON = ttmp | (2 << 20); /* update TCVNTB4, stop */
s2440PWM->rTCON = ttmp | (1 << 20); /* one-shot mode, start */

// Number of timer counts for reschedule interval
dwReschedIncrement = RESCHED_INCREMENT;

// Set OEM timer count for 1 ms
OEMCount1ms = OEM_COUNT_1MS;

// Set OEM clock frequency
OEMClockFreq = OEM_CLOCK_FREQ;

s2440INT->rINTMSK &= ~BIT_TIMER4;

return;
}



...全文
368 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
-小仙- 2009-10-15
  • 打赏
  • 举报
回复
什么是stubs?
Stubs are routines that do not contain executable code. They act as placeholders for functions that need to be fully implemented later.
When implementing stubs, you can leave comments that describe what you eventually need to do to add functionality to your function. By scanning existing sample OAL code, you can easily obtain the function header, such as return type and arguments, for each of these functions.
You can either create the stub functions from scratch or copy an existing boot loader or OAL that is of similar CPU type and board design, and then use #if 0 and #endif tags for the function contents.
To create a stub function
1、Obtain the function header definition for the function to be stubbed by referring to a sample OAL that supports similar hardware. The function header includes a return type and a series of function arguments.
2、From the function header definition, create an empty function implementation.
3、If the function header specifies a return type, add a placeholder return value to satisfy the compiler.
For example, if the function returns a Boolean value, add either return (TRUE); or return (FALSE); to satisfy the compiler. A successful function call is usually signaled by a TRUE or non-zero value, while failure is usually signaled by a FALSE or 0 value. However, this depends on the function and the data being returned.
The following code example shows the stub for the OEMReadData function:
BOOL OEMReadData (DWORD cbData, LPBYTE pbData) { return(TRUE); }
这个链接
http://www.cnemb.com/forum/read.php?tid=23044
上的解决办法是
“找到了问题出在哪里了,OEMIdle函数中的处理应该是有问题,用Stub代替之后正常了。 ”
stub 具体是什么,怎么用, 怎么用它来代替OEMIdle?
看文档的介绍有点费解,
-小仙- 2009-10-15
  • 打赏
  • 举报
回复
我想stubs 的意思应该是,将暂时不实现的function , 直接做return TRUE或return FALSE 处理,居然是这个意思! 汗,
//------------------------------------------------------------------------------
BOOL
OEMGetExtensionDRAM(
LPDWORD lpMemStart,
LPDWORD lpMemLen
)
{
return FALSE; // no extension DRAM
}
------------
-小仙- 2009-10-14
  • 打赏
  • 举报
回复
主要是APP中用到SetTimer了, 从InitClock(void)
中的
// Set OEM timer count for 1 ms
OEMCount1ms = OEM_COUNT_1MS;
可以看出 是1ms !

但真的好像是Tick 在变,
guopeixin 2009-10-14
  • 打赏
  • 举报
回复
如果上层有线程在runing,oem_idle不会使用到,可以不用理会var tick
-小仙- 2009-10-13
  • 打赏
  • 举报
回复
[Quote=引用 6 楼 skynet000 的回复:]
1.同意楼上的,是不是采用的vartick?可以用fixedtick试试
2.注意2440的Timer2、3、4的设置clock第一次分频是共用一个TCFG,会不会你在其他地方修改了这个TCFG?造成timer4不准??
[/Quote]

vartick and fixedticK?
这个没有研究过,请问可否在详细说下, 明天来看看
----
其他的设置,触摸屏用的是Timer1, 其他的Timer不知道哪里在用?
----

skynet000 2009-10-13
  • 打赏
  • 举报
回复
1.同意楼上的,是不是采用的vartick?可以用fixedtick试试
2.注意2440的Timer2、3、4的设置clock第一次分频是共用一个TCFG,会不会你在其他地方修改了这个TCFG?造成timer4不准??
guopeixin 2009-10-13
  • 打赏
  • 举报
回复
[Quote=引用 4 楼 trueman_onlyme 的回复:]
1. 系统时钟(system tick)概念
  
  系统时钟是内核需要的唯一中断(IRQ0),系统时钟每毫秒产生一个中断,当发生中断时内核在ISR中累计,到1000的倍数就是过了一秒钟。在处理系统时钟的ISR中不仅要累计计数,还要决定是否通知内核开始重新调度当前所有的线程。要实现一个OAL,系统时钟是第一个必须做的事。
  
  2. X86平台系统时钟中断的处理工作 系统时钟由InitClock函数负责初始化工作,一般是在OEMInit函数中调用。当发生中断时,ISR首先用下列语句累计计数:
  
  CurMSec += SYSTEM_TICK_MS; /////SYSTEM_TICK_MS = 1
  
  然后根据下列语句判断应该返回什么值:
  
  if ((int) (dwReschedTime – CurMSec) >= 0)
  return SYSINTR_RESCHED; ///重新调度
  else
  return SYSINTR_NOP; ///不再执行任何操作
  
  上述代码中全局变量dwReschedTime在schedule.c中定义,也就是由内核的调度模块决定在何时开始重新调度线程。CurMSec累计了从WindowsCE启动到当前总共产生了多少个system tick。实现系统时钟后还要实现OEMIdle函数,当没有线程准备运行时OEMIdle被调用,OEMIdle函数将CPU置于空闲模式,但在空闲模式下仍然要累计系统时钟。
-------以上资料来源--http://motion.chinaitlab.com/WINCE/30568.html-----

在http://www.cnemb.com/forum/read.php?tid=23044 该贴LZ说是OEMIdle函数的影响,导致时钟时快时慢,但现在不知道究竟是什么影响了, 继续研究
[/Quote]
采用动态tick的情况下,如果OEMIdle函数没有处理好,会影响到系统的tick
但是lz采用了动态tick么?
-小仙- 2009-10-13
  • 打赏
  • 举报
回复
1. 系统时钟(system tick)概念
  
  系统时钟是内核需要的唯一中断(IRQ0),系统时钟每毫秒产生一个中断,当发生中断时内核在ISR中累计,到1000的倍数就是过了一秒钟。在处理系统时钟的ISR中不仅要累计计数,还要决定是否通知内核开始重新调度当前所有的线程。要实现一个OAL,系统时钟是第一个必须做的事。
  
  2. X86平台系统时钟中断的处理工作 系统时钟由InitClock函数负责初始化工作,一般是在OEMInit函数中调用。当发生中断时,ISR首先用下列语句累计计数:
  
  CurMSec += SYSTEM_TICK_MS; /////SYSTEM_TICK_MS = 1
  
  然后根据下列语句判断应该返回什么值:
  
  if ((int) (dwReschedTime – CurMSec) >= 0)
  return SYSINTR_RESCHED; ///重新调度
  else
  return SYSINTR_NOP; ///不再执行任何操作
  
  上述代码中全局变量dwReschedTime在schedule.c中定义,也就是由内核的调度模块决定在何时开始重新调度线程。CurMSec累计了从WindowsCE启动到当前总共产生了多少个system tick。实现系统时钟后还要实现OEMIdle函数,当没有线程准备运行时OEMIdle被调用,OEMIdle函数将CPU置于空闲模式,但在空闲模式下仍然要累计系统时钟。
-------以上资料来源--http://motion.chinaitlab.com/WINCE/30568.html-----

http://www.cnemb.com/forum/read.php?tid=23044 该贴LZ说是OEMIdle函数的影响,导致时钟时快时慢,但现在不知道究竟是什么影响了, 继续研究
ppc_2008 2009-10-13
  • 打赏
  • 举报
回复
顶一下
-小仙- 2009-10-13
  • 打赏
  • 举报
回复
忘了说我的平台是:S3C2440+WINCE5.0
GARY 2009-10-13
  • 打赏
  • 举报
回复
帮顶,up

19,504

社区成员

发帖
与我相关
我的任务
社区描述
硬件/嵌入开发 嵌入开发(WinCE)
社区管理员
  • 嵌入开发(WinCE)社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧