Skip to main content
dodonny
Associate III
Jan 13, 2021 9:49 AM UTC
Solved

HAL_GPIO_WritePin and HAL_GPIO_ReadPin are not boolean compatible with c++ compiler.

You can interpret return value as boolean (and pass value as boolean for gpio read and write as long as you compile you project in C but as soon as tried to compile in C++ those are not compatibles with boolean but return GPIO_PIN_SET or GPIO_PIN_RESET which is not interpreted as boolean by the compiler.

This is issue is on compilation side or HAL library side ?

0693W000007B3hNQAS.jpg 

NB : Screenshot show error as int and not boolean. And I imported stbool :

#ifndef __cplusplus
 
#define bool	_Bool
#define true	1
#define false	0
#else /* __cplusplus */
 
/* Supporting _Bool in C++ is a GCC extension. */
#define _Bool	bool
 
#if __cplusplus < 201103L
/* Defining these macros in C++98 is a GCC extension. */
#define bool	bool
#define false	false
#define true	true
#endif
 
#endif /* __cplusplus */

NB : partially solved this issue by adding -fpermissive to my flags compiler as suggested by the compiler message.

Thanks for your help.

This topic has been closed for replies.
dodonny
dodonnyBest answer
Associate III
Jan 14, 2021 2:18 PM UTC

Thanks for you explaination. I'm just pointing that this error is not present in C and appear as soon as I compiled my project in C++. So it could be disturbing for some people.

8 replies

TDK
Jan 13, 2021 10:49 PM UTC

The compiler error is saying you can't convert an integer (0) to a type GPIO_PinState. It's not a HAL error, it's a usage error. Booleans don't enter into this at all. You can implicitly/silently convert enums to integers, but you can't implicitly/silently convert integers to enums. This is a restriction of the C language.

HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, 0); // warning, can't implicity convert int to GPIO_PinState
HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, GPIO_PIN_RESET); // works
HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, (GPIO_PinState) 0); // explicit conversion also works

HAL_GPIO_ReadPin returns a value of type GPIO_PinState, so it's compatible with this.

"If you feel a post has answered your question, please click ""Accept as Solution""."
dodonny
dodonnyAuthorBest answer
Associate III
Jan 14, 2021 2:18 PM UTC

Thanks for you explaination. I'm just pointing that this error is not present in C and appear as soon as I compiled my project in C++. So it could be disturbing for some people.

Pavel A.
Jan 14, 2021 10:06 PM UTC

In C++ you can define overloads that accept bool parameters without ugly typecasts:

inline void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, bool onoff)
{
 HAL_GPIO_WritePin(GPIOx, GPIO_Pin, onoff ? GPIO_PIN_SET:GPIO_PIN_RESET);
}
 
inline void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, int onoff)
{
 HAL_GPIO_WritePin(GPIOx, GPIO_Pin, onoff ? GPIO_PIN_SET:GPIO_PIN_RESET);
}
 
/* Unfortunately overloads cannot differ only by return value so no simple overload for "bool HAL_GPIO_ReadPin" */

The ST libraries have been written in C and are usable in C++ with due caution.

Many people are comfortable with plain C (especially when we have C11 and GNU extensions), they could be disturbed if C++ is pushed onto them.

As you have just discovered, C++ is not a strict superset of C. It was once intended to be a strict superset but in reality it was not feasible.

-- pa

dodonny
dodonnyAuthor
Associate III
Jan 15, 2021 6:07 AM UTC

Thanks for your detailed explanation !

dodonny
dodonnyAuthor
Associate III
Jan 18, 2021 2:26 PM UTC

What a beauty :

(GPIO_PinState)!(bool)HAL_GPIO_ReadPin(LED_GREEN_GPIO_Port, LED_GREEN_Pin)

:expressionless_face:

Piranha
Principal III
Jan 20, 2021 2:28 PM UTC

Common sense: Do we really need to invent a new type for a GPIO pins, which are inherently boolean at theoretical and physical level?

The HAL team: Yes, because using bool or integers is too simple.

doddonny
Associate
Feb 16, 2021 6:18 AM UTC

flag

ARose.3
Visitor II
Nov 30, 2021 8:43 PM UTC

@TDK​  gave you already an answere:

HAL_GPIO_WritePin(LED_GREEN_GPIO_Port, LED_GREEN_Pin, (GPIO_PinState) 0); // explicit conversion also works

You can exchange the 0 with your bool or int variable.

(CUBE IDE 1.8.0)