drv_usart.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299
  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. * 2017-08-25 LongfeiMa the first version for stm32h7xx
  23. */
  24. #include "stm32h7xx.h"
  25. #include "drv_usart.h"
  26. #include "board.h"
  27. #include <rtdevice.h>
  28. /* Definition for USART1 clock resources */
  29. #define USART1_CLK_ENABLE() __USART1_CLK_ENABLE()
  30. #define USART1_RX_GPIO_CLK_ENABLE() __GPIOB_CLK_ENABLE()
  31. #define USART1_TX_GPIO_CLK_ENABLE() __GPIOA_CLK_ENABLE()
  32. #define USART1_FORCE_RESET() __USART1_FORCE_RESET()
  33. #define USART1_RELEASE_RESET() __USART1_RELEASE_RESET()
  34. /* Definition for USARTx Pins */
  35. #define USART1_TX_PIN GPIO_PIN_9
  36. #define USART1_TX_GPIO_PORT GPIOA
  37. #define USART1_TX_AF GPIO_AF7_USART1
  38. #define USART1_RX_PIN GPIO_PIN_7
  39. #define USART1_RX_GPIO_PORT GPIOB
  40. #define USART1_RX_AF GPIO_AF7_USART1
  41. /* STM32 uart driver */
  42. struct stm32_uart
  43. {
  44. UART_HandleTypeDef UartHandle;
  45. IRQn_Type irq;
  46. };
  47. static rt_err_t stm32_configure(struct rt_serial_device *serial, struct serial_configure *cfg)
  48. {
  49. struct stm32_uart *uart;
  50. RT_ASSERT(serial != RT_NULL);
  51. RT_ASSERT(cfg != RT_NULL);
  52. uart = (struct stm32_uart *)serial->parent.user_data;
  53. uart->UartHandle.Init.BaudRate = cfg->baud_rate;
  54. uart->UartHandle.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  55. uart->UartHandle.Init.Mode = UART_MODE_TX_RX;
  56. uart->UartHandle.AdvancedInit.AdvFeatureInit = UART_ADVFEATURE_NO_INIT;
  57. switch (cfg->data_bits)
  58. {
  59. case DATA_BITS_7:
  60. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_7B;
  61. break;
  62. case DATA_BITS_8:
  63. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  64. break;
  65. case DATA_BITS_9:
  66. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_9B;
  67. break;
  68. default:
  69. uart->UartHandle.Init.WordLength = UART_WORDLENGTH_8B;
  70. break;
  71. }
  72. switch (cfg->stop_bits)
  73. {
  74. case STOP_BITS_1:
  75. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  76. break;
  77. case STOP_BITS_2:
  78. uart->UartHandle.Init.StopBits = UART_STOPBITS_2;
  79. break;
  80. default:
  81. uart->UartHandle.Init.StopBits = UART_STOPBITS_1;
  82. break;
  83. }
  84. switch (cfg->parity)
  85. {
  86. case PARITY_NONE:
  87. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  88. break;
  89. case PARITY_ODD:
  90. uart->UartHandle.Init.Parity = UART_PARITY_ODD;
  91. break;
  92. case PARITY_EVEN:
  93. uart->UartHandle.Init.Parity = UART_PARITY_EVEN;
  94. break;
  95. default:
  96. uart->UartHandle.Init.Parity = UART_PARITY_NONE;
  97. break;
  98. }
  99. if (HAL_UART_DeInit(&uart->UartHandle) != HAL_OK)
  100. {
  101. return RT_ERROR;
  102. }
  103. if (HAL_UART_Init(&uart->UartHandle) != HAL_OK)
  104. {
  105. return RT_ERROR;
  106. }
  107. return RT_EOK;
  108. }
  109. static rt_err_t stm32_control(struct rt_serial_device *serial, int cmd, void *arg)
  110. {
  111. struct stm32_uart *uart;
  112. RT_ASSERT(serial != RT_NULL);
  113. uart = (struct stm32_uart *)serial->parent.user_data;
  114. switch (cmd)
  115. {
  116. case RT_DEVICE_CTRL_CLR_INT:
  117. /* disable rx irq */
  118. UART_DISABLE_IRQ(uart->irq);
  119. /* disable interrupt */
  120. __HAL_UART_DISABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  121. break;
  122. case RT_DEVICE_CTRL_SET_INT:
  123. /* enable rx irq */
  124. UART_ENABLE_IRQ(uart->irq);
  125. /* enable interrupt */
  126. __HAL_UART_ENABLE_IT(&uart->UartHandle, UART_IT_RXNE);
  127. break;
  128. }
  129. return RT_EOK;
  130. }
  131. static int stm32_putc(struct rt_serial_device *serial, char c)
  132. {
  133. struct stm32_uart *uart;
  134. RT_ASSERT(serial != RT_NULL);
  135. uart = (struct stm32_uart *)serial->parent.user_data;
  136. while (!(uart->UartHandle.Instance->ISR & UART_FLAG_TXE));
  137. uart->UartHandle.Instance->TDR = c;
  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_UART1 */
  181. /**
  182. * @brief UART MSP Initialization
  183. * This function configures the hardware resources used in this example:
  184. * - Peripheral's clock enable
  185. * - Peripheral's GPIO Configuration
  186. * - NVIC configuration for UART interrupt request enable
  187. * @param huart: UART handle pointer
  188. * @retval None
  189. */
  190. void HAL_UART_MspInit(UART_HandleTypeDef *huart)
  191. {
  192. GPIO_InitTypeDef GPIO_InitStruct;
  193. if (huart->Instance == USART1)
  194. {
  195. /* Enable GPIO TX/RX clock */
  196. USART1_TX_GPIO_CLK_ENABLE();
  197. USART1_RX_GPIO_CLK_ENABLE();
  198. /* Enable USARTx clock */
  199. USART1_CLK_ENABLE();
  200. /* UART TX GPIO pin configuration */
  201. GPIO_InitStruct.Pin = USART1_TX_PIN;
  202. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  203. GPIO_InitStruct.Pull = GPIO_PULLUP;
  204. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  205. GPIO_InitStruct.Alternate = USART1_TX_AF;
  206. HAL_GPIO_Init(USART1_TX_GPIO_PORT, &GPIO_InitStruct);
  207. /* UART RX GPIO pin configuration */
  208. GPIO_InitStruct.Pin = USART1_RX_PIN;
  209. GPIO_InitStruct.Alternate = USART1_RX_AF;
  210. HAL_GPIO_Init(USART1_RX_GPIO_PORT, &GPIO_InitStruct);
  211. /* NVIC for USART */
  212. HAL_NVIC_SetPriority(USART1_IRQn, 0, 1);
  213. HAL_NVIC_EnableIRQ(USART1_IRQn);
  214. }
  215. }
  216. /**
  217. * @brief UART MSP De-Initialization
  218. * This function frees the hardware resources used in this example:
  219. * - Disable the Peripheral's clock
  220. * - Revert GPIO and NVIC configuration to their default state
  221. * @param huart: UART handle pointer
  222. * @retval None
  223. */
  224. void HAL_UART_MspDeInit(UART_HandleTypeDef *huart)
  225. {
  226. if (huart->Instance == USART1)
  227. {
  228. /* Reset peripherals */
  229. USART1_FORCE_RESET();
  230. USART1_RELEASE_RESET();
  231. /* Disable peripherals and GPIO Clocks */
  232. /* Configure UART Tx as alternate function */
  233. HAL_GPIO_DeInit(USART1_TX_GPIO_PORT, USART1_TX_PIN);
  234. /* Configure UART Rx as alternate function */
  235. HAL_GPIO_DeInit(USART1_RX_GPIO_PORT, USART1_RX_PIN);
  236. /* Disable the NVIC for UART */
  237. HAL_NVIC_DisableIRQ(USART1_IRQn);
  238. }
  239. }
  240. int stm32_hw_usart_init(void)
  241. {
  242. struct stm32_uart *uart;
  243. struct serial_configure config = RT_SERIAL_CONFIG_DEFAULT;
  244. #ifdef RT_USING_UART1
  245. uart = &uart1;
  246. uart->UartHandle.Instance = USART1;
  247. serial1.ops = &stm32_uart_ops;
  248. serial1.config = config;
  249. /* register UART1 device */
  250. rt_hw_serial_register(&serial1, "uart1",
  251. RT_DEVICE_FLAG_RDWR | RT_DEVICE_FLAG_INT_RX,
  252. uart);
  253. #endif /* RT_USING_UART1 */
  254. return 0;
  255. }
  256. INIT_BOARD_EXPORT(stm32_hw_usart_init);