Kuba0040
Well-known member
Hello,
I am currently trying to learn how to use the GTP timers of the Teensy 4.0's CPU. I am doing this as a part of my general adventure into learning how to use ARM microcontrollers as AVR isn’t doing it anymore. Today I wanted to tackle the GTP - General Purpose Timer. I mainly tried to figure things out from the IMXRT1060RM datasheet, which yielded in the code I attached below. I ran into one issue tho. I don't know what names the individual timer interrupts have. On AVR for example the timer interrupt functions are called ISR(TIMERx_COMPA_vect). And you can just paste them into your code and use them. What are they called here? I am using the Compare Channel 1 of GTP1. This is my first time with all of this, so I am not familiar with all the hardware yet. If you also have any suggestions on what I should change to get this code to work, I'll be thankful.
Thanks for the help.
My goal with this code is to have the GTP1 call a function every second. Inside it I'll just place a simple Serial.println to make sure stuff works. Like I said, just to learn how to use these timers on ARM.
I am currently trying to learn how to use the GTP timers of the Teensy 4.0's CPU. I am doing this as a part of my general adventure into learning how to use ARM microcontrollers as AVR isn’t doing it anymore. Today I wanted to tackle the GTP - General Purpose Timer. I mainly tried to figure things out from the IMXRT1060RM datasheet, which yielded in the code I attached below. I ran into one issue tho. I don't know what names the individual timer interrupts have. On AVR for example the timer interrupt functions are called ISR(TIMERx_COMPA_vect). And you can just paste them into your code and use them. What are they called here? I am using the Compare Channel 1 of GTP1. This is my first time with all of this, so I am not familiar with all the hardware yet. If you also have any suggestions on what I should change to get this code to work, I'll be thankful.
Thanks for the help.
My goal with this code is to have the GTP1 call a function every second. Inside it I'll just place a simple Serial.println to make sure stuff works. Like I said, just to learn how to use these timers on ARM.
Code:
void setup()
{
//Disable GTP1 interrupt
GPT1_IR=0; //Just disable all interrupts
//Put GPT1 into restart mode. In restart mode, the timer resets when a interrupt is triggered. This only works with the Compare register 1. Which we will use
//This is done by default as FRR is set to 0 on power up.
//Disable all output modes (the timer can trigger pins to go high or low, we don't want that)
//By deafault this is all 0 so we don't have to do anything here. But use OM3, OM2, OM1 in GPT1_CR
//Disable input capture, this is the same, but for inputs. So the timer can store a value when a pin goes high or something
//By default this is also all 0. But set IM2, IM1 to 0 for that.
//We will be using the Crystal Refrence Clock. So we have to use the PRESCALER24M bits. Let's set a prescaler of 16. The maximum value.
GPT1_PR |= (1<<15);
GPT1_PR |= (1<<14);
GPT1_PR |= (1<<13);
GPT1_PR |= (1<<12);
//Set the clock source to be the Crystal Oscillator Refrence Clock (ipg_clk_24M) which is running at 24MHz. CLKSRC value for this is 101
GTP1_CR |= (1<<6);
GTP1_CR |= (1<<8);
//Clear the GPT status register GPT1_SR. This contains flags for when the timer rolls over and for other interrupts.
GTP1_SR=0;
//Set ENMOD in GTP_CR. This will put the timer into enable ready mode, but not start it yet. This resets the counter to 0.
GTP1_CR |=(1<<ENMOD);
//Enable GTP1
GTP1_CR |= (1<<EN); //Write 1 to the EN (enable) bit.
//Enable GTP1 interrupts
GTP1_CR |= (1<<OF1IE); //Enable the Output Compare 1 interrupt, you know. The one that resets it in restart mode.
//Set the Compare value in the Output Compare Channel 1. This is a 32 bit register
GPT1_OCR1=1500000; //After all the divisions our clock is running at 1.5MHz. This should trigger a interrupt every second.
}
void loop()
{
// put your main code here, to run repeatedly:
}