stm32f1xx_hal_msp.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : stm32f1xx_hal_msp.c
  5. * Description : This file provides code for the MSP Initialization
  6. * and de-Initialization codes.
  7. ******************************************************************************
  8. ** This notice applies to any and all portions of this file
  9. * that are not between comment pairs USER CODE BEGIN and
  10. * USER CODE END. Other portions of this file, whether
  11. * inserted by the user or by software development tools
  12. * are owned by their respective copyright owners.
  13. *
  14. * COPYRIGHT(c) 2019 STMicroelectronics
  15. *
  16. * Redistribution and use in source and binary forms, with or without modification,
  17. * are permitted provided that the following conditions are met:
  18. * 1. Redistributions of source code must retain the above copyright notice,
  19. * this list of conditions and the following disclaimer.
  20. * 2. Redistributions in binary form must reproduce the above copyright notice,
  21. * this list of conditions and the following disclaimer in the documentation
  22. * and/or other materials provided with the distribution.
  23. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  24. * may be used to endorse or promote products derived from this software
  25. * without specific prior written permission.
  26. *
  27. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  28. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  29. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  30. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  31. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  32. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  33. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  34. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  35. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  36. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ******************************************************************************
  39. */
  40. /* USER CODE END Header */
  41. /* Includes ------------------------------------------------------------------*/
  42. #include "main.h"
  43. /* USER CODE BEGIN Includes */
  44. /* USER CODE END Includes */
  45. extern DMA_HandleTypeDef hdma_usart1_rx;
  46. extern DMA_HandleTypeDef hdma_usart2_rx;
  47. /* Private typedef -----------------------------------------------------------*/
  48. /* USER CODE BEGIN TD */
  49. /* USER CODE END TD */
  50. /* Private define ------------------------------------------------------------*/
  51. /* USER CODE BEGIN Define */
  52. /* USER CODE END Define */
  53. /* Private macro -------------------------------------------------------------*/
  54. /* USER CODE BEGIN Macro */
  55. /* USER CODE END Macro */
  56. /* Private variables ---------------------------------------------------------*/
  57. /* USER CODE BEGIN PV */
  58. /* USER CODE END PV */
  59. /* Private function prototypes -----------------------------------------------*/
  60. /* USER CODE BEGIN PFP */
  61. /* USER CODE END PFP */
  62. /* External functions --------------------------------------------------------*/
  63. /* USER CODE BEGIN ExternalFunctions */
  64. /* USER CODE END ExternalFunctions */
  65. /* USER CODE BEGIN 0 */
  66. /* USER CODE END 0 */
  67. /**
  68. * Initializes the Global MSP.
  69. */
  70. void HAL_MspInit(void)
  71. {
  72. /* USER CODE BEGIN MspInit 0 */
  73. /* USER CODE END MspInit 0 */
  74. __HAL_RCC_AFIO_CLK_ENABLE();
  75. __HAL_RCC_PWR_CLK_ENABLE();
  76. /* System interrupt init*/
  77. /**NOJTAG: JTAG-DP Disabled and SW-DP Enabled
  78. */
  79. __HAL_AFIO_REMAP_SWJ_NOJTAG();
  80. /* USER CODE BEGIN MspInit 1 */
  81. /* USER CODE END MspInit 1 */
  82. }
  83. /**
  84. * @brief I2C MSP Initialization
  85. * This function configures the hardware resources used in this example
  86. * @param hi2c: I2C handle pointer
  87. * @retval None
  88. */
  89. void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
  90. {
  91. GPIO_InitTypeDef GPIO_InitStruct = {0};
  92. if(hi2c->Instance==I2C2)
  93. {
  94. /* USER CODE BEGIN I2C2_MspInit 0 */
  95. /* USER CODE END I2C2_MspInit 0 */
  96. __HAL_RCC_GPIOB_CLK_ENABLE();
  97. /**I2C2 GPIO Configuration
  98. PB10 ------> I2C2_SCL
  99. PB11 ------> I2C2_SDA
  100. */
  101. GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  102. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  103. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  104. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  105. /* Peripheral clock enable */
  106. __HAL_RCC_I2C2_CLK_ENABLE();
  107. /* USER CODE BEGIN I2C2_MspInit 1 */
  108. /* USER CODE END I2C2_MspInit 1 */
  109. }
  110. }
  111. /**
  112. * @brief I2C MSP De-Initialization
  113. * This function freeze the hardware resources used in this example
  114. * @param hi2c: I2C handle pointer
  115. * @retval None
  116. */
  117. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
  118. {
  119. if(hi2c->Instance==I2C2)
  120. {
  121. /* USER CODE BEGIN I2C2_MspDeInit 0 */
  122. /* USER CODE END I2C2_MspDeInit 0 */
  123. /* Peripheral clock disable */
  124. __HAL_RCC_I2C2_CLK_DISABLE();
  125. /**I2C2 GPIO Configuration
  126. PB10 ------> I2C2_SCL
  127. PB11 ------> I2C2_SDA
  128. */
  129. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
  130. /* USER CODE BEGIN I2C2_MspDeInit 1 */
  131. /* USER CODE END I2C2_MspDeInit 1 */
  132. }
  133. }
  134. /**
  135. * @brief TIM_Base MSP Initialization
  136. * This function configures the hardware resources used in this example
  137. * @param htim_base: TIM_Base handle pointer
  138. * @retval None
  139. */
  140. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
  141. {
  142. if(htim_base->Instance==TIM6)
  143. {
  144. /* USER CODE BEGIN TIM6_MspInit 0 */
  145. /* USER CODE END TIM6_MspInit 0 */
  146. /* Peripheral clock enable */
  147. __HAL_RCC_TIM6_CLK_ENABLE();
  148. /* USER CODE BEGIN TIM6_MspInit 1 */
  149. /* USER CODE END TIM6_MspInit 1 */
  150. }
  151. }
  152. /**
  153. * @brief TIM_Base MSP De-Initialization
  154. * This function freeze the hardware resources used in this example
  155. * @param htim_base: TIM_Base handle pointer
  156. * @retval None
  157. */
  158. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
  159. {
  160. if(htim_base->Instance==TIM6)
  161. {
  162. /* USER CODE BEGIN TIM6_MspDeInit 0 */
  163. /* USER CODE END TIM6_MspDeInit 0 */
  164. /* Peripheral clock disable */
  165. __HAL_RCC_TIM6_CLK_DISABLE();
  166. /* TIM6 interrupt DeInit */
  167. HAL_NVIC_DisableIRQ(TIM6_IRQn);
  168. /* USER CODE BEGIN TIM6_MspDeInit 1 */
  169. /* USER CODE END TIM6_MspDeInit 1 */
  170. }
  171. }
  172. /**
  173. * @brief UART MSP Initialization
  174. * This function configures the hardware resources used in this example
  175. * @param huart: UART handle pointer
  176. * @retval None
  177. */
  178. void HAL_UART_MspInit(UART_HandleTypeDef* huart)
  179. {
  180. GPIO_InitTypeDef GPIO_InitStruct = {0};
  181. if(huart->Instance==USART1)
  182. {
  183. /* USER CODE BEGIN USART1_MspInit 0 */
  184. /* USER CODE END USART1_MspInit 0 */
  185. /* Peripheral clock enable */
  186. __HAL_RCC_USART1_CLK_ENABLE();
  187. __HAL_RCC_GPIOA_CLK_ENABLE();
  188. /**USART1 GPIO Configuration
  189. PA9 ------> USART1_TX
  190. PA10 ------> USART1_RX
  191. */
  192. GPIO_InitStruct.Pin = GPIO_PIN_9;
  193. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  194. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  195. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  196. GPIO_InitStruct.Pin = GPIO_PIN_10;
  197. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  198. GPIO_InitStruct.Pull = GPIO_NOPULL;
  199. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  200. /* USART1 DMA Init */
  201. /* USART1_RX Init */
  202. hdma_usart1_rx.Instance = DMA1_Channel5;
  203. hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  204. hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  205. hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
  206. hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  207. hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  208. hdma_usart1_rx.Init.Mode = DMA_CIRCULAR;
  209. hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
  210. if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
  211. {
  212. Error_Handler();
  213. }
  214. __HAL_LINKDMA(huart,hdmarx,hdma_usart1_rx);
  215. /* USER CODE BEGIN USART1_MspInit 1 */
  216. /* USER CODE END USART1_MspInit 1 */
  217. }
  218. else if(huart->Instance==USART2)
  219. {
  220. /* USER CODE BEGIN USART2_MspInit 0 */
  221. /* USER CODE END USART2_MspInit 0 */
  222. /* Peripheral clock enable */
  223. __HAL_RCC_USART2_CLK_ENABLE();
  224. __HAL_RCC_GPIOA_CLK_ENABLE();
  225. /**USART2 GPIO Configuration
  226. PA2 ------> USART2_TX
  227. PA3 ------> USART2_RX
  228. */
  229. GPIO_InitStruct.Pin = GPIO_PIN_2;
  230. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  231. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
  232. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  233. GPIO_InitStruct.Pin = GPIO_PIN_3;
  234. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  235. GPIO_InitStruct.Pull = GPIO_NOPULL;
  236. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  237. /* USART2 DMA Init */
  238. /* USART2_RX Init */
  239. hdma_usart2_rx.Instance = DMA1_Channel6;
  240. hdma_usart2_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  241. hdma_usart2_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  242. hdma_usart2_rx.Init.MemInc = DMA_MINC_ENABLE;
  243. hdma_usart2_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  244. hdma_usart2_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  245. hdma_usart2_rx.Init.Mode = DMA_CIRCULAR;
  246. hdma_usart2_rx.Init.Priority = DMA_PRIORITY_LOW;
  247. if (HAL_DMA_Init(&hdma_usart2_rx) != HAL_OK)
  248. {
  249. Error_Handler();
  250. }
  251. __HAL_LINKDMA(huart,hdmarx,hdma_usart2_rx);
  252. /* USER CODE BEGIN USART2_MspInit 1 */
  253. /* USER CODE END USART2_MspInit 1 */
  254. }
  255. }
  256. /**
  257. * @brief UART MSP De-Initialization
  258. * This function freeze the hardware resources used in this example
  259. * @param huart: UART handle pointer
  260. * @retval None
  261. */
  262. void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
  263. {
  264. if(huart->Instance==USART1)
  265. {
  266. /* USER CODE BEGIN USART1_MspDeInit 0 */
  267. /* USER CODE END USART1_MspDeInit 0 */
  268. /* Peripheral clock disable */
  269. __HAL_RCC_USART1_CLK_DISABLE();
  270. /**USART1 GPIO Configuration
  271. PA9 ------> USART1_TX
  272. PA10 ------> USART1_RX
  273. */
  274. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  275. /* USART1 DMA DeInit */
  276. HAL_DMA_DeInit(huart->hdmarx);
  277. /* USART1 interrupt DeInit */
  278. HAL_NVIC_DisableIRQ(USART1_IRQn);
  279. /* USER CODE BEGIN USART1_MspDeInit 1 */
  280. /* USER CODE END USART1_MspDeInit 1 */
  281. }
  282. else if(huart->Instance==USART2)
  283. {
  284. /* USER CODE BEGIN USART2_MspDeInit 0 */
  285. /* USER CODE END USART2_MspDeInit 0 */
  286. /* Peripheral clock disable */
  287. __HAL_RCC_USART2_CLK_DISABLE();
  288. /**USART2 GPIO Configuration
  289. PA2 ------> USART2_TX
  290. PA3 ------> USART2_RX
  291. */
  292. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_2|GPIO_PIN_3);
  293. /* USART2 DMA DeInit */
  294. HAL_DMA_DeInit(huart->hdmarx);
  295. /* USART2 interrupt DeInit */
  296. HAL_NVIC_DisableIRQ(USART2_IRQn);
  297. /* USER CODE BEGIN USART2_MspDeInit 1 */
  298. /* USER CODE END USART2_MspDeInit 1 */
  299. }
  300. }
  301. /* USER CODE BEGIN 1 */
  302. /* USER CODE END 1 */
  303. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/