drv_usart.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432
  1. /*
  2. * File : drv_usart.c
  3. * This file is part of RT-Thread RTOS
  4. * COPYRIGHT (C) 2015, RT-Thread Development Team
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License along
  17. * with this program; if not, write to the Free Software Foundation, Inc.,
  18. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  19. *
  20. * Change Logs:
  21. * Date Author Notes
  22. * 2009-01-05 Bernard the first version
  23. * 2015-08-01 xiaonong the first version for stm32f7xx
  24. * 2016-01-15 ArdaFu the first version for stm32f4xx with STM32 HAL
  25. * 2016-01-15 zyh the first version for stm32f401rc with STM32 HAL
  26. */
  27. #include "drv_usart.h"
  28. #include "board.h"
  29. #include <rtdevice.h>
  30. #include <rthw.h>
  31. #include <rtthread.h>
  32. /* STM32 uart driver */
  33. struct drv_uart
  34. {
  35. UART_HandleTypeDef UartHandle;
  36. IRQn_Type irq;
  37. };
  38. static rt_err_t drv_configure(struct rt_serial_device *serial,
  39. struct serial_configure *cfg)
  40. {
  41. struct drv_uart *uart;
  42. RT_ASSERT(serial != RT_NULL);
  43. RT_ASSERT(cfg != RT_NULL);
  44. uart = (struct drv_uart *)serial->parent.user_data;
  45. uart->UartHandle.Init.BaudRate = cfg->baud_rate;
  46. uart->UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  47. uart->UartHandle.Init.Mode = UART_MODE_TX_RX;
  48. uart->UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  49. switch (cfg->data_bits)
  50. {
  51. case DATA_BITS_8:
  52. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  53. break;
  54. case DATA_BITS_9:
  55. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
  56. break;
  57. default:
  58. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  59. break;
  60. }
  61. switch (cfg->stop_bits)
  62. {
  63. case STOP_BITS_1:
  64. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  65. break;
  66. case STOP_BITS_2:
  67. uart->UartHandle.Init.StopBits = UART_STOPBITS_2;
  68. break;
  69. default:
  70. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  71. break;
  72. }
  73. switch (cfg->parity)
  74. {
  75. case PARITY_NONE:
  76. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  77. break;
  78. case PARITY_ODD:
  79. uart->UartHandle.Init.Parity = UART_PARITY_ODD;
  80. break;
  81. case PARITY_EVEN:
  82. uart->UartHandle.Init.Parity = UART_PARITY_EVEN;
  83. break;
  84. default:
  85. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  86. break;
  87. }
  88. if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
  89. {
  90. return RT_ERROR;
  91. }
  92. return RT_EOK;
  93. }
  94. static rt_err_t drv_control(struct rt_serial_device *serial,
  95. int cmd, void *arg)
  96. {
  97. struct drv_uart *uart;
  98. RT_ASSERT(serial != RT_NULL);
  99. uart = (struct drv_uart *)serial->parent.user_data;
  100. switch (cmd)
  101. {
  102. case RT_DEVICE_CTRL_CLR_INT:
  103. /* disable rx irq */
  104. NVIC_DisableIRQ(uart->irq);
  105. /* disable interrupt */
  106. __HAL_UART_DISABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  107. break;
  108. case RT_DEVICE_CTRL_SET_INT:
  109. /* enable rx irq */
  110. NVIC_EnableIRQ(uart->irq);
  111. /* enable interrupt */
  112. __HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  113. break;
  114. }
  115. return RT_EOK;
  116. }
  117. static int drv_putc(struct rt_serial_device *serial, char c)
  118. {
  119. struct drv_uart *uart;
  120. RT_ASSERT(serial != RT_NULL);
  121. uart = (struct drv_uart *)serial->parent.user_data;
  122. __HAL_UART_CLEAR_FLAG(&(uart->UartHandle), UART_FLAG_TC);
  123. uart->UartHandle.Instance->DR = c;
  124. while (__HAL_UART_GET_FLAG(&(uart->UartHandle), UART_FLAG_TC) == RESET);
  125. return 1;
  126. }
  127. static int drv_getc(struct rt_serial_device *serial)
  128. {
  129. int ch;
  130. struct drv_uart *uart;
  131. RT_ASSERT(serial != RT_NULL);
  132. uart = (struct drv_uart *)serial->parent.user_data;
  133. ch = -1;
  134. if (__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET)
  135. ch = uart->UartHandle.Instance->DR & 0xff;
  136. return ch;
  137. }
  138. static const struct rt_uart_ops drv_uart_ops =
  139. {
  140. drv_configure,
  141. drv_control,
  142. drv_putc,
  143. drv_getc,
  144. };
  145. #if defined(BSP_USING_UART1)
  146. /* UART1 device driver structure */
  147. static struct drv_uart uart1;
  148. struct rt_serial_device serial1;
  149. void USART1_IRQHandler(void)
  150. {
  151. struct drv_uart *uart;
  152. uart = &uart1;
  153. /* enter interrupt */
  154. rt_interrupt_enter();
  155. /* UART in mode Receiver -------------------------------------------------*/
  156. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  157. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  158. {
  159. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  160. /* Clear RXNE interrupt flag */
  161. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  162. }
  163. /* leave interrupt */
  164. rt_interrupt_leave();
  165. }
  166. #endif /* BSP_USING_UART1 */
  167. #if defined(BSP_USING_UART2)
  168. /* UART2 device driver structure */
  169. static struct drv_uart uart2;
  170. struct rt_serial_device serial2;
  171. void USART2_IRQHandler(void)
  172. {
  173. struct drv_uart *uart;
  174. uart = &uart2;
  175. /* enter interrupt */
  176. rt_interrupt_enter();
  177. /* UART in mode Receiver -------------------------------------------------*/
  178. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  179. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  180. {
  181. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  182. /* Clear RXNE interrupt flag */
  183. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  184. }
  185. /* leave interrupt */
  186. rt_interrupt_leave();
  187. }
  188. #endif /* BSP_USING_UART2 */
  189. #if defined(BSP_USING_UART3)
  190. /* UART3 device driver structure */
  191. static struct drv_uart uart3;
  192. struct rt_serial_device serial3;
  193. void USART3_IRQHandler(void)
  194. {
  195. struct drv_uart *uart;
  196. uart = &uart3;
  197. /* enter interrupt */
  198. rt_interrupt_enter();
  199. /* UART in mode Receiver -------------------------------------------------*/
  200. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  201. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  202. {
  203. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  204. /* Clear RXNE interrupt flag */
  205. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  206. }
  207. /* leave interrupt */
  208. rt_interrupt_leave();
  209. }
  210. #endif /* BSP_USING_UART3 */
  211. #if defined(BSP_USING_UART6)
  212. /* UART6 device driver structure */
  213. static struct drv_uart uart6;
  214. struct rt_serial_device serial6;
  215. void USART6_IRQHandler(void)
  216. {
  217. struct drv_uart *uart;
  218. uart = &uart6;
  219. /* enter interrupt */
  220. rt_interrupt_enter();
  221. /* UART in mode Receiver -------------------------------------------------*/
  222. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  223. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  224. {
  225. rt_hw_serial_isr(&serial6, RT_SERIAL_EVENT_RX_IND);
  226. /* Clear RXNE interrupt flag */
  227. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  228. }
  229. /* leave interrupt */
  230. rt_interrupt_leave();
  231. }
  232. #endif /* BSP_USING_UART6 */
  233. /**
  234. * @brief UART MSP Initialization
  235. * This function configures the hardware resources used in this example:
  236. * - Peripheral's clock enable
  237. * - Peripheral's GPIO Configuration
  238. * - NVIC configuration for UART interrupt request enable
  239. * @param huart: UART handle pointer
  240. * @retval None
  241. */
  242. void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
  243. {
  244. GPIO_InitTypeDef GPIO_InitStruct;
  245. #ifdef BSP_USING_UART1
  246. if (uartHandle->Instance == USART1)
  247. {
  248. /* USART1 clock enable */
  249. __HAL_RCC_USART1_CLK_ENABLE();
  250. __HAL_RCC_GPIOA_CLK_ENABLE();
  251. /**USART1 GPIO Configuration
  252. PA9 ------> USART1_TX
  253. PA10 ------> USART1_RX
  254. */
  255. GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  256. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  257. GPIO_InitStruct.Pull = GPIO_PULLUP;
  258. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  259. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  260. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  261. }
  262. #endif /* BSP_USING_UART1 */
  263. #ifdef BSP_USING_UART2
  264. if (uartHandle->Instance == USART2)
  265. {
  266. /* USART2 clock enable */
  267. __HAL_RCC_USART2_CLK_ENABLE();
  268. __HAL_RCC_GPIOA_CLK_ENABLE();
  269. /**USART2 GPIO Configuration
  270. PA2 ------> USART2_TX
  271. PA3 ------> USART2_RX
  272. */
  273. GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  274. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  275. GPIO_InitStruct.Pull = GPIO_PULLUP;
  276. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  277. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  278. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  279. }
  280. #endif /* BSP_USING_UART2 */
  281. #ifdef BSP_USING_UART3
  282. if (uartHandle->Instance == USART3)
  283. {
  284. /* USART3 clock enable */
  285. __HAL_RCC_USART3_CLK_ENABLE();
  286. __HAL_RCC_GPIOB_CLK_ENABLE();
  287. /**USART3 GPIO Configuration
  288. PB10 ------> USART3_TX
  289. PB11 ------> USART3_RX
  290. */
  291. GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
  292. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  293. GPIO_InitStruct.Pull = GPIO_PULLUP;
  294. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  295. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  296. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  297. }
  298. #endif /* BSP_USING_UART3 */
  299. #ifdef BSP_USING_UART6
  300. if (uartHandle->Instance == USART6)
  301. {
  302. /* USART6 clock enable */
  303. __HAL_RCC_USART6_CLK_ENABLE();
  304. __HAL_RCC_GPIOC_CLK_ENABLE();
  305. /**USART6 GPIO Configuration
  306. PC6 ------> USART6_TX
  307. PC7 ------> USART6_RX
  308. */
  309. GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
  310. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  311. GPIO_InitStruct.Pull = GPIO_PULLUP;
  312. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  313. GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
  314. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  315. }
  316. #endif /* BSP_USING_UART6 */
  317. }
  318. void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
  319. {
  320. #ifdef BSP_USING_UART1
  321. if (uartHandle->Instance == USART1)
  322. {
  323. /* Peripheral clock disable */
  324. __HAL_RCC_USART1_CLK_DISABLE();
  325. /**USART1 GPIO Configuration
  326. PA9 ------> USART1_TX
  327. PA10 ------> USART1_RX
  328. */
  329. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
  330. }
  331. #endif /* BSP_USING_UART1 */
  332. #ifdef BSP_USING_UART2
  333. if (uartHandle->Instance == USART2)
  334. {
  335. /* Peripheral clock disable */
  336. __HAL_RCC_USART2_CLK_DISABLE();
  337. /**USART2 GPIO Configuration
  338. PA2 ------> USART2_TX
  339. PA3 ------> USART2_RX
  340. */
  341. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
  342. }
  343. #endif /* BSP_USING_UART2 */
  344. #ifdef BSP_USING_UART3
  345. if (uartHandle->Instance == USART3)
  346. {
  347. /* Peripheral clock disable */
  348. __HAL_RCC_USART3_CLK_DISABLE();
  349. /**USART3 GPIO Configuration
  350. PB10 ------> USART3_TX
  351. PB11 ------> USART3_RX
  352. */
  353. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_11);
  354. }
  355. #endif /* BSP_USING_UART3 */
  356. #ifdef BSP_USING_UART6
  357. if (uartHandle->Instance == USART6)
  358. {
  359. /* Peripheral clock disable */
  360. __HAL_RCC_USART6_CLK_DISABLE();
  361. /**USART6 GPIO Configuration
  362. PC6 ------> USART6_TX
  363. PC7 ------> USART6_RX
  364. */
  365. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_6 | GPIO_PIN_7);
  366. }
  367. #endif /* BSP_USING_UART6 */
  368. }
  369. int hw_usart_init(void)
  370. {
  371. struct drv_uart *uart;
  372. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  373. #ifdef BSP_USING_UART1
  374. uart = &uart1;
  375. uart->UartHandle.Instance = USART1;
  376. uart->irq = USART1_IRQn;
  377. serial1.ops = &drv_uart_ops;
  378. serial1.config = config;
  379. /* register UART1 device */
  380. rt_hw_serial_register(&serial1, "uart1",
  381. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  382. uart);
  383. #endif /* BSP_USING_UART1 */
  384. #ifdef BSP_USING_UART2
  385. uart = &uart2;
  386. uart->UartHandle.Instance = USART2;
  387. uart->irq = USART2_IRQn;
  388. serial2.ops = &drv_uart_ops;
  389. serial2.config = config;
  390. /* register UART2 device */
  391. rt_hw_serial_register(&serial2, "uart2",
  392. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  393. uart);
  394. #endif /* BSP_USING_UART2 */
  395. #ifdef BSP_USING_UART3
  396. uart = &uart3;
  397. uart->UartHandle.Instance = USART3;
  398. uart->irq = USART3_IRQn;
  399. serial3.ops = &drv_uart_ops;
  400. serial3.config = config;
  401. /* register UART3 device */
  402. rt_hw_serial_register(&serial3, "uart3",
  403. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  404. uart);
  405. #endif /* BSP_USING_UART3 */
  406. #ifdef BSP_USING_UART6
  407. uart = &uart6;
  408. uart->UartHandle.Instance = USART6;
  409. uart->irq = USART6_IRQn;
  410. serial6.ops = &drv_uart_ops;
  411. serial6.config = config;
  412. /* register UART6 device */
  413. rt_hw_serial_register(&serial6, "uart6",
  414. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  415. uart);
  416. #endif /* BSP_USING_UART6 */
  417. return 0;
  418. }
  419. INIT_BOARD_EXPORT(hw_usart_init);