瀏覽代碼

!217 rt_hw_us_delay的实现有问题,针对这一问题进行了修改
Merge pull request !217 from cyz/cyz

bernard 3 年之前
父節點
當前提交
45b540d334
共有 1 個文件被更改,包括 24 次插入8 次删除
  1. 24 8
      bsp/stm32/libraries/HAL_Drivers/drv_common.c

+ 24 - 8
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);
 }
 
 /**