You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

stm32f4xx_hal_msp.c 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file stm32f4xx_hal_msp.c
  5. * @brief This file provides code for the MSP Initialization
  6. * and de-Initialization codes.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * <h2><center>&copy; Copyright (c) 2021 STMicroelectronics.
  11. * All rights reserved.</center></h2>
  12. *
  13. * This software component is licensed by ST under BSD 3-Clause license,
  14. * the "License"; You may not use this file except in compliance with the
  15. * License. You may obtain a copy of the License at:
  16. * opensource.org/licenses/BSD-3-Clause
  17. *
  18. ******************************************************************************
  19. */
  20. /* USER CODE END Header */
  21. /* Includes ------------------------------------------------------------------*/
  22. #include "main.h"
  23. /* USER CODE BEGIN Includes */
  24. /* USER CODE END Includes */
  25. /* Private typedef -----------------------------------------------------------*/
  26. /* USER CODE BEGIN TD */
  27. /* USER CODE END TD */
  28. /* Private define ------------------------------------------------------------*/
  29. /* USER CODE BEGIN Define */
  30. /* USER CODE END Define */
  31. /* Private macro -------------------------------------------------------------*/
  32. /* USER CODE BEGIN Macro */
  33. /* USER CODE END Macro */
  34. /* Private variables ---------------------------------------------------------*/
  35. /* USER CODE BEGIN PV */
  36. /* USER CODE END PV */
  37. /* Private function prototypes -----------------------------------------------*/
  38. /* USER CODE BEGIN PFP */
  39. extern void BSP_MotorControl_StepClockHandler(uint8_t deviceId);
  40. extern void BSP_MotorControl_FlagInterruptHandler(void);
  41. extern void ButtonHandler(void);
  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. HAL_NVIC_SetPriorityGrouping(NVIC_PRIORITYGROUP_0);
  58. /* System interrupt init*/
  59. /* USER CODE BEGIN MspInit 1 */
  60. /* USER CODE END MspInit 1 */
  61. }
  62. /**
  63. * @brief RTC MSP Initialization
  64. * This function configures the hardware resources used in this example
  65. * @param hrtc: RTC handle pointer
  66. * @retval None
  67. */
  68. void HAL_RTC_MspInit(RTC_HandleTypeDef* hrtc)
  69. {
  70. if(hrtc->Instance==RTC)
  71. {
  72. /* USER CODE BEGIN RTC_MspInit 0 */
  73. /* USER CODE END RTC_MspInit 0 */
  74. /* Peripheral clock enable */
  75. __HAL_RCC_RTC_ENABLE();
  76. /* RTC interrupt Init */
  77. HAL_NVIC_SetPriority(RTC_Alarm_IRQn, 0, 0);
  78. HAL_NVIC_EnableIRQ(RTC_Alarm_IRQn);
  79. /* USER CODE BEGIN RTC_MspInit 1 */
  80. /* USER CODE END RTC_MspInit 1 */
  81. }
  82. }
  83. /**
  84. * @brief RTC MSP De-Initialization
  85. * This function freeze the hardware resources used in this example
  86. * @param hrtc: RTC handle pointer
  87. * @retval None
  88. */
  89. void HAL_RTC_MspDeInit(RTC_HandleTypeDef* hrtc)
  90. {
  91. if(hrtc->Instance==RTC)
  92. {
  93. /* USER CODE BEGIN RTC_MspDeInit 0 */
  94. /* USER CODE END RTC_MspDeInit 0 */
  95. /* Peripheral clock disable */
  96. __HAL_RCC_RTC_DISABLE();
  97. /* RTC interrupt DeInit */
  98. HAL_NVIC_DisableIRQ(RTC_Alarm_IRQn);
  99. /* USER CODE BEGIN RTC_MspDeInit 1 */
  100. /* USER CODE END RTC_MspDeInit 1 */
  101. }
  102. }
  103. /**
  104. * @brief UART MSP Initialization
  105. * This function configures the hardware resources used in this example
  106. * @param huart: UART handle pointer
  107. * @retval None
  108. */
  109. void HAL_UART_MspInit(UART_HandleTypeDef* huart)
  110. {
  111. GPIO_InitTypeDef GPIO_InitStruct = {0};
  112. if(huart->Instance==USART2)
  113. {
  114. /* USER CODE BEGIN USART2_MspInit 0 */
  115. /* USER CODE END USART2_MspInit 0 */
  116. /* Peripheral clock enable */
  117. __HAL_RCC_USART2_CLK_ENABLE();
  118. __HAL_RCC_GPIOA_CLK_ENABLE();
  119. /**USART2 GPIO Configuration
  120. PA2 ------> USART2_TX
  121. PA3 ------> USART2_RX
  122. */
  123. GPIO_InitStruct.Pin = USART_TX_Pin|USART_RX_Pin;
  124. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  125. GPIO_InitStruct.Pull = GPIO_NOPULL;
  126. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  127. GPIO_InitStruct.Alternate = GPIO_AF7_USART2;
  128. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  129. /* USER CODE BEGIN USART2_MspInit 1 */
  130. /* USER CODE END USART2_MspInit 1 */
  131. }
  132. }
  133. /**
  134. * @brief UART MSP De-Initialization
  135. * This function freeze the hardware resources used in this example
  136. * @param huart: UART handle pointer
  137. * @retval None
  138. */
  139. void HAL_UART_MspDeInit(UART_HandleTypeDef* huart)
  140. {
  141. if(huart->Instance==USART2)
  142. {
  143. /* USER CODE BEGIN USART2_MspDeInit 0 */
  144. /* USER CODE END USART2_MspDeInit 0 */
  145. /* Peripheral clock disable */
  146. __HAL_RCC_USART2_CLK_DISABLE();
  147. /**USART2 GPIO Configuration
  148. PA2 ------> USART2_TX
  149. PA3 ------> USART2_RX
  150. */
  151. HAL_GPIO_DeInit(GPIOA, USART_TX_Pin|USART_RX_Pin);
  152. /* USER CODE BEGIN USART2_MspDeInit 1 */
  153. /* USER CODE END USART2_MspDeInit 1 */
  154. }
  155. }
  156. /**
  157. * @brief PWM MSP Initialization
  158. * @param[in] htim_pwm PWM handle pointer
  159. * @retval None
  160. */
  161. void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* htim_pwm)
  162. {
  163. GPIO_InitTypeDef GPIO_InitStruct;
  164. if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_TICK)
  165. {
  166. /* Peripheral clock enable */
  167. __BSP_MOTOR_CONTROL_BOARD_TIMER_TICK_CLCK_ENABLE();
  168. /* Set Interrupt Group Priority of Timer Interrupt*/
  169. HAL_NVIC_SetPriority(BSP_MOTOR_CONTROL_BOARD_TIMER_TICK_IRQn, BSP_MOTOR_CONTROL_BOARD_TIMER_TICK_PRIORITY, 0);
  170. /* Enable the timer global Interrupt */
  171. HAL_NVIC_EnableIRQ(BSP_MOTOR_CONTROL_BOARD_TIMER_TICK_IRQn);
  172. }
  173. if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_VREFA_PWM)
  174. {
  175. /* Peripheral clock enable */
  176. __BSP_MOTOR_CONTROL_BOARD_TIMER_VREFA_PWM_CLCK_ENABLE();
  177. /* Configure L6208 - VREFA pin -------------------------------------*/
  178. GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_VREFA_PIN;
  179. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  180. GPIO_InitStruct.Pull = GPIO_NOPULL;
  181. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  182. GPIO_InitStruct.Alternate = BSP_MOTOR_CONTROL_BOARD_AFx_TIMx_VREFA_PWM;
  183. HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_VREFA_PORT, &GPIO_InitStruct);
  184. }
  185. if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_VREFB_PWM)
  186. {
  187. /* Peripheral clock enable */
  188. __BSP_MOTOR_CONTROL_BOARD_TIMER_VREFB_PWM_CLCK_ENABLE();
  189. /* Configure L6208 - VREFB pin -------------------------------------*/
  190. GPIO_InitStruct.Pin = BSP_MOTOR_CONTROL_BOARD_VREFB_PIN;
  191. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  192. GPIO_InitStruct.Pull = GPIO_NOPULL;
  193. GPIO_InitStruct.Speed = GPIO_SPEED_MEDIUM;
  194. GPIO_InitStruct.Alternate = BSP_MOTOR_CONTROL_BOARD_AFx_TIMx_VREFB_PWM;
  195. HAL_GPIO_Init(BSP_MOTOR_CONTROL_BOARD_VREFB_PORT, &GPIO_InitStruct);
  196. }
  197. }
  198. /**
  199. * @brief PWM MSP De-Initialization
  200. * @param[in] htim_pwm PWM handle pointer
  201. * @retval None
  202. */
  203. void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* htim_pwm)
  204. {
  205. if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_TICK)
  206. {
  207. /* Peripheral clock disable */
  208. __BSP_MOTOR_CONTROL_BOARD_TIMER_TICK_CLCK_DISABLE();
  209. }
  210. if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_VREFA_PWM)
  211. {
  212. /* Peripheral clock disable */
  213. __BSP_MOTOR_CONTROL_BOARD_TIMER_VREFA_PWM_CLCK_DISABLE();
  214. /* GPIO Deconfiguration */
  215. HAL_GPIO_DeInit(BSP_MOTOR_CONTROL_BOARD_VREFA_PORT, BSP_MOTOR_CONTROL_BOARD_VREFA_PIN);
  216. }
  217. if(htim_pwm->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_VREFB_PWM)
  218. {
  219. /* Peripheral clock enable */
  220. __BSP_MOTOR_CONTROL_BOARD_TIMER_VREFB_PWM_CLCK_DISABLE();
  221. /* GPIO Deconfiguration */
  222. HAL_GPIO_DeInit(BSP_MOTOR_CONTROL_BOARD_VREFB_PORT, BSP_MOTOR_CONTROL_BOARD_VREFB_PIN);
  223. }
  224. }
  225. /**
  226. * @brief PWM Callback
  227. * @param[in] htim PWM handle pointer
  228. * @retval None
  229. */
  230. void HAL_TIM_PWM_PulseFinishedCallback(TIM_HandleTypeDef *htim)
  231. {
  232. if ((htim->Instance == BSP_MOTOR_CONTROL_BOARD_TIMER_TICK)&& (htim->Channel == BSP_MOTOR_CONTROL_BOARD_HAL_ACT_CHAN_TIMER_TICK))
  233. {
  234. if (BSP_MotorControl_GetDeviceState(0) != INACTIVE)
  235. {
  236. BSP_MotorControl_StepClockHandler(0);
  237. }
  238. }
  239. }
  240. /**
  241. * @brief External Line Callback
  242. * @param[in] GPIO_Pin pin number
  243. * @retval None
  244. */
  245. void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
  246. {
  247. if (GPIO_Pin == BSP_MOTOR_CONTROL_BOARD_EN_AND_FLAG_PIN)
  248. {
  249. BSP_MotorControl_FlagInterruptHandler();
  250. }
  251. if (GPIO_Pin == KEY_BUTTON_PIN)
  252. {
  253. ButtonHandler();
  254. }
  255. }
  256. /**
  257. * @brief ADC MSP De-Initialization
  258. * This function freeze the hardware resources used in this example
  259. * @param hadc: ADC handle pointer
  260. * @retval None
  261. */
  262. void HAL_ADC_MspDeInit(ADC_HandleTypeDef* hadc)
  263. {
  264. if(hadc->Instance==ADC1)
  265. {
  266. /* USER CODE BEGIN ADC1_MspDeInit 0 */
  267. /* USER CODE END ADC1_MspDeInit 0 */
  268. /* Peripheral clock disable */
  269. __HAL_RCC_ADC1_CLK_DISABLE();
  270. /**ADC GPIO Configuration
  271. PA0-WKUP1 ------> ADC_IN0
  272. */
  273. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_0);
  274. /* USER CODE BEGIN ADC1_MspDeInit 1 */
  275. /* USER CODE END ADC1_MspDeInit 1 */
  276. }
  277. }
  278. /**
  279. * @brief ADC MSP Initialization
  280. * This function configures the hardware resources used in this example
  281. * @param hadc: ADC handle pointer
  282. * @retval None
  283. */
  284. void HAL_ADC_MspInit(ADC_HandleTypeDef* hadc)
  285. {
  286. GPIO_InitTypeDef GPIO_InitStruct = {0};
  287. if(hadc->Instance==ADC1)
  288. {
  289. /* USER CODE BEGIN ADC1_MspInit 0 */
  290. /* USER CODE END ADC1_MspInit 0 */
  291. /* Peripheral clock enable */
  292. __HAL_RCC_ADC1_CLK_ENABLE();
  293. __HAL_RCC_GPIOA_CLK_ENABLE();
  294. /**ADC GPIO Configuration
  295. PA0-WKUP1 ------> ADC_IN0
  296. */
  297. GPIO_InitStruct.Pin = GPIO_PIN_0;
  298. GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
  299. GPIO_InitStruct.Pull = GPIO_NOPULL;
  300. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  301. /* USER CODE BEGIN ADC1_MspInit 1 */
  302. /* USER CODE END ADC1_MspInit 1 */
  303. }
  304. }
  305. /**
  306. * @brief SPI MSP Initialization
  307. * This function configures the hardware resources used in this example
  308. * @param hspi: SPI handle pointer
  309. * @retval None
  310. */
  311. void HAL_SPI_MspInit(SPI_HandleTypeDef* hspi)
  312. {
  313. GPIO_InitTypeDef GPIO_InitStruct = {0};
  314. if(hspi->Instance==SPI1)
  315. {
  316. /* USER CODE BEGIN SPI1_MspInit 0 */
  317. /* USER CODE END SPI1_MspInit 0 */
  318. /* Peripheral clock enable */
  319. __HAL_RCC_SPI1_CLK_ENABLE();
  320. __HAL_RCC_GPIOA_CLK_ENABLE();
  321. /**SPI1 GPIO Configuration
  322. PA5 ------> SPI1_SCK
  323. PA6 ------> SPI1_MISO
  324. PA7 ------> SPI1_MOSI
  325. */
  326. GPIO_InitStruct.Pin = GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
  327. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  328. GPIO_InitStruct.Pull = GPIO_NOPULL;
  329. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  330. GPIO_InitStruct.Alternate = GPIO_AF5_SPI1;
  331. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  332. /* USER CODE BEGIN SPI1_MspInit 1 */
  333. /* USER CODE END SPI1_MspInit 1 */
  334. }
  335. }
  336. /**
  337. * @brief SPI MSP De-Initialization
  338. * This function freeze the hardware resources used in this example
  339. * @param hspi: SPI handle pointer
  340. * @retval None
  341. */
  342. void HAL_SPI_MspDeInit(SPI_HandleTypeDef* hspi)
  343. {
  344. if(hspi->Instance==SPI1)
  345. {
  346. /* USER CODE BEGIN SPI1_MspDeInit 0 */
  347. /* USER CODE END SPI1_MspDeInit 0 */
  348. /* Peripheral clock disable */
  349. __HAL_RCC_SPI1_CLK_DISABLE();
  350. /**SPI1 GPIO Configuration
  351. PA5 ------> SPI1_SCK
  352. PA6 ------> SPI1_MISO
  353. PA7 ------> SPI1_MOSI
  354. */
  355. HAL_GPIO_DeInit(GPIOA, GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7);
  356. /* USER CODE BEGIN SPI1_MspDeInit 1 */
  357. /* USER CODE END SPI1_MspDeInit 1 */
  358. }
  359. }
  360. /* USER CODE BEGIN 1 */
  361. /* USER CODE END 1 */
  362. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/