drv_uart.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  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. * 2020-06-27 AHTYDHD the first version
  9. */
  10. #include "drv_uart.h"
  11. #include <stdint.h>
  12. #include <stdbool.h>
  13. #include "inc/hw_memmap.h"
  14. #include "inc/hw_ints.h"
  15. #include "driverlib/sysctl.h"
  16. #include "driverlib/gpio.h"
  17. #include "driverlib/pin_map.h"
  18. #include "driverlib/interrupt.h"
  19. #include "driverlib/uart.h"
  20. #ifdef RT_USING_SERIAL
  21. #include "uart_config.h"
  22. #include "tm4c123_config.h"
  23. #define LOG_TAG "drv.uart"
  24. #include <drv_log.h>
  25. #if !defined(BSP_USING_UART0)&&!defined(BSP_USING_UART1)&&!defined(BSP_USING_UART2)&&!defined(BSP_USING_UART3)
  26. #error "Please define at least one BSP_USING_UARTx"
  27. #endif
  28. enum
  29. {
  30. #ifdef BSP_USING_UART0
  31. UART0_INDEX,
  32. #endif
  33. #ifdef BSP_USING_UART1
  34. UART1_INDEX,
  35. #endif
  36. #ifdef BSP_USING_UART2
  37. UART2_INDEX,
  38. #endif
  39. #ifdef BSP_USING_UART3
  40. UART3_INDEX,
  41. #endif
  42. };
  43. uint32_t uart_intbase[] =
  44. {
  45. #ifdef BSP_USING_UART0
  46. INT_UART0,
  47. #endif
  48. #ifdef BSP_USING_UART1
  49. INT_UART1,
  50. #endif
  51. #ifdef BSP_USING_UART2
  52. INT_UART2,
  53. #endif
  54. #ifdef BSP_USING_UART3
  55. INT_UART3
  56. #endif
  57. };
  58. static struct tm4c123_uart_config uart_config[] =
  59. {
  60. #ifdef BSP_USING_UART0
  61. UART0_CONFIG,
  62. #endif
  63. #ifdef BSP_USING_UART1
  64. UART1_CONFIG,
  65. #endif
  66. #ifdef BSP_USING_UART2
  67. UART2_CONFIG,
  68. #endif
  69. #ifdef BSP_USING_UART3
  70. UART3_CONFIG,
  71. #endif
  72. };
  73. static struct tm4c123_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
  74. static rt_err_t tm4c123_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  75. {
  76. struct tm4c123_uart *uart;
  77. RT_ASSERT(serial != RT_NULL);
  78. RT_ASSERT(cfg != RT_NULL);
  79. uart = rt_container_of(serial, struct tm4c123_uart, serial);
  80. UARTFIFOLevelSet(uart->config->uartbase, UART_FIFO_TX1_8, UART_FIFO_RX1_8);
  81. UARTConfigSetExpClk(uart->config->uartbase, SysCtlClockGet(), uart->config->baudrate,
  82. uart->config->mode);
  83. return RT_EOK;
  84. }
  85. static rt_err_t tm4c123_control(struct rt_serial_device *serial, int cmd, void *arg)
  86. {
  87. struct tm4c123_uart *uart;
  88. RT_ASSERT(serial != RT_NULL);
  89. uart = rt_container_of(serial, struct tm4c123_uart, serial);
  90. switch (cmd)
  91. {
  92. /* disable interrupt */
  93. case RT_DEVICE_CTRL_CLR_INT:
  94. /* disable rx irq */
  95. IntDisable(uart->uartintbase);
  96. UARTIntDisable(uart->config->uartbase, UART_INT_RX);
  97. break;
  98. /* enable interrupt */
  99. case RT_DEVICE_CTRL_SET_INT:
  100. /* enable rx irq */
  101. IntEnable(uart->uartintbase);
  102. UARTIntEnable(uart->config->uartbase, UART_INT_RX);
  103. break;
  104. }
  105. return RT_EOK;
  106. }
  107. static int tm4c123_putc(struct rt_serial_device *serial, char c)
  108. {
  109. struct tm4c123_uart *uart;
  110. RT_ASSERT(serial != RT_NULL);
  111. uart = rt_container_of(serial, struct tm4c123_uart, serial);
  112. UARTCharPut(uart->config->uartbase, c);
  113. return 1;
  114. }
  115. static int tm4c123_getc(struct rt_serial_device *serial)
  116. {
  117. int ch;
  118. struct tm4c123_uart *uart;
  119. RT_ASSERT(serial != RT_NULL);
  120. uart = rt_container_of(serial, struct tm4c123_uart, serial);
  121. ch = -1;
  122. ch = UARTCharGetNonBlocking(uart->config->uartbase);
  123. return ch;
  124. }
  125. static rt_size_t tm4c123_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  126. {
  127. /* this is an interface for uart dma, reserved for uptate. */
  128. return 0;
  129. }
  130. static const struct rt_uart_ops tm4c123_uart_ops =
  131. {
  132. .configure = tm4c123_configure,
  133. .control = tm4c123_control,
  134. .putc = tm4c123_putc,
  135. .getc = tm4c123_getc,
  136. .dma_transmit = tm4c123_dma_transmit
  137. };
  138. /**
  139. * Uart common interrupt process. This need add to uart ISR.
  140. *
  141. * @param serial serial device
  142. */
  143. static void uart_isr(struct rt_serial_device *serial)
  144. {
  145. struct tm4c123_uart *uart;
  146. uint32_t ui32Ints;
  147. RT_ASSERT(serial != RT_NULL);
  148. uart = rt_container_of(serial, struct tm4c123_uart, serial);
  149. ui32Ints = UARTIntStatus(uart->config->uartbase, true);
  150. UARTIntClear(uart->config->uartbase, ui32Ints);
  151. /* UART in mode Receiver -------------------------------------------------*/
  152. if (ui32Ints & (UART_INT_RX | UART_INT_RT))
  153. {
  154. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  155. }
  156. }
  157. #if defined(BSP_USING_UART0)
  158. void UART0IntHandler(void)
  159. {
  160. /* enter interrupt */
  161. rt_interrupt_enter();
  162. uart_isr(&(uart_obj[UART0_INDEX].serial));
  163. /* leave interrupt */
  164. rt_interrupt_leave();
  165. }
  166. #endif /* BSP_USING_UART0 */
  167. #if defined(BSP_USING_UART1)
  168. void UART1IntHandler(void)
  169. {
  170. /* enter interrupt */
  171. rt_interrupt_enter();
  172. uart_isr(&(uart_obj[UART1_INDEX].serial));
  173. /* leave interrupt */
  174. rt_interrupt_leave();
  175. }
  176. #endif /* BSP_USING_UART1 */
  177. static void tm4c123_uart_get_dma_config(void)
  178. {
  179. /* this is an interface for uart dma, reserved for update */
  180. }
  181. int rt_hw_usart_init(void)
  182. {
  183. rt_size_t obj_num = sizeof(uart_obj) / sizeof(struct tm4c123_uart);
  184. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  185. rt_err_t result = 0;
  186. uart_hw_config();
  187. for (int i = 0; i < obj_num; i++)
  188. {
  189. uart_obj[i].config = &uart_config[i];
  190. uart_obj[i].uartintbase = uart_intbase[i];
  191. uart_obj[i].serial.ops = &tm4c123_uart_ops;
  192. uart_obj[i].serial.config = config;
  193. /* register UART device */
  194. result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
  195. RT_DEVICE_FLAG_RDWR
  196. | RT_DEVICE_FLAG_INT_RX
  197. | RT_DEVICE_FLAG_INT_TX
  198. | uart_obj[i].uart_dma_flag
  199. , NULL);
  200. RT_ASSERT(result == RT_EOK);
  201. }
  202. return result;
  203. }
  204. #endif /* RT_USING_SERIAL */
  205. /************************** end of file ******************/