Skip to content

Commit dda576e

Browse files
aykevldeadprogram
authored andcommitted
avr: add support for PinInputPullup
1 parent da505a6 commit dda576e

File tree

2 files changed

+26
-1
lines changed

2 files changed

+26
-1
lines changed

src/examples/button/button.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const (
1212

1313
func main() {
1414
led.Configure(machine.PinConfig{Mode: machine.PinOutput})
15-
button.Configure(machine.PinConfig{Mode: machine.PinInput})
15+
button.Configure(machine.PinConfig{Mode: machine.PinInputPullup})
1616

1717
for {
1818
if button.Get() {

src/machine/machine_avr.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ type PinMode uint8
1212

1313
const (
1414
PinInput PinMode = iota
15+
PinInputPullup
1516
PinOutput
1617
)
1718

@@ -34,9 +35,33 @@ func (p Pin) Configure(config PinConfig) {
3435
if config.Mode == PinOutput {
3536
// set output bit
3637
ddr.SetBits(mask)
38+
39+
// Note: if the pin was PinInputPullup before, it'll now be high.
40+
// Otherwise it will be low.
3741
} else {
3842
// configure input: clear output bit
3943
ddr.ClearBits(mask)
44+
45+
if config.Mode == PinInput {
46+
// No pullup (floating).
47+
// The transition may be one of the following:
48+
// output high -> input pullup -> input (safe: output high and input pullup are similar)
49+
// output low -> input -> input (safe: no extra transition)
50+
port.ClearBits(mask)
51+
} else {
52+
// Pullup.
53+
// The transition may be one of the following:
54+
// output high -> input pullup -> input pullup (safe: no extra transition)
55+
// output low -> input -> input pullup (possibly problematic)
56+
// For the last transition (output low -> input -> input pullup),
57+
// the transition may be problematic in some cases because there is
58+
// an intermediate floating state (which may cause irratic
59+
// interrupts, for example). If this is a problem, the application
60+
// should set the pin high before configuring it as PinInputPullup.
61+
// We can't do that here because setting it to high as an
62+
// intermediate state may have other problems.
63+
port.SetBits(mask)
64+
}
4065
}
4166
}
4267

0 commit comments

Comments
 (0)