Skip to content

Commit edf9ba9

Browse files
rogpeppedeadprogram
authored andcommitted
mcp23017: new driver for MCP23017 (I2C port expander)
1 parent c1c05cb commit edf9ba9

File tree

7 files changed

+1029
-0
lines changed

7 files changed

+1029
-0
lines changed

Makefile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,8 @@ smoke-test:
8383
@md5sum ./build/test.hex
8484
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mag3110/main.go
8585
@md5sum ./build/test.hex
86+
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp23017/main.go
87+
@md5sum ./build/test.hex
8688
tinygo build -size short -o ./build/test.hex -target=itsybitsy-m0 ./examples/mcp3008/main.go
8789
@md5sum ./build/test.hex
8890
tinygo build -size short -o ./build/test.hex -target=microbit ./examples/microbitmatrix/main.go

examples/mcp23017/main.go

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package main
2+
3+
import (
4+
"machine"
5+
6+
"tinygo.org/x/drivers/mcp23017"
7+
)
8+
9+
func main() {
10+
err := machine.I2C0.Configure(machine.I2CConfig{
11+
Frequency: machine.TWI_FREQ_400KHZ,
12+
})
13+
if err != nil {
14+
panic(err)
15+
}
16+
dev, err := mcp23017.NewI2C(machine.I2C0, 0x20)
17+
if err != nil {
18+
panic(err)
19+
}
20+
// Configure pin 0 for input and all the others for output.
21+
if err := dev.SetModes([]mcp23017.PinMode{
22+
mcp23017.Input | mcp23017.Pullup,
23+
mcp23017.Output,
24+
}); err != nil {
25+
panic(err)
26+
}
27+
input := dev.Pin(0)
28+
outputMask := ^mcp23017.Pins(1 << 0) // All except pin 0
29+
inputVal, err := input.Get()
30+
if err != nil {
31+
panic(err)
32+
}
33+
println("input value: ", inputVal)
34+
// Set the values of all the output pins.
35+
err = dev.SetPins(0b1011011_01101110, outputMask)
36+
if err != nil {
37+
panic(err)
38+
}
39+
}

0 commit comments

Comments
 (0)