Clean up serial driver

This commit is contained in:
pjht 2020-07-23 10:28:38 -05:00
parent f01785d69e
commit b506923396

View File

@ -1,117 +1,38 @@
#include "../isr.h"
#include "../serial.h"
#include <cpu/ports.h>
#include <stdint.h>
#include <string.h>
#define SERIAL_LINE_ENABLE_DLAB 0x80
static char configured[]={0,0,0,0};
static int data_ports[4]={0x3f8,0x2f8,0x3e8,0x2e8};
static int data_port(int com) {
switch (com) {
case 0: return 0x3f8;
case 1: return 0x2f8;
case 2: return 0x3e8;
case 3: return 0x2e8;
}
return 0;
}
#define data_port(com) (data_ports[com])
#define int_port(com) (data_port(com)+1)
#define fifo_port(com) (data_port(com)+2)
#define line_cmd_port(com) (data_port(com)+3)
#define modem_cmd_port(com) (data_port(com)+4)
#define line_stat_port(com) (data_port(com)+5)
#define scratch_port(com) (data_port(com)+7)
#define is_transmit_fifo_empty(com) (port_byte_in(line_stat_port(com))&0x20)
static int int_port(int com) {
switch (com) {
case 0: return 0x3f9;
case 1: return 0x2f9;
case 2: return 0x3e9;
case 3: return 0x2e9;
}
return 0;
static void configure(int com, int rate) {
configured[com]=1;
port_byte_out(line_cmd_port(com),0x80); // Enable DLAB
port_byte_out(data_port(com),((115200/rate)>>8)&0xFF); //Write high byte of divisor
port_byte_out(data_port(com),(115200/rate)&0xFF); //Write low byte of divisor
port_byte_out(line_cmd_port(com),0x03); //Disable DLAB and set 8N1 trasmission mode
port_byte_out(fifo_port(com),0xC7); //Enable & clear FIFOs and set Data Ready interrupt level to 14.
port_byte_out(modem_cmd_port(com),0x03); //Enable DTR and RTS
port_byte_out(int_port(com),0x0); //Disable interrupts
}
static int fifo_port(int com) {
switch (com) {
case 0: return 0x3fa;
case 1: return 0x2fa;
case 2: return 0x3ea;
case 3: return 0x2ea;
}
return 0;
}
static int line_cmd_port(int com) {
switch (com) {
case 0: return 0x3fb;
case 1: return 0x2fb;
case 2: return 0x3eb;
case 3: return 0x2eb;
}
return 0;
}
static int modem_port(int com) {
switch (com) {
case 0: return 0x3fc;
case 1: return 0x2fc;
case 2: return 0x3ec;
case 3: return 0x2ec;
}
return 0;
}
static int line_stat_port(int com) {
switch (com) {
case 0: return 0x3fd;
case 1: return 0x2fd;
case 2: return 0x3ed;
case 3: return 0x2ed;
}
return 0;
}
static int scratch_port(int com) {
switch (com) {
case 0: return 0x3ff;
case 1: return 0x2ff;
case 2: return 0x3ef;
case 3: return 0x2ef;
}
return 0;
}
static void configure_baud_rate(uint32_t divisor,int com) {
port_byte_out(line_cmd_port(com),SERIAL_LINE_ENABLE_DLAB);
port_byte_out(data_port(com),(divisor>>8)&0xFF);
port_byte_out(data_port(com),divisor&0xFF);
}
static int is_transmit_fifo_empty(int com) {
return port_byte_in(line_stat_port(com))&0x20;
}
static void configure(uint32_t com, uint32_t rate) {
configure_baud_rate(115200/rate,com);
port_byte_out(line_cmd_port(com),0x03);
port_byte_out(fifo_port(com),0xC7);
port_byte_out(modem_port(com),0x03);
port_byte_out(int_port(com),0x01);
}
void serial_init() {
port_byte_out(scratch_port(0),0xaa);
if (port_byte_in(scratch_port(0))==0xaa) {
configure(0,9600);
configured[0]=1;
}
}
void serial_putc(char c) {
if (!configured[0]) return;
if (c=='\n') {
while (!is_transmit_fifo_empty(0)) continue;
port_byte_out(data_port(0),'\r');
while (!is_transmit_fifo_empty(0)) continue;
port_byte_out(data_port(0),'\n');
} else {
while (!is_transmit_fifo_empty(0)) continue;
port_byte_out(data_port(0),c);
}
if (c=='\n') serial_putc('\r');
while(!is_transmit_fifo_empty(0));
port_byte_out(data_port(0),c);
}