2025-06-20 1:36 AM
Hello everyone,
Does anyone have experience with changing the function of a pin in program?(Not CubeMX tool)
I'm currently using the STM32H5 MCU to develop a motor drive inverter.
For rotor angle feedback, we’re considering two options: Encoder (ABZ) and Hall sensors.
As shown below, when configuring TIM2 in Encoder + Index mode, it occupies PA0, PA1, and PA4. However, if TIM2 is configured in Input Capture mode (for Hall sensors), it uses PA0, PA1, and PA2 instead.
We want to let the user select the desired feedback method (either Hall or Encoder), and then have the firmware initialize the corresponding pin configuration at runtime.
So, my question is: how should I config the CubeMX configuration from the beginning, and how can I implement the function initialization in code to change different function?
Has anyone had experience with this type of setup?"
Solved! Go to Solution.
2025-06-24 5:04 AM
AFAIK, CubeMX only supports 1 configuration within a project.
So you could proceed as follows:
Note that there is an option to have Cube not generate the initialisation function calls - you would probably want to do that, so that you can make the calls where & when you require.
2025-06-20 12:34 PM
Hi @yang2
The Pins are configured in the stm32xxxxx_hal_msp.c file in your project's source folder,
and it looks like this.
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
if(htim->Instance==TIM2)
{
/* USER CODE BEGIN TIM2_MspPostInit 0 */
/* USER CODE END TIM2_MspPostInit 0 */
__HAL_RCC_GPIOB_CLK_ENABLE();
/**TIM2 GPIO Configuration
PB10 ------> TIM2_CH3
*/
GPIO_InitStruct.Pin = GPIO_PIN_10;
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(GPIOB, &GPIO_InitStruct);
/* USER CODE BEGIN TIM2_MspPostInit 1 */
/* USER CODE END TIM2_MspPostInit 1 */
}
else if(htim->Instance==TIM3)
{
/* USER CODE BEGIN TIM3_MspPostInit 0 */
/* USER CODE END TIM3_MspPostInit 0 */
__HAL_RCC_GPIOC_CLK_ENABLE();
/**TIM3 GPIO Configuration
PC6 ------> TIM3_CH1
*/
GPIO_InitStruct.Pin = GPIO_PIN_6;
GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/* USER CODE BEGIN TIM3_MspPostInit 1 */
/* USER CODE END TIM3_MspPostInit 1 */
}
}
The channels of the timer can be configured in runtime ,
in you MXTIM_INIT Function
for instance this will configure your tim2 channel 3 in OC mode
you can add you logic here to configure your channel as per the input.
sConfigOC.OCMode = TIM_OCMODE_PWM1;
sConfigOC.Pulse = 0;
sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
if (HAL_TIM_PWM_ConfigChannel(&htim2, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
{
Error_Handler();
}
Thumbs up! If you find this helpful , this helps in knowing what works.
If This has answered your query mark it as Accepted Solution .
2025-06-24 4:36 AM
Hello @Obaid_tanveer,
This post has been escalated to the ST Online Support Team for additional assistance. We'll contact you directly.
Best regards,
Maxime
2025-06-24 5:04 AM
AFAIK, CubeMX only supports 1 configuration within a project.
So you could proceed as follows:
Note that there is an option to have Cube not generate the initialisation function calls - you would probably want to do that, so that you can make the calls where & when you require.