2018-07-06 03:24 AM
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.
2018-07-06 03:41 AM
'I can go down to 5 usec but not to 0.5 usec
(htim2.Init.Period = 35;).'what does that even mean?
2018-07-06 04:55 AM
Here are some results:
MikroeC , 5 usec
Atollic + HAL , 5 usec (IMO far from perfect)
MikroeC , 0.5 usec (not perfect!)
Regards,
Radu G.
2018-07-06 06:09 AM
I can toggle the port pin (
GPIOC GPIO_PIN_9) every 5 usec. but not every 0.5 usec.
2018-07-06 07:15 AM
think about what's involved with an interrupt invocation (aka interrupt overhead) and you will know.
2018-07-06 07:22 AM
'
Atollic + HAL , 5 usec (IMO far from perfect)'
your original statement about having to program the registers directly in MikroC answered that perfectly.
2018-07-06 07:32 AM
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.
2018-07-06 09:17 AM
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.