diff options
author | Patricio Whittingslow <[email protected]> | 2023-02-23 09:26:14 -0300 |
---|---|---|
committer | GitHub <[email protected]> | 2023-02-23 13:26:14 +0100 |
commit | 96b70fd619b9701335a1ae8d827ff1d54bc52330 (patch) | |
tree | 16d6bb380605300a3927ec698cbccb976851e26c /src/machine/machine_rp2040_i2c.go | |
parent | 4d0dfbd6fd607eacfd77460e74eadae2ba73215c (diff) | |
download | tinygo-96b70fd619b9701335a1ae8d827ff1d54bc52330.tar.gz tinygo-96b70fd619b9701335a1ae8d827ff1d54bc52330.zip |
rp2040: provide better errors for invalid pins on i2c and spi (#3443)
machine/rp2040: provide better errors for invalid pins on i2c and spi
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 } |