SAM Timer counter / atmel ASF
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-&gt;TC_CHANNEL[0].TC_SR;  

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

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

}  // TC0_Handler