derive_mmio_example/
lib.rs1#![no_std]
6#![deny(missing_docs)]
7
8use arbitrary_int::u23;
9use bitbybit::{bitenum, bitfield};
10
11pub struct Uart<'a> {
13 regs: MmioUartRegisters<'a>,
14}
15
16impl<'a> Uart<'a> {
17 pub const fn new(regs: MmioUartRegisters<'a>) -> Uart<'a> {
19 Uart { regs }
20 }
21
22 pub fn configure(&mut self, enabled: bool, baud: u32, parity: Parity) {
24 self.regs.modify_control(|w| {
25 w.with_baud_rate(u23::from_u32(baud));
26 w.with_enable(enabled);
27 w.with_parity(parity);
28 w
29 });
30 }
31
32 pub fn transmit(&mut self, byte: u8) {
36 while !self.regs.read_status().tx_ready() {
37 core::hint::spin_loop();
38 }
39 self.regs.write_fifo(byte as u32);
40 }
41}
42
43#[derive(derive_mmio::Mmio)]
47#[repr(C)]
48pub struct UartRegisters {
49 #[mmio(Read, Write)]
54 pub fifo: u32,
55 #[mmio(Read, Write, Modify)]
59 pub control: Control,
60 #[mmio(PureRead)]
64 pub status: Status,
65}
66
67#[bitfield(u32, debug, exclusive)]
69pub struct Control {
70 #[bits(24..=25, rw)]
72 parity: Option<Parity>,
73 #[bits(1..=23, rw)]
75 baud_rate: u23,
76 #[bit(0, rw)]
78 enable: bool,
79}
80
81#[derive(Debug)]
83#[bitenum(u2, exhaustive = false)]
84pub enum Parity {
85 None = 0,
87 Odd = 1,
89 Even = 2,
91}
92
93#[bitfield(u32, debug, exclusive)]
95pub struct Status {
96 #[bit(1, rw)]
98 rx_ready: bool,
99 #[bit(0, rw)]
101 tx_ready: bool,
102}