2021-01-13 01:49 AM
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 ?
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.
Solved! Go to Solution.
2021-01-14 06:18 AM
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.
2021-01-13 02:49 PM
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.
2021-01-14 06:18 AM
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.
2021-01-14 02:06 PM
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
2021-01-14 10:07 PM
Thanks for your detailed explanation !
2021-01-18 06:26 AM
What a beauty :
(GPIO_PinState)!(bool)HAL_GPIO_ReadPin(LED_GREEN_GPIO_Port, LED_GREEN_Pin)
:expressionless_face:
2021-01-20 06:28 AM
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.
2021-02-15 10:18 PM
flag
2021-11-30 12:43 PM
@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)