Skip to main content
Senior III
May 11, 2023
Question

Why HAL_GPIO_Init returns void instead of HAL_StatusTypeDef ?

  • May 11, 2023
  • 9 replies
  • 1430 views

I notice that almost all peripherials return HAL_StatusTypeDef but GPIO not. Why?

This topic has been closed for replies.

9 replies

KnarfB
Super User
May 11, 2023

Look at its implementation, its in the end only register modifications. Not much that could fail. If this is bothering you, change the source code to your needs.

hth

KnarfB

Senior III
May 11, 2023

I don't agree. Where is the null checking? It is enough reason. We pass two pointers as parameters. It is enough to use hal status. Moreover GPIO.c uses hal status in tiny function

Dudo
Tesla DeLorean
Guru
May 11, 2023

Generally error propagation and handling is poor.

Most entirely ignore errors.

Carry them briefly and then give up carrying the forward or recovering.

The other approach is to just crash and die, by failing into an Error_Handler() often one not even reporting the file/line information making tracking it down a pain.

ST prefers to use assert()'s to selectively do parameter and field checking.

They also have a lot of inconsistency in clearing auto/local variable structures, with multiple fields and selective usage, and subsequent ORing or compounding value can lead to latent errors related to stack junk and state.

Often things are global that could be local or of limited/finite scope. Globals typically being implicitly cleared by startup.s code.

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Senior III
May 11, 2023

@Community member​  what do you think based on your experience?​

Dudo
Tesla DeLorean
Guru
May 11, 2023

Likely due to the mundane functionality, and that there are no loops, or status / interlocks on configuring the port.

I suppose it could fail based on parameter issues / errors, but ST's mostly addressed that via assert()'s

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
Senior III
May 11, 2023

Yes but a lot of modules first do check null and them use asserts. I think it's not consequent.

Dudo
LCE
Principal II
May 12, 2023

Well, then write your own!

Which I fully understand.

As most GPIO settings are performed before main loop, it wouldn't even hurt (time-wise) to write a function around HAL_GPIO_Init(), checking at least the pointers.

Senior III
May 12, 2023

Yes of course. I only take care of high quality of HAL/LL code :) that's all :)

Dudo
Senior III
May 17, 2023

Ok but what do you think about such use case.

When I use lock mechanism on GPIO pin, I trying deinitialize it and don't get information about fail but set ting registers is blocked until Reset MCU or peripheral reset. Them i trying Initialize (resue) pin and also I haven't information about fail......

Dudo