AnsweredAssumed Answered

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

Question asked by th.martin on Apr 13, 2016
Latest reply on Apr 29, 2017 by katzenberger.michael
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:

01.void MX_TIM3_Init(void)
02.{
03.  TIM_ClockConfigTypeDef sClockSourceConfig;
04.  TIM_MasterConfigTypeDef sMasterConfig;
05.  TIM_OC_InitTypeDef sConfigOC;
06. 
07.  htim3.Instance = TIM3;
08.  htim3.Init.Prescaler = 32;
09.  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
10.  htim3.Init.Period = 1000;
11.  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
12.  HAL_TIM_Base_Init(&htim3);
13. 
14.  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
15.  HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);
16. 
17.  HAL_TIM_PWM_Init(&htim3);
18. 
19.  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
20.  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
21.  HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);
22. 
23.  sConfigOC.OCMode = TIM_OCMODE_PWM1;
24.  sConfigOC.Pulse = 500;
25.  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
26.  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
27.  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
28. 
29.  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2);
30. 
31.  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3);
32. 
33.  HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4);
34. 
35.  HAL_TIM_MspPostInit(&htim3);
36.}
This initiates the timer.

01.void HAL_TIM_MspPostInit(TIM_HandleTypeDef* htim)
02.{
03. 
04.  GPIO_InitTypeDef GPIO_InitStruct;
05.  if(htim->Instance==TIM3)
06.  {
07.  /* USER CODE BEGIN TIM3_MspPostInit 0 */
08. 
09.  /* USER CODE END TIM3_MspPostInit 0 */
10.   
11.    /**TIM3 GPIO Configuration   
12.    PB0     ------> TIM3_CH3
13.    PB1     ------> TIM3_CH4
14.    PB4     ------> TIM3_CH1
15.    PB5     ------> TIM3_CH2
16.    */
17.    GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_4|GPIO_PIN_5;
18.    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
19.    GPIO_InitStruct.Pull = GPIO_NOPULL;
20.    GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
21.    GPIO_InitStruct.Alternate = GPIO_AF1_TIM3;
22.    HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
23. 
24.  /* USER CODE BEGIN TIM3_MspPostInit 1 */
25. 
26.  /* USER CODE END TIM3_MspPostInit 1 */
27.  }
28.}
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:

1.HAL_TIM_Base_Start(&htim3);
2.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.

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

Outcomes