4
0

legacy_stdio.rs 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. //! 这个模块的两个宏应该公开
  2. //! 如果制造实例的时候,给定了stdout,那么就会打印到这个stdout里面
  3. use crate::util::AmoMutex;
  4. use alloc::boxed::Box;
  5. use embedded_hal::serial::{Read, Write};
  6. use nb::block;
  7. /// Legacy standard input/output
  8. pub trait LegacyStdio: Send {
  9. /// Get a character from legacy stdin
  10. fn getchar(&mut self) -> u8;
  11. /// Put a character into legacy stdout
  12. fn putchar(&mut self, ch: u8);
  13. }
  14. /// Use serial in `embedded-hal` as legacy standard input/output
  15. struct EmbeddedHalSerial<T> {
  16. inner: T,
  17. }
  18. impl<T> EmbeddedHalSerial<T> {
  19. /// Create a wrapper with a value
  20. fn new(inner: T) -> Self {
  21. Self { inner }
  22. }
  23. }
  24. impl<T: Send> LegacyStdio for EmbeddedHalSerial<T>
  25. where
  26. T: Read<u8> + Write<u8>,
  27. {
  28. fn getchar(&mut self) -> u8 {
  29. // 直接调用embedded-hal里面的函数
  30. // 关于unwrap:因为这个是legacy函数,这里没有详细的处理流程,就panic掉
  31. block!(self.inner.read()).ok().unwrap()
  32. }
  33. fn putchar(&mut self, ch: u8) {
  34. // 直接调用函数写一个字节
  35. block!(self.inner.write(ch)).ok();
  36. // 写一次flush一次,因为是legacy,就不考虑效率了
  37. block!(self.inner.flush()).ok();
  38. }
  39. }
  40. struct Fused<T, R>(T, R);
  41. // 和上面的原理差不多,就是分开了
  42. impl<T, R> LegacyStdio for Fused<T, R>
  43. where
  44. T: Write<u8> + Send + 'static,
  45. R: Read<u8> + Send + 'static,
  46. {
  47. fn getchar(&mut self) -> u8 {
  48. block!(self.1.read()).ok().unwrap()
  49. }
  50. fn putchar(&mut self, ch: u8) {
  51. block!(self.0.write(ch)).ok();
  52. block!(self.0.flush()).ok();
  53. }
  54. }
  55. static LEGACY_STDIO: AmoMutex<Option<Box<dyn LegacyStdio>>> = AmoMutex::new(None);
  56. #[doc(hidden)] // use through a macro
  57. pub fn init_legacy_stdio_embedded_hal<T: Read<u8> + Write<u8> + Send + 'static>(serial: T) {
  58. let serial = EmbeddedHalSerial::new(serial);
  59. *LEGACY_STDIO.lock() = Some(Box::new(serial));
  60. }
  61. #[doc(hidden)] // use through a macro
  62. pub fn init_legacy_stdio_embedded_hal_fuse<T, R>(tx: T, rx: R)
  63. where
  64. T: Write<u8> + Send + 'static,
  65. R: Read<u8> + Send + 'static,
  66. {
  67. let serial = Fused(tx, rx);
  68. *LEGACY_STDIO.lock() = Some(Box::new(serial));
  69. }
  70. pub fn legacy_stdio_putchar(ch: u8) {
  71. if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
  72. stdio.putchar(ch)
  73. }
  74. }
  75. pub fn legacy_stdio_getchar() -> usize {
  76. if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
  77. stdio.getchar() as usize
  78. } else {
  79. // According to RISC-V SBI spec 0.3.1-rc1, Section 4.3, this function returns -1
  80. // when fails to read from debug console. Thank you @duskmoon314
  81. usize::from_ne_bytes(isize::to_ne_bytes(-1))
  82. }
  83. }
  84. use core::fmt;
  85. struct Stdout;
  86. impl fmt::Write for Stdout {
  87. fn write_str(&mut self, s: &str) -> fmt::Result {
  88. if let Some(stdio) = LEGACY_STDIO.lock().as_mut() {
  89. for byte in s.as_bytes() {
  90. stdio.putchar(*byte)
  91. }
  92. }
  93. Ok(())
  94. }
  95. }
  96. #[doc(hidden)]
  97. pub fn _print(args: fmt::Arguments) {
  98. use fmt::Write;
  99. Stdout.write_fmt(args).unwrap();
  100. }
  101. /// Prints to the legacy debug console.
  102. ///
  103. /// This is only supported when there exists legacy extension;
  104. /// otherwise platform caller should use an early kernel input/output device
  105. /// declared in platform specific hardware.
  106. #[macro_export(local_inner_macros)]
  107. macro_rules! print {
  108. ($($arg:tt)*) => ({
  109. $crate::legacy_stdio::_print(core::format_args!($($arg)*));
  110. });
  111. }
  112. /// Prints to the legacy debug console, with a newline.
  113. ///
  114. /// This is only supported when there exists legacy extension;
  115. /// otherwise platform caller should use an early kernel input/output device
  116. /// declared in platform specific hardware.
  117. #[macro_export(local_inner_macros)]
  118. macro_rules! println {
  119. ($fmt: literal $(, $($arg: tt)+)?) => {
  120. $crate::legacy_stdio::_print(core::format_args!(core::concat!($fmt, "\r\n") $(, $($arg)+)?));
  121. }
  122. }