Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 1 | // Copyright 2022, The Android Open Source Project |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | //! Console driver for 8250 UART. |
| 16 | |
Michał Mazurek | b366b73 | 2024-11-29 15:12:11 +0100 | [diff] [blame^] | 17 | use crate::arch::platform; |
Andrew Walbran | f86d4b0 | 2024-07-19 17:23:18 +0100 | [diff] [blame] | 18 | use core::fmt::{write, Arguments, Write}; |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 19 | |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 20 | /// Writes a formatted string followed by a newline to the n-th console. |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 21 | /// |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 22 | /// Panics if the n-th console was not initialized by calling [`init`] first. |
Pierre-Clément Tosi | b5a3ab1 | 2023-09-15 11:18:38 +0100 | [diff] [blame] | 23 | pub fn writeln(n: usize, format_args: Arguments) { |
Michał Mazurek | b366b73 | 2024-11-29 15:12:11 +0100 | [diff] [blame^] | 24 | let uart = &mut *platform::uart(n).lock(); |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 25 | write(uart, format_args).unwrap(); |
| 26 | let _ = uart.write_str("\n"); |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 27 | } |
| 28 | |
Michał Mazurek | b366b73 | 2024-11-29 15:12:11 +0100 | [diff] [blame^] | 29 | /// Reinitializes the emergency UART driver and writes a formatted string followed by a newline to |
| 30 | /// it. |
Andrew Walbran | f259488 | 2022-03-15 17:32:53 +0000 | [diff] [blame] | 31 | /// |
| 32 | /// This is intended for use in situations where the UART may be in an unknown state or the global |
| 33 | /// instance may be locked, such as in an exception handler or panic handler. |
Michał Mazurek | b366b73 | 2024-11-29 15:12:11 +0100 | [diff] [blame^] | 34 | pub fn ewriteln(format_args: Arguments) { |
| 35 | let mut uart = platform::emergency_uart(); |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 36 | let _ = write(&mut uart, format_args); |
Pierre-Clément Tosi | bc9fb7e | 2024-06-19 17:28:47 +0100 | [diff] [blame] | 37 | let _ = uart.write_str("\n"); |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 38 | } |
| 39 | |
Pierre-Clément Tosi | b5a3ab1 | 2023-09-15 11:18:38 +0100 | [diff] [blame] | 40 | /// Prints the given formatted string to the n-th console, followed by a newline. |
| 41 | /// |
| 42 | /// Panics if the console has not yet been initialized. May hang if used in an exception context; |
| 43 | /// use `eprintln!` instead. |
| 44 | #[macro_export] |
| 45 | macro_rules! console_writeln { |
| 46 | ($n:expr, $($arg:tt)*) => ({ |
| 47 | $crate::console::writeln($n, format_args!($($arg)*)) |
| 48 | }) |
| 49 | } |
| 50 | |
| 51 | pub(crate) use console_writeln; |
| 52 | |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 53 | /// Prints the given formatted string to the console, followed by a newline. |
| 54 | /// |
Alice Wang | eff5839 | 2023-07-04 13:32:09 +0000 | [diff] [blame] | 55 | /// Panics if the console has not yet been initialized. May hang if used in an exception context; |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 56 | /// use `eprintln!` instead. |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 57 | macro_rules! println { |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 58 | ($($arg:tt)*) => ({ |
Michał Mazurek | b366b73 | 2024-11-29 15:12:11 +0100 | [diff] [blame^] | 59 | $crate::console::console_writeln!($crate::arch::platform::DEFAULT_CONSOLE_INDEX, $($arg)*) |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 60 | }) |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 61 | } |
| 62 | |
Jakob Vukalovic | ef99629 | 2023-04-13 14:28:34 +0000 | [diff] [blame] | 63 | pub(crate) use println; // Make it available in this crate. |
| 64 | |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 65 | /// Prints the given string followed by a newline to the console in an emergency, such as an |
| 66 | /// exception handler. |
| 67 | /// |
| 68 | /// Never panics. |
| 69 | #[macro_export] |
| 70 | macro_rules! eprintln { |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 71 | ($($arg:tt)*) => ({ |
Michał Mazurek | b366b73 | 2024-11-29 15:12:11 +0100 | [diff] [blame^] | 72 | $crate::console::ewriteln(format_args!($($arg)*)) |
Pierre-Clément Tosi | 075aade | 2024-06-18 20:08:13 +0100 | [diff] [blame] | 73 | }) |
Andrew Walbran | c1bcb3c | 2022-03-31 13:13:57 +0000 | [diff] [blame] | 74 | } |