|
现在我是用SysTick实现的,但是发现偶尔会出现返回的周期数比上一次小的情况!不知道是哪里出了问题,请大家帮我看一下,谢谢!
这是SysTick中断处理函数- /* sys tick handler */
- #define TICK_PERIOD_MS 256
- static unsigned long _clock_per_tick = 0;
- static volatile unsigned long _ms;
- void systick_handler(void)
- {
- _ms += TICK_PERIOD_MS;
- }
- /* get current CPU cycle */
- uint64_t get_cycle()
- {
- #ifdef DEBUG
- static int64_t last_cycle = 0;
- #endif
- uint32_t ms, ms_before, ms_after, st_cur, st_ctrl;
- int64_t cycle;
- ms_before = _ms;
- st_cur = HWREG(NVIC_ST_CURRENT);
- st_ctrl = HWREG(NVIC_ST_CTRL);
- ms_after = _ms;
- if ( (_clock_per_tick - st_cur) < 32768 )
- ms = ms_after;/* Maybe _ms has been changed after we first read it */
- else
- ms = ms_before;
- if ( (st_ctrl & NVIC_ST_CTRL_ENABLE) == 0 )
- return 0;
- if ( (st_ctrl & NVIC_ST_CTRL_COUNT) != 0 )
- ms += TICK_PERIOD_MS;
- cycle = (int64_t)(ms / TICK_PERIOD_MS) * (int64_t)_clock_per_tick
- + (int64_t)(_clock_per_tick - 1 - st_cur);
- #ifdef DEBUG
- if ( cycle < last_cycle ) {
- while ( 1 ) {};
- }
- last_cycle = cycle;
- #endif
- return cycle;
- }
复制代码 主程序中这样初始化SysTick- /* Set the clocking to run at 50MHz from the PLL. */
- SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_OSC_MAIN | SYSCTL_XTAL_8MHZ);
- /* Enable all processor interrupts. */
- IntPrioritySet(FAULT_SYSTICK, 0x20);
- IntPrioritySet(INT_ETH, 0xff);
- IntMasterEnable();
- IntEnable(FAULT_MPU);
- IntEnable(FAULT_BUS);
- IntEnable(FAULT_USAGE);
- /* Initialize SysTick */
- /* _clock_per_tick = (uint32_t)((uint64_t)SysCtlClockGet() * (uint64_t)TICK_PERIOD_MS / 1000); */
- _clock_per_tick = TICK_PERIOD_MS * (SysCtlClockGet() / 1000);
- SysTickPeriodSet(_clock_per_tick);
- SysTickEnable();
- SysTickIntEnable();
复制代码 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|