forked from manitou48/teensy4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_count.ino
70 lines (57 loc) · 1.36 KB
/
gpt_count.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
65
66
67
68
69
70
// GPT1 counter like FreqCount
// external pin is 25 GPIO_AD_B0_13 ALT1 (backside)
// test with PWM pin 11 jumpered to 25
// FreqCount API
static inline void counter_init(void)
{
CCM_CCGR1 |= CCM_CCGR1_GPT(CCM_CCGR_ON) ; // enable GPT1 module
GPT1_CR = 0;
GPT1_SR = 0x3F; // clear all prior status
GPT1_CR = GPT_CR_CLKSRC(3);// | GPT_CR_FRR ;// 3 external clock
*(portConfigRegister(25)) = 1; // ALT 1
}
static inline void counter_start(void)
{
GPT1_CR |= GPT_CR_EN; // enable
}
static inline void counter_shutdown(void)
{
GPT1_CR = 0;
}
static inline uint32_t counter_read(void) // was uint16_t in FreqCount?
{
return GPT1_CNT;
}
static inline uint8_t counter_overflow(void)
{
return GPT1_SR & GPT_SR_ROV;
}
static inline void counter_overflow_reset(void)
{
GPT1_SR |= GPT_SR_ROV;
}
volatile uint32_t count_ready, count_output, count_prev;
void tmr_callback() {
uint32_t count = counter_read();
//track rollover ?
count_output = count - count_prev;
count_prev = count;
count_ready = 1;
}
IntervalTimer it1;
void setup() {
Serial.begin(9600);
while (!Serial);
delay(2000);
analogWriteFrequency(11, 1234); // test jumper 12 to 25
analogWrite(11, 128);
counter_init();
it1.begin(tmr_callback, 1000000); // us
counter_start();
}
void loop() {
if (count_ready) {
Serial.println(count_output);
count_ready = 0;
}
}