fh_uart.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  1. /*
  2. * This file is part of FH8620 BSP for RT-Thread distribution.
  3. *
  4. * Copyright (c) 2016 Shanghai Fullhan Microelectronics Co., Ltd.
  5. * All rights reserved
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along
  18. * with this program; if not, write to the Free Software Foundation, Inc.,
  19. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  20. *
  21. * Visit http://www.fullhan.com to get contact with Fullhan.
  22. *
  23. * Change Logs:
  24. * Date Author Notes
  25. */
  26. /*****************************************************************************
  27. * Include Section
  28. * add all #include here
  29. *****************************************************************************/
  30. #include "inc/fh_driverlib.h"
  31. /*****************************************************************************
  32. * Define section
  33. * add all #define here
  34. *****************************************************************************/
  35. /****************************************************************************
  36. * ADT section
  37. * add definition of user defined Data Type that only be used in this file here
  38. ***************************************************************************/
  39. /******************************************************************************
  40. * Function prototype section
  41. * add prototypes for all functions called by this file,execepting those
  42. * declared in header file
  43. *****************************************************************************/
  44. /*****************************************************************************
  45. * Global variables section - Exported
  46. * add declaration of global variables that will be exported here
  47. * e.g.
  48. * int8_t foo;
  49. ****************************************************************************/
  50. /*****************************************************************************
  51. * Global variables section - Local
  52. * define global variables(will be refered only in this file) here,
  53. * static keyword should be used to limit scope of local variable to this file
  54. * e.g.
  55. * static uint8_t ufoo;
  56. *****************************************************************************/
  57. /* function body */
  58. /*****************************************************************************
  59. * Description:
  60. * add funtion description here
  61. * Parameters:
  62. * description for each argument, new argument starts at new line
  63. * Return:
  64. * what does this function returned?
  65. *****************************************************************************/
  66. int uart_init(uart *port)
  67. {
  68. port->UART_IER = 0;
  69. port->UART_LCR = 0;
  70. //port->UART_DLL = 0;
  71. //port->UART_DLH = 0;
  72. }
  73. UINT32 uart_get_status(uart *port)
  74. {
  75. return port->UART_USR;
  76. }
  77. void uart_configure(uart *port, enum data_bits data_bit,
  78. enum stop_bits stop_bit, enum parity parity,
  79. UINT32 buard_rate, UINT32 uart_clk)
  80. {
  81. UINT32 divisor;
  82. UINT32 freq;
  83. UINT32 baud_div;
  84. UINT32 lcr_reg = 0;
  85. UINT32 ret;
  86. /*divisor = DIV(buard_rate);
  87. port->UART_LCR |= UART_LCR_DLAB;
  88. port->UART_DLL = divisor & 0xFF;
  89. port->UART_DLH = (divisor >> 8) & 0xFF;
  90. port->UART_LCR &= ~UART_LCR_DLAB;*/
  91. do{
  92. //clear fifo...
  93. port->UART_FCR = UART_FCR_RFIFOR | UART_FCR_XFIFOR;
  94. //read status..
  95. ret = uart_get_status(port);
  96. }while(ret & UART_USR_BUSY);
  97. switch (data_bit) {
  98. case UART_DATA_BIT5:
  99. lcr_reg |= UART_LCR_DLS5;
  100. break;
  101. case UART_DATA_BIT6:
  102. lcr_reg |= UART_LCR_DLS6;
  103. break;
  104. case UART_DATA_BIT7:
  105. lcr_reg |= UART_LCR_DLS7;
  106. break;
  107. case UART_DATA_BIT8:
  108. lcr_reg |= UART_LCR_DLS8;
  109. break;
  110. default:
  111. lcr_reg |= UART_LCR_DLS8;
  112. break;
  113. }
  114. switch (stop_bit) {
  115. case UART_STOP_BIT1:
  116. lcr_reg |= UART_LCR_STOP1;
  117. break;
  118. case UART_STOP_BIT2:
  119. lcr_reg |= UART_LCR_STOP2;
  120. break;
  121. default:
  122. lcr_reg |= UART_LCR_STOP1;
  123. break;
  124. }
  125. switch (parity) {
  126. case UART_PARITY_EVEN:
  127. lcr_reg |= UART_LCR_EVEN | UART_LCR_PEN;
  128. break;
  129. case UART_PARITY_ODD:
  130. lcr_reg |= UART_LCR_PEN;
  131. break;
  132. case UART_PARITY_ST:
  133. lcr_reg |= UART_LCR_SP;
  134. break;
  135. case UART_PARITY_NONE:
  136. default:
  137. break;
  138. }
  139. switch (buard_rate) {
  140. case 115200:
  141. baud_div = BAUDRATE_115200;
  142. break;
  143. case 57600:
  144. baud_div = BAUDRATE_57600;
  145. break;
  146. case 38400:
  147. baud_div = BAUDRATE_38400;
  148. break;
  149. case 19200:
  150. baud_div = BAUDRATE_19200;
  151. break;
  152. case 9600:
  153. baud_div = BAUDRATE_9600;
  154. break;
  155. default:
  156. baud_div = BAUDRATE_115200;
  157. break;
  158. }
  159. //clear fifo
  160. port->UART_FCR = UART_FCR_RFIFOR | UART_FCR_XFIFOR;
  161. //div
  162. ret = port->UART_LCR;
  163. ret |= UART_LCR_DLAB;
  164. port->UART_LCR = ret;
  165. port->RBRTHRDLL = baud_div & 0x00ff;
  166. port->DLHIER = (baud_div & 0x00ff)>>8;
  167. /* clear DLAB */
  168. ret = ret & 0x7f;
  169. port->UART_LCR = ret;
  170. //line control
  171. port->UART_LCR = lcr_reg;
  172. //fifo control
  173. port->UART_FCR = UART_FCR_FIFOE | UART_FCR_RFIFOR | UART_FCR_XFIFOR | UART_FCR_TET_1_4 | UART_FCR_RT_ONE;
  174. }
  175. int uart_enable_irq(uart *port, UINT32 mode)
  176. {
  177. unsigned int ret;
  178. ret = port->UART_IER;
  179. ret |= mode;
  180. port->UART_IER = ret;
  181. }
  182. int uart_disable_irq(uart *port, UINT32 mode)
  183. {
  184. unsigned int ret;
  185. ret = port->UART_IER;
  186. ret &= ~mode;
  187. port->UART_IER = ret;
  188. }
  189. UINT32 uart_get_iir_status(uart *port)
  190. {
  191. return port->UART_IIR;
  192. }
  193. UINT32 uart_get_line_status(uart *port)
  194. {
  195. return port->UART_LSR;
  196. }
  197. UINT32 uart_is_rx_ready(uart *port)
  198. {
  199. return port->UART_LSR & UART_LSR_DR;
  200. }
  201. UINT8 uart_getc(uart *port)
  202. {
  203. return port->UART_RBR & 0xFF;
  204. }
  205. void uart_putc(uart *port, UINT8 c)
  206. {
  207. //while(!(port->UART_USR & UART_USR_TFNF));
  208. port->UART_THR = c;
  209. }
  210. void uart_set_fifo_mode(uart *port, UINT32 fifo_mode)
  211. {
  212. port->UART_FCR = fifo_mode;
  213. }