From 23c3761ef083cb8561e40f0519f0c746ebe3f602 Mon Sep 17 00:00:00 2001 From: mgq <646208823@qq.com> Date: Sun, 10 Apr 2022 21:17:58 +0800 Subject: [PATCH 1/2] =?UTF-8?q?=E5=AE=9A=E6=97=B6=E5=99=A8=E6=BA=A2?= =?UTF-8?q?=E5=87=BA=E6=A3=80=E6=B5=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- stimer.c | 26 ++++++++++++++++++++++++-- stimer.h | 3 ++- 2 files changed, 26 insertions(+), 3 deletions(-) diff --git a/stimer.c b/stimer.c index d08c605..3b09dba 100644 --- a/stimer.c +++ b/stimer.c @@ -9,9 +9,11 @@ #include "stimer.h" volatile static uint32_t _stimer_ticks = 0; ///< 定时器计数时基 +static uint32_t stimer_tick_last = 0; static stimer_t* _stimer_list = NULL; ///< 定时器列表 + /** * 定时器句柄 * @param timer 定时器句柄 @@ -31,6 +33,7 @@ int stimer_init(stimer_t* timer, const char* name, void (*timeout_callback)(void timer->callback = timeout_callback; timer->current_ticks = 0; timer->timeout_ticks = ticks; + timer->timeout_ticks_copy = ticks; return 0; } @@ -110,19 +113,38 @@ uint32_t stimer_getticks(const stimer_t* timer) void stimer_poll(void) { stimer_t* entry = _stimer_list; + uint32_t tmp_tick = _stimer_ticks; while (entry != NULL) { - if (((int)(_stimer_ticks - entry->current_ticks)) >= entry->timeout_ticks) + if(tmp_tick < stimer_tick_last) + { + uint32_t poll_interval = 0, remainder = 0; + /*计算了轮询间隔*/ + poll_interval = (0xFFFFFFFF - stimer_tick_last) + tmp_tick; + /*计算了剩余时间*/ + remainder = (stimer_tick_last - entry->current_ticks) + poll_interval; + /*计算新的超时时间*/ + entry->timeout_ticks = entry->timeout_ticks - remainder; + /*重置count*/ + entry->current_ticks = tmp_tick; + } + if (((int)(tmp_tick - entry->current_ticks)) >= entry->timeout_ticks) { if (entry->opt == STIMER_OPT_SINGLE) { stimer_stop(entry); } + + if(entry->timeout_ticks != entry->timeout_ticks_copy) + { + entry->timeout_ticks != entry->timeout_ticks_copy; + } entry->callback(entry->arg); - entry->current_ticks = _stimer_ticks; + entry->current_ticks = tmp_tick; } entry = entry->next; } + stimer_tick_last = tmp_tick; } /// 定时器时基函数 diff --git a/stimer.h b/stimer.h index cf1c00a..0363131 100644 --- a/stimer.h +++ b/stimer.h @@ -1,7 +1,7 @@ /* * stimer.h * - * Created on: 2021121 + * Created on: 2021年12月1日 * Author: hello */ @@ -17,6 +17,7 @@ typedef struct _stimer { const char* name; uint32_t timeout_ticks; + uint32_t timeout_ticks_copy; uint32_t current_ticks; void (*callback)(void* arg); int opt; -- Gitee From 0146eba40bbb3b49c003c02c9a65a8219f12ffc0 Mon Sep 17 00:00:00 2001 From: miaoguoqiang <646208823@qq.com> Date: Mon, 11 Apr 2022 09:04:01 +0000 Subject: [PATCH 2/2] update stimer.c. --- stimer.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/stimer.c b/stimer.c index 3b09dba..5d0cf45 100644 --- a/stimer.c +++ b/stimer.c @@ -137,7 +137,7 @@ void stimer_poll(void) if(entry->timeout_ticks != entry->timeout_ticks_copy) { - entry->timeout_ticks != entry->timeout_ticks_copy; + entry->timeout_ticks = entry->timeout_ticks_copy; } entry->callback(entry->arg); entry->current_ticks = tmp_tick; -- Gitee