SAMD21 Timer library for the Arduino Zero

See the full article here.

Intro
For the SAM15x15 Arduino compatible board, which uses the SAMD21G, I needed a timer library. Unfortunately, such a library didn't exist, so I have time invested in its development myself.

Without having a sophisticated library, using the Timer/Counter is a complicated task and you have to go into many details such as counter modes, compare/capture channels, prescaler values etc. Here, a proper library makes life easier. I'm lazy, the only thing I want to do is specify the period time and the output pin, and the rest should be figured out by the library.

Download
Download the avdweb_SAMDtimer library from GitHub, you will find here program examples too.
The Adafruit_ZeroTimer library should also be installed.

Using a timer with an output pin
The library is easy to use; the following code generates a square wave of 1Hz to pin 5 of the Arduino Zero:

SAMDtimer mytimer1 = SAMDtimer(3, TC_COUNTER_SIZE_16BIT, 5, 1e6);

Notes

  • The maximum period time is 1398080us (0.7Hz).
  • The minimum period time is = 1us (1MHz).
  • For now, the library has three timers available (number 3, 4, 5), only in 16-bit mode.
  • There is no check on the proper parameters values.
  • Without specifying the pulse width, it is the half of the period time (duty cycle 50%).

Timer interrupts
A timer can also be used for calling interrupts, without using a timer output pin. To periodically execute an ISR, simply use this code:

SAMDtimer mytimer2 = SAMDtimer(4, myISR, 5e5);

Attaching interrupts to a timer with an output pin
A timer can be used for both calling an interrupt and steering its output pin at the same time. This has to be carried out in two steps: first create a timer with an output pin, and than attach an ISR to it.

Mytimer3.attachInterrupt(myISR);

See the full article here.

When I include libraries my code suddenly can't compile and shows the error "expected '}' at end of input".
Any idea why?

Found the problem

Adafruit_ASFcore Library / system_interrupt.h / Line 426

#ifdef __cplusplus
//}       // remove due to nested extern C' complaints
#endif

In someones infinite wisdom, they wrongly blamed and commented out the bracket to fix a problem caused by there code elsewhere

How would you reset the interrupt counter?

jojojijijojo:
How would you reset the interrupt counter?

Yes, I would be interested to know how to reset the interrupt counter, too.

I am having the same problem when I use the Arduino Create IDE. The compiler flags an error on line 426 of the system_interrupt.h file, as previously reported.

The library that I downloaded already did have the change that starwarslegokid suggested.

BTW, the libraries worked with the desktop IDE.

Any ideas?

I am a newbi. I have been trying to get a little TAU (RabidPrototypes) micro to do some multitasking using the timer interrupts. I have tried all of the ZERO timer libraries I could find including SAMD libraries. NEVER WORKED.
Is there someone out there who has managed to do this successfully?
Believe me, I am no expert and so tend to copy proven code.
Gilles