cancel
Showing results for 
Search instead for 
Did you mean: 

Change the peripheral function in program

yang2
Associate III

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.

yang2_0-1750407561620.pngyang2_1-1750407601437.png

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?"

 

 

1 ACCEPTED SOLUTION

Accepted Solutions
Andrew Neil
Super User

AFAIK, CubeMX only supports 1 configuration within a project.

So you could proceed as follows:

  1. Create the project with one feedback method (say, Hall).
  2. Created a second project, with the other feedback method (encoder).
  3. Now you can take the initialisation code for the two options from those projects, and merge it into one project with the two options.

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.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

View solution in original post

3 REPLIES 3
Obaid_tanveer
Associate III

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 .

Maxime_MARCHETTO
Community manager
Community manager

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


In order to give better visibility on the answered topics, please click on 'Accept as Solution' on the reply which solved your issue or answered your question.
Andrew Neil
Super User

AFAIK, CubeMX only supports 1 configuration within a project.

So you could proceed as follows:

  1. Create the project with one feedback method (say, Hall).
  2. Created a second project, with the other feedback method (encoder).
  3. Now you can take the initialisation code for the two options from those projects, and merge it into one project with the two options.

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.

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.