usart.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363
  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) 2018 STMicroelectronics International N.V.
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted, provided that the following conditions are met:
  18. *
  19. * 1. Redistribution of source code must retain the above copyright notice,
  20. * this list of conditions and the following disclaimer.
  21. * 2. Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * 3. Neither the name of STMicroelectronics nor the names of other
  25. * contributors to this software may be used to endorse or promote products
  26. * derived from this software without specific written permission.
  27. * 4. This software, including modifications and/or derivative works of this
  28. * software, must execute solely and exclusively on microcontroller or
  29. * microprocessor devices manufactured by or for STMicroelectronics.
  30. * 5. Redistribution and use of this software other than as permitted under
  31. * this license is void and will automatically terminate your rights under
  32. * this license.
  33. *
  34. * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
  35. * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
  36. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
  37. * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
  38. * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
  39. * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  40. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  41. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  42. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  43. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  44. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  45. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  46. *
  47. ******************************************************************************
  48. */
  49. /* Includes ------------------------------------------------------------------*/
  50. #include "usart.h"
  51. #include "gpio.h"
  52. #include "dma.h"
  53. /* USER CODE BEGIN 0 */
  54. /* USER CODE END 0 */
  55. UART_HandleTypeDef huart2;
  56. UART_HandleTypeDef huart3;
  57. UART_HandleTypeDef huart6;
  58. DMA_HandleTypeDef hdma_usart2_rx;
  59. DMA_HandleTypeDef hdma_usart2_tx;
  60. DMA_HandleTypeDef hdma_usart3_rx;
  61. DMA_HandleTypeDef hdma_usart3_tx;
  62. /* USART2 init function */
  63. void MX_USART2_UART_Init(void)
  64. {
  65. huart2.Instance = USART2;
  66. huart2.Init.BaudRate = 9600;
  67. huart2.Init.WordLength = UART_WORDLENGTH_8B;
  68. huart2.Init.StopBits = UART_STOPBITS_1;
  69. huart2.Init.Parity = UART_PARITY_NONE;
  70. huart2.Init.Mode = UART_MODE_TX_RX;
  71. huart2.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  72. huart2.Init.OverSampling = UART_OVERSAMPLING_16;
  73. if (HAL_UART_Init(&huart2) != HAL_OK)
  74. {
  75. _Error_Handler(__FILE__, __LINE__);
  76. }
  77. }
  78. /* USART3 init function */
  79. void MX_USART3_UART_Init(void)
  80. {
  81. huart3.Instance = USART3;
  82. huart3.Init.BaudRate = 460800;
  83. huart3.Init.WordLength = UART_WORDLENGTH_8B;
  84. huart3.Init.StopBits = UART_STOPBITS_1;
  85. huart3.Init.Parity = UART_PARITY_NONE;
  86. huart3.Init.Mode = UART_MODE_TX_RX;
  87. huart3.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  88. huart3.Init.OverSampling = UART_OVERSAMPLING_16;
  89. if (HAL_UART_Init(&huart3) != HAL_OK)
  90. {
  91. _Error_Handler(__FILE__, __LINE__);
  92. }
  93. }
  94. /* USART6 init function */
  95. void MX_USART6_UART_Init(void)
  96. {
  97. huart6.Instance = USART6;
  98. huart6.Init.BaudRate = 9600;
  99. huart6.Init.WordLength = UART_WORDLENGTH_8B;
  100. huart6.Init.StopBits = UART_STOPBITS_1;
  101. huart6.Init.Parity = UART_PARITY_NONE;
  102. huart6.Init.Mode = UART_MODE_TX_RX;
  103. huart6.Init.HwFlowCtl = UART_HWCONTROL_NONE;
  104. huart6.Init.OverSampling = UART_OVERSAMPLING_16;
  105. if (HAL_UART_Init(&huart6) != HAL_OK)
  106. {
  107. _Error_Handler(__FILE__, __LINE__);
  108. }
  109. }
  110. void HAL_UART_MspInit(UART_HandleTypeDef* uartHandle)
  111. {
  112. GPIO_InitTypeDef GPIO_InitStruct;
  113. if(uartHandle->Instance==USART2)
  114. {
  115. /* USER CODE BEGIN USART2_MspInit 0 */
  116. /* USER CODE END USART2_MspInit 0 */
  117. /* USART2 clock enable */
  118. __HAL_RCC_USART2_CLK_ENABLE();
  119. /**USART2 GPIO Configuration
  120. PD5 ------> USART2_TX
  121. PD6 ------> USART2_RX
  122. */
  123. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6;
  124. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  125. GPIO_InitStruct.Pull = GPIO_PULLUP;
  126. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  127. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  128. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  129. /* USART2 DMA Init */
  130. /* USART2_RX Init */
  131. hdma_usart2_rx.Instance = DMA1_Stream5;
  132. hdma_usart2_rx.Init.Channel = DMA_CHANNEL_4;
  133. hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  134. hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  135. hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
  136. hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  137. hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  138. hdma_usart2_rx.Init.Mode = DMA_CIRCULAR;
  139. hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
  140. hdma_usart2_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  141. if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
  142. {
  143. _Error_Handler(__FILE__, __LINE__);
  144. }
  145. __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart2_rx);
  146. /* USART2_TX Init */
  147. hdma_usart2_tx.Instance = DMA1_Stream6;
  148. hdma_usart2_tx.Init.Channel = DMA_CHANNEL_4;
  149. hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  150. hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  151. hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE;
  152. hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  153. hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  154. hdma_usart2_tx.Init.Mode = DMA_NORMAL;
  155. hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW;
  156. hdma_usart2_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  157. if (HAL_DMA_Init(&hdma_usart2_tx) != HAL_OK)
  158. {
  159. _Error_Handler(__FILE__, __LINE__);
  160. }
  161. __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart2_tx);
  162. /* USER CODE BEGIN USART2_MspInit 1 */
  163. /* USER CODE END USART2_MspInit 1 */
  164. }
  165. else if(uartHandle->Instance==USART3)
  166. {
  167. /* USER CODE BEGIN USART3_MspInit 0 */
  168. /* USER CODE END USART3_MspInit 0 */
  169. /* USART3 clock enable */
  170. __HAL_RCC_USART3_CLK_ENABLE();
  171. /**USART3 GPIO Configuration
  172. PB10 ------> USART3_TX
  173. PB11 ------> USART3_RX
  174. */
  175. GPIO_InitStruct.Pin = STLK_RX_Pin|STLK_TX_Pin;
  176. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  177. GPIO_InitStruct.Pull = GPIO_PULLUP;
  178. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  179. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  180. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  181. /* USART3 DMA Init */
  182. /* USART3_RX Init */
  183. hdma_usart3_rx.Instance = DMA1_Stream1;
  184. hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4;
  185. hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  186. hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  187. hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
  188. hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  189. hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  190. hdma_usart3_rx.Init.Mode = DMA_CIRCULAR;
  191. hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
  192. hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  193. if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
  194. {
  195. _Error_Handler(__FILE__, __LINE__);
  196. }
  197. __HAL_LINKDMA(uartHandle,hdmarx,hdma_usart3_rx);
  198. /* USART3_TX Init */
  199. hdma_usart3_tx.Instance = DMA1_Stream3;
  200. hdma_usart3_tx.Init.Channel = DMA_CHANNEL_4;
  201. hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  202. hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  203. hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
  204. hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  205. hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  206. hdma_usart3_tx.Init.Mode = DMA_NORMAL;
  207. hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
  208. hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  209. if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
  210. {
  211. _Error_Handler(__FILE__, __LINE__);
  212. }
  213. __HAL_LINKDMA(uartHandle,hdmatx,hdma_usart3_tx);
  214. /* USART3 interrupt Init */
  215. HAL_NVIC_SetPriority(USART3_IRQn, 0, 0);
  216. HAL_NVIC_EnableIRQ(USART3_IRQn);
  217. /* USER CODE BEGIN USART3_MspInit 1 */
  218. /* USER CODE END USART3_MspInit 1 */
  219. }
  220. else if(uartHandle->Instance==USART6)
  221. {
  222. /* USER CODE BEGIN USART6_MspInit 0 */
  223. /* USER CODE END USART6_MspInit 0 */
  224. /* USART6 clock enable */
  225. __HAL_RCC_USART6_CLK_ENABLE();
  226. /**USART6 GPIO Configuration
  227. PC6 ------> USART6_TX
  228. PC7 ------> USART6_RX
  229. */
  230. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  231. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  232. GPIO_InitStruct.Pull = GPIO_PULLUP;
  233. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  234. GPIO_InitStruct.Alternate = GPIO_AF8_USART6;
  235. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  236. /* USART6 interrupt Init */
  237. HAL_NVIC_SetPriority(USART6_IRQn, 0, 0);
  238. HAL_NVIC_EnableIRQ(USART6_IRQn);
  239. /* USER CODE BEGIN USART6_MspInit 1 */
  240. /* USER CODE END USART6_MspInit 1 */
  241. }
  242. }
  243. void HAL_UART_MspDeInit(UART_HandleTypeDef* uartHandle)
  244. {
  245. if(uartHandle->Instance==USART2)
  246. {
  247. /* USER CODE BEGIN USART2_MspDeInit 0 */
  248. /* USER CODE END USART2_MspDeInit 0 */
  249. /* Peripheral clock disable */
  250. __HAL_RCC_USART2_CLK_DISABLE();
  251. /**USART2 GPIO Configuration
  252. PD5 ------> USART2_TX
  253. PD6 ------> USART2_RX
  254. */
  255. HAL_GPIO_DeInit(GPIOD, GPIO_PIN_5|GPIO_PIN_6);
  256. /* USART2 DMA DeInit */
  257. HAL_DMA_DeInit(uartHandle->hdmarx);
  258. HAL_DMA_DeInit(uartHandle->hdmatx);
  259. /* USER CODE BEGIN USART2_MspDeInit 1 */
  260. /* USER CODE END USART2_MspDeInit 1 */
  261. }
  262. else if(uartHandle->Instance==USART3)
  263. {
  264. /* USER CODE BEGIN USART3_MspDeInit 0 */
  265. /* USER CODE END USART3_MspDeInit 0 */
  266. /* Peripheral clock disable */
  267. __HAL_RCC_USART3_CLK_DISABLE();
  268. /**USART3 GPIO Configuration
  269. PB10 ------> USART3_TX
  270. PB11 ------> USART3_RX
  271. */
  272. HAL_GPIO_DeInit(GPIOB, STLK_RX_Pin|STLK_TX_Pin);
  273. /* USART3 DMA DeInit */
  274. HAL_DMA_DeInit(uartHandle->hdmarx);
  275. HAL_DMA_DeInit(uartHandle->hdmatx);
  276. /* USART3 interrupt Deinit */
  277. HAL_NVIC_DisableIRQ(USART3_IRQn);
  278. /* USER CODE BEGIN USART3_MspDeInit 1 */
  279. /* USER CODE END USART3_MspDeInit 1 */
  280. }
  281. else if(uartHandle->Instance==USART6)
  282. {
  283. /* USER CODE BEGIN USART6_MspDeInit 0 */
  284. /* USER CODE END USART6_MspDeInit 0 */
  285. /* Peripheral clock disable */
  286. __HAL_RCC_USART6_CLK_DISABLE();
  287. /**USART6 GPIO Configuration
  288. PC6 ------> USART6_TX
  289. PC7 ------> USART6_RX
  290. */
  291. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_6|GPIO_PIN_7);
  292. /* USART6 interrupt Deinit */
  293. HAL_NVIC_DisableIRQ(USART6_IRQn);
  294. /* USER CODE BEGIN USART6_MspDeInit 1 */
  295. /* USER CODE END USART6_MspDeInit 1 */
  296. }
  297. }
  298. /* USER CODE BEGIN 1 */
  299. /* USER CODE END 1 */
  300. /**
  301. * @}
  302. */
  303. /**
  304. * @}
  305. */
  306. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/