AnsweredAssumed Answered

Gated Timer always active

Question asked by Kai Mauer on Jun 26, 2015
Hi,
I use two timers, timer 2 as slave with 2kHz and timer 4 as master (1Hz) in free running mode. I want to switch on and off timer 2 with timer 4. I configured the timers and they work as expected. However, when the slave timer is in gated mode it always is on and cannot be switched on and off according to the 1Hz frequency with timer 4.
What is wrong with my config? Here is the important code, down below is the complete code.
// SLAVE
TIM_SlaveConfigTypeDef sSlaveConfig;
 
sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
 
//ITR3 (TS = 011): Timer 2 = Slave, Timer 4 = Master
sSlaveConfig.InputTrigger = TIM_TS_ITR3;
sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING;
sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
sSlaveConfig.TriggerFilter = 0;
 
HAL_TIM_SlaveConfigSynchronization(&Tim2Handle, &sSlaveConfig);
 
// MASTER
TIM_MasterConfigTypeDef sTIM_MasterConfigTypeDef;
sTIM_MasterConfigTypeDef.MasterOutputTrigger = TIM_TRGO_ENABLE;
sTIM_MasterConfigTypeDef.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
 
HAL_TIMEx_MasterConfigSynchronization(&Tim4Handle, &sTIM_MasterConfigTypeDef);


The complete code:
/**
 ******************************************************************************
 * @file    timerSync.c
 * @author  timertick_t
 * @version
 * @date    25.06.2015
 * @brief   This file contains functions for timer sync example
 *
 ******************************************************************************
 */
 
#include "stm32f4xx.h"
#include "stm32f4_discovery.h"
#include "pwmTimer2.h"  //defines
#include "timerSync.h"
 
TIM_HandleTypeDef Tim4Handle;
TIM_HandleTypeDef Tim2Handle;
void timerSync(void) {
    /*
     Alignment:
     Timer 2 32-bit: PWM beep - PA1 TIM2_CH2
 
     Timer 3 16-bit: Resolver -  TI1 + TI2 = TIM3_CH1 + TIM3_CH2
     TIM3_CH1: PA6 PC6 PB4
     TIM3_CH2: PA7 PC7 PB5
     PC7 = MCLK (Audio)
     => TIM3_CH1: PC6
     => TIM3_CH2: PC7
 
     Timer 4 16-bit: IRQ call: LED blink
     Timer 5 16-bit:
 
     Table 97. TIMx internal trigger connection:
     ITR3 (TS = 011)
     => Timer 2: Slave
     => Timer 4: Master
     */
 
    BSP_LED_Init(LED5);
    BSP_LED_Init(LED6);
    // some household stuff
    if (HAL_Init() != HAL_OK) {
        /* Start Conversation Error */
        Error_Handler();
    }
 
    //Step 1:
    //Config slave, Timer 2 32-bit: PWM beep - PA1 TIM2_CH2
    // frequency 0x20E0 = 9.980kHz, 0xA400 = 2.000kHz
    // => Timer 2 32-bit: PWM beep - PA1 TIM2_CH2
 
    /* First and foremost: Enable TIM2 clock & Port */
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOAEN;
 
    GPIO_InitTypeDef GPIO_InitStructure;
    GPIO_InitStructure.Pin = GPIO_PIN_1;
    GPIO_InitStructure.Mode = GPIO_MODE_AF_PP;  //Alternate function ! ! !
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    GPIO_InitStructure.Alternate = GPIO_AF1_TIM2;
    HAL_GPIO_Init(GPIOA, &GPIO_InitStructure);
 
    //Timer settings:
    //frequency determined by the value of the TIMx_ARR
    //duty cycle determined by the value of the TIMx_CCRx register
 
    /* Set TIM2 instance */
    Tim2Handle.Instance = TIM2;
    Tim2Handle.Init.Prescaler = 0;
    Tim2Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    Tim2Handle.Init.Period = FREQUENCY; // 0x20E0 = 9.980kHz, 0xA400 = 2.000kHz (presc = 0)
    Tim2Handle.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; // 4 oder 1: kein Einfluss
    Tim2Handle.Init.RepetitionCounter = 0x10000;  // todo necessary?
    HAL_TIM_Base_Init(&Tim2Handle);
 
    TIM_ClockConfigTypeDef sConfigTIM_ClockConfig;
    sConfigTIM_ClockConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; //TIM_CLOCKSOURCE_ITR3;
    sConfigTIM_ClockConfig.ClockPolarity = TIM_CLOCKPOLARITY_RISING;
    sConfigTIM_ClockConfig.ClockPrescaler = TIM_CLOCKPRESCALER_DIV1; // without influence 1, 2, 4, 8x
    sConfigTIM_ClockConfig.ClockFilter = 0;
    HAL_TIM_ConfigClockSource(&Tim2Handle, &sConfigTIM_ClockConfig);
 
    TIM_OC_InitTypeDef sConfigTIM_OC;
    sConfigTIM_OC.OCMode = TIM_OCMODE_PWM1;
    sConfigTIM_OC.Pulse = (FREQUENCY * 4) / 10; // duty cycle 40%
    sConfigTIM_OC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigTIM_OC.OCNPolarity = TIM_OCNPOLARITY_HIGH;
    sConfigTIM_OC.OCFastMode = TIM_OCFAST_ENABLE;
    sConfigTIM_OC.OCIdleState = TIM_OCIDLESTATE_RESET;
    sConfigTIM_OC.OCNIdleState = TIM_OCNIDLESTATE_RESET;
    HAL_TIM_PWM_ConfigChannel(&Tim2Handle, &sConfigTIM_OC, TIM_CHANNEL_2);
 
    TIM_SlaveConfigTypeDef sSlaveConfig;
 
    /*!< Slave mode selection This parameter can be a value of @ref TIM_Slave_Mode */
    // TIM_SLAVEMODE_GATED
    // TIM_SLAVEMODE_TRIGGER
    // TIM_SLAVEMODE_EXTERNAL1
    sSlaveConfig.SlaveMode = TIM_SLAVEMODE_GATED;
 
    /*!< Input Trigger source This parameter can be a value of @ref TIM_Trigger_Selection */
    //ITR3 (TS = 011): Timer 2 = Slave, Timer 4 = Master
    sSlaveConfig.InputTrigger = TIM_TS_ITR3;
 
    /*!< Input Trigger polarity This parameter can be a value of @ref TIM_Trigger_Polarity */
    sSlaveConfig.TriggerPolarity = TIM_TRIGGERPOLARITY_RISING; // TIM_TRIGGERPOLARITY_RISING;
 
    /*!< Input trigger prescaler  This parameter can be a value of @ref TIM_Trigger_Prescaler */
    sSlaveConfig.TriggerPrescaler = TIM_TRIGGERPRESCALER_DIV1;
 
    /*!< Input trigger filter  This parameter can be a number between Min_Data = 0x0 and Max_Data = 0xF */
    sSlaveConfig.TriggerFilter = 0;
 
    HAL_TIM_SlaveConfigSynchronization(&Tim2Handle, &sSlaveConfig);
 
    HAL_TIM_PWM_Init(&Tim2Handle);
 
    HAL_TIM_PWM_Start(&Tim2Handle, TIM_CHANNEL_2);
 
    //#########################################################################################
    //Step 2:
    //Config master, on/off (blink)
 
    uint32_t uwPrescalerValue = (uint32_t) ((SystemCoreClock / 2) / 10000) - 1;
 
    /* First and foremost: Enable TIM clock & Port */
    // LED: GPIO B8
    RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;
 
    //Timer 2
    RCC->APB1ENR |= RCC_APB1ENR_TIM2EN;
 
    //GPIO B8, today with open-drain
    GPIO_InitStructure.Pin = GPIO_PIN_8;
    GPIO_InitStructure.Mode = GPIO_MODE_OUTPUT_OD;  // Open drain
    GPIO_InitStructure.Speed = GPIO_SPEED_FAST;
    GPIO_InitStructure.Pull = GPIO_NOPULL;
    HAL_GPIO_Init(GPIOB, &GPIO_InitStructure);
    HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_RESET);  // LED ON!
    //HAL_GPIO_WritePin(GPIOB, GPIO_PIN_8, GPIO_PIN_SET);  // LED OFF!
 
    /* Set TIMx instance */
    Tim4Handle.Instance = TIM4;
    Tim4Handle.Init.Period = 10000 - 1; //9999 = 0x270F
    Tim4Handle.Init.Prescaler = uwPrescalerValue;
    Tim4Handle.Init.ClockDivision = 0;
    Tim4Handle.Init.CounterMode = TIM_COUNTERMODE_UP;
    if (HAL_TIM_Base_Init(&Tim4Handle) != HAL_OK) {
        /* Initialization Error */
        Error_Handler();
    };
 
    TIM_MasterConfigTypeDef sTIM_MasterConfigTypeDef;
    /*!< Trigger output (TRGO) selection. This parameter can be a value of @ref TIM_Master_Mode_Selection
     #define    TIM_TRGO_RESET            ((uint32_t)0x0000)
     #define    TIM_TRGO_ENABLE           (TIM_CR2_MMS_0)
     #define    TIM_TRGO_UPDATE           (TIM_CR2_MMS_1)
     #define    TIM_TRGO_OC1              ((TIM_CR2_MMS_1 | TIM_CR2_MMS_0))
     #define    TIM_TRGO_OC1REF           (TIM_CR2_MMS_2)    */
    sTIM_MasterConfigTypeDef.MasterOutputTrigger = TIM_TRGO_ENABLE;
 
    /*!< Master/slave mode selection. This parameter can be a value of @ref TIM_Master_Slave_Mode */
    sTIM_MasterConfigTypeDef.MasterSlaveMode = TIM_MASTERSLAVEMODE_ENABLE;
 
    HAL_TIMEx_MasterConfigSynchronization(&Tim4Handle, &sTIM_MasterConfigTypeDef);
 
    /*##-2- Start the TIM Base generation in interrupt mode ####################*/
    if (HAL_TIM_Base_Start_IT(&Tim4Handle) != HAL_OK) {
        /* Starting Error */
        Error_Handler();
    }
    while (0) {
    };
}
 
/**
 * @brief  Period elapsed callback in non blocking mode
 * @param  htim: TIM handle
 * @retval None
 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) {
    if (htim) {
    }; //dummy for compiler warning
    HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_8);
}
 
/**
 *
 * From ... /stm32f4xx_hal_msp.c:
 *
 * @brief TIM MSP Initialization
 *        This function configures the hardware resources used in this example:
 *           - Peripheral's clock enable
 *           - Peripheral's GPIO Configuration
 * @param htim: TIM handle pointer
 * @retval None
 */
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef *htim) {
    if (htim) {
    }; //dummy
 
    /*##-1- Enable peripherals and GPIO Clocks #################################*/
    __HAL_RCC_TIM4_CLK_ENABLE();
    //TIM4_CLK_ENABLE();
 
    /*##-2- Configure the NVIC for TIMx ########################################*/
    /* Set Interrupt Group Priority */
    HAL_NVIC_SetPriority(TIM4_IRQn, 4, 0);
 
    /* Enable the TIMx global Interrupt */
    HAL_NVIC_EnableIRQ(TIM4_IRQn);
}
 
void Error_Handler(void) {
    BSP_LED_On(LED5);
    while (1) {
    }
}
 
#if NO_CODE_HERE
#endif // #if NO_CODE_HERE

Outcomes