TC and Period Capture

Hello,

I would like to use TC3 and it’s PWP/PPW function on my SparkFun SAMD21 Dev Breakout. It is very similar to the Arduino MKR Zero, the Arduino Zero or the Arduino M0 (pro).
Therefor I wired two LEDs and one button up. The button is active low and when pressed it should fire ther interrupt and via TC3 I can get the ticks since the last button pressed event.

The code itself runs upon the point the button is pressed and the LED is toggled in the TC3_Handler() method.
By the next button pressed the LED should be toggled again, but this doesen’t happpen. The event only gets fired one time. So I can tell, that the code by principle is working, but suddenly stops.

Maybe you can see where the problem lies :smiley:

That is my code to start TC3

void start_TC3()
{
  PM->APBCMASK.bit.TC3_ = 1;
  
  GCLK->CLKCTRL.bit.ID = 27;    //Select TCC2, TC3    GCLK_CLKCTRL_ID_TCC2_TC3
  GCLK->CLKCTRL.bit.GEN = 1;    //Clock Generator 1 (OSC8M)
  GCLK->CLKCTRL.bit.CLKEN = 1;
  
  while (GCLK->STATUS.bit.SYNCBUSY)
  {
    ;
  }
  TC3->COUNT16.CTRLA.bit.WAVEGEN = 0x0;   //NFRQ
  TC3->COUNT16.CTRLA.bit.PRESCALER = 0x0; 
  TC3->COUNT16.CTRLC.bit.CPTEN0 = 1;
  TC3->COUNT16.CTRLC.bit.CPTEN1 = 1;
  
  TC3->COUNT16.EVCTRL.bit.MCEO0 = 1;
  TC3->COUNT16.EVCTRL.bit.TCEI = 1;
  TC3->COUNT16.EVCTRL.bit.EVACT = 6;
  
    
  TC3->COUNT16.INTENSET.bit.MC0 = 1;
  NVIC_EnableIRQ(TC3_IRQn);
  TC3->COUNT16.CTRLA.bit.ENABLE = 1;
  while (GCLK->STATUS.bit.SYNCBUSY)
  {
    ;
  }
}

And the Handler:

void TC3_Handler()
{
  TC3->COUNT16.INTFLAG.bit.MC0 = 1;
  PORT->Group[0].OUTTGL.reg = (1 << PIN_PA14);
}

Now the EIC vonfiguration:

void start_EIC()
{
  EIC->EVCTRL.bit.EXTINTEO5 = 1;
  
  while(EIC->STATUS.bit.SYNCBUSY)
  {
    ;
  }
}

And finally the EVSYS channel configuration:

void start_EVSYS()
{
  PM->APBCMASK.bit.EVSYS_ = 1;

  GCLK->CLKCTRL.bit.ID = 7;
  GCLK->CLKCTRL.bit.GEN = 0;
  GCLK->CLKCTRL.bit.CLKEN = 1;
  while (GCLK->STATUS.bit.SYNCBUSY)
  {
    ;
  }

  EVSYS->USER.bit.USER = 18; //TC3
  EVSYS->USER.bit.CHANNEL = 1;

  while(!(EVSYS->CHSTATUS.bit.USRRDY1))
  {
    ;
  }

  EVSYS->CHANNEL.bit.CHANNEL = 0;
  EVSYS->CHANNEL.bit.EDGSEL = 3;
  EVSYS->CHANNEL.bit.EVGEN = 0x11;  //EXTINT5
  EVSYS->CHANNEL.bit.PATH = 0;

  while(EVSYS->CHSTATUS.bit.CHBUSY0)
  {
    ;
  }
}

The blink method just tells me the button is pressed

void blink()
{
  PORT->Group[0].OUTTGL.reg = (1 << PIN_PA09);
}

The Arduino Program:

void setup() 
{
  pinMode(2, OUTPUT);
  digitalWrite(2, HIGH);

  pinMode(3, OUTPUT);
  digitalWrite(3, HIGH);
  
  //Button pressed Interrupt, active low

  pinMode(A4, INPUT_PULLUP);
  attachInterrupt(A4, blink, FALLING);

  start_TC3();
  start_EIC();
  start_EVSYS();
}

void loop() 
{
  //Is-Alive-Signal

  delay(500);
  PORT->Group[0].OUTTGL.reg = (1 << PIN_PA09);
}

You should specify which hardware this is for as it's not for an UNO (that's the hardware we expect if no special hardware is mentioned).

Post complete code!

Sorry, I added the hardware in my initial post.
It is a SparkFun SAMD21 Dev Breakout which is very similar to the Arduino MKR Zero, the Arduino Zero or the Arduino M0 (pro).