Skip to main content
Visitor II
Feb 26, 2024 5:43 PM UTC
Question

Direct I/O access

Hello,

I want to ask if there is a way to access the I/O pins directly.

Insread of writing "HAL_GPIO_WritePin(GPIOC, led1_Pin, 1);"   write "led1_Pin=1"

or instead of "if(HAL_GPIO_ReadPin (GPIOC, myinput_Pin))" write " if(myinput_Pin);)

 

Thanks for any ideas.

 

 

This topic has been closed for replies.

5 replies

Pavel A.
Feb 26, 2024 5:53 PM UTC

Not in plain C. This would require C++ where you can define led1_Pin as some kind of object and overload its assignment operator so that it calls HAL_GPIO_WritePin )). Maybe in Rust too.

Tesla DeLorean
Guru
Feb 26, 2024 5:59 PM UTC

GPIOC->BSRR = (1 << 7); // PC7 = HIGH

if (GPIOC->IDR & (1 << 6)) puts("Input PC6 HIGH");

 

Tips, Buy me a coffee, or three.. PayPal Venmo (See Profile) Up vote any posts that you find helpful, it shows what's working..
STTwo-32
ST Technical Moderator
Feb 26, 2024 5:59 PM UTC

Hello @Polizos and welcome to the ST Community :smiling_face_with_smiling_eyes:.

I suggest you to use the direct register access to read, write and configure the GPIO pins   for that you cab take a look at the reference manual of your MCU and refer to the videos 1 and 7 of this playlist.

Best Regards.

STTwo-32.

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
Lead II
Feb 27, 2024 3:58 AM UTC

Just see the source code for "HAL_GPIO_WritePin()" and you get a clue which register (and bits) to use.

BTW: the run-time-overhead of these GPIO functions is not dramatic. See this code:

void HAL_GPIO_WritePin(GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
{
 /* Check the parameters */
 assert_param(IS_GPIO_PIN(GPIO_Pin));
 assert_param(IS_GPIO_PIN_ACTION(PinState));

 if (PinState != GPIO_PIN_RESET)
 {
 GPIOx->BSRR = GPIO_Pin;
 }
 else
 {
 GPIOx->BSRR = (uint32_t)GPIO_Pin << GPIO_NUMBER;
 }
}

How can your code be "way" faster? (it is already an almost "direct" access)

gregstm
Senior II
Feb 27, 2024 4:19 AM UTC

And here's another alternative for writing bits (I like it because it's neat, maintainable and most importantly - efficient)  -

#define RED_LED_ON GPIOB->BSRR = GPIO_BSRR_BS_14

#define RED_LED_OFF GPIOB->BSRR = GPIO_BSRR_BR_14