Logging cleanup and fix for RTT
This commit is contained in:
parent
f8ef06ee0c
commit
c2e3f1bec3
28
firmware/.vscode/launch.json
vendored
28
firmware/.vscode/launch.json
vendored
|
|
@ -9,9 +9,9 @@
|
|||
"chip": "esp32s3",
|
||||
"coreConfigs": [
|
||||
{
|
||||
"programBinary": "target/xtensa-esp32s3-none-elf/debug/acid-firmware"
|
||||
}
|
||||
]
|
||||
"programBinary": "target/xtensa-esp32s3-none-elf/debug/acid-firmware",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"name": "probe-rs release restart",
|
||||
|
|
@ -21,9 +21,9 @@
|
|||
"chip": "esp32s3",
|
||||
"coreConfigs": [
|
||||
{
|
||||
"programBinary": "target/xtensa-esp32s3-none-elf/release/acid-firmware"
|
||||
}
|
||||
]
|
||||
"programBinary": "target/xtensa-esp32s3-none-elf/release/acid-firmware",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"preLaunchTask": "rust: cargo build",
|
||||
|
|
@ -37,9 +37,9 @@
|
|||
"chip": "esp32s3",
|
||||
"coreConfigs": [
|
||||
{
|
||||
"programBinary": "target/xtensa-esp32s3-none-elf/debug/acid-firmware"
|
||||
}
|
||||
]
|
||||
"programBinary": "target/xtensa-esp32s3-none-elf/debug/acid-firmware",
|
||||
},
|
||||
],
|
||||
},
|
||||
{
|
||||
"preLaunchTask": "rust: cargo build --release",
|
||||
|
|
@ -53,9 +53,9 @@
|
|||
"chip": "esp32s3",
|
||||
"coreConfigs": [
|
||||
{
|
||||
"programBinary": "target/xtensa-esp32s3-none-elf/release/acid-firmware"
|
||||
}
|
||||
]
|
||||
}
|
||||
]
|
||||
"programBinary": "target/xtensa-esp32s3-none-elf/release/acid-firmware",
|
||||
},
|
||||
],
|
||||
},
|
||||
],
|
||||
}
|
||||
|
|
|
|||
4
firmware/.vscode/settings.json
vendored
Normal file
4
firmware/.vscode/settings.json
vendored
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
{
|
||||
"rust-analyzer.cargo.noDefaultFeatures": true,
|
||||
"rust-analyzer.cargo.features": ["probe"],
|
||||
}
|
||||
10
firmware/.vscode/tasks.json
vendored
10
firmware/.vscode/tasks.json
vendored
|
|
@ -4,9 +4,12 @@
|
|||
{
|
||||
"label": "rust: cargo build",
|
||||
"type": "cargo",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/acid-firmware"
|
||||
},
|
||||
"command": "build",
|
||||
"args": [
|
||||
"--no-default-features", "--features=probe,info"
|
||||
"--no-default-features", "--features=probe"
|
||||
],
|
||||
"problemMatcher": [
|
||||
"$rustc"
|
||||
|
|
@ -19,9 +22,12 @@
|
|||
{
|
||||
"label": "rust: cargo build --release",
|
||||
"type": "cargo",
|
||||
"options": {
|
||||
"cwd": "${workspaceFolder}/acid-firmware"
|
||||
},
|
||||
"command": "build",
|
||||
"args": [
|
||||
"--release", "--no-default-features", "--features=probe,info"
|
||||
"--release", "--no-default-features", "--features=probe"
|
||||
],
|
||||
"problemMatcher": [
|
||||
"$rustc"
|
||||
|
|
|
|||
2
firmware/Cargo.lock
generated
2
firmware/Cargo.lock
generated
|
|
@ -22,7 +22,7 @@ dependencies = [
|
|||
"embassy-sync 0.7.2",
|
||||
"embassy-time",
|
||||
"embedded-cli",
|
||||
"embedded-io 0.7.1",
|
||||
"embedded-io 0.6.1",
|
||||
"embedded-storage-async",
|
||||
"embuild",
|
||||
"enumset",
|
||||
|
|
|
|||
|
|
@ -1,6 +1,6 @@
|
|||
[target.'cfg(all(any(target_arch = "riscv32", target_arch = "xtensa"), target_os = "none"))']
|
||||
runner = "espflash flash --monitor"
|
||||
# runner = "probe-rs run --chip esp32s3 --preverify"
|
||||
# runner = "espflash flash --monitor"
|
||||
runner = "probe-rs run --chip esp32s3 --preverify"
|
||||
|
||||
[build]
|
||||
target = "xtensa-esp32s3-none-elf"
|
||||
|
|
|
|||
|
|
@ -95,7 +95,7 @@ i-slint-core = { version = "1.14.1", git = "https://github.com/Limeth/slint", re
|
|||
|
||||
# Crates for serial UART CLI
|
||||
embedded-cli = { version = "0.2.1", default-features = false, features = ["help", "macros"] }
|
||||
embedded-io = "0.7"
|
||||
embedded-io = "0.6"
|
||||
mutually_exclusive_features = "0.1.0"
|
||||
|
||||
[dependencies.ekv]
|
||||
|
|
|
|||
|
|
@ -1,12 +1,14 @@
|
|||
|
||||
use core::fmt::Write;
|
||||
|
||||
use embedded_cli::cli::CliBuilder;
|
||||
use embedded_cli::Command;
|
||||
use esp_hal::{Async, uart::{TxError, UartRx}};
|
||||
use log::{info, error};
|
||||
use embedded_cli::cli::CliBuilder;
|
||||
use esp_hal::{
|
||||
Async,
|
||||
uart::{TxError, UartRx},
|
||||
};
|
||||
use log::{error, info};
|
||||
|
||||
use crate::logging::with_uart_tx;
|
||||
use crate::logging::uart::with_uart_tx;
|
||||
|
||||
struct Writer;
|
||||
|
||||
|
|
@ -16,15 +18,11 @@ impl embedded_io::ErrorType for Writer {
|
|||
|
||||
impl embedded_io::Write for Writer {
|
||||
fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> {
|
||||
with_uart_tx(|_, uart| {
|
||||
uart.write(buf)
|
||||
})
|
||||
with_uart_tx(|_, uart| uart.write(buf))
|
||||
}
|
||||
|
||||
fn flush(&mut self) -> Result<(), Self::Error> {
|
||||
with_uart_tx(|_, uart| {
|
||||
uart.flush()
|
||||
})
|
||||
with_uart_tx(|_, uart| uart.flush())
|
||||
}
|
||||
}
|
||||
|
||||
|
|
@ -35,7 +33,6 @@ enum Base/*<'a>*/ {
|
|||
// /// To whom to say hello (World by default)
|
||||
// name: Option<&'a str>,
|
||||
// },
|
||||
|
||||
/// Display the version of the firmware.
|
||||
Version,
|
||||
|
||||
|
|
@ -73,16 +70,26 @@ pub async fn run_console(mut uart_rx: UartRx<'_, Async>) {
|
|||
// write!(cli.writer(), "Hello, {}", name.unwrap_or("World"))?;
|
||||
// }
|
||||
Base::Version => {
|
||||
cli.writer().write_fmt(format_args!("{} - {} - {}", env!("CARGO_PKG_NAME"), env!("CARGO_PKG_VERSION"), env!("GIT_COMMIT"))).unwrap();
|
||||
cli.writer()
|
||||
.write_fmt(format_args!(
|
||||
"{} - {} - {}",
|
||||
env!("CARGO_PKG_NAME"),
|
||||
env!("CARGO_PKG_VERSION"),
|
||||
env!("GIT_COMMIT")
|
||||
))
|
||||
.unwrap();
|
||||
}
|
||||
Base::Reset => {
|
||||
cli.writer().write_str("Performing software reset.").unwrap();
|
||||
cli.writer()
|
||||
.write_str("Performing software reset.")
|
||||
.unwrap();
|
||||
esp_hal::system::software_reset();
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}),
|
||||
).unwrap();
|
||||
)
|
||||
.unwrap();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -1,8 +1,10 @@
|
|||
use core::cell::RefCell;
|
||||
use core::fmt::Write;
|
||||
use core::{cell::RefCell, fmt::Arguments};
|
||||
|
||||
use critical_section::{CriticalSection, Mutex};
|
||||
use esp_hal::Blocking;
|
||||
use esp_hal::clock::CpuClock;
|
||||
use esp_hal::psram::{FlashFreq, PsramConfig, PsramSize, SpiRamFreq, SpiTimingConfigCoreClock};
|
||||
use esp_hal::uart::UartTx;
|
||||
use log::{LevelFilter, Log};
|
||||
|
||||
|
|
@ -19,13 +21,70 @@ pub const LOG_LEVEL_FILTER: LevelFilter = {
|
|||
} else if string.eq_ignore_ascii_case("TRACE") {
|
||||
LevelFilter::Trace
|
||||
} else {
|
||||
panic!("Unknown `ESP_LOG` value. Only `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`, or `OFF` may be used.");
|
||||
panic!(
|
||||
"Unknown `ESP_LOG` value. Only `ERROR`, `WARN`, `INFO`, `DEBUG`, `TRACE`, or `OFF` may be used."
|
||||
);
|
||||
}
|
||||
} else {
|
||||
LevelFilter::Off
|
||||
}
|
||||
};
|
||||
|
||||
const RESET: &str = "\u{001B}[0m";
|
||||
const RED: &str = "\u{001B}[31m";
|
||||
const GREEN: &str = "\u{001B}[32m";
|
||||
const YELLOW: &str = "\u{001B}[33m";
|
||||
const BLUE: &str = "\u{001B}[34m";
|
||||
const CYAN: &str = "\u{001B}[35m";
|
||||
|
||||
fn with_formatted_log_record<R>(
|
||||
record: &log::Record,
|
||||
callback: impl FnOnce(Arguments<'_>) -> R,
|
||||
) -> R {
|
||||
let color = match record.level() {
|
||||
log::Level::Error => RED,
|
||||
log::Level::Warn => YELLOW,
|
||||
log::Level::Info => GREEN,
|
||||
log::Level::Debug => BLUE,
|
||||
log::Level::Trace => CYAN,
|
||||
};
|
||||
let args = format_args!(
|
||||
"{}{:>5} - {}{}\n",
|
||||
color,
|
||||
record.level(),
|
||||
record.args(),
|
||||
RESET
|
||||
);
|
||||
(callback)(args)
|
||||
}
|
||||
|
||||
/// The default USB logger.
|
||||
#[cfg(feature = "usb-log")]
|
||||
pub mod usb {
|
||||
use super::*;
|
||||
|
||||
pub fn setup_logging() -> impl Future<Output = ()> {
|
||||
esp_println::logger::init_logger(LOG_LEVEL_FILTER);
|
||||
log::info!("Logger initialized!");
|
||||
async {}
|
||||
}
|
||||
}
|
||||
|
||||
/// Alternative logger via UART.
|
||||
#[cfg(feature = "alt-log")]
|
||||
pub mod uart {
|
||||
use super::*;
|
||||
use crate::console;
|
||||
use core::{cell::RefCell, fmt::Write};
|
||||
use critical_section::{CriticalSection, Mutex};
|
||||
use esp_hal::{
|
||||
Blocking,
|
||||
gpio::interconnect::{PeripheralInput, PeripheralOutput},
|
||||
peripherals::UART2,
|
||||
uart::{Uart, UartTx},
|
||||
};
|
||||
use log::{Log, info};
|
||||
|
||||
static ALT_LOGGER_UART: Mutex<RefCell<Option<UartTx<'static, Blocking>>>> =
|
||||
Mutex::new(RefCell::new(None));
|
||||
|
||||
|
|
@ -40,42 +99,12 @@ pub fn with_uart_tx<R>(
|
|||
})
|
||||
}
|
||||
|
||||
struct AlternativeLogger;
|
||||
|
||||
impl Log for AlternativeLogger {
|
||||
#[allow(unused)]
|
||||
fn enabled(&self, _: &log::Metadata) -> bool {
|
||||
// Filtered by `log` already
|
||||
true
|
||||
}
|
||||
|
||||
#[allow(unused)]
|
||||
fn log(&self, record: &log::Record) {
|
||||
with_uart_tx(|cs, uart| {
|
||||
print_log_record(uart, record);
|
||||
})
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
const RESET: &str = "\u{001B}[0m";
|
||||
const RED: &str = "\u{001B}[31m";
|
||||
const GREEN: &str = "\u{001B}[32m";
|
||||
const YELLOW: &str = "\u{001B}[33m";
|
||||
const BLUE: &str = "\u{001B}[34m";
|
||||
const CYAN: &str = "\u{001B}[35m";
|
||||
|
||||
#[cfg(feature = "rtt-log")]
|
||||
#[allow(unused)]
|
||||
use ::rtt_target::{rprint as print, rprintln as println};
|
||||
|
||||
#[cfg(feature = "alt-log")]
|
||||
#[allow(unused)]
|
||||
macro_rules! println {
|
||||
() => {{
|
||||
do_print(Default::default());
|
||||
}};
|
||||
// TODO: I don't think this is necessary. Consider removing.
|
||||
// () => {{
|
||||
// do_print(Default::default());
|
||||
// }};
|
||||
|
||||
($($arg:tt)*) => {{
|
||||
do_print(::core::format_args!($($arg)*));
|
||||
|
|
@ -85,38 +114,34 @@ macro_rules! println {
|
|||
#[allow(unused)]
|
||||
fn do_print(args: core::fmt::Arguments<'_>) {
|
||||
with_uart_tx(|_, uart| {
|
||||
uart.write_fmt(args).unwrap();
|
||||
uart.write_str("\n").unwrap();
|
||||
uart.write_fmt(format_args!("{}\n", args)).unwrap();
|
||||
uart.flush().unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
fn print_log_record(uart: &mut UartTx<'_, Blocking>, record: &log::Record) {
|
||||
let color = match record.level() {
|
||||
log::Level::Error => RED,
|
||||
log::Level::Warn => YELLOW,
|
||||
log::Level::Info => GREEN,
|
||||
log::Level::Debug => BLUE,
|
||||
log::Level::Trace => CYAN,
|
||||
};
|
||||
let reset = RESET;
|
||||
let args = format_args!(
|
||||
"{}{:>5} - {}{}\n",
|
||||
color,
|
||||
record.level(),
|
||||
record.args(),
|
||||
reset
|
||||
);
|
||||
uart.write_fmt(args).unwrap();
|
||||
uart.flush().unwrap();
|
||||
struct UartLogger;
|
||||
|
||||
impl Log for UartLogger {
|
||||
#[allow(unused)]
|
||||
fn enabled(&self, _: &log::Metadata) -> bool {
|
||||
// Filtered by `log` already
|
||||
true
|
||||
}
|
||||
|
||||
#[cfg(feature = "rtt-log")]
|
||||
use panic_rtt_target as _;
|
||||
#[allow(unused)]
|
||||
fn log(&self, record: &log::Record) {
|
||||
with_uart_tx(|cs, uart| {
|
||||
with_formatted_log_record(record, |args| uart.write_fmt(args)).unwrap();
|
||||
uart.flush().unwrap();
|
||||
})
|
||||
}
|
||||
|
||||
fn flush(&self) {}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alt-log")]
|
||||
#[panic_handler]
|
||||
fn panic_handler(info: &core::panic::PanicInfo) -> ! {
|
||||
use super::{RED, RESET};
|
||||
use esp_backtrace::Backtrace;
|
||||
|
||||
println!("{RED}");
|
||||
|
|
@ -135,17 +160,43 @@ fn panic_handler(info: &core::panic::PanicInfo) -> ! {
|
|||
loop {}
|
||||
}
|
||||
|
||||
#[cfg(feature = "alt-log")]
|
||||
pub fn setup_alternative_logging(
|
||||
alt_uart: UartTx<'static, Blocking>,
|
||||
level_filter: log::LevelFilter,
|
||||
) {
|
||||
pub fn setup_logging(
|
||||
uart: impl esp_hal::uart::Instance + 'static,
|
||||
tx: impl PeripheralOutput<'static>,
|
||||
rx: impl PeripheralInput<'static>,
|
||||
) -> impl Future<Output = ()> {
|
||||
let (uart_rx, uart_tx) = Uart::new(uart, Default::default())
|
||||
.unwrap()
|
||||
.with_tx(tx)
|
||||
.with_rx(rx)
|
||||
.split();
|
||||
|
||||
critical_section::with(|cs| {
|
||||
*ALT_LOGGER_UART.borrow(cs).borrow_mut() = Some(alt_uart);
|
||||
*ALT_LOGGER_UART.borrow(cs).borrow_mut() = Some(uart_tx);
|
||||
});
|
||||
|
||||
unsafe {
|
||||
log::set_logger_racy(&AlternativeLogger).unwrap();
|
||||
log::set_max_level_racy(level_filter);
|
||||
log::set_logger_racy(&UartLogger).unwrap();
|
||||
log::set_max_level_racy(LOG_LEVEL_FILTER);
|
||||
}
|
||||
|
||||
info!("Logger initialized!");
|
||||
console::run_console(uart_rx.into_async())
|
||||
}
|
||||
}
|
||||
|
||||
/// Logging via RTT for probe-rs.
|
||||
#[cfg(feature = "rtt-log")]
|
||||
pub mod rtt {
|
||||
use super::*;
|
||||
#[allow(unused)]
|
||||
use ::rtt_target::{rprint as print, rprintln as println};
|
||||
use panic_rtt_target as _; // Use the RTT panic handler.
|
||||
use rtt_target::ChannelMode;
|
||||
|
||||
pub fn setup_logging() -> impl Future<Output = ()> {
|
||||
rtt_target::rtt_init_log!(LOG_LEVEL_FILTER, ChannelMode::BlockIfFull);
|
||||
log::info!("Logger initialized!");
|
||||
async {}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -117,15 +117,6 @@ static SIGNAL_UI_RENDER: Signal<CriticalSectionRawMutex, ()> = Signal::new();
|
|||
|
||||
#[esp_rtos::main]
|
||||
async fn main(_spawner: Spawner) {
|
||||
#[cfg(feature = "usb-log")]
|
||||
{
|
||||
esp_println::logger::init_logger(LOG_LEVEL_FILTER);
|
||||
info!("Logger initialized!");
|
||||
}
|
||||
|
||||
#[cfg(feature = "rtt-log")]
|
||||
rtt_target::rtt_init_log!(LOG_LEVEL_FILTER);
|
||||
|
||||
let config = esp_hal::Config::default()
|
||||
.with_cpu_clock(CpuClock::max())
|
||||
.with_psram(PsramConfig {
|
||||
|
|
@ -135,24 +126,14 @@ async fn main(_spawner: Spawner) {
|
|||
ram_frequency: SpiRamFreq::Freq80m,
|
||||
});
|
||||
let peripherals: esp_hal::peripherals::Peripherals = esp_hal::init(config);
|
||||
info!("System initialized!");
|
||||
|
||||
#[cfg(feature = "usb-log")]
|
||||
let console_task = logging::usb::setup_logging();
|
||||
#[cfg(feature = "alt-log")]
|
||||
let alt_uart_rx_task = {
|
||||
use esp_hal::uart::Uart;
|
||||
|
||||
let (uart_rx, uart_tx) = Uart::new(peripherals.UART2, Default::default())
|
||||
.unwrap()
|
||||
.with_tx(peripherals.GPIO12)
|
||||
.with_rx(peripherals.GPIO5)
|
||||
.split();
|
||||
logging::setup_alternative_logging(uart_tx, LOG_LEVEL_FILTER);
|
||||
info!("Logger initialized!");
|
||||
console::run_console(uart_rx.into_async())
|
||||
};
|
||||
|
||||
#[cfg(not(feature = "alt-log"))]
|
||||
let alt_uart_rx_task = async {};
|
||||
let console_task =
|
||||
logging::uart::setup_logging(peripherals.UART2, peripherals.GPIO12, peripherals.GPIO5);
|
||||
#[cfg(feature = "rtt-log")]
|
||||
let console_task = logging::rtt::setup_logging();
|
||||
|
||||
// Use the internal DRAM as the heap.
|
||||
// Memory reclaimed from the esp-idf bootloader.
|
||||
|
|
@ -444,7 +425,7 @@ async fn main(_spawner: Spawner) {
|
|||
),
|
||||
create_hid_report_interceptor(),
|
||||
user_controller.event_loop(),
|
||||
alt_uart_rx_task
|
||||
console_task
|
||||
]
|
||||
.await;
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue