drv_uart.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289
  1. ///*
  2. // * Copyright (c) 2006-2024, RT-Thread Development Team
  3. // *
  4. // * SPDX-License-Identifier: Apache-2.0
  5. // *
  6. // * Change Logs:
  7. // * Date Author Notes
  8. // * 2019-07-15 yandld The first version for MCXN
  9. // */
  10. #include <rtthread.h>
  11. #include "drv_uart.h"
  12. #include "fsl_lpuart.h"
  13. #include "fsl_common.h"
  14. #ifdef RT_USING_SERIAL
  15. #include <rtdevice.h>
  16. /* lpc uart driver */
  17. struct mcx_uart
  18. {
  19. struct rt_serial_device *serial; /* Select serial device */
  20. LPUART_Type *uart_base; /* serial base */
  21. IRQn_Type irqn; /* serial interrupt */
  22. clock_name_t clock_src; /* serial RTC */
  23. clock_attach_id_t clock_attach_id; /* RTC ID */
  24. clock_ip_name_t clock_ip_name; /* serial clock name */
  25. clock_div_name_t clock_div_name; /* serial clock div */
  26. char *device_name; /* serial device name */
  27. };
  28. static void uart_isr(struct rt_serial_device *serial);
  29. #if defined(BSP_USING_UART2)
  30. struct rt_serial_device serial2;
  31. void LP_FLEXCOMM2_IRQHandler(void)
  32. {
  33. uart_isr(&serial2); /* Serial interrupt handling function */
  34. }
  35. #endif /* BSP_USING_UART2 */
  36. #if defined(BSP_USING_UART4)
  37. struct rt_serial_device serial4;
  38. void LP_FLEXCOMM4_IRQHandler(void)
  39. {
  40. uart_isr(&serial4); /* Serial interrupt handling function */
  41. }
  42. #endif /* BSP_USING_UART4 */
  43. #if defined(BSP_USING_UART5)
  44. struct rt_serial_device serial5;
  45. void LP_FLEXCOMM5_IRQHandler(void)
  46. {
  47. uart_isr(&serial5); /* Serial interrupt handling function */
  48. }
  49. #endif /* BSP_USING_UART5 */
  50. #if defined(BSP_USING_UART6) /* same UART4 */
  51. struct rt_serial_device serial6;
  52. void LP_FLEXCOMM6_IRQHandler(void)
  53. {
  54. uart_isr(&serial6);
  55. }
  56. #endif /* BSP_USING_UART6 */
  57. static const struct mcx_uart uarts[] = /* Initializes the above structure */
  58. {
  59. #ifdef BSP_USING_UART2
  60. {
  61. &serial2,
  62. LPUART2,
  63. LP_FLEXCOMM2_IRQn,
  64. kCLOCK_Fro12M,
  65. kFRO12M_to_FLEXCOMM2,
  66. kCLOCK_LPFlexComm2,
  67. kCLOCK_DivFlexcom2Clk,
  68. "uart2",
  69. },
  70. #endif
  71. #ifdef BSP_USING_UART4
  72. {
  73. &serial4,
  74. LPUART4,
  75. LP_FLEXCOMM4_IRQn,
  76. kCLOCK_Fro12M,
  77. kFRO12M_to_FLEXCOMM4,
  78. kCLOCK_LPFlexComm4,
  79. kCLOCK_DivFlexcom4Clk,
  80. "uart4",
  81. },
  82. #endif
  83. #ifdef BSP_USING_UART5
  84. {
  85. &serial5,
  86. LPUART5,
  87. LP_FLEXCOMM5_IRQn,
  88. kCLOCK_Fro12M,
  89. kFRO12M_to_FLEXCOMM5,
  90. kCLOCK_LPFlexComm5,
  91. kCLOCK_DivFlexcom5Clk,
  92. "uart5",
  93. },
  94. #endif
  95. #ifdef BSP_USING_UART6
  96. {
  97. &serial6,
  98. LPUART6,
  99. LP_FLEXCOMM6_IRQn,
  100. kCLOCK_Fro12M,
  101. kFRO12M_to_FLEXCOMM6,
  102. kCLOCK_LPFlexComm6,
  103. kCLOCK_DivFlexcom6Clk,
  104. "uart6",
  105. },
  106. #endif
  107. };
  108. /**
  109. * Configuring the serial port Module.
  110. *
  111. * @param serial device
  112. * @param Configure the serial port configuration structure to set the TX RX features
  113. */
  114. static rt_err_t mcx_configure(struct rt_serial_device *serial, struct serial_configure *cfg) /* Configuring the serial port Module */
  115. {
  116. struct mcx_uart *uart; /* Serial port hardware structure, calling the structure initialized above */
  117. lpuart_config_t config;/* It contains basic configuration parameters of the serial port, such as baud rate, data bit, stop bit, and parity check */
  118. RT_ASSERT(serial != RT_NULL); /* assert */
  119. RT_ASSERT(cfg != RT_NULL);
  120. uart = (struct mcx_uart *)serial->parent.user_data;
  121. CLOCK_SetClkDiv(uart->clock_div_name, 1u);
  122. CLOCK_AttachClk(uart->clock_attach_id);
  123. CLOCK_EnableClock(uart->clock_ip_name);
  124. LPUART_GetDefaultConfig(&config);
  125. config.baudRate_Bps = cfg->baud_rate;
  126. switch (cfg->data_bits)
  127. {
  128. case DATA_BITS_7:
  129. config.dataBitsCount = kLPUART_SevenDataBits;
  130. break;
  131. default:
  132. config.dataBitsCount = kLPUART_EightDataBits;
  133. break;
  134. }
  135. config.enableTx = true;
  136. config.enableRx = true;
  137. LPUART_Init(uart->uart_base, &config, CLOCK_GetFreq(uart->clock_src));
  138. return RT_EOK;
  139. }
  140. /**
  141. * Serial Control Function.
  142. *
  143. * @param serial device struct
  144. * @param control Cmd
  145. * @param Parameters passed to the control command
  146. */
  147. static rt_err_t mcx_control(struct rt_serial_device *serial, int cmd, void *arg)/* serial control */
  148. {
  149. struct mcx_uart *uart = (struct mcx_uart *)serial->parent.user_data; /* Convert the type to struct mcx_uart */
  150. RT_ASSERT(uart != RT_NULL); /* Assert */
  151. switch (cmd)
  152. {
  153. case RT_DEVICE_CTRL_CLR_INT:
  154. /* disable rx irq */
  155. LPUART_DisableInterrupts(uart->uart_base, kLPUART_RxDataRegFullInterruptEnable);
  156. DisableIRQ(uart->irqn);
  157. break;
  158. case RT_DEVICE_CTRL_SET_INT:
  159. /* enable rx irq */
  160. LPUART_EnableInterrupts(uart->uart_base, kLPUART_RxDataRegFullInterruptEnable);
  161. EnableIRQ(uart->irqn);
  162. break;
  163. }
  164. return RT_EOK;
  165. }
  166. /**
  167. * Sends a single character function to a serial device.
  168. *
  169. * @param serial device struct
  170. * @param The serial port character you want to send
  171. */
  172. static int mcx_putc(struct rt_serial_device *serial, char ch)
  173. {
  174. struct mcx_uart *uart = (struct mcx_uart *)serial->parent.user_data;
  175. while(!(kLPUART_TxDataRegEmptyFlag & LPUART_GetStatusFlags(uart->uart_base)));
  176. LPUART_WriteByte(uart->uart_base, ch);
  177. return 1;
  178. }
  179. static int mcx_getc(struct rt_serial_device *serial)
  180. {
  181. struct mcx_uart *uart = (struct mcx_uart *)serial->parent.user_data;
  182. if (kLPUART_RxDataRegFullInterruptEnable & LPUART_GetStatusFlags(uart->uart_base))
  183. /* Check whether the receive cache is full and read the status flag bit of the status register
  184. This flag is read, indicating that there is data in the cache and can be read */
  185. {
  186. return LPUART_ReadByte(uart->uart_base);
  187. }
  188. else
  189. {
  190. return -1;
  191. }
  192. }
  193. /**
  194. * Uart common interrupt process. This need add to uart ISR.
  195. *
  196. * @param serial serial device
  197. */
  198. static void uart_isr(struct rt_serial_device *serial)
  199. {
  200. struct mcx_uart *uart; /* Create a serial port hardware structure variable */
  201. RT_ASSERT(serial != RT_NULL);
  202. uart = (struct mcx_uart *) serial->parent.user_data;
  203. RT_ASSERT(uart != RT_NULL);
  204. /* enter interrupt */
  205. rt_interrupt_enter();
  206. /* UART in mode Receiver -------------------------------------------------*/
  207. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  208. /* leave interrupt */
  209. rt_interrupt_leave();
  210. }
  211. static const struct rt_uart_ops mcx_uart_ops =
  212. {
  213. mcx_configure,
  214. mcx_control,
  215. mcx_putc,
  216. mcx_getc,
  217. };
  218. int rt_hw_uart_init(void)
  219. {
  220. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT; /* initial struct [115200,8,1,NONE] */
  221. int i;
  222. /* Registers loops for multiple serial devices */
  223. for (i = 0; i < sizeof(uarts) / sizeof(uarts[0]); i++) /* sizeof(uarts) / sizeof(uarts[0] : Calculate the number of struct mcx_uart serial ports */
  224. {
  225. uarts[i].serial->ops = &mcx_uart_ops;
  226. uarts[i].serial->config = config;
  227. /**
  228. * register UART device.
  229. *
  230. * @param Indicates the structure of the serial port device to be registered
  231. * @param device name
  232. * @param Flag bit mask
  233. * @param A pointer to the current device that is used as user private data at registration
  234. */
  235. rt_hw_serial_register(uarts[i].serial, uarts[i].device_name, RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX, (void *)&uarts[i]);
  236. }
  237. return 0;
  238. }
  239. INIT_BOARD_EXPORT(rt_hw_uart_init); /* RT-Thread Automatic initialization mechanism */
  240. #endif /*BSP_USING_SERIAL */