Timer 32bit 150MHz with timertool

Status
Not open for further replies.

michastro

Member
Hello,
I am just beginning with Teensy after a long time of programmation on other micropic. I have a 4.0 Teensy and I need a timer with a resolution of about 80MHz or more and a 32bits timer, but for instance I have seen in Timertool wiki that 150MHz timer is limited to 16bits. Perhaps I have missed something.
Thanks for your help
Michel
 
You can switch the GPT (32bit) timers to 150MHz. Have a look at the configuration chapter in the wiki.
 
Here you are:
Code:
#include "TeensyTimerTool.h"
using namespace TeensyTimerTool;

PeriodicTimer myTimer(GPT1);

void printCurrentTime(){
    Serial.printf("Called at: %d\n", micros());
}

void setup(){
    myTimer.begin(printCurrentTime, 100ms);
}

void loop(){
}

Which prints:
Screenshot 2021-04-15 183942.jpg
 
Thanks a lot!! Now I have to find a way to select medium speed (100MHz) and how to change the value of the counter to have very precise timer interval, it's to pilot motors of a telescope with very precise time interval, it's the reason of 100MHz base.
Thanks again.
Michel
 
So now I am in the very big doc of the IMXRT1060 and the source code of TimerTool. It's not very easy to find how to have a 150MHz or 100MHz with the CCM and use it for the GPT1 timer. I have search examples on internet, but all the codes I have seen use the 24MHz default clock.
Michel
 
So now I am in the very big doc of the IMXRT1060 and the source code of TimerTool. It's not very easy to find how to have a 150MHz or 100MHz with the CCM and use it for the GPT1 timer. I have search examples on internet, but all the codes I have seen use the 24MHz default clock.
Michel

Have pasted some snips from GPT code Im working on, might help. Some of it inspired by manitou48 here. You can change the line #define GPT_CLOCK_SOURCE 3 to vary the source for the GPT timers. Note that 3 is for external clock. Cant remember offhand which numeric option does what but you should have one for 150MHz internal clock? See section 52 of manual.

My timer init code takes the clock source for the timers as one of its parameters. I'm also installing different interrupt handlers according to need, I'm testing with rollover and compare1 interrupts at present.

Hope of some use

Steve

Code:
// some defines:

#define GPT1 1
#define GPT2 2
#define GPT_BOTH 3
#define GPT_BOTH_GPT2FIRST 4
#define GPT_CLOCK_SOURCE 3                     // 3 = external GPT clock, note you can set internal sources see section 52 of chip datasheet
#define GATE_CLOCK_FREQ 10000000
#define NO_INTERRUPT 0                              // Counter ISR setup control variable 
#define ROLLOVER 1                                  // ISR setup
#define COMPARE_RESET 2                             // ISR setup
#define COMPARE_FREEZE 3                            // ISR setup


// Example lines that call the init function:

initGPTCounter(GPT1,GPT_CLOCK_SOURCE,0,ROLLOVER);                    // Sets up GPT1 with clock input on pin 14, clock source options 1..3. 
initGPTCounter(GPT2,GPT_CLOCK_SOURCE,gateCountVal,COMPARE_RESET);    // Sets up GPT2 with clock input on pin 25, params: GPTn, clkSource, compare1 value,interrupt type)
initGPTCounter(GPT2,GPT_CLOCK_SOURCE,0,NO_INTERRUPT);                 // Sets up GPT1 with clock input on pin 14, clock source options 1..3. 
initGPTCounter(GPT1,GPT_CLOCK_SOURCE,gateCountVal,COMPARE_FREEZE);     // Sets up GPT2 with clock input on pin 25, params: GPTn, clkSource, compare1 value,interrupt type)

// start function also:
startGPTCounter(GPT_BOTH);                                            // GPT_BOTH starts GPT1 first, gives most accurate count see comments in function


void initGPTCounter(uint8_t GPTn, uint8_t clkSource, uint32_t compare1, uint8_t interrupt)
{
  if (GPTn==GPT1)
    {
      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(clkSource);               // GPT_CR_FRR is clear so that compare 1 events will reset counter,clkSource = 3 = external clock
      GPT1_CR |= GPT_CR_ENMOD;                          // ENMOD set in GPTn_CR reg means that counter is always reset to zero whenever enabled/reenabled
      *(portConfigRegister(25)) = 1;                    // ALT 1  Clock pin is 25  
      if (interrupt == ROLLOVER  && compare1 == 0)       setupIntGPTRollover(GPT1);                                  // Enables timer rollover interrupt on GPTn (GPT1 #define = 1)
      else if (interrupt == COMPARE_RESET && compare1)   setupIntGPTCompare(GPT1,compare1,interrupt);                // Setup compare interrupt with counter reset on compare 1 match GP1
      else if (interrupt == COMPARE_FREEZE && compare1)  setupIntGPTCompare(GPT1,compare1,interrupt);                // Interrupt routines can freeze counters when called (interrupt==3), interrupt < 3 = continue counting
    }
  else if (GPTn==GPT2)
    {
      CCM_CCGR0 |= CCM_CCGR0_GPT2_BUS(CCM_CCGR_ON);
      GPT2_CR = 0;
      GPT2_SR = 0x3F;                              // clear all prior status
      GPT2_CR = GPT_CR_CLKSRC(clkSource);          // GPT_CR_FRR is clear, clkSource 3 = external clock
      GPT2_CR |= GPT_CR_ENMOD;                     // ENMOD set = reset counter to zero on every enable/reenable
      IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_02 = 8;     // select input pin (14)
      IOMUXC_GPT2_IPP_IND_CLKIN_SELECT_INPUT = 1;
      if (interrupt == ROLLOVER && compare1 == 0)        setupIntGPTRollover(GPT2);                          // Enables timer rollover interrupt on GPTn (GPT1 #define = 1)
      else if (interrupt == COMPARE_RESET && compare1)   setupIntGPTCompare(GPT2,compare1,interrupt);        // Setup compare interrupt/counter reset on compare 1 match GP2
      else if (interrupt == COMPARE_FREEZE && compare1)  setupIntGPTCompare(GPT2,compare1,interrupt);        // Set up ISR that freezes counters when called
    }


void startGPTCounter(uint8_t GPTn)
{
  if (GPTn == GPT_BOTH)  {GPT1_CR |= GPT_CR_EN;  GPT2_CR |= GPT_CR_EN;  return;}             // Enable both GPT 1 and 2 near-simultaneously, GPT1 first
  if (GPTn == GPT_BOTH_GPT2FIRST)  {GPT2_CR |= GPT_CR_EN;  GPT1_CR |= GPT_CR_EN;  return;}   //  GPT 2 enabled first
  if (GPTn == GPT1)  {GPT1_CR |= GPT_CR_EN;  return; }                            // Enable only GPT1. Counter will reset to start from zero assuming GPT_CR_ENMOD is set by init() 
  if (GPTn == GPT2)  {GPT2_CR |= GPT_CR_EN;  return;}                             // Enable only GPT2
}
 
Status
Not open for further replies.
Back
Top