Electronic – SAM Timer counter / atmel ASF

atmelccortex-m4embedded

I'm trying to implement function call after having timeroverflow or comparison of counter registers.

Chip: SAM4E

tc_init(TC0, 2, TC_CMR_TCCLKS_TIMER_CLOCK3);
tc_write_rc(TC0, 2, 750000);
tc_enable_interrupt(TC0,2,TC_IER_COVFS | TC_IER_CPCS);

NVIC_DisableIRQ(TC2_IRQn);
NVIC_ClearPendingIRQ(TC2_IRQn);
NVIC_SetPriority(TC2_IRQn, 0);
NVIC_EnableIRQ(TC2_IRQn);


tc_start(TC0,2);

I tried many codes that didn't work. I had to switch to TC0 so I could see the timer value actually changing:

(TC0->TC_CHANNEL[2].TC_CV)

void TC2_Handler(void)
{
    ii++;
}

unfortunately this code doesn't work. (it has a bug that goes to infinite loop).

i can see timer value changing with this code but there is no interrupt handler working:

 sysclk_enable_peripheral_clock(ID_TC2);

tc_init(TC0, 2, TC_CMR_TCCLKS_TIMER_CLOCK3);
tc_write_rc(TC0, 2, 75000);
tc_enable_interrupt(TC0,2, TC_IER_COVFS  );

NVIC_DisableIRQ(TC2_IRQn);
NVIC_ClearPendingIRQ(TC2_IRQn);
NVIC_SetPriority(TC2_IRQn, 0);
NVIC_EnableIRQ(TC2_IRQn);


tc_start(TC0,2);

why is this not correct code?
why 'TC_IER_COVFS' doesn't trigger TC2_handler(). and it does not work also. (although i can see timer started)…
why 'TC_IER_CPCS' trigger fault ( i guess it goes to dummy_handler!).
what would be the correct way to fix this

Best Answer

unfortunately this code doesn't work. (it has a bug that goes to infinite loop).

It is not an infinite loop: you do not clear the interrupt bit so when you return the interrupt is still pending and it goes back into the service routine.

I found some code I wrote a time ago for the SAM:

void TC0_Handler(void)
{ uint32_t dummy_sr;
  // must read status register to clear interrupt
  // This is unfortunately not (clearly?) mention in the datasheet
  dummy_sr = TC0->TC_CHANNEL[0].TC_SR;  

  // If we are running in one-shot mode disable the timer
  if (one_shot)
     TC0->TC_CHANNEL[0].TC_CCR = TC_CCR_CLKDIS;

  // Call the call back function
  (*t0_handler)();

}  // TC0_Handler