cancel
Showing results for 
Search instead for 
Did you mean: 

PWM mode problems with TIM14 (STM32f030)

VK.3
Associate II

Hi ST,

I'm trying to enable PWM mode of Timer14 (STM32F030C8T6). But the output is always LOW.

I do all the steps described in the ST Periferal Library. Also I successfully run the similar code on STM32F103xxxx.

Could you check this simple code? Is there a hardware bug with Tim14 pwm mode? Thanks.

#include "stm32f0xx_tim.h"
#include "stm32f0xx_rcc.h"
#include "stm32f0xx_gpio.h"
#include "stm32f0xx.h"
 
int main(void)
{
	GPIO_InitTypeDef port;
	TIM_TimeBaseInitTypeDef timer;
	TIM_OCInitTypeDef timerPWM;
 
	RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM14, ENABLE);
 
	GPIO_StructInit(&port);
	port.GPIO_Mode = GPIO_Mode_AF;
	port.GPIO_Pin = GPIO_Pin_4;
	port.GPIO_Speed = GPIO_Speed_Level_3;
	GPIO_Init(GPIOA, &port);
 
	TIM_TimeBaseStructInit(&timer);
	timer.TIM_Prescaler = 480;
	timer.TIM_Period = 1000;
	timer.TIM_ClockDivision = TIM_CKD_DIV1;
	timer.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInit(TIM14, &timer);
 
//	TIM_SetAutoreload(TIM14, 1000);
	TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
 
	TIM_OCStructInit(&timerPWM);
	timerPWM.TIM_OCMode = TIM_OCMode_PWM1;
	timerPWM.TIM_OutputState = TIM_OutputState_Enable;
	timerPWM.TIM_Pulse = 500;
	timerPWM.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OC1Init(TIM14, &timerPWM);
    TIM_Cmd(TIM14, ENABLE);
 
//	TIM_OC1FastConfig(TIM14, TIM_OCFast_Disable);
//	TIM_CtrlPWMOutputs(TIM14, ENABLE);
//	TIM_OC1PreloadConfig(TIM14, TIM_OCPreload_Enable);
 
    while(1)
    {
    }
}

1 ACCEPTED SOLUTION

Accepted Solutions
VK.3
Associate II

Thanks @Community member​ !

It was my mistake. PWM operates well.

I selected the wrong AF channel.0693W000004K914QAC.png

View solution in original post

5 REPLIES 5

Read out and check/post content of TIM and relevant GPIO registers.

JW

VK.3
Associate II

Hi @Community member​ ,

I checked all the bits and the configuration seems OK.

BTW, if I enable and configure the interrupt for the PWM I observe the interrupt (as expected). But the output pin is still in LOW state.

Here are registers for timer14 and GPIOA. (I used UART to get the registers content. So the registers also contain the UART setting).

Eventually, it looks like a hardware bug somewhere in the path between the Timer14 and the GPIO.

TIM14->ARR = 00001F40
TIM14->CCMR1 = 00000068
tmp=TIM14->CCMR2 = 00000000
tmp=TIM14->CR1 = 00000001
TIM14->EGR = 00000000
TIM14->CCER = 00000001
 
GPIOA->MODER = 28280200
GPIOA->OTYPER = 00000000
GPIOA->PUPDR = 24000000
GPIOA->OSPEEDR = 0C3C0300
GPIOA->AFR[0] = 00000000
GPIOA->AFR[1] = 00000110

GPIOA->AFR[0] = 00000000

Look into datasheet,  Alternate functions ... table - for PA4, TIM4_CH1 is AF4. That means, that GPIOA->AFR[0] must have the 4th nibble set to 4, i.e.

GPIOA->AFR[0] = 0x00040000

I don't know how is this set in SPL.

JW

VK.3
Associate II

Thanks @Community member​ !

It was my mistake. PWM operates well.

I selected the wrong AF channel.0693W000004K914QAC.png

Oh, I see.

Unfortunately, the assignment of peripherals to AF in the GPIO matrix is not fixed (it is to some extent in 'F2/'F4/'F7, but it's not in other families, and it tends to get worse with increasing number of pins). So, at the end of the day, you need to check each pin individually in RM, or rely on tools like CubeMX, if you can bear their result.

JW