forked from google/periph
/
mcp23xxx.go
220 lines (183 loc) · 5.57 KB
/
mcp23xxx.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
// Copyright 2020 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package mcp23xxx
import (
"fmt"
"strconv"
"periph.io/x/periph/conn/gpio/gpioreg"
"periph.io/x/periph/conn/i2c"
"periph.io/x/periph/conn/spi"
)
// Dev his a handle for a configured MCP23xxx device.
type Dev struct {
// Pins provide access to extender pins.
Pins [][]Pin
}
// Variant is the type denoting a specific variant of the family.
type Variant string
const (
// MCP23008 8-bit I2C extender. Datasheet: https://www.microchip.com/wwwproducts/en/MCP23008
MCP23008 Variant = "MCP23008"
// MCP23S08 8-bit SPI extender. Datasheet: https://www.microchip.com/wwwproducts/en/MCP23S08
MCP23S08 Variant = "MCP23S08"
// MCP23009 8-bit I2C extender. Datasheet: https://www.microchip.com/wwwproducts/en/MCP23009
MCP23009 Variant = "MCP23009"
// MCP23S09 8-bit SPI extender. Datasheet: https://www.microchip.com/wwwproducts/en/MCP23S09
MCP23S09 Variant = "MCP23S09"
// MCP23016 16-bit I2C extender. Datasheet: https://www.microchip.com/wwwproducts/en/MCP23016
MCP23016 Variant = "MCP23016"
// MCP23017 8-bit I2C extender. Datasheet: https://www.microchip.com/wwwproducts/en/MCP23017
MCP23017 Variant = "MCP23017"
// MCP23S17 8-bit SPI extender. Datasheet: https://www.microchip.com/wwwproducts/en/MCP23S17
MCP23S17 Variant = "MCP23S17"
// MCP23018 8-bit I2C extender. Datasheet: https://www.microchip.com/wwwproducts/en/MCP23018
MCP23018 Variant = "MCP23018"
// MCP23S18 8-bit SPI extender. Datasheet: https://www.microchip.com/wwwproducts/en/MCP23S18
MCP23S18 Variant = "MCP23S18"
)
// NewI2C initializes an IO extender through I2C connection.
func NewI2C(b i2c.Bus, variant Variant, addr uint16) (*Dev, error) {
if addr&0xFFF8 != 0x20 {
return nil, fmt.Errorf("%s: Supported address range is 0x20 - 0x27", variant)
}
devicename := string(variant) + "_" + strconv.FormatInt(int64(addr), 16)
ra := &i2cRegisterAccess{
Dev: &i2c.Dev{Bus: b, Addr: addr},
}
return makeDev(ra, variant, devicename)
}
// NewSPI initializes an IO extender through I2C connection.
func NewSPI(b spi.Conn, variant Variant) (*Dev, error) {
devicename := string(variant)
ra := &spiRegisterAccess{
Conn: b,
}
return makeDev(ra, variant, devicename)
}
// Close removes any registration to the device.
func (d *Dev) Close() error {
for _, port := range d.Pins {
for _, pin := range port {
err := gpioreg.Unregister(pin.Name())
if err != nil {
return err
}
}
}
return nil
}
func makeDev(ra registerAccess, variant Variant, devicename string) (*Dev, error) {
var ports []port
switch variant {
case MCP23008, MCP23009, MCP23S08, MCP23S09:
ports = mcp23x089port(devicename, ra)
case MCP23016:
ports = mcp23x16ports(devicename, ra)
case MCP23017, MCP23S17, MCP23018, MCP23S18:
ports = mcp23x178ports(devicename, ra)
default:
return nil, fmt.Errorf("%s: Unsupported variant", devicename)
}
pins := make([][]Pin, len(ports))
for i := range ports {
// pre-cache iodir
_, err := ports[i].iodir.readValue(false)
if err != nil {
return nil, err
}
pins[i] = ports[i].pins()
for _, pin := range pins[i] {
gpioreg.Register(pin)
}
}
return &Dev{
Pins: pins,
}, nil
}
func mcp23x178ports(devicename string, ra registerAccess) []port {
return []port{{
name: devicename + "_PORTA",
// GPIO basic registers
iodir: ra.define(0x00),
gpio: ra.define(0x12),
olat: ra.define(0x14),
// polarity setting
ipol: ra.define(0x02),
// pull-up control register
gppu: ra.define(0x0C),
supportPullup: true,
// interrupt handling registers
gpinten: ra.define(0x04),
intcon: ra.define(0x08),
intf: ra.define(0x0E),
intcap: ra.define(0x10),
supportInterrupt: true,
}, {
name: devicename + "_PORTB",
// GPIO basic registers
iodir: ra.define(0x01),
gpio: ra.define(0x13),
olat: ra.define(0x15),
// polarity setting
ipol: ra.define(0x03),
supportPullup: true,
// pull-up control register
gppu: ra.define(0x0D),
// interrupt handling registers
gpinten: ra.define(0x05),
intcon: ra.define(0x0B),
intf: ra.define(0x0F),
intcap: ra.define(0x11),
supportInterrupt: true,
}}
}
func mcp23x089port(devicename string, ra registerAccess) []port {
return []port{{
name: devicename,
// GPIO basic registers
iodir: ra.define(0x00),
gpio: ra.define(0x09),
olat: ra.define(0x0A),
// polarity setting
ipol: ra.define(0x01),
// pull-up control register
gppu: ra.define(0x06),
supportPullup: true,
// interrupt handling registers
gpinten: ra.define(0x02),
intcon: ra.define(0x04),
intf: ra.define(0x07),
intcap: ra.define(0x08),
supportInterrupt: true,
}}
}
func mcp23x16ports(devicename string, ra registerAccess) []port {
return []port{{
name: devicename + "_PORT0",
// GPIO basic registers
iodir: ra.define(0x06),
gpio: ra.define(0x00),
olat: ra.define(0x02),
// polarity setting
ipol: ra.define(0x04),
// pull-up control register
supportPullup: false,
// interrupt handling registers
supportInterrupt: false,
intcap: ra.define(0x08),
}, {
name: devicename + "_PORT1",
// GPIO basic registers
iodir: ra.define(0x07),
gpio: ra.define(0x01),
olat: ra.define(0x03),
// polarity setting
ipol: ra.define(0x05),
// pull-up control register
supportPullup: false,
// interrupt handling registers
supportInterrupt: false,
intcap: ra.define(0x09),
}}
}