1
0

drv_usart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485
  1. /*
  2. * Copyright (c) 2006-2023, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-08-20 Abbcc first version
  9. * 2022-12-26 luobeihai add apm32F0 serie MCU support
  10. */
  11. #include "board.h"
  12. #include "drv_usart.h"
  13. #ifdef RT_USING_SERIAL
  14. #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && \
  15. !defined(BSP_USING_UART3) && !defined(BSP_USING_UART4) && \
  16. !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6)
  17. #error "Please define at least one BSP_USING_UARTx"
  18. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  19. #endif
  20. struct apm32_usart
  21. {
  22. const char *name;
  23. USART_T *usartx;
  24. IRQn_Type irq_type;
  25. struct rt_serial_device serial;
  26. };
  27. enum
  28. {
  29. #ifdef BSP_USING_UART1
  30. UART1_INDEX,
  31. #endif
  32. #ifdef BSP_USING_UART2
  33. UART2_INDEX,
  34. #endif
  35. #ifdef BSP_USING_UART3
  36. UART3_INDEX,
  37. #endif
  38. #ifdef BSP_USING_UART4
  39. UART4_INDEX,
  40. #endif
  41. #ifdef BSP_USING_UART5
  42. UART5_INDEX,
  43. #endif
  44. #ifdef BSP_USING_UART6
  45. UART6_INDEX,
  46. #endif
  47. };
  48. static struct apm32_usart usart_config[] =
  49. {
  50. #ifdef BSP_USING_UART1
  51. {
  52. "uart1",
  53. USART1,
  54. USART1_IRQn,
  55. },
  56. #endif
  57. #ifdef BSP_USING_UART2
  58. {
  59. "uart2",
  60. USART2,
  61. USART2_IRQn,
  62. },
  63. #endif
  64. #if defined(SOC_SERIES_APM32F1) || defined(SOC_SERIES_APM32F4)
  65. #ifdef BSP_USING_UART3
  66. {
  67. "uart3",
  68. USART3,
  69. USART3_IRQn,
  70. },
  71. #endif
  72. #ifdef BSP_USING_UART4
  73. {
  74. "uart4",
  75. UART4,
  76. UART4_IRQn,
  77. },
  78. #endif
  79. #ifdef BSP_USING_UART5
  80. {
  81. "uart5",
  82. UART5,
  83. UART5_IRQn,
  84. },
  85. #endif
  86. #ifdef BSP_USING_UART6
  87. {
  88. "uart6",
  89. USART6,
  90. USART6_IRQn,
  91. },
  92. #endif
  93. #endif
  94. };
  95. static rt_err_t apm32_uart_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  96. {
  97. USART_Config_T USART_ConfigStruct;
  98. RT_ASSERT(serial != RT_NULL);
  99. RT_ASSERT(cfg != RT_NULL);
  100. struct apm32_usart *usart_instance = (struct apm32_usart *) serial->parent.user_data;
  101. apm32_usart_init();
  102. USART_ConfigStruct.baudRate = cfg->baud_rate;
  103. USART_ConfigStruct.mode = USART_MODE_TX_RX;
  104. USART_ConfigStruct.parity = USART_PARITY_NONE;
  105. #if defined(SOC_SERIES_APM32F0)
  106. switch (cfg->flowcontrol)
  107. {
  108. case RT_SERIAL_FLOWCONTROL_NONE:
  109. USART_ConfigStruct.hardwareFlowCtrl = USART_FLOW_CTRL_NONE;
  110. break;
  111. case RT_SERIAL_FLOWCONTROL_CTSRTS:
  112. USART_ConfigStruct.hardwareFlowCtrl = USART_FLOW_CTRL_RTS_CTS;
  113. break;
  114. default:
  115. USART_ConfigStruct.hardwareFlowCtrl = USART_FLOW_CTRL_NONE;
  116. break;
  117. }
  118. #else
  119. switch (cfg->flowcontrol)
  120. {
  121. case RT_SERIAL_FLOWCONTROL_NONE:
  122. USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
  123. break;
  124. case RT_SERIAL_FLOWCONTROL_CTSRTS:
  125. USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_RTS_CTS;
  126. break;
  127. default:
  128. USART_ConfigStruct.hardwareFlow = USART_HARDWARE_FLOW_NONE;
  129. break;
  130. }
  131. #endif
  132. switch (cfg->data_bits)
  133. {
  134. case DATA_BITS_8:
  135. if (cfg->parity == PARITY_ODD || cfg->parity == PARITY_EVEN)
  136. USART_ConfigStruct.wordLength = USART_WORD_LEN_9B;
  137. else
  138. USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
  139. break;
  140. case DATA_BITS_9:
  141. USART_ConfigStruct.wordLength = USART_WORD_LEN_9B;
  142. break;
  143. default:
  144. USART_ConfigStruct.wordLength = USART_WORD_LEN_8B;
  145. break;
  146. }
  147. switch (cfg->stop_bits)
  148. {
  149. case STOP_BITS_1:
  150. USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
  151. break;
  152. case STOP_BITS_2:
  153. USART_ConfigStruct.stopBits = USART_STOP_BIT_2;
  154. break;
  155. default:
  156. USART_ConfigStruct.stopBits = USART_STOP_BIT_1;
  157. break;
  158. }
  159. switch (cfg->parity)
  160. {
  161. case PARITY_NONE:
  162. USART_ConfigStruct.parity = USART_PARITY_NONE;
  163. break;
  164. case PARITY_ODD:
  165. USART_ConfigStruct.parity = USART_PARITY_ODD;
  166. break;
  167. case PARITY_EVEN:
  168. USART_ConfigStruct.parity = USART_PARITY_EVEN;
  169. break;
  170. default:
  171. USART_ConfigStruct.parity = USART_PARITY_NONE;
  172. break;
  173. }
  174. USART_Config(usart_instance->usartx, &USART_ConfigStruct);
  175. USART_Enable(usart_instance->usartx);
  176. return RT_EOK;
  177. }
  178. static rt_err_t apm32_uart_control(struct rt_serial_device *serial, int cmd, void *arg)
  179. {
  180. struct apm32_usart *usart;
  181. RT_ASSERT(serial != RT_NULL);
  182. usart = (struct apm32_usart *) serial->parent.user_data;
  183. RT_ASSERT(usart != RT_NULL);
  184. #if defined(SOC_SERIES_APM32F0)
  185. switch (cmd)
  186. {
  187. /* disable interrupt */
  188. case RT_DEVICE_CTRL_CLR_INT:
  189. /* disable rx irq */
  190. NVIC_DisableIRQRequest(usart->irq_type);
  191. /* disable interrupt */
  192. USART_DisableInterrupt(usart->usartx, USART_INT_RXBNEIE);
  193. break;
  194. /* enable interrupt */
  195. case RT_DEVICE_CTRL_SET_INT:
  196. /* enable rx irq */
  197. NVIC_EnableIRQRequest(usart->irq_type, 1);
  198. /* enable interrupt */
  199. USART_EnableInterrupt(usart->usartx, USART_INT_RXBNEIE);
  200. break;
  201. }
  202. #else
  203. switch (cmd)
  204. {
  205. /* disable interrupt */
  206. case RT_DEVICE_CTRL_CLR_INT:
  207. /* disable rx irq */
  208. NVIC_DisableIRQRequest(usart->irq_type);
  209. /* disable interrupt */
  210. USART_DisableInterrupt(usart->usartx, USART_INT_RXBNE);
  211. break;
  212. /* enable interrupt */
  213. case RT_DEVICE_CTRL_SET_INT:
  214. /* enable rx irq */
  215. NVIC_EnableIRQRequest(usart->irq_type, 1, 0);
  216. /* enable interrupt */
  217. USART_EnableInterrupt(usart->usartx, USART_INT_RXBNE);
  218. break;
  219. }
  220. #endif
  221. return RT_EOK;
  222. }
  223. static int apm32_uart_putc(struct rt_serial_device *serial, char c)
  224. {
  225. struct apm32_usart *usart;
  226. RT_ASSERT(serial != RT_NULL);
  227. usart = (struct apm32_usart *) serial->parent.user_data;
  228. RT_ASSERT(usart != RT_NULL);
  229. USART_TxData(usart->usartx, (uint8_t) c);
  230. while (USART_ReadStatusFlag(usart->usartx, USART_FLAG_TXC) == RESET);
  231. return 1;
  232. }
  233. static int apm32_uart_getc(struct rt_serial_device *serial)
  234. {
  235. int ch;
  236. struct apm32_usart *usart;
  237. RT_ASSERT(serial != RT_NULL);
  238. usart = (struct apm32_usart *) serial->parent.user_data;
  239. RT_ASSERT(usart != RT_NULL);
  240. ch = -1;
  241. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_RXBNE) != RESET)
  242. {
  243. ch = USART_RxData(usart->usartx);
  244. }
  245. return ch;
  246. }
  247. /**
  248. * Uart common interrupt process. This need add to usart ISR.
  249. *
  250. * @param serial serial device
  251. */
  252. static void usart_isr(struct rt_serial_device *serial)
  253. {
  254. struct apm32_usart *usart;
  255. RT_ASSERT(serial != RT_NULL);
  256. usart = (struct apm32_usart *) serial->parent.user_data;
  257. RT_ASSERT(usart != RT_NULL);
  258. /* UART in mode Receiver */
  259. #if defined(SOC_SERIES_APM32F0)
  260. if ((USART_ReadStatusFlag(usart->usartx, USART_FLAG_RXBNE) != RESET) &&
  261. (USART_ReadIntFlag(usart->usartx, USART_INT_FLAG_RXBNE) != RESET))
  262. #else
  263. if ((USART_ReadStatusFlag(usart->usartx, USART_FLAG_RXBNE) != RESET) &&
  264. (USART_ReadIntFlag(usart->usartx, USART_INT_RXBNE) != RESET))
  265. #endif
  266. {
  267. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  268. }
  269. else
  270. {
  271. #if defined(SOC_SERIES_APM32F0)
  272. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_OVRE) != RESET)
  273. {
  274. USART_ClearStatusFlag(usart->usartx, USART_FLAG_OVRE);
  275. }
  276. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_NEF) != RESET)
  277. {
  278. USART_ClearStatusFlag(usart->usartx, USART_FLAG_NEF);
  279. }
  280. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_FEF) != RESET)
  281. {
  282. USART_ClearStatusFlag(usart->usartx, USART_FLAG_FEF);
  283. }
  284. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_PEF) != RESET)
  285. {
  286. USART_ClearStatusFlag(usart->usartx, USART_FLAG_PEF);
  287. }
  288. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_CTSF) != RESET)
  289. {
  290. USART_ClearStatusFlag(usart->usartx, USART_FLAG_CTSF);
  291. }
  292. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_LBDF) != RESET)
  293. {
  294. USART_ClearStatusFlag(usart->usartx, USART_FLAG_LBDF);
  295. }
  296. #else
  297. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_OVRE) != RESET)
  298. {
  299. USART_ClearStatusFlag(usart->usartx, USART_FLAG_OVRE);
  300. }
  301. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_NE) != RESET)
  302. {
  303. USART_ClearStatusFlag(usart->usartx, USART_FLAG_NE);
  304. }
  305. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_FE) != RESET)
  306. {
  307. USART_ClearStatusFlag(usart->usartx, USART_FLAG_FE);
  308. }
  309. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_PE) != RESET)
  310. {
  311. USART_ClearStatusFlag(usart->usartx, USART_FLAG_PE);
  312. }
  313. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_CTS) != RESET)
  314. {
  315. USART_ClearStatusFlag(usart->usartx, USART_FLAG_CTS);
  316. }
  317. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_LBD) != RESET)
  318. {
  319. USART_ClearStatusFlag(usart->usartx, USART_FLAG_LBD);
  320. }
  321. #endif
  322. if (USART_ReadStatusFlag(usart->usartx, USART_FLAG_TXBE) != RESET)
  323. {
  324. USART_ClearStatusFlag(usart->usartx, USART_FLAG_TXBE);
  325. }
  326. }
  327. }
  328. #if defined(BSP_USING_UART1)
  329. void USART1_IRQHandler(void)
  330. {
  331. /* enter interrupt */
  332. rt_interrupt_enter();
  333. usart_isr(&(usart_config[UART1_INDEX].serial));
  334. /* leave interrupt */
  335. rt_interrupt_leave();
  336. }
  337. #endif /* BSP_USING_UART1 */
  338. #if defined(BSP_USING_UART2)
  339. void USART2_IRQHandler(void)
  340. {
  341. /* enter interrupt */
  342. rt_interrupt_enter();
  343. usart_isr(&(usart_config[UART2_INDEX].serial));
  344. /* leave interrupt */
  345. rt_interrupt_leave();
  346. }
  347. #endif /* BSP_USING_UART2 */
  348. #if defined(BSP_USING_UART3)
  349. void USART3_IRQHandler(void)
  350. {
  351. /* enter interrupt */
  352. rt_interrupt_enter();
  353. usart_isr(&(usart_config[UART3_INDEX].serial));
  354. /* leave interrupt */
  355. rt_interrupt_leave();
  356. }
  357. #endif /* BSP_USING_UART3 */
  358. #if defined(BSP_USING_UART4)
  359. void UART4_IRQHandler(void)
  360. {
  361. /* enter interrupt */
  362. rt_interrupt_enter();
  363. usart_isr(&(usart_config[UART4_INDEX].serial));
  364. /* leave interrupt */
  365. rt_interrupt_leave();
  366. }
  367. #endif /* BSP_USING_UART4 */
  368. #if defined(BSP_USING_UART5)
  369. void UART5_IRQHandler(void)
  370. {
  371. /* enter interrupt */
  372. rt_interrupt_enter();
  373. usart_isr(&(usart_config[UART5_INDEX].serial));
  374. /* leave interrupt */
  375. rt_interrupt_leave();
  376. }
  377. #endif /* BSP_USING_UART5 */
  378. #if defined(BSP_USING_UART6)
  379. void USART6_IRQHandler(void)
  380. {
  381. /* enter interrupt */
  382. rt_interrupt_enter();
  383. usart_isr(&(usart_config[UART6_INDEX].serial));
  384. /* leave interrupt */
  385. rt_interrupt_leave();
  386. }
  387. #endif /* BSP_USING_UART6 */
  388. static const struct rt_uart_ops apm32_usart_ops =
  389. {
  390. .configure = apm32_uart_configure,
  391. .control = apm32_uart_control,
  392. .putc = apm32_uart_putc,
  393. .getc = apm32_uart_getc,
  394. .dma_transmit = RT_NULL
  395. };
  396. int rt_hw_usart_init(void)
  397. {
  398. rt_size_t obj_num;
  399. int index;
  400. obj_num = sizeof(usart_config) / sizeof(struct apm32_usart);
  401. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  402. rt_err_t result = 0;
  403. for (index = 0; index < obj_num; index++)
  404. {
  405. usart_config[index].serial.ops = &apm32_usart_ops;
  406. usart_config[index].serial.config = config;
  407. /* register USART device */
  408. result = rt_hw_serial_register(&usart_config[index].serial,
  409. usart_config[index].name,
  410. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX
  411. | RT_DEVICE_FLAG_INT_TX, &usart_config[index]);
  412. RT_ASSERT(result == RT_EOK);
  413. }
  414. return result;
  415. }
  416. #endif /* RT_USING_SERIAL */