How to sleep in no_std?

I want to sleep in no std (a loop sending request).

A #[no_std] program doesn't have access to an operating system, so whichever mechanism you use to sleep will depend entirely upon your target platform.

The embedded-hal contains an embedded_hal::blocking::delay module which defines things like DelayUs and DelayMs. Because the embedded-hal is just a Hardware Abstraction Layer you'll need to find the appropriate implementation for your device.

For example, the stm32f1xx_hal crate provides a stm32f1xx_hal::delay::Delay type for STM32 F1 devices which is based on the system timer (the SysTick interrupt).

It's not possible to give you more detailed information without knowing which device you want to run this on.


What about wasm32-unknown-unknown?

If you're running in a browser there's no way to sleep directly, you'd need to call the setTimeout() JavaScript function. Probably with Window::set_timeout_with_callback_and_timeout_and_arguments_0() from the web-sys crate.


Hang on... If you're using wasm32-unknown-unknown I'm assuming this is something that's embedded in the browser or called from Node.js. If you're sending a request, shouldn't it return a promise which will be resolved when the response is received?

If you get a promise then you can schedule a function to be invoked when the response is received by calling the promise's .then() method. The js-sys crate already lets you call this from Rust using Promise::then().


The request may failed. I want to wait maybe 3 seconds then send it again.

You can use clearTimeout() to cancel a function scheduled with setTimeout().

It's written in JavaScript, but Javascript fetch, retry upon failure may give you an idea for how this can be implemented.

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