-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlptmrcnt.ino
64 lines (51 loc) · 1.62 KB
/
lptmrcnt.ino
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
// teensy adclptmr use LPTMR0 counter (pin 13)
// version 1 count with rollover like FreqCount but with ISR
// jumper PWM 23 to pin 13 for clock source
// https://forum.pjrc.com/threads/40782-LPTMR-on-the-Teensy-3-1-3-2-3-5-3-6
#define PRREG(x) Serial.print(#x" 0x"); Serial.println(x,HEX)
uint32_t prev;
volatile uint32_t msw;
void lptmr_isr(void)
{
LPTMR0_CSR |= LPTMR_CSR_TCF; // clear, first thing else ticks doubles?
msw++;
}
void lptmr_init() {
CORE_PIN13_CONFIG = PORT_PCR_MUX(3); // pin 13 alt for LPTMR
SIM_SCGC5 |= SIM_SCGC5_LPTIMER; // Enable Low Power Timer Access
LPTMR0_CSR = 0; // Disable
LPTMR0_PSR = LPTMR_PSR_PBYP; // Bypass prescaler/filter
LPTMR0_CMR = 0xffff; // counts til interrupt
LPTMR0_CSR = LPTMR_CSR_TIE | // Interrupt Enable
LPTMR_CSR_TPS(2) | // Pin: 0=CMP0, 1=xtal, 2=pin13
LPTMR_CSR_TMS; // Mode Select, 0=timer, 1=counter
LPTMR0_CSR |= LPTMR_CSR_TEN; // Enable
NVIC_ENABLE_IRQ(IRQ_LPTMR);
}
IntervalTimer it1;
volatile uint32_t dataReady, ticks;
void it1cb() {
LPTMR0_CNR = 1; // dummy prime
ticks = LPTMR0_CNR | (msw << 16); // ? need atomic
dataReady = 1;
}
void setup() {
Serial.begin(9600);
while (!Serial);
delay(2000);
analogWriteFrequency(23, 30000000);
analogWrite(23, 128);
it1.begin(it1cb, 1000000); // every second
lptmr_init();
PRREG(LPTMR0_CSR);
PRREG(LPTMR0_CMR);
}
void loop() {
while (1) { // avoid yield jitter
if (dataReady) {
Serial.println(ticks - prev);
prev = ticks;
dataReady = 0;
}
}
}