From dda576e80bb4217ba67700e377b441783edee62d Mon Sep 17 00:00:00 2001 From: Ayke van Laethem Date: Thu, 14 May 2020 23:39:27 +0200 Subject: [PATCH] avr: add support for PinInputPullup --- src/examples/button/button.go | 2 +- src/machine/machine_avr.go | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+), 1 deletion(-) diff --git a/src/examples/button/button.go b/src/examples/button/button.go index ad028ea0..698aa7a5 100644 --- a/src/examples/button/button.go +++ b/src/examples/button/button.go @@ -12,7 +12,7 @@ const ( func main() { led.Configure(machine.PinConfig{Mode: machine.PinOutput}) - button.Configure(machine.PinConfig{Mode: machine.PinInput}) + button.Configure(machine.PinConfig{Mode: machine.PinInputPullup}) for { if button.Get() { diff --git a/src/machine/machine_avr.go b/src/machine/machine_avr.go index a1c6b069..f1dbf89d 100644 --- a/src/machine/machine_avr.go +++ b/src/machine/machine_avr.go @@ -12,6 +12,7 @@ type PinMode uint8 const ( PinInput PinMode = iota + PinInputPullup PinOutput ) @@ -34,9 +35,33 @@ func (p Pin) Configure(config PinConfig) { if config.Mode == PinOutput { // set output bit ddr.SetBits(mask) + + // Note: if the pin was PinInputPullup before, it'll now be high. + // Otherwise it will be low. } else { // configure input: clear output bit ddr.ClearBits(mask) + + if config.Mode == PinInput { + // No pullup (floating). + // The transition may be one of the following: + // output high -> input pullup -> input (safe: output high and input pullup are similar) + // output low -> input -> input (safe: no extra transition) + port.ClearBits(mask) + } else { + // Pullup. + // The transition may be one of the following: + // output high -> input pullup -> input pullup (safe: no extra transition) + // output low -> input -> input pullup (possibly problematic) + // For the last transition (output low -> input -> input pullup), + // the transition may be problematic in some cases because there is + // an intermediate floating state (which may cause irratic + // interrupts, for example). If this is a problem, the application + // should set the pin high before configuring it as PinInputPullup. + // We can't do that here because setting it to high as an + // intermediate state may have other problems. + port.SetBits(mask) + } } }