11//! Stdout based on the UART hooked up to FTDI or J-Link
22
3- use core:: fmt;
3+ use core:: { fmt, ptr } ;
44use e310x_hal:: {
55 clock:: Clocks ,
66 e310x:: Uart0 ,
@@ -10,12 +10,11 @@ use e310x_hal::{
1010 time:: Bps ,
1111} ;
1212use nb:: block;
13- use riscv:: interrupt;
14-
15- static mut STDOUT : Option < SerialWrapper > = None ;
1613
1714struct SerialWrapper ( Tx < Uart0 > ) ;
1815
16+ static mut STDOUT : Option < SerialWrapper > = None ;
17+
1918impl 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
6059pub 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
6968pub 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