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

setInterval takes quite a long time to execute #18

Closed stripwax closed 2 years ago

stripwax commented 2 years ago

Perhaps not a bug in its own right as such (see also #17) , this is more of an inefficiency, or optimisation opportunity. The setInterval calls take quite a substantial number of cycles to execute - on the order of 100 us. While this might not be significant in most usage, in my usecase I am calling setInterval from my ISR, and this usecase is sensitive to the ISR executing as quickly as possible.

I have no access to debugging tools so I don't have actual measurements, but by checking micros() before/after call to setInterval I was able to register around 100us change.

There are some improvements that can be made to reduce the instruction count. The repeated masking-out of the prescalar flags, one bit at a time, and incurring the sync delay after each write, seems to correspond to about half of the cycle count - this can be done in one single write (and per datasheet this can actually be done in the same write as setting Enabled to false) . Another area where cycle count is high seems to be coming from the double manipulation to convert a period to a frequency and then convert it back to a period - in my experimentation, if you avoid the float conversion from period to frequency (setInterval calling setFrequency) and back again (setFrequency converting the frequency back to a period before calling setPeriod_TIMER_TC3), you can shave off a few 10s of us here. With some local changes I was finding my calls to setInterval were now around 70us rather than 110us .

stripwax commented 2 years ago

It might also be possible to avoid some of the setup steps that are currently being carried out in setFrequency - for example, if the timer has already been configured to 16 bit mode, and is already running, and interrupts are already enabled, then a subsequent call to setInterval does not need to carry out these setup instructions again.

stripwax commented 2 years ago

Hi, could you comment on why this issue had been marked Invalid? Do you disagree with the observations?

khoih-prog commented 2 years ago

Hi @stripwax

The new SAMD_TimerInterrupt releases v1.7.0 has just been published. Your contribution is noted in Contributions and Thanks.

I'm looking forward to receiving your setInterval() enhancements for SAMD21 TCC and SAMD51 TC3

Best Regards,


Releases v1.7.0

  1. Optimize code for setInterval() of SAMD21 TC3. Check setInterval on a running timer results in a period significantly longer than the specified period #17
  2. Update Packages_Patches
stripwax commented 2 years ago

I'm not working on such enhancements and I do not have access to any SAMD51 devices.

On Mon, 25 Apr 2022, 15:46 Khoi Hoang, @.***> wrote:

Hi @stripwax https://github.com/stripwax

The new SAMD_TimerInterrupt releases v1.7.0 https://github.com/khoih-prog/SAMD_TimerInterrupt/releases/tag/v1.7.0 has just been published. Your contribution is noted in Contributions and Thanks https://github.com/khoih-prog/SAMD_TimerInterrupt#contributions-and-thanks .

I'm looking forward to receiving your setInterval() enhancements for SAMD21 TCC and SAMD51 TC3

Best Regards,

Releases v1.7.0

  1. Optimize code for setInterval() of SAMD21 TC3. Check setInterval on a running timer results in a period significantly longer than the specified period #17 https://github.com/khoih-prog/SAMD_TimerInterrupt/issues/17
  2. Update Packages_Patches

— Reply to this email directly, view it on GitHub https://github.com/khoih-prog/SAMD_TimerInterrupt/issues/18#issuecomment-1108970486, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABF47HAE75IN4QAD4MJ2BM3VG3ZAFANCNFSM5TBIYC2A . You are receiving this because you were mentioned.Message ID: @.***>