Skip to content

Commit 02cafef

Browse files
Fixup the serial console
1 parent 7ee51ff commit 02cafef

File tree

2 files changed

+24
-22
lines changed

2 files changed

+24
-22
lines changed

src/lib.rs

Lines changed: 21 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ static API: Api = Api::new();
4242
/// We store our VGA console here.
4343
static VGA_CONSOLE: CsRefCell<Option<vgaconsole::VgaConsole>> = CsRefCell::new(None);
4444

45-
/// We store our VGA console here.
45+
/// We store our serial console here.
4646
static SERIAL_CONSOLE: CsRefCell<Option<SerialConsole>> = CsRefCell::new(None);
4747

4848
/// Note if we are panicking right now.
@@ -146,20 +146,16 @@ impl StdInput {
146146
}
147147
}
148148

149-
// if let Some((uart_dev, _serial_conf)) = menu.context.config.get_serial_console() {
150-
// while !self.buffer.is_full() {
151-
// let mut buffer = [0u8];
152-
// let wrapper = neotron_common_bios::FfiBuffer::new(&mut buffer);
153-
// match (api.serial_read)(uart_dev, wrapper, neotron_common_bios::FfiOption::None) {
154-
// neotron_common_bios::ApiResult::Ok(n) if n >= 0 => {
155-
// self.buffer.enqueue(buffer[0]).unwrap();
156-
// }
157-
// _ => {
158-
// break;
159-
// }
160-
// }
161-
// }
162-
// }
149+
if let Some(console) = SERIAL_CONSOLE.lock().as_mut() {
150+
while !self.buffer.is_full() {
151+
let mut buffer = [0u8];
152+
if let Ok(1) = console.read_data(&mut buffer) {
153+
self.buffer.enqueue(buffer[0]).unwrap();
154+
} else {
155+
break;
156+
}
157+
}
158+
}
163159

164160
self.get_buffered_data(buffer)
165161
}
@@ -259,7 +255,8 @@ impl Api {
259255
struct SerialConsole(u8);
260256

261257
impl SerialConsole {
262-
fn write_bstr(&mut self, data: &[u8]) -> core::fmt::Result {
258+
/// Write some bytes to the serial console
259+
fn write_bstr(&mut self, data: &[u8]) {
263260
let api = API.get();
264261
let is_panic = IS_PANIC.load(Ordering::Relaxed);
265262
let res = (api.serial_write)(
@@ -273,7 +270,14 @@ impl SerialConsole {
273270
if !is_panic {
274271
res.unwrap();
275272
}
276-
Ok(())
273+
}
274+
275+
/// Try and get as many bytes as we can from the serial console.
276+
fn read_data(&mut self, buffer: &mut [u8]) -> Result<usize, bios::Error> {
277+
let api = API.get();
278+
let ffi_buffer = bios::FfiBuffer::new(buffer);
279+
let res = (api.serial_read)(self.0, ffi_buffer, bios::FfiOption::Some(bios::Timeout::new_ms(0)));
280+
res.into()
277281
}
278282
}
279283

src/program.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -319,15 +319,13 @@ extern "C" fn api_write(
319319
buffer: neotron_api::FfiByteSlice,
320320
) -> neotron_api::Result<()> {
321321
if fd == neotron_api::file::Handle::new_stdout() {
322-
let mut guard = crate::VGA_CONSOLE.try_lock().unwrap();
322+
let mut guard = crate::VGA_CONSOLE.lock();
323323
if let Some(console) = guard.as_mut() {
324324
console.write_bstr(buffer.as_slice());
325325
}
326-
let mut guard = crate::SERIAL_CONSOLE.try_lock().unwrap();
326+
let mut guard = crate::SERIAL_CONSOLE.lock();
327327
if let Some(console) = guard.as_mut() {
328-
if let Err(_e) = console.write_bstr(buffer.as_slice()) {
329-
return neotron_api::Result::Err(neotron_api::Error::DeviceSpecific);
330-
}
328+
console.write_bstr(buffer.as_slice());
331329
}
332330
neotron_api::Result::Ok(())
333331
} else {

0 commit comments

Comments
 (0)