Teensy 4.1: What is wrong here? At 10 MHz I have exactly 10 MHz on the counter. Increasing the oscillator >10 MHz leads to incorrect counting results.
Code:
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 uint32_t counter_read(void)
{
return GPT1_CNT;
}
volatile uint32_t count_ready;
volatile uint32_t count_output;
volatile uint32_t count_prev;
void tmr_callback()
{
uint32_t count = counter_read();
count_output = count - count_prev;
count_prev = count;
count_ready = 1;
}
IntervalTimer it1;
void setup()
{
Serial.begin(9600);
while (!Serial);
analogWriteFrequency(11, 10'000'000); // test jumper 11 to 25
analogWrite(11, 128);
Serial.print("CPU clock: ");
Serial.print(F_CPU_ACTUAL / 1E6);
Serial.println(" MHz");
Serial.print("BUS clock: ");
Serial.print(F_BUS_ACTUAL / 1E6);
Serial.println(" MHz");
counter_init();
it1.begin(tmr_callback, 1'000'000); // us
counter_start();
}
void loop()
{
if (count_ready)
{
Serial.print(count_output);
Serial.print(" ");
Serial.print(count_output / 1E6);
Serial.println(" MHz");
count_ready = 0;
}
}
analogWriteFrequency(11, 10'000'000); // test jumper 11 to 25
Code:
CPU clock: 600.00 MHz
BUS clock: 150.00 MHz
9999129 10.00 MHz
10000000 10.00 MHz
10000000 10.00 MHz
10000000 10.00 MHz
10000000 10.00 MHz
10000000 10.00 MHz
analogWriteFrequency(11, 12'000'000); // test jumper 11 to 25
Code:
CPU clock: 600.00 MHz
BUS clock: 150.00 MHz
11075439 11.08 MHz
11076923 11.08 MHz
11076923 11.08 MHz
11076923 11.08 MHz
11076923 11.08 MHz
analogWriteFrequency(11, 15'000'000); // test jumper 11 to 25
Code:
CPU clock: 600.00 MHz
BUS clock: 150.00 MHz
8997685 9.00 MHz
9000000 9.00 MHz
9000000 9.00 MHz
9000000 9.00 MHz