drv_uart.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. /*
  2. * File : drv_uart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2017, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2017-05-30 Bernard the first version
  23. * 2018-03-15 flyingcys add amebaz
  24. */
  25. #include <rtl8710b.h>
  26. #include <serial_api.h>
  27. #include "board.h"
  28. #include "drv_uart.h"
  29. struct device_uart
  30. {
  31. serial_t serial;
  32. rt_uint32_t irqno;
  33. };
  34. extern int LOGUART_SetBaud(u32 BaudRate);
  35. #ifdef BSP_USING_UART0
  36. static struct rt_serial_device serial0;
  37. static struct device_uart uart0;
  38. #endif
  39. static rt_err_t ameba_uart_configure (struct rt_serial_device *serial, struct serial_configure *cfg);
  40. static rt_err_t ameba_uart_control (struct rt_serial_device *serial, int cmd, void *arg);
  41. static int ameba_uart_putc (struct rt_serial_device *serial, char c);
  42. static int ameba_uart_getc (struct rt_serial_device *serial);
  43. static rt_size_t ameba_uart_dma_transmit (struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  44. static void ameba_uart_irq(uint32_t id, SerialIrq event);
  45. const struct rt_uart_ops _uart_ops =
  46. {
  47. ameba_uart_configure,
  48. ameba_uart_control,
  49. ameba_uart_putc,
  50. ameba_uart_getc,
  51. ameba_uart_dma_transmit
  52. };
  53. /*
  54. * UART interface
  55. */
  56. static rt_err_t ameba_uart_configure (struct rt_serial_device *serial, struct serial_configure *cfg)
  57. {
  58. struct device_uart * uart;
  59. RT_ASSERT(serial != RT_NULL);
  60. serial->config = *cfg;
  61. uart = serial->parent.user_data;
  62. RT_ASSERT(uart != RT_NULL);
  63. /* Set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
  64. serial_format(&uart->serial, 8, ParityNone, 1);
  65. /* set baudrate */
  66. serial_baud(&uart->serial, 115200);
  67. return (RT_EOK);
  68. }
  69. static rt_err_t ameba_uart_control (struct rt_serial_device *serial, int cmd, void *arg)
  70. {
  71. struct device_uart * uart;
  72. uart = serial->parent.user_data;
  73. RT_ASSERT(uart != RT_NULL);
  74. switch (cmd)
  75. {
  76. case RT_DEVICE_CTRL_CLR_INT:
  77. /* Disable the UART Interrupt */
  78. serial_irq_set(&uart->serial, RxIrq, 0);
  79. serial_irq_handler(&uart->serial, RT_NULL, 0);
  80. break;
  81. case RT_DEVICE_CTRL_SET_INT:
  82. /* install interrupt */
  83. serial_irq_handler(&uart->serial, ameba_uart_irq, (uint32_t)serial);
  84. /* Enable the UART Interrupt */
  85. serial_irq_set(&uart->serial, RxIrq, 1);
  86. break;
  87. }
  88. return (RT_EOK);
  89. }
  90. static int ameba_uart_putc (struct rt_serial_device *serial, char c)
  91. {
  92. struct device_uart* uart;
  93. uart = serial->parent.user_data;
  94. /* FIFO status, contain valid data */
  95. /* write data */
  96. serial_putc(&uart->serial, c);
  97. return (1);
  98. }
  99. static int ameba_uart_getc (struct rt_serial_device *serial)
  100. {
  101. struct device_uart* uart = serial->parent.user_data;
  102. if(!serial_readable(&uart->serial))
  103. return -1;
  104. /* Receive Data Available */
  105. return serial_getc(&uart->serial);
  106. }
  107. static rt_size_t ameba_uart_dma_transmit (struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  108. {
  109. return (0);
  110. }
  111. static void ameba_uart_irq(uint32_t id, SerialIrq event)
  112. {
  113. struct rt_serial_device *serial = (struct rt_serial_device *)id;
  114. if(event == RxIrq)
  115. {
  116. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  117. }
  118. else if(event == TxIrq)
  119. {
  120. }
  121. }
  122. static rt_err_t dbg_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  123. static rt_err_t dbg_control(struct rt_serial_device *serial, int cmd, void *arg);
  124. static int dbg_putc(struct rt_serial_device *serial, char c);
  125. static int dbg_getc(struct rt_serial_device *serial);
  126. static struct rt_serial_device ameba_dbg_serial;
  127. const struct rt_uart_ops _ambed_dbg_ops =
  128. {
  129. dbg_configure,
  130. dbg_control,
  131. dbg_putc,
  132. dbg_getc,
  133. RT_NULL,
  134. };
  135. static rt_err_t dbg_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  136. {
  137. LOGUART_SetBaud(115200);
  138. return RT_EOK;
  139. }
  140. void dbg_uart_irq_handler(void * data)
  141. {
  142. u32 IrqEn = DiagGetIsrEnReg();
  143. DiagSetIsrEnReg(0);
  144. rt_hw_serial_isr(&ameba_dbg_serial, RT_SERIAL_EVENT_RX_IND);
  145. DiagSetIsrEnReg(IrqEn);
  146. }
  147. static rt_err_t dbg_control(struct rt_serial_device *serial, int cmd, void *arg)
  148. {
  149. switch (cmd)
  150. {
  151. case RT_DEVICE_CTRL_CLR_INT:
  152. /* Disable the UART Interrupt */
  153. NVIC_DisableIRQ(UART_LOG_IRQ); /* this is rom_code_patch */
  154. break;
  155. case RT_DEVICE_CTRL_SET_INT:
  156. /* install interrupt */
  157. DIAG_UartReInit((IRQ_FUN) dbg_uart_irq_handler);
  158. /* Enable the UART Interrupt */
  159. NVIC_SetPriority(UART_LOG_IRQ, 10); /* this is rom_code_patch */
  160. break;
  161. }
  162. return (RT_EOK);
  163. }
  164. static int dbg_putc(struct rt_serial_device *serial, char c)
  165. {
  166. DiagPutChar(c);
  167. return 1;
  168. };
  169. static int dbg_getc(struct rt_serial_device *serial)
  170. {
  171. int c = -1;
  172. if(!UART_Readable(UART2_DEV))
  173. return -1;
  174. c = DiagGetChar(_FALSE);
  175. return c;
  176. }
  177. /*
  178. * UART Initiation
  179. */
  180. int rt_hw_uart_init(void)
  181. {
  182. struct rt_serial_device *serial;
  183. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  184. #ifdef BSP_USING_UART0
  185. {
  186. struct device_uart *uart;
  187. serial = &serial0;
  188. uart = &uart0;
  189. /* Init UART Hardware */
  190. serial_init(&uart->serial, UART_TX, UART_RX);
  191. serial->ops = &_uart_ops;
  192. serial->config = config;
  193. serial->config.baud_rate = 115200;
  194. uart->irqno = UART0_IRQ;
  195. rt_hw_serial_register(serial,
  196. "uart0",
  197. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  198. uart);
  199. }
  200. #endif
  201. {
  202. serial = &ameba_dbg_serial;
  203. serial->ops = &_ambed_dbg_ops;
  204. serial->config = config;
  205. rt_hw_serial_register(serial,
  206. RT_CONSOLE_DEVICE_NAME,
  207. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  208. RT_NULL);
  209. }
  210. return 0;
  211. }