From d9af4a635e71ad5354415b521b7761adf3f7d10b Mon Sep 17 00:00:00 2001 From: Richard Meadows <962920+richardeoin@users.noreply.github.com> Date: Mon, 11 Sep 2023 23:02:09 +0200 Subject: [PATCH] Replace casts of &T to &mut T with UnsafeCell Specifically we cast *const T to *const UnsafeCell then use the [UnsafeCell::raw_get](https://doc.rust-lang.org/stable/std/cell/struct.UnsafeCell.html#method.raw_get) method --- src/serial.rs | 6 ++++-- src/spi.rs | 7 +++++-- src/xspi/mod.rs | 9 +++++++-- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/src/serial.rs b/src/serial.rs index 845ac499..2eee59e2 100644 --- a/src/serial.rs +++ b/src/serial.rs @@ -7,6 +7,7 @@ //! - [Advanced USART Functions](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/serial-advanced.rs) //! - [Inverted Signal Levels](https://github.com/stm32-rs/stm32h7xx-hal/blob/master/examples/serial-inverted-loopback.rs) +use core::cell::UnsafeCell; use core::fmt; use core::marker::PhantomData; use core::ptr; @@ -1114,8 +1115,9 @@ macro_rules! usart { // NOTE(write_volatile) 8-bit write that's not // possible through the svd2rust API unsafe { - ptr::write_volatile( - &(*$USARTX::ptr()).tdr as *const _ as *mut _, byte) + let tdr = &(*$USARTX::ptr()).tdr as *const _ as *const UnsafeCell; + + ptr::write_volatile(UnsafeCell::raw_get(tdr), byte) } Ok(()) } else { diff --git a/src/spi.rs b/src/spi.rs index 44edc11f..3247a0e2 100644 --- a/src/spi.rs +++ b/src/spi.rs @@ -62,6 +62,7 @@ //! //! [embedded_hal]: https://docs.rs/embedded-hal/0.2.3/embedded_hal/spi/index.html +use core::cell::UnsafeCell; use core::convert::From; use core::marker::PhantomData; use core::ptr; @@ -1083,8 +1084,9 @@ macro_rules! spi { { // NOTE(write_volatile) see note above unsafe { + let txdr = &self.spi.txdr as *const _ as *const UnsafeCell<$TY>; ptr::write_volatile( - &self.spi.txdr as *const _ as *mut $TY, + UnsafeCell::raw_get(txdr), word, ) } @@ -1112,8 +1114,9 @@ macro_rules! spi { { // NOTE(write_volatile/read_volatile) write/read only 1 word unsafe { + let txdr = &self.spi.txdr as *const _ as *const UnsafeCell<$TY>; ptr::write_volatile( - &self.spi.txdr as *const _ as *mut $TY, + UnsafeCell::raw_get(txdr), word, ); return Ok(ptr::read_volatile( diff --git a/src/xspi/mod.rs b/src/xspi/mod.rs index e536c760..cdcd8a23 100644 --- a/src/xspi/mod.rs +++ b/src/xspi/mod.rs @@ -143,6 +143,7 @@ mod common { stm32, time::Hertz, }; + use core::cell::UnsafeCell; use core::{marker::PhantomData, ptr}; /// Represents operation modes of the XSPI interface. @@ -641,7 +642,9 @@ mod common { // Write data to the FIFO in a byte-wise manner. unsafe { for byte in data { - ptr::write_volatile(&self.rb.dr as *const _ as *mut u8, *byte); + let dr = &self.rb.dr as *const _ as *const UnsafeCell; + + ptr::write_volatile(UnsafeCell::raw_get(dr), *byte); } } @@ -723,7 +726,9 @@ mod common { // Transaction starts here unsafe { for byte in data { - ptr::write_volatile(&self.rb.dr as *const _ as *mut u8, *byte); + let dr = &self.rb.dr as *const _ as *const UnsafeCell; + + ptr::write_volatile(UnsafeCell::raw_get(dr), *byte); } }