Skip to main content
Explorer II
October 22, 2021
Solved

HAL bug in GPIO deinit (STM32L0)

  • October 22, 2021
  • 3 replies
  • 2421 views

in software package STM32L0 1.12.1

in file stm32l0xx_hal_gpio.c

in function void HAL_GPIO_DeInit(GPIO_TypeDef *GPIOx, uint32_t GPIO_Pin)

The pin is deinited by setting it as input floating:

/* Configure IO Direction in Input Floting Mode */
GPIOx->MODER |= (GPIO_MODER_MODE0 << (position * 2U));

But on STM32L0x3 all the pins are configured as ANALOG after a reset.

See also the reference manual RM0367 (Section "9.4.1 GPIO port mode register (GPIOx_MODER)").

The code above should be replaced with

/* Configure IO Direction as Analog Mode */
GPIOx->MODER |= (GPIO_MODER_MODE3 << (position * 2U));

I'm assuming that all the devices in the L0 family have pins defaulting to ANALOG; otherwise, the code above should be tailored to the various subfamilies.

    This topic has been closed for replies.
    Best answer by Piranha

    This one is not a bug. Floating mode is the analog mode. The zero in "MODE0" is a pin number, not an I/O mode. The GPIO_MODER_MODE0 is a bit field mask with a value 3. Look at the code:

    https://github.com/STMicroelectronics/STM32CubeL0/blob/00db5bdf2702687f645b65cd800ddc19c6e55878/Drivers/CMSIS/Device/ST/STM32L0xx/Include/stm32l073xx.h#L2560

    P.S. Semantically using a bit field mask doesn't make sense. Instead they should use the reset value, which is just a simple number 3, or define something like GPIO_MODER_MODE_ANALOG or GPIO_MODER_MODE_RESET.

    3 replies

    PiranhaAnswer
    Graduate II
    October 23, 2021

    This one is not a bug. Floating mode is the analog mode. The zero in "MODE0" is a pin number, not an I/O mode. The GPIO_MODER_MODE0 is a bit field mask with a value 3. Look at the code:

    https://github.com/STMicroelectronics/STM32CubeL0/blob/00db5bdf2702687f645b65cd800ddc19c6e55878/Drivers/CMSIS/Device/ST/STM32L0xx/Include/stm32l073xx.h#L2560

    P.S. Semantically using a bit field mask doesn't make sense. Instead they should use the reset value, which is just a simple number 3, or define something like GPIO_MODER_MODE_ANALOG or GPIO_MODER_MODE_RESET.

    SZanoAuthor
    Explorer II
    October 25, 2021

    You're right, I mistook the meaning of MODE0.

    Also the comment about "input floating" - which to me means "input mode without pull-up or pull-down", not "analog" - contributed to the confusion.

    Indeed, other families' files (h7, f4, l4) have an appropriate comment, either "Input Floating" (0x0) or "Analog" (0x3)

    Thanks

    Technical Moderator
    October 25, 2021

    Hi @Community member​ & @Piranha​ ,

    I tracked your proposals in Internal ticket number: 116446 (This is an internal tracking number and is not accessible or usable by customers).

    Thanks for sharing your thoughts.

    -Amel