1
1
//! Stdout based on the UART hooked up to FTDI or J-Link
2
2
3
- use core:: fmt;
3
+ use core:: { fmt, ptr } ;
4
4
use e310x_hal:: {
5
5
clock:: Clocks ,
6
6
e310x:: Uart0 ,
@@ -10,12 +10,11 @@ use e310x_hal::{
10
10
time:: Bps ,
11
11
} ;
12
12
use nb:: block;
13
- use riscv:: interrupt;
14
-
15
- static mut STDOUT : Option < SerialWrapper > = None ;
16
13
17
14
struct SerialWrapper ( Tx < Uart0 > ) ;
18
15
16
+ static mut STDOUT : Option < SerialWrapper > = None ;
17
+
19
18
impl core:: fmt:: Write for SerialWrapper {
20
19
fn write_str ( & mut self , s : & str ) -> fmt:: Result {
21
20
for byte in s. as_bytes ( ) {
@@ -50,28 +49,28 @@ pub fn configure<X, Y>(
50
49
let serial = Serial :: new ( uart, ( tx, rx) , baud_rate, clocks) ;
51
50
let ( tx, rx) = serial. split ( ) ;
52
51
53
- interrupt :: free ( || unsafe {
54
- STDOUT . replace ( SerialWrapper ( tx) ) ;
52
+ critical_section :: with ( |_| {
53
+ unsafe { & mut * ptr :: addr_of_mut! ( STDOUT ) } . replace ( SerialWrapper ( tx) ) ;
55
54
} ) ;
56
55
rx
57
56
}
58
57
59
58
/// Writes string to stdout
60
59
pub 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 ) } {
63
62
let _ = stdout. write_str ( s) ;
64
63
}
65
- } )
64
+ } ) ;
66
65
}
67
66
68
67
/// Writes formatted string to stdout
69
68
pub 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 ) } {
72
71
let _ = stdout. write_fmt ( args) ;
73
72
}
74
- } )
73
+ } ) ;
75
74
}
76
75
77
76
/// Macro for printing to the serial standard output
0 commit comments