cancel
Showing results for 
Search instead for 
Did you mean: 

HAL_GPIO_WritePin do extra work when pin state is known in avance

vybor
Associate II

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.

7 REPLIES 7

LL_GPIO_SetOutputPin

LL_GPIO_ResetOutputPin

 

HAL Api lack such functions. 

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.

As Andrei mentioned, the LL (low Level) API does have these functions.  Look in stm32xxxx_ll_gpio.h.

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.

TDK
Super User

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.

If you feel a post has answered your question, please click "Accept as Solution".
vybor
Associate II

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.