cancel
Showing results for 
Search instead for 
Did you mean: 

How to generate interrupt of exact 1us in STM32F746ZG?

GPati.3
Associate II

I have created a simple project, where I need to get the interrupt at exactly 1us.

0693W000006H8xOQAS.jpgPlease find attached an image of the clock configurations that were created in STM32Cub3MX.

I am using TIM3 which is associated with APB1, so the MAX frequency for this would be 108.

Please find below the configuration of the TIM3.

static void MX_TIM3_Init(void)
{
 
  /* USER CODE BEGIN TIM3_Init 0 */
 
  /* USER CODE END TIM3_Init 0 */
 
  TIM_ClockConfigTypeDef sClockSourceConfig = {0};
  TIM_MasterConfigTypeDef sMasterConfig = {0};
 
  /* USER CODE BEGIN TIM3_Init 1 */
  uint32_t PULSE_WIDTH = HAL_RCC_GetPCLK1Freq() * 2 / 1000000 ;
 
  /* USER CODE END TIM3_Init 1 */
  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 1-1;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 26;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  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();
  }
  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    Error_Handler();
  }
  /* USER CODE BEGIN TIM3_Init 2 */
  /* USER CODE END TIM3_Init 2 */
 
}

With the above setting of Prescalar & Period, The least interrupt trigger interval I get is 1.6us, But I need exactly 1 us.

The ISR of this is as below

void TIM3_IRQHandler(void)
{
  /* USER CODE BEGIN TIM3_IRQn 0 */
 
  /* USER CODE END TIM3_IRQn 0 */
  HAL_TIM_IRQHandler(&htim3);
  /* USER CODE BEGIN TIM3_IRQn 1 */
  HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_8);
  /* USER CODE END TIM3_IRQn 1 */
}

I am simply starting the timer in the main function as below :

HAL_TIM_Base_Start_IT(&htim3);

Can anyone please help me with this?

Also some insight about what is the minimum interrupt time that can be achieved with STM32f746xx?

Any help in this regard is highly appreciated.

Thanks in Advance.

5 REPLIES 5

Find a way to solve your problem without interrupting at 1 MHz​

T​he Period value here should be higher to get 1 MHz

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
KnarfB
Principal III

There is a big difference between toggling a GPIO and exceuting an interrupt handler @ 1 MHz. If you set TIM10 to PWM channel 1 output, you can toggle PB8 @ 1 MHz without any interrupt handler. An interrupt handler needs few dozends of MCU cycles for saving and restoring the MCU state. @ 216 MHz MCU clock there are only 216 machine cycles between interrupts, not much room left for doing useful things.

Hi Tesla DeLorean

Thank you for the message

Can you please give some more details about your answer?

I want to print out 5 waveforms during 1us.

So, I printed out 5 MHz period PWM from TIM3.

We tried to change the duty cycle of TIM3 to zero at interrupt time by running a 1 MHz timer counter on TIM6. However, it was confirmed that TIM6's 1 MHz timer counter interrupt failed to generate a signal for re-timing. Do you know the solution?