2019-04-18 01:47 PM
Hi everyone.
I'm desiging a low power application and clearly the most intuitive way
to reduce power consumption is just to turn off what we don't need.
And here a doubt about the GPIO clock management raised.
AFAICT, __HAL_RCC_GPIO_CLK_ENABLE should be called whenever we use a GPIO pin
of a certain port and, when we're not using that pin anymore, we can just
disable the clock by calling __HAL_RCC_GPIO_CLK_DISABLE.
But, say that I have two modules, an UART module and an ADC module, and each
module should initialize what it needs, which includes the GPIO ports
of the GPIO pin they use.
Now, if I want to turn off one of the two modules, I'd disable the hardware
resource they use too, which means calling __HAL_RCC_GPIO_CLK_DISABLE.
What happens if the UART module and the ADC module are using the same GPIO
ports?
At some point one module will be running with the GPIO port's clock disable,
since an other module that was using the same port has been deinitialized.
I initially thought to implement a reference counted wrapper for GPIO clock
enable/disable,
but before implementing it I wanted to verify that disabling a gpio port's clock in one module
would effectively cause malfunction of another module using the same gpio port.
And well..., it didn't cause any problem. The other module continued to run
smoothly.
Which leads to the question: what do __HAL_RCC_GPIO_CLK_DISABLE actually do?
Furthermore, by reading this thread
(https://community.st.com/s/question/0D50X00009XkhLwSAJ/disabling-gpio-clocks-for-af-io-fails)
, it seems that there is a datasheet in which it's written that the GPIO port
clock should be disabled if the GPIO pins are only used in alternate function
mode, which is exactly my case for the UART and ADC module.
(Ok, that thread was asking why what states the datasheet doesn't work, but anyway...)
But then why do the examples, demos and code generated by STM32CubeMX call
__HAL_RCC_GPIO_CLK_ENABLE/DISABLE in HAL_PP_MspInit functions where
only alternate GPIOs are used?
2019-04-18 03:29 PM
Might not be the best answer, to me the clock enable of GPIO port registers is like TIMER:
To write on the peripheral registers (for example MODER or AF) you will need these registers to have clock for read/write.
Otherwise, registers looks like read only and typically stuck to 0x0000 after reset.
2019-04-18 03:54 PM
By disabling GPIO's clock, GPIO functionality "freezes".
This means all registers preserve its values, also are not readable and return some predefined value such zeroes and can by modified by re-enabling the clock.
If an external pin configured as eg GPIO output PP and set it to HI, after disabling the clock the pin's state don't change.
So , external pins can be used as AF input or Analog modes with GPIO's clock disabled. (just configure AFRL,AFRH,MODER. etc registers appropriately before disable the clock)
2019-04-19 01:18 PM
Now everything makes sense. Thank for your answer!
2019-04-19 02:24 PM
Well,the best way to find out what logic is clocked or not is to simply try it in debug mode manually.... answer will be faster than a post on this forum...