uart.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. #include <board.h>
  27. #include <rtdevice.h>
  28. #include "fh_arch.h"
  29. #include "libraries/inc/fh_uart.h"
  30. void rt_fh_uart_handler(int vector, void *param)
  31. {
  32. int status;
  33. unsigned int ret;
  34. struct fh_uart *uart;
  35. unsigned int reg_status;
  36. rt_device_t dev = (rt_device_t)param;
  37. uart = (struct fh_uart *)dev->user_data;
  38. status = uart_get_iir_status(uart->uart_port);
  39. if (status & UART_IIR_NOINT)
  40. {
  41. return;
  42. }
  43. if(status & UART_IIR_THREMPTY){
  44. //first close tx isr
  45. uart_disable_irq(uart->uart_port,UART_IER_ETBEI);
  46. rt_hw_serial_isr((struct rt_serial_device *)dev, RT_SERIAL_EVENT_TX_DONE);
  47. }
  48. else if((status & UART_IIR_CHRTOUT)==UART_IIR_CHRTOUT){
  49. //bug....
  50. //if no data in rx fifo
  51. reg_status = uart_get_status(uart->uart_port);
  52. if((reg_status & 1<<3) == 0)
  53. ret = uart_getc(uart->uart_port);
  54. }
  55. else{
  56. rt_interrupt_enter();
  57. rt_hw_serial_isr((struct rt_serial_device *)dev, RT_SERIAL_EVENT_RX_IND);
  58. rt_interrupt_leave();
  59. }
  60. }
  61. /**
  62. * UART device in RT-Thread
  63. */
  64. static rt_err_t fh_uart_configure(struct rt_serial_device *serial,
  65. struct serial_configure *cfg)
  66. {
  67. int div;
  68. enum data_bits data_mode;
  69. enum stop_bits stop_mode;
  70. enum parity parity_mode;
  71. struct fh_uart *uart;
  72. RT_ASSERT(serial != RT_NULL);
  73. RT_ASSERT(cfg != RT_NULL);
  74. uart = (struct fh_uart *)serial->parent.user_data;
  75. switch (cfg->data_bits)
  76. {
  77. case DATA_BITS_8:
  78. data_mode = UART_DATA_BIT8;
  79. break;
  80. case DATA_BITS_7:
  81. data_mode = UART_DATA_BIT7;
  82. break;
  83. case DATA_BITS_6:
  84. data_mode = UART_DATA_BIT6;
  85. break;
  86. case DATA_BITS_5:
  87. data_mode = UART_DATA_BIT5;
  88. break;
  89. default:
  90. data_mode = UART_DATA_BIT8;
  91. break;
  92. }
  93. switch (cfg->stop_bits)
  94. {
  95. case STOP_BITS_2:
  96. stop_mode = UART_STOP_BIT2;
  97. break;
  98. case STOP_BITS_1:
  99. default:
  100. stop_mode = UART_STOP_BIT1;
  101. break;
  102. }
  103. switch (cfg->parity)
  104. {
  105. case PARITY_ODD:
  106. parity_mode = UART_PARITY_ODD;
  107. break;
  108. case PARITY_EVEN:
  109. parity_mode = UART_PARITY_EVEN;
  110. break;
  111. case PARITY_NONE:
  112. default:
  113. parity_mode = UART_PARITY_NONE;
  114. break;
  115. }
  116. uart_disable_irq(uart->uart_port, UART_IER_ERBFI);
  117. uart_configure(uart->uart_port, data_mode,
  118. stop_mode, parity_mode,
  119. cfg->baud_rate, UART_CLOCK_FREQ);
  120. uart_enable_irq(uart->uart_port, UART_IER_ERBFI);
  121. return RT_EOK;
  122. }
  123. static rt_err_t fh_uart_control(struct rt_serial_device *serial,
  124. int cmd, void *arg)
  125. {
  126. struct fh_uart* uart;
  127. RT_ASSERT(serial != RT_NULL);
  128. uart = (struct fh_uart *)serial->parent.user_data;
  129. switch (cmd)
  130. {
  131. case RT_DEVICE_CTRL_CLR_INT:
  132. /* disable rx irq */
  133. rt_hw_interrupt_mask(uart->irq);
  134. uart_disable_irq(uart->uart_port,UART_IER_ERBFI);
  135. break;
  136. case RT_DEVICE_CTRL_SET_INT:
  137. /* enable rx irq */
  138. rt_hw_interrupt_umask(uart->irq);
  139. uart_enable_irq(uart->uart_port,UART_IER_ERBFI);
  140. break;
  141. }
  142. return RT_EOK;
  143. }
  144. static int fh_uart_putc(struct rt_serial_device *serial, char c)
  145. {
  146. struct fh_uart *uart = serial->parent.user_data;
  147. unsigned int ret;
  148. ret = uart_get_status(uart->uart_port);
  149. if(serial->parent.open_flag & RT_DEVICE_FLAG_INT_TX){
  150. //RT_DEVICE_FLAG_INT_TX
  151. if(c == '\n'){
  152. fh_uart_putc(serial,'\r');
  153. }
  154. if(ret & UART_USR_TFNF){
  155. uart_putc(uart->uart_port, c);
  156. return 1;
  157. }
  158. //open tx isr here..
  159. uart_enable_irq(uart->uart_port,UART_IER_ETBEI);
  160. return -1;
  161. }
  162. //poll mode
  163. else{
  164. while(!(uart_get_status(uart->uart_port) & UART_USR_TFNF))
  165. ;
  166. uart_putc(uart->uart_port, c);
  167. return 1;
  168. }
  169. }
  170. static int fh_uart_getc(struct rt_serial_device *serial)
  171. {
  172. int result;
  173. struct fh_uart *uart = serial->parent.user_data;
  174. if (uart_is_rx_ready(uart->uart_port))
  175. {
  176. result = uart_getc(uart->uart_port);
  177. }
  178. else
  179. {
  180. result = -1;
  181. }
  182. return result;
  183. }
  184. static const struct rt_uart_ops fh_uart_ops =
  185. {
  186. fh_uart_configure,
  187. fh_uart_control,
  188. fh_uart_putc,
  189. fh_uart_getc,
  190. };
  191. #if defined(RT_USING_UART0)
  192. static struct rt_serial_device serial0;
  193. struct fh_uart uart0 = {
  194. (uart *)UART0_REG_BASE,
  195. UART0_IRQn
  196. };
  197. #endif
  198. #if defined(RT_USING_UART1)
  199. static struct rt_serial_device serial1;
  200. struct fh_uart uart1 = {
  201. (uart *)UART1_REG_BASE,
  202. UART1_IRQn
  203. };
  204. #endif
  205. /**
  206. * This function will handle init uart
  207. */
  208. void rt_hw_uart_init(void)
  209. {
  210. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  211. #if defined(RT_USING_UART0)
  212. #if(0)
  213. serial0.ops = &fh_uart_ops;
  214. serial0.config = config;
  215. /* register vcom device */
  216. rt_hw_serial_register(&serial0, "uart0",
  217. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM | RT_DEVICE_FLAG_INT_TX | RT_DEVICE_FLAG_STANDALONE,
  218. &uart0);
  219. rt_hw_interrupt_install(uart0.irq, rt_fh_uart_handler,
  220. (void *)&(serial0.parent), "UART0");
  221. rt_hw_interrupt_umask(uart0.irq);
  222. #endif
  223. serial0.ops = &fh_uart_ops;
  224. serial0.config = config;
  225. /* register vcom device */
  226. rt_hw_serial_register(&serial0, "uart0",
  227. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM ,
  228. &uart0);
  229. rt_hw_interrupt_install(uart0.irq, rt_fh_uart_handler,
  230. (void *)&(serial0.parent), "UART0");
  231. rt_hw_interrupt_umask(uart0.irq);
  232. #endif
  233. #if defined(RT_USING_UART1)
  234. serial1.ops = &fh_uart_ops;
  235. serial1.config = config;
  236. /* register vcom device */
  237. rt_hw_serial_register(&serial1, "uart1",
  238. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX | RT_DEVICE_FLAG_STREAM ,
  239. &uart1);
  240. rt_hw_interrupt_install(uart1.irq, rt_fh_uart_handler,
  241. (void *)&(serial1.parent), "UART1");
  242. rt_hw_interrupt_umask(uart1.irq);
  243. #endif
  244. }