drv_uart.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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. * 2017-05-30 Bernard the first version
  9. * 2018-03-15 flyingcys add amebaz
  10. */
  11. #include <rtl8710b.h>
  12. #include <serial_api.h>
  13. #include "board.h"
  14. #include "drv_uart.h"
  15. struct device_uart
  16. {
  17. serial_t serial;
  18. rt_uint32_t irqno;
  19. };
  20. extern int LOGUART_SetBaud(u32 BaudRate);
  21. #ifdef BSP_USING_UART0
  22. static struct rt_serial_device serial0;
  23. static struct device_uart uart0;
  24. #endif
  25. static rt_err_t ameba_uart_configure (struct rt_serial_device *serial, struct serial_configure *cfg);
  26. static rt_err_t ameba_uart_control (struct rt_serial_device *serial, int cmd, void *arg);
  27. static int ameba_uart_putc (struct rt_serial_device *serial, char c);
  28. static int ameba_uart_getc (struct rt_serial_device *serial);
  29. static rt_size_t ameba_uart_dma_transmit (struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction);
  30. static void ameba_uart_irq(uint32_t id, SerialIrq event);
  31. const struct rt_uart_ops _uart_ops =
  32. {
  33. ameba_uart_configure,
  34. ameba_uart_control,
  35. ameba_uart_putc,
  36. ameba_uart_getc,
  37. ameba_uart_dma_transmit
  38. };
  39. /*
  40. * UART interface
  41. */
  42. static rt_err_t ameba_uart_configure (struct rt_serial_device *serial, struct serial_configure *cfg)
  43. {
  44. struct device_uart * uart;
  45. RT_ASSERT(serial != RT_NULL);
  46. serial->config = *cfg;
  47. uart = serial->parent.user_data;
  48. RT_ASSERT(uart != RT_NULL);
  49. /* Set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
  50. serial_format(&uart->serial, 8, ParityNone, 1);
  51. /* set baudrate */
  52. serial_baud(&uart->serial, 115200);
  53. return (RT_EOK);
  54. }
  55. static rt_err_t ameba_uart_control (struct rt_serial_device *serial, int cmd, void *arg)
  56. {
  57. struct device_uart * uart;
  58. uart = serial->parent.user_data;
  59. RT_ASSERT(uart != RT_NULL);
  60. switch (cmd)
  61. {
  62. case RT_DEVICE_CTRL_CLR_INT:
  63. /* Disable the UART Interrupt */
  64. serial_irq_set(&uart->serial, RxIrq, 0);
  65. serial_irq_handler(&uart->serial, RT_NULL, 0);
  66. break;
  67. case RT_DEVICE_CTRL_SET_INT:
  68. /* install interrupt */
  69. serial_irq_handler(&uart->serial, ameba_uart_irq, (uint32_t)serial);
  70. /* Enable the UART Interrupt */
  71. serial_irq_set(&uart->serial, RxIrq, 1);
  72. break;
  73. }
  74. return (RT_EOK);
  75. }
  76. static int ameba_uart_putc (struct rt_serial_device *serial, char c)
  77. {
  78. struct device_uart* uart;
  79. uart = serial->parent.user_data;
  80. /* FIFO status, contain valid data */
  81. /* write data */
  82. serial_putc(&uart->serial, c);
  83. return (1);
  84. }
  85. static int ameba_uart_getc (struct rt_serial_device *serial)
  86. {
  87. struct device_uart* uart = serial->parent.user_data;
  88. if(!serial_readable(&uart->serial))
  89. return -1;
  90. /* Receive Data Available */
  91. return serial_getc(&uart->serial);
  92. }
  93. static rt_size_t ameba_uart_dma_transmit (struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  94. {
  95. return (0);
  96. }
  97. static void ameba_uart_irq(uint32_t id, SerialIrq event)
  98. {
  99. struct rt_serial_device *serial = (struct rt_serial_device *)id;
  100. if(event == RxIrq)
  101. {
  102. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  103. }
  104. else if(event == TxIrq)
  105. {
  106. }
  107. }
  108. static rt_err_t dbg_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  109. static rt_err_t dbg_control(struct rt_serial_device *serial, int cmd, void *arg);
  110. static int dbg_putc(struct rt_serial_device *serial, char c);
  111. static int dbg_getc(struct rt_serial_device *serial);
  112. static struct rt_serial_device ameba_dbg_serial;
  113. const struct rt_uart_ops _ambed_dbg_ops =
  114. {
  115. dbg_configure,
  116. dbg_control,
  117. dbg_putc,
  118. dbg_getc,
  119. RT_NULL,
  120. };
  121. static rt_err_t dbg_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  122. {
  123. LOGUART_SetBaud(115200);
  124. return RT_EOK;
  125. }
  126. void dbg_uart_irq_handler(void * data)
  127. {
  128. u32 IrqEn = DiagGetIsrEnReg();
  129. DiagSetIsrEnReg(0);
  130. rt_hw_serial_isr(&ameba_dbg_serial, RT_SERIAL_EVENT_RX_IND);
  131. DiagSetIsrEnReg(IrqEn);
  132. }
  133. static rt_err_t dbg_control(struct rt_serial_device *serial, int cmd, void *arg)
  134. {
  135. switch (cmd)
  136. {
  137. case RT_DEVICE_CTRL_CLR_INT:
  138. /* Disable the UART Interrupt */
  139. NVIC_DisableIRQ(UART_LOG_IRQ); /* this is rom_code_patch */
  140. break;
  141. case RT_DEVICE_CTRL_SET_INT:
  142. /* install interrupt */
  143. DIAG_UartReInit((IRQ_FUN) dbg_uart_irq_handler);
  144. /* Enable the UART Interrupt */
  145. NVIC_SetPriority(UART_LOG_IRQ, 10); /* this is rom_code_patch */
  146. break;
  147. }
  148. return (RT_EOK);
  149. }
  150. static int dbg_putc(struct rt_serial_device *serial, char c)
  151. {
  152. DiagPutChar(c);
  153. return 1;
  154. };
  155. static int dbg_getc(struct rt_serial_device *serial)
  156. {
  157. int c = -1;
  158. if(!UART_Readable(UART2_DEV))
  159. return -1;
  160. c = DiagGetChar(_FALSE);
  161. return c;
  162. }
  163. /*
  164. * UART Initiation
  165. */
  166. int rt_hw_uart_init(void)
  167. {
  168. struct rt_serial_device *serial;
  169. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  170. #ifdef BSP_USING_UART0
  171. {
  172. struct device_uart *uart;
  173. serial = &serial0;
  174. uart = &uart0;
  175. /* Init UART Hardware */
  176. serial_init(&uart->serial, UART_TX, UART_RX);
  177. serial->ops = &_uart_ops;
  178. serial->config = config;
  179. serial->config.baud_rate = 115200;
  180. uart->irqno = UART0_IRQ;
  181. rt_hw_serial_register(serial,
  182. "uart0",
  183. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  184. uart);
  185. }
  186. #endif
  187. {
  188. serial = &ameba_dbg_serial;
  189. serial->ops = &_ambed_dbg_ops;
  190. serial->config = config;
  191. rt_hw_serial_register(serial,
  192. RT_CONSOLE_DEVICE_NAME,
  193. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  194. RT_NULL);
  195. }
  196. return 0;
  197. }