OiO.lk Blog C# Is it ok to call HAL_TIM_PWM_Start on runtime with STM32F091?
C#

Is it ok to call HAL_TIM_PWM_Start on runtime with STM32F091?


I have an initialization block of code that initializes the timer 2 on an STM32F091. Inside this block, there’s a section that needs to run accordingly to a certain variable (type_b ) like:

static void MX_TIM2_Init(void)
{
  ... some code ...
  htim2.Init.Prescaler = 0;//400;
  htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim2.Init.Period = 60-1;//1000;
  ... some other code ...
if(type_b == 1) {
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_1);
  HAL_TIM_PWM_Start(&htim2, TIM_CHANNEL_2);

  __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_1, 0);
  __HAL_TIM_SET_COMPARE(&htim2, TIM_CHANNEL_2, 0);
}
... some other code ...
HAL_TIM_MspPostInit(&htim2);
}

Now, there are certain locations in the code base where the variable type_b could change. This means that if, at the first start of the microcontroller, type_b != 1 the block inside the if wouldn’t be executed.

On runtime, it could happen that type_b = 1. If that happens I need to execute the block of code inside the if(type_b == 1)

Is it safe to just add the HAL_TIM_PWM_Start and __HAL_TIM_SET_COMPARE on runtime or do I need to call again the MX_TIM2_Init function that re-initializes everything?

From what I’m seeing, things work calling the functions on runtime but, I don’t know if it’s best practice or if there are some drawback



You need to sign in to view this answers

Exit mobile version