drv_usart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  1. /*
  2. * Copyright (c) 2006-2022, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2022-02-22 airm2m first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include "board.h"
  13. #include "drv_usart.h"
  14. #ifdef RT_USING_SERIAL
  15. #define LOG_TAG "drv.usart"
  16. #include <drv_log.h>
  17. #if !defined(BSP_USING_UART1) && !defined(BSP_USING_UART2) && !defined(BSP_USING_UART3) && \
  18. !defined(BSP_USING_UART4) && !defined(BSP_USING_UART5) && !defined(BSP_USING_UART6) && \
  19. !defined(BSP_USING_UART7) && !defined(BSP_USING_UART8)
  20. #error "Please define at least one BSP_USING_UARTx"
  21. /* this driver can be disabled at menuconfig -> RT-Thread Components -> Device Drivers */
  22. #endif
  23. /* air32 config class */
  24. struct air32_uart_config
  25. {
  26. const char *name;
  27. USART_TypeDef *Instance;
  28. IRQn_Type irq_type;
  29. };
  30. /* air32 uart dirver class */
  31. struct air32_uart
  32. {
  33. USART_InitTypeDef Init;
  34. struct air32_uart_config *config;
  35. struct rt_serial_device serial;
  36. };
  37. #if defined(BSP_USING_UART1)
  38. #ifndef UART1_CONFIG
  39. #define UART1_CONFIG \
  40. { \
  41. .name = "uart1", \
  42. .Instance = USART1, \
  43. .irq_type = USART1_IRQn, \
  44. }
  45. #endif /* UART1_CONFIG */
  46. #endif /* BSP_USING_UART1 */
  47. #if defined(BSP_USING_UART2)
  48. #ifndef UART2_CONFIG
  49. #define UART2_CONFIG \
  50. { \
  51. .name = "uart2", \
  52. .Instance = USART2, \
  53. .irq_type = USART2_IRQn, \
  54. }
  55. #endif /* UART2_CONFIG */
  56. #endif /* BSP_USING_UART2 */
  57. #if defined(BSP_USING_UART3)
  58. #ifndef UART3_CONFIG
  59. #define UART3_CONFIG \
  60. { \
  61. .name = "uart3", \
  62. .Instance = USART3, \
  63. .irq_type = USART3_IRQn, \
  64. }
  65. #endif /* UART3_CONFIG */
  66. #endif /* BSP_USING_UART3 */
  67. enum
  68. {
  69. #ifdef BSP_USING_UART1
  70. UART1_INDEX,
  71. #endif
  72. #ifdef BSP_USING_UART2
  73. UART2_INDEX,
  74. #endif
  75. #ifdef BSP_USING_UART3
  76. UART3_INDEX,
  77. #endif
  78. #ifdef BSP_USING_UART4
  79. UART4_INDEX,
  80. #endif
  81. #ifdef BSP_USING_UART5
  82. UART5_INDEX,
  83. #endif
  84. #ifdef BSP_USING_UART6
  85. UART6_INDEX,
  86. #endif
  87. #ifdef BSP_USING_UART7
  88. UART7_INDEX,
  89. #endif
  90. #ifdef BSP_USING_UART8
  91. UART8_INDEX,
  92. #endif
  93. };
  94. static struct air32_uart_config uart_config[] =
  95. {
  96. #ifdef BSP_USING_UART1
  97. UART1_CONFIG,
  98. #endif
  99. #ifdef BSP_USING_UART2
  100. UART2_CONFIG,
  101. #endif
  102. #ifdef BSP_USING_UART3
  103. UART3_CONFIG,
  104. #endif
  105. #ifdef BSP_USING_UART4
  106. UART4_CONFIG,
  107. #endif
  108. #ifdef BSP_USING_UART5
  109. UART5_CONFIG,
  110. #endif
  111. #ifdef BSP_USING_UART6
  112. UART6_CONFIG,
  113. #endif
  114. #ifdef BSP_USING_UART7
  115. UART7_CONFIG,
  116. #endif
  117. #ifdef BSP_USING_UART8
  118. UART8_CONFIG,
  119. #endif
  120. };
  121. static struct air32_uart uart_obj[sizeof(uart_config) / sizeof(uart_config[0])] = {0};
  122. static rt_err_t air32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  123. {
  124. struct air32_uart *uart;
  125. RT_ASSERT(serial != RT_NULL);
  126. RT_ASSERT(cfg != RT_NULL);
  127. uart = rt_container_of(serial, struct air32_uart, serial);
  128. air32_usart_clock_and_io_init(uart->config->Instance);
  129. USART_StructInit(&uart->Init);
  130. uart->Init.USART_BaudRate = cfg->baud_rate;
  131. uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  132. uart->Init.USART_Mode = USART_Mode_Rx | USART_Mode_Tx;
  133. switch (cfg->flowcontrol)
  134. {
  135. case RT_SERIAL_FLOWCONTROL_NONE:
  136. uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  137. break;
  138. case RT_SERIAL_FLOWCONTROL_CTSRTS:
  139. uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_RTS_CTS;
  140. break;
  141. default:
  142. uart->Init.USART_HardwareFlowControl = USART_HardwareFlowControl_None;
  143. break;
  144. }
  145. switch (cfg->data_bits)
  146. {
  147. case DATA_BITS_8:
  148. if (cfg->parity == PARITY_ODD || cfg->parity == PARITY_EVEN)
  149. uart->Init.USART_WordLength = USART_WordLength_9b;
  150. else
  151. uart->Init.USART_WordLength = USART_WordLength_8b;
  152. break;
  153. case DATA_BITS_9:
  154. uart->Init.USART_WordLength = USART_WordLength_9b;
  155. break;
  156. default:
  157. uart->Init.USART_WordLength = USART_WordLength_8b;
  158. break;
  159. }
  160. switch (cfg->stop_bits)
  161. {
  162. case STOP_BITS_1:
  163. uart->Init.USART_StopBits = USART_StopBits_1;
  164. break;
  165. case STOP_BITS_2:
  166. uart->Init.USART_StopBits = USART_StopBits_2;
  167. break;
  168. default:
  169. uart->Init.USART_StopBits = USART_StopBits_1;
  170. break;
  171. }
  172. switch (cfg->parity)
  173. {
  174. case PARITY_NONE:
  175. uart->Init.USART_Parity = USART_Parity_No;
  176. break;
  177. case PARITY_ODD:
  178. uart->Init.USART_Parity = USART_Parity_Odd;
  179. break;
  180. case PARITY_EVEN:
  181. uart->Init.USART_Parity = USART_Parity_Even;
  182. break;
  183. default:
  184. uart->Init.USART_Parity = USART_Parity_No;
  185. break;
  186. }
  187. USART_Init(uart->config->Instance, &uart->Init);
  188. USART_Cmd(uart->config->Instance, ENABLE);
  189. return RT_EOK;
  190. }
  191. static rt_err_t air32_control(struct rt_serial_device *serial, int cmd, void *arg)
  192. {
  193. struct air32_uart *uart;
  194. NVIC_InitTypeDef NVIC_InitStruct;
  195. RT_ASSERT(serial != RT_NULL);
  196. uart = rt_container_of(serial, struct air32_uart, serial);
  197. NVIC_InitStruct.NVIC_IRQChannel = uart->config->irq_type;
  198. NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
  199. NVIC_InitStruct.NVIC_IRQChannelSubPriority = 1;
  200. switch (cmd)
  201. {
  202. /* disable interrupt */
  203. case RT_DEVICE_CTRL_CLR_INT:
  204. /* disable rx irq */
  205. // NVIC_DisableIRQ(uart->config->irq_type);
  206. NVIC_InitStruct.NVIC_IRQChannelCmd = DISABLE;
  207. NVIC_Init(&NVIC_InitStruct);
  208. /* disable interrupt */
  209. USART_ITConfig(uart->config->Instance,USART_IT_RXNE,DISABLE);
  210. break;
  211. /* enable interrupt */
  212. case RT_DEVICE_CTRL_SET_INT:
  213. /* enable rx irq */
  214. // NVIC_EnableIRQ(uart->config->irq_type);
  215. NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
  216. NVIC_Init(&NVIC_InitStruct);
  217. /* enable interrupt */
  218. USART_ITConfig(uart->config->Instance, USART_IT_RXNE,ENABLE);
  219. break;
  220. }
  221. return RT_EOK;
  222. }
  223. static int air32_putc(struct rt_serial_device *serial, char c)
  224. {
  225. struct air32_uart *uart;
  226. RT_ASSERT(serial != RT_NULL);
  227. uart = rt_container_of(serial, struct air32_uart, serial);
  228. while (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_TXE) == RESET);
  229. USART_SendData(uart->config->Instance, (uint8_t)c);
  230. return 1;
  231. }
  232. static int air32_getc(struct rt_serial_device *serial)
  233. {
  234. int ch;
  235. struct air32_uart *uart;
  236. RT_ASSERT(serial != RT_NULL);
  237. uart = rt_container_of(serial, struct air32_uart, serial);
  238. ch = -1;
  239. if (RESET != USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXNE))
  240. {
  241. ch = USART_ReceiveData(uart->config->Instance) & 0xff;
  242. }
  243. return ch;
  244. }
  245. static rt_ssize_t air32_dma_transmit(struct rt_serial_device *serial, rt_uint8_t *buf, rt_size_t size, int direction)
  246. {
  247. return 0;
  248. }
  249. /**
  250. * Uart common interrupt process. This need add to uart ISR.
  251. *
  252. * @param serial serial device
  253. */
  254. static void uart_isr(struct rt_serial_device *serial)
  255. {
  256. struct air32_uart *uart;
  257. RT_ASSERT(serial != RT_NULL);
  258. uart = rt_container_of(serial, struct air32_uart, serial);
  259. /* UART in mode Receiver -------------------------------------------------*/
  260. if ((USART_GetITStatus(uart->config->Instance, USART_IT_RXNE) != RESET) && (RESET != USART_GetFlagStatus(uart->config->Instance, USART_FLAG_RXNE)))
  261. {
  262. rt_hw_serial_isr(serial, RT_SERIAL_EVENT_RX_IND);
  263. USART_ClearITPendingBit(uart->config->Instance, USART_IT_RXNE);
  264. USART_ClearFlag(uart->config->Instance, USART_FLAG_RXNE);
  265. }
  266. else
  267. {
  268. if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_CTS) != RESET)
  269. {
  270. USART_ClearFlag(uart->config->Instance, USART_FLAG_CTS);
  271. }
  272. if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_LBD) != RESET)
  273. {
  274. USART_ClearFlag(uart->config->Instance, USART_FLAG_LBD);
  275. }
  276. if (USART_GetFlagStatus(uart->config->Instance, USART_FLAG_TC) != RESET)
  277. {
  278. USART_ClearFlag(uart->config->Instance, USART_FLAG_TC);
  279. }
  280. }
  281. }
  282. #if defined(BSP_USING_UART1)
  283. void USART1_IRQHandler(void)
  284. {
  285. /* enter interrupt */
  286. rt_interrupt_enter();
  287. uart_isr(&(uart_obj[UART1_INDEX].serial));
  288. /* leave interrupt */
  289. rt_interrupt_leave();
  290. }
  291. #endif /* BSP_USING_UART1 */
  292. #if defined(BSP_USING_UART2)
  293. void USART2_IRQHandler(void)
  294. {
  295. /* enter interrupt */
  296. rt_interrupt_enter();
  297. uart_isr(&(uart_obj[UART2_INDEX].serial));
  298. /* leave interrupt */
  299. rt_interrupt_leave();
  300. }
  301. #endif /* BSP_USING_UART2 */
  302. #if defined(BSP_USING_UART3)
  303. void USART3_IRQHandler(void)
  304. {
  305. /* enter interrupt */
  306. rt_interrupt_enter();
  307. uart_isr(&(uart_obj[UART3_INDEX].serial));
  308. /* leave interrupt */
  309. rt_interrupt_leave();
  310. }
  311. #endif /* BSP_USING_UART3*/
  312. #if defined(BSP_USING_UART4)
  313. void UART4_IRQHandler(void)
  314. {
  315. /* enter interrupt */
  316. rt_interrupt_enter();
  317. uart_isr(&(uart_obj[UART4_INDEX].serial));
  318. /* leave interrupt */
  319. rt_interrupt_leave();
  320. }
  321. #endif /* BSP_USING_UART4*/
  322. #if defined(BSP_USING_UART5)
  323. void UART5_IRQHandler(void)
  324. {
  325. /* enter interrupt */
  326. rt_interrupt_enter();
  327. uart_isr(&(uart_obj[UART5_INDEX].serial));
  328. /* leave interrupt */
  329. rt_interrupt_leave();
  330. }
  331. #endif /* BSP_USING_UART5*/
  332. #if defined(BSP_USING_UART6)
  333. void USART6_IRQHandler(void)
  334. {
  335. /* enter interrupt */
  336. rt_interrupt_enter();
  337. uart_isr(&(uart_obj[UART6_INDEX].serial));
  338. /* leave interrupt */
  339. rt_interrupt_leave();
  340. }
  341. #endif /* BSP_USING_UART6*/
  342. #if defined(BSP_USING_UART7)
  343. void UART7_IRQHandler(void)
  344. {
  345. /* enter interrupt */
  346. rt_interrupt_enter();
  347. uart_isr(&(uart_obj[UART7_INDEX].serial));
  348. /* leave interrupt */
  349. rt_interrupt_leave();
  350. }
  351. #endif /* BSP_USING_UART7*/
  352. #if defined(BSP_USING_UART8)
  353. void UART8_IRQHandler(void)
  354. {
  355. /* enter interrupt */
  356. rt_interrupt_enter();
  357. uart_isr(&(uart_obj[UART8_INDEX].serial));
  358. /* leave interrupt */
  359. rt_interrupt_leave();
  360. }
  361. #endif /* BSP_USING_UART8*/
  362. static const struct rt_uart_ops air32_uart_ops =
  363. {
  364. .configure = air32_configure,
  365. .control = air32_control,
  366. .putc = air32_putc,
  367. .getc = air32_getc,
  368. .dma_transmit = air32_dma_transmit
  369. };
  370. int rt_hw_usart_init(void)
  371. {
  372. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  373. rt_err_t result = 0;
  374. for (rt_size_t i = 0; i < sizeof(uart_obj) / sizeof(struct air32_uart); i++)
  375. {
  376. /* init UART object */
  377. uart_obj[i].config = &uart_config[i];
  378. uart_obj[i].serial.ops = &air32_uart_ops;
  379. uart_obj[i].serial.config = config;
  380. /* register UART device */
  381. result = rt_hw_serial_register(&uart_obj[i].serial, uart_obj[i].config->name,
  382. RT_DEVICE_FLAG_RDWR
  383. | RT_DEVICE_FLAG_INT_RX
  384. | RT_DEVICE_FLAG_INT_TX
  385. , RT_NULL);
  386. RT_ASSERT(result == RT_EOK);
  387. }
  388. return result;
  389. }
  390. #endif /* RT_USING_SERIAL */