rust-embedded / svd2rust

Generate Rust register maps (`struct`s) from SVD files
Apache License 2.0
692 stars 150 forks source link

Add support for atomic operations in microcontrollers that support it #197

Open dvc94ch opened 6 years ago

dvc94ch commented 6 years ago

svd2rust should generate an additional (set, clear, toggle or and, or, xor) operations when atomic's are supported. While ARM MCU's provide stateless registers, the E310-G000 does not. Instead it provides AMOAND, AMOOR, AMOXOR instructions to be used instead.

This currently makes hard to implement embedded-hal traits without violating safety, since turning an OutputPin high requires a modify operation, which is not atomic.

luojia65 commented 4 years ago

Atomic modify operation is verified in this HAL crate: gpio.rs file. I believe we can refer to this pull request: https://github.com/rust-embedded/svd2rust/pull/407 We may always perform atomic operations on chips, which falls back to normal read-and-write when atomic operations are not supported. By this optimization we would save output binary length as we could make more use of instructions provided. This optimization may not accelerate runtime speed, would result in faster or (at least) the same speed; that's because for some chips they just unwrap atomic instructions into normal read-and-write after prepocessed instruction fetch. But considering code size, it would be worth a try to do this optimization .

There's another problem on memory order for atomic modify if there is one. There are many memory Orderings in standard library. Which ordering should we choose for modify function?