Skip to content

Commit

Permalink
replace static muts with mutexes
Browse files Browse the repository at this point in the history
  • Loading branch information
romancardenas committed Oct 11, 2024
1 parent 3ca6b50 commit 47a4a26
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 13 deletions.
1 change: 1 addition & 0 deletions hifive1/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]
- Replace static muts with Mutexes
- Apply clippy changes
- Bump MSRV to 1.72
- Adapt to new Cargo workspace
Expand Down
1 change: 1 addition & 0 deletions hifive1/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ edition = "2021"
rust-version = "1.72"

[dependencies]
critical-section = { version = "1.1.3" }
e310x-hal = { path = "../e310x-hal", version = "0.11.0" }
embedded-hal = "0.2.7"
riscv = "0.10.1"
Expand Down
25 changes: 12 additions & 13 deletions hifive1/src/stdout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
//! Stdout based on the UART hooked up to FTDI or J-Link
use core::fmt;
use core::{cell::RefCell, fmt};
use critical_section::Mutex;
use e310x_hal::{
clock::Clocks,
e310x::Uart0,
Expand All @@ -10,12 +11,11 @@ use e310x_hal::{
time::Bps,
};
use nb::block;
use riscv::interrupt;

static mut STDOUT: Option<SerialWrapper> = None;

struct SerialWrapper(Tx<Uart0>);

static STDOUT: Mutex<RefCell<Option<SerialWrapper>>> = Mutex::new(RefCell::new(None));

impl core::fmt::Write for SerialWrapper {
fn write_str(&mut self, s: &str) -> fmt::Result {
for byte in s.as_bytes() {
Expand Down Expand Up @@ -50,28 +50,27 @@ pub fn configure<X, Y>(
let serial = Serial::new(uart, (tx, rx), baud_rate, clocks);
let (tx, rx) = serial.split();

interrupt::free(|| unsafe {
STDOUT.replace(SerialWrapper(tx));
});
critical_section::with(|cs| STDOUT.borrow(cs).replace(Some(SerialWrapper(tx))));

rx
}

/// Writes string to stdout
pub fn write_str(s: &str) {
interrupt::free(|| unsafe {
if let Some(stdout) = STDOUT.as_mut() {
critical_section::with(|cs| {
if let Some(stdout) = STDOUT.borrow(cs).borrow_mut().as_mut() {
let _ = stdout.write_str(s);
}
})
});
}

/// Writes formatted string to stdout
pub fn write_fmt(args: fmt::Arguments) {
interrupt::free(|| unsafe {
if let Some(stdout) = STDOUT.as_mut() {
critical_section::with(|cs| {
if let Some(stdout) = STDOUT.borrow(cs).borrow_mut().as_mut() {
let _ = stdout.write_fmt(args);
}
})
});
}

/// Macro for printing to the serial standard output
Expand Down

0 comments on commit 47a4a26

Please sign in to comment.