Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Time-Keeping Library #256

Open
Cryptjar opened this issue Apr 12, 2022 · 1 comment · May be fixed by #257
Open

Time-Keeping Library #256

Cryptjar opened this issue Apr 12, 2022 · 1 comment · May be fixed by #257

Comments

@Cryptjar
Copy link

I was recently playing around with trying to benchmark stuff on my Arduino Uno. Thus, I wanted to get a time source on the Arduino to check how long executing some code takes. Of course, I read your awesome blog post about writing a millis() function

Unfortunately, milliseconds were a bit coarse for me, so I went ahead and also implemented a micros() function. This involved quite a bit of tricky code, and so I'm wondering about how to turn this into a library. My code so far.

Foremost, I'm wondering where would be the best place to put such a library:

  • as part of avr-hal, maybe arduino-hal (but as you mentioned in your blog post, statically hooking up an interrupt, might be not appropriate for the HAL)
  • or, as a standalone library, but the HAL crates aren't released yet, which makes depending on them difficult

Any advice is welcome.

@Cryptjar
Copy link
Author

I prototyped a bit more, and I think, I tackle these design goals:

  • Be generic over the timer hardware to be used (e.g. TC0 vs TC1 on Uno)
  • Support various CPU-clock rates and resolutions (i.e. interrupt intervals: precision vs overhead)
  • Add a macro with which the user "prepares" a hardware timer to be used for time-keeping (i.e. implementing the timer interrupt handle). This way, the HAL doesn't implement the interrupt handlers itself.

My implementation plan so far is to:

  • Add a trait (e.g. TimerOps) to avr-hal-generic that abstracts over timers that are usable for this time-keeping purpose
  • Implement TimerOps in the respective MCU crates (e.g. atmega_hal) for all eligible timers (e.g. TC0 & TC1 for atmega328p)
  • Add a macro to the MCU crates that allows to define the interrupt handler for a specific hardware timer
  • Add a trait (e.g. PreparedTimer) to arduino_hal, that wraps a specific hardware timer register type and ensures that the user implemented the respective timer interrupt.
  • Also add a macro to arduino_hal to let the user easily create a wrapper struct that implements PreparedTimer for them, including the interrupt handler for the selected hardware timer
  • Add a "Clock" wrapper struct in arduino_hal that implements the clock interface (via embedded-time / embedded-hal) based on a value of PreparedTimer

I intend to prepare a PR soon.

I guess this essentially resolves the placing question that I had earlier.
However, naming has become a sort of challenge, especially considering terms such as "Clock" and "Timer", which seem a bit overused.

@Cryptjar Cryptjar linked a pull request Apr 18, 2022 that will close this issue
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant