//------------------------------------------------------------------------------ // // Function: OALTimerReduceSysTick // UINT32 OALTimerReduceSysTick(UINT32 count, UINT32 margin) { UINT32 rc, edge, compare; edge = OALTimerGetCount(); compare = OALTimerGetCompare(); // Update the countdown value OALTimerSetCompare(count); if (edge > margin) { // We are far enough from the interrupt that we should return the // number of ticks that would have occurred since the last tick based // on the new number of counts per tick. We will avoid the interrupt and // just start fresh. rc = (compare - edge) / count; OALTimerSetCount(count); } else { // We are close enough to the next interrupt that we will let the // interrupt occur. We will also return the number of counts that would // have happened since the last tick based on the new number of counts // per tick, but we will subtract one because another is just about to // happen. rc = (compare / count) - 1; } return rc; }
//------------------------------------------------------------------------------ // // Function: OALTimerExtendSysTick // VOID OALTimerExtendSysTick(UINT32 count, UINT32 margin, UINT32 offset) { UINT32 compare, edge; edge = OALTimerGetCount(); compare = OALTimerGetCompare(); if (edge > ((count - compare) + margin + offset)) { // We are far enough away from the interrupt that we should just // increase the time it will take to hit the next interrupt taking into // account the offset provided. OALTimerSetCount(edge - (count - offset - compare)); } OALTimerSetCompare(count); }
//------------------------------------------------------------------------------ // // Function: OALTimerRecharge // // This function recharge count/compare timer. In case that we are late more // than margin value we will use count as new counter base. Under // normal conditions previous compare value stored in global variable is used. // Using global variable instead reading compare register allows compensate // offset when we reduce system tick to value which can cause hazard. // VOID OALTimerRecharge(UINT32 period, UINT32 margin) { UINT32 count; count = OALTimerGetCount(); // Check if recharge was called in required margin if ((INT32)(count - OALTimerGetCompare() - margin) < 0) { // Yes, base new period on previous (no shift) g_timer.base = g_timer.compare; g_timer.compare += period; } else { // No, base new period on actual counter value (shift) g_timer.base = count; g_timer.compare = g_timer.base + period; } OALTimerSetCompare(g_timer.compare); }
//------------------------------------------------------------------------------ // // Function: OALTimerCountsSinceSysTick // INT32 OALTimerCountsSinceSysTick() { return OALTimerGetCompare() - OALTimerGetCount(); }