AnsweredAssumed Answered

STM32F746, PWM

Question asked by s.boris on Jul 14, 2016
Latest reply on Jul 15, 2016 by s.boris
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);
    }
}

Outcomes