cancel
Showing results for 
Search instead for 
Did you mean: 

GPIO Hal remap

Posted on December 22, 2016 at 14:14

Hi. I'm trying to remap a gpio pin from out to it but the following lines of code don't work.

Can someone help me?

I need to remap GPIOx, GPIO_Pin from out to in on the fly.

void DigitalInOut::SwitchToInput() {

    HAL_GPIO_DeInit(GPIOx, GPIO_Pin);

    GPIO_InitTypeDef GPIO_InitStruct;

    GPIO_InitStruct.Pin = GPIO_Pin;

    GPIO_InitStruct.Mode = GPIO_MODE_INPUT;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);

}

void DigitalInOut::SwitchToOutput() {

    HAL_GPIO_DeInit(GPIOx, GPIO_Pin);

    GPIO_InitTypeDef GPIO_InitStruct;

    GPIO_InitStruct.Pin = GPIO_Pin;

    GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;

    GPIO_InitStruct.Pull = GPIO_NOPULL;

    GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;

    HAL_GPIO_Init(GPIOx, &GPIO_InitStruct);

}
6 REPLIES 6
Khouloud GARSI
Lead II
Posted on December 23, 2016 at 11:56

Hi Denis,

Your issue is not clear. Could you please give more details: What STM32 product are you using? what do you want to do exactly? where have you found an issue?

Regards,

Khouloud.

Posted on December 23, 2016 at 12:42

Using CubeMX (hal) I have defined a pin as digital output to manage a lcd 2x16 display. OI need to convert it to digital input and then to digital output again. Is there a way? Can someone share an example?

Posted on December 23, 2016 at 14:32

Hello Denis, at the first sight your code should work. The one thing which I would recommend to check is that clock for the corresponding GPIO port is enabled. You can do it in debug session by checking the RCC registers. You can check there also the GPIO registers (for instance GPIOx_MODER for mode).   

Posted on December 24, 2016 at 00:14

How do you know that that code does not work?

Posted on January 03, 2017 at 09:23

Solved by increasing the stack size.

Khouloud GARSI
Lead II
Posted on January 03, 2017 at 13:22

Hi Denis,

Glad that your issue is solved

And thanks for sharing the solution, this may be helpful for other users.

Khouloud.