cancel
Showing results for 
Search instead for 
Did you mean: 

STM32F103, HAL and timer(s)

RaduG
Associate II
Posted on July 06, 2018 at 12:24

I created a new project (using CubeMx & Atollic TrueStudio) for STM32F103RB and tried to toggle a bit as quick as possible; I use TIMER2 and this function to initialize it:

=======================

/* TIM2 init function */

static void MX_TIM2_Init(void)

{

   TIM_ClockConfigTypeDef sClockSourceConfig;

   TIM_MasterConfigTypeDef sMasterConfig;

   htim2.Instance = TIM2;

   htim2.Init.Prescaler = 0;

   htim2.Init.CounterMode = TIM_COUNTERMODE_UP;

   //htim2.Init.Period = 35; //72,000,000 / 36 => 0.5 usec

   htim2.Init.Period = 35999; //72,000,000 / 36,000 => 0.5 msec

   htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;

   htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;

   if (HAL_TIM_Base_Init(&htim2) != HAL_OK)

   {

      _Error_Handler(__FILE__, __LINE__);

   }

   sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;

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

   {

      _Error_Handler(__FILE__, __LINE__);

   }   

   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;

   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;

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

   {

   _Error_Handler(__FILE__, __LINE__);

   }

}

=======================

 

The ISR for TIMER2 looks like this:

=======================

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

   if (htim->Instance == TIM2)

   {

      HAL_GPIO_TogglePin(GPIOC, GPIO_PIN_9);

   }

}

=======================

The clocks & GPIO are correctly initialized (IMO).

With this configuration I can go down to 5 usec but not to 0.5 usec (htim2.Init.Period = 35;).

What am I doing wrong?

I also have to mention that I can successfully generate an interrupt every 0.5 usec (with TIMER2) with a different compiler (MikroeC) with the same hardware and project settings. The only difference is that with MikroeC one has to program the registers (very low) because there is no HAL available for their compilers.

Is this because of the HAL implementation or am I missing something important?

Regards,

Radu G.  

7 REPLIES 7
henry.dick
Senior II
Posted on July 06, 2018 at 12:41

'I can go down to 5 usec but not to 0.5 usec

(htim2.Init.Period = 35;).'

what does that even mean?

RaduG
Associate II
Posted on July 06, 2018 at 13:55

Here are some results:

MikroeC , 5 usec 

0690X0000060LfMQAU.png

Atollic + HAL , 5 usec (IMO far from perfect)

0690X0000060LVFQA2.png

MikroeC , 0.5 usec (not perfect!)

0690X0000060LfqQAE.png

Regards,

Radu G.

Posted on July 06, 2018 at 13:09

I can toggle the port pin (

GPIOC GPIO_PIN_9) every 5 usec. but not every 0.5 usec.

Posted on July 06, 2018 at 14:15

think about what's involved with an interrupt invocation (aka interrupt overhead) and you will know.

Posted on July 06, 2018 at 14:22

'

Atollic + HAL , 5 usec (IMO far from perfect)'

your original statement about having to program the registers directly in MikroC answered that perfectly.

Posted on July 06, 2018 at 14:32

Yeah, i suspected that but, as I am by no means, an HAL expert, I had to ask.

MikroeC has its own quirks (buggy IDE, closed-source libraries, etc.) but it seems to generate more compact code.

Thank you for taking the time to answer my question!

Regards,

Radu G. 

Posted on July 06, 2018 at 16:17

HAL isn't the champion of code (space/time) efficiency, for sure.

ISR inefficiencies can be a problem if you are looking for high speed. other solutions exist, depending on what you are trying to do.