Problem with timer interrupt

Hey there,

I´m having problems with outputting sound(wav files stored as byte-arrays) over DAC1 with a timer interrupt and using Serial.println(). I don´t understand why.
So after some trying, I rewrote and isolated the timer interrupt part. For me there is a strange thing happening:
The following code does not turn off the builtin led. But when u uncomment "digitalWrite(LED_BUILTIN, LOW);" in TC6_Handler() it does turn off.
So it seems to me that "playing = false" is executed and i wonder why the statement after "while(playing)" isn´t executed.

void startTimer(Tc* tc, uint32_t channel, IRQn_Type irq, uint32_t frequency) 
{
  pmc_set_writeprotect(false);
  pmc_enable_periph_clk((uint32_t)irq);
  TC_Configure(tc, channel, TC_CMR_WAVE | TC_CMR_WAVSEL_UP_RC | TC_CMR_TCCLKS_TIMER_CLOCK4);
  uint32_t rc = VARIANT_MCK / 128 / frequency; //128 because we selected TIMER_CLOCK4 above
  TC_SetRA(tc, channel, rc / 2); //50% high, 50% low
  TC_SetRC(tc, channel, rc);
  TC_Start(tc, channel);
  tc->TC_CHANNEL[channel].TC_IER = TC_IER_CPCS;
  tc->TC_CHANNEL[channel].TC_IDR = ~TC_IER_CPCS;
  NVIC_EnableIRQ(irq);
}


  volatile long pos = 0;
  volatile bool playing = false;

void TC6_Handler()
{
    if(playing == false) return;

    pos++;
    
    if(pos == 1000)
    {
      playing = false;
      //digitalWrite(LED_BUILTIN, LOW);
    }
}
void StartTimer()
{
  startTimer(TC2, 0, TC6_IRQn, 1); 
}
void StopTimer(){NVIC_DisableIRQ(TC6_IRQn);}

void setup()
{
  pinMode(LED_BUILTIN, OUTPUT);
  digitalWrite(LED_BUILTIN, HIGH);
}
void loop()
{
  StartTimer();
  playing = true;
  while(playing);
  digitalWrite(LED_BUILTIN, LOW);
  StopTimer();
}

As you maybe can tell, I´m working with timer interrupts the first time. I have no idea why it seems like the Due gets stuck at "while(playing)".
Please share your thoughts with me.
Thanks, deeperrr

If you want the Timer interrupt callback function to fire again and again, you have to read and clear TC_SR inside the TC interrupt handler.

An example sketch with a timer counter to blink an LED:

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  tc_setup();
}

void loop() {
}

void tc_setup() {

  PMC->PMC_PCER0 |= PMC_PCER0_PID29;                       // TC2 power ON : Timer Counter 0 channel 2 IS TC2

  TC0->TC_CHANNEL[2].TC_CMR = TC_CMR_TCCLKS_TIMER_CLOCK1   // MCK/2, clk on rising edge
                              | TC_CMR_EEVT_XC0            // TIOB2 in output
                              | TC_CMR_WAVE                // Waveform mode
                              | TC_CMR_WAVSEL_UP_RC;       // UP mode with automatic trigger on RC Compare

  TC0->TC_CHANNEL[2].TC_RC = 2100;                         // Frequency = (Mck/2)/TC_RC  Hz = 20 KHz
  TC0->TC_CHANNEL[2].TC_RA = 21;  //<********************   Any Duty cycle in between 1 and TC_RC
  TC0->TC_CHANNEL[2].TC_IER = TC_IER_CPCS;                 //Interrupt on RC compare match
  NVIC_EnableIRQ(TC2_IRQn);                                // Enable interrupts

  TC0->TC_CHANNEL[2].TC_CCR = TC_CCR_SWTRG | TC_CCR_CLKEN; // Software trigger TC2 counter and enable
}

void TC2_Handler() {

  static uint32_t Count;

  TC0->TC_CHANNEL[2].TC_SR;         // Read and clear status register;

  if (Count++ == 20000) {
    PIOB->PIO_ODSR ^= PIO_ODSR_P27; // Toggle LED_BUILTIN every 1 Hz
    Count = 0;
  }
  // Some stuff, as short as possible
}