Skip to content

Commit

Permalink
replace static muts with pointers
Browse files Browse the repository at this point in the history
  • Loading branch information
romancardenas committed Oct 12, 2024
1 parent 3ca6b50 commit 36e3a8a
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 12 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
23 changes: 11 additions & 12 deletions hifive1/src/stdout.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! Stdout based on the UART hooked up to FTDI or J-Link
use core::fmt;
use core::{fmt, ptr};
use e310x_hal::{
clock::Clocks,
e310x::Uart0,
Expand All @@ -10,12 +10,11 @@ use e310x_hal::{
time::Bps,
};
use nb::block;
use riscv::interrupt;

static mut STDOUT: Option<SerialWrapper> = None;

struct SerialWrapper(Tx<Uart0>);

static mut STDOUT: Option<SerialWrapper> = 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 +49,28 @@ 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(|_| {
unsafe { &mut *ptr::addr_of_mut!(STDOUT) }.replace(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(|_| {
if let Some(stdout) = unsafe { &mut *ptr::addr_of_mut!(STDOUT) } {
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(|_| {
if let Some(stdout) = unsafe { &mut *ptr::addr_of_mut!(STDOUT) } {
let _ = stdout.write_fmt(args);
}
})
});
}

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

0 comments on commit 36e3a8a

Please sign in to comment.