How to implement timeouts in an embedded-hal-based driver?

I want to implement timeouts in my embedded-hal-based driver. For example, it will send request to uart, then wait for response with timeout.

As I understand, embedded_hal::timer::CountDown is the main trait for timers. These timers are not reusable: one instance is intended to fire once and then to become unusable:

Otherwise the behavior of calling wait after the last call returned Ok is UNSPECIFIED. Implementers are suggested to panic on this scenario to signal a programmer error.

I don't want each query method to take a timer as argument, it's too uncomfortable to use. I want timeout to be configured once when creating driver instance.

What should I do? Make driver instance to have a FnMut() -> C field (where C: CountDown)?

I'm not quite sure what you're asking.. You are implementing some timers using the CountDown trait, but you want timers initialized once at driver creation? Can't you just initialize it at that time and have CountDown be some sort of handle to the timer you initialized, then? I'm not quite sure which queries you're referring to.

No, according to documentation, such timers will be unusable after first use:

the behavior of calling wait after the last call returned Ok is UNSPECIFIED. Implementers are suggested to panic on this scenario to signal a programmer error.

For example, for temperature sensor, you will be able to measure temperature only once. After second call to get_temperature, CountDown timer used for timeout will panic.

Yes yes, what I meant was that you could share the state of every timer in some global, and have a cheap way of creating new timers and attaching them to the global? I.e. the CountDown is a handle to the global.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.