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)
{
    UART_init(19200);
 
    xfunc_in = UART_getc;
    xfunc_out = UART_putc;
 
    xprintf("Initializing GPIO and Timer\n");
 
    GPIO_InitTypeDef GPIO_InitStruct;
 
    xprintf("1\n");
 
    /* Peripheral clock enable */
    __GPIOC_CLK_ENABLE();
    __TIM3_CLK_ENABLE();
 
    xprintf("2\n");
 
    /**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);
 
    xprintf("3\n");
 
    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;
    HAL_TIM_Base_Init(&htim3);
 
    xprintf("4\n");
 
    HAL_TIM_PWM_Init(&htim3);
 
    xprintf("6\n");
 
    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);
 
    xprintf("7\n");
 
    HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
 
    xprintf("8\n");
 
    int i = 0;
 
    while (1)
    {
        __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_1, i++);
        if (i > 1700) i = 0;
 
        HAL_Delay(100);
    }
 
    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
HardFault_Handler(void);
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

Outcomes