2024-02-26 09:43 AM
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.
2024-02-26 09:53 AM - edited 2024-02-26 09:54 AM
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.
2024-02-26 09:59 AM - edited 2024-02-26 10:00 AM
GPIOC->BSRR = (1 << 7); // PC7 = HIGH
if (GPIOC->IDR & (1 << 6)) puts("Input PC6 HIGH");
2024-02-26 09:59 AM
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.
2024-02-26 07:58 PM
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)
2024-02-26 08:19 PM
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