usart.c 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /**
  2. ******************************************************************************
  3. * File Name : USART.c
  4. * Description : This file provides code for the configuration
  5. * of the USART instances.
  6. ******************************************************************************
  7. ** This notice applies to any and all portions of this file
  8. * that are not between comment pairs USER CODE BEGIN and
  9. * USER CODE END. Other portions of this file, whether
  10. * inserted by the user or by software development tools
  11. * are owned by their respective copyright owners.
  12. *
  13. * COPYRIGHT(c) 2019 STMicroelectronics
  14. *
  15. * Redistribution and use in source and binary forms, with or without modification,
  16. * are permitted provided that the following conditions are met:
  17. * 1. Redistributions of source code must retain the above copyright notice,
  18. * this list of conditions and the following disclaimer.
  19. * 2. Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  23. * may be used to endorse or promote products derived from this software
  24. * without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  29. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  30. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  31. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  32. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  33. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  34. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  35. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "usart.h"
  41. #include "gpio.h"
  42. #include "dma.h"
  43. /* USER CODE BEGIN 0 */
  44. /* USER CODE END 0 */
  45. UART_HandleTypeDef huart1;
  46. UART_HandleTypeDef huart2;
  47. UART_HandleTypeDef huart3;
  48. DMA_HandleTypeDef hdma_usart2_rx;
  49. DMA_HandleTypeDef hdma_usart2_tx;
  50. /* USART1 init function */
  51. void MX_USART1_UART_Init(void)
  52. {
  53. huart1.Instance = USART1;
  54. huart1.Init.BaudRate = 115200;
  55. huart1.Init.WordLength = UART_WORDLENGTH_8B;
  56. huart1.Init.StopBits = UART_STOPBITS_1;
  57. huart1.Init.Parity = UART_PARITY_NONE;
  58. huart1.Init.Mode = UART_MODE_TX_RX;
  59. huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  60. huart1.Init.OverSampling = UART_OVERSAMPLING_16;
  61. if (HAL_UART_Init(&huart1) != HAL_OK)
  62. {
  63. _Error_Handler(__FILE__, __LINE__);
  64. }
  65. }
  66. /* USART2 init function */
  67. void MX_USART2_UART_Init(void)
  68. {
  69. huart2.Instance = USART2;
  70. huart2.Init.BaudRate = 115200;
  71. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  72. huart2.Init.StopBits = UART_STOPBITS_1;
  73. huart2.Init.Parity = UART_PARITY_NONE;
  74. huart2.Init.Mode = UART_MODE_TX_RX;
  75. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  76. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  77. if (HAL_UART_Init(&huart2) != HAL_OK)
  78. {
  79. _Error_Handler(__FILE__, __LINE__);
  80. }
  81. }
  82. /* USART3 init function */
  83. void MX_USART3_UART_Init(void)
  84. {
  85. huart3.Instance = USART3;
  86. huart3.Init.BaudRate = 115200;
  87. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  88. huart3.Init.StopBits = UART_STOPBITS_1;
  89. huart3.Init.Parity = UART_PARITY_NONE;
  90. huart3.Init.Mode = UART_MODE_TX_RX;
  91. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  92. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  93. if (HAL_UART_Init(&huart3) != HAL_OK)
  94. {
  95. _Error_Handler(__FILE__, __LINE__);
  96. }
  97. }
  98. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  99. {
  100. GPIO_InitTypeDef GPIO_InitStruct;
  101. if(uartHandle->Instance==USART1)
  102. {
  103. /* USER CODE BEGIN USART1_MspInit 0 */
  104. /* USER CODE END USART1_MspInit 0 */
  105. /* USART1 clock enable */
  106. __HAL_RCC_USART1_CLK_ENABLE();
  107. /**USART1 GPIO Configuration
  108. PA9 ------> USART1_TX
  109. PA10 ------> USART1_RX
  110. */
  111. GPIO_InitStruct.Pin = GPIO_PIN_9;
  112. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  113. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  114. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  115. GPIO_InitStruct.Pin = GPIO_PIN_10;
  116. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  117. GPIO_InitStruct.Pull = GPIO_NOPULL;
  118. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  119. /* USART1 interrupt Init */
  120. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  121. HAL_NVIC_EnableIRQ(USART1_IRQn);
  122. /* USER CODE BEGIN USART1_MspInit 1 */
  123. /* USER CODE END USART1_MspInit 1 */
  124. }
  125. else if(uartHandle->Instance==USART2)
  126. {
  127. /* USER CODE BEGIN USART2_MspInit 0 */
  128. /* USER CODE END USART2_MspInit 0 */
  129. /* USART2 clock enable */
  130. __HAL_RCC_USART2_CLK_ENABLE();
  131. /**USART2 GPIO Configuration
  132. PA2 ------> USART2_TX
  133. PA3 ------> USART2_RX
  134. */
  135. GPIO_InitStruct.Pin = GPIO_PIN_2;
  136. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  137. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  138. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  139. GPIO_InitStruct.Pin = GPIO_PIN_3;
  140. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  141. GPIO_InitStruct.Pull = GPIO_NOPULL;
  142. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  143. /* USART2 DMA Init */
  144. /* USART2_RX Init */
  145. hdma_usart2_rx.Instance = DMA1_Channel6;
  146. hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  147. hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  148. hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
  149. hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  150. hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  151. hdma_usart2_rx.Init.Mode = DMA_NORMAL;
  152. hdma_usart2_rx.Init.Priority = DMA_PRIORITY_HIGH;
  153. if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
  154. {
  155. _Error_Handler(__FILE__, __LINE__);
  156. }
  157. __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart2_rx);
  158. /* USART2_TX Init */
  159. hdma_usart2_tx.Instance = DMA1_Channel7;
  160. hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  161. hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  162. hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE;
  163. hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  164. hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  165. hdma_usart2_tx.Init.Mode = DMA_NORMAL;
  166. hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW;
  167. if (HAL_DMA_Init(&hdma_usart2_tx) != HAL_OK)
  168. {
  169. _Error_Handler(__FILE__, __LINE__);
  170. }
  171. __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart2_tx);
  172. /* USART2 interrupt Init */
  173. HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  174. HAL_NVIC_EnableIRQ(USART2_IRQn);
  175. /* USER CODE BEGIN USART2_MspInit 1 */
  176. /* USER CODE END USART2_MspInit 1 */
  177. }
  178. else if(uartHandle->Instance==USART3)
  179. {
  180. /* USER CODE BEGIN USART3_MspInit 0 */
  181. /* USER CODE END USART3_MspInit 0 */
  182. /* USART3 clock enable */
  183. __HAL_RCC_USART3_CLK_ENABLE();
  184. /**USART3 GPIO Configuration
  185. PB10 ------> USART3_TX
  186. PB11 ------> USART3_RX
  187. */
  188. GPIO_InitStruct.Pin = GPIO_PIN_10;
  189. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  190. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  191. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  192. GPIO_InitStruct.Pin = GPIO_PIN_11;
  193. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  194. GPIO_InitStruct.Pull = GPIO_NOPULL;
  195. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  196. /* USART3 interrupt Init */
  197. HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
  198. HAL_NVIC_EnableIRQ(USART3_IRQn);
  199. /* USER CODE BEGIN USART3_MspInit 1 */
  200. /* USER CODE END USART3_MspInit 1 */
  201. }
  202. }
  203. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  204. {
  205. if(uartHandle->Instance==USART1)
  206. {
  207. /* USER CODE BEGIN USART1_MspDeInit 0 */
  208. /* USER CODE END USART1_MspDeInit 0 */
  209. /* Peripheral clock disable */
  210. __HAL_RCC_USART1_CLK_DISABLE();
  211. /**USART1 GPIO Configuration
  212. PA9 ------> USART1_TX
  213. PA10 ------> USART1_RX
  214. */
  215. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  216. /* USART1 interrupt Deinit */
  217. HAL_NVIC_DisableIRQ(USART1_IRQn);
  218. /* USER CODE BEGIN USART1_MspDeInit 1 */
  219. /* USER CODE END USART1_MspDeInit 1 */
  220. }
  221. else if(uartHandle->Instance==USART2)
  222. {
  223. /* USER CODE BEGIN USART2_MspDeInit 0 */
  224. /* USER CODE END USART2_MspDeInit 0 */
  225. /* Peripheral clock disable */
  226. __HAL_RCC_USART2_CLK_DISABLE();
  227. /**USART2 GPIO Configuration
  228. PA2 ------> USART2_TX
  229. PA3 ------> USART2_RX
  230. */
  231. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);
  232. /* USART2 DMA DeInit */
  233. HAL_DMA_DeInit(uartHandle->hdmarx);
  234. HAL_DMA_DeInit(uartHandle->hdmatx);
  235. /* USART2 interrupt Deinit */
  236. HAL_NVIC_DisableIRQ(USART2_IRQn);
  237. /* USER CODE BEGIN USART2_MspDeInit 1 */
  238. /* USER CODE END USART2_MspDeInit 1 */
  239. }
  240. else if(uartHandle->Instance==USART3)
  241. {
  242. /* USER CODE BEGIN USART3_MspDeInit 0 */
  243. /* USER CODE END USART3_MspDeInit 0 */
  244. /* Peripheral clock disable */
  245. __HAL_RCC_USART3_CLK_DISABLE();
  246. /**USART3 GPIO Configuration
  247. PB10 ------> USART3_TX
  248. PB11 ------> USART3_RX
  249. */
  250. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
  251. /* USART3 interrupt Deinit */
  252. HAL_NVIC_DisableIRQ(USART3_IRQn);
  253. /* USER CODE BEGIN USART3_MspDeInit 1 */
  254. /* USER CODE END USART3_MspDeInit 1 */
  255. }
  256. }
  257. /* USER CODE BEGIN 1 */
  258. /* USER CODE END 1 */
  259. /**
  260. * @}
  261. */
  262. /**
  263. * @}
  264. */
  265. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/