/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file tim.c * @brief This file provides code for the configuration * of the TIM instances. ****************************************************************************** * @attention * * Copyright (c) 2022 STMicroelectronics. * All rights reserved. * * This software is licensed under terms that can be found in the LICENSE file * in the root directory of this software component. * If no LICENSE file comes with this software, it is provided AS-IS. * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "tim.h" /* USER CODE BEGIN 0 */ #include "stdint.h" /* USER CODE END 0 */ TIM_HandleTypeDef htim2; TIM_HandleTypeDef htim6; TIM_HandleTypeDef htim7; TIM_HandleTypeDef htim15; TIM_HandleTypeDef htim16; TIM_HandleTypeDef htim17; /* TIM2 init function */ void MX_TIM2_Init(void) { /* USER CODE BEGIN TIM2_Init 0 */ /* USER CODE END TIM2_Init 0 */ TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_IC_InitTypeDef sConfigIC = {0}; /* USER CODE BEGIN TIM2_Init 1 */ /* USER CODE END TIM2_Init 1 */ htim2.Instance = TIM2; htim2.Init.Prescaler = 0; htim2.Init.CounterMode = TIM_COUNTERMODE_UP; htim2.Init.Period = 4294967295; htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_IC_Init(&htim2) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK) { Error_Handler(); } sConfigIC.ICPolarity = TIM_INPUTCHANNELPOLARITY_RISING; sConfigIC.ICSelection = TIM_ICSELECTION_DIRECTTI; sConfigIC.ICPrescaler = TIM_ICPSC_DIV1; sConfigIC.ICFilter = 0; if (HAL_TIM_IC_ConfigChannel(&htim2, &sConfigIC, TIM_CHANNEL_2) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM2_Init 2 */ /* USER CODE END TIM2_Init 2 */ } /* TIM6 init function */ void MX_TIM6_Init(void) { /* USER CODE BEGIN TIM6_Init 0 */ /* USER CODE END TIM6_Init 0 */ TIM_MasterConfigTypeDef sMasterConfig = {0}; /* USER CODE BEGIN TIM6_Init 1 */ /* USER CODE END TIM6_Init 1 */ htim6.Instance = TIM6; htim6.Init.Prescaler = 0; htim6.Init.CounterMode = TIM_COUNTERMODE_UP; htim6.Init.Period = (uint32_t)(80/ADC_SAMP_RATE_MHz)-1;//2.5Mzxz 1M 采样率 htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_Base_Init(&htim6) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM6_Init 2 */ /* USER CODE END TIM6_Init 2 */ } /* TIM7 init function */ void MX_TIM7_Init(void) { /* USER CODE BEGIN TIM7_Init 0 */ /* USER CODE END TIM7_Init 0 */ TIM_MasterConfigTypeDef sMasterConfig = {0}; /* USER CODE BEGIN TIM7_Init 1 */ /* USER CODE END TIM7_Init 1 */ htim7.Instance = TIM7; htim7.Init.Prescaler = 79; htim7.Init.CounterMode = TIM_COUNTERMODE_UP; htim7.Init.Period = 65535; htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim7) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM7_Init 2 */ /* USER CODE END TIM7_Init 2 */ } /* TIM15 init function */ void MX_TIM15_Init(void) { /* USER CODE BEGIN TIM15_Init 0 */ /* USER CODE END TIM15_Init 0 */ TIM_ClockConfigTypeDef sClockSourceConfig = {0}; TIM_MasterConfigTypeDef sMasterConfig = {0}; TIM_OC_InitTypeDef sConfigOC = {0}; TIM_BreakDeadTimeConfigTypeDef sBreakDeadTimeConfig = {0}; /* USER CODE BEGIN TIM15_Init 1 */ /* USER CODE END TIM15_Init 1 */ htim15.Instance = TIM15; htim15.Init.Prescaler = 0; htim15.Init.CounterMode = TIM_COUNTERMODE_UP; htim15.Init.Period = (uint32_t)(80/DRIVE_FREQ_MHz)-1; //200 ; htim15.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim15.Init.RepetitionCounter = DRIVE_NUM; htim15.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_Base_Init(&htim15) != HAL_OK) { Error_Handler(); } sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; if (HAL_TIM_ConfigClockSource(&htim15, &sClockSourceConfig) != HAL_OK) { Error_Handler(); } if (HAL_TIM_PWM_Init(&htim15) != HAL_OK) { Error_Handler(); } if (HAL_TIM_OnePulse_Init(&htim15, TIM_OPMODE_SINGLE) != HAL_OK) { Error_Handler(); } sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; if (HAL_TIMEx_MasterConfigSynchronization(&htim15, &sMasterConfig) != HAL_OK) { Error_Handler(); } sConfigOC.OCMode = TIM_OCMODE_PWM1; sConfigOC.Pulse = (uint32_t)(40/DRIVE_FREQ_MHz)-1; sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW; sConfigOC.OCNPolarity = TIM_OCNPOLARITY_HIGH; sConfigOC.OCFastMode = TIM_OCFAST_ENABLE; sConfigOC.OCIdleState = TIM_OCIDLESTATE_RESET; sConfigOC.OCNIdleState = TIM_OCNIDLESTATE_RESET; if (HAL_TIM_PWM_ConfigChannel(&htim15, &sConfigOC, TIM_CHANNEL_1) != HAL_OK) { Error_Handler(); } sBreakDeadTimeConfig.OffStateRunMode = TIM_OSSR_DISABLE; sBreakDeadTimeConfig.OffStateIDLEMode = TIM_OSSI_DISABLE; sBreakDeadTimeConfig.LockLevel = TIM_LOCKLEVEL_OFF; sBreakDeadTimeConfig.DeadTime = 0; sBreakDeadTimeConfig.BreakState = TIM_BREAK_DISABLE; sBreakDeadTimeConfig.BreakPolarity = TIM_BREAKPOLARITY_HIGH; sBreakDeadTimeConfig.AutomaticOutput = TIM_AUTOMATICOUTPUT_DISABLE; if (HAL_TIMEx_ConfigBreakDeadTime(&htim15, &sBreakDeadTimeConfig) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM15_Init 2 */ /* USER CODE END TIM15_Init 2 */ HAL_TIM_MspPostInit(&htim15); } // 计时时钟 用于算法性能分析 /* TIM16 init function */ void MX_TIM16_Init(void) { /* USER CODE BEGIN TIM16_Init 0 */ /* USER CODE END TIM16_Init 0 */ /* USER CODE BEGIN TIM16_Init 1 */ /* USER CODE END TIM16_Init 1 */ htim16.Instance = TIM16; htim16.Init.Prescaler = 7999; htim16.Init.CounterMode = TIM_COUNTERMODE_UP; htim16.Init.Period = 65535; htim16.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim16.Init.RepetitionCounter = 0; htim16.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE; if (HAL_TIM_Base_Init(&htim16) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM16_Init 2 */ /* USER CODE END TIM16_Init 2 */ } //用于实时更新时间 /* TIM17 init function */ void MX_TIM17_Init(void) { /* USER CODE BEGIN TIM17_Init 0 */ /* USER CODE END TIM17_Init 0 */ /* USER CODE BEGIN TIM17_Init 1 */ /* USER CODE END TIM17_Init 1 */ htim17.Instance = TIM17; htim17.Init.Prescaler = 8000-1; htim17.Init.CounterMode = TIM_COUNTERMODE_UP; htim17.Init.Period = 10000; htim17.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; htim17.Init.RepetitionCounter = 0; htim17.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; if (HAL_TIM_Base_Init(&htim17) != HAL_OK) { Error_Handler(); } /* USER CODE BEGIN TIM17_Init 2 */ HAL_TIM_Base_Start_IT(&htim17); /* USER CODE END TIM17_Init 2 */ } void HAL_TIM_IC_MspInit(TIM_HandleTypeDef* tim_icHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(tim_icHandle->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspInit 0 */ /* USER CODE END TIM2_MspInit 0 */ /* TIM2 clock enable */ __HAL_RCC_TIM2_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /**TIM2 GPIO Configuration PA1 ------> TIM2_CH2 */ GPIO_InitStruct.Pin = GPIO_PIN_1; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ } } void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle) { if(tim_baseHandle->Instance==TIM6) { /* USER CODE BEGIN TIM6_MspInit 0 */ /* USER CODE END TIM6_MspInit 0 */ /* TIM6 clock enable */ __HAL_RCC_TIM6_CLK_ENABLE(); /* USER CODE BEGIN TIM6_MspInit 1 */ /* USER CODE END TIM6_MspInit 1 */ } else if(tim_baseHandle->Instance==TIM7) { /* USER CODE BEGIN TIM7_MspInit 0 */ /* USER CODE END TIM7_MspInit 0 */ /* TIM7 clock enable */ __HAL_RCC_TIM7_CLK_ENABLE(); /* USER CODE BEGIN TIM7_MspInit 1 */ /* USER CODE END TIM7_MspInit 1 */ } else if(tim_baseHandle->Instance==TIM15) { /* USER CODE BEGIN TIM15_MspInit 0 */ /* USER CODE END TIM15_MspInit 0 */ /* TIM15 clock enable */ __HAL_RCC_TIM15_CLK_ENABLE(); /* USER CODE BEGIN TIM15_MspInit 1 */ /* USER CODE END TIM15_MspInit 1 */ } else if(tim_baseHandle->Instance==TIM16) { /* USER CODE BEGIN TIM16_MspInit 0 */ /* USER CODE END TIM16_MspInit 0 */ /* TIM16 clock enable */ __HAL_RCC_TIM16_CLK_ENABLE(); /* TIM16 interrupt Init */ HAL_NVIC_SetPriority(TIM1_UP_TIM16_IRQn, 15, 0); HAL_NVIC_EnableIRQ(TIM1_UP_TIM16_IRQn); /* USER CODE BEGIN TIM16_MspInit 1 */ /* USER CODE END TIM16_MspInit 1 */ } else if(tim_baseHandle->Instance==TIM17) { /* USER CODE BEGIN TIM17_MspInit 0 */ /* USER CODE END TIM17_MspInit 0 */ /* TIM17 clock enable */ __HAL_RCC_TIM17_CLK_ENABLE(); /* TIM17 interrupt Init */ HAL_NVIC_SetPriority(TIM1_TRG_COM_TIM17_IRQn, 14, 0); HAL_NVIC_EnableIRQ(TIM1_TRG_COM_TIM17_IRQn); /* USER CODE BEGIN TIM17_MspInit 1 */ /* USER CODE END TIM17_MspInit 1 */ } } void HAL_TIM_MspPostInit(TIM_HandleTypeDef* timHandle) { GPIO_InitTypeDef GPIO_InitStruct = {0}; if(timHandle->Instance==TIM15) { /* USER CODE BEGIN TIM15_MspPostInit 0 */ /* USER CODE END TIM15_MspPostInit 0 */ __HAL_RCC_GPIOA_CLK_ENABLE(); /**TIM15 GPIO Configuration PA2 ------> TIM15_CH1 */ GPIO_InitStruct.Pin = GPIO_PIN_2; GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; GPIO_InitStruct.Alternate = GPIO_AF14_TIM15; HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); /* USER CODE BEGIN TIM15_MspPostInit 1 */ /* USER CODE END TIM15_MspPostInit 1 */ } } void HAL_TIM_IC_MspDeInit(TIM_HandleTypeDef* tim_icHandle) { if(tim_icHandle->Instance==TIM2) { /* USER CODE BEGIN TIM2_MspDeInit 0 */ /* USER CODE END TIM2_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM2_CLK_DISABLE(); /**TIM2 GPIO Configuration PA1 ------> TIM2_CH2 */ HAL_GPIO_DeInit(GPIOA, GPIO_PIN_1); /* USER CODE BEGIN TIM2_MspDeInit 1 */ /* USER CODE END TIM2_MspDeInit 1 */ } } void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle) { if(tim_baseHandle->Instance==TIM6) { /* USER CODE BEGIN TIM6_MspDeInit 0 */ /* USER CODE END TIM6_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM6_CLK_DISABLE(); /* USER CODE BEGIN TIM6_MspDeInit 1 */ /* USER CODE END TIM6_MspDeInit 1 */ } else if(tim_baseHandle->Instance==TIM7) { /* USER CODE BEGIN TIM7_MspDeInit 0 */ /* USER CODE END TIM7_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM7_CLK_DISABLE(); /* USER CODE BEGIN TIM7_MspDeInit 1 */ /* USER CODE END TIM7_MspDeInit 1 */ } else if(tim_baseHandle->Instance==TIM15) { /* USER CODE BEGIN TIM15_MspDeInit 0 */ /* USER CODE END TIM15_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM15_CLK_DISABLE(); /* USER CODE BEGIN TIM15_MspDeInit 1 */ /* USER CODE END TIM15_MspDeInit 1 */ } else if(tim_baseHandle->Instance==TIM16) { /* USER CODE BEGIN TIM16_MspDeInit 0 */ /* USER CODE END TIM16_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM16_CLK_DISABLE(); /* TIM16 interrupt Deinit */ HAL_NVIC_DisableIRQ(TIM1_UP_TIM16_IRQn); /* USER CODE BEGIN TIM16_MspDeInit 1 */ /* USER CODE END TIM16_MspDeInit 1 */ } else if(tim_baseHandle->Instance==TIM17) { /* USER CODE BEGIN TIM17_MspDeInit 0 */ /* USER CODE END TIM17_MspDeInit 0 */ /* Peripheral clock disable */ __HAL_RCC_TIM17_CLK_DISABLE(); /* TIM17 interrupt Deinit */ HAL_NVIC_DisableIRQ(TIM1_TRG_COM_TIM17_IRQn); /* USER CODE BEGIN TIM17_MspDeInit 1 */ /* USER CODE END TIM17_MspDeInit 1 */ } } /* USER CODE BEGIN 1 */ /* USER CODE END 1 */