137fdb2f5SManos Pitsidianakis // Copyright 2024, Linaro Limited 237fdb2f5SManos Pitsidianakis // Author(s): Manos Pitsidianakis <manos.pitsidianakis@linaro.org> 337fdb2f5SManos Pitsidianakis // SPDX-License-Identifier: GPL-2.0-or-later 437fdb2f5SManos Pitsidianakis // 537fdb2f5SManos Pitsidianakis // PL011 QEMU Device Model 637fdb2f5SManos Pitsidianakis // 737fdb2f5SManos Pitsidianakis // This library implements a device model for the PrimeCell® UART (PL011) 837fdb2f5SManos Pitsidianakis // device in QEMU. 937fdb2f5SManos Pitsidianakis // 1037fdb2f5SManos Pitsidianakis #![doc = include_str!("../README.md")] 1137fdb2f5SManos Pitsidianakis //! # Library crate 1237fdb2f5SManos Pitsidianakis //! 1337fdb2f5SManos Pitsidianakis //! See [`PL011State`](crate::device::PL011State) for the device model type and 1437fdb2f5SManos Pitsidianakis //! the [`registers`] module for register types. 1537fdb2f5SManos Pitsidianakis 1637fdb2f5SManos Pitsidianakis #![deny( 1737fdb2f5SManos Pitsidianakis rustdoc::broken_intra_doc_links, 1837fdb2f5SManos Pitsidianakis rustdoc::redundant_explicit_links, 1937fdb2f5SManos Pitsidianakis clippy::correctness, 2037fdb2f5SManos Pitsidianakis clippy::suspicious, 2137fdb2f5SManos Pitsidianakis clippy::complexity, 2237fdb2f5SManos Pitsidianakis clippy::perf, 2337fdb2f5SManos Pitsidianakis clippy::cargo, 2437fdb2f5SManos Pitsidianakis clippy::nursery, 2537fdb2f5SManos Pitsidianakis clippy::style, 2637fdb2f5SManos Pitsidianakis // restriction group 2737fdb2f5SManos Pitsidianakis clippy::dbg_macro, 2837fdb2f5SManos Pitsidianakis clippy::as_underscore, 2937fdb2f5SManos Pitsidianakis clippy::assertions_on_result_states, 3037fdb2f5SManos Pitsidianakis // pedantic group 3137fdb2f5SManos Pitsidianakis clippy::doc_markdown, 3237fdb2f5SManos Pitsidianakis clippy::borrow_as_ptr, 3337fdb2f5SManos Pitsidianakis clippy::cast_lossless, 3437fdb2f5SManos Pitsidianakis clippy::option_if_let_else, 3537fdb2f5SManos Pitsidianakis clippy::missing_const_for_fn, 3637fdb2f5SManos Pitsidianakis clippy::cognitive_complexity, 3737fdb2f5SManos Pitsidianakis clippy::missing_safety_doc, 3837fdb2f5SManos Pitsidianakis )] 39*93243319SManos Pitsidianakis #![allow(clippy::result_unit_err)] 4037fdb2f5SManos Pitsidianakis 4137fdb2f5SManos Pitsidianakis extern crate bilge; 4237fdb2f5SManos Pitsidianakis extern crate bilge_impl; 4337fdb2f5SManos Pitsidianakis extern crate qemu_api; 4437fdb2f5SManos Pitsidianakis 4537fdb2f5SManos Pitsidianakis pub mod device; 4637fdb2f5SManos Pitsidianakis pub mod device_class; 4737fdb2f5SManos Pitsidianakis pub mod memory_ops; 4837fdb2f5SManos Pitsidianakis 4937fdb2f5SManos Pitsidianakis pub const TYPE_PL011: &::core::ffi::CStr = c"pl011"; 5037fdb2f5SManos Pitsidianakis 5137fdb2f5SManos Pitsidianakis /// Offset of each register from the base memory address of the device. 5237fdb2f5SManos Pitsidianakis /// 5337fdb2f5SManos Pitsidianakis /// # Source 5437fdb2f5SManos Pitsidianakis /// ARM DDI 0183G, Table 3-1 p.3-3 5537fdb2f5SManos Pitsidianakis #[doc(alias = "offset")] 5637fdb2f5SManos Pitsidianakis #[allow(non_camel_case_types)] 5737fdb2f5SManos Pitsidianakis #[repr(u64)] 5837fdb2f5SManos Pitsidianakis #[derive(Debug)] 5937fdb2f5SManos Pitsidianakis pub enum RegisterOffset { 6037fdb2f5SManos Pitsidianakis /// Data Register 6137fdb2f5SManos Pitsidianakis /// 6237fdb2f5SManos Pitsidianakis /// A write to this register initiates the actual data transmission 6337fdb2f5SManos Pitsidianakis #[doc(alias = "UARTDR")] 6437fdb2f5SManos Pitsidianakis DR = 0x000, 6537fdb2f5SManos Pitsidianakis /// Receive Status Register or Error Clear Register 6637fdb2f5SManos Pitsidianakis #[doc(alias = "UARTRSR")] 6737fdb2f5SManos Pitsidianakis #[doc(alias = "UARTECR")] 6837fdb2f5SManos Pitsidianakis RSR = 0x004, 6937fdb2f5SManos Pitsidianakis /// Flag Register 7037fdb2f5SManos Pitsidianakis /// 7137fdb2f5SManos Pitsidianakis /// A read of this register shows if transmission is complete 7237fdb2f5SManos Pitsidianakis #[doc(alias = "UARTFR")] 7337fdb2f5SManos Pitsidianakis FR = 0x018, 7437fdb2f5SManos Pitsidianakis /// Fractional Baud Rate Register 7537fdb2f5SManos Pitsidianakis /// 7637fdb2f5SManos Pitsidianakis /// responsible for baud rate speed 7737fdb2f5SManos Pitsidianakis #[doc(alias = "UARTFBRD")] 7837fdb2f5SManos Pitsidianakis FBRD = 0x028, 7937fdb2f5SManos Pitsidianakis /// `IrDA` Low-Power Counter Register 8037fdb2f5SManos Pitsidianakis #[doc(alias = "UARTILPR")] 8137fdb2f5SManos Pitsidianakis ILPR = 0x020, 8237fdb2f5SManos Pitsidianakis /// Integer Baud Rate Register 8337fdb2f5SManos Pitsidianakis /// 8437fdb2f5SManos Pitsidianakis /// Responsible for baud rate speed 8537fdb2f5SManos Pitsidianakis #[doc(alias = "UARTIBRD")] 8637fdb2f5SManos Pitsidianakis IBRD = 0x024, 8737fdb2f5SManos Pitsidianakis /// line control register (data frame format) 8837fdb2f5SManos Pitsidianakis #[doc(alias = "UARTLCR_H")] 8937fdb2f5SManos Pitsidianakis LCR_H = 0x02C, 9037fdb2f5SManos Pitsidianakis /// Toggle UART, transmission or reception 9137fdb2f5SManos Pitsidianakis #[doc(alias = "UARTCR")] 9237fdb2f5SManos Pitsidianakis CR = 0x030, 9337fdb2f5SManos Pitsidianakis /// Interrupt FIFO Level Select Register 9437fdb2f5SManos Pitsidianakis #[doc(alias = "UARTIFLS")] 9537fdb2f5SManos Pitsidianakis FLS = 0x034, 9637fdb2f5SManos Pitsidianakis /// Interrupt Mask Set/Clear Register 9737fdb2f5SManos Pitsidianakis #[doc(alias = "UARTIMSC")] 9837fdb2f5SManos Pitsidianakis IMSC = 0x038, 9937fdb2f5SManos Pitsidianakis /// Raw Interrupt Status Register 10037fdb2f5SManos Pitsidianakis #[doc(alias = "UARTRIS")] 10137fdb2f5SManos Pitsidianakis RIS = 0x03C, 10237fdb2f5SManos Pitsidianakis /// Masked Interrupt Status Register 10337fdb2f5SManos Pitsidianakis #[doc(alias = "UARTMIS")] 10437fdb2f5SManos Pitsidianakis MIS = 0x040, 10537fdb2f5SManos Pitsidianakis /// Interrupt Clear Register 10637fdb2f5SManos Pitsidianakis #[doc(alias = "UARTICR")] 10737fdb2f5SManos Pitsidianakis ICR = 0x044, 10837fdb2f5SManos Pitsidianakis /// DMA control Register 10937fdb2f5SManos Pitsidianakis #[doc(alias = "UARTDMACR")] 11037fdb2f5SManos Pitsidianakis DMACR = 0x048, 11137fdb2f5SManos Pitsidianakis ///// Reserved, offsets `0x04C` to `0x07C`. 11237fdb2f5SManos Pitsidianakis //Reserved = 0x04C, 11337fdb2f5SManos Pitsidianakis } 11437fdb2f5SManos Pitsidianakis 11537fdb2f5SManos Pitsidianakis impl core::convert::TryFrom<u64> for RegisterOffset { 11637fdb2f5SManos Pitsidianakis type Error = u64; 11737fdb2f5SManos Pitsidianakis 11837fdb2f5SManos Pitsidianakis fn try_from(value: u64) -> Result<Self, Self::Error> { 11937fdb2f5SManos Pitsidianakis macro_rules! case { 12037fdb2f5SManos Pitsidianakis ($($discriminant:ident),*$(,)*) => { 12137fdb2f5SManos Pitsidianakis /* check that matching on all macro arguments compiles, which means we are not 12237fdb2f5SManos Pitsidianakis * missing any enum value; if the type definition ever changes this will stop 12337fdb2f5SManos Pitsidianakis * compiling. 12437fdb2f5SManos Pitsidianakis */ 12537fdb2f5SManos Pitsidianakis const fn _assert_exhaustive(val: RegisterOffset) { 12637fdb2f5SManos Pitsidianakis match val { 12737fdb2f5SManos Pitsidianakis $(RegisterOffset::$discriminant => (),)* 12837fdb2f5SManos Pitsidianakis } 12937fdb2f5SManos Pitsidianakis } 13037fdb2f5SManos Pitsidianakis 13137fdb2f5SManos Pitsidianakis match value { 13237fdb2f5SManos Pitsidianakis $(x if x == Self::$discriminant as u64 => Ok(Self::$discriminant),)* 13337fdb2f5SManos Pitsidianakis _ => Err(value), 13437fdb2f5SManos Pitsidianakis } 13537fdb2f5SManos Pitsidianakis } 13637fdb2f5SManos Pitsidianakis } 13737fdb2f5SManos Pitsidianakis case! { DR, RSR, FR, FBRD, ILPR, IBRD, LCR_H, CR, FLS, IMSC, RIS, MIS, ICR, DMACR } 13837fdb2f5SManos Pitsidianakis } 13937fdb2f5SManos Pitsidianakis } 14037fdb2f5SManos Pitsidianakis 14137fdb2f5SManos Pitsidianakis pub mod registers { 14237fdb2f5SManos Pitsidianakis //! Device registers exposed as typed structs which are backed by arbitrary 14337fdb2f5SManos Pitsidianakis //! integer bitmaps. [`Data`], [`Control`], [`LineControl`], etc. 14437fdb2f5SManos Pitsidianakis //! 14537fdb2f5SManos Pitsidianakis //! All PL011 registers are essentially 32-bit wide, but are typed here as 14637fdb2f5SManos Pitsidianakis //! bitmaps with only the necessary width. That is, if a struct bitmap 14737fdb2f5SManos Pitsidianakis //! in this module is for example 16 bits long, it should be conceived 14837fdb2f5SManos Pitsidianakis //! as a 32-bit register where the unmentioned higher bits are always 14937fdb2f5SManos Pitsidianakis //! unused thus treated as zero when read or written. 15037fdb2f5SManos Pitsidianakis use bilge::prelude::*; 15137fdb2f5SManos Pitsidianakis 15237fdb2f5SManos Pitsidianakis // TODO: FIFO Mode has different semantics 15337fdb2f5SManos Pitsidianakis /// Data Register, `UARTDR` 15437fdb2f5SManos Pitsidianakis /// 15537fdb2f5SManos Pitsidianakis /// The `UARTDR` register is the data register. 15637fdb2f5SManos Pitsidianakis /// 15737fdb2f5SManos Pitsidianakis /// For words to be transmitted: 15837fdb2f5SManos Pitsidianakis /// 15937fdb2f5SManos Pitsidianakis /// - if the FIFOs are enabled, data written to this location is pushed onto 16037fdb2f5SManos Pitsidianakis /// the transmit 16137fdb2f5SManos Pitsidianakis /// FIFO 16237fdb2f5SManos Pitsidianakis /// - if the FIFOs are not enabled, data is stored in the transmitter 16337fdb2f5SManos Pitsidianakis /// holding register (the 16437fdb2f5SManos Pitsidianakis /// bottom word of the transmit FIFO). 16537fdb2f5SManos Pitsidianakis /// 16637fdb2f5SManos Pitsidianakis /// The write operation initiates transmission from the UART. The data is 16737fdb2f5SManos Pitsidianakis /// prefixed with a start bit, appended with the appropriate parity bit 16837fdb2f5SManos Pitsidianakis /// (if parity is enabled), and a stop bit. The resultant word is then 16937fdb2f5SManos Pitsidianakis /// transmitted. 17037fdb2f5SManos Pitsidianakis /// 17137fdb2f5SManos Pitsidianakis /// For received words: 17237fdb2f5SManos Pitsidianakis /// 17337fdb2f5SManos Pitsidianakis /// - if the FIFOs are enabled, the data byte and the 4-bit status (break, 17437fdb2f5SManos Pitsidianakis /// frame, parity, 17537fdb2f5SManos Pitsidianakis /// and overrun) is pushed onto the 12-bit wide receive FIFO 17637fdb2f5SManos Pitsidianakis /// - if the FIFOs are not enabled, the data byte and status are stored in 17737fdb2f5SManos Pitsidianakis /// the receiving 17837fdb2f5SManos Pitsidianakis /// holding register (the bottom word of the receive FIFO). 17937fdb2f5SManos Pitsidianakis /// 18037fdb2f5SManos Pitsidianakis /// The received data byte is read by performing reads from the `UARTDR` 18137fdb2f5SManos Pitsidianakis /// register along with the corresponding status information. The status 18237fdb2f5SManos Pitsidianakis /// information can also be read by a read of the `UARTRSR/UARTECR` 18337fdb2f5SManos Pitsidianakis /// register. 18437fdb2f5SManos Pitsidianakis /// 18537fdb2f5SManos Pitsidianakis /// # Note 18637fdb2f5SManos Pitsidianakis /// 18737fdb2f5SManos Pitsidianakis /// You must disable the UART before any of the control registers are 18837fdb2f5SManos Pitsidianakis /// reprogrammed. When the UART is disabled in the middle of 18937fdb2f5SManos Pitsidianakis /// transmission or reception, it completes the current character before 19037fdb2f5SManos Pitsidianakis /// stopping. 19137fdb2f5SManos Pitsidianakis /// 19237fdb2f5SManos Pitsidianakis /// # Source 19337fdb2f5SManos Pitsidianakis /// ARM DDI 0183G 3.3.1 Data Register, UARTDR 19437fdb2f5SManos Pitsidianakis #[bitsize(16)] 19537fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 19637fdb2f5SManos Pitsidianakis #[doc(alias = "UARTDR")] 19737fdb2f5SManos Pitsidianakis pub struct Data { 19837fdb2f5SManos Pitsidianakis _reserved: u4, 19937fdb2f5SManos Pitsidianakis pub data: u8, 20037fdb2f5SManos Pitsidianakis pub framing_error: bool, 20137fdb2f5SManos Pitsidianakis pub parity_error: bool, 20237fdb2f5SManos Pitsidianakis pub break_error: bool, 20337fdb2f5SManos Pitsidianakis pub overrun_error: bool, 20437fdb2f5SManos Pitsidianakis } 20537fdb2f5SManos Pitsidianakis 20637fdb2f5SManos Pitsidianakis // TODO: FIFO Mode has different semantics 20737fdb2f5SManos Pitsidianakis /// Receive Status Register / Error Clear Register, `UARTRSR/UARTECR` 20837fdb2f5SManos Pitsidianakis /// 20937fdb2f5SManos Pitsidianakis /// The UARTRSR/UARTECR register is the receive status register/error clear 21037fdb2f5SManos Pitsidianakis /// register. Receive status can also be read from the `UARTRSR` 21137fdb2f5SManos Pitsidianakis /// register. If the status is read from this register, then the status 21237fdb2f5SManos Pitsidianakis /// information for break, framing and parity corresponds to the 21337fdb2f5SManos Pitsidianakis /// data character read from the [Data register](Data), `UARTDR` prior to 21437fdb2f5SManos Pitsidianakis /// reading the UARTRSR register. The status information for overrun is 21537fdb2f5SManos Pitsidianakis /// set immediately when an overrun condition occurs. 21637fdb2f5SManos Pitsidianakis /// 21737fdb2f5SManos Pitsidianakis /// 21837fdb2f5SManos Pitsidianakis /// # Note 21937fdb2f5SManos Pitsidianakis /// The received data character must be read first from the [Data 22037fdb2f5SManos Pitsidianakis /// Register](Data), `UARTDR` before reading the error status associated 22137fdb2f5SManos Pitsidianakis /// with that data character from the `UARTRSR` register. This read 22237fdb2f5SManos Pitsidianakis /// sequence cannot be reversed, because the `UARTRSR` register is 22337fdb2f5SManos Pitsidianakis /// updated only when a read occurs from the `UARTDR` register. However, 22437fdb2f5SManos Pitsidianakis /// the status information can also be obtained by reading the `UARTDR` 22537fdb2f5SManos Pitsidianakis /// register 22637fdb2f5SManos Pitsidianakis /// 22737fdb2f5SManos Pitsidianakis /// # Source 22837fdb2f5SManos Pitsidianakis /// ARM DDI 0183G 3.3.2 Receive Status Register/Error Clear Register, 22937fdb2f5SManos Pitsidianakis /// UARTRSR/UARTECR 23037fdb2f5SManos Pitsidianakis #[bitsize(8)] 23137fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 23237fdb2f5SManos Pitsidianakis pub struct ReceiveStatusErrorClear { 23337fdb2f5SManos Pitsidianakis pub framing_error: bool, 23437fdb2f5SManos Pitsidianakis pub parity_error: bool, 23537fdb2f5SManos Pitsidianakis pub break_error: bool, 23637fdb2f5SManos Pitsidianakis pub overrun_error: bool, 23737fdb2f5SManos Pitsidianakis _reserved_unpredictable: u4, 23837fdb2f5SManos Pitsidianakis } 23937fdb2f5SManos Pitsidianakis 24037fdb2f5SManos Pitsidianakis impl ReceiveStatusErrorClear { 24137fdb2f5SManos Pitsidianakis pub fn reset(&mut self) { 24237fdb2f5SManos Pitsidianakis // All the bits are cleared to 0 on reset. 24337fdb2f5SManos Pitsidianakis *self = 0.into(); 24437fdb2f5SManos Pitsidianakis } 24537fdb2f5SManos Pitsidianakis } 24637fdb2f5SManos Pitsidianakis 24737fdb2f5SManos Pitsidianakis impl Default for ReceiveStatusErrorClear { 24837fdb2f5SManos Pitsidianakis fn default() -> Self { 24937fdb2f5SManos Pitsidianakis 0.into() 25037fdb2f5SManos Pitsidianakis } 25137fdb2f5SManos Pitsidianakis } 25237fdb2f5SManos Pitsidianakis 25337fdb2f5SManos Pitsidianakis #[bitsize(16)] 25437fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 25537fdb2f5SManos Pitsidianakis /// Flag Register, `UARTFR` 25637fdb2f5SManos Pitsidianakis #[doc(alias = "UARTFR")] 25737fdb2f5SManos Pitsidianakis pub struct Flags { 25837fdb2f5SManos Pitsidianakis /// CTS Clear to send. This bit is the complement of the UART clear to 25937fdb2f5SManos Pitsidianakis /// send, `nUARTCTS`, modem status input. That is, the bit is 1 26037fdb2f5SManos Pitsidianakis /// when `nUARTCTS` is LOW. 26137fdb2f5SManos Pitsidianakis pub clear_to_send: bool, 26237fdb2f5SManos Pitsidianakis /// DSR Data set ready. This bit is the complement of the UART data set 26337fdb2f5SManos Pitsidianakis /// ready, `nUARTDSR`, modem status input. That is, the bit is 1 when 26437fdb2f5SManos Pitsidianakis /// `nUARTDSR` is LOW. 26537fdb2f5SManos Pitsidianakis pub data_set_ready: bool, 26637fdb2f5SManos Pitsidianakis /// DCD Data carrier detect. This bit is the complement of the UART data 26737fdb2f5SManos Pitsidianakis /// carrier detect, `nUARTDCD`, modem status input. That is, the bit is 26837fdb2f5SManos Pitsidianakis /// 1 when `nUARTDCD` is LOW. 26937fdb2f5SManos Pitsidianakis pub data_carrier_detect: bool, 27037fdb2f5SManos Pitsidianakis /// BUSY UART busy. If this bit is set to 1, the UART is busy 27137fdb2f5SManos Pitsidianakis /// transmitting data. This bit remains set until the complete 27237fdb2f5SManos Pitsidianakis /// byte, including all the stop bits, has been sent from the 27337fdb2f5SManos Pitsidianakis /// shift register. This bit is set as soon as the transmit FIFO 27437fdb2f5SManos Pitsidianakis /// becomes non-empty, regardless of whether the UART is enabled 27537fdb2f5SManos Pitsidianakis /// or not. 27637fdb2f5SManos Pitsidianakis pub busy: bool, 27737fdb2f5SManos Pitsidianakis /// RXFE Receive FIFO empty. The meaning of this bit depends on the 27837fdb2f5SManos Pitsidianakis /// state of the FEN bit in the UARTLCR_H register. If the FIFO 27937fdb2f5SManos Pitsidianakis /// is disabled, this bit is set when the receive holding 28037fdb2f5SManos Pitsidianakis /// register is empty. If the FIFO is enabled, the RXFE bit is 28137fdb2f5SManos Pitsidianakis /// set when the receive FIFO is empty. 28237fdb2f5SManos Pitsidianakis pub receive_fifo_empty: bool, 28337fdb2f5SManos Pitsidianakis /// TXFF Transmit FIFO full. The meaning of this bit depends on the 28437fdb2f5SManos Pitsidianakis /// state of the FEN bit in the UARTLCR_H register. If the FIFO 28537fdb2f5SManos Pitsidianakis /// is disabled, this bit is set when the transmit holding 28637fdb2f5SManos Pitsidianakis /// register is full. If the FIFO is enabled, the TXFF bit is 28737fdb2f5SManos Pitsidianakis /// set when the transmit FIFO is full. 28837fdb2f5SManos Pitsidianakis pub transmit_fifo_full: bool, 28937fdb2f5SManos Pitsidianakis /// RXFF Receive FIFO full. The meaning of this bit depends on the state 29037fdb2f5SManos Pitsidianakis /// of the FEN bit in the UARTLCR_H register. If the FIFO is 29137fdb2f5SManos Pitsidianakis /// disabled, this bit is set when the receive holding register 29237fdb2f5SManos Pitsidianakis /// is full. If the FIFO is enabled, the RXFF bit is set when 29337fdb2f5SManos Pitsidianakis /// the receive FIFO is full. 29437fdb2f5SManos Pitsidianakis pub receive_fifo_full: bool, 29537fdb2f5SManos Pitsidianakis /// Transmit FIFO empty. The meaning of this bit depends on the state of 29637fdb2f5SManos Pitsidianakis /// the FEN bit in the [Line Control register](LineControl), 29737fdb2f5SManos Pitsidianakis /// `UARTLCR_H`. If the FIFO is disabled, this bit is set when the 29837fdb2f5SManos Pitsidianakis /// transmit holding register is empty. If the FIFO is enabled, 29937fdb2f5SManos Pitsidianakis /// the TXFE bit is set when the transmit FIFO is empty. This 30037fdb2f5SManos Pitsidianakis /// bit does not indicate if there is data in the transmit shift 30137fdb2f5SManos Pitsidianakis /// register. 30237fdb2f5SManos Pitsidianakis pub transmit_fifo_empty: bool, 30337fdb2f5SManos Pitsidianakis /// `RI`, is `true` when `nUARTRI` is `LOW`. 30437fdb2f5SManos Pitsidianakis pub ring_indicator: bool, 30537fdb2f5SManos Pitsidianakis _reserved_zero_no_modify: u7, 30637fdb2f5SManos Pitsidianakis } 30737fdb2f5SManos Pitsidianakis 30837fdb2f5SManos Pitsidianakis impl Flags { 30937fdb2f5SManos Pitsidianakis pub fn reset(&mut self) { 31037fdb2f5SManos Pitsidianakis // After reset TXFF, RXFF, and BUSY are 0, and TXFE and RXFE are 1 31137fdb2f5SManos Pitsidianakis self.set_receive_fifo_full(false); 31237fdb2f5SManos Pitsidianakis self.set_transmit_fifo_full(false); 31337fdb2f5SManos Pitsidianakis self.set_busy(false); 31437fdb2f5SManos Pitsidianakis self.set_receive_fifo_empty(true); 31537fdb2f5SManos Pitsidianakis self.set_transmit_fifo_empty(true); 31637fdb2f5SManos Pitsidianakis } 31737fdb2f5SManos Pitsidianakis } 31837fdb2f5SManos Pitsidianakis 31937fdb2f5SManos Pitsidianakis impl Default for Flags { 32037fdb2f5SManos Pitsidianakis fn default() -> Self { 32137fdb2f5SManos Pitsidianakis let mut ret: Self = 0.into(); 32237fdb2f5SManos Pitsidianakis ret.reset(); 32337fdb2f5SManos Pitsidianakis ret 32437fdb2f5SManos Pitsidianakis } 32537fdb2f5SManos Pitsidianakis } 32637fdb2f5SManos Pitsidianakis 32737fdb2f5SManos Pitsidianakis #[bitsize(16)] 32837fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 32937fdb2f5SManos Pitsidianakis /// Line Control Register, `UARTLCR_H` 33037fdb2f5SManos Pitsidianakis #[doc(alias = "UARTLCR_H")] 33137fdb2f5SManos Pitsidianakis pub struct LineControl { 33237fdb2f5SManos Pitsidianakis /// 15:8 - Reserved, do not modify, read as zero. 33337fdb2f5SManos Pitsidianakis _reserved_zero_no_modify: u8, 33437fdb2f5SManos Pitsidianakis /// 7 SPS Stick parity select. 33537fdb2f5SManos Pitsidianakis /// 0 = stick parity is disabled 33637fdb2f5SManos Pitsidianakis /// 1 = either: 33737fdb2f5SManos Pitsidianakis /// • if the EPS bit is 0 then the parity bit is transmitted and checked 33837fdb2f5SManos Pitsidianakis /// as a 1 • if the EPS bit is 1 then the parity bit is 33937fdb2f5SManos Pitsidianakis /// transmitted and checked as a 0. This bit has no effect when 34037fdb2f5SManos Pitsidianakis /// the PEN bit disables parity checking and generation. See Table 3-11 34137fdb2f5SManos Pitsidianakis /// on page 3-14 for the parity truth table. 34237fdb2f5SManos Pitsidianakis pub sticky_parity: bool, 34337fdb2f5SManos Pitsidianakis /// WLEN Word length. These bits indicate the number of data bits 34437fdb2f5SManos Pitsidianakis /// transmitted or received in a frame as follows: b11 = 8 bits 34537fdb2f5SManos Pitsidianakis /// b10 = 7 bits 34637fdb2f5SManos Pitsidianakis /// b01 = 6 bits 34737fdb2f5SManos Pitsidianakis /// b00 = 5 bits. 34837fdb2f5SManos Pitsidianakis pub word_length: WordLength, 34937fdb2f5SManos Pitsidianakis /// FEN Enable FIFOs: 35037fdb2f5SManos Pitsidianakis /// 0 = FIFOs are disabled (character mode) that is, the FIFOs become 35137fdb2f5SManos Pitsidianakis /// 1-byte-deep holding registers 1 = transmit and receive FIFO 35237fdb2f5SManos Pitsidianakis /// buffers are enabled (FIFO mode). 35337fdb2f5SManos Pitsidianakis pub fifos_enabled: Mode, 35437fdb2f5SManos Pitsidianakis /// 3 STP2 Two stop bits select. If this bit is set to 1, two stop bits 35537fdb2f5SManos Pitsidianakis /// are transmitted at the end of the frame. The receive 35637fdb2f5SManos Pitsidianakis /// logic does not check for two stop bits being received. 35737fdb2f5SManos Pitsidianakis pub two_stops_bits: bool, 35837fdb2f5SManos Pitsidianakis /// EPS Even parity select. Controls the type of parity the UART uses 35937fdb2f5SManos Pitsidianakis /// during transmission and reception: 36037fdb2f5SManos Pitsidianakis /// - 0 = odd parity. The UART generates or checks for an odd number of 36137fdb2f5SManos Pitsidianakis /// 1s in the data and parity bits. 36237fdb2f5SManos Pitsidianakis /// - 1 = even parity. The UART generates or checks for an even number 36337fdb2f5SManos Pitsidianakis /// of 1s in the data and parity bits. 36437fdb2f5SManos Pitsidianakis /// This bit has no effect when the `PEN` bit disables parity checking 36537fdb2f5SManos Pitsidianakis /// and generation. See Table 3-11 on page 3-14 for the parity 36637fdb2f5SManos Pitsidianakis /// truth table. 36737fdb2f5SManos Pitsidianakis pub parity: Parity, 36837fdb2f5SManos Pitsidianakis /// 1 PEN Parity enable: 36937fdb2f5SManos Pitsidianakis /// 37037fdb2f5SManos Pitsidianakis /// - 0 = parity is disabled and no parity bit added to the data frame 37137fdb2f5SManos Pitsidianakis /// - 1 = parity checking and generation is enabled. 37237fdb2f5SManos Pitsidianakis /// 37337fdb2f5SManos Pitsidianakis /// See Table 3-11 on page 3-14 for the parity truth table. 37437fdb2f5SManos Pitsidianakis pub parity_enabled: bool, 37537fdb2f5SManos Pitsidianakis /// BRK Send break. 37637fdb2f5SManos Pitsidianakis /// 37737fdb2f5SManos Pitsidianakis /// If this bit is set to `1`, a low-level is continually output on the 37837fdb2f5SManos Pitsidianakis /// `UARTTXD` output, after completing transmission of the 37937fdb2f5SManos Pitsidianakis /// current character. For the proper execution of the break command, 38037fdb2f5SManos Pitsidianakis /// the software must set this bit for at least two complete 38137fdb2f5SManos Pitsidianakis /// frames. For normal use, this bit must be cleared to `0`. 38237fdb2f5SManos Pitsidianakis pub send_break: bool, 38337fdb2f5SManos Pitsidianakis } 38437fdb2f5SManos Pitsidianakis 38537fdb2f5SManos Pitsidianakis impl LineControl { 38637fdb2f5SManos Pitsidianakis pub fn reset(&mut self) { 38737fdb2f5SManos Pitsidianakis // All the bits are cleared to 0 when reset. 38837fdb2f5SManos Pitsidianakis *self = 0.into(); 38937fdb2f5SManos Pitsidianakis } 39037fdb2f5SManos Pitsidianakis } 39137fdb2f5SManos Pitsidianakis 39237fdb2f5SManos Pitsidianakis impl Default for LineControl { 39337fdb2f5SManos Pitsidianakis fn default() -> Self { 39437fdb2f5SManos Pitsidianakis 0.into() 39537fdb2f5SManos Pitsidianakis } 39637fdb2f5SManos Pitsidianakis } 39737fdb2f5SManos Pitsidianakis 39837fdb2f5SManos Pitsidianakis #[bitsize(1)] 39937fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)] 40037fdb2f5SManos Pitsidianakis /// `EPS` "Even parity select", field of [Line Control 40137fdb2f5SManos Pitsidianakis /// register](LineControl). 40237fdb2f5SManos Pitsidianakis pub enum Parity { 40337fdb2f5SManos Pitsidianakis /// - 0 = odd parity. The UART generates or checks for an odd number of 40437fdb2f5SManos Pitsidianakis /// 1s in the data and parity bits. 40537fdb2f5SManos Pitsidianakis Odd = 0, 40637fdb2f5SManos Pitsidianakis /// - 1 = even parity. The UART generates or checks for an even number 40737fdb2f5SManos Pitsidianakis /// of 1s in the data and parity bits. 40837fdb2f5SManos Pitsidianakis Even = 1, 40937fdb2f5SManos Pitsidianakis } 41037fdb2f5SManos Pitsidianakis 41137fdb2f5SManos Pitsidianakis #[bitsize(1)] 41237fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)] 41337fdb2f5SManos Pitsidianakis /// `FEN` "Enable FIFOs" or Device mode, field of [Line Control 41437fdb2f5SManos Pitsidianakis /// register](LineControl). 41537fdb2f5SManos Pitsidianakis pub enum Mode { 41637fdb2f5SManos Pitsidianakis /// 0 = FIFOs are disabled (character mode) that is, the FIFOs become 41737fdb2f5SManos Pitsidianakis /// 1-byte-deep holding registers 41837fdb2f5SManos Pitsidianakis Character = 0, 41937fdb2f5SManos Pitsidianakis /// 1 = transmit and receive FIFO buffers are enabled (FIFO mode). 42037fdb2f5SManos Pitsidianakis FIFO = 1, 42137fdb2f5SManos Pitsidianakis } 42237fdb2f5SManos Pitsidianakis 42337fdb2f5SManos Pitsidianakis impl From<Mode> for bool { 42437fdb2f5SManos Pitsidianakis fn from(val: Mode) -> Self { 42537fdb2f5SManos Pitsidianakis matches!(val, Mode::FIFO) 42637fdb2f5SManos Pitsidianakis } 42737fdb2f5SManos Pitsidianakis } 42837fdb2f5SManos Pitsidianakis 42937fdb2f5SManos Pitsidianakis #[bitsize(2)] 43037fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, Debug, Eq, FromBits, PartialEq)] 43137fdb2f5SManos Pitsidianakis /// `WLEN` Word length, field of [Line Control register](LineControl). 43237fdb2f5SManos Pitsidianakis /// 43337fdb2f5SManos Pitsidianakis /// These bits indicate the number of data bits transmitted or received in a 43437fdb2f5SManos Pitsidianakis /// frame as follows: 43537fdb2f5SManos Pitsidianakis pub enum WordLength { 43637fdb2f5SManos Pitsidianakis /// b11 = 8 bits 43737fdb2f5SManos Pitsidianakis _8Bits = 0b11, 43837fdb2f5SManos Pitsidianakis /// b10 = 7 bits 43937fdb2f5SManos Pitsidianakis _7Bits = 0b10, 44037fdb2f5SManos Pitsidianakis /// b01 = 6 bits 44137fdb2f5SManos Pitsidianakis _6Bits = 0b01, 44237fdb2f5SManos Pitsidianakis /// b00 = 5 bits. 44337fdb2f5SManos Pitsidianakis _5Bits = 0b00, 44437fdb2f5SManos Pitsidianakis } 44537fdb2f5SManos Pitsidianakis 44637fdb2f5SManos Pitsidianakis /// Control Register, `UARTCR` 44737fdb2f5SManos Pitsidianakis /// 44837fdb2f5SManos Pitsidianakis /// The `UARTCR` register is the control register. All the bits are cleared 44937fdb2f5SManos Pitsidianakis /// to `0` on reset except for bits `9` and `8` that are set to `1`. 45037fdb2f5SManos Pitsidianakis /// 45137fdb2f5SManos Pitsidianakis /// # Source 45237fdb2f5SManos Pitsidianakis /// ARM DDI 0183G, 3.3.8 Control Register, `UARTCR`, Table 3-12 45337fdb2f5SManos Pitsidianakis #[bitsize(16)] 45437fdb2f5SManos Pitsidianakis #[doc(alias = "UARTCR")] 45537fdb2f5SManos Pitsidianakis #[derive(Clone, Copy, DebugBits, FromBits)] 45637fdb2f5SManos Pitsidianakis pub struct Control { 45737fdb2f5SManos Pitsidianakis /// `UARTEN` UART enable: 0 = UART is disabled. If the UART is disabled 45837fdb2f5SManos Pitsidianakis /// in the middle of transmission or reception, it completes the current 45937fdb2f5SManos Pitsidianakis /// character before stopping. 1 = the UART is enabled. Data 46037fdb2f5SManos Pitsidianakis /// transmission and reception occurs for either UART signals or SIR 46137fdb2f5SManos Pitsidianakis /// signals depending on the setting of the SIREN bit. 46237fdb2f5SManos Pitsidianakis pub enable_uart: bool, 46337fdb2f5SManos Pitsidianakis /// `SIREN` `SIR` enable: 0 = IrDA SIR ENDEC is disabled. `nSIROUT` 46437fdb2f5SManos Pitsidianakis /// remains LOW (no light pulse generated), and signal transitions on 46537fdb2f5SManos Pitsidianakis /// SIRIN have no effect. 1 = IrDA SIR ENDEC is enabled. Data is 46637fdb2f5SManos Pitsidianakis /// transmitted and received on nSIROUT and SIRIN. UARTTXD remains HIGH, 46737fdb2f5SManos Pitsidianakis /// in the marking state. Signal transitions on UARTRXD or modem status 46837fdb2f5SManos Pitsidianakis /// inputs have no effect. This bit has no effect if the UARTEN bit 46937fdb2f5SManos Pitsidianakis /// disables the UART. 47037fdb2f5SManos Pitsidianakis pub enable_sir: bool, 47137fdb2f5SManos Pitsidianakis /// `SIRLP` SIR low-power IrDA mode. This bit selects the IrDA encoding 47237fdb2f5SManos Pitsidianakis /// mode. If this bit is cleared to 0, low-level bits are transmitted as 47337fdb2f5SManos Pitsidianakis /// an active high pulse with a width of 3/ 16th of the bit period. If 47437fdb2f5SManos Pitsidianakis /// this bit is set to 1, low-level bits are transmitted with a pulse 47537fdb2f5SManos Pitsidianakis /// width that is 3 times the period of the IrLPBaud16 input signal, 47637fdb2f5SManos Pitsidianakis /// regardless of the selected bit rate. Setting this bit uses less 47737fdb2f5SManos Pitsidianakis /// power, but might reduce transmission distances. 47837fdb2f5SManos Pitsidianakis pub sir_lowpower_irda_mode: u1, 47937fdb2f5SManos Pitsidianakis /// Reserved, do not modify, read as zero. 48037fdb2f5SManos Pitsidianakis _reserved_zero_no_modify: u4, 48137fdb2f5SManos Pitsidianakis /// `LBE` Loopback enable. If this bit is set to 1 and the SIREN bit is 48237fdb2f5SManos Pitsidianakis /// set to 1 and the SIRTEST bit in the Test Control register, UARTTCR 48337fdb2f5SManos Pitsidianakis /// on page 4-5 is set to 1, then the nSIROUT path is inverted, and fed 48437fdb2f5SManos Pitsidianakis /// through to the SIRIN path. The SIRTEST bit in the test register must 48537fdb2f5SManos Pitsidianakis /// be set to 1 to override the normal half-duplex SIR operation. This 48637fdb2f5SManos Pitsidianakis /// must be the requirement for accessing the test registers during 48737fdb2f5SManos Pitsidianakis /// normal operation, and SIRTEST must be cleared to 0 when loopback 48837fdb2f5SManos Pitsidianakis /// testing is finished. This feature reduces the amount of external 48937fdb2f5SManos Pitsidianakis /// coupling required during system test. If this bit is set to 1, and 49037fdb2f5SManos Pitsidianakis /// the SIRTEST bit is set to 0, the UARTTXD path is fed through to the 49137fdb2f5SManos Pitsidianakis /// UARTRXD path. In either SIR mode or UART mode, when this bit is set, 49237fdb2f5SManos Pitsidianakis /// the modem outputs are also fed through to the modem inputs. This bit 49337fdb2f5SManos Pitsidianakis /// is cleared to 0 on reset, to disable loopback. 49437fdb2f5SManos Pitsidianakis pub enable_loopback: bool, 49537fdb2f5SManos Pitsidianakis /// `TXE` Transmit enable. If this bit is set to 1, the transmit section 49637fdb2f5SManos Pitsidianakis /// of the UART is enabled. Data transmission occurs for either UART 49737fdb2f5SManos Pitsidianakis /// signals, or SIR signals depending on the setting of the SIREN bit. 49837fdb2f5SManos Pitsidianakis /// When the UART is disabled in the middle of transmission, it 49937fdb2f5SManos Pitsidianakis /// completes the current character before stopping. 50037fdb2f5SManos Pitsidianakis pub enable_transmit: bool, 50137fdb2f5SManos Pitsidianakis /// `RXE` Receive enable. If this bit is set to 1, the receive section 50237fdb2f5SManos Pitsidianakis /// of the UART is enabled. Data reception occurs for either UART 50337fdb2f5SManos Pitsidianakis /// signals or SIR signals depending on the setting of the SIREN bit. 50437fdb2f5SManos Pitsidianakis /// When the UART is disabled in the middle of reception, it completes 50537fdb2f5SManos Pitsidianakis /// the current character before stopping. 50637fdb2f5SManos Pitsidianakis pub enable_receive: bool, 50737fdb2f5SManos Pitsidianakis /// `DTR` Data transmit ready. This bit is the complement of the UART 50837fdb2f5SManos Pitsidianakis /// data transmit ready, `nUARTDTR`, modem status output. That is, when 50937fdb2f5SManos Pitsidianakis /// the bit is programmed to a 1 then `nUARTDTR` is LOW. 51037fdb2f5SManos Pitsidianakis pub data_transmit_ready: bool, 51137fdb2f5SManos Pitsidianakis /// `RTS` Request to send. This bit is the complement of the UART 51237fdb2f5SManos Pitsidianakis /// request to send, `nUARTRTS`, modem status output. That is, when the 51337fdb2f5SManos Pitsidianakis /// bit is programmed to a 1 then `nUARTRTS` is LOW. 51437fdb2f5SManos Pitsidianakis pub request_to_send: bool, 51537fdb2f5SManos Pitsidianakis /// `Out1` This bit is the complement of the UART Out1 (`nUARTOut1`) 51637fdb2f5SManos Pitsidianakis /// modem status output. That is, when the bit is programmed to a 1 the 51737fdb2f5SManos Pitsidianakis /// output is 0. For DTE this can be used as Data Carrier Detect (DCD). 51837fdb2f5SManos Pitsidianakis pub out_1: bool, 51937fdb2f5SManos Pitsidianakis /// `Out2` This bit is the complement of the UART Out2 (`nUARTOut2`) 52037fdb2f5SManos Pitsidianakis /// modem status output. That is, when the bit is programmed to a 1, the 52137fdb2f5SManos Pitsidianakis /// output is 0. For DTE this can be used as Ring Indicator (RI). 52237fdb2f5SManos Pitsidianakis pub out_2: bool, 52337fdb2f5SManos Pitsidianakis /// `RTSEn` RTS hardware flow control enable. If this bit is set to 1, 52437fdb2f5SManos Pitsidianakis /// RTS hardware flow control is enabled. Data is only requested when 52537fdb2f5SManos Pitsidianakis /// there is space in the receive FIFO for it to be received. 52637fdb2f5SManos Pitsidianakis pub rts_hardware_flow_control_enable: bool, 52737fdb2f5SManos Pitsidianakis /// `CTSEn` CTS hardware flow control enable. If this bit is set to 1, 52837fdb2f5SManos Pitsidianakis /// CTS hardware flow control is enabled. Data is only transmitted when 52937fdb2f5SManos Pitsidianakis /// the `nUARTCTS` signal is asserted. 53037fdb2f5SManos Pitsidianakis pub cts_hardware_flow_control_enable: bool, 53137fdb2f5SManos Pitsidianakis } 53237fdb2f5SManos Pitsidianakis 53337fdb2f5SManos Pitsidianakis impl Control { 53437fdb2f5SManos Pitsidianakis pub fn reset(&mut self) { 53537fdb2f5SManos Pitsidianakis *self = 0.into(); 53637fdb2f5SManos Pitsidianakis self.set_enable_receive(true); 53737fdb2f5SManos Pitsidianakis self.set_enable_transmit(true); 53837fdb2f5SManos Pitsidianakis } 53937fdb2f5SManos Pitsidianakis } 54037fdb2f5SManos Pitsidianakis 54137fdb2f5SManos Pitsidianakis impl Default for Control { 54237fdb2f5SManos Pitsidianakis fn default() -> Self { 54337fdb2f5SManos Pitsidianakis let mut ret: Self = 0.into(); 54437fdb2f5SManos Pitsidianakis ret.reset(); 54537fdb2f5SManos Pitsidianakis ret 54637fdb2f5SManos Pitsidianakis } 54737fdb2f5SManos Pitsidianakis } 54837fdb2f5SManos Pitsidianakis 54937fdb2f5SManos Pitsidianakis /// Interrupt status bits in UARTRIS, UARTMIS, UARTIMSC 55037fdb2f5SManos Pitsidianakis pub const INT_OE: u32 = 1 << 10; 55137fdb2f5SManos Pitsidianakis pub const INT_BE: u32 = 1 << 9; 55237fdb2f5SManos Pitsidianakis pub const INT_PE: u32 = 1 << 8; 55337fdb2f5SManos Pitsidianakis pub const INT_FE: u32 = 1 << 7; 55437fdb2f5SManos Pitsidianakis pub const INT_RT: u32 = 1 << 6; 55537fdb2f5SManos Pitsidianakis pub const INT_TX: u32 = 1 << 5; 55637fdb2f5SManos Pitsidianakis pub const INT_RX: u32 = 1 << 4; 55737fdb2f5SManos Pitsidianakis pub const INT_DSR: u32 = 1 << 3; 55837fdb2f5SManos Pitsidianakis pub const INT_DCD: u32 = 1 << 2; 55937fdb2f5SManos Pitsidianakis pub const INT_CTS: u32 = 1 << 1; 56037fdb2f5SManos Pitsidianakis pub const INT_RI: u32 = 1 << 0; 56137fdb2f5SManos Pitsidianakis pub const INT_E: u32 = INT_OE | INT_BE | INT_PE | INT_FE; 56237fdb2f5SManos Pitsidianakis pub const INT_MS: u32 = INT_RI | INT_DSR | INT_DCD | INT_CTS; 56337fdb2f5SManos Pitsidianakis 56437fdb2f5SManos Pitsidianakis #[repr(u32)] 56537fdb2f5SManos Pitsidianakis pub enum Interrupt { 56637fdb2f5SManos Pitsidianakis OE = 1 << 10, 56737fdb2f5SManos Pitsidianakis BE = 1 << 9, 56837fdb2f5SManos Pitsidianakis PE = 1 << 8, 56937fdb2f5SManos Pitsidianakis FE = 1 << 7, 57037fdb2f5SManos Pitsidianakis RT = 1 << 6, 57137fdb2f5SManos Pitsidianakis TX = 1 << 5, 57237fdb2f5SManos Pitsidianakis RX = 1 << 4, 57337fdb2f5SManos Pitsidianakis DSR = 1 << 3, 57437fdb2f5SManos Pitsidianakis DCD = 1 << 2, 57537fdb2f5SManos Pitsidianakis CTS = 1 << 1, 57637fdb2f5SManos Pitsidianakis RI = 1 << 0, 57737fdb2f5SManos Pitsidianakis } 57837fdb2f5SManos Pitsidianakis 57937fdb2f5SManos Pitsidianakis impl Interrupt { 58037fdb2f5SManos Pitsidianakis pub const E: u32 = INT_OE | INT_BE | INT_PE | INT_FE; 58137fdb2f5SManos Pitsidianakis pub const MS: u32 = INT_RI | INT_DSR | INT_DCD | INT_CTS; 58237fdb2f5SManos Pitsidianakis } 58337fdb2f5SManos Pitsidianakis } 58437fdb2f5SManos Pitsidianakis 58537fdb2f5SManos Pitsidianakis // TODO: You must disable the UART before any of the control registers are 58637fdb2f5SManos Pitsidianakis // reprogrammed. When the UART is disabled in the middle of transmission or 58737fdb2f5SManos Pitsidianakis // reception, it completes the current character before stopping 588