STM32L072: PWM Output with HAL
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 adaptvoid
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++;
}
}
