Arduino DUE dual timer

Hello,

Currently I am trying to get two RC compare timer interrupts working on the arduino DUE. TC0 timer interrupt is working fine, but the TC0 timer interrupt doesn't work. Does anybody have an idea what my error is? Thank you in advance.

Jasper

volatile uint8_t flag;
volatile uint8_t flag1;
void setup() {
   SystemInit();
   configure_io();
   configure_tc();
 
   flag = 0;
   flag1 = 0;

}

void loop() {

}

void TC0_Handler(void)
{
    volatile uint32_t ul_status;
 
    // read status from TC0 status register
    ul_status = TC0->TC_CHANNEL->TC_SR;
 
    if(flag == 0)
    {
        // Enable output
        PIOB->PIO_SODR = PIO_PB27; // Arduino Due LED (L)
        PIOD->PIO_SODR = PIO_PD0; // Arduino Due Pin 25
        flag = 1;
    }
    else
    {
        // Disable output
        PIOB->PIO_CODR = PIO_PB27; // Arduino Due LED (L)
        PIOD->PIO_CODR = PIO_PD0; // Arduino Due Pin 25
        flag = 0;
    }
 
}

void TC1_Handler(void)
{
    volatile uint32_t ul_status;
 
    // read status from TC0 status register
    ul_status = TC1->TC_CHANNEL->TC_SR;
    
    if(flag1 == 0)
    {
        // Enable output
        PIOD->PIO_SODR = PIO_PD1; // Arduino Due Pin 26
        flag1 = 1;
    }
    else
    {
        // Disable output
        PIOD->PIO_CODR = PIO_PD1; // Arduino Due Pin 26
        flag1 = 0;
    }
 
}
 
static void configure_tc(void)
{
    // Enable TC0 (27 is TC0, 28 is TC1)
    PMC->PMC_PCER0 = (1 << 28)|(1 << 27);
    // Disable TC clock
    TC0->TC_CHANNEL->TC_CCR = TC_CCR_CLKDIS;
    TC1->TC_CHANNEL->TC_CCR = TC_CCR_CLKDIS;
 
    // Disable interrupts
    TC0->TC_CHANNEL->TC_IDR = 0xFFFFFFFF;
    TC1->TC_CHANNEL->TC_IDR = 0xFFFFFFFF;
 
    // Clear status register
    TC0->TC_CHANNEL->TC_SR;
    TC1->TC_CHANNEL->TC_SR;
    // Set Mode
    //TC0->TC_BMR = TC_BMR_TC0XC0S_TCLK0 | TC_BMR_QDEN;
    TC0->TC_CHANNEL->TC_CMR = TC_CMR_CPCTRG | TC_CMR_TCCLKS_TIMER_CLOCK1;// | TC_CMR_WAVE | TC_CMR_EEVTEDG_EDGE;
    TC1->TC_CHANNEL->TC_CMR = TC_CMR_CPCTRG | TC_CMR_TCCLKS_TIMER_CLOCK1;
    // Compare Value
    TC0->TC_CHANNEL->TC_RC = 100;
    TC1->TC_CHANNEL->TC_RC = 100;
    // Configure and enable interrupt on RC compare
    NVIC_EnableIRQ((IRQn_Type) ID_TC0);
    NVIC_EnableIRQ((IRQn_Type) ID_TC1);
    TC0->TC_CHANNEL->TC_IER = TC_IER_CPCS;
    TC1->TC_CHANNEL->TC_IER = TC_IER_CPCS;
 
    // Reset counter (SWTRG) and enable counter clock (CLKEN)
    TC0->TC_CHANNEL->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
    TC1->TC_CHANNEL->TC_CCR = TC_CCR_CLKEN | TC_CCR_SWTRG;
}
 
static void configure_io(void)
{
    // Enable IO
    PIOB->PIO_PER = PIO_PB27;
    PIOD->PIO_PER = PIO_PD0 | PIO_PD1;
 
    // Set to output
    PIOB->PIO_OER = PIO_PB27;
    PIOD->PIO_OER = PIO_PD0 | PIO_PD1;
 
    // Disable pull-up
    PIOB->PIO_PUDR = PIO_PB27;
    PIOD->PIO_PUDR = PIO_PD0 | PIO_PD1;
 
}

It is because you are not initializing Timer Counter 1, you are initializing Timer Counter 3.

The timers look like this

Timer | Channel | Handler
0          0             TC0 <-- You are using this one
0          1             TC1
0          2             TC2
1          0             TC3 <-- You are also using this one
1          1             TC4
1          2             TC5
2          0             TC6
2          1             TC7
2          2             TC8

Also, you aren't initializing them correctly, when you see TC_CHANNEL, it really should be TC_CHANNEL[channel]

So for example

// Clear status register
    TC0->TC_CHANNEL->TC_SR;
    TC1->TC_CHANNEL->TC_SR;

Should be

// Clear status register
    TC0->TC_CHANNEL[0]->TC_SR;
    TC0->TC_CHANNEL[1]->TC_SR;

Hello,

Thank you so much for your response! The code works like a charm now.

Jasper