-
Notifications
You must be signed in to change notification settings - Fork 2.1k
periph/gpio: add gpio_init_low() / gpio_init_high() helper functions #12612
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
That's probably a bug in that implementation. gpio_init() should not touch the currently configured level. |
I created a feature branch where I changed the behaviour for EFM32 w.r.t. this issue. If that works in #12380 then it should fix this issue for EFM32. |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
@basilfx want to give this a review? |
@benpicco I gave this PR a review and I approve it partially. Since this extends the GPIO API, I don' feel comfortable to ACK for that part. The change does make sense, and it is only a helper function that doesn't affect the rest. Also, I think it needs at least a test that hits these two new functions. |
@benpicco I think I got you covered for a test case: basilfx@173c05f. Feel free to cherry-pick that one. |
I feel strongly that the API should include a way to set the driven level of a GPIO in the same call as setting the mode to output, so I'm in favor of this PR. Only the GPIO peripheral driver knows how to deterministically configure a pin as an output without unintentional momentary assertions of the wrong level. Consumers of the driver do not have a guaranteed way to do that with the current API. Most of them accomplish it by calling In fact I even feel that the API shouldn't include a way to set the mode to output without also defining the driven level in the same call, but of course that's outside the scope of this PR. At a glance, the only thought I had about the specifics of the API is that perhaps it's preferable to have high/low passed as a parameter rather than having two separate functions to call. Then again, perhaps it isn't. Maybe both. Both would be consistent with the fact that we have both |
Is it that bad? |
This actually goes against documentation, periph "handles" are only allowed to be used after they have been init()ed correctly. That's our shortcut to not have to check the validity of e.g., a gpio_t parameter, on every set or clear. |
GPIO pins have to be configured before they can be read / written.
@kaspar030 should we add the contested tag to this one then? |
no, that was just for the workaround mentioned (first I'm not happy with adding and calling these Would an alternative be wrapping the actual implementations? |
I'm trying but I'm struggling to understand which aspect you object to. Are you saying you'd be happier with As a hardware designer I object to an API that permits telling the hardware to become an output without simultaneously telling it which level to output. It's the very definition of undefined behavior. Perhaps we would be better off with separate functions to init GPIOs as input versus output? After all from a hardware perspective it is fundamentally different. Inputs need pull resistors specified whereas outputs need the driven level specified. |
yes, I mean only from an API perspective.
👍 GPIO_OUT could be an alias for GPIO_OUT_LOW. That shouldn't be too hard to fix / confirm in the existing implementations? |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
What was missing here to get this in? |
ping @maribu @benpicco @kaspar030 |
I was objecting the API clutter:
vs.
|
Note that |
This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. If you want me to ignore this issue, please mark it with the "State: don't stale" label. Thank you for your contributions. |
Contribution description
Provide a way to init a GPIO as output with a set level.
Some platforms can to this on init already and will always just set
the GPIO to LOW which can be undesireable.
For all other platforms, provide a fall-back implementation.
The functions are also convenient when de-initializing a previously
otherwise configured Pin, e.g. when disconnecting the power on an external
sensor through a transistor and wanting to avoid current leaking through a
high idle UART TX line.
Testing procedure
The code is trivial, but you can check check that a pin is really HIGH after calling
gpio_init_high(GPIO_PIN(0,1));
Issues/PRs references
Provides a clean solution for #12380