Skip to content

Commit 47a4a26

Browse files
committed
replace static muts with mutexes
1 parent 3ca6b50 commit 47a4a26

File tree

3 files changed

+14
-13
lines changed

3 files changed

+14
-13
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: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
//! Stdout based on the UART hooked up to FTDI or J-Link
22
3-
use core::fmt;
3+
use core::{cell::RefCell, fmt};
4+
use critical_section::Mutex;
45
use e310x_hal::{
56
clock::Clocks,
67
e310x::Uart0,
@@ -10,12 +11,11 @@ use e310x_hal::{
1011
time::Bps,
1112
};
1213
use nb::block;
13-
use riscv::interrupt;
14-
15-
static mut STDOUT: Option<SerialWrapper> = None;
1614

1715
struct SerialWrapper(Tx<Uart0>);
1816

17+
static STDOUT: Mutex<RefCell<Option<SerialWrapper>>> = Mutex::new(RefCell::new(None));
18+
1919
impl core::fmt::Write for SerialWrapper {
2020
fn write_str(&mut self, s: &str) -> fmt::Result {
2121
for byte in s.as_bytes() {
@@ -50,28 +50,27 @@ pub fn configure<X, Y>(
5050
let serial = Serial::new(uart, (tx, rx), baud_rate, clocks);
5151
let (tx, rx) = serial.split();
5252

53-
interrupt::free(|| unsafe {
54-
STDOUT.replace(SerialWrapper(tx));
55-
});
53+
critical_section::with(|cs| STDOUT.borrow(cs).replace(Some(SerialWrapper(tx))));
54+
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(|cs| {
61+
if let Some(stdout) = STDOUT.borrow(cs).borrow_mut().as_mut() {
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(|cs| {
70+
if let Some(stdout) = STDOUT.borrow(cs).borrow_mut().as_mut() {
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)