// CC BY // code by SASANO Takayoshi #include "io.h" #define COM_PORT 0x3f8 // COM1 #define COM_CLOCK 1843200 // Hz #define COM_SPEED 115200 #define COM_DIVISOR (COM_CLOCK / (COM_SPEED * 16)) #define RHR (COM_PORT + 0x00) // r #define THR (COM_PORT + 0x00) // w #define IER (COM_PORT + 0x01) // rw #define ISR (COM_PORT + 0x02) // r #define FCR (COM_PORT + 0x02) // w #define LCR (COM_PORT + 0x03) // rw #define MCR (COM_PORT + 0x04) // rw #define LSR (COM_PORT + 0x05) // r #define DLL (COM_PORT + 0x00) // rw #define DLM (COM_PORT + 0x01) // rw void serial_out(char c) { while (!(in_b(LSR) & 0x20)); // wait for Tx ready out_b(THR, c); } char serial_in(void) { unsigned char s, c; while (1) { s = in_b(LSR); // get status if (s & 0x1f) { // Rx ready c = in_b(RHR); // read from FIFO if (!(s & 0x1e)) { // no error return c; } } } /* NOTREACHED */ } void serial_init(void) { out_b(IER, 0x00); // disable interrupt out_b(LCR, 0x80); // set divisor out_b(DLM, 0xff); // (avoid divisor = 0) out_b(DLL, (COM_DIVISOR >> 0) & 0xff); out_b(DLM, (COM_DIVISOR >> 8) & 0xff); out_b(LCR, 0x03); // data 8bit, stop 1bit, non-parity out_b(MCR, 0x03); // RTS#, DTR# assert out_b(FCR, 0x07); // FIFO enable and reset }