Teensy 4.1 GPT compare output reset

bmd1103

Member
I'm working on using a 4.1 to drive RC servo using PWM. For simplicity I would like to use GPT2, with COMPARE_1 resetting the count, and COMPARE_2 and COMPARE_3 giving me 2 channels of PWM. I have used manitou48's "gptisr3" as a starting point. I want to set OP2 and OP3 on the start of a cycle and clear them on the compare event.

I have managed to get the general approach working, with the outputs toggling on the compares using GPT_CR_OMx(1). However, when I replace the (1) with (2) to clear the output on a match, it stays low (and if I use (3) to set it, it stays high). I cannot find anywhere in the GPT part of the manual that indicates how I can reset these pins. I can get the required result by using GPIO pins as in the code, but would like to be totally self-contained in my application.

Any pointers would be appreciated.



Code:
const uint32_t maxPos = 0x000493e0;
const uint32_t minPos = 0x000249f0;
uint32_t pos1;
uint32_t pos2;

void gpt2_isr()
{
  uint32_t srtmp = GPT2_SR;
//  GPT2_SR &= 0x3F;  // clear all set bits
  if (srtmp & GPT_SR_OF1)
  {
    digitalWriteFast(3, 1 - digitalReadFast(3));
    digitalWriteFast(4, HIGH);
    digitalWriteFast(5, HIGH);
  GPT2_SR &= GPT_SR_OF1;  // clear all set bits
  }
  if (srtmp & GPT_SR_OF2)
  {
    digitalWriteFast(4, LOW);
  GPT2_SR &= GPT_SR_OF2;
  }
  if (srtmp & GPT_SR_OF3)
  {
    digitalWriteFast(5,LOW);
  GPT2_SR &= GPT_SR_OF3;
  }

  asm volatile("dsb");
}

void setup()
{
  pinMode(3, OUTPUT);
  pinMode(4, OUTPUT);
  pinMode(5, OUTPUT);
  pinMode(13, OUTPUT);
  CCM_CSCMR1 &= ~CCM_CSCMR1_PERCLK_CLK_SEL; // turn off 24MHz mode
  CCM_CCGR0 |= CCM_CCGR0_GPT2_BUS(CCM_CCGR_ON) ;  // enable GPT2 module

  //configure GPT2 for test
  GPT2_CR = 0;                                //clear the control register, FRR = 0 means restart after Compare
  GPT2_SR = 0x3F;                             //clear all prior status
  GPT2_PR = 0;                                //prescale register set divide by 1
  GPT2_CR |= GPT_CR_CLKSRC(1);                //clock selection #1 (peripheral clock = 150 MHz)
 
  GPT2_CR |= GPT_CR_ENMOD;            //reset count to zero before enabling
  GPT2_CR |= GPT_CR_OM3(0b010);       //  mode 2 - clear output on match
  GPT2_CR |= GPT_CR_OM2(0b011);       //  mode 3 - set output on match
  GPT2_OCR1 = 0x002dc6c0;             //Compare1 value = 20 mS at 150 MHz
  GPT2_OCR2 = 0x000249f0;             //Compare2 value = 1 mS at150 MHz
  GPT2_OCR3 = 0x000493e0;             //Compare3 value = 2 mS at 150 MHz
  GPT2_IR = GPT_IR_OF1IE;                  //enable interrupt on full count flag
  GPT2_CR |= GPT_CR_EN;                   //enable GPT2
  //configure Teensy pin 16, 17 as GPT2 outputs
  IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_07 = 8;    // GPT2 Compare3 is now output on pin 16
  IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B1_06 = 8;    // GPT2 Compare2 is now output on pin 17

  attachInterruptVector(IRQ_GPT2, gpt2_isr);
  NVIC_ENABLE_IRQ(IRQ_GPT2);
}

void loop()
{
  digitalWrite(13, !digitalRead(13));
  delay(200);
}
 
I have managed a "fix" - change the compare pin mode to "set on compare", force it with FO2/FO2, then change back to "clear on compare":
Code:
void gpt2_isr()
{
  uint32_t srtmp = GPT2_SR;
//  GPT2_SR &= 0x3F;  // clear all set bits
  if (srtmp & GPT_SR_OF1)
  {
    GPT2_CR ^= (GPT_CR_OM2(0b011) | GPT_CR_OM3(0b011));   // Change output modes to set on event
    GPT2_CR |= (GPT_CR_FO2 | GPT_CR_FO3);                 // Force the event
    GPT2_CR ^= (GPT_CR_OM2(0b011) | GPT_CR_OM3(0b011));   // Change back to clear on event
    
    digitalWriteFast(3, 1 - digitalReadFast(3));          // Drive pins for testing.
    digitalWriteFast(4, HIGH);
    digitalWriteFast(5, HIGH);
    GPT2_SR &= GPT_SR_OF1; 
  }
  if (srtmp & GPT_SR_OF2)
  {
    digitalWriteFast(4, LOW);
  GPT2_SR &= GPT_SR_OF2;
  }
  if (srtmp & GPT_SR_OF3)
  {
    digitalWriteFast(5,LOW);
  GPT2_SR &= GPT_SR_OF3;
  }

  asm volatile("dsb");
}

But it feels like a horrible kludge ...
 
Back
Top