Author Topic: STM32 HAL getstate usage  (Read 1000 times)

0 Members and 1 Guest are viewing this topic.

Offline VekettiTopic starter

  • Regular Contributor
  • *
  • Posts: 178
  • Country: fi
STM32 HAL getstate usage
« on: November 12, 2023, 06:18:49 pm »
Hi,

I can't find any information on what is the correct way of using "HAL_TIM_Base_GetState()". It doesn't return boolen nor HAL_TIM_STATE_READY. What might be the correct way?

What I'm trying to achieve is, I have timer in interrupt mode which I start and stop in code by "HAL_TIM_Base_Start_IT" or "HAL_TIM_Base_Stop_IT", but I'd need to check whether it is running or stopped first. I could of course set boolean whenever I start it or stop it, but there must be a better way to check this?

Thank you in advance.
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: STM32 HAL getstate usage
« Reply #1 on: November 12, 2023, 11:40:13 pm »
It returns htim->state.
State is HAL_TIM_StateTypeDef type:
Code: [Select]
enum  HAL_TIM_StateTypeDef {
  HAL_TIM_STATE_RESET = 0x00U, HAL_TIM_STATE_READY = 0x01U, HAL_TIM_STATE_BUSY = 0x02U, HAL_TIM_STATE_TIMEOUT = 0x03U,
  HAL_TIM_STATE_ERROR = 0x04U
}

It's meant for internal use.

By default, it's in reset state.
When you init the peripheral, it becomes ready.
Most HAL functions check this, for example some will instantly return error if state is not ready.
Same as if, for example, you start a DMA transfer, the timer handle will be busy until DMA finishes, attempts to use the timer will probably return busy.
When the DMA is over, the handle will be unlocked back to ready.
This is not 100% accurate, it's been some time since I last used HAL.
Navigate the library and see where htim->state is used.
« Last Edit: November 12, 2023, 11:46:17 pm by DavidAlfa »
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 

Offline VekettiTopic starter

  • Regular Contributor
  • *
  • Posts: 178
  • Country: fi
Re: STM32 HAL getstate usage
« Reply #2 on: November 13, 2023, 07:52:38 pm »
Ok, I don't know what did I do wrong as the HAL_TIM_STATE_READY was not recognized. Here's the correct format:
Code: [Select]
if (HAL_TIM_Base_GetState(&htim6) == HAL_TIM_STATE_BUSY )
When timer is in not running:
HAL_TIM_STATE_READY

When timer is running:
HAL_TIM_STATE_BUSY

Just out of curiosity, I see many doesn't use HAL libraries for some reason, makes me wonder what else do you use if not HAL and why?
 

Online DavidAlfa

  • Super Contributor
  • ***
  • Posts: 5912
  • Country: es
Re: STM32 HAL getstate usage
« Reply #3 on: November 13, 2023, 08:00:21 pm »
HAL eats a lot of memory.
If you want to run a timer, you can get it in a few lines in "Low level" code.
HAL will use ram for the peripheral handlers, do lots of checks required for every possible use...
It's OK for starting, also for very fast development, while at low level will need lots of RTFM, debugging, bug fixing...
But the result will way a lot smaller and faster.
For complicated devices like F4/H7 series, well, it's a good option.
A much simpler STM32G0 is a lot easier. I use ST LL libraries for them, providing very compact code.
Hantek DSO2x1x            Drive        FAQ          DON'T BUY HANTEK! (Aka HALF-MADE)
Stm32 Soldering FW      Forum      Github      Donate
 
The following users thanked this post: Veketti

Offline VekettiTopic starter

  • Regular Contributor
  • *
  • Posts: 178
  • Country: fi
Re: STM32 HAL getstate usage
« Reply #4 on: November 14, 2023, 06:42:53 pm »
Oh, it's HAL that's occupying the memory. I was wondering that even though just few lines of code and flash is already 40% full. So for the low end STM32 it is almost a must to not use HAL or otherwise you'll run out of memory very quickly. Well, got also bunch of G070 with 128k memory if ever I'll get in trouble with this G050's 32k memory..
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf