1 /*
2  * Copyright (c) 2006-2021, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2006-09-06     XuXinming    first version
9  * 2006-09-20     Bernard      clean code according code style
10  */
11 
12 #include <rtthread.h>
13 #include <rthw.h>
14 
15 #include "s3c44b0.h"
16 
17 void rt_serial_init(void);
18 void rt_console_puts(const char* str);
19 void rt_serial_putc(const char c);
20 
21 #define USTAT_RCV_READY     0x01    /* receive data ready */
22 #define USTAT_TXB_EMPTY     0x02    /* tx buffer empty */
23 
serial_flush_input(void)24 rt_inline void serial_flush_input(void)
25 {
26     volatile unsigned int tmp;
27 
28     /* keep on reading as long as the receiver is not empty */
29     while(UTRSTAT0 & USTAT_RCV_READY) tmp = URXH0;
30 }
31 
serial_flush_output(void)32 rt_inline void serial_flush_output(void)
33 {
34     /* wait until the transmitter is no longer busy */
35     while(!(UTRSTAT0 & USTAT_TXB_EMPTY)) ;
36 }
37 
38 /**
39  * @addtogroup S3C44B0
40  */
41 /*@{*/
42 
43 /**
44  * This function is used to display a string on console, normally, it's
45  * invoked by rt_kprintf
46  *
47  * @param str the displayed string
48  */
rt_console_puts(const char * str)49 void rt_console_puts(const char* str)
50 {
51     while (*str)
52     {
53         rt_serial_putc (*str++);
54     }
55 }
56 
57 /**
58  * This function initializes serial
59  */
rt_serial_init()60 void rt_serial_init()
61 {
62     rt_uint32_t divisor = 0;
63 
64     divisor = 0x20;
65 
66     serial_flush_output();
67     serial_flush_input();
68 
69     /* UART interrupt off */
70     UCON0   = 0;
71     /* FIFO disable */
72     UFCON0  =0x0;
73     UMCON0  =0x0;
74     /* set baudrate */
75     UBRDIV0 = divisor;
76 
77     /* word length=8bit, stop bit = 1, no parity, use external clock */
78     ULCON0  = 0x03;
79 
80     UCON0   = 0x5;
81 }
82 
83 /**
84  * This function read a character from serial without interrupt enable mode
85  *
86  * @return the read char
87  */
rt_serial_getc()88 char rt_serial_getc()
89 {
90     while ((UTRSTAT0 & USTAT_RCV_READY) == 0);
91 
92     return URXH0;
93 }
94 
95 /**
96  * This function will write a character to serial without interrupt enable mode
97  *
98  * @param c the char to write
99  */
rt_serial_putc(const char c)100 void rt_serial_putc(const char c)
101 {
102     /*
103         to be polite with serial console add a line feed
104         to the carriage return character
105     */
106     if (c=='\n')rt_serial_putc('\r');
107 
108     /* wait for room in the transmit FIFO */
109     while(!(UTRSTAT0 & USTAT_TXB_EMPTY));
110 
111     UTXH0 = (rt_uint8_t)c;
112 }
113 
114 /*@}*/
115