cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F1 and TIM3 PWM - LL drivers

Posted on March 19, 2018 at 20:11

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.

0690X0000060A51QAE.png

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

1 ACCEPTED SOLUTION

Accepted Solutions
Posted on March 19, 2018 at 21:51

The library appears to do exactly what you commanded it to do:

  TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;

thus, it did not enable that channel.

Or, use LL_TIM_CC_EnableChannel().

JW

(Yes, I wouldn't touch Cube with a stick, LL or HAL. This was 5 minutes of browsing F4Cube sources; but I believe 'F1 will be the same).

View solution in original post

4 REPLIES 4
Szymon PANECKI
Senior III
Posted on March 19, 2018 at 21:12

Hello Bogdan,

Please find attached a main.c file from example application based on LL, which generates PWM signal. Please note, that it is not exactly what you ask for, because project was developed for STM32L0 and it uses TIM2, but I guess it can be reused for your purpose.Please review the file and check the functions, which are called there to control timer. I hope it will be helpful to you.

Regards

Szymon

________________

Attachments :

main.c.zip : https://st--c.eu10.content.force.com/sfc/dist/version/download/?oid=00Db0000000YtG6&ids=0680X000006HxrA&d=%2Fa%2F0X0000000b1f%2F1wXc21Jyscc.XNdSAI_8IFiQsHU3FwFZ_njKJexyOSI&asPdf=false
Posted on March 19, 2018 at 21:40

Hi Szymon,

Thank you for the example. Yes, the example it's slightly different (IT is used, etc). It's good to see this approach as well.

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

Normally I use HAL and the TIM3/PWM works fine without IT with F1.

The problem here, with replacing HAL with LL, is that the LL does not initialize CCER so I needed to use manual fix.

OR another function is required to initialize CCER - the only one I found is 'LL_TIM_OC_ConfigOutput' but it does not work as expected by me (at least I was not lucky to make it working).

Posted on March 19, 2018 at 21:51

The library appears to do exactly what you commanded it to do:

  TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;

thus, it did not enable that channel.

Or, use LL_TIM_CC_EnableChannel().

JW

(Yes, I wouldn't touch Cube with a stick, LL or HAL. This was 5 minutes of browsing F4Cube sources; but I believe 'F1 will be the same).

Posted on March 19, 2018 at 22:12

Yes, I was looking for an option to enable the channel through CubeMX GUI but failed.

The 'TIM_OC_InitStruct.OCState = LL_TIM_OCSTATE_DISABLE;' was automatically generated by the CubeMX.

Needless to say that 'LL_TIM_CC_EnableChannel(TIM3, LL_TIM_CHANNEL_CH3);' solves the problem perfectly.

Thank you for your time spent on such stupid issues.

I am not so skilled in finding such issues - for me it's easier to use the datasheet.