diff options
author | Ayke van Laethem <[email protected]> | 2020-05-14 23:39:27 +0200 |
---|---|---|
committer | Ron Evans <[email protected]> | 2020-05-22 13:17:04 +0200 |
commit | dda576e80bb4217ba67700e377b441783edee62d (patch) | |
tree | b1451ee578c8b84241471f80ff33a29b454aec17 /src | |
parent | da505a6b170a27826eb3d18d3be41237dc066ca6 (diff) | |
download | tinygo-dda576e80bb4217ba67700e377b441783edee62d.tar.gz tinygo-dda576e80bb4217ba67700e377b441783edee62d.zip |
avr: add support for PinInputPullup
Diffstat (limited to 'src')
-rw-r--r-- | src/examples/button/button.go | 2 | ||||
-rw-r--r-- | src/machine/machine_avr.go | 25 |
2 files changed, 26 insertions, 1 deletions
diff --git a/src/examples/button/button.go b/src/examples/button/button.go index ad028ea0d..698aa7a5d 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 a1c6b069d..f1dbf89d1 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) + } } } |