2021-03-12 02:43:17 +08:00
|
|
|
package i2c
|
|
|
|
|
2024-02-05 01:50:43 +08:00
|
|
|
import "gobot.io/x/gobot/v2/drivers/common/bit"
|
|
|
|
|
2021-03-12 02:43:17 +08:00
|
|
|
// PCA9501 supports addresses from 0x00 to 0x7F
|
|
|
|
// 0x00 - 0x3F: GPIO, 0x40 - 0x7F: EEPROM
|
|
|
|
//
|
|
|
|
// 0 EE A5 A4 A3 A2 A1 A0|rd
|
|
|
|
// Lowest bit (rd) is mapped to switch between write(0)/read(1), it is not part of the "real" address.
|
|
|
|
// Highest bit (EE) is mapped to switch between GPIO(0)/EEPROM(1).
|
|
|
|
//
|
|
|
|
// The EEPROM address will be generated from GPIO address in this driver.
|
|
|
|
const pca9501DefaultAddress = 0x3F // this applies, if all 6 address pins left open (have pull up resistors)
|
|
|
|
|
|
|
|
// PCA9501Driver is a Gobot Driver for the PCA9501 8-bit GPIO & 2-kbit EEPROM with 6 address program pins.
|
|
|
|
// 2-kbit EEPROM has 256 byte, means addresses between 0x00-0xFF
|
|
|
|
//
|
2022-11-14 01:47:19 +08:00
|
|
|
// please refer to data sheet: https://www.nxp.com/docs/en/data-sheet/PCA9501.pdf
|
|
|
|
//
|
2021-03-12 02:43:17 +08:00
|
|
|
// PCA9501 is the replacement for PCF8574, so this driver should also work for PCF8574 except EEPROM calls
|
|
|
|
type PCA9501Driver struct {
|
|
|
|
connectionMem Connection
|
|
|
|
*Driver
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewPCA9501Driver creates a new driver with specified i2c interface
|
|
|
|
// Params:
|
2023-10-21 02:50:42 +08:00
|
|
|
//
|
|
|
|
// a Connector - the Adaptor to use with this Driver
|
2021-03-12 02:43:17 +08:00
|
|
|
//
|
|
|
|
// Optional params:
|
|
|
|
//
|
2023-10-21 02:50:42 +08:00
|
|
|
// i2c.WithBus(int): bus to use with this driver
|
|
|
|
// i2c.WithAddress(int): address to use with this driver
|
2021-03-12 02:43:17 +08:00
|
|
|
func NewPCA9501Driver(a Connector, options ...func(Config)) *PCA9501Driver {
|
|
|
|
p := &PCA9501Driver{
|
|
|
|
Driver: NewDriver(a, "PCA9501", pca9501DefaultAddress, options...),
|
|
|
|
}
|
|
|
|
p.afterStart = p.initialize
|
|
|
|
|
|
|
|
// API commands
|
2023-11-16 03:51:52 +08:00
|
|
|
//nolint:forcetypeassert // ok here
|
2021-03-12 02:43:17 +08:00
|
|
|
p.AddCommand("WriteGPIO", func(params map[string]interface{}) interface{} {
|
|
|
|
pin := params["pin"].(uint8)
|
|
|
|
val := params["val"].(uint8)
|
|
|
|
err := p.WriteGPIO(pin, val)
|
|
|
|
return map[string]interface{}{"err": err}
|
|
|
|
})
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
//nolint:forcetypeassert // ok here
|
2021-03-12 02:43:17 +08:00
|
|
|
p.AddCommand("ReadGPIO", func(params map[string]interface{}) interface{} {
|
|
|
|
pin := params["pin"].(uint8)
|
|
|
|
val, err := p.ReadGPIO(pin)
|
|
|
|
return map[string]interface{}{"val": val, "err": err}
|
|
|
|
})
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
//nolint:forcetypeassert // ok here
|
2021-03-12 02:43:17 +08:00
|
|
|
p.AddCommand("WriteEEPROM", func(params map[string]interface{}) interface{} {
|
|
|
|
address := params["address"].(uint8)
|
|
|
|
val := params["val"].(uint8)
|
|
|
|
err := p.WriteEEPROM(address, val)
|
|
|
|
return map[string]interface{}{"err": err}
|
|
|
|
})
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
//nolint:forcetypeassert // ok here
|
2021-03-12 02:43:17 +08:00
|
|
|
p.AddCommand("ReadEEPROM", func(params map[string]interface{}) interface{} {
|
|
|
|
address := params["address"].(uint8)
|
|
|
|
val, err := p.ReadEEPROM(address)
|
|
|
|
return map[string]interface{}{"val": val, "err": err}
|
|
|
|
})
|
|
|
|
return p
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteGPIO writes a value to a gpio pin (0-7)
|
|
|
|
func (p *PCA9501Driver) WriteGPIO(pin uint8, val uint8) error {
|
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
|
|
|
// read current value of CTRL register, 0 is output, 1 is no output
|
|
|
|
iodir, err := p.connection.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// set pin as output by clearing bit
|
2024-02-05 01:50:43 +08:00
|
|
|
iodirVal := bit.Clear(int(iodir), pin)
|
2021-03-12 02:43:17 +08:00
|
|
|
// write CTRL register
|
2024-11-01 19:54:20 +08:00
|
|
|
err = p.connection.WriteByte(uint8(iodirVal)) //nolint:gosec // TODO: fix later
|
2021-03-12 02:43:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// read current value of port
|
|
|
|
cVal, err := p.connection.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
// set or reset the bit in value
|
2024-02-05 01:50:43 +08:00
|
|
|
var nVal int
|
2021-03-12 02:43:17 +08:00
|
|
|
if val == 0 {
|
2024-02-05 01:50:43 +08:00
|
|
|
nVal = bit.Clear(int(cVal), pin)
|
2021-03-12 02:43:17 +08:00
|
|
|
} else {
|
2024-02-05 01:50:43 +08:00
|
|
|
nVal = bit.Set(int(cVal), pin)
|
2021-03-12 02:43:17 +08:00
|
|
|
}
|
|
|
|
// write new value to port
|
2024-11-01 19:54:20 +08:00
|
|
|
err = p.connection.WriteByte(uint8(nVal)) //nolint:gosec // TODO: fix later
|
2021-03-12 02:43:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadGPIO reads a value from a given gpio pin (0-7)
|
|
|
|
func (p *PCA9501Driver) ReadGPIO(pin uint8) (uint8, error) {
|
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
|
|
|
// read current value of CTRL register, 0 is no input, 1 is an input
|
|
|
|
iodir, err := p.connection.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// set pin as input by setting bit
|
2024-02-05 01:50:43 +08:00
|
|
|
iodirVal := bit.Set(int(iodir), pin)
|
2021-03-12 02:43:17 +08:00
|
|
|
// write CTRL register
|
2024-11-01 19:54:20 +08:00
|
|
|
err = p.connection.WriteByte(uint8(iodirVal)) //nolint:gosec // TODO: fix later
|
2021-03-12 02:43:17 +08:00
|
|
|
if err != nil {
|
|
|
|
return 0, err
|
|
|
|
}
|
|
|
|
// read port and create return bit
|
|
|
|
val, err := p.connection.ReadByte()
|
|
|
|
if err != nil {
|
|
|
|
return val, err
|
|
|
|
}
|
2023-10-21 02:50:42 +08:00
|
|
|
val = 1 << pin & val
|
2021-03-12 02:43:17 +08:00
|
|
|
if val > 1 {
|
|
|
|
val = 1
|
|
|
|
}
|
|
|
|
return val, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ReadEEPROM reads a value from a given address (0x00-0xFF)
|
|
|
|
// Note: only this sequence for memory read is supported: "STARTW-DATA1-STARTR-DATA2-STOP"
|
|
|
|
// DATA1: EEPROM address, DATA2: read value
|
|
|
|
func (p *PCA9501Driver) ReadEEPROM(address uint8) (uint8, error) {
|
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
|
|
|
return p.connectionMem.ReadByteData(address)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WriteEEPROM writes a value to a given address in memory (0x00-0xFF)
|
|
|
|
func (p *PCA9501Driver) WriteEEPROM(address uint8, val uint8) error {
|
|
|
|
p.mutex.Lock()
|
|
|
|
defer p.mutex.Unlock()
|
|
|
|
|
|
|
|
return p.connectionMem.WriteByteData(address, val)
|
|
|
|
}
|
|
|
|
|
2023-11-16 03:51:52 +08:00
|
|
|
func (p *PCA9501Driver) initialize() error {
|
2021-03-12 02:43:17 +08:00
|
|
|
// initialize the EEPROM connection
|
2022-12-10 20:10:23 +08:00
|
|
|
bus := p.GetBusOrDefault(p.connector.DefaultI2cBus())
|
2021-03-12 02:43:17 +08:00
|
|
|
addressMem := p.GetAddressOrDefault(pca9501DefaultAddress) | 0x40
|
2023-11-16 03:51:52 +08:00
|
|
|
var err error
|
2022-12-10 20:10:23 +08:00
|
|
|
p.connectionMem, err = p.connector.GetI2cConnection(addressMem, bus)
|
2023-11-16 03:51:52 +08:00
|
|
|
return err
|
2021-03-12 02:43:17 +08:00
|
|
|
}
|