/** * Remove the timer in Chained list of timers. * This service may panic if: * tmr parameter is is null, invalid, or timer is not running. * * Authorized execution levels: task, fiber, ISR * * @param tmr : handler on the timer (value returned by timer_create ). * */ void timer_stop(T_TIMER tmr) { T_TIMER_LIST_ELT *timer = (T_TIMER_LIST_ELT *)tmr; bool doSignal = false; if (NULL != timer) { int flags = irq_lock(); /* if timer is active */ if (timer->desc.status == E_TIMER_RUNNING) { #ifdef __DEBUG_OS_ABSTRACTION_TIMER _log( "\nINFO : timer_stop : stopping timer at addr = 0x%x", (uint32_t)timer); #endif /* remove the timer */ if (g_CurrentTimerHead == timer) { doSignal = true; } remove_timer(timer); irq_unlock(flags); if (doSignal) { /* the next timer to expire was removed, unblock timer_task to assess the change */ signal_timer_task(); } } else { /* tmr is not running */ irq_unlock(flags); } } else { /* tmr is not a timer from g_TimerPool_elements */ panic(E_OS_ERR); } }
/** * Create a timer object. * This service may panic if err parameter is null and: * callback parameter is null, or * no timer is available. * * Authorized execution levels: task, fiber, ISR * * @param callback: pointer to the function to be executed. * @param privData: pointer to data that shall be passed to the callback * @param delay: number of milliseconds between function executions * @param repeat: specifies if the timer shall be re-started after each execution of the callback * @param startup : specifies if the timer shall be start immediately * @param err (out): execution status: * E_OS_OK : callback is programmed * E_OS_ERR: no timer is available, or callback parameter is null * * @return Handler on the timer, NULL if the service fails (e.g. no available * timer or callback is a null pointer). */ T_TIMER timer_create(T_ENTRY_POINT callback, void *privData, uint32_t delay, bool repeat, bool startup, OS_ERR_TYPE *err) { T_TIMER_LIST_ELT *timer = NULL; /* check input parameters */ if ((NULL != callback) && (OS_WAIT_FOREVER != delay)) { /* delay should be set to 0 if startup flag is false * otherwise delay should be a positive value if startup flag is true */ if (((0 < delay) && (true == startup)) || ((0 <= delay) && (false == startup))) { /* find and reserve a timer resource from g_TimerPool_elements */ /* rem: timer points to an element from the global g_TimerPool_elements */ timer = g_TimerPool_alloc(); if (timer != NULL) { /* initialize timer descriptor */ timer->desc.callback = callback; timer->desc.data = privData; timer->desc.delay = delay; timer->desc.repeat = repeat; timer->desc.status = E_TIMER_READY; /* insert timer in the list of active timers */ if (startup) { int flags; timer->desc.expiration = get_uptime_ms() + timer->desc.delay; flags = irq_lock(); add_timer(timer); irq_unlock(flags); if (g_CurrentTimerHead == timer) { /* new timer is the next to expire, unblock timer_task to assess the change */ signal_timer_task(); } } #ifdef __DEBUG_OS_ABSTRACTION_TIMER _log( "\nINFO : timer_create : new timer will expire at %u (now = %u ) - addr = 0x%x", timer->desc.expiration, get_uptime_ms(), (uint32)timer); #endif error_management(err, E_OS_OK); } else { /* all timers from the pool are already being used */ error_management(err, E_OS_ERR_NO_MEMORY); } } else { /* delay and startup parameter are inconsistent */ error_management(err, E_OS_ERR); } } else { /* callback == NULL or delay == 0 : at least one parameter is invalid */ error_management(err, E_OS_ERR); } return (T_TIMER)timer; }
/** * start the timer. * This service may panic if err parameter is null and: * no timer is available or timer is running . * * Authorized execution levels: task, fiber, ISR * * @param tmr : handler on the timer (value returned by timer_create ). * @param err (out): execution status: */ void timer_start(T_TIMER tmr, uint32_t delay, OS_ERR_TYPE* err) { T_TIMER_LIST_ELT* timer = (T_TIMER_LIST_ELT*) tmr; OS_ERR_TYPE localErr = E_OS_OK; /* if timer is created */ if (NULL != timer) { if(timer->desc.status == E_TIMER_READY) { /* if timer parameter are valid */ if ((NULL != timer->desc.callback) && (0 < delay)) { #ifdef __DEBUG_OS_ABSTRACTION_TIMER _log ("\nINFO : timer_start : starting timer "); #endif /* Update expiration time */ timer->desc.delay = CONVERT_MS_TO_TICKS(delay); timer->desc.expiration = _GET_TICK() + timer->desc.delay; disable_scheduling(); /* add the timer */ add_timer(timer); /* new timer is the next to expire, unblock timer_task to assess the change */ if (g_CurrentTimerHead == timer) { signal_timer_task(); } enable_scheduling(); } else { /* timer is not valid */ localErr = E_OS_ERR; } } else if(timer->desc.status == E_TIMER_RUNNING) { localErr = E_OS_ERR_BUSY; } } else { /* tmr is not a timer from g_TimerPool_elements */ localErr = E_OS_ERR; } error_management(err, localErr); }
/** * Remove the timer in Chained list of timers. * This service may panic if err parameter is null and: * tmr parameter is is null, invalid, or timer is not running. * * Authorized execution levels: task, fiber, ISR * * @param tmr : handler on the timer (value returned by timer_create ). * @param err (out): execution status: */ void timer_stop(T_TIMER tmr, OS_ERR_TYPE* err) { T_TIMER_LIST_ELT* timer = (T_TIMER_LIST_ELT*)tmr ; OS_ERR_TYPE localErr = E_OS_OK; bool doSignal = false; if ( NULL != timer ) { /* if timer is active */ if (timer->desc.status == E_TIMER_RUNNING) { #ifdef __DEBUG_OS_ABSTRACTION_TIMER _log ("\nINFO : timer_stop : stopping timer at addr = 0x%x", (uint32_t) timer); #endif /* remove the timer */ disable_scheduling(); if ( g_CurrentTimerHead == timer ) { doSignal = true ; } remove_timer(timer); if ( doSignal ) { /* the next timer to expire was removed, unblock timer_task to assess the change */ signal_timer_task(); } enable_scheduling(); } else { /* tmr is not running */ localErr = E_OS_OK; } } else { /* tmr is not a timer from g_TimerPool_elements */ localErr = E_OS_ERR; } error_management(err, localErr); }
/** * start the timer. * This service may panic if err parameter is null and: * no timer is available or timer is running . * * Authorized execution levels: task, fiber, ISR * * @param tmr : handler on the timer (value returned by timer_create ). * @param err (out): execution status: */ void timer_start(T_TIMER tmr, uint32_t delay, OS_ERR_TYPE *err) { T_TIMER_LIST_ELT *timer = (T_TIMER_LIST_ELT *)tmr; OS_ERR_TYPE localErr = E_OS_OK; /* if timer is created */ if (NULL != timer) { int flags = irq_lock(); if (timer->desc.status == E_TIMER_READY) { /* if timer parameter are valid */ if ((NULL != timer->desc.callback) && (0 < delay)) { #ifdef __DEBUG_OS_ABSTRACTION_TIMER _log("\nINFO : timer_start : starting timer "); #endif /* Update expiration time */ timer->desc.delay = delay; timer->desc.expiration = get_uptime_ms() + timer->desc.delay; /* add the timer */ add_timer(timer); irq_unlock(flags); /* new timer is the next to expire, unblock timer_task to assess the change */ if (g_CurrentTimerHead == timer) { signal_timer_task(); } } else { /* timer is not valid */ localErr = E_OS_ERR; } } else if (timer->desc.status == E_TIMER_RUNNING) { irq_unlock(flags); localErr = E_OS_ERR_BUSY; if (err != NULL) *err = localErr; return; } } else { /* tmr is not a timer from g_TimerPool_elements */ localErr = E_OS_ERR; } error_management(err, localErr); }