stm32l4xx_hal_msp.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file stm32l4xx_hal_msp.c
  5. * @brief This file provides code for the MSP Initialization
  6. * and de-Initialization codes.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2025 STMicroelectronics.
  11. * All rights reserved.
  12. *
  13. * This software is licensed under terms that can be found in the LICENSE file
  14. * in the root directory of this software component.
  15. * If no LICENSE file comes with this software, it is provided AS-IS.
  16. *
  17. ******************************************************************************
  18. */
  19. /* USER CODE END Header */
  20. /* Includes ------------------------------------------------------------------*/
  21. #include "main.h"
  22. /* USER CODE BEGIN Includes */
  23. /* USER CODE END Includes */
  24. extern DMA_HandleTypeDef hdma_usart1_rx;
  25. extern DMA_HandleTypeDef hdma_usart1_tx;
  26. extern DMA_HandleTypeDef hdma_usart2_rx;
  27. extern DMA_HandleTypeDef hdma_usart2_tx;
  28. /* Private typedef -----------------------------------------------------------*/
  29. /* USER CODE BEGIN TD */
  30. /* USER CODE END TD */
  31. /* Private define ------------------------------------------------------------*/
  32. /* USER CODE BEGIN Define */
  33. /* USER CODE END Define */
  34. /* Private macro -------------------------------------------------------------*/
  35. /* USER CODE BEGIN Macro */
  36. /* USER CODE END Macro */
  37. /* Private variables ---------------------------------------------------------*/
  38. /* USER CODE BEGIN PV */
  39. /* USER CODE END PV */
  40. /* Private function prototypes -----------------------------------------------*/
  41. /* USER CODE BEGIN PFP */
  42. /* USER CODE END PFP */
  43. /* External functions --------------------------------------------------------*/
  44. /* USER CODE BEGIN ExternalFunctions */
  45. /* USER CODE END ExternalFunctions */
  46. /* USER CODE BEGIN 0 */
  47. /* USER CODE END 0 */
  48. /**
  49. * Initializes the Global MSP.
  50. */
  51. void HAL_MspInit(void)
  52. {
  53. /* USER CODE BEGIN MspInit 0 */
  54. /* USER CODE END MspInit 0 */
  55. __HAL_RCC_SYSCFG_CLK_ENABLE();
  56. __HAL_RCC_PWR_CLK_ENABLE();
  57. /* System interrupt init*/
  58. /* USER CODE BEGIN MspInit 1 */
  59. /* USER CODE END MspInit 1 */
  60. }
  61. /**
  62. * @brief UART MSP Initialization
  63. * This function configures the hardware resources used in this example
  64. * @param huart: UART handle pointer
  65. * @retval None
  66. */
  67. void HAL_UART_MspInit(UART_HandleTypeDef* huart)
  68. {
  69. GPIO_InitTypeDef GPIO_InitStruct = {0};
  70. RCC_PeriphCLKInitTypeDef PeriphClkInit = {0};
  71. if(huart->Instance==USART1)
  72. {
  73. /* USER CODE BEGIN USART1_MspInit 0 */
  74. /* USER CODE END USART1_MspInit 0 */
  75. /** Initializes the peripherals clock
  76. */
  77. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART1;
  78. PeriphClkInit.Usart1ClockSelection = RCC_USART1CLKSOURCE_PCLK2;
  79. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  80. {
  81. Error_Handler();
  82. }
  83. /* Peripheral clock enable */
  84. __HAL_RCC_USART1_CLK_ENABLE();
  85. __HAL_RCC_GPIOA_CLK_ENABLE();
  86. /**USART1 GPIO Configuration
  87. PA9 ------> USART1_TX
  88. PA10 ------> USART1_RX
  89. PA12 ------> USART1_DE
  90. */
  91. GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_12;
  92. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  93. GPIO_InitStruct.Pull = GPIO_NOPULL;
  94. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  95. GPIO_InitStruct.Alternate = GPIO_AF7_USART1;
  96. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  97. /* USART1 DMA Init */
  98. /* USART1_RX Init */
  99. hdma_usart1_rx.Instance = DMA1_Channel5;
  100. hdma_usart1_rx.Init.Request = DMA_REQUEST_2;
  101. hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  102. hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  103. hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
  104. hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  105. hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  106. hdma_usart1_rx.Init.Mode = DMA_NORMAL;
  107. hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
  108. if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
  109. {
  110. Error_Handler();
  111. }
  112. __HAL_LINKDMA(huart,hdmarx,hdma_usart1_rx);
  113. /* USART1_TX Init */
  114. hdma_usart1_tx.Instance = DMA1_Channel4;
  115. hdma_usart1_tx.Init.Request = DMA_REQUEST_2;
  116. hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  117. hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  118. hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
  119. hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  120. hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  121. hdma_usart1_tx.Init.Mode = DMA_NORMAL;
  122. hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW;
  123. if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
  124. {
  125. Error_Handler();
  126. }
  127. __HAL_LINKDMA(huart,hdmatx,hdma_usart1_tx);
  128. /* USART1 interrupt Init */
  129. HAL_NVIC_SetPriority(USART1_IRQn, 0, 0);
  130. HAL_NVIC_EnableIRQ(USART1_IRQn);
  131. /* USER CODE BEGIN USART1_MspInit 1 */
  132. /* USER CODE END USART1_MspInit 1 */
  133. }
  134. else if(huart->Instance==USART2)
  135. {
  136. /* USER CODE BEGIN USART2_MspInit 0 */
  137. /* USER CODE END USART2_MspInit 0 */
  138. /** Initializes the peripherals clock
  139. */
  140. PeriphClkInit.PeriphClockSelection = RCC_PERIPHCLK_USART2;
  141. PeriphClkInit.Usart2ClockSelection = RCC_USART2CLKSOURCE_PCLK1;
  142. if (HAL_RCCEx_PeriphCLKConfig(&PeriphClkInit) != HAL_OK)
  143. {
  144. Error_Handler();
  145. }
  146. /* Peripheral clock enable */
  147. __HAL_RCC_USART2_CLK_ENABLE();
  148. __HAL_RCC_GPIOA_CLK_ENABLE();
  149. /**USART2 GPIO Configuration
  150. PA2 ------> USART2_TX
  151. PA15 (JTDI) ------> USART2_RX
  152. */
  153. GPIO_InitStruct.Pin = VCP_TX_Pin;
  154. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  155. GPIO_InitStruct.Pull = GPIO_NOPULL;
  156. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  157. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  158. HAL_GPIO_Init(VCP_TX_GPIO_Port, &GPIO_InitStruct);
  159. GPIO_InitStruct.Pin = VCP_RX_Pin;
  160. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  161. GPIO_InitStruct.Pull = GPIO_NOPULL;
  162. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  163. GPIO_InitStruct.Alternate = GPIO_AF3_USART2;
  164. HAL_GPIO_Init(VCP_RX_GPIO_Port, &GPIO_InitStruct);
  165. /* USART2 DMA Init */
  166. /* USART2_RX Init */
  167. hdma_usart2_rx.Instance = DMA1_Channel6;
  168. hdma_usart2_rx.Init.Request = DMA_REQUEST_2;
  169. hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  170. hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  171. hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
  172. hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  173. hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  174. hdma_usart2_rx.Init.Mode = DMA_NORMAL;
  175. hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
  176. if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
  177. {
  178. Error_Handler();
  179. }
  180. __HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx);
  181. /* USART2_TX Init */
  182. hdma_usart2_tx.Instance = DMA1_Channel7;
  183. hdma_usart2_tx.Init.Request = DMA_REQUEST_2;
  184. hdma_usart2_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  185. hdma_usart2_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  186. hdma_usart2_tx.Init.MemInc = DMA_MINC_ENABLE;
  187. hdma_usart2_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  188. hdma_usart2_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  189. hdma_usart2_tx.Init.Mode = DMA_NORMAL;
  190. hdma_usart2_tx.Init.Priority = DMA_PRIORITY_LOW;
  191. if (HAL_DMA_Init(&hdma_usart2_tx) != HAL_OK)
  192. {
  193. Error_Handler();
  194. }
  195. __HAL_LINKDMA(huart,hdmatx,hdma_usart2_tx);
  196. /* USART2 interrupt Init */
  197. HAL_NVIC_SetPriority(USART2_IRQn, 0, 0);
  198. HAL_NVIC_EnableIRQ(USART2_IRQn);
  199. /* USER CODE BEGIN USART2_MspInit 1 */
  200. /* USER CODE END USART2_MspInit 1 */
  201. }
  202. }
  203. /**
  204. * @brief UART MSP De-Initialization
  205. * This function freeze the hardware resources used in this example
  206. * @param huart: UART handle pointer
  207. * @retval None
  208. */
  209. void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
  210. {
  211. if(huart->Instance==USART1)
  212. {
  213. /* USER CODE BEGIN USART1_MspDeInit 0 */
  214. /* USER CODE END USART1_MspDeInit 0 */
  215. /* Peripheral clock disable */
  216. __HAL_RCC_USART1_CLK_DISABLE();
  217. /**USART1 GPIO Configuration
  218. PA9 ------> USART1_TX
  219. PA10 ------> USART1_RX
  220. PA12 ------> USART1_DE
  221. */
  222. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10|GPIO_PIN_12);
  223. /* USART1 DMA DeInit */
  224. HAL_DMA_DeInit(huart->hdmarx);
  225. HAL_DMA_DeInit(huart->hdmatx);
  226. /* USART1 interrupt DeInit */
  227. HAL_NVIC_DisableIRQ(USART1_IRQn);
  228. /* USER CODE BEGIN USART1_MspDeInit 1 */
  229. /* USER CODE END USART1_MspDeInit 1 */
  230. }
  231. else if(huart->Instance==USART2)
  232. {
  233. /* USER CODE BEGIN USART2_MspDeInit 0 */
  234. /* USER CODE END USART2_MspDeInit 0 */
  235. /* Peripheral clock disable */
  236. __HAL_RCC_USART2_CLK_DISABLE();
  237. /**USART2 GPIO Configuration
  238. PA2 ------> USART2_TX
  239. PA15 (JTDI) ------> USART2_RX
  240. */
  241. HAL_GPIO_DeInit(GPIOA, VCP_TX_Pin|VCP_RX_Pin);
  242. /* USART2 DMA DeInit */
  243. HAL_DMA_DeInit(huart->hdmarx);
  244. HAL_DMA_DeInit(huart->hdmatx);
  245. /* USART2 interrupt DeInit */
  246. HAL_NVIC_DisableIRQ(USART2_IRQn);
  247. /* USER CODE BEGIN USART2_MspDeInit 1 */
  248. /* USER CODE END USART2_MspDeInit 1 */
  249. }
  250. }
  251. /* USER CODE BEGIN 1 */
  252. /* USER CODE END 1 */