stm32-rs / stm32g0xx-hal

Peripheral access API for STM32G0 series microcontrollers
Apache License 2.0
73 stars 51 forks source link

Allow output value to be set before configuring pin as an output #93

Closed davidlattimore closed 2 years ago

davidlattimore commented 2 years ago

Somewhat related to #87 (but more general)....

Sometimes you have a pin that has an external pull-up and you'd like to configure the pin as an output (generally an open-drain output) without momentarily pulling the pin low. I'd like it if we could have an API that allowed this. Possibilities that come to mind:

  1. Implement OutputPin for Analog. Downside would be that users might call set_low/high and expect an immediate effect.
  2. Implement some other method on pins that does the same thing as set_low/high, but makes the intent clearer. e.g. preset_low / preset_high.
  3. Add an argument to into_open_drain to specify the initial state. But this would be a breaking change.
  4. Add a new API for creating an OpenDrain output that allowed specifying the initial value and make into_open_drain just be a shortcut for this.

Thoughts?

andresv commented 2 years ago

Do you happen to know if something like this is available in some other HAL?

davidlattimore commented 2 years ago

Good idea. Looks like stm32f4xx-hal and stm32l4xx-hal both implement into_open_drain_output_in_state, which takes an embedded_hal::digital::v2::PinState.

The other crates I looked at had no obvious support: stm32h7xx-hal stm32f3xx-hal stm32g4xx-hal stm32l1xx-hal stm32f0xx-hal

andresv commented 2 years ago

Then it makes sense to do it the same way. Can you make a PR.

@dotcypress would you be happy with such change?

dotcypress commented 2 years ago

Yes, we definitely need to fix that. Let me think, probably provided solution is fine.