drv_usart.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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. while ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_TXE) == RESET));
  123. uart->UartHandle.Instance->DR = c;
  124. return 1;
  125. }
  126. static int drv_getc(struct rt_serial_device *serial)
  127. {
  128. int ch;
  129. struct drv_uart *uart;
  130. RT_ASSERT(serial != RT_NULL);
  131. uart = (struct drv_uart *)serial->parent.user_data;
  132. ch = -1;
  133. if (__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET)
  134. ch = uart->UartHandle.Instance->DR & 0xff;
  135. return ch;
  136. }
  137. static const struct rt_uart_ops drv_uart_ops =
  138. {
  139. drv_configure,
  140. drv_control,
  141. drv_putc,
  142. drv_getc,
  143. };
  144. #if defined(BSP_USING_UART1)
  145. /* UART1 device driver structure */
  146. static struct drv_uart uart1;
  147. struct rt_serial_device serial1;
  148. void USART1_IRQHandler(void)
  149. {
  150. struct drv_uart *uart;
  151. uart = &uart1;
  152. /* enter interrupt */
  153. rt_interrupt_enter();
  154. /* UART in mode Receiver -------------------------------------------------*/
  155. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  156. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  157. {
  158. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  159. /* Clear RXNE interrupt flag */
  160. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  161. }
  162. /* leave interrupt */
  163. rt_interrupt_leave();
  164. }
  165. #endif /* BSP_USING_UART1 */
  166. #if defined(BSP_USING_UART2)
  167. /* UART2 device driver structure */
  168. static struct drv_uart uart2;
  169. struct rt_serial_device serial2;
  170. void USART2_IRQHandler(void)
  171. {
  172. struct drv_uart *uart;
  173. uart = &uart2;
  174. /* enter interrupt */
  175. rt_interrupt_enter();
  176. /* UART in mode Receiver -------------------------------------------------*/
  177. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  178. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  179. {
  180. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  181. /* Clear RXNE interrupt flag */
  182. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  183. }
  184. /* leave interrupt */
  185. rt_interrupt_leave();
  186. }
  187. #endif /* BSP_USING_UART2 */
  188. #if defined(BSP_USING_UART3)
  189. /* UART3 device driver structure */
  190. static struct drv_uart uart3;
  191. struct rt_serial_device serial3;
  192. void USART3_IRQHandler(void)
  193. {
  194. struct drv_uart *uart;
  195. uart = &uart3;
  196. /* enter interrupt */
  197. rt_interrupt_enter();
  198. /* UART in mode Receiver -------------------------------------------------*/
  199. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  200. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  201. {
  202. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  203. /* Clear RXNE interrupt flag */
  204. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  205. }
  206. /* leave interrupt */
  207. rt_interrupt_leave();
  208. }
  209. #endif /* BSP_USING_UART3 */
  210. #if defined(BSP_USING_UART6)
  211. /* UART6 device driver structure */
  212. static struct drv_uart uart6;
  213. struct rt_serial_device serial6;
  214. void USART6_IRQHandler(void)
  215. {
  216. struct drv_uart *uart;
  217. uart = &uart6;
  218. /* enter interrupt */
  219. rt_interrupt_enter();
  220. /* UART in mode Receiver -------------------------------------------------*/
  221. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  222. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  223. {
  224. rt_hw_serial_isr(&serial6, RT_SERIAL_EVENT_RX_IND);
  225. /* Clear RXNE interrupt flag */
  226. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  227. }
  228. /* leave interrupt */
  229. rt_interrupt_leave();
  230. }
  231. #endif /* BSP_USING_UART6 */
  232. /**
  233. * @brief UART MSP Initialization
  234. * This function configures the hardware resources used in this example:
  235. * - Peripheral's clock enable
  236. * - Peripheral's GPIO Configuration
  237. * - NVIC configuration for UART interrupt request enable
  238. * @param huart: UART handle pointer
  239. * @retval None
  240. */
  241. void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
  242. {
  243. GPIO_InitTypeDef GPIO_InitStruct;
  244. if (uartHandle->Instance == USART1)
  245. {
  246. /* USART1 clock enable */
  247. __HAL_RCC_USART1_CLK_ENABLE();
  248. __HAL_RCC_GPIOA_CLK_ENABLE();
  249. /**USART1 GPIO Configuration
  250. PA9 ------> USART1_TX
  251. PA10 ------> USART1_RX
  252. */
  253. GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  254. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  255. GPIO_InitStruct.Pull = GPIO_PULLUP;
  256. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  257. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  258. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  259. }
  260. else if (uartHandle->Instance == USART2)
  261. {
  262. /* USART2 clock enable */
  263. __HAL_RCC_USART2_CLK_ENABLE();
  264. __HAL_RCC_GPIOA_CLK_ENABLE();
  265. /**USART2 GPIO Configuration
  266. PA2 ------> USART2_TX
  267. PA3 ------> USART2_RX
  268. */
  269. GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  270. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  271. GPIO_InitStruct.Pull = GPIO_PULLUP;
  272. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  273. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  274. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  275. }
  276. else if (uartHandle->Instance == USART3)
  277. {
  278. /* USART3 clock enable */
  279. __HAL_RCC_USART3_CLK_ENABLE();
  280. __HAL_RCC_GPIOB_CLK_ENABLE();
  281. /**USART3 GPIO Configuration
  282. PB10 ------> USART3_TX
  283. PB11 ------> USART3_RX
  284. */
  285. GPIO_InitStruct.Pin = GPIO_PIN_10 | GPIO_PIN_11;
  286. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  287. GPIO_InitStruct.Pull = GPIO_PULLUP;
  288. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  289. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  290. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  291. }
  292. else if (uartHandle->Instance == USART6)
  293. {
  294. /* USART6 clock enable */
  295. __HAL_RCC_USART6_CLK_ENABLE();
  296. __HAL_RCC_GPIOC_CLK_ENABLE();
  297. /**USART6 GPIO Configuration
  298. PC6 ------> USART6_TX
  299. PC7 ------> USART6_RX
  300. */
  301. GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
  302. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  303. GPIO_InitStruct.Pull = GPIO_PULLUP;
  304. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  305. GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
  306. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  307. }
  308. }
  309. void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
  310. {
  311. if (uartHandle->Instance == USART1)
  312. {
  313. /* Peripheral clock disable */
  314. __HAL_RCC_USART1_CLK_DISABLE();
  315. /**USART1 GPIO Configuration
  316. PA9 ------> USART1_TX
  317. PA10 ------> USART1_RX
  318. */
  319. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
  320. }
  321. else if (uartHandle->Instance == USART2)
  322. {
  323. /* Peripheral clock disable */
  324. __HAL_RCC_USART2_CLK_DISABLE();
  325. /**USART2 GPIO Configuration
  326. PA2 ------> USART2_TX
  327. PA3 ------> USART2_RX
  328. */
  329. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
  330. }
  331. else if (uartHandle->Instance == USART3)
  332. {
  333. /* Peripheral clock disable */
  334. __HAL_RCC_USART3_CLK_DISABLE();
  335. /**USART3 GPIO Configuration
  336. PB10 ------> USART3_TX
  337. PB11 ------> USART3_RX
  338. */
  339. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10 | GPIO_PIN_11);
  340. }
  341. else if (uartHandle->Instance == USART6)
  342. {
  343. /* Peripheral clock disable */
  344. __HAL_RCC_USART6_CLK_DISABLE();
  345. /**USART6 GPIO Configuration
  346. PC6 ------> USART6_TX
  347. PC7 ------> USART6_RX
  348. */
  349. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_6 | GPIO_PIN_7);
  350. }
  351. }
  352. int hw_usart_init(void)
  353. {
  354. struct drv_uart *uart;
  355. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  356. #ifdef BSP_USING_UART1
  357. uart = &uart1;
  358. uart->UartHandle.Instance = USART1;
  359. uart->irq = USART1_IRQn;
  360. serial1.ops = &drv_uart_ops;
  361. serial1.config = config;
  362. /* register UART1 device */
  363. rt_hw_serial_register(&serial1, "uart1",
  364. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  365. uart);
  366. #endif /* BSP_USING_UART1 */
  367. #ifdef BSP_USING_UART2
  368. uart = &uart2;
  369. uart->UartHandle.Instance = USART2;
  370. uart->irq = USART2_IRQn;
  371. serial2.ops = &drv_uart_ops;
  372. serial2.config = config;
  373. /* register UART2 device */
  374. rt_hw_serial_register(&serial2, "uart2",
  375. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  376. uart);
  377. #endif /* BSP_USING_UART2 */
  378. #ifdef BSP_USING_UART3
  379. uart = &uart3;
  380. uart->UartHandle.Instance = USART3;
  381. uart->irq = USART3_IRQn;
  382. serial3.ops = &drv_uart_ops;
  383. serial3.config = config;
  384. /* register UART3 device */
  385. rt_hw_serial_register(&serial3, "uart3",
  386. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  387. uart);
  388. #endif /* BSP_USING_UART3 */
  389. #ifdef BSP_USING_UART6
  390. uart = &uart6;
  391. uart->UartHandle.Instance = USART6;
  392. uart->irq = USART6_IRQn;
  393. serial6.ops = &drv_uart_ops;
  394. serial6.config = config;
  395. /* register UART6 device */
  396. rt_hw_serial_register(&serial6, "uart6",
  397. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  398. uart);
  399. #endif /* BSP_USING_UART6 */
  400. return 0;
  401. }
  402. INIT_BOARD_EXPORT(hw_usart_init);