Skip to content

第十二周工作总结

📦 实现UART日志驱动,为embassy-preempt-log添加第二个输出后端

本周在embassy-preempt/modules/embassy-preempt-log目录下实现了UART驱动,为日志系统添加了除defmt-rtt外的第二个输出后端,使得日志可以通过串口输出。

工作目的

为了后续对系统进行测试,需要让之前的日志系统可用,在运行时能输出更多清晰的信息

问题:串口输出相比defmt-rtt会有延迟,测试数据可能会有偏差


工作成果

目前已经可以从串口接受运行时的日志

bash
cat /dev/ttyUSB0

OSInit
Init_Heap: start=0x41415000, size=10240
FixedSizeBlockAllocator::init: start=0x41415000 size=10240
FixedSizeBlockAllocator::init: completed
Init_Heap: completed
Init Stack Allocator
FixedSizeBlockAllocator::init: start=0x41400000 size=86016
FixedSizeBlockAllocator::init: completed
alloc_stack
alloc: will alloc Layout { size: 4096, align: 4 (1 << 2) }
fallback_alloc: requesting size=4096 align=4096
fallback_alloc: success ptr=0x41400000
alloc a stack at 0x41400000
alloc_stack
alloc: will alloc Layout { size: 8192, align: 4 (1 << 2) }
fallback_alloc: requesting size=8192 align=8192
fallback_alloc: success ptr=0x41402000
alloc a stack at 0x41402000
Init JH7110 Platform
OS_InitTaskIdle
create idle task
Creating sync task with priority 63
the size of future is 8
the prio is exist
init of OS_TASK_STORAGE
prio: 63, _name: ""
alloc of Arena
size of the task storage is 200
created task name:  will be set
alloc: will alloc Layout { size: 25, align: 1 (1 << 0) }
fallback_alloc: requesting size=128 align=128
fallback_alloc: success ptr=0x41415000
defmt test
mip: 0x0

UART驱动实现

核心文件

在 embassy_preempt/modules/embassy-preempt-log/src/uart.rs 中实现了基于uart16550的串口驱动:

rust
/// Fixed UART base address for JH7110 VisionFive 2
const UART_BASE: usize = 0x10010000;

/// Newtype wrapper to make raw pointer Send + Sync
struct UartPtr(*const Uart16550<u32>);

unsafe impl Send for UartPtr {}
unsafe impl Sync for UartPtr {}

/// Global UART pointer, initialized with Once
static UART_PTR: Once<UartPtr> = Once::new();

pub struct Uart;

impl fmt::Write for Uart {
    fn write_str(&mut self, s: &str) -> fmt::Result {
        unsafe { uart_write_str(s) };
        Ok(())
    }
}

日志后端扩展

Feature Flags更新

在 embassy_preempt/modules/embassy-preempt-log/Cargo.toml 中添加了UART相关功能标志:

toml
[features]
default = []
log-base = ["dep:defmt"]
log-rtt = ["dep:defmt-rtt"]
log-uart = ["dep:defmt", "dep:uart16550", "dep:spin"]  # 新增