AnsweredAssumed Answered

STM32F1 and TIM3 PWM - LL drivers

Question asked by Bogdan Golab on Mar 19, 2018
Latest reply on Mar 19, 2018 by Bogdan Golab

Hi,

 

Today's F1 v1.6.1 HAL driver pack fixed all issues I had with GPIO so decided to move forward with TIM3 and LL.

 

The goal: use TIM3/PWM to blink the BLUE LED (PC8) - STM32VLDiscovery.

 

 

 

CubeMX generated code:

/* TIM3 init function */
static void MX_TIM3_Init(void)
{

 

  LL_TIM_InitTypeDef TIM_InitStruct;
  LL_TIM_OC_InitTypeDef TIM_OC_InitStruct;

 

  LL_GPIO_InitTypeDef GPIO_InitStruct;

 

  /* Peripheral clock enable */
  LL_APB1_GRP1_EnableClock(LL_APB1_GRP1_PERIPH_TIM3);

 

  TIM_InitStruct.Prescaler = 24000;
  TIM_InitStruct.CounterMode = LL_TIM_COUNTERMODE_UP;
  TIM_InitStruct.Autoreload = 1000;
  TIM_InitStruct.ClockDivision = LL_TIM_CLOCKDIVISION_DIV1;
  LL_TIM_Init(TIM3, &TIM_InitStruct);

 

  LL_TIM_DisableARRPreload(TIM3);

 

  LL_TIM_SetClockSource(TIM3, LL_TIM_CLOCKSOURCE_INTERNAL);

 

  LL_TIM_OC_EnablePreload(TIM3, LL_TIM_CHANNEL_CH3);

 

  TIM_OC_InitStruct.OCMode = LL_TIM_OCMODE_PWM1;
  TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.OCNState = LL_TIM_OCSTATE_DISABLE;
  TIM_OC_InitStruct.CompareValue = 500;
  TIM_OC_InitStruct.OCPolarity = LL_TIM_OCPOLARITY_HIGH;
  LL_TIM_OC_Init(TIM3, LL_TIM_CHANNEL_CH3, &TIM_OC_InitStruct);

 

  LL_TIM_OC_DisableFast(TIM3, LL_TIM_CHANNEL_CH3);

 

  LL_TIM_SetTriggerOutput(TIM3, LL_TIM_TRGO_RESET);

 

  LL_TIM_DisableMasterSlaveMode(TIM3);

 

  /**TIM3 GPIO Configuration  
  PC8   ------> TIM3_CH3
  */
  GPIO_InitStruct.Pin = LED_BLUE_Pin;
  GPIO_InitStruct.Mode = LL_GPIO_MODE_ALTERNATE;
  GPIO_InitStruct.Speed = LL_GPIO_SPEED_FREQ_LOW;
  GPIO_InitStruct.OutputType = LL_GPIO_OUTPUT_PUSHPULL;
  LL_GPIO_Init(LED_BLUE_GPIO_Port, &GPIO_InitStruct);

 

  LL_GPIO_AF_EnableRemap_TIM3();

 

}

 

And the main() part:

 

 /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM3_Init();
  /* USER CODE BEGIN 2 */
 [...]

 

=====================================

I added: "LL_TIM_EnableCounter(TIM3);" but the LED was still off. Started the debugger and noticed that the CCER register is cleared so I enabled CC3E manually and the LED started blinking.

 

Tried the "LL_TIM_OC_ConfigOutput(TIM3, LL_TIM_CHANNEL_CH3, LL_TIM_OCPOLARITY_HIGH|xx);" but failed.

 

Finally I fixed the issue using CCER register and it of course works.
    TIM3->CCER=0x00000100;                    // Capture Compare Output Enable  - channel3

--------------------------------------

 

If you could share your experience and help me setup the TIM3 for PWM job using LL drivers.

-----------------------------

I do not have LL drivers 'how to guide' so it is easier for me to play at the register level than investigate why LL drivers behavies different way than I expect.

 

I'd be grateful if  someone from ST could share the LL manual - how the drivers are supposed to be used.

Thanks in advance,

Bogdan

Outcomes