drv_usart.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c) 2006-2018, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2017-08-25 LongfeiMa the first version for stm32h7xx
  9. */
  10. #include "stm32h7xx.h"
  11. #include "drv_usart.h"
  12. #include "board.h"
  13. #include <rtdevice.h>
  14. /* Definition for USART1 clock resources */
  15. #define USART1_CLK_ENABLE() __USART1_CLK_ENABLE()
  16. #define USART1_RX_GPIO_CLK_ENABLE() __GPIOB_CLK_ENABLE()
  17. #define USART1_TX_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE()
  18. #define USART1_FORCE_RESET() __USART1_FORCE_RESET()
  19. #define USART1_RELEASE_RESET() __USART1_RELEASE_RESET()
  20. /* Definition for USARTx Pins */
  21. #define USART1_TX_PIN GPIO_PIN_9
  22. #define USART1_TX_GPIO_PORT GPIOA
  23. #define USART1_TX_AF GPIO_AF7_USART1
  24. #define USART1_RX_PIN GPIO_PIN_7
  25. #define USART1_RX_GPIO_PORT GPIOB
  26. #define USART1_RX_AF GPIO_AF7_USART1
  27. /* Definition for USART3 clock resources */
  28. #define USART3_CLK_ENABLE() __USART3_CLK_ENABLE()
  29. #define USART3_RX_GPIO_CLK_ENABLE() __GPIOD_CLK_ENABLE()
  30. #define USART3_TX_GPIO_CLK_ENABLE() __GPIOD_CLK_ENABLE()
  31. #define USART3_FORCE_RESET() __USART3_FORCE_RESET()
  32. #define USART3_RELEASE_RESET() __USART3_RELEASE_RESET()
  33. /* Definition for USARTx Pins */
  34. #define USART3_TX_PIN GPIO_PIN_8
  35. #define USART3_TX_GPIO_PORT GPIOD
  36. #define USART3_TX_AF GPIO_AF7_USART3
  37. #define USART3_RX_PIN GPIO_PIN_9
  38. #define USART3_RX_GPIO_PORT GPIOD
  39. #define USART3_RX_AF GPIO_AF7_USART3
  40. /* STM32 uart driver */
  41. struct stm32_uart
  42. {
  43. UART_HandleTypeDef UartHandle;
  44. IRQn_Type irq;
  45. };
  46. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  47. {
  48. struct stm32_uart *uart;
  49. RT_ASSERT(serial != RT_NULL);
  50. RT_ASSERT(cfg != RT_NULL);
  51. uart = (struct stm32_uart *)serial->parent.user_data;
  52. uart->UartHandle.Init.BaudRate = cfg->baud_rate;
  53. uart->UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  54. uart->UartHandle.Init.Mode = UART_MODE_TX_RX;
  55. uart->UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  56. switch (cfg->data_bits)
  57. {
  58. case DATA_BITS_7:
  59. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_7B;
  60. break;
  61. case DATA_BITS_8:
  62. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  63. break;
  64. case DATA_BITS_9:
  65. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
  66. break;
  67. default:
  68. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  69. break;
  70. }
  71. switch (cfg->stop_bits)
  72. {
  73. case STOP_BITS_1:
  74. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  75. break;
  76. case STOP_BITS_2:
  77. uart->UartHandle.Init.StopBits = UART_STOPBITS_2;
  78. break;
  79. default:
  80. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  81. break;
  82. }
  83. switch (cfg->parity)
  84. {
  85. case PARITY_NONE:
  86. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  87. break;
  88. case PARITY_ODD:
  89. uart->UartHandle.Init.Parity = UART_PARITY_ODD;
  90. break;
  91. case PARITY_EVEN:
  92. uart->UartHandle.Init.Parity = UART_PARITY_EVEN;
  93. break;
  94. default:
  95. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  96. break;
  97. }
  98. if (HAL_UART_DeInit(&uart->UartHandle) != HAL_OK)
  99. {
  100. return RT_ERROR;
  101. }
  102. if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
  103. {
  104. return RT_ERROR;
  105. }
  106. return RT_EOK;
  107. }
  108. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
  109. {
  110. struct stm32_uart *uart;
  111. RT_ASSERT(serial != RT_NULL);
  112. uart = (struct stm32_uart *)serial->parent.user_data;
  113. switch (cmd)
  114. {
  115. case RT_DEVICE_CTRL_CLR_INT:
  116. /* disable rx irq */
  117. UART_DISABLE_IRQ(uart->irq);
  118. /* disable interrupt */
  119. __HAL_UART_DISABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  120. break;
  121. case RT_DEVICE_CTRL_SET_INT:
  122. /* enable rx irq */
  123. UART_ENABLE_IRQ(uart->irq);
  124. /* enable interrupt */
  125. __HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  126. break;
  127. }
  128. return RT_EOK;
  129. }
  130. static int stm32_putc(struct rt_serial_device *serial, char c)
  131. {
  132. struct stm32_uart *uart;
  133. RT_ASSERT(serial != RT_NULL);
  134. uart = (struct stm32_uart *)serial->parent.user_data;
  135. __HAL_UART_CLEAR_FLAG(&(uart->UartHandle), UART_FLAG_TC);
  136. uart->UartHandle.Instance->TDR = c;
  137. while (__HAL_UART_GET_FLAG(&(uart->UartHandle), UART_FLAG_TC) == RESET);
  138. return 1;
  139. }
  140. static int stm32_getc(struct rt_serial_device *serial)
  141. {
  142. int ch;
  143. struct stm32_uart *uart;
  144. RT_ASSERT(serial != RT_NULL);
  145. uart = (struct stm32_uart *)serial->parent.user_data;
  146. ch = -1;
  147. if (uart->UartHandle.Instance->ISR & UART_FLAG_RXNE)
  148. {
  149. ch = uart->UartHandle.Instance->RDR & 0xff;
  150. }
  151. return ch;
  152. }
  153. static const struct rt_uart_ops stm32_uart_ops =
  154. {
  155. stm32_configure,
  156. stm32_control,
  157. stm32_putc,
  158. stm32_getc,
  159. };
  160. #if defined(RT_USING_UART1)
  161. /* UART1 device driver structure */
  162. static struct stm32_uart uart1;
  163. struct rt_serial_device serial1;
  164. void USART1_IRQHandler(void)
  165. {
  166. struct stm32_uart *uart;
  167. uart = &uart1;
  168. /* enter interrupt */
  169. rt_interrupt_enter();
  170. /* UART in mode Receiver ---------------------------------------------------*/
  171. if ((__HAL_UART_GET_IT(&uart->UartHandle, UART_IT_RXNE) != RESET) && (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  172. {
  173. rt_hw_serial_isr(&serial1, RT_SERIAL_EVENT_RX_IND);
  174. /* Clear RXNE interrupt flag */
  175. __HAL_UART_SEND_REQ(&uart->UartHandle, UART_RXDATA_FLUSH_REQUEST);
  176. }
  177. /* leave interrupt */
  178. rt_interrupt_leave();
  179. }
  180. #endif /* RT_USING_UART3 */
  181. #if defined(RT_USING_UART3)
  182. /* UART1 device driver structure */
  183. static struct stm32_uart uart3;
  184. struct rt_serial_device serial3;
  185. void USART3_IRQHandler(void)
  186. {
  187. struct stm32_uart *uart;
  188. uart = &uart3;
  189. /* enter interrupt */
  190. rt_interrupt_enter();
  191. /* UART in mode Receiver ---------------------------------------------------*/
  192. if ((__HAL_UART_GET_IT(&uart->UartHandle, UART_IT_RXNE) != RESET) && (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  193. {
  194. rt_hw_serial_isr(&serial3, RT_SERIAL_EVENT_RX_IND);
  195. /* Clear RXNE interrupt flag */
  196. __HAL_UART_SEND_REQ(&uart->UartHandle, UART_RXDATA_FLUSH_REQUEST);
  197. }
  198. /* leave interrupt */
  199. rt_interrupt_leave();
  200. }
  201. #endif /* RT_USING_UART3 */
  202. /**
  203. * @brief UART MSP Initialization
  204. * This function configures the hardware resources used in this example:
  205. * - Peripheral's clock enable
  206. * - Peripheral's GPIO Configuration
  207. * - NVIC configuration for UART interrupt request enable
  208. * @param huart: UART handle pointer
  209. * @retval None
  210. */
  211. void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  212. {
  213. GPIO_InitTypeDef GPIO_InitStruct;
  214. if (huart->Instance == USART1)
  215. {
  216. /* Enable GPIO TX/RX clock */
  217. USART1_TX_GPIO_CLK_ENABLE();
  218. USART1_RX_GPIO_CLK_ENABLE();
  219. /* Enable USARTx clock */
  220. USART1_CLK_ENABLE();
  221. /* UART TX GPIO pin configuration */
  222. GPIO_InitStruct.Pin = USART1_TX_PIN;
  223. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  224. GPIO_InitStruct.Pull = GPIO_PULLUP;
  225. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  226. GPIO_InitStruct.Alternate = USART1_TX_AF;
  227. HAL_GPIO_Init(USART1_TX_GPIO_PORT, &GPIO_InitStruct);
  228. /* UART RX GPIO pin configuration */
  229. GPIO_InitStruct.Pin = USART1_RX_PIN;
  230. GPIO_InitStruct.Alternate = USART1_RX_AF;
  231. HAL_GPIO_Init(USART1_RX_GPIO_PORT, &GPIO_InitStruct);
  232. /* NVIC for USART */
  233. HAL_NVIC_SetPriority(USART1_IRQn, 0, 1);
  234. HAL_NVIC_EnableIRQ(USART1_IRQn);
  235. }
  236. if (huart->Instance == USART3)
  237. {
  238. /* Enable GPIO TX/RX clock */
  239. USART3_TX_GPIO_CLK_ENABLE();
  240. USART3_RX_GPIO_CLK_ENABLE();
  241. /* Enable USARTx clock */
  242. USART3_CLK_ENABLE();
  243. /* UART TX GPIO pin configuration */
  244. GPIO_InitStruct.Pin = USART3_TX_PIN;
  245. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  246. GPIO_InitStruct.Pull = GPIO_PULLUP;
  247. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  248. GPIO_InitStruct.Alternate = USART3_TX_AF;
  249. HAL_GPIO_Init(USART3_TX_GPIO_PORT, &GPIO_InitStruct);
  250. /* UART RX GPIO pin configuration */
  251. GPIO_InitStruct.Pin = USART3_RX_PIN;
  252. GPIO_InitStruct.Alternate = USART3_RX_AF;
  253. HAL_GPIO_Init(USART3_RX_GPIO_PORT, &GPIO_InitStruct);
  254. /* NVIC for USART */
  255. HAL_NVIC_SetPriority(USART3_IRQn, 0, 1);
  256. HAL_NVIC_EnableIRQ(USART3_IRQn);
  257. }
  258. }
  259. /**
  260. * @brief UART MSP De-Initialization
  261. * This function frees the hardware resources used in this example:
  262. * - Disable the Peripheral's clock
  263. * - Revert GPIO and NVIC configuration to their default state
  264. * @param huart: UART handle pointer
  265. * @retval None
  266. */
  267. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  268. {
  269. if (huart->Instance == USART1)
  270. {
  271. /* Reset peripherals */
  272. USART1_FORCE_RESET();
  273. USART1_RELEASE_RESET();
  274. /* Disable peripherals and GPIO Clocks */
  275. /* Configure UART Tx as alternate function */
  276. HAL_GPIO_DeInit(USART1_TX_GPIO_PORT, USART1_TX_PIN);
  277. /* Configure UART Rx as alternate function */
  278. HAL_GPIO_DeInit(USART1_RX_GPIO_PORT, USART1_RX_PIN);
  279. /* Disable the NVIC for UART */
  280. HAL_NVIC_DisableIRQ(USART1_IRQn);
  281. }
  282. if (huart->Instance == USART3)
  283. {
  284. /* Reset peripherals */
  285. USART3_FORCE_RESET();
  286. USART3_RELEASE_RESET();
  287. /* Disable peripherals and GPIO Clocks */
  288. /* Configure UART Tx as alternate function */
  289. HAL_GPIO_DeInit(USART3_TX_GPIO_PORT, USART3_TX_PIN);
  290. /* Configure UART Rx as alternate function */
  291. HAL_GPIO_DeInit(USART3_RX_GPIO_PORT, USART3_RX_PIN);
  292. /* Disable the NVIC for UART */
  293. HAL_NVIC_DisableIRQ(USART3_IRQn);
  294. }
  295. }
  296. int stm32_hw_usart_init(void)
  297. {
  298. struct stm32_uart *uart;
  299. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  300. #ifdef RT_USING_UART1
  301. uart = &uart1;
  302. uart->UartHandle.Instance = USART1;
  303. serial1.ops = &stm32_uart_ops;
  304. serial1.config = config;
  305. /* register UART1 device */
  306. rt_hw_serial_register(&serial1, "uart1",
  307. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  308. uart);
  309. #endif /* RT_USING_UART1 */
  310. #ifdef RT_USING_UART3
  311. uart = &uart3;
  312. uart->UartHandle.Instance = USART3;
  313. serial3.ops = &stm32_uart_ops;
  314. serial3.config = config;
  315. /* register UART3 device */
  316. rt_hw_serial_register(&serial3, "uart3",
  317. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  318. uart);
  319. #endif /* RT_USING_UART3 */
  320. return 0;
  321. }
  322. INIT_BOARD_EXPORT(stm32_hw_usart_init);