AnsweredAssumed Answered

Timer sync and IRQ

Question asked by stastny.petr on Sep 11, 2016
Latest reply on Sep 11, 2016 by waclawek.jan
Hello,
I have a problem with Timer sync and IRQ. I use TIM4 to generate period and TIM4 to generate one pulse every period. It is because I need to recalculate the next period time every period. It is for ramp velocity driving the motor. The problem is that when I enable TIM4_IRQ (commented line), The TIM1 does not receive trigger. Can you help me please how to fix this?
Thank you

    TIM_HandleTypeDef htim1;
    TIM_HandleTypeDef htim4;

int
main(void)
{
  
    GPIO_InitTypeDef   GPIO_InitStruct;
    TIM_HandleTypeDef htim1;
    TIM_HandleTypeDef htim4;
    TIM_MasterConfigTypeDef sMasterConfig4;
    TIM_HandleTypeDef    TimHandle;
    TIM_OnePulse_InitTypeDef sConfig;
  
  HAL_Init();
  SystemClock_Config();
  
  __HAL_RCC_GPIOA_CLK_ENABLE();  
    
  GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
  GPIO_InitStruct.Pull = GPIO_PULLUP;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_VERY_HIGH;
  GPIO_InitStruct.Alternate = GPIO_AF1_TIM1;
    
  GPIO_InitStruct.Pin = GPIO_PIN_8;
  HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  
    __TIM1_CLK_ENABLE();
    __TIM4_CLK_ENABLE();
  
/// PERIOD ///
  
  htim4.Instance = TIM4;
  htim4.Init.Prescaler = 41;
  htim4.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim4.Init.Period = (10 -1); // us
  HAL_TIM_Base_Init(&htim4);
  
  HAL_TIM_Base_Start_IT(&htim4);
  HAL_NVIC_SetPriority(TIM4_IRQn, 0, 1);
//  HAL_NVIC_EnableIRQ(TIM4_IRQn);
  
    sMasterConfig4.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig4.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  HAL_TIMEx_MasterConfigSynchronization(&htim4, &sMasterConfig4);
      
  
/// PULSE ///
  
  htim1.Instance = TIM1;
  htim1.Init.Period            = 104; // 2,5 us puls
  htim1.Init.Prescaler         = 0;
  htim1.Init.ClockDivision     = 0;
  htim1.Init.CounterMode       = TIM_COUNTERMODE_UP;
  htim1.Init.RepetitionCounter = 0;
  
  if (HAL_TIM_OnePulse_Init(&htim1, TIM_OPMODE_SINGLE) != HAL_OK)
  {
    Error_Handler();
  }
  
  sConfig.OCMode       = TIM_OCMODE_PWM2;
  sConfig.OCPolarity   = TIM_OCPOLARITY_HIGH;
  sConfig.Pulse        = 1;
  sConfig.ICPolarity   = TIM_ICPOLARITY_RISING;
  sConfig.ICSelection  = TIM_ICSELECTION_DIRECTTI;
  sConfig.ICFilter     = 0;
  sConfig.OCNPolarity  = TIM_OCNPOLARITY_HIGH;
  sConfig.OCIdleState  = TIM_OCIDLESTATE_RESET;
  sConfig.OCNIdleState = TIM_OCNIDLESTATE_RESET;
  
  if (HAL_TIM_OnePulse_ConfigChannel(&htim1, &sConfig, TIM_CHANNEL_1, TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }
  
        TIM1->SMCR &= ~TIM_SMCR_TS;
        TIM1->SMCR |= TIM_TS_ITR3; // ITR3 for TIM1 = TIM4
    
        TIM1->SMCR &= ~TIM_SMCR_SMS;
        TIM1->SMCR |= TIM_SLAVEMODE_TRIGGER;
      
  if (HAL_TIM_OnePulse_Start(&htim1, TIM_CHANNEL_1) != HAL_OK)
  {
    Error_Handler();
  }
  
      
  while (1)
  {
  
  }
  
}
  
void TIM4_IRQHandler(void)
{
    if (__HAL_TIM_GET_FLAG(&htim4, TIM_FLAG_UPDATE) != RESET)      //In case other interrupts are also running
    {
        if (__HAL_TIM_GET_ITSTATUS(&htim4, TIM_IT_UPDATE) != RESET)
        {
            __HAL_TIM_CLEAR_FLAG(&htim4, TIM_FLAG_UPDATE);
                            HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_5);
//                                  TIM4->ARR = 10;/* recalculation */
    }
}


Outcomes