You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Is there a reason that the embedded_hal::delay::DelayUs API is not defined as follows:
use core::time::Duration;pubtraitDelay{fndelay(&mutself,duration:Duration);#[inline]fndelay_us(&mutself,us:u32){self.delay(Duration::from_micros(us))}#[inline]fndelay_ms(&mutself,ms:u32){self.delay(Duration::from_millis(us))}}impl<T:Delay>Delayfor&mutT{ ... }
I haven't actually implemented this API myself, but it seems like this would be both much cleaner, and much more flexible (e.g. allowing nanosecond resolution as well)?
Is the issue that core::time::Duration is too large to pass around?
Another oddity: DelayUs from embedded-hal has a default implementation for delay_ms, while the same trait from embedded-hal-async does not have this default implementation.
Fair enough, though perhaps it would still makes sense to design the API as I've outlined above?
That way, people on 32-bit targets could still use the usual delay_ms/delay_us methods if they're concerned about 64-bit numbers (implementers could provide efficient versions for each of these), while most people could use the usual delay method (which after inlining would likely compile down to the same thing, if we assume the common case of a constant duration).
Another oddity: DelayUs from embedded-hal has a default implementation for delay_ms, while the same trait from embedded-hal-async does not have this default implementation.
Thanks for pointing this out! This was due to a limitation in async functions in traits when it was added, but it's now possible so that's been fixed (see #535).
Fair enough, though perhaps it would still makes sense to design the API as I've outlined above?
The problem is that the end users (who've picked a target) aren't the ones calling delay: it's driver authors, writing platform-agnostic code, that call it from some generic Delay provider. The end user can't choose which delay a driver called, so they will likely end up with delays based on core's Duration -- which is 12 bytes in the end (u64 seconds + u32 nanoseconds), so the computation and memory overhead could be significant.
We have however just changed (in #537) the trait to permit nanosecond resolution, although many platforms likely will round up significantly (e.g. probably to the next highest µs).
The hope is that one day we can invent a suitable Duration type that everyone is happy with, even on restricted 8/16-bit platforms, and in that case we'll introduce a new Delay trait that uses it and in the meantime we have DelayNs which provides for delay_ms/delay_us/delay_ns. There is also a possibility of calling the current trait Delay (#541) and either giving up on finding a Duration, or if we do, just adding it as a new method to Delay.
Activity
madsmtm commentedon Nov 21, 2023
Another oddity:
DelayUsfromembedded-halhas a default implementation fordelay_ms, while the same trait fromembedded-hal-asyncdoes not have this default implementation.ryankurte commentedon Nov 21, 2023
iirc this is because
core::time::Durationis 64-bit and we didn't want to impose that on (what are predominantly) 32-bit targetsmadsmtm commentedon Nov 21, 2023
Fair enough, though perhaps it would still makes sense to design the API as I've outlined above?
That way, people on 32-bit targets could still use the usual
delay_ms/delay_usmethods if they're concerned about 64-bit numbers (implementers could provide efficient versions for each of these), while most people could use the usualdelaymethod (which after inlining would likely compile down to the same thing, if we assume the common case of a constant duration).adamgreig commentedon Dec 2, 2023
Thanks for pointing this out! This was due to a limitation in async functions in traits when it was added, but it's now possible so that's been fixed (see #535).
The problem is that the end users (who've picked a target) aren't the ones calling
delay: it's driver authors, writing platform-agnostic code, that call it from some genericDelayprovider. The end user can't choose which delay a driver called, so they will likely end up with delays based on core'sDuration-- which is 12 bytes in the end (u64 seconds + u32 nanoseconds), so the computation and memory overhead could be significant.We have however just changed (in #537) the trait to permit nanosecond resolution, although many platforms likely will round up significantly (e.g. probably to the next highest µs).
The hope is that one day we can invent a suitable
Durationtype that everyone is happy with, even on restricted 8/16-bit platforms, and in that case we'll introduce a newDelaytrait that uses it and in the meantime we haveDelayNswhich provides for delay_ms/delay_us/delay_ns. There is also a possibility of calling the current traitDelay(#541) and either giving up on finding aDuration, or if we do, just adding it as a new method toDelay.