with the given example from here: What is the use of __IO & static keywords in C?, I am working on converting some C files that are using microcontrollers to rust
I would like to know the equivalent of this volatile type in rust
i am aware of unsafe code in rust: is the __IO alternative an example of this?
I'm using a STM32f4XX type HAL
Asked
Active
Viewed 131 times
1

AthulMuralidhar
- 662
- 1
- 9
- 26
-
1Perhaps this will help: https://docs.rs/volatile/latest/volatile/ It came from a web search on: `volatile rust` – Craig Estey Dec 13 '22 at 17:52
-
2Rust doesn't have `volatile` types. Instead, it has [volatile read/write pointer operations](https://doc.rust-lang.org/stable/std/ptr/fn.read_volatile.html). You must use those or some wrapper around them, as mentioned by Craig. – PitaJ Dec 13 '22 at 17:55
-
i think the write pointer ops is what i was looking for, thx @PitaJ :) will check that one out and see if it works for my use case :) – AthulMuralidhar Dec 13 '22 at 18:03
-
2__IO is simply `volatile` in STM32 HAL – 0___________ Dec 13 '22 at 21:46
1 Answers
1
All the I/O qualifier macros are defined in core_cm4.h (normally included indirectly via stm324xx.h) thus:
#ifdef __cplusplus
#define __I volatile /*!< Defines 'read only' permissions */
#else
#define __I volatile const /*!< Defines 'read only' permissions */
#endif
#define __O volatile /*!< Defines 'write only' permissions */
#define __IO volatile /*!< Defines 'read / write' permissions */
as you can see they are simply aliases for standard C qualifiers and not themselves C keyword or even compiler extensions - they are part of the CMSIS.
I am no Rust expert, but it seems that there is no direct equivalent in Rust, in that you cannot simply qualify a variable to ensure all accesses are explicit, so memory-mapped I/O uses a different approach. There is an mmio crate and read_volatile
/write_volatile
and also a Volatile
wrapper, all of which may be relevant to this issue.

Clifford
- 88,407
- 13
- 85
- 165
-
all the comments in the op was super helpful as well - thanks for the explanation wrt the c part of things :) – AthulMuralidhar Dec 30 '22 at 02:51