cancel
Showing results for 
Search instead for 
Did you mean: 

Change GPIO direction dynamically

jerome2
Associate II
Posted on July 10, 2014 at 14:39

Hi All

I'm using an STM32L151 in my project.

I have one GPIO configured as an input, and I need to change the direction dynamically. What is the most efficient way to do this ?

Each time I want to change direction, should I:

Call GPIO_Deinit()

Re-populate the structure GPIO_InitStructure according to the new direction

Call GPIO_Init()

Is there a most efficient way to do ?

Thanks

Jerome

#gpio-port #stm32l #gpio #stm32
2 REPLIES 2
Posted on July 10, 2014 at 15:56

Don't use the ''library'' functions, write to GPIO registers (here: GPIOx_MODER, or the bit-band ''mirror'' of the respective bit(s)) directly.

JW

e23
Associate
Posted on July 14, 2014 at 09:25

My goal was to switch between driven high and high-Z, what worked for me was:

  • init the GPIO as output and set to high
then

  • GPIOA->MODER |= (0x0001<<2);

    // make OUTPUT

  • GPIOA->MODER &= ~(0x0003<<2);

    // make INPUT