Timer Interrupts on Due

A brief explanation:
The SAM3X8E CPU has 3 Timer Counters (TC) they are called TC0, TC1, TC2.
Every Timer Counter contains 3 Channels numbered 0, 1 and 2 (this give us a total of 9 Channels).
Every Channel has its own counters and interrupt handler that are independent from other Channels.

In other words each Channel can be considered as a separate "Timer", and is like having 9 separate timers.

To initialize a Channel you need the following parameters:

TC0/TC1/TC2 - The Timer Counter instance
0 / 1 / 2 - The Channel number inside Timer Counter

If you want to use interrupts you must enable the NVIC (Nested Vector Interrupt Controller) for that channel with:

NVIC_EnableIRQ(TCx_IRQn);

where TCx_IRQn is the ID of the interrupt to enable. These id are listed in the following table together with the ISR handler function name:

TC Chan NVIC "irq" IRQ handler function PMC id
TC0 0 TC0_IRQn TC0_Handler ID_TC0
TC0 1 TC1_IRQn TC1_Handler ID_TC1
TC0 2 TC2_IRQn TC2_Handler ID_TC2
TC1 0 TC3_IRQn TC3_Handler ID_TC3
TC1 1 TC4_IRQn TC4_Handler ID_TC4
TC1 2 TC5_IRQn TC5_Handler ID_TC5
TC2 0 TC6_IRQn TC6_Handler ID_TC6
TC2 1 TC7_IRQn TC7_Handler ID_TC7
TC2 2 TC8_IRQn TC8_Handler ID_TC8

(note that TC2_IRQn is the irq id for TC0-channel-2 not for TC2...)

but this is still not enough! Every peripheral in the SAM3X is off by default (to save power) and should be turned on. To turn on you need to run the following command:

pmc_enable_periph_clk(id);

where id is found on the last column of the above table (ID_TCx). It happened that ID_TCx constant equals TCx_IRQn, so Sebastian Vik has simplified a bit the function using TCx_IRQn as input for pmc_enable_periph_clk:

pmc_enable_periph_clk((uint32_t)irq);

Hope this helps to decode whats happening with timers inside SAM3X.

Selachii:
Are there any ready-to-use-libs (like Arduino Playground - Timer1) for using Hardware Timer on Due?

Or is there a beginner-friendly How-To (like http://www.engblaze.com/microcontroller-tutorial-avr-and-arduino-timer-interrupts/) for the Due?

Nope, there are no libs neither tutorials.

I've planned a SAM3Timer library (to simplify implementation of Arduino Core and some libraries) but I didn't started it yet.

Volunteers? :slight_smile:

2 Likes