defines for UART register bits

This commit is contained in:
Robert Morris 2020-07-23 06:27:20 -04:00 committed by Frans Kaashoek
parent 0f50e9527c
commit 2ae9c8e272

View file

@ -19,13 +19,21 @@
// some have different meanings for // some have different meanings for
// read vs write. // read vs write.
// see http://byterunner.com/16550.html // see http://byterunner.com/16550.html
#define RHR 0 // receive holding register (for input bytes) #define RHR 0 // receive holding register (for input bytes)
#define THR 0 // transmit holding register (for output bytes) #define THR 0 // transmit holding register (for output bytes)
#define IER 1 // interrupt enable register #define IER 1 // interrupt enable register
#define FCR 2 // FIFO control register #define IER_TX_ENABLE (1<<0)
#define ISR 2 // interrupt status register #define IER_RX_ENABLE (1<<1)
#define LCR 3 // line control register #define FCR 2 // FIFO control register
#define LSR 5 // line status register #define FCR_FIFO_ENABLE (1<<0)
#define FCR_FIFO_CLEAR (3<<1) // clear the content of the two FIFOs
#define ISR 2 // interrupt status register
#define LCR 3 // line control register
#define LCR_EIGHT_BITS (3<<0)
#define LCR_BAUD_LATCH (1<<7) // special mode to set baud rate
#define LSR 5 // line status register
#define LSR_RX_READY (1<<0) // input is waiting to be read from RHR
#define LSR_TX_IDLE (1<<5) // THR can accept another character to send
#define ReadReg(reg) (*(Reg(reg))) #define ReadReg(reg) (*(Reg(reg)))
#define WriteReg(reg, v) (*(Reg(reg)) = (v)) #define WriteReg(reg, v) (*(Reg(reg)) = (v))
@ -46,7 +54,7 @@ uartinit(void)
WriteReg(IER, 0x00); WriteReg(IER, 0x00);
// special mode to set baud rate. // special mode to set baud rate.
WriteReg(LCR, 0x80); WriteReg(LCR, LCR_BAUD_LATCH);
// LSB for baud rate of 38.4K. // LSB for baud rate of 38.4K.
WriteReg(0, 0x03); WriteReg(0, 0x03);
@ -56,13 +64,13 @@ uartinit(void)
// leave set-baud mode, // leave set-baud mode,
// and set word length to 8 bits, no parity. // and set word length to 8 bits, no parity.
WriteReg(LCR, 0x03); WriteReg(LCR, LCR_EIGHT_BITS);
// reset and enable FIFOs. // reset and enable FIFOs.
WriteReg(FCR, 0x07); WriteReg(FCR, FCR_FIFO_ENABLE | FCR_FIFO_CLEAR);
// enable transmit and receive interrupts. // enable transmit and receive interrupts.
WriteReg(IER, 0x02 | 0x01); WriteReg(IER, IER_TX_ENABLE | IER_RX_ENABLE);
initlock(&uart_tx_lock, "uart"); initlock(&uart_tx_lock, "uart");
} }
@ -102,7 +110,7 @@ uartputc_sync(int c)
push_off(); push_off();
// wait for Transmit Holding Empty to be set in LSR. // wait for Transmit Holding Empty to be set in LSR.
while((ReadReg(LSR) & (1 << 5)) == 0) while((ReadReg(LSR) & LSR_TX_IDLE) == 0)
; ;
WriteReg(THR, c); WriteReg(THR, c);
@ -122,7 +130,7 @@ uartstart()
return; return;
} }
if((ReadReg(LSR) & (1 << 5)) == 0){ if((ReadReg(LSR) & LSR_TX_IDLE) == 0){
// the UART transmit holding register is full, // the UART transmit holding register is full,
// so we cannot give it another byte. // so we cannot give it another byte.
// it will interrupt when it's ready for a new byte. // it will interrupt when it's ready for a new byte.