GPT1 Reset Count

Dimitri

Well-known member
Hi All,

Does anyone know how to reset the General Purpose Timer Counter registers? For example, within an interrupt, I want to read the value of GPT1_CNT, and then reset GPT1_CNT to zero. This is def possible with FlexTimers and others, however I cannot determine how to do this with General Purpose Timers.

Any Ideas?

Code:
const byte pin_LED = 13;
const byte pin_InputSig = 11;

boolean LED_st = 1;
uint32_t TempVal = 0;

///////////////////////////////////////////////////////////////////////////////////////////////
void setup() 
{ 
  Serial.begin(115200);
  pinMode(pin_LED,OUTPUT);
  digitalWrite(pin_LED,HIGH);

  //Setup General Purpose Timers
  Setup_GPT1();
  attachInterrupt(digitalPinToInterrupt(pin_InputSig), ISR_GPT1_DSK, FALLING);
  
  t1 = millis();
  t2 = t1;
}

///////////////////////////////////////////////////////////////////////////////////////////////
void loop()
{
  int i; 
  t1 = millis();
  
  if(((t1-t2) >= 100) || (t2 > t1))
  {
    LED_st = !LED_st;    
    digitalWrite(pin_LED,LED_st);
    
    Serial.print(t1);
    Serial.print("\t");
    Serial.print(GPT1_CNT);
    Serial.print("\t");
    Serial.print(TestVal_A); 
    Serial.println();

    t2 = t1;
  }  
}

///////////////////////////////////////////////////////////////////////////////////////////////
void ISR_GPT1_DSK()
{
  TestVal_A = GPT1_CNT;
  GPT1_SR = 0x003F; 
  //GPT1_CNT = 0;
   
  asm volatile ("dsb");  // wait for clear  memory barrier  
}

///////////////////////////////////////////////////////////////////////////////////////////////
void Setup_GPT1()
{
  /* Initialize GPT1 */
  CCM_CCGR1 |= CCM_CCGR1_GPT1_BUS(CCM_CCGR_ON) | CCM_CCGR1_GPT1_SERIAL(CCM_CCGR_ON);
  GPT1_SR = 0x3F;                 // clear all prior status
  GPT1_CR |=  GPT_CR_CLKSRC(1);   // Set clocksource to Peripheral Clock
  GPT1_CR |= GPT_CR_ENMOD;        // Set Reset Mode
  GPT1_OCR1 = 0x1FFFFFFF;
  GPT1_CR |= GPT_CR_EN;
}
 
Hi All,

I found a way to reset the counter, but it's kind of dirty...

In the iMXRT1060 document, it says that if you set GPT1_OCR1 to a new value, it will reset GPT1_CNT.

So within my ISR, I just set GPT1_OCR1 to an arbitrary high value (0x1FFFFFFF). No need for the value to change, any write to this register will set GPT1_CNT to zero.
 
you could also do a reset, add this to your ISR
Code:
    GPT1_CR |= GPT_CR_SWR;
    GPT1_CR |=  GPT_CR_CLKSRC(1);   // Set clocksource to Peripheral Clock
 
Back
Top