-
Notifications
You must be signed in to change notification settings - Fork 201
Description
The first code example in this section:
https://docs.rust-embedded.org/book/static-guarantees/design-contracts.html
shows that direction and input/output mode can only be configured AFTER enabling a GPIO. But it should be the other way around.
You always want to configure the direction first. Imagine enabling a GPIO and for a split seconds it is configured as an output GPIO even though it is supposed to be an input GPIO. You could short your circuit and therefore damage your hardware. Enabling the GPIO should almost always be your last operation on a GPIO to avoid damage or glitches.
In case of output you usually want to:
- configure output value (high/low)
- configure direction
- enable the pin
Arguably 1 and 2 can be switched out since the enable bit will prevent any glitches that come from setting the direction before the output value.
Of course you don't have any of these problems if you simply add function configures the register in one go. For example create functions like configure_output(high_low: bool, enable: bool) and configure_input(mode: "u2", enable: bool).
In any case, I think the example gives a dangerous recommendation.