diff options
Diffstat (limited to 'src/machine/machine_rp2040_i2c.go')
-rw-r--r-- | src/machine/machine_rp2040_i2c.go | 19 |
1 files changed, 18 insertions, 1 deletions
diff --git a/src/machine/machine_rp2040_i2c.go b/src/machine/machine_rp2040_i2c.go index d49e7f576..2c0c2e6ed 100644 --- a/src/machine/machine_rp2040_i2c.go +++ b/src/machine/machine_rp2040_i2c.go @@ -57,6 +57,8 @@ var ( ErrInvalidTgtAddr = errors.New("invalid target i2c address not in 0..0x80 or is reserved") ErrI2CGeneric = errors.New("i2c error") ErrRP2040I2CDisable = errors.New("i2c rp2040 peripheral timeout in disable") + errInvalidI2CSDA = errors.New("invalid I2C SDA pin") + errInvalidI2CSCL = errors.New("invalid I2C SCL pin") ) // Tx performs a write and then a read transfer placing the result in @@ -90,7 +92,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error { // SCL: 3, 7, 11, 15, 19, 27 func (i2c *I2C) Configure(config I2CConfig) error { const defaultBaud uint32 = 100_000 // 100kHz standard mode - if config.SCL == 0 { + if config.SCL == 0 && config.SDA == 0 { // If config pins are zero valued or clock pin is invalid then we set default values. switch i2c.Bus { case rp.I2C0: @@ -101,6 +103,21 @@ func (i2c *I2C) Configure(config I2CConfig) error { config.SDA = I2C1_SDA_PIN } } + var okSDA, okSCL bool + switch i2c.Bus { + case rp.I2C0: + okSDA = config.SDA%4 == 0 + okSCL = (config.SCL+1)%4 == 0 + case rp.I2C1: + okSDA = (config.SDA+2)%4 == 0 + okSCL = (config.SCL+3)%4 == 0 + } + + if !okSDA { + return errInvalidI2CSDA + } else if !okSCL { + return errInvalidI2CSCL + } if config.Frequency == 0 { config.Frequency = defaultBaud } |