From c2e15e003c60fd0aeb412dbc7c6deb1c1f55a0ef Mon Sep 17 00:00:00 2001 From: Meco Man <920369182@qq.com> Date: Tue, 29 Dec 2020 00:08:24 +0800 Subject: [PATCH] add function rt_hw_1ms_tick_get() --- bsp/stm32/libraries/HAL_Drivers/drv_common.c | 2 +- components/net/lwip-1.4.1/src/arch/sys_arch.c | 3 ++- components/net/lwip-2.0.2/src/arch/sys_arch.c | 3 ++- components/net/lwip-2.1.2/src/arch/sys_arch.c | 3 ++- include/rthw.h | 5 +++++ src/clock.c | 17 +++++++++++++++++ 6 files changed, 29 insertions(+), 4 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_common.c b/bsp/stm32/libraries/HAL_Drivers/drv_common.c index f150d81fef..c6fb516b0e 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_common.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_common.c @@ -58,7 +58,7 @@ void SysTick_Handler(void) uint32_t HAL_GetTick(void) { - return rt_tick_get() * 1000 / RT_TICK_PER_SECOND; + return rt_hw_1ms_tick_get(); } void HAL_SuspendTick(void) diff --git a/components/net/lwip-1.4.1/src/arch/sys_arch.c b/components/net/lwip-1.4.1/src/arch/sys_arch.c index 2392dde2fa..0919c29e01 100644 --- a/components/net/lwip-1.4.1/src/arch/sys_arch.c +++ b/components/net/lwip-1.4.1/src/arch/sys_arch.c @@ -32,6 +32,7 @@ */ #include +#include #include "lwip/sys.h" #include "lwip/opt.h" @@ -617,7 +618,7 @@ u32_t sys_jiffies(void) u32_t sys_now(void) { - return rt_tick_get() * (1000 / RT_TICK_PER_SECOND); + return rt_hw_1ms_tick_get(); } #ifdef RT_LWIP_PPP diff --git a/components/net/lwip-2.0.2/src/arch/sys_arch.c b/components/net/lwip-2.0.2/src/arch/sys_arch.c index 40c79011ec..1404628a60 100644 --- a/components/net/lwip-2.0.2/src/arch/sys_arch.c +++ b/components/net/lwip-2.0.2/src/arch/sys_arch.c @@ -32,6 +32,7 @@ */ #include +#include #include "lwip/sys.h" #include "lwip/opt.h" @@ -627,7 +628,7 @@ u32_t sys_jiffies(void) u32_t sys_now(void) { - return rt_tick_get() * (1000 / RT_TICK_PER_SECOND); + return rt_hw_1ms_tick_get(); } diff --git a/components/net/lwip-2.1.2/src/arch/sys_arch.c b/components/net/lwip-2.1.2/src/arch/sys_arch.c index 0633501bb6..8905765440 100644 --- a/components/net/lwip-2.1.2/src/arch/sys_arch.c +++ b/components/net/lwip-2.1.2/src/arch/sys_arch.c @@ -33,6 +33,7 @@ */ #include +#include #include "lwip/sys.h" #include "lwip/opt.h" @@ -641,7 +642,7 @@ u32_t sys_jiffies(void) u32_t sys_now(void) { - return rt_tick_get() * (1000 / RT_TICK_PER_SECOND); + return rt_hw_1ms_tick_get(); } #if MEM_OVERFLOW_CHECK || MEMP_OVERFLOW_CHECK diff --git a/include/rthw.h b/include/rthw.h index 89877faf90..c3960350b8 100644 --- a/include/rthw.h +++ b/include/rthw.h @@ -134,6 +134,11 @@ void rt_hw_exception_install(rt_err_t (*exception_handle)(void *context)); */ void rt_hw_us_delay(rt_uint32_t us); +/* + * provides a tick value ALWAYS in millisecond + */ +rt_tick_t rt_hw_1ms_tick_get(void); + #ifdef RT_USING_SMP typedef union { unsigned long slock; diff --git a/src/clock.c b/src/clock.c index 9fd276cf6f..778126fb23 100644 --- a/src/clock.c +++ b/src/clock.c @@ -13,6 +13,7 @@ * 2010-07-13 Bernard fix rt_tick_from_millisecond issue found by kuronca * 2011-06-26 Bernard add rt_tick_set function. * 2018-11-22 Jesven add per cpu tick + * 2020-12-29 Meco Man add function rt_hw_1ms_tick_get() */ #include @@ -116,5 +117,21 @@ rt_tick_t rt_tick_from_millisecond(rt_int32_t ms) } RTM_EXPORT(rt_tick_from_millisecond); +/** + * This function provides a tick value ALWAYS in millisecond + * + * @return 1ms-based tick + */ +RT_WEAK rt_tick_t rt_hw_1ms_tick_get(void) +{ +#if 1000 % RT_TICK_PER_SECOND == 0 + return rt_tick_get() * (1000U / RT_TICK_PER_SECOND); +#else + #warning "rt-thread cannot provide a correct 1ms-based tick any longer,\ + please redefine this function in another file by using a high-precision hard-timer." + return 0; +#endif +} + /**@}*/