|
| 1 | +use allwinner_hal::{ |
| 2 | + gpio::Function, |
| 3 | + uart::{ReceiveHalf, TransmitHalf}, |
| 4 | +}; |
| 5 | +use allwinner_rt::soc::d1::UART0; |
| 6 | +use embedded_io::Write; |
| 7 | +use spin::Mutex; |
| 8 | + |
| 9 | +#[doc(hidden)] |
| 10 | +pub static STDOUT: Mutex<Option<SyterKitStdoutInner>> = Mutex::new(None); |
| 11 | +#[doc(hidden)] |
| 12 | +pub static STDIN: Mutex<Option<SyterKitStdinInner>> = Mutex::new(None); |
| 13 | + |
| 14 | +#[doc(hidden)] |
| 15 | +pub struct SyterKitStdoutInner { |
| 16 | + pub inner: TransmitHalf<UART0, 0, Function<'static, 'B', 8, 6>>, |
| 17 | +} |
| 18 | + |
| 19 | +unsafe impl Send for SyterKitStdoutInner {} |
| 20 | + |
| 21 | +#[doc(hidden)] |
| 22 | +pub struct SyterKitStdinInner { |
| 23 | + pub inner: ReceiveHalf<UART0, 0, Function<'static, 'B', 9, 6>>, |
| 24 | +} |
| 25 | + |
| 26 | +unsafe impl Send for SyterKitStdinInner {} |
| 27 | + |
| 28 | +// macro internal, used in `print` and `println` macros in `macros.rs`. |
| 29 | +#[doc(hidden)] |
| 30 | +#[inline] |
| 31 | +pub fn _print(args: core::fmt::Arguments) { |
| 32 | + if let Some(serial) = &mut *STDOUT.lock() { |
| 33 | + serial.inner.write_fmt(args).ok(); |
| 34 | + } |
| 35 | +} |
| 36 | + |
| 37 | +/// A handle to the global standard output stream of the current runtime. |
| 38 | +pub struct Stdout { |
| 39 | + inner: spin::MutexGuard<'static, Option<SyterKitStdoutInner>>, |
| 40 | +} |
| 41 | + |
| 42 | +impl embedded_io::ErrorType for Stdout { |
| 43 | + type Error = core::convert::Infallible; |
| 44 | +} |
| 45 | + |
| 46 | +impl embedded_io::Write for Stdout { |
| 47 | + #[inline] |
| 48 | + fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { |
| 49 | + if let Some(stdout) = &mut *self.inner { |
| 50 | + stdout.inner.write_all(buf).ok(); |
| 51 | + } |
| 52 | + Ok(buf.len()) |
| 53 | + } |
| 54 | + #[inline] |
| 55 | + fn flush(&mut self) -> Result<(), Self::Error> { |
| 56 | + if let Some(stdout) = &mut *self.inner { |
| 57 | + stdout.inner.flush().ok(); |
| 58 | + } |
| 59 | + Ok(()) |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +/// A handle to the standard input stream of a runtime. |
| 64 | +pub struct Stdin { |
| 65 | + inner: spin::MutexGuard<'static, Option<SyterKitStdinInner>>, |
| 66 | +} |
| 67 | + |
| 68 | +impl embedded_io::ErrorType for Stdin { |
| 69 | + type Error = core::convert::Infallible; |
| 70 | +} |
| 71 | + |
| 72 | +impl embedded_io::Read for Stdin { |
| 73 | + #[inline] |
| 74 | + fn read(&mut self, buf: &mut [u8]) -> Result<usize, Self::Error> { |
| 75 | + if let Some(stdin) = &mut *self.inner { |
| 76 | + stdin.inner.read(buf).ok(); |
| 77 | + } |
| 78 | + Ok(buf.len()) |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +/// Constructs a new handle to the standard output of the current environment. |
| 83 | +#[inline] |
| 84 | +pub fn stdout() -> Stdout { |
| 85 | + Stdout { |
| 86 | + inner: STDOUT.lock(), |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +/// Constructs a new handle to the standard input of the current environment. |
| 91 | +#[inline] |
| 92 | +pub fn stdin() -> Stdin { |
| 93 | + Stdin { |
| 94 | + inner: STDIN.lock(), |
| 95 | + } |
| 96 | +} |
0 commit comments