Skip to main content
Visitor II
February 26, 2024
Question

Direct I/O access

  • February 26, 2024
  • 4 replies
  • 1342 views

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.

    4 replies

    Super User
    February 26, 2024

    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.

    Graduate II
    February 26, 2024

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

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

     

    Technical Moderator
    February 26, 2024

    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.

    Visitor II
    February 27, 2024

    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)

    Graduate
    February 27, 2024

    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