Skip to main content
Cpatt.1
Associate II
January 30, 2021
Question

Hi, can someone suggest me how to configure GPIO ports of stm32f407 MUC as bidirectional IO

  • January 30, 2021
  • 3 replies
  • 802 views

..

This topic has been closed for replies.

3 replies

KnarfB
Super User
January 30, 2021

At register level, there is an IDR (input data register) and ODR (output data register). They both work independently, i.e. you can read back the IO value with IDR while in output "mode".

This works with HAL as well:

So you might setup a GPIO pin as open-drain output with a pull-up in the chip configuration or code:

 /*Configure GPIO pin : PA0 */
 GPIO_InitStruct.Pin = GPIO_PIN_0;
 GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_OD;
 GPIO_InitStruct.Pull = GPIO_PULLUP;
 GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
 HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);

and read back the current pin level using

GPIO_PinState val = HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0);

and change the output level using

HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0, val );

as you like.

hth

KnarfB

waclawek.jan
Super User
January 30, 2021

If Open Drain is not an option because of its inherent limitations, you can of course change the "direction" of given pin simply by changing its respective GPIO_MODER field.

I don't think this is something easily clickable in CubeMX, though.

JW

Piranha
Principal III
January 30, 2021

AN4899 nicely explains GPIO in details.