remove COM

This commit is contained in:
trimill 2024-01-27 03:03:24 -05:00
parent 481b4e303a
commit 7bbdccc05c
No known key found for this signature in database
GPG key ID: 4F77A16E17E10BCB
2 changed files with 24 additions and 42 deletions

View file

@ -2,14 +2,7 @@
#include <stdint.h> #include <stdint.h>
enum serial_com_port { int serial_init(void);
COM1 = 1, uint8_t serial_in(void);
COM2 = 2, void serial_out(uint8_t ch);
COM3 = 3, void serial_out_str(char *str);
COM4 = 4,
};
int serial_init(enum serial_com_port com);
uint8_t serial_in(enum serial_com_port com);
void serial_out(enum serial_com_port com, uint8_t ch);
void serial_out_str(enum serial_com_port com, char *str);

View file

@ -1,58 +1,47 @@
#include <serial.h> #include <serial.h>
#include "bindings.h" #include "bindings.h"
// port numbers for first four serial ports #define PORT 0x3F8
static uint16_t SERIAL_PORTS[] = {
[COM1] = 0x3F8,
[COM2] = 0x2F8,
[COM3] = 0x3E8,
[COM4] = 0x2E8,
};
// initialize the specified COM port for serial // initialize the specified COM port for serial
// see https://wiki.osdev.org/Serial_Ports // see https://wiki.osdev.org/Serial_Ports
// and https://en.wikibooks.org/wiki/Serial_Programming/8250_UART_Programming // and https://en.wikibooks.org/wiki/Serial_Programming/8250_UART_Programming
int serial_init(enum serial_com_port com) { int serial_init(void) {
uint16_t port = SERIAL_PORTS[com]; outb(PORT + 1, 0x00); // disable interrupts
outb(port + 1, 0x00); // disable interrupts outb(PORT + 3, 0x80); // enable DLAB (divisor latch access bit)
outb(port + 3, 0x80); // enable DLAB (divisor latch access bit) outb(PORT + 0, 0x03); // (lo) Set baud divisor to 3 38400 baud
outb(port + 0, 0x03); // (lo) Set baud divisor to 3 38400 baud outb(PORT + 1, 0x00); // (hi)
outb(port + 1, 0x00); // (hi) outb(PORT + 3, 0x03); // disable DLAB, set 8 bits per word, one stop bit, no parity
outb(port + 3, 0x03); // disable DLAB, set 8 bits per word, one stop bit, no parity outb(PORT + 2, 0xC7); // enable and clear FIFOs, set to maximum threshold
outb(port + 2, 0xC7); // enable and clear FIFOs, set to maximum threshold
// outb(port + 4, 0x0B); // TODO copied this from osdev wiki but i don't think you need it here // outb(port + 4, 0x0B); // TODO copied this from osdev wiki but i don't think you need it here
outb(port + 4, 0x1E); // set in loopback mode for test outb(PORT + 4, 0x1E); // set in loopback mode for test
outb(port + 0, 0xAE); // test by sending 0xAE outb(PORT + 0, 0xAE); // test by sending 0xAE
uint8_t response = inb(port + 0); uint8_t response = inb(PORT + 0);
if(response != 0xAE) { if(response != 0xAE) {
// TODO panic here? // TODO panic here?
return -1; return -1;
} }
// disable loopback, enable IRQs // disable loopback, enable IRQs
outb(port + 4, 0x0F); outb(PORT + 4, 0x0F);
return 0; return 0;
} }
uint8_t serial_in(enum serial_com_port com) { uint8_t serial_in(void) {
uint16_t port = SERIAL_PORTS[com];
// wait for data to be available // wait for data to be available
while((inb(port + 5) & 0x01) == 0); while((inb(PORT + 5) & 0x01) == 0);
return inb(port); return inb(PORT);
} }
void serial_out(enum serial_com_port com, uint8_t ch) { void serial_out(uint8_t ch) {
uint16_t port = SERIAL_PORTS[com];
// wait for output to be free // wait for output to be free
while((inb(port + 5) & 0x20) == 0); while((inb(PORT + 5) & 0x20) == 0);
outb(port, ch); outb(PORT, ch);
} }
void serial_out_str(enum serial_com_port com, char *str) { void serial_out_str(char *str) {
uint16_t port = SERIAL_PORTS[com];
for(; *str != '\0'; str++) { for(; *str != '\0'; str++) {
while((inb(port + 5) & 0x20) == 0); serial_out(*str);
outb(port, *str);
} }
} }