cancel
Showing results for 
Search instead for 
Did you mean: 

Direct I/O access

Polizos
Associate

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.

 

 

5 REPLIES 5
Pavel A.
Evangelist III

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.

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

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

 

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..
STTwo-32
ST Employee

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.

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 III

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