Skip to content

Commit

Permalink
Replace casts of &T to &mut T with UnsafeCell
Browse files Browse the repository at this point in the history
Specifically we cast *const T to *const UnsafeCell<T> then use the
[UnsafeCell::raw_get](https://doc.rust-lang.org/stable/std/cell/struct.UnsafeCell.html#method.raw_get)
method
  • Loading branch information
richardeoin committed Sep 17, 2023
1 parent 741e37c commit d9af4a6
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
6 changes: 4 additions & 2 deletions src/serial.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<u8>;

ptr::write_volatile(UnsafeCell::raw_get(tdr), byte)
}
Ok(())
} else {
Expand Down
7 changes: 5 additions & 2 deletions src/spi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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,
)
}
Expand Down Expand Up @@ -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(
Expand Down
9 changes: 7 additions & 2 deletions src/xspi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<u8>;

ptr::write_volatile(UnsafeCell::raw_get(dr), *byte);
}
}

Expand Down Expand Up @@ -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<u8>;

ptr::write_volatile(UnsafeCell::raw_get(dr), *byte);
}
}

Expand Down

0 comments on commit d9af4a6

Please sign in to comment.