drv_uart.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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 <rthw.h>
  26. #include <rtthread.h>
  27. #include <rtdevice.h>
  28. #include "board.h"
  29. #include "drv_uart.h"
  30. struct device_uart
  31. {
  32. serial_t serial;
  33. rt_uint32_t irqno;
  34. };
  35. #ifdef RT_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. rt_uint32_t baud_div;
  59. struct device_uart * uart;
  60. RT_ASSERT(serial != RT_NULL);
  61. serial->config = *cfg;
  62. uart = serial->parent.user_data;
  63. RT_ASSERT(uart != RT_NULL);
  64. /* Set databits, stopbits and parity. (8-bit data, 1 stopbit, no parity) */
  65. serial_format(&uart->serial, 8, ParityNone, 1);
  66. /* set baudrate */
  67. serial_baud(&uart->serial, 115200);
  68. return (RT_EOK);
  69. }
  70. static rt_err_t ameba_uart_control (struct rt_serial_device *serial, int cmd, void *arg)
  71. {
  72. struct device_uart * uart;
  73. uart = serial->parent.user_data;
  74. RT_ASSERT(uart != RT_NULL);
  75. switch (cmd)
  76. {
  77. case RT_DEVICE_CTRL_CLR_INT:
  78. /* Disable the UART Interrupt */
  79. serial_irq_set(&uart->serial, RxIrq, 0);
  80. serial_irq_handler(&uart->serial, RT_NULL, 0);
  81. break;
  82. case RT_DEVICE_CTRL_SET_INT:
  83. /* install interrupt */
  84. serial_irq_handler(&uart->serial, ameba_uart_irq, (uint32_t)serial);
  85. /* Enable the UART Interrupt */
  86. serial_irq_set(&uart->serial, RxIrq, 1);
  87. break;
  88. }
  89. return (RT_EOK);
  90. }
  91. static int ameba_uart_putc (struct rt_serial_device *serial, char c)
  92. {
  93. struct device_uart* uart;
  94. uart = serial->parent.user_data;
  95. /* FIFO status, contain valid data */
  96. /* write data */
  97. serial_putc(&uart->serial, c);
  98. return (1);
  99. }
  100. static int ameba_uart_getc (struct rt_serial_device *serial)
  101. {
  102. struct device_uart* uart = serial->parent.user_data;
  103. if(!serial_readable(&uart->serial))
  104. return -1;
  105. /* Receive Data Available */
  106. return serial_getc(&uart->serial);
  107. }
  108. static rt_size_t ameba_uart_dma_transmit (struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  109. {
  110. return (0);
  111. }
  112. static void ameba_uart_irq(uint32_t id, SerialIrq event)
  113. {
  114. struct rt_serial_device *serial = (struct rt_serial_device *)id;
  115. if(event == RxIrq)
  116. {
  117. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  118. }
  119. else if(event == TxIrq)
  120. {
  121. }
  122. }
  123. static rt_err_t dbg_configure(struct rt_serial_device *serial, struct serial_configure *cfg);
  124. static rt_err_t dbg_control(struct rt_serial_device *serial, int cmd, void *arg);
  125. static int dbg_putc(struct rt_serial_device *serial, char c);
  126. static int dbg_getc(struct rt_serial_device *serial);
  127. static struct rt_serial_device ameba_dbg_serial;
  128. const struct rt_uart_ops _ambed_dbg_ops =
  129. {
  130. dbg_configure,
  131. dbg_control,
  132. dbg_putc,
  133. dbg_getc,
  134. RT_NULL,
  135. };
  136. static rt_err_t dbg_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  137. {
  138. LOGUART_SetBaud(115200);
  139. return RT_EOK;
  140. }
  141. void dbg_uart_irq_handler(void * data)
  142. {
  143. u32 IrqEn = DiagGetIsrEnReg();
  144. DiagSetIsrEnReg(0);
  145. rt_hw_serial_isr(&ameba_dbg_serial, RT_SERIAL_EVENT_RX_IND);
  146. DiagSetIsrEnReg(IrqEn);
  147. }
  148. static rt_err_t dbg_control(struct rt_serial_device *serial, int cmd, void *arg)
  149. {
  150. switch (cmd)
  151. {
  152. case RT_DEVICE_CTRL_CLR_INT:
  153. /* Disable the UART Interrupt */
  154. NVIC_DisableIRQ(UART_LOG_IRQ); /* this is rom_code_patch */
  155. break;
  156. case RT_DEVICE_CTRL_SET_INT:
  157. /* install interrupt */
  158. DIAG_UartReInit((IRQ_FUN) dbg_uart_irq_handler);
  159. /* Enable the UART Interrupt */
  160. NVIC_SetPriority(UART_LOG_IRQ, 10); /* this is rom_code_patch */
  161. break;
  162. }
  163. return (RT_EOK);
  164. }
  165. static int dbg_putc(struct rt_serial_device *serial, char c)
  166. {
  167. DiagPutChar(c);
  168. return 1;
  169. };
  170. static int dbg_getc(struct rt_serial_device *serial)
  171. {
  172. int c = -1;
  173. if(!UART_Readable(UART2_DEV))
  174. return -1;
  175. c = DiagGetChar(_FALSE);
  176. return c;
  177. }
  178. /*
  179. * UART Initiation
  180. */
  181. int rt_hw_uart_init(void)
  182. {
  183. struct rt_serial_device *serial;
  184. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  185. #ifdef RT_USING_UART0
  186. {
  187. struct device_uart *uart;
  188. serial = &serial0;
  189. uart = &uart0;
  190. /* Init UART Hardware */
  191. serial_init(&uart->serial, UART_TX, UART_RX);
  192. serial->ops = &_uart_ops;
  193. serial->config = config;
  194. serial->config.baud_rate = 115200;
  195. uart->irqno = UART0_IRQ;
  196. rt_hw_serial_register(serial,
  197. "uart0",
  198. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  199. uart);
  200. }
  201. #endif
  202. {
  203. serial = &ameba_dbg_serial;
  204. serial->ops = &_ambed_dbg_ops;
  205. serial->config = config;
  206. rt_hw_serial_register(serial,
  207. RT_CONSOLE_DEVICE_NAME,
  208. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  209. RT_NULL);
  210. }
  211. return 0;
  212. }