forked from manitou48/teensy4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgpt_isr.ino
53 lines (46 loc) · 1.36 KB
/
gpt_isr.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
// GPT ISR test, run at 1mhz from 24mhz clock, test either GPT1 or GPT2
// could do PWM with OCRn registers, but no GPT output pins on T4
volatile uint32_t ticks;
void gpt1_isr() {
GPT1_SR |= GPT_SR_OF3; // clear all set bits
ticks++;
while (GPT1_SR & GPT_SR_OF1); // wait for clear
}
void gpt1_init(uint32_t us) {
CCM_CCGR1 |= CCM_CCGR1_GPT(CCM_CCGR_ON) ; // enable GPT1 module
GPT1_CR = 0;
GPT1_PR = 23; // prescale+1
GPT1_OCR1 = us - 1; // compare
GPT1_SR = 0x3F; // clear all prior status
GPT1_IR = GPT_IR_OF1IE;
GPT1_CR = GPT_CR_EN | GPT_CR_CLKSRC(1) ;// 1 ipg 24mhz 4 32khz
attachInterruptVector(IRQ_GPT1, gpt1_isr);
NVIC_ENABLE_IRQ(IRQ_GPT1);
}
void gpt2_isr() {
GPT2_SR |= GPT_SR_OF3; // clear all set bits
ticks++;
// while (GPT2_SR & GPT_SR_OF1); // wait for clear
asm volatile ("dsb");
}
void gpt2_init(uint32_t us) {
CCM_CCGR0 |= CCM_CCGR0_GPT2_BUS(CCM_CCGR_ON) ; // enable GPT2 module
GPT2_CR = 0;
GPT2_PR = 23; // prescale+1
GPT2_OCR1 = us - 1; // compare
GPT2_SR = 0x3F; // clear all prior status
GPT2_IR = GPT_IR_OF1IE;
GPT2_CR = GPT_CR_EN | GPT_CR_CLKSRC(1) ;// 1 ipg 24mhz 4 32khz
attachInterruptVector(IRQ_GPT2, gpt2_isr);
NVIC_ENABLE_IRQ(IRQ_GPT2);
}
void setup() {
Serial.begin(9600);
while (!Serial);
delay(2000);
gpt2_init(50); // us
}
void loop() {
Serial.println(ticks);
delay(1000);
}