cancel
Showing results for 
Search instead for 
Did you mean: 

How to trigger a function at an output compares ?

SGROU.1
Associate III

Hello,

I try that when my OUTPUT compare is triggered, it triggers a function.

As for the PeriodElapsedCallback method, when the timer is in overflow, it creates an interuption and the PeriodElapsedCallback function is called. Is there a function doing the same job as this one but when a compare output is triggered?

I found this function HAL_TIM_OC_DelayElapsedCallback

that seems to be doing what I'm trying to do, but I can't use it ...

Thanks for your helps .

8 REPLIES 8

Cube is open source, you can look it up yourself:

void HAL_TIM_IRQHandler(TIM_HandleTypeDef *htim)
{
  /* Capture compare 1 event */
  if (__HAL_TIM_GET_FLAG(htim, TIM_FLAG_CC1) != RESET)
  {
    if (__HAL_TIM_GET_IT_SOURCE(htim, TIM_IT_CC1) != RESET)
    {
      {
        __HAL_TIM_CLEAR_IT(htim, TIM_IT_CC1);
        htim->Channel = HAL_TIM_ACTIVE_CHANNEL_1;
 
        /* Input capture event */
        if ((htim->Instance->CCMR1 & TIM_CCMR1_CC1S) != 0x00U)
        {
#if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
          htim->IC_CaptureCallback(htim);
#else
          HAL_TIM_IC_CaptureCallback(htim);
#endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
        }
        /* Output compare event */
        else
        {
#if (USE_HAL_TIM_REGISTER_CALLBACKS == 1)
          htim->OC_DelayElapsedCallback(htim);
          htim->PWM_PulseFinishedCallback(htim);
#else
          HAL_TIM_OC_DelayElapsedCallback(htim);
          HAL_TIM_PWM_PulseFinishedCallback(htim);
#endif /* USE_HAL_TIM_REGISTER_CALLBACKS */
        }
        htim->Channel = HAL_TIM_ACTIVE_CHANNEL_CLEARED;
      }
    }
  }

JW

Hello,

Thanks for the answer, I know that cube is open source, of course I already went to see the HAL_TIM_IRQHandler function but I didn't find/understand what I was looking for.

Here is a piece of code where I tried to implement the HAL_TIM_OC_DelayElapsedCallback() function which I think is the function I'm looking for.

I have a breakpoint in this function, but my program never returns in this function and I don't understand why.

"Output Compare callback" but i dont' understand why she's not working in my program ...0693W000007CXT3QAO.png

MX_GPIO_Init();
  MX_UART4_Init();
  MX_TIM3_Init();
 
  HAL_TIM_OC_Start(&htim3, TIM_CHANNEL_2);
  HAL_TIM_OC_Start(&htim3, TIM_CHANNEL_4);
 
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
 
 
  }
  /* USER CODE END 3 */
}
 
static void MX_TIM3_Init(void)
{
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
  TIM_OC_InitTypeDef sConfigOC = {0};
 
  /* USER CODE BEGIN TIM3_Init 1 */
 
  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 16000;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 1000;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_OC_Init(&htim3) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.OCMode = TIM_OCMODE_TOGGLE;
  sConfigOC.Pulse = 250;
  sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
  sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  {
    Error_Handler();
  }
  sConfigOC.Pulse = 750;
  if (HAL_TIM_OC_ConfigChannel(&htim3, &sConfigOC, TIM_CHANNEL_4) != HAL_OK)
  {
    Error_Handler();
  }
 
  HAL_TIM_MspPostInit(&htim3);
 
}
 
  HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_RESET);
 
  /*Configure GPIO pin : LD1_Pin */
  GPIO_InitStruct.Pin = LD1_Pin;
  GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  GPIO_InitStruct.Pull = GPIO_NOPULL;
  GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  HAL_GPIO_Init(LD1_GPIO_Port, &GPIO_InitStruct);
 
}
 
void HAL_TIM_OC_DelayElapsedCallback(TIM_HandleTypeDef* htim){
 
	if(htim->Channel == HAL_TIM_ACTIVE_CHANNEL_2){
		HAL_GPIO_WritePin(LD1_GPIO_Port, LD1_Pin, GPIO_PIN_SET);
	}
 
}

> I have a breakpoint in this function, but my program never returns in this function and I don't understand why.

Place a breakpoint into the ISR itself (i.e. that function I copied out above). Does the program reach that point? If yes, find out where does it go instead of your HAL_TIM_OC_DelayElapsedCallback. If not, debug as usually for interrupts.

JW

PS. Where do you enable the given interrupts in TIMx_DIER?

By placing a breakpoint in the ISR, the program does not reach the breakpoint but goes directly to this line:  

  /* Process Locked */
  __HAL_LOCK(htim);

Well here is in attachment my file STM32F7xx_hal_tim.c because apparently in your file there is not this line, and in the github corresponding to my board, there is not this line in this file either ...

The problem is at line 3418.

I didn't know I had to authorize something, what should I authorize for my timer?

Maybe CubeMX has already generated this authorization automatically, it often does it ...

Line 3418 in the above file is in HAL_TIM_OC_ConfigChannel() - that's not in ISR.

I don't know what is authorization. I don't use Cube/CubeMX.

JW

Oh yes, you're right!

I looked and my breakpoint is well in ISR.

But then when I debug, my program is redirected to this line __HAL_LOCK ...

Edit : I tried to recreate the same project but on a STMF429, same problem, my program blocks at __HAL_LOCK ...