I'm trying to measure the elapsed time between two logic pulses with the GPT capture functions as described in the reference manual section 52.5.2.1. I found Paul's example code elsewhere in the forum (https://forum.pjrc.com/threads/5426...B-(600-Mhz-M7)?p=193217&viewfull=1#post193217 ) from the time he was doing crystal measurements. I have twiddled it a little bit per the reference manual, but I could not get it to (1) increment GPTx_CNT or (2) capture a count into GPTx_CNT in either its original form or my modified form. Code further below. I'm using the Arduino 1.8.13 environment with TeensyDuino 1.8.5. My development host is running debian 10.5 on an x86-64. I've commented out the capture pin mapping to focus first on the counter running issue, but you can see what options I was fiddling with. None worked.
I've run a few of the TeensyTimerTool library examples, and they work as expected, so I believe the hardware is still OK. Nice library, BTW. Interestingly, when I add Serial code to dump GPT1_CNT every second in a loop in one of the demos, the count has incremented only 25 to 30, which seems... really small. But the timers themselves work, so... IDK.
Pins: I stared at the Teensy 4.1 schematic (https://www.pjrc.com/teensy/schematic.html) and the ref manual (https://www.pjrc.com/teensy/IMXRT1060RM_rev2.pdf) a long time before I decided that GPT1_CAPTURE was not usable because none of the pads it could be muxed to were available as Teensy pins, but that I could use either GPT2_CAPTURE1 on GPIO_AD_B1_04, Teensy 4.1 pin 40 (A16) or GPT2_CAPTURE1 on GPIO_AD_B1_03, Teensy 4.1 pin 15 (A1). I tried both of these pins and didn't see any ISR calls, but, since the GPT2_CNT register itself did not appear to be incrementing, that is probably the first problem in line.
Any ideas? Thanks so much for any help!
Output:
Best,
Mike
I've run a few of the TeensyTimerTool library examples, and they work as expected, so I believe the hardware is still OK. Nice library, BTW. Interestingly, when I add Serial code to dump GPT1_CNT every second in a loop in one of the demos, the count has incremented only 25 to 30, which seems... really small. But the timers themselves work, so... IDK.
Pins: I stared at the Teensy 4.1 schematic (https://www.pjrc.com/teensy/schematic.html) and the ref manual (https://www.pjrc.com/teensy/IMXRT1060RM_rev2.pdf) a long time before I decided that GPT1_CAPTURE was not usable because none of the pads it could be muxed to were available as Teensy pins, but that I could use either GPT2_CAPTURE1 on GPIO_AD_B1_04, Teensy 4.1 pin 40 (A16) or GPT2_CAPTURE1 on GPIO_AD_B1_03, Teensy 4.1 pin 15 (A1). I tried both of these pins and didn't see any ISR calls, but, since the GPT2_CNT register itself did not appear to be incrementing, that is probably the first problem in line.
Any ideas? Thanks so much for any help!
Code:
#include <imxrt.h>
void setup() {
pinMode(13, OUTPUT);
digitalWrite(13, HIGH);
delay(1000);
digitalWrite(13, LOW);
Serial.begin(9600);
while (!Serial);
delay(5000);
digitalWrite(13, HIGH);
Serial.println("ok");
delay(1000);
// IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_03 = 8; // GPT2_CAPTURE1 on GPIO_AD_B1_04, Teensy 4.1 pin 40 (A16)
// IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_04 = 8; // GPT2_CAPTURE1 on GPIO_AD_B1_03, Teensy 4.1 pin 15 (A1)
// IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_03 = 0x13000; //Pullup 22k & Hyst
// IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_04 = 0x13000; //Pullup 22k & Hyst
IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_04 = 0x10000 | 0x2000; //Pullup 100k & Hyst
// IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B1_04 = 0x2000; //Pullup 100k
CCM_CCGR0 |= CCM_CCGR0_GPT2_BUS(CCM_CCGR_ON) | CCM_CCGR0_GPT2_SERIAL(CCM_CCGR_ON);
// CCM_CCGR1 |= CCM_CCGR1_GPT1_BUS(CCM_CCGR_ON) | CCM_CCGR1_GPT1_SERIAL(CCM_CCGR_ON);
// 52.6.1: Init and select clock source
// See imxrt.h for macro definitions and register bit values
GPT2_CR &= ~(GPT_CR_EN); // Set EN = 0
GPT2_IR = 0; // Disable GPT interrupt register
GPT2_CR &= ~(GPT_CR_OM1(0x07)); // Disable OM1
GPT2_CR &= ~(GPT_CR_OM2(0x07)); // Disable OM2
GPT2_CR &= ~(GPT_CR_OM3(0x07)); // Disable OM3
GPT2_CR &= ~(GPT_CR_IM1(0x03)); // Disable IM1
GPT2_CR &= ~(GPT_CR_IM2(0x03)); // Disable IM2
GPT2_CR |= GPT_CR_SWR; // Assert SWR
Serial.print("GPT2_CR after SWR is ");
Serial.println(GPT2_CR);
GPT2_PR = GPT_PR_PRESCALER(2); // divide-by-(2+1) = 8MHz clock
GPT2_SR |= 0x3F; // Clear flags in status register (write 1 to clear bottom 6 bits)
GPT2_CR |= GPT_CR_ENMOD; // bring GPT counter to 0x00000000
GPT2_CR |= GPT_CR_EN | // Enable GPT
GPT_CR_EN_24M | // Enable 24MHz crystal input
GPT_CR_CLKSRC(5) | // use 24MHz crystal clock source
GPT_CR_IM1(2) | // with capture on falling edge (2) (or, rising edge=1, or both edges=3)
GPT_CR_FRR; // in free run mode
Serial.print("GPT2_CR after setup is ");
Serial.println(GPT2_CR);
GPT2_IR |= GPT_IR_IF1IE; // Enable CAPTURE1 interrupt
attachInterruptVector(IRQ_GPT2, capture);
NVIC_ENABLE_IRQ(IRQ_GPT2);
}
volatile uint32_t ticks;
volatile uint32_t count;
void capture() {
GPT2_SR = GPT_SR_IF1; // clear capture flag bit
static uint32_t last = 0;
count++;
uint32_t t = GPT2_ICR1;
uint32_t n = t - last;
last = t;
// asm("dsb"); // data synchronization barrier
Serial.printf("t=%u, delta=%u\n", t, n);
}
void loop() {
uint32_t now1 = GPT1_CNT;
uint32_t now2 = GPT2_CNT;
Serial.print(now1);
Serial.print(" ");
Serial.print(now2);
Serial.print(" ");
Serial.println(count);
delay(1000);
}
Output:
Code:
18:22:46.683 -> 0 0 0
18:22:47.677 -> 0 0 0
18:22:48.672 -> 0 0 0
18:22:49.666 -> 0 0 0
18:22:50.661 -> 0 0 0
18:22:51.655 -> 0 0 0
Best,
Mike
Last edited: