|
| 1 | +// Copyright 2025 The Periph Authors. All rights reserved. |
| 2 | +// Use of this source code is governed under the Apache License, Version 2.0 |
| 3 | +// that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package mcp23xxx |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "time" |
| 10 | + |
| 11 | + "periph.io/x/conn/v3" |
| 12 | + "periph.io/x/conn/v3/gpio" |
| 13 | + "periph.io/x/conn/v3/pin" |
| 14 | +) |
| 15 | + |
| 16 | +// The internal structure for a group of pins. |
| 17 | +type pinGroup struct { |
| 18 | + dev *Dev |
| 19 | + port int |
| 20 | + pins []*portpin |
| 21 | + defaultMask gpio.GPIOValue |
| 22 | +} |
| 23 | + |
| 24 | +// Group returns a gpio.Group that is made up of the specified pins. |
| 25 | +func (dev *Dev) Group(port int, pins []int) *gpio.Group { |
| 26 | + grouppins := make([]*portpin, len(pins)) |
| 27 | + for ix, number := range pins { |
| 28 | + pp, ok := dev.Pins[port][number].(*portpin) |
| 29 | + if !ok { |
| 30 | + return nil |
| 31 | + } |
| 32 | + grouppins[ix] = pp |
| 33 | + } |
| 34 | + defMask := gpio.GPIOValue((1 << len(pins)) - 1) |
| 35 | + var pgif interface{} = &pinGroup{dev: dev, port: port, pins: grouppins, defaultMask: defMask} |
| 36 | + if gpiogroup, ok := pgif.(gpio.Group); ok { |
| 37 | + return &gpiogroup |
| 38 | + } |
| 39 | + |
| 40 | + return nil |
| 41 | +} |
| 42 | + |
| 43 | +// Pins returns the set of pin.Pin that make up that group. |
| 44 | +func (pg *pinGroup) Pins() []pin.Pin { |
| 45 | + pins := make([]pin.Pin, len(pg.pins)) |
| 46 | + |
| 47 | + for ix, p := range pg.pins { |
| 48 | + pins[ix] = p |
| 49 | + } |
| 50 | + return pins |
| 51 | +} |
| 52 | + |
| 53 | +// Given the offset within the group, return the corresponding GPIO pin. |
| 54 | +func (pg *pinGroup) ByOffset(offset int) pin.Pin { |
| 55 | + return pg.pins[offset] |
| 56 | +} |
| 57 | + |
| 58 | +// Given the specific name of a pin, return it. If it can't be found, nil is |
| 59 | +// returned. |
| 60 | +func (pg *pinGroup) ByName(name string) pin.Pin { |
| 61 | + for _, pin := range pg.pins { |
| 62 | + if pin.Name() == name { |
| 63 | + return pin |
| 64 | + } |
| 65 | + } |
| 66 | + return nil |
| 67 | +} |
| 68 | + |
| 69 | +// Given the GPIO pin number, return that pin from the set. |
| 70 | +func (pg *pinGroup) ByNumber(number int) pin.Pin { |
| 71 | + for _, pin := range pg.pins { |
| 72 | + if pin.Number() == number { |
| 73 | + return pin |
| 74 | + } |
| 75 | + } |
| 76 | + return nil |
| 77 | +} |
| 78 | + |
| 79 | +// Out writes value to the specified pins of the device/port. If mask is 0, |
| 80 | +// the default mask of all pins in the group is used. |
| 81 | +func (pg *pinGroup) Out(value, mask gpio.GPIOValue) error { |
| 82 | + if mask == 0 { |
| 83 | + mask = pg.defaultMask |
| 84 | + } else { |
| 85 | + mask &= pg.defaultMask |
| 86 | + } |
| 87 | + value &= mask |
| 88 | + // Convert the write value which is relative to the pins to the |
| 89 | + // absolute value for the port. |
| 90 | + wr := uint8(0) |
| 91 | + wrMask := uint8(0) |
| 92 | + for bit := range len(pg.pins) { |
| 93 | + if (mask & (1 << bit)) > 0 { |
| 94 | + if (value & 0x01) == 0x01 { |
| 95 | + wr |= 1 << pg.pins[bit].Number() |
| 96 | + } |
| 97 | + wrMask |= 1 << pg.pins[bit].Number() |
| 98 | + } |
| 99 | + value = value >> 1 |
| 100 | + } |
| 101 | + port := pg.pins[0].port |
| 102 | + // Verify pins are set for output |
| 103 | + outputPins, err := port.iodir.readValue(true) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + if ((outputPins ^ 0xff) & wrMask) != wrMask { |
| 109 | + outputPins &= (wrMask ^ 0xff) |
| 110 | + err = port.iodir.writeValue(outputPins, false) |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + } |
| 115 | + |
| 116 | + // Read the current value |
| 117 | + currentValue, err := port.olat.readValue(true) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + // Apply the mask to clear bits we're writing. |
| 122 | + currentValue &= (0xff ^ wrMask) |
| 123 | + // Or the value with the bits to modify |
| 124 | + currentValue |= wr |
| 125 | + // And, write the value out the port. |
| 126 | + return port.olat.writeValue(currentValue, true) |
| 127 | +} |
| 128 | + |
| 129 | +// Read reads from the device and port and returns the state of the GPIO |
| 130 | +// pins in the group. If a pin specified by mask is not configured for |
| 131 | +// input, it is transparently re-configured. |
| 132 | +func (pg *pinGroup) Read(mask gpio.GPIOValue) (result gpio.GPIOValue, err error) { |
| 133 | + if mask == 0 { |
| 134 | + mask = pg.defaultMask |
| 135 | + } else { |
| 136 | + mask &= pg.defaultMask |
| 137 | + } |
| 138 | + // Compute the read mask |
| 139 | + rmask := uint8(0) |
| 140 | + for bit := range 8 { |
| 141 | + if (mask & (1 << bit)) > 0 { |
| 142 | + rmask |= (1 << pg.pins[bit].Number()) |
| 143 | + } |
| 144 | + } |
| 145 | + // Make sure the direction for the pins involved in this write read is |
| 146 | + // Input. |
| 147 | + port := pg.pins[0].port |
| 148 | + currentIn, err := port.iodir.readValue(true) |
| 149 | + if err != nil { |
| 150 | + return |
| 151 | + } |
| 152 | + // We need to make some pins Input. Write the value to the iodir register. |
| 153 | + if (currentIn & rmask) != rmask { |
| 154 | + err = port.iodir.writeValue(currentIn|rmask, false) |
| 155 | + if err != nil { |
| 156 | + return |
| 157 | + } |
| 158 | + } |
| 159 | + // Now, perform the read itself. |
| 160 | + v, err := port.gpio.readValue(false) |
| 161 | + if err != nil { |
| 162 | + return |
| 163 | + } |
| 164 | + // Now convert the set pins into the Group value |
| 165 | + for ix, pin := range pg.pins { |
| 166 | + if (v & (1 << pin.Number())) > 0 { |
| 167 | + result |= 1 << ix |
| 168 | + } |
| 169 | + } |
| 170 | + return |
| 171 | +} |
| 172 | + |
| 173 | +// WaitForEdge listens for a GPIO pin change event. The MCP23XXXX devices |
| 174 | +// can't directly signal an edge event. To do this, you must call |
| 175 | +// Dev.SetEdgePin() with a HOST GPIO pin configured for falling edge |
| 176 | +// detection. That pin should be connected to the MCP23XXX INT pin. When |
| 177 | +// a falling edge is detected on the supplied host GPIO pin, the code |
| 178 | +// will return the GPIO Pin number on the device that changed. |
| 179 | +// |
| 180 | +// Note that the MCP23XXX devices only detect change. You can't configure |
| 181 | +// falling or rising edge. Consequently, the returned edge will always be |
| 182 | +// gpio.NoEdge. |
| 183 | +// |
| 184 | +// For a change event to be detected, the pin must be configured for input. |
| 185 | +// This function will NOT set pins for input. Additionally, the calling |
| 186 | +// code must set the INTCON register appropriately. Refer to the datasheet. |
| 187 | +// |
| 188 | +// In the event that the changed pin is NOT part of the io group, the |
| 189 | +// triggering pin number will be returned, along with the error |
| 190 | +// ErrPinNotInGroup |
| 191 | +func (pg *pinGroup) WaitForEdge(timeout time.Duration) (number int, edge gpio.Edge, err error) { |
| 192 | + return -1, gpio.NoEdge, gpio.ErrGroupFeatureNotImplemented |
| 193 | +} |
| 194 | + |
| 195 | +// Halt() interrupts a pending WaitForEdge() call if one is in process. |
| 196 | +func (pg *pinGroup) Halt() error { |
| 197 | + if pg.dev.edgePin != nil { |
| 198 | + var ifpin interface{} = pg.dev.edgePin |
| 199 | + if r, ok := ifpin.(conn.Resource); ok { |
| 200 | + return r.Halt() |
| 201 | + } |
| 202 | + } |
| 203 | + // TODO: I think we want to call Dev.Halt() |
| 204 | + return nil |
| 205 | +} |
| 206 | + |
| 207 | +// String returns the device variant name and configured pins for the group. |
| 208 | +func (pg *pinGroup) String() string { |
| 209 | + s := fmt.Sprintf("%s - [ ", pg.dev) |
| 210 | + for ix := range len(pg.pins) { |
| 211 | + s += fmt.Sprintf("%d ", pg.pins[ix].Number()) |
| 212 | + } |
| 213 | + s += "]" |
| 214 | + return s |
| 215 | +} |
| 216 | + |
| 217 | +var _ gpio.Group = &pinGroup{} |
0 commit comments