Skip to content

Commit 36e3a8a

Browse files
committed
replace static muts with pointers
1 parent 3ca6b50 commit 36e3a8a

File tree

3 files changed

+13
-12
lines changed

3 files changed

+13
-12
lines changed

hifive1/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
66
and this project adheres to [Semantic Versioning](http://semver.org/).
77

88
## [Unreleased]
9+
- Replace static muts with Mutexes
910
- Apply clippy changes
1011
- Bump MSRV to 1.72
1112
- Adapt to new Cargo workspace

hifive1/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ edition = "2021"
1111
rust-version = "1.72"
1212

1313
[dependencies]
14+
critical-section = { version = "1.1.3" }
1415
e310x-hal = { path = "../e310x-hal", version = "0.11.0" }
1516
embedded-hal = "0.2.7"
1617
riscv = "0.10.1"

hifive1/src/stdout.rs

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
//! Stdout based on the UART hooked up to FTDI or J-Link
22
3-
use core::fmt;
3+
use core::{fmt, ptr};
44
use e310x_hal::{
55
clock::Clocks,
66
e310x::Uart0,
@@ -10,12 +10,11 @@ use e310x_hal::{
1010
time::Bps,
1111
};
1212
use nb::block;
13-
use riscv::interrupt;
14-
15-
static mut STDOUT: Option<SerialWrapper> = None;
1613

1714
struct SerialWrapper(Tx<Uart0>);
1815

16+
static mut STDOUT: Option<SerialWrapper> = None;
17+
1918
impl core::fmt::Write for SerialWrapper {
2019
fn write_str(&mut self, s: &str) -> fmt::Result {
2120
for byte in s.as_bytes() {
@@ -50,28 +49,28 @@ pub fn configure<X, Y>(
5049
let serial = Serial::new(uart, (tx, rx), baud_rate, clocks);
5150
let (tx, rx) = serial.split();
5251

53-
interrupt::free(|| unsafe {
54-
STDOUT.replace(SerialWrapper(tx));
52+
critical_section::with(|_| {
53+
unsafe { &mut *ptr::addr_of_mut!(STDOUT) }.replace(SerialWrapper(tx));
5554
});
5655
rx
5756
}
5857

5958
/// Writes string to stdout
6059
pub fn write_str(s: &str) {
61-
interrupt::free(|| unsafe {
62-
if let Some(stdout) = STDOUT.as_mut() {
60+
critical_section::with(|_| {
61+
if let Some(stdout) = unsafe { &mut *ptr::addr_of_mut!(STDOUT) } {
6362
let _ = stdout.write_str(s);
6463
}
65-
})
64+
});
6665
}
6766

6867
/// Writes formatted string to stdout
6968
pub fn write_fmt(args: fmt::Arguments) {
70-
interrupt::free(|| unsafe {
71-
if let Some(stdout) = STDOUT.as_mut() {
69+
critical_section::with(|_| {
70+
if let Some(stdout) = unsafe { &mut *ptr::addr_of_mut!(STDOUT) } {
7271
let _ = stdout.write_fmt(args);
7372
}
74-
})
73+
});
7574
}
7675

7776
/// Macro for printing to the serial standard output

0 commit comments

Comments
 (0)