diff --git a/apps/system/components/DebugConsole/DebugConsole.camkes b/apps/system/components/DebugConsole/DebugConsole.camkes index 2ff735e..fb569a2 100644 --- a/apps/system/components/DebugConsole/DebugConsole.camkes +++ b/apps/system/components/DebugConsole/DebugConsole.camkes @@ -9,9 +9,11 @@ component DebugConsole { dataport Buf tx_dataport; uses dataport_io_inf uart_tx; + has mutex tx_mutex; dataport Buf rx_dataport; uses dataport_io_inf uart_rx; + has mutex rx_mutex; provides LoggerInterface logger; uses ProcessControlInterface proc_ctrl; diff --git a/apps/system/components/DebugConsole/kata-uart-client/src/lib.rs b/apps/system/components/DebugConsole/kata-uart-client/src/lib.rs index 2185a76..9f0c152 100644 --- a/apps/system/components/DebugConsole/kata-uart-client/src/lib.rs +++ b/apps/system/components/DebugConsole/kata-uart-client/src/lib.rs @@ -7,15 +7,19 @@ use kata_io as io; // C interface to external UART driver. extern "C" { static rx_dataport: *mut cty::c_uchar; - static tx_dataport: *mut cty::c_uchar; fn uart_rx_update(n: cty::size_t); + fn rx_mutex_lock(); + fn rx_mutex_unlock(); + + static tx_dataport: *mut cty::c_uchar; fn uart_tx_update(n: cty::size_t); + fn tx_mutex_lock(); + fn tx_mutex_unlock(); } // Console logging interface. #[no_mangle] pub extern "C" fn logger_log(msg: *const cstr_core::c_char) { - // TODO(sleffler): is the uart driver ok w/ multiple writers? let output: &mut dyn io::Write = &mut self::Tx {}; unsafe { let _ = writeln!(output, "{}", CStr::from_ptr(msg).to_str().unwrap()); @@ -27,9 +31,11 @@ pub struct Rx {} impl io::Read for Rx { fn read(&mut self, buf: &mut [u8]) -> Result { unsafe { + rx_mutex_lock(); uart_rx_update(buf.len()); let port = core::slice::from_raw_parts(rx_dataport, buf.len()); buf.copy_from_slice(&port); + rx_mutex_unlock(); } Ok(buf.len()) } @@ -40,9 +46,11 @@ pub struct Tx {} impl io::Write for Tx { fn write(&mut self, buf: &[u8]) -> Result { unsafe { + tx_mutex_lock(); let port = core::slice::from_raw_parts_mut(tx_dataport, buf.len()); port.copy_from_slice(buf); uart_tx_update(buf.len()); + tx_mutex_unlock(); } Ok(buf.len()) }