cancel
Showing results for 
Search instead for 
Did you mean: 

How to write a variable value to a GPIO port?

Posted on January 21, 2018 at 05:28

Hello to all Forum members. I would like to know how can I write a variable (e.g. of 16 bits) directly to any GPIO port using the HAL API provided by STM32Cube Classic HAL.

Currently I'm using Keil uVision. I've created a new project and selected STM32F429ZI device (NUCLEO-F429ZI Board). In the Software components dialog I've selected Device -> STM32Cube Framework (API) -> Classic and inside STM32Cube HAL the sub-components Common, Cortex, GPIO, PWR and RCC. Everything compiles without problems but I'm unable to find a function to write a variable to a port. Previously with Peripheral Libraries I just use the form

uint16_t myVariable = 0xABCD;

GPIO_Write(GPIOB, myVariable);

and everything is ok, but with Cube HAL I cannot found anything similar.

I have used the function HAL_GPIO_WritePin(...) to set/clear groups of bits, but it does not fits my needs in this case.

Any ideas on what to do?

Thanks in advance. Yosmany.

#hal-gpio
1 ACCEPTED SOLUTION

Accepted Solutions
Posted on January 21, 2018 at 14:09

Hardware Abstraction means that a subset of hardware's functionality is chosen and implemented in functions and structures of the Hardware Abstraction Layer. It means, you give up exact control over the functionality in exchange for promise of portability.

Here, writing to port is a functionality which has not been chosen as part of the Hardware Abstraction. It means, you either circumvent this limitation while maintaining the Abstraction (e.g. by maintaining a local copy of the port and doing HAL_GPIO_TogglePin(GPIOx, local_copy ^ new_value); local_copy = new_value); or go for either normal programming as Mario said above or or using LL_GPIO_WriteOutputPort() - both resulting in breaking the Abstraction and potentially making your program not portable by the HAL means.

JW

View solution in original post

4 REPLIES 4
Mario Popovic
Associate III
Posted on January 21, 2018 at 11:55

Maybe not the cleanest way, but you can allways do: GPIOB->ODR = myVariable;

Posted on January 21, 2018 at 14:09

Hardware Abstraction means that a subset of hardware's functionality is chosen and implemented in functions and structures of the Hardware Abstraction Layer. It means, you give up exact control over the functionality in exchange for promise of portability.

Here, writing to port is a functionality which has not been chosen as part of the Hardware Abstraction. It means, you either circumvent this limitation while maintaining the Abstraction (e.g. by maintaining a local copy of the port and doing HAL_GPIO_TogglePin(GPIOx, local_copy ^ new_value); local_copy = new_value); or go for either normal programming as Mario said above or or using LL_GPIO_WriteOutputPort() - both resulting in breaking the Abstraction and potentially making your program not portable by the HAL means.

JW

Posted on January 21, 2018 at 16:36

Hello, thanks to Mario and JW. I assumed it would be this way, I

was just trying to know if there was any other way within the HAL API, to keep 'inside' the abstraction level, but as you said, there´s no way at this moment. Thank you very much for your reply. Yosmany.

John Craven
Senior
Posted on January 21, 2018 at 18:49

Since ST seems to be very poor about reading this type of post and jumping to add this obvious usage to HAL.

You should post new thread specifically as suggestion to add new HAL functions 

HAL_GPIO_WritePort, HAL_GPIO_ReadPort, HAL_GPIO_TogglePort, etc