cancel
Showing results for 
Search instead for 
Did you mean: 

Callback function for HAL_TIM_OnePulse_Start_IT?

FwMaker
Associate II

How do I get a callback function for the HAL_TIM_OnePulse_Start_IT function?

I have a STM32G071 Nucleo board.

I whish to start a one-shot timer that calls an interrupt callback function when the time has elapsed. For this I have chosen the timer 6 and had the CubeMX, to generate the code:

static void MX_TIM6_Init(void)
{
 
  /* USER CODE BEGIN TIM6_Init 0 */
 
  /* USER CODE END TIM6_Init 0 */
 
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 
  /* USER CODE BEGIN TIM6_Init 1 */
 
  /* USER CODE END TIM6_Init 1 */
  htim6.Instance = TIM6;
  htim6.Init.Prescaler = 3;
  htim6.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim6.Init.Period = 40000;
  htim6.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
  if (HAL_TIM_Base_Init(&htim6) != HAL_OK)
  {
    Error_Handler();
  }
  if (HAL_TIM_OnePulse_Init(&htim6, TIM_OPMODE_SINGLE) != HAL_OK)
  {
    Error_Handler();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_UPDATE;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim6, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM6_Init 2 */
 
  /* USER CODE END TIM6_Init 2 */
 
}

I have then tried to start the timer with:

HAL_TIM_OnePulse_Start_IT(&htim6, TIM_CHANNEL_6);

and then tried to get the ISR to toggle the LED with this code:

void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *TimHandle){
	HAL_GPIO_TogglePin(LED4_GPIO_Port, LED4_Pin);
}

And this code:

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *TimHandle){
	HAL_GPIO_TogglePin(LED4_GPIO_Port, LED4_Pin);
}

But none of these are working.

What am I doing wrong?

1 ACCEPTED SOLUTION

Accepted Solutions
FwMaker
Associate II

Thanks for your suggestions Jaroslav

I actually found a solution to the problem, by watching this video; that has a an example that uses OnePulse:

https://www.youtube.com/watch?v=3yvY7pLMHAg&t=289s

And what I found that were missing in order to make my code work was this as a part of the timer init:

/* USER CODE BEGIN TIM6_Init 2 */
  // Clear interrupt bit in SR that was set as a side effect of generating an update event in TIM_Base_SetConfig
  __HAL_TIM_CLEAR_IT(&htim6, TIM_IT_UPDATE);
  // enable timer update interrupt
  __HAL_TIM_ENABLE_IT(&htim6,TIM_IT_UPDATE);
  /* USER CODE END TIM6_Init 2 */

 The timer pulse-width were set with:

htim6.Instance->ARR=20;

I could NOT make the code work, with start of timer by:

HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_OnePulse_Start_IT(&htim6, TIM_CHANNEL_6);

so I started the timer with:

__HAL_TIM_ENABLE(&htim6);

like described in the video.

The interrupt handler were:

/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
	if(htim==&htim6) // check source
	{
		transmissionReceived=1;
	}
}
/* USER CODE END 4 */

(Like you also suggested 😊 )

View solution in original post

2 REPLIES 2
Jaroslav JANOS
ST Employee

Hello,

you have to start the timer with:

HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_OnePulse_Start_IT(&htim6, TIM_CHANNEL_6);

In your case, the UIE bit in DIER register is not set and the interrupt is not enabled.

The IRQ handler will then call your HAL_TIM_PeriodElapsedCallback() function and toggle the pin.

-- Jaroslav

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

FwMaker
Associate II

Thanks for your suggestions Jaroslav

I actually found a solution to the problem, by watching this video; that has a an example that uses OnePulse:

https://www.youtube.com/watch?v=3yvY7pLMHAg&t=289s

And what I found that were missing in order to make my code work was this as a part of the timer init:

/* USER CODE BEGIN TIM6_Init 2 */
  // Clear interrupt bit in SR that was set as a side effect of generating an update event in TIM_Base_SetConfig
  __HAL_TIM_CLEAR_IT(&htim6, TIM_IT_UPDATE);
  // enable timer update interrupt
  __HAL_TIM_ENABLE_IT(&htim6,TIM_IT_UPDATE);
  /* USER CODE END TIM6_Init 2 */

 The timer pulse-width were set with:

htim6.Instance->ARR=20;

I could NOT make the code work, with start of timer by:

HAL_TIM_Base_Start_IT(&htim6);
HAL_TIM_OnePulse_Start_IT(&htim6, TIM_CHANNEL_6);

so I started the timer with:

__HAL_TIM_ENABLE(&htim6);

like described in the video.

The interrupt handler were:

/* USER CODE BEGIN 4 */
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim){
	if(htim==&htim6) // check source
	{
		transmissionReceived=1;
	}
}
/* USER CODE END 4 */

(Like you also suggested 😊 )