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 ;
45use e310x_hal:: {
56 clock:: Clocks ,
67 e310x:: Uart0 ,
@@ -10,12 +11,11 @@ use e310x_hal::{
1011 time:: Bps ,
1112} ;
1213use nb:: block;
13- use riscv:: interrupt;
14-
15- static mut STDOUT : Option < SerialWrapper > = None ;
1614
1715struct SerialWrapper ( Tx < Uart0 > ) ;
1816
17+ static STDOUT : Mutex < RefCell < Option < SerialWrapper > > > = Mutex :: new ( RefCell :: new ( None ) ) ;
18+
1919impl 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
6059pub 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
6968pub 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