Electrical – Not being able to enable PWM using the HAL TIM library on stm32f0

hal-librarypwmstm32stm32cubemxstm32f0

The goal is to use the PWM feature of stm32 HAL TIM libraries to light up 4 leds on pins 0, 1, 4 and 5

I have generated the following code using CubeMX:

void MX_TIM3_Init(void)
{
  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;
  TIM_OC_InitTypeDef sConfigOC;

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 32;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 1000;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  HAL_TIM_Base_Init(&htim3);

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);

  HAL_TIM_PWM_Init(&htim3);

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);

  sConfigOC.OCMode = TIM_OCMODE_PWM1;
  sConfigOC.Pulse = 500;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);

  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2);

  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3);

  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4);

  HAL_TIM_MspPostInit(&htim3);    
}

This initiates the timer.

void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
{    
  GPIO_InitTypeDef GPIO_InitStruct;
  if(htim->Instance==TIM3)
  {
  /* USER CODE BEGIN TIM3_MspPostInit 0 */

  /* USER CODE END TIM3_MspPostInit 0 */

    /**TIM3 GPIO Configuration    
    PB0     ------> TIM3_CH3
    PB1     ------> TIM3_CH4
    PB4     ------> TIM3_CH1
    PB5     ------> TIM3_CH2 
    */
    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_NOPULL;
    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
    GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

  /* USER CODE BEGIN TIM3_MspPostInit 1 */

  /* USER CODE END TIM3_MspPostInit 1 */
  }
}

This initiates the GPIO pins for PWM.

Also the clock is enabled in the HAL_TIM_Base_MspInit callback function using __TIM3_CLK_ENABLE().

In the main.c file I have added the following code after all initializations, to start the base timer and PWM:

HAL_TIM_Base_Start(&htim3);
HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_ALL);

Now after running this code on the stm32f070cb the LEDs do not light up at least slightly.

I have tried changing the compare values CCRx. Also the GPIO pins work since I can use HAL_GPIO_WritePin to turn on the LEDs.
Besides that I found out that the counter is actually running between 0 and 1000 using the __HAL_TIM_GetCounter macro. It just seems that the LEDs are not reacting to the compare values.

Did someone have a similar problem or have some hints to go from here?

Best Answer

Thanks to a kind suggestion I was able to get the PWM working using the TIM_PWMOutput example under the STM32F0 CubeMX firmware:

STM32Cube_FW_F0_V1.5.0\Projects\STM32F0308Discovery\Examples\TIM\TIM_PWMOutput

I have now solved my problem using the code from the example. Though I am not sure where it is different from the code generated by CubeMX.