-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmicros_1us.cpp
More file actions
151 lines (119 loc) · 4 KB
/
Copy pathmicros_1us.cpp
File metadata and controls
151 lines (119 loc) · 4 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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
---------------------------
micros_1us
author : vincent jaunet
date : 10-01-2013
---------------------------
Description :
Replacements for the Arduino micros() and millis function with better than
4us resolution. Using the Timer 2 so that other tlibrairies using
micros/millis/delay etc would still work.
Copyright (c) <2017> <Vincent Jaunet>
License GNU Lesser GPL
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#include "micros_1us.h"
#define clockCyclesPerMicrosecond() ( F_CPU / 1000000L )
#define clockCyclesToMicroseconds(a) ( (a) / clockCyclesPerMicrosecond() )
#define microsecondsToClockCycles(a) ( (a) * clockCyclesPerMicrosecond() )
// the prescaler is set so that timer2 ticks every 8 clock cycles, and the
// the overflow handler is called every 256 ticks.
#define MICROS_PER_TIMER2_OVERFLOW (clockCyclesToMicroseconds(8 * 256))
// the whole number of milliseconds per timer2 overflow
#define MILLIS_INCVAL (MICROS_PER_TIMER2_OVERFLOW / 1000)
// the fractional number of milliseconds per timer2 overflow. we shift right
// by three to fit these numbers into a byte. (for the clock speeds we care
// about - 8 and 16 MHz - this doesn't lose precision.)
#define FRACT_INCVAL ((MICROS_PER_TIMER2_OVERFLOW % 1000) >> 3)
#define FRACT_MAXVAL (1000 >> 3)
volatile unsigned long timer2_overflow_count = 0;
volatile unsigned long timer2_millis = 0;
static unsigned char timer2_fract = 0;
ISR(TIMER2_OVF_vect)
{
// copy these to local variables so they can be stored in registers
// (volatile variables must be read from memory on every access)
unsigned long m = timer2_millis;
unsigned char f = timer2_fract;
m += MILLIS_INCVAL;
f += FRACT_INCVAL;
if (f >= FRACT_MAXVAL) {
f -= FRACT_MAXVAL;
m += 1;
}
timer2_fract = f;
timer2_millis = m;
timer2_overflow_count++;
}
unsigned long m1us_millis()
{
unsigned long m;
uint8_t oldSREG = SREG;
// disable interrupts while we read timer2_millis or we might get an
// inconsistent value (e.g. in the middle of a write to timer2_millis)
cli();
m = timer2_millis;
SREG = oldSREG;
return m;
}
unsigned long m1us_micros() {
unsigned long m;
uint8_t oldSREG = SREG, t;
cli();
m = timer2_overflow_count;
#if defined(TCNT2)
t = TCNT2;
#elif defined(TCNT2L)
t = TCNT2L;
#else
#error TIMER 2 not defined
#endif
#ifdef TIFR2
if ((TIFR2 & _BV(TOV2)) && (t < 255))
m++;
#else
if ((TIFR & _BV(TOV2)) && (t < 255))
m++;
#endif
SREG = oldSREG;
return ((m << 8) + t)/2;
}
void m1us_init()
{
// set timer 0 mode
#if defined(TCCR2A)
TCCR2A = 0;
#else
#error Timer 2 normal mode error
#endif
// set timer 2 prescale factor to 8
#if defined(TCCR2B) && defined(CS20) && defined(CS21) && defined(CS22)
cbi(TCCR2B, CS20);
sbi(TCCR2B, CS21);
cbi(TCCR2B, CS22);
#else
#error Timer 2 prescale factor 64 not set correctly
#endif
// enable timer 0 overflow interrupt
#if defined(TIMSK2) && defined(TOIE2)
sbi(TIMSK2, TOIE2);
#else
#error Timer 2 overflow interrupt not set correctly
#endif
// //enalbe interrupt again
// cli();
}