drv_uart.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  1. /*
  2. * Copyright (c) 2022-2025, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2025-01-22 chasel first version
  9. */
  10. #include "board.h"
  11. #include <mm32_device.h>
  12. #include <rtdevice.h>
  13. #include "drv_uart.h"
  14. #include <hal_gpio.h>
  15. #include <hal_uart.h>
  16. #include <hal_rcc.h>
  17. #include <hal_misc.h>
  18. /* uart driver */
  19. struct mm32_uart
  20. {
  21. UART_TypeDef *uart;
  22. IRQn_Type irq;
  23. };
  24. static rt_err_t mm32_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  25. {
  26. struct mm32_uart *uart;
  27. UART_InitTypeDef UART_InitStructure;
  28. RT_ASSERT(serial != RT_NULL);
  29. RT_ASSERT(cfg != RT_NULL);
  30. uart = (struct mm32_uart *)serial->parent.user_data;
  31. UART_InitStructure.BaudRate = cfg->baud_rate;
  32. if (cfg->data_bits == DATA_BITS_8)
  33. UART_InitStructure.WordLength = UART_WordLength_8b;
  34. if (cfg->stop_bits == STOP_BITS_1)
  35. UART_InitStructure.StopBits = UART_StopBits_1;
  36. else if (cfg->stop_bits == STOP_BITS_2)
  37. UART_InitStructure.StopBits = UART_StopBits_2;
  38. UART_InitStructure.Parity = UART_Parity_No;
  39. UART_InitStructure.HWFlowControl = UART_HWFlowControl_None;
  40. UART_InitStructure.Mode = UART_Mode_Rx | UART_Mode_Tx;
  41. UART_Init(uart->uart, &UART_InitStructure);
  42. /* Enable UART */
  43. UART_Cmd(uart->uart, ENABLE);
  44. return RT_EOK;
  45. }
  46. static rt_err_t mm32_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  47. {
  48. struct mm32_uart *uart;
  49. RT_ASSERT(serial != RT_NULL);
  50. uart = (struct mm32_uart *)serial->parent.user_data;
  51. switch (cmd)
  52. {
  53. case RT_DEVICE_CTRL_CLR_INT:
  54. /* disable rx irq */
  55. NVIC_DisableIRQ(uart->irq);
  56. UART_ITConfig(uart->uart, UART_IT_RX, DISABLE);
  57. break;
  58. case RT_DEVICE_CTRL_SET_INT:
  59. /* enable rx irq */
  60. NVIC_EnableIRQ(uart->irq);
  61. /* enable interrupt */
  62. UART_ITConfig(uart->uart, UART_IT_RX, ENABLE);
  63. break;
  64. }
  65. return RT_EOK;
  66. }
  67. static int mm32_uart_putc(struct rt_serial_device *serial, char c)
  68. {
  69. struct mm32_uart *uart;
  70. RT_ASSERT(serial != RT_NULL);
  71. uart = (struct mm32_uart *)serial->parent.user_data;
  72. while ((uart->uart->CSR & UART_CSR_TXC_Msk) == 0)
  73. ;
  74. uart->uart->TDR = c;
  75. return 1;
  76. }
  77. static int mm32_uart_getc(struct rt_serial_device *serial)
  78. {
  79. int ch;
  80. struct mm32_uart *uart;
  81. RT_ASSERT(serial != RT_NULL);
  82. uart = (struct mm32_uart *)serial->parent.user_data;
  83. ch = -1;
  84. if (uart->uart->CSR & UART_FLAG_RXAVL)
  85. {
  86. ch = uart->uart->RDR & 0xff;
  87. }
  88. return ch;
  89. }
  90. static const struct rt_uart_ops mm32_uart_ops =
  91. {
  92. mm32_uart_configure,
  93. mm32_uart_control,
  94. mm32_uart_putc,
  95. mm32_uart_getc,
  96. };
  97. #if defined(BSP_USING_UART1)
  98. /* UART1 device driver structure */
  99. static struct mm32_uart uart1;
  100. struct rt_serial_device serial1;
  101. void UART1_IRQHandler(void)
  102. {
  103. struct mm32_uart *uart;
  104. uart = &uart1;
  105. /* enter interrupt */
  106. rt_interrupt_enter();
  107. if (UART_GetITStatus(uart->uart, UART_IT_RX) != RESET)
  108. {
  109. UART_ClearITPendingBit(uart->uart, UART_IT_RX);
  110. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  111. }
  112. if (UART_GetITStatus(uart->uart, UART_IT_TX) != RESET)
  113. {
  114. /* clear interrupt */
  115. UART_ClearITPendingBit(uart->uart, UART_IT_TX);
  116. }
  117. /* leave interrupt */
  118. rt_interrupt_leave();
  119. }
  120. #endif /* BSP_USING_UART1 */
  121. #if defined(BSP_USING_UART2)
  122. /* UART2 device driver structure */
  123. static struct mm32_uart uart2;
  124. struct rt_serial_device serial2;
  125. void UART2_IRQHandler(void)
  126. {
  127. struct mm32_uart *uart;
  128. uart = &uart2;
  129. /* enter interrupt */
  130. rt_interrupt_enter();
  131. if (UART_GetITStatus(uart->uart, UART_IT_RX) != RESET)
  132. {
  133. UART_ClearITPendingBit(uart->uart, UART_IT_RX);
  134. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  135. }
  136. if (UART_GetITStatus(uart->uart, UART_IT_TX) != RESET)
  137. {
  138. /* clear interrupt */
  139. UART_ClearITPendingBit(uart->uart, UART_IT_TX);
  140. }
  141. /* leave interrupt */
  142. rt_interrupt_leave();
  143. }
  144. #endif /* BSP_USING_UART2 */
  145. #if defined(BSP_USING_UART3)
  146. /* UART3 device driver structure */
  147. static struct mm32_uart uart3;
  148. struct rt_serial_device serial3;
  149. void UART3_IRQHandler(void)
  150. {
  151. struct mm32_uart *uart;
  152. uart = &uart3;
  153. /* enter interrupt */
  154. rt_interrupt_enter();
  155. if (UART_GetITStatus(uart->uart, UART_IT_RX) != RESET)
  156. {
  157. UART_ClearITPendingBit(uart->uart, UART_IT_RX);
  158. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  159. }
  160. if (UART_GetITStatus(uart->uart, UART_IT_TX) != RESET)
  161. {
  162. /* clear interrupt */
  163. UART_ClearITPendingBit(uart->uart, UART_IT_TX);
  164. }
  165. /* leave interrupt */
  166. rt_interrupt_leave();
  167. }
  168. #endif /* BSP_USING_UART3 */
  169. int rt_hw_uart_init(void)
  170. {
  171. struct mm32_uart *uart;
  172. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  173. #ifdef BSP_USING_UART1
  174. mm32_msp_uart_init((void *)UART1);
  175. uart = &uart1;
  176. uart->uart = UART1;
  177. uart->irq = UART1_IRQn;
  178. config.baud_rate = BAUD_RATE_115200;
  179. serial1.ops = &mm32_uart_ops;
  180. serial1.config = config;
  181. /* register UART1 device */
  182. rt_hw_serial_register(&serial1, "uart1",
  183. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  184. uart);
  185. #endif /* BSP_USING_UART1 */
  186. #ifdef BSP_USING_UART2
  187. mm32_msp_uart_init((void *)UART2);
  188. uart = &uart2;
  189. uart->uart = UART2;
  190. uart->irq = UART2_IRQn;
  191. config.baud_rate = BAUD_RATE_115200;
  192. serial2.ops = &mm32_uart_ops;
  193. serial2.config = config;
  194. /* register UART2 device */
  195. rt_hw_serial_register(&serial2, "uart2",
  196. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  197. uart);
  198. #endif /* BSP_USING_UART2 */
  199. #ifdef BSP_USING_UART3
  200. mm32_msp_uart_init((void *)UART3);
  201. uart = &uart3;
  202. uart->uart = UART3;
  203. uart->irq = UART3_IRQn;
  204. config.baud_rate = BAUD_RATE_115200;
  205. serial3.ops = &mm32_uart_ops;
  206. serial3.config = config;
  207. /* register UART2 device */
  208. rt_hw_serial_register(&serial3, "uart3",
  209. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  210. uart);
  211. #endif /* BSP_USING_UART3 */
  212. return 0;
  213. }