forked from tenbaht/sduino
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwiring-micros.c
More file actions
52 lines (43 loc) · 1.04 KB
/
wiring-micros.c
File metadata and controls
52 lines (43 loc) · 1.04 KB
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
#include "wiring-header.h"
/**
*/
unsigned long micros()
{
#ifdef USE_SPL
unsigned long m;
uint8_t t;
BEGIN_CRITICAL
m = timer4_overflow_count;
t = TIM4_GetCounter();
// check if a fresh update event is still pending
// if (TIM4->SR1 & 0x01)
if ((TIM4_GetFlagStatus(TIM4_IT_UPDATE)==SET) && (t < (T4PERIOD-1)))
m++;
END_CRITICAL
// return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
// return ((m*250+t) * (64/16) ); // FIXME: use calculated value
m *= T4PERIOD;
m += t;
// m <<= 2;
m *= ((T4PRESCALER_FACTOR*1000000L)/(F_CPU));
return m;
#else
unsigned long m;
uint8_t t;
BEGIN_CRITICAL
m = timer4_overflow_count;
t = TIM4->CNTR;
// check if a fresh update event is still pending
// if (TIM4->SR1 & 0x01)
if ((TIM4->SR1 & (uint8_t)TIM4_IT_UPDATE) && (t < (T4PERIOD-1)))
m++;
END_CRITICAL
// return ((m << 8) + t) * (64 / clockCyclesPerMicrosecond());
// return ((m*250+t) * (64/16) ); // FIXME: use calculated value
m *= T4PERIOD;
m += t;
// m <<= 2;
m *= ((T4PRESCALER_FACTOR*1000000L)/(F_CPU));
return m;
#endif
}