serial.c 2.3 KB

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