Skip to content

Commit 85a0aaa

Browse files
authored
Merge pull request #2381 from Guozhanxin/hw_us
[bsp][stm32] add rt_hw_us_delay
2 parents 6008be4 + f4e74cc commit 85a0aaa

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

bsp/stm32/libraries/HAL_Drivers/drv_common.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,41 @@ void _Error_Handler(char *s, int num)
8686
/* USER CODE END Error_Handler */
8787
}
8888

89+
/**
90+
* This function will delay for some us.
91+
*
92+
* @param us the delay time of us
93+
*/
94+
void rt_hw_us_delay(rt_uint32_t us)
95+
{
96+
rt_uint32_t ticks;
97+
rt_uint32_t told, tnow, tcnt = 0;
98+
rt_uint32_t reload = SysTick->LOAD;
99+
100+
ticks = us * reload / (1000000 / RT_TICK_PER_SECOND);
101+
told = SysTick->VAL;
102+
while (1)
103+
{
104+
tnow = SysTick->VAL;
105+
if (tnow != told)
106+
{
107+
if (tnow < told)
108+
{
109+
tcnt += told - tnow;
110+
}
111+
else
112+
{
113+
tcnt += reload - tnow + told;
114+
}
115+
told = tnow;
116+
if (tcnt >= ticks)
117+
{
118+
break;
119+
}
120+
}
121+
}
122+
}
123+
89124
/**
90125
* This function will initial STM32 board.
91126
*/

0 commit comments

Comments
 (0)