drv_usart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  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(RT_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 /* RT_USING_UART1 */
  166. #if defined(RT_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 /* RT_USING_UART2 */
  188. #if defined(RT_USING_UART6)
  189. /* UART2 device driver structure */
  190. static struct drv_uart uart6;
  191. struct rt_serial_device serial6;
  192. void USART6_IRQHandler(void)
  193. {
  194. struct drv_uart *uart;
  195. uart = &uart6;
  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(&serial6, 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 /* RT_USING_UART3 */
  210. /**
  211. * @brief UART MSP Initialization
  212. * This function configures the hardware resources used in this example:
  213. * - Peripheral's clock enable
  214. * - Peripheral's GPIO Configuration
  215. * - NVIC configuration for UART interrupt request enable
  216. * @param huart: UART handle pointer
  217. * @retval None
  218. */
  219. void HAL_UART_MspInit(UART_HandleTypeDef *uartHandle)
  220. {
  221. GPIO_InitTypeDef GPIO_InitStruct;
  222. if (uartHandle->Instance == USART1)
  223. {
  224. /* USART1 clock enable */
  225. __HAL_RCC_USART1_CLK_ENABLE();
  226. __HAL_RCC_GPIOA_CLK_ENABLE();
  227. /**USART1 GPIO Configuration
  228. PA9 ------> USART1_TX
  229. PA10 ------> USART1_RX
  230. */
  231. GPIO_InitStruct.Pin = GPIO_PIN_9 | GPIO_PIN_10;
  232. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  233. GPIO_InitStruct.Pull = GPIO_PULLUP;
  234. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  235. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  236. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  237. }
  238. else if (uartHandle->Instance == USART2)
  239. {
  240. /* USART2 clock enable */
  241. __HAL_RCC_USART2_CLK_ENABLE();
  242. __HAL_RCC_GPIOA_CLK_ENABLE();
  243. /**USART2 GPIO Configuration
  244. PA2 ------> USART2_TX
  245. PA3 ------> USART2_RX
  246. */
  247. GPIO_InitStruct.Pin = GPIO_PIN_2 | GPIO_PIN_3;
  248. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  249. GPIO_InitStruct.Pull = GPIO_PULLUP;
  250. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  251. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  252. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  253. }
  254. else if (uartHandle->Instance == USART6)
  255. {
  256. /* USART6 clock enable */
  257. __HAL_RCC_USART6_CLK_ENABLE();
  258. __HAL_RCC_GPIOC_CLK_ENABLE();
  259. /**USART6 GPIO Configuration
  260. PC6 ------> USART6_TX
  261. PC7 ------> USART6_RX
  262. */
  263. GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
  264. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  265. GPIO_InitStruct.Pull = GPIO_PULLUP;
  266. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  267. GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
  268. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  269. }
  270. }
  271. void HAL_UART_MspDeInit(UART_HandleTypeDef *uartHandle)
  272. {
  273. if (uartHandle->Instance == USART1)
  274. {
  275. /* Peripheral clock disable */
  276. __HAL_RCC_USART1_CLK_DISABLE();
  277. /**USART1 GPIO Configuration
  278. PA9 ------> USART1_TX
  279. PA10 ------> USART1_RX
  280. */
  281. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9 | GPIO_PIN_10);
  282. }
  283. else if (uartHandle->Instance == USART2)
  284. {
  285. /* Peripheral clock disable */
  286. __HAL_RCC_USART2_CLK_DISABLE();
  287. /**USART2 GPIO Configuration
  288. PA2 ------> USART2_TX
  289. PA3 ------> USART2_RX
  290. */
  291. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2 | GPIO_PIN_3);
  292. }
  293. else if (uartHandle->Instance == USART6)
  294. {
  295. /* Peripheral clock disable */
  296. __HAL_RCC_USART6_CLK_DISABLE();
  297. /**USART6 GPIO Configuration
  298. PC6 ------> USART6_TX
  299. PC7 ------> USART6_RX
  300. */
  301. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_6 | GPIO_PIN_7);
  302. }
  303. }
  304. int hw_usart_init(void)
  305. {
  306. struct drv_uart *uart;
  307. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  308. #ifdef RT_USING_UART1
  309. uart = &uart1;
  310. uart->UartHandle.Instance = USART1;
  311. uart->irq = USART1_IRQn;
  312. serial1.ops = &drv_uart_ops;
  313. serial1.config = config;
  314. /* register UART1 device */
  315. rt_hw_serial_register(&serial1, "uart1",
  316. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  317. uart);
  318. #endif /* RT_USING_UART1 */
  319. #ifdef RT_USING_UART2
  320. uart = &uart2;
  321. uart->UartHandle.Instance = USART2;
  322. uart->irq = USART2_IRQn;
  323. serial2.ops = &drv_uart_ops;
  324. serial2.config = config;
  325. /* register UART2 device */
  326. rt_hw_serial_register(&serial2, "uart2",
  327. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  328. uart);
  329. #endif /* RT_USING_UART2 */
  330. #ifdef RT_USING_UART6
  331. uart = &uart6;
  332. uart->UartHandle.Instance = USART6;
  333. uart->irq = USART6_IRQn;
  334. serial6.ops = &drv_uart_ops;
  335. serial6.config = config;
  336. /* register UART2 device */
  337. rt_hw_serial_register(&serial6, "uart6",
  338. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  339. uart);
  340. #endif /* RT_USING_UART2 */
  341. return 0;
  342. }
  343. INIT_BOARD_EXPORT(hw_usart_init);