📄 ixosaltime.c
字号:
/** * @file IxOsalTime.c * * @brief OS-independant implementation for timer-related * functions. * * * Design Notes: * * @par * IXP400 SW Release version 2.1 * * -- Copyright Notice -- * * @par * Copyright (c) 2001-2005, Intel Corporation. * All rights reserved. * * @par * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions * are met: * 1. Redistributions of source code must retain the above copyright * notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. * 3. Neither the name of the Intel Corporation nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * * * @par * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * SUCH DAMAGE. * * * @par * -- End of Copyright Notice -- */#include "IxOsal.h"/* Define a warning threshhold */#define IX_OSAL_TIMER_WARNING_THRESHOLD (IX_OSAL_MAX_TIMERS - 20)/* Define 100 ms max callback time */#define IX_MAX_TIMER_CALLBACK_TIME (100000000)/* Define Timer struct */typedef struct{ BOOL inUse; BOOL isRepeating; UINT32 id; UINT32 priority; void *callbackParam; IxOsalTimeval period; IxOsalTimeval expires; IxOsalVoidFnVoidPtr callback;} IxOsalTimerRec;/* define IX_OSAL_MAX_TIMERS */#define IX_OSAL_MAX_TIMERS 100/* define two semaphore valures */#define IX_OSAL_TIMER_SEM_UNAVAILABLE 0#define IX_OSAL_TIMER_SEM_AVAILABLE 1/* define private date structs */PRIVATE IxOsalTimerRec ixOsalTimers[IX_OSAL_MAX_TIMERS];PRIVATE IxOsalSemaphore ixOsalTimerRecalcSem;PRIVATE IxOsalSemaphore ixOsalCriticalSectSem;PRIVATE char *ixOsalTimerThreadName = "tOSALTimer";PRIVATE UINT32 lastTimerId = 0;PRIVATE UINT32 ixOsalHigestTimeSlotUsed = 0;PRIVATE BOOL ixOsalThresholdErr = FALSE;PRIVATE UINT32 ixOsalTimerCbCnt = 0;PRIVATE BOOL ixOsalTimerInited = FALSE;/* * Private function definitions */PRIVATE IX_STATUScreateNewTimer (IxOsalVoidFnVoidPtr func, void *param, UINT32 priority, UINT32 interval, BOOL isRepeating, IxOsalTimer * pTimerId);PRIVATE IxOsalTimerRec *evaluateTimerPriority (IxOsalTimerRec * first, IxOsalTimerRec * second);PRIVATE IxOsalTimerRec *findNextTimeout (IxOsalTimeval now);PRIVATE void timerSleep (IxOsalTimerRec * nextTimer, IxOsalTimeval now);PRIVATE void rescheduleTimer (IxOsalTimerRec * nextTimer);PRIVATE voidcallTimerCallback (IxOsalVoidFnVoidPtr callback, void *callbackParam);int timerLoop (void);PRIVATE IX_STATUS timerInit (void);/* Public timeval functions */PUBLIC UINT32ixOsalTimevalToTicks (IxOsalTimeval tv){ UINT32 tickPerSecs = 0; UINT32 nanoSecsPerTick = 0; tickPerSecs = ixOsalSysClockRateGet (); nanoSecsPerTick = IX_OSAL_BILLION / tickPerSecs; return ((tv.secs * tickPerSecs) + (tv.nsecs / nanoSecsPerTick));}PUBLIC voidixOsalTicksToTimeval (UINT32 ticks, IxOsalTimeval * pTv){ UINT32 tickPerSecs = 0; UINT32 nanoSecsPerTick = 0; /* * Reset the time value */ pTv->secs = 0; pTv->nsecs = 0; tickPerSecs = ixOsalSysClockRateGet (); nanoSecsPerTick = IX_OSAL_BILLION / tickPerSecs; /* * value less than 1 sec */ if (tickPerSecs > ticks) /* value less then 1 sec */ { pTv->nsecs = ticks * nanoSecsPerTick; } else { pTv->secs = ticks / tickPerSecs; pTv->nsecs = (ticks % tickPerSecs) * nanoSecsPerTick; }}/* * Public timer functions */PUBLIC IX_STATUSixOsalSingleShotTimerSchedule (IxOsalTimer * timer, UINT32 period, UINT32 priority, IxOsalVoidFnVoidPtr callback, void *param){ IX_STATUS ixStatus = IX_FAIL; if (ixOsalTimerInited == FALSE) { /* * Init timer */ ixStatus = timerInit (); if (ixStatus != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "ixOsalSingleShotTimerSchedule: fail to init timer \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } ixOsalTimerInited = TRUE; } ixStatus = createNewTimer (callback, param, priority, period, FALSE, timer); if (ixStatus != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "ixOsalSingleShotTimerSchedule: fail to create timer \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } return IX_SUCCESS;}PUBLIC IX_STATUSixOsalRepeatingTimerSchedule (IxOsalTimer * timer, UINT32 period, UINT32 priority, IxOsalVoidFnVoidPtr callback, void *param){ IX_STATUS ixStatus = IX_FAIL; if (ixOsalTimerInited == FALSE) { /* * Init timer */ ixStatus = timerInit (); if (ixStatus != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "ixOsalRepeatingTimerSchedule: fail to init timer \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } ixOsalTimerInited = TRUE; } ixStatus = createNewTimer (callback, param, priority, period, TRUE, timer); if (ixStatus != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "ixOsalRepeatingTimerSchedule: fail to create timer \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } return IX_SUCCESS;}PUBLIC IX_STATUSixOsalTimerCancel (IxOsalTimer * timer){ UINT32 id; UINT32 i; IX_STATUS status = IX_FAIL; if (ixOsalTimerInited == FALSE) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "ixOsalTimerCancel: call schedule APIs first to start timer \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } id = *timer; status = ixOsalSemaphoreWait (&ixOsalCriticalSectSem, IX_OSAL_WAIT_FOREVER); if (status != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "ixOsalTimerCancel: fail to get semaphore \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } /* * NOTE : there is no need to get the timer callback thread to wake * up and recalculate. If this timer is the next to expire, then * the timer callback thread will wake up but not find any timers to * callback and will just go back to sleep again anyway. */ /* * NOTE2 : We cancel a timer by doing a linear search for the timer id. * This is not terribly efficient but is the safest way to ensure that * cancellations do not cancel the wrong timer by mistake. Also we * assume timer cancellation does not occur often. If the timer to * be cancelled is not found, an error is logged. */ for (i = 0; i < ixOsalHigestTimeSlotUsed; i++) { if (ixOsalTimers[i].inUse && ixOsalTimers[i].id == id) { ixOsalTimers[i].inUse = FALSE; break; } } status = ixOsalSemaphorePost (&ixOsalCriticalSectSem); if (status != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "ixOsalTimerCancel: fail to free semaphore \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } return IX_SUCCESS;}voidixOsalTimersShow (void){ UINT32 i = 0; UINT32 count = 0; if (ixOsalTimerInited == FALSE) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "ixOsalTimersShow: call schedule APIs first to start timer \n", 0, 0, 0, 0, 0, 0); return; } ixOsalLog (IX_OSAL_LOG_LVL_USER, IX_OSAL_LOG_DEV_STDOUT, "Timers\n", 0, 0, 0, 0, 0, 0); for (i = 0; i < ixOsalHigestTimeSlotUsed; i++) { if (ixOsalTimers[i].inUse == TRUE) { ixOsalLog (IX_OSAL_LOG_LVL_USER, IX_OSAL_LOG_DEV_STDOUT, "id=%d, repeat=%d, priority=%d\n", ixOsalTimers[i].id, ixOsalTimers[i].isRepeating, ixOsalTimers[i].priority, 0, 0, 0); count++; } } ixOsalLog (IX_OSAL_LOG_LVL_USER, IX_OSAL_LOG_DEV_STDOUT, "total=%d\n", count, 0, 0, 0, 0, 0); ixOsalLog (IX_OSAL_LOG_LVL_USER, IX_OSAL_LOG_DEV_STDOUT, "num called=%u\n", ixOsalTimerCbCnt, 0, 0, 0, 0, 0); ixOsalLog (IX_OSAL_LOG_LVL_USER, IX_OSAL_LOG_DEV_STDOUT, "Timer threshold reached Error Flag: %d\n", ixOsalThresholdErr, 0, 0, 0, 0, 0);}/* Private functions only used in this file */PRIVATE IX_STATUStimerInit (void){ IxOsalThread taskId; IX_STATUS ixStatus; IxOsalThreadAttr timerThreadAttr; ixStatus = ixOsalSemaphoreInit (&ixOsalCriticalSectSem, IX_OSAL_TIMER_SEM_AVAILABLE); if (ixStatus != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "Error creating critical section semaphore\n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } ixStatus = ixOsalSemaphoreInit (&ixOsalTimerRecalcSem, IX_OSAL_TIMER_SEM_AVAILABLE); if (ixStatus != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "Error creating timer recalc semaphore\n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } timerThreadAttr.stackSize = 10 * 1024; timerThreadAttr.priority = 90; timerThreadAttr.name = ixOsalTimerThreadName; ixStatus = ixOsalThreadCreate (&taskId, &timerThreadAttr, (IxOsalVoidFnVoidPtr) timerLoop, NULL); if (ixStatus != IX_SUCCESS) { ixOsalLog (IX_OSAL_LOG_LVL_ERROR, IX_OSAL_LOG_DEV_STDOUT, "timerInit: fail to create timer thread. \n", 0, 0, 0, 0, 0, 0); return IX_FAIL; } ixStatus = ixOsalThreadStart (&taskId);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -