AnsweredAssumed Answered

STM32Cube Libraries PWM

Question asked by de_lazzari.giacomo on Oct 19, 2014
Latest reply on Oct 20, 2014 by Montassar BEN ROMDHANE
Hi, I am trying to output two PWM signals through channels 1 & 3 of TIM3.
I'm using the Nucleo F401RE board, so my MCU is STM32F401RE. I checked the example code included with the HAL and also the code generated by STM32CubeMX. This is my current code:

#include "stm32f4xx.h"
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_rcc.h"
#include "stm32f4xx_hal_gpio.h"
#include "stm32f4xx_hal_tim.h"
#include "stm32f4xx_hal_tim_ex.h"
#include "uart.h"
#include "xprintf.h"
TIM_HandleTypeDef htim3;
int main(void)
    xfunc_in = UART_getc;
    xfunc_out = UART_putc;
    xprintf("Initializing GPIO and Timer\n");
    GPIO_InitTypeDef GPIO_InitStruct;
    /* Peripheral clock enable */
    /**TIM3 GPIO Configuration
    PC6     ------> TIM3_CH1
    PC8     ------> TIM3_CH3
    GPIO_InitStruct.Pin       = GPIO_PIN_5;
    GPIO_InitStruct.Mode      = GPIO_MODE_OUTPUT_PP;
    GPIO_InitStruct.Pull      = GPIO_NOPULL;
    GPIO_InitStruct.Speed     = GPIO_SPEED_HIGH;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_8;
    GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
    GPIO_InitStruct.Pull = GPIO_PULLUP;
    GPIO_InitStruct.Speed = GPIO_SPEED_HIGH;
    GPIO_InitStruct.Alternate = GPIO_AF2_TIM3;
    HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
    TIM_ClockConfigTypeDef sClockSourceConfig;
    TIM_OC_InitTypeDef sConfigOC;
    TIM_MasterConfigTypeDef sMasterConfig;
    htim3.Instance = TIM3;
    htim3.Init.Prescaler = 35;
    htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim3.Init.Period = 100;
    htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 25;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_1);
    HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3);
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
    int i = 0;
    while (1)
        __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, i++);
        if (i > 1700) i = 0;
    return 0;

When I launch the code it stops at the serial checkpoint number 6, so it seems to fail while configuring the PWM channel. When I pause the debug I notice that the
function is called.

But if I remove the following line:
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
it happens that the program reaches the checkpoint number 8 and enters the while loop, which makes no sense to me.

Can someone help me? Thanks