drv_usart.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. */
  26. #include "drv_usart.h"
  27. #include "board.h"
  28. #include <rtdevice.h>
  29. #include <rthw.h>
  30. #include <rtthread.h>
  31. /* Exported types ------------------------------------------------------------*/
  32. /* Exported constants --------------------------------------------------------*/
  33. /* User can use this section to tailor USARTx/UARTx instance used and associated
  34. resources */
  35. /* Definition for USARTx clock resources */
  36. #define USARTx USART2
  37. #define USARTx_CLK_ENABLE() __HAL_RCC_USART2_CLK_ENABLE();
  38. #define USARTx_RX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  39. #define USARTx_TX_GPIO_CLK_ENABLE() __HAL_RCC_GPIOA_CLK_ENABLE()
  40. #define USARTx_FORCE_RESET() __HAL_RCC_USART2_FORCE_RESET()
  41. #define USARTx_RELEASE_RESET() __HAL_RCC_USART2_RELEASE_RESET()
  42. /* Definition for USARTx Pins */
  43. #define USARTx_TX_PIN GPIO_PIN_2
  44. #define USARTx_TX_GPIO_PORT GPIOA
  45. #define USARTx_TX_AF GPIO_AF7_USART2
  46. #define USARTx_RX_PIN GPIO_PIN_3
  47. #define USARTx_RX_GPIO_PORT GPIOA
  48. #define USARTx_RX_AF GPIO_AF7_USART2
  49. /* STM32 uart driver */
  50. struct drv_uart
  51. {
  52. UART_HandleTypeDef UartHandle;
  53. IRQn_Type irq;
  54. };
  55. static rt_err_t drv_configure(struct rt_serial_device *serial,
  56. struct serial_configure *cfg)
  57. {
  58. struct drv_uart *uart;
  59. RT_ASSERT(serial != RT_NULL);
  60. RT_ASSERT(cfg != RT_NULL);
  61. uart = (struct drv_uart *)serial->parent.user_data;
  62. uart->UartHandle.Init.BaudRate = cfg->baud_rate;
  63. uart->UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  64. uart->UartHandle.Init.Mode = UART_MODE_TX_RX;
  65. uart->UartHandle.Init.OverSampling = UART_OVERSAMPLING_16;
  66. switch (cfg->data_bits)
  67. {
  68. case DATA_BITS_8:
  69. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  70. break;
  71. case DATA_BITS_9:
  72. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
  73. break;
  74. default:
  75. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  76. break;
  77. }
  78. switch (cfg->stop_bits)
  79. {
  80. case STOP_BITS_1:
  81. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  82. break;
  83. case STOP_BITS_2:
  84. uart->UartHandle.Init.StopBits = UART_STOPBITS_2;
  85. break;
  86. default:
  87. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  88. break;
  89. }
  90. switch (cfg->parity)
  91. {
  92. case PARITY_NONE:
  93. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  94. break;
  95. case PARITY_ODD:
  96. uart->UartHandle.Init.Parity = UART_PARITY_ODD;
  97. break;
  98. case PARITY_EVEN:
  99. uart->UartHandle.Init.Parity = UART_PARITY_EVEN;
  100. break;
  101. default:
  102. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  103. break;
  104. }
  105. if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
  106. {
  107. return RT_ERROR;
  108. }
  109. return RT_EOK;
  110. }
  111. static rt_err_t drv_control(struct rt_serial_device *serial,
  112. int cmd, void *arg)
  113. {
  114. struct drv_uart *uart;
  115. RT_ASSERT(serial != RT_NULL);
  116. uart = (struct drv_uart *)serial->parent.user_data;
  117. switch (cmd)
  118. {
  119. case RT_DEVICE_CTRL_CLR_INT:
  120. /* disable rx irq */
  121. NVIC_DisableIRQ(uart->irq);
  122. /* disable interrupt */
  123. __HAL_UART_DISABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  124. break;
  125. case RT_DEVICE_CTRL_SET_INT:
  126. /* enable rx irq */
  127. NVIC_EnableIRQ(uart->irq);
  128. /* enable interrupt */
  129. __HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  130. break;
  131. }
  132. return RT_EOK;
  133. }
  134. static int drv_putc(struct rt_serial_device *serial, char c)
  135. {
  136. struct drv_uart *uart;
  137. RT_ASSERT(serial != RT_NULL);
  138. uart = (struct drv_uart *)serial->parent.user_data;
  139. while((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_TXE) == RESET));
  140. uart->UartHandle.Instance->DR = c;
  141. return 1;
  142. }
  143. static int drv_getc(struct rt_serial_device *serial)
  144. {
  145. int ch;
  146. struct drv_uart *uart;
  147. RT_ASSERT(serial != RT_NULL);
  148. uart = (struct drv_uart *)serial->parent.user_data;
  149. ch = -1;
  150. if (__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET)
  151. ch = uart->UartHandle.Instance->DR & 0xff;
  152. return ch;
  153. }
  154. static const struct rt_uart_ops drv_uart_ops =
  155. {
  156. drv_configure,
  157. drv_control,
  158. drv_putc,
  159. drv_getc,
  160. };
  161. #if defined(RT_USING_UART2)
  162. /* UART1 device driver structure */
  163. static struct drv_uart uart2;
  164. struct rt_serial_device serial2;
  165. void USART2_IRQHandler(void)
  166. {
  167. struct drv_uart *uart;
  168. uart = &uart2;
  169. /* enter interrupt */
  170. rt_interrupt_enter();
  171. /* UART in mode Receiver -------------------------------------------------*/
  172. if ((__HAL_UART_GET_FLAG(&uart->UartHandle, UART_FLAG_RXNE) != RESET) &&
  173. (__HAL_UART_GET_IT_SOURCE(&uart->UartHandle, UART_IT_RXNE) != RESET))
  174. {
  175. rt_hw_serial_isr(&serial2, RT_SERIAL_EVENT_RX_IND);
  176. /* Clear RXNE interrupt flag */
  177. __HAL_UART_CLEAR_FLAG(&uart->UartHandle, UART_FLAG_RXNE);
  178. }
  179. /* leave interrupt */
  180. rt_interrupt_leave();
  181. }
  182. #endif /* RT_USING_UART1 */
  183. /**
  184. * @brief UART MSP Initialization
  185. * This function configures the hardware resources used in this example:
  186. * - Peripheral's clock enable
  187. * - Peripheral's GPIO Configuration
  188. * - NVIC configuration for UART interrupt request enable
  189. * @param huart: UART handle pointer
  190. * @retval None
  191. */
  192. void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  193. {
  194. GPIO_InitTypeDef GPIO_InitStruct;
  195. if (huart->Instance == USART2)
  196. {
  197. /*##-1- Enable peripherals and GPIO Clocks #################################*/
  198. /* Enable GPIO TX/RX clock */
  199. USARTx_TX_GPIO_CLK_ENABLE();
  200. USARTx_RX_GPIO_CLK_ENABLE();
  201. /* Enable USARTx clock */
  202. USARTx_CLK_ENABLE();
  203. /*##-2- Configure peripheral GPIO ##########################################*/
  204. /* UART TX GPIO pin configuration */
  205. GPIO_InitStruct.Pin = USARTx_TX_PIN;
  206. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  207. GPIO_InitStruct.Pull = GPIO_PULLUP;
  208. GPIO_InitStruct.Speed = GPIO_SPEED_FAST;
  209. GPIO_InitStruct.Alternate = USARTx_TX_AF;
  210. HAL_GPIO_Init(USARTx_TX_GPIO_PORT, &GPIO_InitStruct);
  211. /* UART RX GPIO pin configuration */
  212. GPIO_InitStruct.Pin = USARTx_RX_PIN;
  213. GPIO_InitStruct.Alternate = USARTx_RX_AF;
  214. HAL_GPIO_Init(USARTx_RX_GPIO_PORT, &GPIO_InitStruct);
  215. HAL_NVIC_SetPriority(USART2_IRQn, 0, 1);
  216. HAL_NVIC_EnableIRQ(USART2_IRQn);
  217. }
  218. }
  219. /**
  220. * @brief UART MSP De-Initialization
  221. * This function frees the hardware resources used in this example:
  222. * - Disable the Peripheral's clock
  223. * - Revert GPIO and NVIC configuration to their default state
  224. * @param huart: UART handle pointer
  225. * @retval None
  226. */
  227. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  228. {
  229. if (huart->Instance == USART2)
  230. {
  231. /*##-1- Reset peripherals ##################################################*/
  232. USARTx_FORCE_RESET();
  233. USARTx_RELEASE_RESET();
  234. /*##-2- Disable peripherals and GPIO Clocks #################################*/
  235. /* Configure UART Tx as alternate function */
  236. HAL_GPIO_DeInit(USARTx_TX_GPIO_PORT, USARTx_TX_PIN);
  237. /* Configure UART Rx as alternate function */
  238. HAL_GPIO_DeInit(USARTx_RX_GPIO_PORT, USARTx_RX_PIN);
  239. HAL_NVIC_DisableIRQ(USART2_IRQn);
  240. }
  241. }
  242. int hw_usart_init(void)
  243. {
  244. struct drv_uart *uart;
  245. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  246. #ifdef RT_USING_UART2
  247. uart = &uart2;
  248. uart->UartHandle.Instance = USART2;
  249. serial2.ops = &drv_uart_ops;
  250. serial2.config = config;
  251. /* register UART1 device */
  252. rt_hw_serial_register(&serial2, "uart2",
  253. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  254. uart);
  255. #endif /* RT_USING_UART1 */
  256. return 0;
  257. }
  258. INIT_BOARD_EXPORT(hw_usart_init);