forked from keatis/dwt_delay
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdwt_delay.c
executable file
·46 lines (42 loc) · 1.47 KB
/
dwt_delay.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
* Simple microseconds delay routine, utilizingARM's DWT
* (Data Watchpoint and Trace Unit) and HAL library.
* Intended to use with gcc compiler, but can be easily edited
* for any other C compiler.
* Max K
*
*
* This file is part of DWT_Delay package.
* DWT_Delay is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License,
* or (at your option) any later version.
*
* us_delay is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
* the GNU General Public License for more details.
* http://www.gnu.org/licenses/.
*/
#include "stm32f1xx_hal.h" // change to whatever MCU you use
#include "dwt_delay.h"
void DWT_Init(void)
{
if (!(CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk)) {
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
}
}
/**
* Delay routine itself.
* Time is in microseconds (1/1000000th of a second), not to be
* confused with millisecond (1/1000th).
*
* @param uint32_t us Number of microseconds to delay for
*/
void DWT_Delay(uint32_t us) // microseconds
{
int32_t targetTick = DWT->CYCCNT + us * (SystemCoreClock/1000000);
while (DWT->CYCCNT <= targetTick);
}