matos/drivers/serial.c

56 lines
1.3 KiB
C
Raw Normal View History

2018-11-08 21:11:45 +01:00
#include "serial.h"
#include "io.h"
#include "irq.h"
#define PORT 0x3f8 /* COM1 */
#define SERIAL_MAX_SPEED 115200
/* The type of parity. */
#define UART_NO_PARITY 0x00
#define UART_ODD_PARITY 0x08
#define UART_EVEN_PARITY 0x18
/* The type of word length. */
#define UART_5BITS_WORD 0x00
#define UART_6BITS_WORD 0x01
#define UART_7BITS_WORD 0x02
#define UART_8BITS_WORD 0x03
/* The type of the length of stop bit. */
#define UART_1_STOP_BIT 0x00
#define UART_2_STOP_BITS 0x04
2018-11-08 22:08:27 +01:00
void serialSetup(int speed)
2018-11-08 21:11:45 +01:00
{
unsigned short div = SERIAL_MAX_SPEED / speed;
outb(PORT + 1, 0x00); // Disable all interrupts
outb(PORT + 3, 0x80); // Enable DLAB (set baud rate divisor)
outb(PORT + 0, div & 0xFF); // Set divisor lo byte
outb(PORT + 1, div >> 8); // Set divisor hi byte
outb(PORT + 3,
UART_NO_PARITY | UART_8BITS_WORD | UART_1_STOP_BIT); // 8 bits, no parity, one stop bit
outb(PORT + 2, 0xC7); // Enable FIFO, clear them, with 14-byte threshold
outb(PORT + 4, 0x0B); // IRQs enabled, RTS/DSR set
irqSetRoutine(IRQ_COM1, serial_handler);
2018-11-08 21:11:45 +01:00
}
int isTransmitEmpty()
{
return (inb(PORT + 5) & 0x20);
2018-11-08 21:11:45 +01:00
}
2019-05-17 09:57:14 +02:00
void serialPutc(char a)
2018-11-08 21:11:45 +01:00
{
while (isTransmitEmpty() == 0)
;
outb(PORT, a);
}
2018-11-08 22:08:27 +01:00
void serialDoIrq(struct interrupt_frame *level)
{
(void)level;
char c = inb(PORT);
2019-05-17 09:57:14 +02:00
serialPutc(c);
2018-11-08 21:11:45 +01:00
}