tim.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file tim.c
  5. * @brief This file provides code for the configuration
  6. * of the TIM instances.
  7. ******************************************************************************
  8. * @attention
  9. *
  10. * Copyright (c) 2024 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 "tim.h"
  22. /* USER CODE BEGIN 0 */
  23. /* USER CODE END 0 */
  24. TIM_HandleTypeDef htim1;
  25. TIM_HandleTypeDef htim3;
  26. TIM_HandleTypeDef htim4;
  27. /* TIM1 init function */
  28. void MX_TIM1_Init(void)
  29. {
  30. /* USER CODE BEGIN TIM1_Init 0 */
  31. /* USER CODE END TIM1_Init 0 */
  32. TIM_MasterConfigTypeDef sMasterConfig = {0};
  33. TIM_OC_InitTypeDef sConfigOC = {0};
  34. TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0};
  35. /* USER CODE BEGIN TIM1_Init 1 */
  36. /* USER CODE END TIM1_Init 1 */
  37. htim1.Instance = TIM1;
  38. htim1.Init.Prescaler = 0;
  39. htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
  40. htim1.Init.Period = 65535;
  41. htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  42. htim1.Init.RepetitionCounter = 0;
  43. htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  44. if (HAL_TIM_PWM_Init(&htim1) != HAL_OK)
  45. {
  46. Error_Handler();
  47. }
  48. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  49. sMasterConfig.MasterOutputTrigger2 = TIM_TRGO2_RESET;
  50. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  51. if (HAL_TIMEx_MasterConfigSynchronization(&htim1, &sMasterConfig) != HAL_OK)
  52. {
  53. Error_Handler();
  54. }
  55. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  56. sConfigOC.Pulse = 0;
  57. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  58. sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
  59. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  60. sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET;
  61. sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  62. if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  63. {
  64. Error_Handler();
  65. }
  66. if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  67. {
  68. Error_Handler();
  69. }
  70. if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  71. {
  72. Error_Handler();
  73. }
  74. if (HAL_TIM_PWM_ConfigChannel(&htim1, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  75. {
  76. Error_Handler();
  77. }
  78. sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE;
  79. sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE;
  80. sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF;
  81. sBreakDeadTimeConfig.DeadTime = 0;
  82. sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE;
  83. sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH;
  84. sBreakDeadTimeConfig.BreakFilter = 0;
  85. sBreakDeadTimeConfig.Break2State = TIM_BREAK2_DISABLE;
  86. sBreakDeadTimeConfig.Break2Polarity = TIM_BREAK2POLARITY_HIGH;
  87. sBreakDeadTimeConfig.Break2Filter = 0;
  88. sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE;
  89. if (HAL_TIMEx_ConfigBreakDeadTime(&htim1, &sBreakDeadTimeConfig) != HAL_OK)
  90. {
  91. Error_Handler();
  92. }
  93. /* USER CODE BEGIN TIM1_Init 2 */
  94. /* USER CODE END TIM1_Init 2 */
  95. HAL_TIM_MspPostInit(&htim1);
  96. }
  97. /* TIM3 init function */
  98. void MX_TIM3_Init(void)
  99. {
  100. /* USER CODE BEGIN TIM3_Init 0 */
  101. /* USER CODE END TIM3_Init 0 */
  102. TIM_MasterConfigTypeDef sMasterConfig = {0};
  103. TIM_OC_InitTypeDef sConfigOC = {0};
  104. /* USER CODE BEGIN TIM3_Init 1 */
  105. /* USER CODE END TIM3_Init 1 */
  106. htim3.Instance = TIM3;
  107. htim3.Init.Prescaler = 0;
  108. htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  109. htim3.Init.Period = 65535;
  110. htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  111. htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  112. if (HAL_TIM_PWM_Init(&htim3) != HAL_OK)
  113. {
  114. Error_Handler();
  115. }
  116. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  117. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  118. if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  119. {
  120. Error_Handler();
  121. }
  122. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  123. sConfigOC.Pulse = 0;
  124. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  125. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  126. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  127. {
  128. Error_Handler();
  129. }
  130. if (HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  131. {
  132. Error_Handler();
  133. }
  134. /* USER CODE BEGIN TIM3_Init 2 */
  135. /* USER CODE END TIM3_Init 2 */
  136. HAL_TIM_MspPostInit(&htim3);
  137. }
  138. /* TIM4 init function */
  139. void MX_TIM4_Init(void)
  140. {
  141. /* USER CODE BEGIN TIM4_Init 0 */
  142. /* USER CODE END TIM4_Init 0 */
  143. TIM_MasterConfigTypeDef sMasterConfig = {0};
  144. TIM_OC_InitTypeDef sConfigOC = {0};
  145. /* USER CODE BEGIN TIM4_Init 1 */
  146. /* USER CODE END TIM4_Init 1 */
  147. htim4.Instance = TIM4;
  148. htim4.Init.Prescaler = 0;
  149. htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  150. htim4.Init.Period = 65535;
  151. htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  152. htim4.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  153. if (HAL_TIM_PWM_Init(&htim4) != HAL_OK)
  154. {
  155. Error_Handler();
  156. }
  157. sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  158. sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  159. if (HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig) != HAL_OK)
  160. {
  161. Error_Handler();
  162. }
  163. sConfigOC.OCMode = TIM_OCMODE_PWM1;
  164. sConfigOC.Pulse = 0;
  165. sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  166. sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  167. if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  168. {
  169. Error_Handler();
  170. }
  171. if (HAL_TIM_PWM_ConfigChannel(&htim4, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  172. {
  173. Error_Handler();
  174. }
  175. /* USER CODE BEGIN TIM4_Init 2 */
  176. /* USER CODE END TIM4_Init 2 */
  177. HAL_TIM_MspPostInit(&htim4);
  178. }
  179. void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef* tim_pwmHandle)
  180. {
  181. if(tim_pwmHandle->Instance==TIM1)
  182. {
  183. /* USER CODE BEGIN TIM1_MspInit 0 */
  184. /* USER CODE END TIM1_MspInit 0 */
  185. /* TIM1 clock enable */
  186. __HAL_RCC_TIM1_CLK_ENABLE();
  187. /* USER CODE BEGIN TIM1_MspInit 1 */
  188. /* USER CODE END TIM1_MspInit 1 */
  189. }
  190. else if(tim_pwmHandle->Instance==TIM3)
  191. {
  192. /* USER CODE BEGIN TIM3_MspInit 0 */
  193. /* USER CODE END TIM3_MspInit 0 */
  194. /* TIM3 clock enable */
  195. __HAL_RCC_TIM3_CLK_ENABLE();
  196. /* USER CODE BEGIN TIM3_MspInit 1 */
  197. /* USER CODE END TIM3_MspInit 1 */
  198. }
  199. else if(tim_pwmHandle->Instance==TIM4)
  200. {
  201. /* USER CODE BEGIN TIM4_MspInit 0 */
  202. /* USER CODE END TIM4_MspInit 0 */
  203. /* TIM4 clock enable */
  204. __HAL_RCC_TIM4_CLK_ENABLE();
  205. /* USER CODE BEGIN TIM4_MspInit 1 */
  206. /* USER CODE END TIM4_MspInit 1 */
  207. }
  208. }
  209. void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle)
  210. {
  211. GPIO_InitTypeDef GPIO_InitStruct = {0};
  212. if(timHandle->Instance==TIM1)
  213. {
  214. /* USER CODE BEGIN TIM1_MspPostInit 0 */
  215. /* USER CODE END TIM1_MspPostInit 0 */
  216. __HAL_RCC_GPIOE_CLK_ENABLE();
  217. /**TIM1 GPIO Configuration
  218. PE9 ------> TIM1_CH1
  219. PE11 ------> TIM1_CH2
  220. PE13 ------> TIM1_CH3
  221. PE14 ------> TIM1_CH4
  222. */
  223. GPIO_InitStruct.Pin = GPIO_PIN_9|GPIO_PIN_11|GPIO_PIN_13|GPIO_PIN_14;
  224. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  225. GPIO_InitStruct.Pull = GPIO_NOPULL;
  226. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  227. GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
  228. HAL_GPIO_Init(GPIOE, &GPIO_InitStruct);
  229. /* USER CODE BEGIN TIM1_MspPostInit 1 */
  230. /* USER CODE END TIM1_MspPostInit 1 */
  231. }
  232. else if(timHandle->Instance==TIM3)
  233. {
  234. /* USER CODE BEGIN TIM3_MspPostInit 0 */
  235. /* USER CODE END TIM3_MspPostInit 0 */
  236. __HAL_RCC_GPIOC_CLK_ENABLE();
  237. /**TIM3 GPIO Configuration
  238. PC6 ------> TIM3_CH1
  239. PC7 ------> TIM3_CH2
  240. */
  241. GPIO_InitStruct.Pin = GPIO_PIN_6|GPIO_PIN_7;
  242. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  243. GPIO_InitStruct.Pull = GPIO_NOPULL;
  244. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  245. GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
  246. HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  247. /* USER CODE BEGIN TIM3_MspPostInit 1 */
  248. /* USER CODE END TIM3_MspPostInit 1 */
  249. }
  250. else if(timHandle->Instance==TIM4)
  251. {
  252. /* USER CODE BEGIN TIM4_MspPostInit 0 */
  253. /* USER CODE END TIM4_MspPostInit 0 */
  254. __HAL_RCC_GPIOD_CLK_ENABLE();
  255. /**TIM4 GPIO Configuration
  256. PD12 ------> TIM4_CH1
  257. PD13 ------> TIM4_CH2
  258. */
  259. GPIO_InitStruct.Pin = GPIO_PIN_12|GPIO_PIN_13;
  260. GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  261. GPIO_InitStruct.Pull = GPIO_NOPULL;
  262. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  263. GPIO_InitStruct.Alternate = GPIO_AF2_TIM4;
  264. HAL_GPIO_Init(GPIOD, &GPIO_InitStruct);
  265. /* USER CODE BEGIN TIM4_MspPostInit 1 */
  266. /* USER CODE END TIM4_MspPostInit 1 */
  267. }
  268. }
  269. void HAL_TIM_PWM_MspDeInit(TIM_HandleTypeDef* tim_pwmHandle)
  270. {
  271. if(tim_pwmHandle->Instance==TIM1)
  272. {
  273. /* USER CODE BEGIN TIM1_MspDeInit 0 */
  274. /* USER CODE END TIM1_MspDeInit 0 */
  275. /* Peripheral clock disable */
  276. __HAL_RCC_TIM1_CLK_DISABLE();
  277. /* USER CODE BEGIN TIM1_MspDeInit 1 */
  278. /* USER CODE END TIM1_MspDeInit 1 */
  279. }
  280. else if(tim_pwmHandle->Instance==TIM3)
  281. {
  282. /* USER CODE BEGIN TIM3_MspDeInit 0 */
  283. /* USER CODE END TIM3_MspDeInit 0 */
  284. /* Peripheral clock disable */
  285. __HAL_RCC_TIM3_CLK_DISABLE();
  286. /* USER CODE BEGIN TIM3_MspDeInit 1 */
  287. /* USER CODE END TIM3_MspDeInit 1 */
  288. }
  289. else if(tim_pwmHandle->Instance==TIM4)
  290. {
  291. /* USER CODE BEGIN TIM4_MspDeInit 0 */
  292. /* USER CODE END TIM4_MspDeInit 0 */
  293. /* Peripheral clock disable */
  294. __HAL_RCC_TIM4_CLK_DISABLE();
  295. /* USER CODE BEGIN TIM4_MspDeInit 1 */
  296. /* USER CODE END TIM4_MspDeInit 1 */
  297. }
  298. }
  299. /* USER CODE BEGIN 1 */
  300. /* USER CODE END 1 */