Skip to main content
YAkse
Associate III
July 8, 2019
Solved

Why some HAL declarations uses "__" before the name?

  • July 8, 2019
  • 1 reply
  • 2744 views

I am not sure how to ask but some functions in HAL libraries, just starts with HAL prefix, for example

HAL_GPIO_WritePin() ;

But also some macros start with double underscores for example

__HAL_GPIO_EXTI_CLEAR_IT()

I know that ones are macros and others are functions but is there a reason why they are used this style ?

Thanks.

This topic has been closed for replies.
Best answer by After Forever

I just indicated the existence of the paradigm, but whether ST engineers are using the double underscore for that or something else I can't tell. If the function/macro is documented and is being used in ST-provided examples then I think you can safely use them, underscore or not.

P.S.

From Wikipedia:

An underscore as the first character in an ID is often used to indicate internal implementation that is not considered part of the API and should not be called by code outside that implementation. Python uses this for private member variables of classes, this is common in other languages such as C++ even though those provide keywords to indicate that members are private. It is extensively used to hide variables and functions used for implementations in header files. In fact the use of single underscore for this became so common that C compilers had to standardize on a double leading underscore (for instance __DATE__) for actual built-in variables to avoid conflicts with the ones in header files.

1 reply

After Forever
Senior III
July 8, 2019

It's an old known programming paradigm used to indicate that the variable/macro/function/etc is for private use (used by the library itself, not to be used by the end-user), thus you can't rely on it to not break in the next version.

YAkse
YAkseAuthor
Associate III
July 8, 2019

So at any part of coding, If the library is well-designed, I don't have to use the ones with "__" right? There should be a function doing the same thing ?

Thanks.

Edit : I think what I thought is wrong, because for example I can't find a function which makes the job of

#define __HAL_TIM_GET_COUNTER(__HANDLE__) \

  ((__HANDLE__)->Instance->CNT)

After Forever
After ForeverBest answer
Senior III
July 8, 2019

I just indicated the existence of the paradigm, but whether ST engineers are using the double underscore for that or something else I can't tell. If the function/macro is documented and is being used in ST-provided examples then I think you can safely use them, underscore or not.

P.S.

From Wikipedia:

An underscore as the first character in an ID is often used to indicate internal implementation that is not considered part of the API and should not be called by code outside that implementation. Python uses this for private member variables of classes, this is common in other languages such as C++ even though those provide keywords to indicate that members are private. It is extensively used to hide variables and functions used for implementations in header files. In fact the use of single underscore for this became so common that C compilers had to standardize on a double leading underscore (for instance __DATE__) for actual built-in variables to avoid conflicts with the ones in header files.