From 796c8affdd0b16c5286ed8b01a4871736ae719c8 Mon Sep 17 00:00:00 2001 From: cyz <181662417@qq.com> Date: Wed, 9 Jun 2021 12:35:52 +0800 Subject: [PATCH] =?UTF-8?q?rt=5Fhw=5Fus=5Fdelay=E7=9A=84=E5=AE=9E=E7=8E=B0?= =?UTF-8?q?=E6=9C=89=E9=97=AE=E9=A2=98=EF=BC=8C=E9=92=88=E5=AF=B9=E8=BF=99?= =?UTF-8?q?=E4=B8=80=E9=97=AE=E9=A2=98=E8=BF=9B=E8=A1=8C=E4=BA=86=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- bsp/stm32/libraries/HAL_Drivers/drv_common.c | 32 +++++++++++++++----- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/bsp/stm32/libraries/HAL_Drivers/drv_common.c b/bsp/stm32/libraries/HAL_Drivers/drv_common.c index 74007b6e88..61171e7c8d 100644 --- a/bsp/stm32/libraries/HAL_Drivers/drv_common.c +++ b/bsp/stm32/libraries/HAL_Drivers/drv_common.c @@ -113,16 +113,32 @@ void _Error_Handler(char *s, int num) */ void rt_hw_us_delay(rt_uint32_t us) { - rt_uint32_t start, now, delta, reload, us_tick; - start = SysTick->VAL; - reload = SysTick->LOAD; - us_tick = SystemCoreClock / 1000000UL; - do + rt_uint32_t ticks; + rt_uint32_t told, tnow, tcnt = 0; + rt_uint32_t reload = SysTick->LOAD; + + ticks = us * reload / (1000000 / RT_TICK_PER_SECOND); + told = SysTick->VAL; + while (1) { - now = SysTick->VAL; - delta = start >= now ? start - now : reload + start - now; + tnow = SysTick->VAL; + if (tnow != told) + { + if (tnow < told) + { + tcnt += told - tnow; + } + else + { + tcnt += reload - tnow + told; + } + told = tnow; + if (tcnt >= ticks) + { + break; + } + } } - while (delta < us_tick * us); } /**