| // Copyright 2022 Google LLC |
| // |
| // Licensed under the Apache License, Version 2.0 (the "License"); |
| // you may not use this file except in compliance with the License. |
| // You may obtain a copy of the License at |
| // |
| // https://www.apache.org/licenses/LICENSE-2.0 |
| // |
| // Unless required by applicable law or agreed to in writing, software |
| // distributed under the License is distributed on an "AS IS" BASIS, |
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| // See the License for the specific language governing permissions and |
| // limitations under the License. |
| |
| //! App-side dprintf macro that calls through to the debug_uart capsule. |
| |
| use core::fmt; |
| use core2::io::{Cursor, Write}; |
| use libtock::syscalls; |
| use matcha_config::*; |
| |
| #[macro_export] |
| macro_rules! dprintf { |
| ($msg:expr) => ({ |
| $crate::dprintf::vdprintf(format_args!($msg)) |
| }); |
| ($fmt:expr, $($arg:tt)+) => ({ |
| $crate::dprintf::vdprintf(format_args!($fmt, $($arg)+)) |
| }); |
| } |
| |
| pub fn vdprintf(args: fmt::Arguments) { |
| let mut uart_buf = [0u8; 256]; |
| let mut cur = Cursor::new(&mut uart_buf[..]); |
| if cur.write_fmt(args).is_ok() { |
| let pos = cur.position(); |
| drop(cur); |
| let allow = syscalls::allow(CAPSULE_DPRINTF, 0, &mut uart_buf); |
| let _result = syscalls::command(CAPSULE_DPRINTF, CMD_DPRINTF_PRINT, pos as usize, 0); |
| drop(allow); |
| } |
| } |