|
|
|
@ -15,7 +15,7 @@ static uint32_t _timer_ticks = 0;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Initializes the timer struct handle.
|
|
|
|
* @brief Initializes the timer struct handle.
|
|
|
|
* @param handle: the timer handle strcut.
|
|
|
|
* @param handle: the timer handle struct.
|
|
|
|
* @param timeout_cb: timeout callback.
|
|
|
|
* @param timeout_cb: timeout callback.
|
|
|
|
* @param timeout: delay to start the timer.
|
|
|
|
* @param timeout: delay to start the timer.
|
|
|
|
* @param repeat: repeat interval time.
|
|
|
|
* @param repeat: repeat interval time.
|
|
|
|
@ -25,20 +25,20 @@ static uint32_t _timer_ticks = 0;
|
|
|
|
void timer_init(struct Timer* handle, void (*timeout_cb)(void *arg), \
|
|
|
|
void timer_init(struct Timer* handle, void (*timeout_cb)(void *arg), \
|
|
|
|
uint32_t timeout, uint32_t repeat, void *arg)
|
|
|
|
uint32_t timeout, uint32_t repeat, void *arg)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
// memset(handle, sizeof(struct Timer), 0);
|
|
|
|
// memset(handle, 0, sizeof(struct Timer));
|
|
|
|
handle->timeout_cb = timeout_cb;
|
|
|
|
handle->timeout_cb = timeout_cb;
|
|
|
|
handle->timeout = timeout;
|
|
|
|
handle->timeout = timeout;
|
|
|
|
handle->repeat = repeat;
|
|
|
|
handle->repeat = repeat;
|
|
|
|
handle->cur_ticks = _timer_ticks;
|
|
|
|
handle->cur_ticks = _timer_ticks;
|
|
|
|
handle->cur_expired_time = handle->timeout;
|
|
|
|
handle->cur_expired_time = handle->timeout;
|
|
|
|
handle->arg = arg;
|
|
|
|
handle->arg = arg;
|
|
|
|
//printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u, timeout: %u\r\n",
|
|
|
|
//printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u, timeout: %u\r\n",
|
|
|
|
// handle->cur_ticks, handle->cur_expired_time, _timer_ticks, timeout);
|
|
|
|
// handle->cur_ticks, handle->cur_expired_time, _timer_ticks, timeout);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Start the timer work, add the handle into work list.
|
|
|
|
* @brief Start the timer work, add the handle into work list.
|
|
|
|
* @param btn: target handle strcut.
|
|
|
|
* @param handle: target handle struct.
|
|
|
|
* @retval 0: succeed. -1: already exist.
|
|
|
|
* @retval 0: succeed. -1: already exist.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
int timer_start(struct Timer* handle)
|
|
|
|
int timer_start(struct Timer* handle)
|
|
|
|
@ -48,7 +48,7 @@ int timer_start(struct Timer* handle)
|
|
|
|
while(target) {
|
|
|
|
while(target) {
|
|
|
|
if(target == handle) {
|
|
|
|
if(target == handle) {
|
|
|
|
return -1; //already exist.
|
|
|
|
return -1; //already exist.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target = target->next;
|
|
|
|
target = target->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
handle->next = head_handle;
|
|
|
|
handle->next = head_handle;
|
|
|
|
@ -59,7 +59,7 @@ int timer_start(struct Timer* handle)
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief Stop the timer work, remove the handle off work list.
|
|
|
|
* @brief Stop the timer work, remove the handle off work list.
|
|
|
|
* @param handle: target handle strcut.
|
|
|
|
* @param handle: target handle struct.
|
|
|
|
* @retval 0: succeed. -1: timer not exist.
|
|
|
|
* @retval 0: succeed. -1: timer not exist.
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
int timer_stop(struct Timer* handle)
|
|
|
|
int timer_stop(struct Timer* handle)
|
|
|
|
@ -74,10 +74,10 @@ int timer_stop(struct Timer* handle)
|
|
|
|
return 0; // found specified timer
|
|
|
|
return 0; // found specified timer
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
curr = &entry->next;
|
|
|
|
curr = &entry->next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
@ -90,18 +90,18 @@ void timer_loop(void)
|
|
|
|
struct Timer* target;
|
|
|
|
struct Timer* target;
|
|
|
|
|
|
|
|
|
|
|
|
for(target = head_handle; target; target = target->next) {
|
|
|
|
for(target = head_handle; target; target = target->next) {
|
|
|
|
/*
|
|
|
|
/*
|
|
|
|
More detail on tick-clock overflow, please see https://blog.csdn.net/szullc/article/details/115332326
|
|
|
|
More detail on tick-clock overflow, please see https://blog.csdn.net/szullc/article/details/115332326
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
if(_timer_ticks - target->cur_ticks >= target->cur_expired_time) {
|
|
|
|
if(_timer_ticks - target->cur_ticks >= target->cur_expired_time) {
|
|
|
|
//printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u\r\n",
|
|
|
|
//printf("cur_ticks: %u, cur_expired_time: %u, _timer_ticks: %u\r\n",
|
|
|
|
// target->cur_ticks, target->cur_expired_time, _timer_ticks);
|
|
|
|
// target->cur_ticks, target->cur_expired_time, _timer_ticks);
|
|
|
|
if(target->repeat == 0) {
|
|
|
|
if(target->repeat == 0) {
|
|
|
|
timer_stop(target);
|
|
|
|
timer_stop(target);
|
|
|
|
} else {
|
|
|
|
} else {
|
|
|
|
target->cur_ticks = _timer_ticks;
|
|
|
|
target->cur_ticks = _timer_ticks;
|
|
|
|
target->cur_expired_time = target->repeat;
|
|
|
|
target->cur_expired_time = target->repeat;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target->timeout_cb(target->arg);
|
|
|
|
target->timeout_cb(target->arg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
@ -110,7 +110,7 @@ void timer_loop(void)
|
|
|
|
/**
|
|
|
|
/**
|
|
|
|
* @brief background ticks, timer repeat invoking interval nms.
|
|
|
|
* @brief background ticks, timer repeat invoking interval nms.
|
|
|
|
* @param None.
|
|
|
|
* @param None.
|
|
|
|
* @retval None.
|
|
|
|
* @retval None
|
|
|
|
*/
|
|
|
|
*/
|
|
|
|
void timer_ticks(void)
|
|
|
|
void timer_ticks(void)
|
|
|
|
{
|
|
|
|
{
|
|
|
|
|