cancel
Showing results for 
Search instead for 
Did you mean: 

STM32L072: PWM Output with HAL

davide
Associate II
Posted on May 20, 2017 at 14:34

Hello everybody!

I cannot find whats missing in my timer initialisation: TIM3_CH2 interrupt is generated correctly (as the callback function is executed and a led on a generic GPIO of the STM32 Demo Board is driven by consequence). Unfortunately, nothing happens on PB5, initialised as AF4_TIM3, that is TIM3_CH2 output of my PWM waveform.

The attached code is working fine, except from the PWM Output.

Can some one helps me find out what's wrong? After 5+ years of using SPL I have started now using HAL with this new project, very good libraries, but I'm struggling a little to adapt

void

tim3_Init(

void

) {

GPIO_InitTypeDef

GPIO_InitStruct;

__I2C1_CLK_DISABLE();

__GPIOB_CLK_ENABLE();

GPIO_InitStruct.

Pin

= GPIO_PIN_5;

GPIO_InitStruct.

Mode

= GPIO_MODE_AF_PP;

GPIO_InitStruct.

Pull

= GPIO_NOPULL;

GPIO_InitStruct.

Speed

= GPIO_SPEED_FREQ_VERY_HIGH;

GPIO_InitStruct.

Alternate

= GPIO_AF4_TIM3;

HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);

/* 0) Enable Peripheral Clock */

__TIM3_CLK_ENABLE();

/* 1) Initialize TIMx peripheral ---------------------------------------------- */

TimHandle3.

Instance

= TIM3;

//Timer to Initialize

TimHandle3.

Init

.

Prescaler

= (SystemCoreClock / 1000000) - 1;

//

Prescaler

for 1MHz clock [16bit]

TimHandle3.

Init

.

Period

      = 1000 - 1;    

//Count for 1000 1Mhz Ticks: 1ms

TimHandle3.

Init

.

ClockDivision

= TIM_CLOCKDIVISION_DIV1;

TimHandle3.

Init

.

CounterMode

= TIM_COUNTERMODE_UP;

if

(HAL_TIM_PWM_Init(&TimHandle3) !=

HAL_OK

)

{

  while

(1);

}

/* 2) Activate NVIC for TIM3 */

HAL_NVIC_SetPriority(

TIM3_IRQn

, TIM3_IRQ_PRIORITY, 0U);

HAL_NVIC_ClearPendingIRQ(

TIM3_IRQn

);

HAL_NVIC_EnableIRQ(

TIM3_IRQn

);

/* Timer Output Compare Configuration Structure declaration */

TIM_OC_InitTypeDef

sConfig;

/* 3) Configuration for TIM3_CH2 ---------------------------------------------- */

sConfig.

OCMode

= TIM_OCMODE_PWM1;

sConfig.

OCPolarity

= TIM_OCPOLARITY_HIGH;

sConfig.

OCFastMode

= TIM_OCFAST_DISABLE;

sConfig.

Pulse

= 500;

//HalfPeriod Duty

if

(HAL_TIM_PWM_ConfigChannel(&TimHandle3, &sConfig, TIM_CHANNEL_2) !=

HAL_OK

)

{

  while

(1);

}

/* Setup ISR for TIM3_CH2 and start it */

if

(HAL_TIM_PWM_Start_IT(&TimHandle3, TIM_CHANNEL_2) !=

HAL_OK

)

{

  while

(1);

}

}

/* TIM3 Interrupt Handler */

void

TIM3_IRQHandler(

void

){

  HAL_TIM_IRQHandler( &TimHandle3 );

}

/* Use HAL structure to switch between TIM3 ISR Cases */

void

HAL_TIM_PWM_PulseFinishedCallback(

TIM_HandleTypeDef

*htim)

{

  if

( htim->

Channel

==

HAL_TIM_ACTIVE_CHANNEL_2

){

    static

uint32_t

test = 0;

    if

( !(test % 500) ){

      HAL_GPIO_TogglePin(getPORT(LED4), getPIN(LED4)); //IS NOT PB5! 

    }

    test++;

  }

}

2 REPLIES 2
Posted on May 22, 2017 at 11:57

RM0376

20.4.20  TIM3 option register (TIM3_OR) .

Reset value: 0x0000

Bit 3 TI_RMP: Timer3 remap on PB5

This bit is set and cleared by software.

1: TIM3_CH2 selected

0: TIM22_CH2 selected

?

JW

Posted on May 22, 2017 at 20:26

Hi waclawek.jan,

thank you for pointing out the remap, i didn't have tryed it yet but i know that's the thing i missed from the initialisation... did it a dozens of times on SPL, just assumed it was did automatically with HAL: silly me, i didn't have read the Tim_Ex driver section of the manual.

What's missing is ' HAL_TIMEx_RemapConfig( &TimHandle3, TIM3_TI2_GPIOB5_AF5 ) '

Thank you again,

have a great day,

Davide.