xv6-65oo2/kernel/uart.c

76 lines
1.1 KiB
C
Raw Normal View History

#include "types.h"
#include "param.h"
2019-05-31 13:45:59 +00:00
#include "memlayout.h"
#include "riscv.h"
#include "spinlock.h"
#include "proc.h"
#include "defs.h"
2019-05-31 13:45:59 +00:00
//
// qemu -machine virt has a 16550a UART
// qemu/hw/riscv/virt.c
// http://byterunner.com/16550.html
//
// caller should lock.
//
2019-05-31 13:45:59 +00:00
// address of one of the registers
#define R(reg) ((volatile unsigned char *)(UART0 + reg))
void
uartinit(void)
{
// disable interrupts -- IER
2019-05-31 13:45:59 +00:00
*R(1) = 0x00;
2019-05-31 13:45:59 +00:00
// special mode to set baud rate
*R(3) = 0x80;
2019-05-31 13:45:59 +00:00
// LSB for baud rate of 38.4K
*R(0) = 0x03;
2019-05-31 13:45:59 +00:00
// MSB for baud rate of 38.4K
*R(1) = 0x00;
2019-05-31 13:45:59 +00:00
// leave set-baud mode,
// and set word length to 8 bits, no parity.
*R(3) = 0x03;
// reset and enable FIFOs -- FCR.
2019-05-31 13:45:59 +00:00
*R(2) = 0x07;
// enable receive interrupts -- IER.
*R(1) = 0x01;
}
void
uartputc(int c)
{
// wait for Transmit Holding Empty to be set in LSR.
while((*R(5) & (1 << 5)) == 0)
;
2019-05-31 13:45:59 +00:00
*R(0) = c;
}
2019-06-03 21:49:27 +00:00
int
uartgetc(void)
{
2019-06-03 21:59:17 +00:00
if(*R(5) & 0x01){
2019-06-03 21:49:27 +00:00
// input data is ready.
return *R(0);
} else {
return -1;
2019-06-03 21:59:17 +00:00
}
}
void
uartintr(void)
{
2019-06-03 21:59:17 +00:00
while(1){
int c = uartgetc();
if(c == -1)
break;
consoleintr(c);
}
}