Skip to content

SAMG55 Timer Counter

The datasheet of the SAMG55 MCU and the ASF for it seem to have some inconsistencies between themselves, making using the timer counter module for signal generation and/or triggering interrupts a nightmare. I have written down some of my findings here, for those people Googling around, hoping to know why they cannot get it to work.

Because I don’t wish trying to figure this out for anyone upon themselves I have written them down here for some clarification.

In the datasheet it refers to Timer Counter A and Timer Counter B. Within the asf module these are known as TC0 and TC1.

The timer counter on the SAMG55 consists of 6 channels. Three of these are bound to TC0 and three of these are bound to TC1. Confusingly, these channels are labeled 0…5, meaning that TC0 is both the first timer/counter and also channel 0 of TC0 itself. Please see table below for clarification.

Timer/CounterChannelName
TC00TC0
TC01TC1
TC02TC2
TC10TC3
TC11TC4
TC12TC5
Mapping of the SAMG55 Timer Counter Modules

Furthermore, it seems like the peripheral clock is not enabled per peripheral (TC0 and TC1) but by channel (TC0..TC5) So if you want to use TC1 Channel 0 (So TC3) you need to enable the clock for ID_TC3.

Example

In order to enable interrupts on the first channel of timer counter 1. (So TC3) The following code will suffice.

sysclk_enable_peripheral_clock(ID_TC3); //Enable channel 3's clock.

uint32_t tc_configuration = TC_CMR_TCCLKS_TIMER_CLOCK1|TC_CMR_CPCTRG; //Set clk to MCK/2 and reset the counter on capcture compare register C.

tc_init(TC1,0,tc_configuration);           //Init
tc_enable_interrupt(TC1,0,TC_IER_CPCS);    //Enable the compare C interrupt
tc_write_rc(TC1,0,1024);  //Write a value to register C
tc_start(TC1,0);

//Set the interrupt priority for TC3
NVIC_DisableIRQ(TC3_IRQn);
NVIC_ClearPendingIRQ(TC3_IRQn);
NVIC_SetPriority(TC3_IRQn, 2);
NVIC_EnableIRQ(TC3_IRQn);

The above example should configure channel 3, which is the first channel of timer/counter 1 to trigger an interrupt. This interrupt triggers TC3_Handler. This took me far too long to figure out, so I hope it does not for you.

Leave a Reply

Your email address will not be published. Required fields are marked *