drv_uart.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * 2021-09-05 qinweizhong first version
  9. */
  10. #include <rtdevice.h>
  11. #include "drv_uart.h"
  12. #include "tae32f53xx_ll.h"
  13. #include <tae32f53xx_ll_cortex.h>
  14. #include "drv_uart.h"
  15. /* uart driver */
  16. struct tae32_uart
  17. {
  18. UART_TypeDef *uart;
  19. IRQn_Type irq;
  20. };
  21. static void uart_init(void)
  22. {
  23. UART_InitTypeDef uart_init;
  24. uart_init.baudrate = 115200;
  25. uart_init.dat_len = UART_DAT_LEN_8b;
  26. uart_init.stop_len = UART_STOP_LEN_1b;
  27. uart_init.parity = UART_PARITY_NO;
  28. uart_init.tx_tl = UART_TX_EMPTY_TRI_LVL_EMPTY;
  29. uart_init.rx_tl = UART_RX_AVL_TRI_LVL_1CHAR;
  30. LL_UART_Init(UART0, &uart_init);
  31. __LL_UART_RxDatAvl_INT_En(UART0);
  32. }
  33. static rt_err_t _uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  34. {
  35. struct tae32_uart *uart;
  36. UART_InitTypeDef UART_InitStructure;
  37. RT_ASSERT(serial != RT_NULL);
  38. RT_ASSERT(cfg != RT_NULL);
  39. uart = (struct tae32_uart *)serial->parent.user_data;
  40. UART_InitStructure.baudrate = cfg->baud_rate;
  41. if (cfg->data_bits == DATA_BITS_8)
  42. UART_InitStructure.dat_len = UART_DAT_LEN_8b;
  43. if (cfg->stop_bits == STOP_BITS_1)
  44. UART_InitStructure.stop_len = UART_STOP_LEN_1b;
  45. else if (cfg->stop_bits == STOP_BITS_2)
  46. UART_InitStructure.stop_len = UART_STOP_LEN_2b;
  47. UART_InitStructure.parity = UART_PARITY_NO;
  48. UART_InitStructure.tx_tl = UART_TX_EMPTY_TRI_LVL_EMPTY;
  49. UART_InitStructure.rx_tl = UART_RX_AVL_TRI_LVL_1CHAR;
  50. LL_UART_Init(uart->uart, &UART_InitStructure);
  51. return RT_EOK;
  52. }
  53. static rt_err_t _uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  54. {
  55. struct tae32_uart *uart;
  56. RT_ASSERT(serial != RT_NULL);
  57. uart = (struct tae32_uart *)serial->parent.user_data;
  58. switch (cmd)
  59. {
  60. case RT_DEVICE_CTRL_CLR_INT:
  61. /* disable rx irq */
  62. NVIC_DisableIRQ(uart->irq);
  63. __LL_UART_RxDatAvl_INT_Dis(uart->uart);
  64. break;
  65. case RT_DEVICE_CTRL_SET_INT:
  66. /* enable rx irq */
  67. NVIC_EnableIRQ(uart->irq);
  68. /* enable interrupt */
  69. __LL_UART_RxDatAvl_INT_En(uart->uart);
  70. break;
  71. }
  72. return RT_EOK;
  73. }
  74. static int _uart_putc(struct rt_serial_device *serial, char c)
  75. {
  76. struct tae32_uart *uart;
  77. RT_ASSERT(serial != RT_NULL);
  78. uart = (struct tae32_uart *)serial->parent.user_data;
  79. while (!__LL_UART_IsTxFIFONotFull(uart->uart)) {};
  80. __LL_UART_TxBuf9bits_Write(uart->uart, (uint16_t)c);
  81. return 1;
  82. }
  83. static int _uart_getc(struct rt_serial_device *serial)
  84. {
  85. int ch;
  86. struct tae32_uart *uart;
  87. RT_ASSERT(serial != RT_NULL);
  88. uart = (struct tae32_uart *)serial->parent.user_data;
  89. ch = -1;
  90. if (__LL_UART_IsDatReady(uart->uart))
  91. {
  92. ch = __LL_UART_RxBuf9bits_Read(uart->uart);
  93. }
  94. else
  95. {
  96. rt_thread_mdelay(10);
  97. }
  98. return ch;
  99. }
  100. static const struct rt_uart_ops _uart_ops =
  101. {
  102. _uart_configure,
  103. _uart_control,
  104. _uart_putc,
  105. _uart_getc,
  106. };
  107. #if defined(BSP_USING_UART0)
  108. /* UART0 device driver structure */
  109. static struct tae32_uart uart0;
  110. struct rt_serial_device serial0;
  111. void UART0_IRQHandler(void)
  112. {
  113. struct tae32_uart *uart;
  114. uart = &uart0;
  115. /* enter interrupt */
  116. rt_interrupt_enter();
  117. if (__LL_UART_INT_ID_Get(uart->uart) == UART_INT_ID_RX_AVL)
  118. {
  119. rt_hw_serial_isr(&serial0, RT_SERIAL_EVENT_RX_IND);
  120. }
  121. if (__LL_UART_INT_ID_Get(uart->uart) != UART_INT_ID_RX_AVL)
  122. {
  123. /* clear interrupt */
  124. }
  125. /* leave interrupt */
  126. rt_interrupt_leave();
  127. }
  128. #endif /* BSP_USING_UART1 */
  129. #if defined(BSP_USING_UART1)
  130. /* UART2 device driver structure */
  131. static struct tae32_uart uart1;
  132. struct rt_serial_device serial1;
  133. void UART1_IRQHandler(void)
  134. {
  135. struct tae32_uart *uart;
  136. uart = &uart1;
  137. /* enter interrupt */
  138. rt_interrupt_enter();
  139. if (__LL_UART_INT_ID_Get(uart->uart) == UART_INT_ID_RX_AVL)
  140. {
  141. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  142. }
  143. if (__LL_UART_INT_ID_Get(uart->uart) != UART_INT_ID_RX_AVL)
  144. {
  145. /* clear interrupt */
  146. }
  147. /* leave interrupt */
  148. rt_interrupt_leave();
  149. }
  150. #endif /* BSP_USING_UART1 */
  151. int rt_hw_uart_init(void)
  152. {
  153. struct tae32_uart *uart;
  154. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  155. #ifdef BSP_USING_UART0
  156. uart = &uart0;
  157. uart->uart = UART0;
  158. uart->irq = UART0_IRQn;
  159. config.baud_rate = BAUD_RATE_115200;
  160. serial0.ops = &_uart_ops;
  161. serial0.config = config;
  162. uart_init();
  163. /* register UART1 device */
  164. rt_hw_serial_register(&serial0, "uart0",
  165. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  166. uart);
  167. #endif /* BSP_USING_UART0 */
  168. #ifdef BSP_USING_UART1
  169. uart = &uart1;
  170. uart->uart = UART1;
  171. uart->irq = UART1_IRQn;
  172. config.baud_rate = BAUD_RATE_115200;
  173. serial2.ops = &_uart_ops;
  174. serial2.config = config;
  175. /* register UART1 device */
  176. rt_hw_serial_register(&serial1, "uart1",
  177. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  178. uart);
  179. #endif /* BSP_USING_UART1 */
  180. return 0;
  181. }
  182. INIT_BOARD_EXPORT(rt_hw_uart_init);