cancel
Showing results for 
Search instead for 
Did you mean: 

Cannot start to count up TIM3 in PWM generation mode

HMasu.1
Senior

I use STM32MP151CAA3 mounted on my own board.

I want to start to count up TIM3 in PWM generation mode in Cortex-M4.

I have written following code, However, ​TIM3 doesn't start to count up (After Line 56, TIM3_CNT value doesn't change at all).

In this code, PB04 port is used as PWM output port.

void TIM3_Count_Start(void)
{
	TIM_HandleTypeDef m_handleTim3;
	TIM_OC_InitTypeDef sConfigOC;
	TIM_SlaveConfigTypeDef sSlaveConfig;
	TIM_MasterConfigTypeDef sMasterConfig;
	GPIO_InitTypeDef GPIO_InitStruct;
 
	/* Peripheral clock enable */
        __TIM3_CLK_ENABLE();
  
        GPIO_InitStruct.Pin = GPIO_PIN_4;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Pull = GPIO_NOPULL;
        GPIO_InitStruct.Speed = GPIO_SPEED_LOW;
        GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
 
	m_handleTim3.Instance = TIM3;
	m_handleTim3.Init.Prescaler = 0;
	m_handleTim3.Init.CounterMode = TIM_COUNTERMODE_UP;
	m_handleTim3.Init.Period = 0;
	m_handleTim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
	HAL_TIM_Base_Init(&m_handleTim3);
 
	HAL_TIM_PWM_Init(&m_handleTim3);
 
	sConfigOC.OCMode = TIM_OCMODE_PWM1;
	sConfigOC.Pulse = 0xFFFF;
	sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
	sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
	HAL_TIM_PWM_ConfigChannel(&m_handleTim3, &sConfigOC, TIM_CHANNEL_1);
 
	sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;
	sSlaveConfig.InputTrigger = TIM_TS_ITR0;
	HAL_TIM_SlaveConfigSynchronization(&m_handleTim3, &sSlaveConfig);
 
	sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
	sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
	HAL_TIMEx_MasterConfigSynchronization(&m_handleTim3, &sMasterConfig);
 
	TIM3->PSC = 399;
	TIM3->ARR = 49;
 
	TIM3->CCR1 = 0xFFFF;
 
	TIM3->CR1 |= 0x0080;
 
	TIM3->EGR |= TIM_EGR_UG;
 
	TIM3->CCER |= 0x0001;
 
	TIM3->SR = 0;
	TIM3->DIER |= 0x0001;
 
	TIM3->CR1 |= 0x0001;		/* TIM3 Count Start */
}

Is there any missing in above code?

Best Regards.

H. Masuda

1 ACCEPTED SOLUTION

Accepted Solutions
TDK
Guru

> sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;

If you want the timer to run by itself, set this to TIM_SLAVEMODE_DISABLE. Otherwise, you need to ensure there is a external clock source driving it.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

6 REPLIES 6
Michal Dudka
Senior III

To what value it should count ? 😉

@Michal Dudka​ 

I expect that TIM3_CNT counts up from 0 to TIM3_ARR value (and returns to 0, and counts up to TIM3_ARR value again).

When I used to use STM32F437ZI, TIM3​_CNT counts up in PWM generation mode, so I expect it to behave similarly.

H. Masuda.​

Michal Dudka
Senior III

Right... sorry i've overlooked second part of your code. I've read your "period" and "prescaler" settings in HAL timer init part. One do not expect that you initialize/config timer twice consecutively (why the hell ?). Rewrite your code to some familiar form (use HAL or LL or use proper bit macros like "TIM_EGR_UG"). Nobody have time to list thru datasheet and counts bit by bit to read your timer configuration. I am not familiar with MPUs but i would look into RCC configuration first ... check if timer clock is enabled.

TDK
Guru

> sSlaveConfig.SlaveMode = TIM_SLAVEMODE_EXTERNAL1;

If you want the timer to run by itself, set this to TIM_SLAVEMODE_DISABLE. Otherwise, you need to ensure there is a external clock source driving it.

If you feel a post has answered your question, please click "Accept as Solution".

Hi @Michal Dudka​ 

Thank you for your answer.

I am going to use it as an example.

And, I take care to ask questions that are easier to understand.​

H.Masuda

Hi @TDK​ 

I tried to use another timer as prescaler, but the settings to do that were wrong.

Your advice solved the problem.

Thank you very much.

H. Masuda