cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103 timer interrupt fastest time is less than 1us?

Junior.Hou
Associate II

I use STM32F103CB in my project.​ but I found timer interrupt fastest time can't less than 1us.

Timer prescaler is 0, up counter mode, counter period is 36, no inernal clock division and auto-reload enable. When timer interrupt occur. I set Pin high and low in turn. But the output time from the pin is 1us.

/* TIM2 init function */

void MX_TIM2_Init(void)

{

 TIM_ClockConfigTypeDef sClockSourceConfig = {0};

 TIM_MasterConfigTypeDef sMasterConfig = {0};

 htim2.Instance = TIM2;

 htim2.Init.Prescaler = 0;

 htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

 htim2.Init.Period = 36;

 htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

 htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

 if (HAL_TIM_Base_Init(&htim2) != HAL_OK)

 {

  Error_Handler();

 }

 sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

 if (HAL_TIM_ConfigClockSource(&htim2, &sClockSourceConfig) != HAL_OK)

 {

  Error_Handler();

 }

 sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

 sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

 if (HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig) != HAL_OK)

 {

  Error_Handler();

 }

}

/**

 * @brief This function handles TIM2 global interrupt.

 */

void TIM2_IRQHandler(void)

{

 /* USER CODE BEGIN TIM2_IRQn 0 */

TIM2->SR &= ~(1 << 0);

static uint8_t bPin = 0;

bPin = !bPin;

if (bPin)

{

GPIOB->BSRR = 0x8000;

}

else

{

GPIOB->BSRR = 0x80000000;

}

 /* USER CODE END TIM2_IRQn 0 */

 /* USER CODE BEGIN TIM2_IRQn 1 */

 /* USER CODE END TIM2_IRQn 1 */

}

0693W000007D8VpQAK.jpg

1 ACCEPTED SOLUTION

Accepted Solutions

I've just realized that we are talking us not ms. And that means that this simply may be the case of the ISR taking too long to execute/unrealistically high interrupt rate.

JW

View solution in original post

9 REPLIES 9

And how exactly is the RCC set and from what primary source? Read our and post RCC registers content.

Not the problem here, but don't RMW TIMx_SR bits.

> &= ~(1 << 0);

TIM2->SR = ~(1 << 0);

JW

Junior.Hou
Associate II

Hi, Waclawek

Thank you for your reply.

The system clock uses an 8MHz crystal oscillator and the configuration is as follows.

void SystemClock_Config(void)

{

 RCC_OscInitTypeDef RCC_OscInitStruct = {0};

 RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 /** Initializes the RCC Oscillators according to the specified parameters

 * in the RCC_OscInitTypeDef structure.

 */

 RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

 RCC_OscInitStruct.HSEState = RCC_HSE_ON;

 RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;

 RCC_OscInitStruct.HSIState = RCC_HSI_ON;

 RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;

 RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;

 RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;

 if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)

 {

  Error_Handler();

 }

 /** Initializes the CPU, AHB and APB buses clocks

 */

 RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK

               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;

 RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;

 RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;

 RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;

 RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;

 if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)

 {

  Error_Handler();

 }

}

I don't use Cube/HAL and don't quite understand those statements. They appear to do what you intend - system clock 72MHz, APB1 = AHB/2 thus TIM clock = 2x APB1 = 72MHz - but the presented result contradict that.

Output system clock onto MCO pin and check if it is 72MHz as you expect.

Or try to run exactly the same code without using PLL, running at the internal 8MHz RC oscillator, and check the output waveform at that rate - is it 9x slower than with PLL?

Read out and check/post the RCC and TIM registers content.

JW

Piranha
Chief II

Everything is working exactly as it should work. Stop clicking CubeMX, open the reference manual and read what a timer update event actually is.

I've just realized that we are talking us not ms. And that means that this simply may be the case of the ISR taking too long to execute/unrealistically high interrupt rate.

JW

Gudgel.boB
Senior

1 microsecond is pretty fast but, if that is about all you need to do, to toggle a bit, then that should work OK. I don't know how fast this processor is though.

What I would do is to use a TIMER output to do this work. Then you won't need to use the processor time.

I have a situation where I needed to output a semi-variable square wave (or close to 50%) from around 4 MHz to 8 MHz and that worked great.

Hi, Waclawek

Thank you for your reply. I found the gotcha ISR taking too long to execute when you reply 2 days ago.

Hi, Piranha

Thank you for your reply.

Hi, Gudgel

Thank you for your reply.

I use STM32F103CB and work at 72MHz.If use timer interrupt to output a square wave the fastest frequency is almost 1MHz.