| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778 |
- /* Serial (UART) driver: COM1 at 0x3F8, 115200 8N1, polled. */
- #include "serial.h"
- #include "timer.h"
- #define COM1_BASE 0x3F8
- #define RBR 0 /* Receive Buffer (DLAB=0) */
- #define THR 0 /* Transmit Holding (DLAB=0) */
- #define DLL 0 /* Divisor Latch Low (DLAB=1) */
- #define IER 1 /* Interrupt Enable */
- #define DLH 1 /* Divisor Latch High (DLAB=1) */
- #define LCR 3 /* Line Control */
- #define LSR 5 /* Line Status */
- #define LCR_8N1 0x03
- #define LCR_DLAB 0x80
- #define LSR_DR 0x01 /* Data Ready */
- #define LSR_THRE 0x20 /* Transmit Holding Register Empty */
- static inline unsigned char inb(unsigned short port)
- {
- unsigned char v;
- __asm__ volatile ("inb %1, %0" : "=a"(v) : "Nd"(port));
- return v;
- }
- static inline void outb(unsigned short port, unsigned char v)
- {
- __asm__ volatile ("outb %0, %1" : : "a"(v), "Nd"(port));
- }
- void serial_init(void)
- {
- /* Divisor for 115200: 115200 / 115200 = 1. Use 1. */
- outb(COM1_BASE + LCR, LCR_DLAB);
- outb(COM1_BASE + DLL, 1);
- outb(COM1_BASE + DLH, 0);
- outb(COM1_BASE + LCR, LCR_8N1);
- outb(COM1_BASE + IER, 0); /* No interrupts */
- }
- void serial_drain(void)
- {
- while (serial_getchar(0) >= 0)
- ;
- }
- void serial_putchar(char c)
- {
- unsigned int t0 = get_ticks_ms();
- while ((get_ticks_ms() - t0) < 100) {
- if (inb(COM1_BASE + LSR) & LSR_THRE) {
- outb(COM1_BASE + THR, (unsigned char)c);
- return;
- }
- }
- }
- void serial_putstr(const char *s)
- {
- while (*s)
- serial_putchar(*s++);
- }
- int serial_getchar(unsigned int timeout_ms)
- {
- unsigned int t0 = get_ticks_ms();
- while ((get_ticks_ms() - t0) < timeout_ms) {
- if (inb(COM1_BASE + LSR) & LSR_DR)
- return (unsigned char)inb(COM1_BASE + RBR);
- }
- return -1;
- }
- int serial_has_byte(void)
- {
- return (inb(COM1_BASE + LSR) & LSR_DR) ? 1 : 0;
- }
|