cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F746, PWM

boris239955_stm1_st
Associate II
Posted on July 14, 2016 at 08:43

Hello, I want to generate pwm out through tim4 channel 1

This is the code, but isn't wokring

void hw_init_timer_4(TIMER_INIT* _tim4)
{
GPIO_InitTypeDef gpio_init;
RCC_ClkInitTypeDef clkconfig;
TIM_ClockConfigTypeDef clk_src_cnf; // Configuration timer clock
TIM_MasterConfigTypeDef master_cnf; // Master config
TIM_OC_InitTypeDef config_mode; // Config timer mode
U32 tim_clk;
U32 pFLatency;
U32 APB1_prescaler = 0;
U32 tim_prescaler = 0;
U32 tim_int_period = 0;
U32 tim_pulse = 0;
__HAL_RCC_TIM4_CLK_ENABLE();
HAL_NVIC_SetPriority(TIM4_IRQn, 0, _tim4->tim_priority); // Set timer 4 priority
HAL_NVIC_EnableIRQ(TIM4_IRQn); // Enable timer 4 global interrupt
hw_timer_4_app_callback = _tim4->tim_callback;
HAL_RCC_GetClockConfig(&clkconfig, &pFLatency);
// get APB1 prescaler
APB1_prescaler = clkconfig.APB1CLKDivider;
// Compute timer clock
if (APB1_prescaler == RCC_HCLK_DIV1)
{
tim_clk = HAL_RCC_GetPCLK1Freq(); // PCLK1 is 50Mhz
}
else
{
tim_clk = 2 * HAL_RCC_GetPCLK1Freq(); // PCLK1 is 100Mhz
}
tim_prescaler = (U32) ((tim_clk / _tim4->tim_freq) - 1); // Compute the prescaler value to have time counter clock equal to 1MHz
tim_int_period = (_tim4->tim_freq / _tim4->tim_int_freq) - 1; // Interrupt frequency in Hz
htim4.Instance = TIM4;
htim4.Init.Period = tim_int_period;
htim4.Init.Prescaler = tim_prescaler;
htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
htim4.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
HAL_TIM_Base_Init(&htim4);
clk_src_cnf.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
HAL_TIM_ConfigClockSource(&htim4, &clk_src_cnf);
HAL_TIM_PWM_Init(&htim4);
master_cnf.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
master_cnf.MasterOutputTrigger = TIM_TRGO_UPDATE; // TIM_TRGO_RESET
//master_cnf.MasterOutputTrigger2 = TIM_TRGO2_ENABLE;
HAL_TIMEx_MasterConfigSynchronization(&htim4, &master_cnf);
config_mode.OCMode = TIM_OCMODE_PWM1;
config_mode.Pulse = 2;
config_mode.OCPolarity = TIM_OCPOLARITY_HIGH;
config_mode.OCFastMode = TIM_OCFAST_DISABLE;
HAL_TIM_PWM_ConfigChannel(&htim4, &config_mode, TIM_CHANNEL_1);
HAL_TIM_MspPostInit(&htim4);
HAL_TIM_PWM_Start(&htim4, TIM_CHANNEL_1);
}
void HAL_TIM_MspPostInit(TIM_HandleTypeDef* tim_handler)
{
GPIO_InitTypeDef gpio_init;
if (tim_handler->Instance == TIM4)
{
// Config output pin to tim4
__HAL_RCC_GPIOD_CLK_ENABLE();
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_12, GPIO_PIN_RESET);
gpio_init.Pin = GPIO_PIN_12;
gpio_init.Mode = GPIO_MODE_AF_PP;
gpio_init.Speed = GPIO_SPEED_HIGH;
gpio_init.Pull = GPIO_NOPULL; //GPIO_NOPULL
gpio_init.Alternate = GPIO_AF2_TIM4;
HAL_GPIO_Init(GPIOD, &gpio_init);
}
}

#pwm #output #stm32f746discovery
2 REPLIES 2
slimen
Senior
Posted on July 14, 2016 at 16:54

Hi,

I recommend you to have a look to the TIM_PWMOutput example under the STM32F7Cube firmware package, it may be helpful: 

STM32Cube_FW_F7_V1.4.0\Projects\STM32756G_EVAL\Examples\TIM\TIM_PWMOutput

Regards

boris239955_stm1_st
Associate II
Posted on July 15, 2016 at 06:58

Hi, thank you for the answer. I found what is wrong, I use stm32f746 discovery board, and I didnt init correct GPIO.