Electrical – PWM on Nucleo-STM32F103RB

hal-librarypwmstm32stm32f10x

I'm trying to just get timer based PWM output working on the F103 Nucleo board and I can't get the MCU to output anything. I spent a decent amount of time looking through the examples in the related questions relating to the F4 and F0 MCUs, but couldn't find an answer there. My code originally came from the CubeMX tool, but I changed the name of the PWM_MspInit function to match the function that HAL_TIM_PWM_INIT() calls.

My timer initialization and PWM inialization fuctions look like this:

/* TIM3 init function */
static void MX_TIM3_Init(void) {

    TIM_ClockConfigTypeDef sClockSourceConfig;
    TIM_MasterConfigTypeDef sMasterConfig;
    TIM_OC_InitTypeDef sConfigOC;

    htim3.Instance = TIM3;
    htim3.Init.Prescaler = 6400-1;
    htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
    htim3.Init.Period = 10000-1;
    htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
    HAL_TIM_PWM_Init(&htim3);

    sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
    HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig);

    HAL_TIM_PWM_Init(&htim3);

    sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
    sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
    HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig);

    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = 1000-1;
    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_2);
    HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_3);
    HAL_TIM_PWM_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4);
    HAL_TIM_PWM_Start(&htim3,TIM_CHANNEL_ALL);
}

void HAL_TIM_PWM_MspInit(TIM_HandleTypeDef *htim) {

    GPIO_InitTypeDef GPIO_InitStruct;

    if (htim->Instance == TIM3) {
        __HAL_RCC_GPIOA_CLK_ENABLE();
        __HAL_RCC_GPIOB_CLK_ENABLE();

        //Configure GPIO pins : PA6 PA7
        GPIO_InitStruct.Pin = GPIO_PIN_6 | GPIO_PIN_7;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

        //Configure GPIO pins : PB0 PB1
        GPIO_InitStruct.Pin = GPIO_PIN_0 | GPIO_PIN_1;
        GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
        GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
        HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
    }
}

Then my main() looks like this:

int main(void) {

    // Reset of all peripherals, Initializes the Flash interface and the Systick. */
    HAL_Init();

    // Configure the system clock
    SystemClock_Config();

    // Initialize all configured peripherals
    MX_TIM3_Init();

    // Infinite loop
    while (1) {

    }
}

The only part of my main.c not shown is the unmodified SystemClock_Config() function that CubeMX produced. To my understanding because of the 64MHz system clock this should produce a 1Hz PWM signal with a 10% duty cycle. When I probe PA6 or PA7 I get nothing.

Best Answer

There are two obvious problems that I could see with your code!

  1. You have not clearly mentioned how you have changed the name of HAL_TIM_PWM_MspInit() to HAL_TIM_PWM_Init() but I am assuming you have directly changed the function name in the stm32f1xx_hal_msp.c file. That could be the main mistake! Because both of them are different functions and does different initializations.

HAL_TIM_PWM_MspInit() initializes the Timer clock and HAL_TIM_PWM_Init() initializes the TIM PWM Time base according to the configurations given.

  1. You are skipping two important things in the timer configuration. One is HAL_TIM_MspPostInit() function which initializes the TIMER GPIO pins and the other is "AutoReloadPreload" has to be enabled!

    htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; HAL_TIM_MspPostInit(&htim3)