2026-01-21 1:16 PM - last edited on 2026-01-21 1:25 PM by Andrew Neil
It would be helpfull to have couple of function in addition to
HAL_GPIO_WritePin( GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState )
for cases when pin state is known in advance (not result of calculation in runtime)
void HAL_GPIO_SetPin (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BSRR = (uint32_t) GPIO_Pin;
}
void HAL_GPIO_ClearPin (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
{
assert_param(IS_GPIO_PIN(GPIO_Pin));
GPIOx->BRR = (uint32_t) GPIO_Pin;
}Props:
1 One parameter less
2 One assert less
3 No condition check
4 Less typing
5 Less work in runtime
6 Better search. One can serach SetPin or ClearPin instead of all HAL_GPIO_WritePin at first and then see pin state value in common list.
7 Sense of command in one place instead of combination of command_name + parameter_value
8 More uniform with HAL_GPIO_TogglePin. Set, Clear, Toggle have the same signature
Edited to apply source code formatting - please see How to insert source code for future reference.
2026-01-21 1:34 PM
LL_GPIO_SetOutputPin
LL_GPIO_ResetOutputPin
2026-01-21 1:45 PM
HAL Api lack such functions.
2026-01-21 1:50 PM
The HAL routines are doing all of the checks that you'd do if you had enough time and experience. But if you want "quick as possible and forget the checks", then these functions already exist, just use them.
2026-01-21 2:03 PM
As Andrei mentioned, the LL (low Level) API does have these functions. Look in stm32xxxx_ll_gpio.h.
2026-01-21 2:04 PM - edited 2026-01-21 2:34 PM
I don't want forget the checks, I want not to do useless work. To pass extra paramer (pin state), to make two assert instead of one, to make conditional check. Pair of Set/Reset functions do job better, than one WritePin whith extra parameter. No problem to add them to HAL Api. And make HAL api nore uniform with LL api.
2026-01-21 3:10 PM
I think this is a good idea as well. It's such a basic and useful feature and avoids the user having to look up a define for a pin state definition. Except that it be called HAL_GPIO_SetPin and HAL_GPIO_ResetPin to keep terminology consistent.
2026-01-21 3:32 PM
When I see SetPin / ResetPin function I understand it's meaning at once.
When I see WritePin I have to see at parameter value to understand what happens. It is bad API.