serial.c 1.3 KB
Newer Older
M
Ming, Bai 已提交
1
/*
2
 * Copyright (c) 2006-2018, RT-Thread Development Team
M
Ming, Bai 已提交
3
 *
4
 * SPDX-License-Identifier: Apache-2.0
M
Ming, Bai 已提交
5 6 7 8
 *
 * Change Logs:
 * Date           Author       Notes
 * 2006-09-15     QiuYi        the first version
9
 * 2006-10-10     Bernard      use keyboard instead of serial
M
Ming, Bai 已提交
10
 */
11

M
Ming, Bai 已提交
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
#include <rtthread.h>
#include <rthw.h>

#include <bsp.h>

/**
 * @addtogroup QEMU
 */
/*@{*/

/**
 * This function initializes serial
 */
void rt_serial_init(void)
{
27 28 29 30 31 32 33
    outb(COM1+3,0x80);	/* set DLAB of line control reg */
    outb(COM1,0x0c);	/* LS of divisor (48 -> 2400 bps */
    outb(COM1+1,0x00);	/* MS of divisor */
    outb(COM1+3,0x03);	/* reset DLAB */
    outb(COM1+4,0x0b);	/* set DTR,RTS, OUT_2 */
    outb(COM1+1,0x0d);	/* enable all intrs but writes */
    inb(COM1);			/* read data port to reset things (?) */
M
Ming, Bai 已提交
34 35 36 37 38 39 40 41 42
}

/**
 * This function read a character from serial without interrupt enable mode 
 *
 * @return the read char
 */
char rt_serial_getc(void)
{
43
    while(!(inb(COM1+COMSTATUS) & COMDATA));
M
Ming, Bai 已提交
44

45
    return inb(COM1+COMREAD);
M
Ming, Bai 已提交
46 47 48 49 50 51 52 53 54
}

/**
 * This function will write a character to serial without interrupt enable mode
 *
 * @param c the char to write
 */
void rt_serial_putc(const char c)
{
55 56 57 58 59 60 61 62 63
    int val;
    
    while(1)
    {
           if ((val = inb(COM1+COMSTATUS)) & THRE) 
            break;
    }
    
    outb(COM1+COMWRITE, c&0xff);
M
Ming, Bai 已提交
64 65 66
}

/*@}*/