status_code_t sw_timer_start(uint8_t timer_id, uint32_t timer_count, sw_timeout_type_t timeout_type, FUNC_PTR timer_cb, void *param_cb) { uint32_t now; uint32_t point_in_time; if (TOTAL_NUMBER_OF_SW_TIMERS <= timer_id || NULL == timer_cb) { return ERR_INVALID_ARG; } if (NULL != timer_array[timer_id].timer_cb) { /* * Timer is already running if the callback function of the * corresponding timer index in the timer array is not NULL. */ return ERR_TIMER_ALREADY_RUNNING; } now = gettime(); switch (timeout_type) { case SW_TIMEOUT_RELATIVE: { if ((timer_count > MAX_TIMEOUT) || (timer_count < MIN_TIMEOUT)) { return ERR_INVALID_ARG; } point_in_time = ADD_TIME(timer_count, now); } break; case SW_TIMEOUT_ABSOLUTE: { uint32_t timeout; timeout = SUB_TIME(timer_count, now); if ((timeout > MAX_TIMEOUT) || (timeout < MIN_TIMEOUT)) { return ERR_INVALID_ARG; } point_in_time = timer_count; } break; default: return ERR_INVALID_ARG; } start_absolute_timer(timer_id, point_in_time, timer_cb, param_cb); return STATUS_OK; }
/** * @brief Starts regular timer * * This function starts a regular timer and registers the corresponding * callback function to handle the timeout event. * * @param timer_id Timer identifier * @param timer_count Timeout in microseconds * @param timeout_type @ref TIMEOUT_RELATIVE / @ref TIMEOUT_ABSOLUTE * @param timer_cb Callback handler invoked upon timer expiry * @param param_cb Argument for the callback handler * * @return * - @ref PAL_TMR_INVALID_ID if the timer identifier is undefined, * - @ref MAC_INVALID_PARAMETER if the callback function for this timer is NULL or * timeout_type is invalid, * - @ref PAL_TMR_ALREADY_RUNNING if the timer is already running, * - @ref MAC_SUCCESS if timer is started, or * - @ref PAL_TMR_INVALID_TIMEOUT if timeout is not within the timeout range. */ retval_t pal_timer_start(uint8_t timer_id, uint32_t timer_count, timeout_type_t timeout_type, FUNC_PTR timer_cb, void *param_cb) { uint32_t now; uint32_t point_in_time; if (timer_id >= TOTAL_NUMBER_OF_TIMERS) { return PAL_TMR_INVALID_ID; } if (NULL == timer_cb) { return MAC_INVALID_PARAMETER; } if (NULL != timer_array[timer_id].timer_cb) { /* * Timer is already running if the callback function of the * corresponding timer index in the timer array is not NULL. */ return PAL_TMR_ALREADY_RUNNING; } now = gettime(); /* The timeout is pre scaled according to the clock period. */ timer_count = (uint32_t)(timer_count / CLOCK_PERIOD); switch (timeout_type) { case TIMEOUT_RELATIVE: { if ((timer_count > MAX_TIMEOUT) || (timer_count < MIN_TIMEOUT)) { return PAL_TMR_INVALID_TIMEOUT; } point_in_time = ADD_TIME(timer_count, now); } break; case TIMEOUT_ABSOLUTE: { uint32_t timeout; timeout = SUB_TIME(timer_count, now); if ((timeout > MAX_TIMEOUT) || (timeout < MIN_TIMEOUT)) { return PAL_TMR_INVALID_TIMEOUT; } point_in_time = timer_count; } break; default: return MAC_INVALID_PARAMETER; } start_absolute_timer(timer_id, point_in_time, timer_cb, param_cb); return MAC_SUCCESS; }
/** * @brief Start regular timer * * This function starts a regular timer and installs the corresponding * callback function to handle the timeout event. * * @param timer_id Timer identifier * @param timer_count Timeout in microseconds * @param timeout_type @ref TIMEOUT_RELATIVE / @ref TIMEOUT_ABSOLUTE * @param timer_cb Callback handler invoked upon timer expiry * @param param_cb Argument for the callback handler * * @return * - @ref INVALID_ID if the timer identifier is undefined, * - @ref INVALID_PARAMETER if the callback function for this timer is NULL or * timeout_type is invalid, * - @ref ALREADY_RUNNING if the timer is already running, * - @ref SUCCESS if timer is started, or * - @ref INVALID_TIMEOUT if timeout is not within timeout range. */ retval_t pal_timer_start(uint8_t timer_id, uint32_t timer_count, timeout_type_t timeout_type, void *timer_cb, void *param_cb) { uint32_t now; uint32_t point_in_time; uint32_t timeout; if (TOTAL_NUMBER_OF_TIMERS <= timer_id) { return PAL_TMR_INVALID_ID; } if (NULL == timer_cb) { return MAC_INVALID_PARAMETER; } if (NULL != timer_array[timer_id].timer_cb) { /* * Timer is already running if the callback function of the * corresponding timer index in the timer array is not NULL. */ return PAL_TMR_ALREADY_RUNNING; } /*To get the current system time*/ now = gettime(); switch(timeout_type) { /* Timeout Relative */ case TIMEOUT_RELATIVE: { if ((timer_count > MAX_TIMEOUT) || (timer_count < MIN_TIMEOUT)) { return PAL_TMR_INVALID_TIMEOUT; } point_in_time = ADD_TIME(timer_count, now); } break; /* Timeout Absolute */ case TIMEOUT_ABSOLUTE: { timeout = SUB_TIME(timer_count, now); if ((timeout > MAX_TIMEOUT) || (timeout < MIN_TIMEOUT)) { return PAL_TMR_INVALID_TIMEOUT; } point_in_time = timer_count; } break; default: return MAC_INVALID_PARAMETER; } /*Starting the absolute timer for the output compare to get fired*/ start_absolute_timer(timer_id, point_in_time, timer_cb, param_cb); return MAC_SUCCESS; }