-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDelay.cpp
58 lines (51 loc) · 1.14 KB
/
Delay.cpp
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
47
48
49
50
51
52
53
54
55
56
57
58
/*
* Delays.cpp
*
* Created on: Oct 18, 2019
* Author: Tata
*/
#include <Delay.h>
static int32_t initDone = 0;
Delay::Delay() {
// TODO Auto-generated constructor stub
if (!initDone)
{
// if (!(CoreDebug->DEMCR & CoreDebug_DEMCR_TRCENA_Msk))
{
CoreDebug->DEMCR |= CoreDebug_DEMCR_TRCENA_Msk;
DWT->CYCCNT = 0;
DWT->CTRL |= DWT_CTRL_CYCCNTENA_Msk;
}
initDone = 1; //initialize only once regardless of number of objects
}
}
Delay::~Delay() {
// TODO Auto-generated destructor stub
}
/*
* hns delay in 0.1 us (or 100ns) units
* eg. Delay.hns(47) means 4.7 us delay
*/
void Delay::hns(uint32_t hns) // hundreds of nanosec delay
{
uint32_t curDwt = DWT->CYCCNT;
if (hns > 0)
{
uint32_t tp = ( hns * (SystemCoreClock/1000000UL) + 1)/10;
while ( (DWT->CYCCNT - curDwt) < tp);
}
}
void Delay::us(uint32_t us) // microseconds
{
uint32_t curDwt = DWT->CYCCNT;
if (us > 0)
{
uint32_t tp = us * (SystemCoreClock/1000000UL);
while ( (DWT->CYCCNT - curDwt) < tp);
}
}
void Delay::ms(uint32_t ms) // microseconds
{
uint32_t m = ms;
while(m--) us(1000UL);
}