some_serial/ns16550/
mmio.rs

1//! NS16550 MMIO 版本实现
2//!
3//! 适用于嵌入式平台的内存映射 IO 版本
4
5use super::{Kind, Ns16550};
6use core::ptr::NonNull;
7use rdif_serial::Serial;
8
9pub struct Mmio {
10    base: usize,
11}
12
13impl Kind for Mmio {
14    fn read_reg(&self, reg: u8) -> u8 {
15        unsafe {
16            let addr = self.base + (reg as usize) * 4;
17            core::ptr::read_volatile(addr as *const u8)
18        }
19    }
20
21    fn write_reg(&mut self, reg: u8, val: u8) {
22        unsafe {
23            let addr = self.base + (reg as usize) * 4;
24            core::ptr::write_volatile(addr as *mut u8, val);
25        }
26    }
27
28    fn get_base(&self) -> usize {
29        self.base
30    }
31
32    fn set_base(&mut self, base: usize) {
33        self.base = base;
34    }
35}
36
37impl Ns16550<Mmio> {
38    pub fn new_mmio(base: NonNull<u8>, clock_freq: u32) -> Serial<Self> {
39        Self::new(
40            Mmio {
41                base: base.as_ptr() as _,
42            },
43            clock_freq,
44        )
45    }
46}