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

Add asm::delay_cycles #127

Merged
merged 1 commit into from
May 8, 2023
Merged

Add asm::delay_cycles #127

merged 1 commit into from
May 8, 2023

Conversation

agausmann
Copy link
Contributor

This is a counterpart to cortex_m::asm::delay but for AVR.

It is intended for very simple delays in low-level drivers that don't require precision or real-time accuracy.

In those situations, it provides these benefits over the alternatives:

  • Compared to Delay from avr-hal, this dependency is more convenient; Sometimes it is not desired or not even possible to depend on avr-hal, and also the driver does not need to know the CPU clock frequency (if it doesn't care about 16x longer delays at 1MHz vs 16MHz).

  • Compared to using hardware timers, this doesn't require ownership of any system resources, leaving more for the end user.

@agausmann
Copy link
Contributor Author

agausmann commented May 7, 2023

One example where this is useful: Adding a "debouncing delay" between detach and attach of USB pull resistors, where it doesn't really matter if the delay is 1ms or 16ms, and the user API is significantly simplified by not using timers or hal::Delay.

Work in progress here: agausmann/atmega-usbd#13

Based on the implementation of the same API in the nRF usb-device driver, which uses cortex_m::asm::delay

@agausmann
Copy link
Contributor Author

agausmann commented May 7, 2023

This could also be used to re-implement avr_hal_generic::Delay for accurate delays up to ~250 seconds at 16 MHz (max counter value ~4 billion, divided by 16 MHz), which should be plenty for the majority of Delay use cases

This is a counterpart to `cortex_m::asm::delay` but for AVR.

It is intended for very simple delays in low-level drivers
that don't require precision or real-time accuracy.

In those situations, it provides these benefits over the alternatives:

- Compared to `Delay` from avr-hal, this dependency is more convenient;
Sometimes it is not desired or not even possible to depend on avr-hal,
and also the driver does not need to know the CPU clock frequency
(if it doesn't care about 16x longer delays at 1MHz vs 16MHz).

- Compared to using hardware timers, this doesn't require ownership of
any system resources, leaving more for the end user.
Copy link
Owner

@Rahix Rahix left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi, thanks for the contribution.

I am okay with adding this, but I'd really like to see something along side it which uses real time. I think delay_cycles() has big potential for leading to hard to debug problems when people reuse code on different clock rates...

I am wondering whether we should just move the delay & clock code from https://github.com/Rahix/avr-hal/blob/main/avr-hal-generic/src/delay.rs into avr-device as it doesn't really depend on anything anyway.

@agausmann
Copy link
Contributor Author

I am wondering whether we should just move the delay & clock code into avr-device

Yeah, that would also be a good idea.

I still think it would be useful to have a public cycle-based delay. Of course the documentation should have warnings and recommendations to use other APIs, which I did add.

In my case, the clock variation is really not a concern. Almost every scenario where you have USB, you are going to have a 16MHz oscillator due to the clock requirements of the USB (and therefore probably a 16MHz CPU clock, unless CKDIV8 fuse is enabled)

@Rahix Rahix merged commit a458e4c into Rahix:main May 8, 2023
@agausmann agausmann deleted the delay-cycles branch May 8, 2023 17:30
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 this pull request may close these issues.

2 participants