The HAL function to read the current output status of a GPIO
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-12 11:24 PM
If I configure one GPIO pin to output. Is there a function in HAL to read back the current output status of it?
Solved! Go to Solution.
Accepted Solutions
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-13 9:20 AM
You can use HAL_GPIO_ReadPin().
Even if the pin is set to output the IDR register will give you its current output status.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2017-09-13 9:20 AM
You can use HAL_GPIO_ReadPin().
Even if the pin is set to output the IDR register will give you its current output status.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-01-16 9:14 PM
It is not true. If a pin is configured as open-drain output and its ODR bit value is 1, it may be pulled down externally. Then its IDR bit is 0, but ODR bit still is 1. So, HAL_GPIO_ReadPin will return 0 while output state is 1.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-01-16 11:45 PM
Well spotted, @hdmi87​ , even if this is a rather old thread, it's always good to correct a mistake.
It appears that there's no function in Cube/HAL to read GPIO_ODR, it appears that the pin-toggling function is the only one which mentions ODR at all.
OTOH, reading back ODR has a relatively rare usage. Cube/HAL, as inevitably every "library" does, implements only a subset of possible functionality, and of course that subset is based on the "most often used" functionality. I'd leave this to simply reading the register.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2020-02-12 1:47 PM
Hi,
​
Thanks for your attention to such an old question from me.
​
Yes. Though it is wired, in a project I had to do this​. So, if I'm sure the output pin is not configured as OD, the method David proposed should work, right?
​
Best​ regards.
​
Diverger​
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-19 1:17 PM
If you just want to read back what you wrote to the pin, you can check whether GPIOx->BSRR is GPIO_PIN_RESET or GPIO_PIN_SET.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-20 2:07 AM
Have you actually tried this?
GPIOx_BSRR are write-only and read-as-zero.
To find out the state of output register, read, well, the output register, i.e. GPIOx_ODR.
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-20 2:13 AM
I wouldn't answer a 4 year old question with a mind experiment so yes, I've tried it and it works.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-20 2:30 AM
On which STM32 and how exactly?
JW
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Email to a Friend
- Report Inappropriate Content
‎2022-08-28 2:02 AM
As the BSRR is a write only register, even if it does read something, it is not guaranteed to be correct. And there ir no sense in doing it anyway, because it doesn't have any advantage over reading the ODR register.
