mirror of
https://github.com/tinygo-org/tinygo.git
synced 2026-08-18 11:33:59 +00:00
rp2040: provide better errors for invalid pins on i2c and spi (#3443)
machine/rp2040: provide better errors for invalid pins on i2c and spi
This commit is contained in:
committed by
GitHub
parent
4d0dfbd6fd
commit
96b70fd619
@@ -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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user