UART

  • Serial Communication Protocol & Hardware..
  • UART로 키보드 입력 받음

PL011 Register Summary

PL011 UART

OffsetNameTypeResetWidthDescription
0x000UARTDRRW0x---12/8Data Register, UARTDR
0x004UARTRSR/ UARTECRRW0x04/0Receive Status Register / Error Clear Register, UARTRSR/UARTECR
0x008-0x014----Reserved
0x018UARTFRRO0b-10010---9Flag Register, UARTFR
0x01C----Reserved
0x020UARTILPRRW0x008IrDA Low-Power Counter Register, UARTILPR
0x024UARTIBRDRW0x000016Integer Baud Rate Register, UARTIBRD
0x028UARTFBRDRW0x006Fractional Baud Rate Register, UARTFBRD
0x02CUARTLCR_HRW0x008Line Control Register, UARTLCR_H
0x030UARTCRRW0x030016Control Register, UARTCR
0x034UARTIFLSRW0x126Interrupt FIFO Level Select Register, UARTIFLS
0x038UARTIMSCRW0x00011Interrupt Mask Set/Clear Register, UARTIMSC
0x03CUARTRISRO0x00-11Raw Interrupt Status Register, UARTRIS
0x040UARTMISRO0x00-11Masked Interrupt Status Register, UARTMIS
0x044UARTICRWO-11Interrupt Clear Register, UARTICR
0x048UARTDMACRRW0x003DMA Control Register, UARTDMACR
0x04C-0x07C----Reserved
0x080-0x08C----Reserved for test purposes
0x090-0xFCC----Reserved
0xFD0-0xFDC----Reserved for future ID expansion
0xFE0UARTPeriphID0RO0x118UARTPeriphID0 Register
0xFE4UARTPeriphID1RO0x108UARTPeriphID1 Register
0xFE8UARTPeriphID2RO0x_4[1]8UARTPeriphID2 Register
0xFECUARTPeriphID3RO0x008UARTPeriphID3 Register
0xFF0UARTPCellID0RO0x0D8UARTPCellID0 Register
0xFF4UARTPCellID1RO0xF08UARTPCellID1 Register
0xFF8UARTPCellID2RO0x058UARTPCellID2 Register
0xFFCUARTPCellID3RO0xB18UARTPCellID3 Register

Reference

https://krinkinmu.github.io/2020/11/29/PL011.html

Link to original

Registers

REGISTERS

Interrupt Registers

Interrupts

Interrupt Registers

UARTICR: Interrupt Clear Register

BitsNameFunction
15:11ReservedReserved, read as zero, do not modify.
10OEICOverrun error interrupt clear. Clears the UARTOEINTR interrupt.
9BEICBreak error interrupt clear. Clears the UARTBEINTR interrupt.
8PEICParity error interrupt clear. Clears the UARTPEINTR interrupt.
7FEICFraming error interrupt clear. Clears the UARTFEINTR interrupt.
6RTICReceive timeout interrupt clear. Clears the UARTRTINTR interrupt.
5TXICTransmit interrupt clear. Clears the UARTTXINTR interrupt.
4RXICReceive interrupt clear. Clears the UARTRXINTR interrupt.
3DSRMICnUARTDSR modem interrupt clear. Clears the UARTDSRINTR interrupt.
2DCDMICnUARTDCD modem interrupt clear. Clears the UARTDCDINTR interrupt.
1CTSMICnUARTCTS modem interrupt clear. Clears the UARTCTSINTR interrupt.
0RIMICnUARTRI modem interrupt clear. Clears the UARTRIINTR interrupt.

UARTIFLS: Interrupt FIFO Level Select Register

BitsNameFunction
15:6-Reserved, do not modify, read as zero.
5:3RXIFLSELReceive interrupt FIFO level select. The trigger points for the receive interrupt are as follows:

b000 = Receive FIFO becomes ≥ 1/8 full

b001 = Receive FIFO becomes ≥ 1/4 full

b010 = Receive FIFO becomes ≥ 1/2 full

b011 = Receive FIFO becomes ≥ 3/4 full

b100 = Receive FIFO becomes ≥ 7/8 full

b101-b111 = reserved.
2:0TXIFLSELTransmit interrupt FIFO level select. The trigger points for the transmit interrupt are as follows:

b000 = Transmit FIFO becomes ≤ 1/8 full

b001 = Transmit FIFO becomes ≤ 1/4 full

b010 = Transmit FIFO becomes ≤ 1/2 full

b011 = Transmit FIFO becomes ≤ 3/4 full

b100 = Transmit FIFO becomes ≤ 7/8 full

b101-b111 = reserved.

UARTIMSC: Interrupt Mask.Set.Clear Register

BitsNameFunction
15:11-Reserved, read as zero, do not modify.
10OEIMOverrun error interrupt mask. A read returns the current mask for the UARTOEINTRinterrupt.

On a write of 1, the mask of the UARTOEINTR interrupt is set. A write of 0 clears the mask.
9BEIMBreak error interrupt mask. A read returns the current mask for the UARTBEINTRinterrupt.

On a write of 1, the mask of the UARTBEINTR interrupt is set. A write of 0 clears the mask.
8PEIMParity error interrupt mask. A read returns the current mask for the UARTPEINTRinterrupt.

On a write of 1, the mask of the UARTPEINTR interrupt is set. A write of 0 clears the mask.
7FEIMFraming error interrupt mask. A read returns the current mask for the UARTFEINTRinterrupt.

On a write of 1, the mask of the UARTFEINTR interrupt is set. A write of 0 clears the mask.
6RTIMReceive timeout interrupt mask. A read returns the current mask for the UARTRTINTRinterrupt.

On a write of 1, the mask of the UARTRTINTR interrupt is set. A write of 0 clears the mask.
5TXIMTransmit interrupt mask. A read returns the current mask for the UARTTXINTRinterrupt.

On a write of 1, the mask of the UARTTXINTR interrupt is set. A write of 0 clears the mask.
4RXIMReceive interrupt mask. A read returns the current mask for the UARTRXINTRinterrupt.

On a write of 1, the mask of the UARTRXINTR interrupt is set. A write of 0 clears the mask.
3DSRMIMnUARTDSR modem interrupt mask. A read returns the current mask for the UARTDSRINTR interrupt.

On a write of 1, the mask of the UARTDSRINTR interrupt is set. A write of 0 clears the mask.
2DCDMIMnUARTDCD modem interrupt mask. A read returns the current mask for the UARTDCDINTR interrupt.

On a write of 1, the mask of the UARTDCDINTR interrupt is set. A write of 0 clears the mask.
1CTSMIMnUARTCTS modem interrupt mask. A read returns the current mask for the UARTCTSINTR interrupt.

On a write of 1, the mask of the UARTCTSINTR interrupt is set. A write of 0 clears the mask.
0RIMIMnUARTRI modem interrupt mask. A read returns the current mask for the UARTRIINTR interrupt.

On a write of 1, the mask of the UARTRIINTR interrupt is set. A write of 0 clears the mask.

UARTMIS: Masked Interrupt Status Register

BitsNameFunction
15:11-Reserved, read as zero, do not modify
10OEMISOverrun error masked interrupt status. Returns the masked interrupt state of the UARTOEINTR interrupt.
9BEMISBreak error masked interrupt status. Returns the masked interrupt state of the UARTBEINTR interrupt.
8PEMISParity error masked interrupt status. Returns the masked interrupt state of the UARTPEINTR interrupt.
7FEMISFraming error masked interrupt status. Returns the masked interrupt state of the UARTFEINTR interrupt.
6RTMISReceive timeout masked interrupt status. Returns the masked interrupt state of the UARTRTINTR interrupt.
5TXMISTransmit masked interrupt status. Returns the masked interrupt state of the UARTTXINTR interrupt.
4RXMISReceive masked interrupt status. Returns the masked interrupt state of the UARTRXINTR interrupt.
3DSRMMISnUARTDSR modem masked interrupt status. Returns the masked interrupt state of the UARTDSRINTR interrupt.
2DCDMMISnUARTDCD modem masked interrupt status. Returns the masked interrupt state of the UARTDCDINTR interrupt.
1CTSMMISnUARTCTS modem masked interrupt status. Returns the masked interrupt state of the UARTCTSINTR interrupt.
0RIMMISnUARTRI modem masked interrupt status. Returns the masked interrupt state of the UARTRIINTR interrupt.
Link to original

Implementation

Initialization

fn init(&mut self) {
	// Turn Off UART Temporarily
	self.registers.CR.set(0); 
	// Clear all interrupts
	self.registers.ICR.write(ICR::ALL::CLEAR); 
	// Set Baud Rate
	self.registers.IBRD.write(IBRD::BAUD_DIVINT.val(3));
	self.registers.FBRD.write(FBRD::BAUD_DIVINT.val(3));
	// Set DataFrame as 8bit + FIFO mode
	self.registers.LCR_H.write(LCR_H::WLEN::EightBit + LCR_H::FEN::FifosEnabled);
	self.registers.IFLS.write(IFLS::RXIFLSEL::OneEights);
	// Turn On Interrupt
	self.registers.IMSC.write(IMSC::RXIM::Enabled + IMSC::RTIM::Enabled);
	// Turn On UART
	self.registers.CR.write(CR::UARTEN::Enabled + CR::TXE::Enable + CR::RXE::Enabled);
}

Write Char to Console

fn _write_char(&mut self, c: char) {
	self._flush();
	self.registers.DR.set(c as u32);
}

Read Char from Keyboard

fn _read_char(&mut self, nonblocking: bool) {
	if nonblocking && self.registers.FR.matches_all(FR::RXFD::SET) {
		return None;
	}
	while self.registers.FR.matches_all(FR::RXFE::SET) {
		asm::nop();
	}
	let mut c = self.registers.DR.get() as u8 as char;
	if c == '\r' {
		c = '\n';
	}
	Some(c)
}

Flush

fn _flush(&self) {
	while self.registers.FR.matches_all(FR::BUSY::SET) {
		asm::nop();
	}
}