machine/rp2: unexport machine-specific errors

Errors are part of API, and the exported rp2 errors seemed arbitrary.
For example, the very particular ErrRP2040I2CDisable was exported, but
errI2CWriteTimeout (which is defined on all platforms) is not.

While here, remove "RP2040" from an error name and make the messages
consistent and idiomatic.
This commit is contained in:
Elias Naur
2025-05-03 11:41:07 +02:00
committed by Ron Evans
parent d840971c42
commit 2da9d26e21
+19 -19
View File
@@ -58,15 +58,15 @@ type I2C struct {
} }
var ( var (
ErrInvalidI2CBaudrate = errors.New("invalid i2c baudrate") errInvalidI2CBaudrate = errors.New("i2c: invalid baudrate")
ErrInvalidTgtAddr = errors.New("invalid target i2c address not in 0..0x80 or is reserved") errInvalidTgtAddr = errors.New("i2c: invalid target address: not in 0..0x80 or is reserved")
ErrI2CGeneric = errors.New("i2c error") errI2CGeneric = errors.New("i2c: generic error")
ErrRP2040I2CDisable = errors.New("i2c rp2040 peripheral timeout in disable") errI2CDisable = errors.New("i2c: peripheral timeout in disable")
errInvalidI2CSDA = errors.New("invalid I2C SDA pin") errInvalidI2CSDA = errors.New("i2c: invalid SDA pin")
errInvalidI2CSCL = errors.New("invalid I2C SCL pin") errInvalidI2CSCL = errors.New("i2c: invalid SCL pin")
ErrI2CAlreadyListening = errors.New("i2c already listening") errI2CAlreadyListening = errors.New("i2c: already listening")
ErrI2CWrongMode = errors.New("i2c wrong mode") errI2CWrongMode = errors.New("i2c: wrong mode")
ErrI2CUnderflow = errors.New("i2c underflow") errI2CUnderflow = errors.New("i2c: underflow")
) )
// Tx performs a write and then a read transfer placing the result in // Tx performs a write and then a read transfer placing the result in
@@ -84,7 +84,7 @@ var (
// Performs only a write transfer. // Performs only a write transfer.
func (i2c *I2C) Tx(addr uint16, w, r []byte) error { func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
if i2c.mode != I2CModeController { if i2c.mode != I2CModeController {
return ErrI2CWrongMode return errI2CWrongMode
} }
return i2c.tx(uint8(addr), w, r) return i2c.tx(uint8(addr), w, r)
} }
@@ -94,7 +94,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
// addr is the address to listen to // addr is the address to listen to
func (i2c *I2C) Listen(addr uint16) error { func (i2c *I2C) Listen(addr uint16) error {
if i2c.mode != I2CModeTarget { if i2c.mode != I2CModeTarget {
return ErrI2CWrongMode return errI2CWrongMode
} }
return i2c.listen(uint8(addr)) return i2c.listen(uint8(addr))
@@ -155,7 +155,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
func (i2c *I2C) SetBaudRate(br uint32) error { func (i2c *I2C) SetBaudRate(br uint32) error {
if br == 0 { if br == 0 {
return ErrInvalidI2CBaudrate return errInvalidI2CBaudrate
} }
// I2C is synchronous design that runs from clk_sys // I2C is synchronous design that runs from clk_sys
@@ -167,7 +167,7 @@ func (i2c *I2C) SetBaudRate(br uint32) error {
hcnt := period - lcnt hcnt := period - lcnt
// Check for out-of-range divisors: // Check for out-of-range divisors:
if hcnt > rp.I2C0_IC_FS_SCL_HCNT_IC_FS_SCL_HCNT_Msk || hcnt < 8 || lcnt > rp.I2C0_IC_FS_SCL_LCNT_IC_FS_SCL_LCNT_Msk || lcnt < 8 { if hcnt > rp.I2C0_IC_FS_SCL_HCNT_IC_FS_SCL_HCNT_Msk || hcnt < 8 || lcnt > rp.I2C0_IC_FS_SCL_LCNT_IC_FS_SCL_LCNT_Msk || lcnt < 8 {
return ErrInvalidI2CBaudrate return errInvalidI2CBaudrate
} }
// Per I2C-bus specification a device in standard or fast mode must // Per I2C-bus specification a device in standard or fast mode must
@@ -187,7 +187,7 @@ func (i2c *I2C) SetBaudRate(br uint32) error {
} }
if sdaTxHoldCnt > lcnt-2 { if sdaTxHoldCnt > lcnt-2 {
return ErrInvalidI2CBaudrate return errInvalidI2CBaudrate
} }
err := i2c.disable() err := i2c.disable()
if err != nil { if err != nil {
@@ -220,7 +220,7 @@ func (i2c *I2C) disable() error {
i2c.Bus.IC_ENABLE.Set(0) i2c.Bus.IC_ENABLE.Set(0)
for i2c.Bus.IC_ENABLE_STATUS.Get()&1 != 0 { for i2c.Bus.IC_ENABLE_STATUS.Get()&1 != 0 {
if ticks() > deadline { if ticks() > deadline {
return ErrRP2040I2CDisable return errI2CDisable
} }
} }
return nil return nil
@@ -286,7 +286,7 @@ func (i2c *I2C) tx(addr uint8, tx, rx []byte) (err error) {
const timeout_us = 4_000 const timeout_us = 4_000
deadline := ticks() + timeout_us deadline := ticks() + timeout_us
if addr >= 0x80 || isReservedI2CAddr(addr) { if addr >= 0x80 || isReservedI2CAddr(addr) {
return ErrInvalidTgtAddr return errInvalidTgtAddr
} }
txlen := len(tx) txlen := len(tx)
rxlen := len(rx) rxlen := len(rx)
@@ -420,7 +420,7 @@ func (i2c *I2C) tx(addr uint8, tx, rx []byte) (err error) {
case abortReason == 0 || abortReason&rp.I2C0_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK != 0: case abortReason == 0 || abortReason&rp.I2C0_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK != 0:
// No reported errors - seems to happen if there is nothing connected to the bus. // No reported errors - seems to happen if there is nothing connected to the bus.
// Address byte not acknowledged // Address byte not acknowledged
err = ErrI2CGeneric err = errI2CGeneric
case abortReason&rp.I2C0_IC_TX_ABRT_SOURCE_ABRT_TXDATA_NOACK != 0: case abortReason&rp.I2C0_IC_TX_ABRT_SOURCE_ABRT_TXDATA_NOACK != 0:
// Address acknowledged, some data not acknowledged // Address acknowledged, some data not acknowledged
fallthrough fallthrough
@@ -434,7 +434,7 @@ func (i2c *I2C) tx(addr uint8, tx, rx []byte) (err error) {
// listen sets up for async handling of requests on the I2C bus. // listen sets up for async handling of requests on the I2C bus.
func (i2c *I2C) listen(addr uint8) error { func (i2c *I2C) listen(addr uint8) error {
if addr >= 0x80 || isReservedI2CAddr(addr) { if addr >= 0x80 || isReservedI2CAddr(addr) {
return ErrInvalidTgtAddr return errInvalidTgtAddr
} }
err := i2c.disable() err := i2c.disable()
@@ -497,7 +497,7 @@ func (i2c *I2C) Reply(buf []byte) error {
stat := i2c.Bus.IC_RAW_INTR_STAT.Get() stat := i2c.Bus.IC_RAW_INTR_STAT.Get()
if stat&rp.I2C0_IC_INTR_MASK_M_RD_REQ == 0 { if stat&rp.I2C0_IC_INTR_MASK_M_RD_REQ == 0 {
return ErrI2CWrongMode return errI2CWrongMode
} }
i2c.Bus.IC_CLR_RD_REQ.Get() // clear restart i2c.Bus.IC_CLR_RD_REQ.Get() // clear restart