khoih-prog / SAMD_TimerInterrupt

This library enables you to use Interrupt from Hardware Timers on an SAMD-based board. These SAMD Hardware Timers, using Interrupt, still work even if other functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's mandatory if you need to measure some data requiring better accuracy. It now supports 16 ISR-based Timers, while consuming only 1 Hardware Timer. Timers' interval is very long (ulong millisecs). The most important feature is they're ISR-based Timers. Therefore, their executions are not blocked by bad-behaving functions or tasks. This important feature is absolutely necessary for mission-critical tasks. SAMD21 now can use 6 Timers
MIT License
34 stars 15 forks source link

guidance #4

Closed sdetweil closed 3 years ago

sdetweil commented 3 years ago

I am using NANO IOT, SAMD board all I want is a timer2 isr routine to count so I can replace millis() which uses timer 0 and I have to use low power sleep which kills timer 0.

but I am missing some terminology, and haven't found a place to get it. from the sample

// You can select SAMD Hardware Timer  from SAMD_TIMER_1 or SAMD_TIMER_3

// Depending on the board, you can select SAMD21 Hardware Timer from TC3-TCC
// SAMD21 Hardware Timer from TC3 or TCC
// SAMD51 Hardware Timer only TC3

// Init SAMD timer TIMER_TC3
SAMDTimer ITimer(TIMER_TC3);

what is the difference between SAMD21 or SAMD51 and what is TCC and TC3 and why would I pick one of the other and how does all this relate to timer2?

I have written ISR in just about every language on just about every hardware type, so all of the process is not the problem

your library looks like it encapsulates all the prescaler and other technical goop I don't want to know about, which is wonderful. just need to get over the hump somehow.

(and my name is Sam D... which is pretty coincidental)

sdetweil commented 3 years ago

and then theres this

define HW_TIMER_INTERVAL_MS 1L // so I think MS means Mili-seconds as Micro is usually codes uS

// Interval in microsecs ... then the comment says micro if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler)) // but this seems to imply 1 second

or it means you want Millisecond interval, but the timer is microseconds in which case I would change the constant to

define HW_TIMER_INTERVAL_MS 1000L

and take the multiply out

just trying to understand the words

sdetweil commented 3 years ago

using either TCC or TC3 both work

thank you for a great library!

khoih-prog commented 3 years ago

Thanks for your interest in the library, and I'm glad to have chance to work with a SAMD-related person here ;-)

You're using NANO_33_IOT, which is using SAMD21 MCU and you can use both TC3 and TCC timers. You have to pick the timer not being used by the core or other libraries, you have to read or try to know.

define HW_TIMER_INTERVAL_MS 1L // so I think MS means Mili-seconds as Micro is usually codes uS

// Interval in microsecs ... then the comment says micro if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler)) // but this seems to imply 1 second

The timer count unit in

ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_MS * 1000, TimerHandler);

is in microseconds (which in HW or SW, we use uS, while ms is for miilisecond).

So if you're using HW_TIMER_INTERVAL_MS in ms, you have to multiply to 1000 to convert to uS.

Or if you're defining in uS, you can use it directly

ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler);

Thanks and happy exploring,

Best Regards,