stm32f2xx_hal_msp.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * File Name : stm32f2xx_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_usart1_tx;
  47. extern DMA_HandleTypeDef hdma_usart3_rx;
  48. extern DMA_HandleTypeDef hdma_usart3_tx;
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* USER CODE BEGIN TD */
  51. /* USER CODE END TD */
  52. /* Private define ------------------------------------------------------------*/
  53. /* USER CODE BEGIN Define */
  54. /* USER CODE END Define */
  55. /* Private macro -------------------------------------------------------------*/
  56. /* USER CODE BEGIN Macro */
  57. /* USER CODE END Macro */
  58. /* Private variables ---------------------------------------------------------*/
  59. /* USER CODE BEGIN PV */
  60. /* USER CODE END PV */
  61. /* Private function prototypes -----------------------------------------------*/
  62. /* USER CODE BEGIN PFP */
  63. /* USER CODE END PFP */
  64. /* External functions --------------------------------------------------------*/
  65. /* USER CODE BEGIN ExternalFunctions */
  66. /* USER CODE END ExternalFunctions */
  67. /* USER CODE BEGIN 0 */
  68. /* USER CODE END 0 */
  69. /**
  70. * Initializes the Global MSP.
  71. */
  72. void HAL_MspInit(void)
  73. {
  74. /* USER CODE BEGIN MspInit 0 */
  75. /* USER CODE END MspInit 0 */
  76. __HAL_RCC_SYSCFG_CLK_ENABLE();
  77. __HAL_RCC_PWR_CLK_ENABLE();
  78. /* System interrupt init*/
  79. /* USER CODE BEGIN MspInit 1 */
  80. /* USER CODE END MspInit 1 */
  81. }
  82. /**
  83. * @brief I2C MSP Initialization
  84. * This function configures the hardware resources used in this example
  85. * @param hi2c: I2C handle pointer
  86. * @retval None
  87. */
  88. void HAL_I2C_MspInit(I2C_HandleTypeDef* hi2c)
  89. {
  90. GPIO_InitTypeDef GPIO_InitStruct = {0};
  91. if(hi2c->Instance==I2C3)
  92. {
  93. /* USER CODE BEGIN I2C3_MspInit 0 */
  94. /* USER CODE END I2C3_MspInit 0 */
  95. __HAL_RCC_GPIOC_CLK_ENABLE();
  96. __HAL_RCC_GPIOA_CLK_ENABLE();
  97. /**I2C3 GPIO Configuration
  98. PC9 ------> I2C3_SDA
  99. PA8 ------> I2C3_SCL
  100. */
  101. GPIO_InitStruct.Pin = GPIO_PIN_9;
  102. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  103. GPIO_InitStruct.Pull = GPIO_PULLUP;
  104. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  105. GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
  106. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  107. GPIO_InitStruct.Pin = GPIO_PIN_8;
  108. GPIO_InitStruct.Mode = GPIO_MODE_AF_OD;
  109. GPIO_InitStruct.Pull = GPIO_PULLUP;
  110. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  111. GPIO_InitStruct.Alternate = GPIO_AF4_I2C3;
  112. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  113. /* Peripheral clock enable */
  114. __HAL_RCC_I2C3_CLK_ENABLE();
  115. /* USER CODE BEGIN I2C3_MspInit 1 */
  116. /* USER CODE END I2C3_MspInit 1 */
  117. }
  118. }
  119. /**
  120. * @brief I2C MSP De-Initialization
  121. * This function freeze the hardware resources used in this example
  122. * @param hi2c: I2C handle pointer
  123. * @retval None
  124. */
  125. void HAL_I2C_MspDeInit(I2C_HandleTypeDef* hi2c)
  126. {
  127. if(hi2c->Instance==I2C3)
  128. {
  129. /* USER CODE BEGIN I2C3_MspDeInit 0 */
  130. /* USER CODE END I2C3_MspDeInit 0 */
  131. /* Peripheral clock disable */
  132. __HAL_RCC_I2C3_CLK_DISABLE();
  133. /**I2C3 GPIO Configuration
  134. PC9 ------> I2C3_SDA
  135. PA8 ------> I2C3_SCL
  136. */
  137. HAL_GPIO_DeInit(GPIOC, GPIO_PIN_9);
  138. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_8);
  139. /* USER CODE BEGIN I2C3_MspDeInit 1 */
  140. /* USER CODE END I2C3_MspDeInit 1 */
  141. }
  142. }
  143. /**
  144. * @brief TIM_Base MSP Initialization
  145. * This function configures the hardware resources used in this example
  146. * @param htim_base: TIM_Base handle pointer
  147. * @retval None
  148. */
  149. void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base)
  150. {
  151. if(htim_base->Instance==TIM6)
  152. {
  153. /* USER CODE BEGIN TIM6_MspInit 0 */
  154. /* USER CODE END TIM6_MspInit 0 */
  155. /* Peripheral clock enable */
  156. __HAL_RCC_TIM6_CLK_ENABLE();
  157. /* USER CODE BEGIN TIM6_MspInit 1 */
  158. /* USER CODE END TIM6_MspInit 1 */
  159. }
  160. }
  161. /**
  162. * @brief TIM_Base MSP De-Initialization
  163. * This function freeze the hardware resources used in this example
  164. * @param htim_base: TIM_Base handle pointer
  165. * @retval None
  166. */
  167. void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base)
  168. {
  169. if(htim_base->Instance==TIM6)
  170. {
  171. /* USER CODE BEGIN TIM6_MspDeInit 0 */
  172. /* USER CODE END TIM6_MspDeInit 0 */
  173. /* Peripheral clock disable */
  174. __HAL_RCC_TIM6_CLK_DISABLE();
  175. /* TIM6 interrupt DeInit */
  176. HAL_NVIC_DisableIRQ(TIM6_DAC_IRQn);
  177. /* USER CODE BEGIN TIM6_MspDeInit 1 */
  178. /* USER CODE END TIM6_MspDeInit 1 */
  179. }
  180. }
  181. /**
  182. * @brief UART MSP Initialization
  183. * This function configures the hardware resources used in this example
  184. * @param huart: UART handle pointer
  185. * @retval None
  186. */
  187. void HAL_UART_MspInit(UART_HandleTypeDef* huart)
  188. {
  189. GPIO_InitTypeDef GPIO_InitStruct = {0};
  190. if(huart->Instance==USART1)
  191. {
  192. /* USER CODE BEGIN USART1_MspInit 0 */
  193. /* USER CODE END USART1_MspInit 0 */
  194. /* Peripheral clock enable */
  195. __HAL_RCC_USART1_CLK_ENABLE();
  196. __HAL_RCC_GPIOA_CLK_ENABLE();
  197. /**USART1 GPIO Configuration
  198. PA9 ------> USART1_TX
  199. PA10 ------> USART1_RX
  200. */
  201. GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_10;
  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 = GPIO_AF7_USART1;
  206. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  207. /* USART1 DMA Init */
  208. /* USART1_RX Init */
  209. hdma_usart1_rx.Instance = DMA2_Stream2;
  210. hdma_usart1_rx.Init.Channel = DMA_CHANNEL_4;
  211. hdma_usart1_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  212. hdma_usart1_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  213. hdma_usart1_rx.Init.MemInc = DMA_MINC_ENABLE;
  214. hdma_usart1_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  215. hdma_usart1_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  216. hdma_usart1_rx.Init.Mode = DMA_NORMAL;
  217. hdma_usart1_rx.Init.Priority = DMA_PRIORITY_LOW;
  218. hdma_usart1_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  219. if (HAL_DMA_Init(&hdma_usart1_rx) != HAL_OK)
  220. {
  221. Error_Handler();
  222. }
  223. __HAL_LINKDMA(huart,hdmarx,hdma_usart1_rx);
  224. /* USART1_TX Init */
  225. hdma_usart1_tx.Instance = DMA2_Stream7;
  226. hdma_usart1_tx.Init.Channel = DMA_CHANNEL_4;
  227. hdma_usart1_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  228. hdma_usart1_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  229. hdma_usart1_tx.Init.MemInc = DMA_MINC_ENABLE;
  230. hdma_usart1_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  231. hdma_usart1_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  232. hdma_usart1_tx.Init.Mode = DMA_NORMAL;
  233. hdma_usart1_tx.Init.Priority = DMA_PRIORITY_LOW;
  234. hdma_usart1_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  235. if (HAL_DMA_Init(&hdma_usart1_tx) != HAL_OK)
  236. {
  237. Error_Handler();
  238. }
  239. __HAL_LINKDMA(huart,hdmatx,hdma_usart1_tx);
  240. /* USER CODE BEGIN USART1_MspInit 1 */
  241. /* USER CODE END USART1_MspInit 1 */
  242. }
  243. else if(huart->Instance==USART3)
  244. {
  245. /* USER CODE BEGIN USART3_MspInit 0 */
  246. /* USER CODE END USART3_MspInit 0 */
  247. /* Peripheral clock enable */
  248. __HAL_RCC_USART3_CLK_ENABLE();
  249. __HAL_RCC_GPIOB_CLK_ENABLE();
  250. /**USART3 GPIO Configuration
  251. PB10 ------> USART3_TX
  252. PB11 ------> USART3_RX
  253. */
  254. GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11;
  255. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  256. GPIO_InitStruct.Pull = GPIO_PULLUP;
  257. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  258. GPIO_InitStruct.Alternate = GPIO_AF7_USART3;
  259. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  260. /* USART3 DMA Init */
  261. /* USART3_RX Init */
  262. hdma_usart3_rx.Instance = DMA1_Stream1;
  263. hdma_usart3_rx.Init.Channel = DMA_CHANNEL_4;
  264. hdma_usart3_rx.Init.Direction = DMA_PERIPH_TO_MEMORY;
  265. hdma_usart3_rx.Init.PeriphInc = DMA_PINC_DISABLE;
  266. hdma_usart3_rx.Init.MemInc = DMA_MINC_ENABLE;
  267. hdma_usart3_rx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  268. hdma_usart3_rx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  269. hdma_usart3_rx.Init.Mode = DMA_NORMAL;
  270. hdma_usart3_rx.Init.Priority = DMA_PRIORITY_LOW;
  271. hdma_usart3_rx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  272. if (HAL_DMA_Init(&hdma_usart3_rx) != HAL_OK)
  273. {
  274. Error_Handler();
  275. }
  276. __HAL_LINKDMA(huart,hdmarx,hdma_usart3_rx);
  277. /* USART3_TX Init */
  278. hdma_usart3_tx.Instance = DMA1_Stream3;
  279. hdma_usart3_tx.Init.Channel = DMA_CHANNEL_4;
  280. hdma_usart3_tx.Init.Direction = DMA_MEMORY_TO_PERIPH;
  281. hdma_usart3_tx.Init.PeriphInc = DMA_PINC_DISABLE;
  282. hdma_usart3_tx.Init.MemInc = DMA_MINC_ENABLE;
  283. hdma_usart3_tx.Init.PeriphDataAlignment = DMA_PDATAALIGN_BYTE;
  284. hdma_usart3_tx.Init.MemDataAlignment = DMA_MDATAALIGN_BYTE;
  285. hdma_usart3_tx.Init.Mode = DMA_NORMAL;
  286. hdma_usart3_tx.Init.Priority = DMA_PRIORITY_LOW;
  287. hdma_usart3_tx.Init.FIFOMode = DMA_FIFOMODE_DISABLE;
  288. if (HAL_DMA_Init(&hdma_usart3_tx) != HAL_OK)
  289. {
  290. Error_Handler();
  291. }
  292. __HAL_LINKDMA(huart,hdmatx,hdma_usart3_tx);
  293. /* USER CODE BEGIN USART3_MspInit 1 */
  294. /* USER CODE END USART3_MspInit 1 */
  295. }
  296. }
  297. /**
  298. * @brief UART MSP De-Initialization
  299. * This function freeze the hardware resources used in this example
  300. * @param huart: UART handle pointer
  301. * @retval None
  302. */
  303. void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
  304. {
  305. if(huart->Instance==USART1)
  306. {
  307. /* USER CODE BEGIN USART1_MspDeInit 0 */
  308. /* USER CODE END USART1_MspDeInit 0 */
  309. /* Peripheral clock disable */
  310. __HAL_RCC_USART1_CLK_DISABLE();
  311. /**USART1 GPIO Configuration
  312. PA9 ------> USART1_TX
  313. PA10 ------> USART1_RX
  314. */
  315. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_9|GPIO_PIN_10);
  316. /* USART1 DMA DeInit */
  317. HAL_DMA_DeInit(huart->hdmarx);
  318. HAL_DMA_DeInit(huart->hdmatx);
  319. /* USART1 interrupt DeInit */
  320. HAL_NVIC_DisableIRQ(USART1_IRQn);
  321. /* USER CODE BEGIN USART1_MspDeInit 1 */
  322. /* USER CODE END USART1_MspDeInit 1 */
  323. }
  324. else if(huart->Instance==USART3)
  325. {
  326. /* USER CODE BEGIN USART3_MspDeInit 0 */
  327. /* USER CODE END USART3_MspDeInit 0 */
  328. /* Peripheral clock disable */
  329. __HAL_RCC_USART3_CLK_DISABLE();
  330. /**USART3 GPIO Configuration
  331. PB10 ------> USART3_TX
  332. PB11 ------> USART3_RX
  333. */
  334. HAL_GPIO_DeInit(GPIOB, GPIO_PIN_10|GPIO_PIN_11);
  335. /* USART3 DMA DeInit */
  336. HAL_DMA_DeInit(huart->hdmarx);
  337. HAL_DMA_DeInit(huart->hdmatx);
  338. /* USART3 interrupt DeInit */
  339. HAL_NVIC_DisableIRQ(USART3_IRQn);
  340. /* USER CODE BEGIN USART3_MspDeInit 1 */
  341. /* USER CODE END USART3_MspDeInit 1 */
  342. }
  343. }
  344. /* USER CODE BEGIN 1 */
  345. /* USER CODE END 1 */
  346. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/