Could someone help me about this DUE Timer or Interrupt code?

Here is the code, in fact I see these code on this forum, but I really cannot understand:

volatile boolean l;

//TC1 ch 0
void TC3_Handler()
{
TC_GetStatus(TC1, 0);
digitalWrite(13, l = !l);
}

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);
}

void setup(){
pinMode(13,OUTPUT);
startTimer(TC1, 0, TC3_IRQn, 4); //TC1 channel 0, the IRQ for that channel and the desired frequency
}

void loop(){
}

Could someone explain this code to me line by line?
And answer the following questions:

  1. How many interrupt levels are available in the Arduino Due? Why?
    2.How many independent timers are available in the Arduino Due? Why?
    3.How can the timers be connected to the interrupt levels? Why?
    4.Can you vary the blinking frequency in this sketch? Why and How?
    5.Can you use a different timer and channel? Why and How?
    If you can explain those code line by line how this code work first that will be best, because I cannot understand those code in this moment.
    Very, very grateful for this. :wink:

The Arduino Due timers are documented in the Atmel SAM3X8E ARM Cortex-M3 CPU documentation.

The code in startTimer() is boilerplate and will serve you well as is.

There are 9 timers handled by three “timer counters” TCs: TC0, TC1, TC2.

Each Timer Counter (TC) is responsible for 3 “channels” these are each independent timers with independent ISRs.

The ISRs (interrupt service routines) are named:

TC0_IRQn, TC1_IRQn, …, TC8_IRQn

The 0…8 in the names is TC*3+channel

EDIT: In the handler, this line: TC_GetStatus(, );
is necessary to re-enable the timer interrupt. You will not get any further calls on this interrupt if this is not done.

HTH,
Stan

StanO8:
The Arduino Due timers are documented in the Atmel SAM3X8E ARM Cortex-M3 CPU documentation.

The code in startTimer() is boilerplate and will serve you well as is.

There are 9 timers handled by three “timer counters” TCs: TC0, TC1, TC2.

Each Timer Counter (TC) is responsible for 3 “channels” these are each independent timers with independent ISRs.

The ISRs (interrupt service routines) are named:

TC0_IRQn, TC1_IRQn, …, TC8_IRQn

The 0…8 in the names is TC*3+channel

EDIT: In the handler, this line: TC_GetStatus(, );
is necessary to re-enable the timer interrupt. You will not get any further calls on this interrupt if this is not done.

HTH,
Stan

Hi Stan,

Very thank you for your answer. Now I understand more of it. Is there any explanation of this code in the documentation? Can you give a link to that documentation? If you have time, please tell me more about this code and how TC and ISR work.

StanO8:
The Arduino Due timers are documented in the Atmel SAM3X8E ARM Cortex-M3 CPU documentation.

The code in startTimer() is boilerplate and will serve you well as is.

There are 9 timers handled by three “timer counters” TCs: TC0, TC1, TC2.

Each Timer Counter (TC) is responsible for 3 “channels” these are each independent timers with independent ISRs.

The ISRs (interrupt service routines) are named:

TC0_IRQn, TC1_IRQn, …, TC8_IRQn

The 0…8 in the names is TC*3+channel

EDIT: In the handler, this line: TC_GetStatus(, );
is necessary to re-enable the timer interrupt. You will not get any further calls on this interrupt if this is not done.

HTH,
Stan

I don’t know which section is about my questions? Could you point me out;)

Datasheet is 1467 pages:

This link is given right at the start of the official Due page.