Skip to content

Commit 2da9d26

Browse files
eliasnaurdeadprogram
authored andcommitted
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.
1 parent d840971 commit 2da9d26

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/machine/machine_rp2_i2c.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,15 @@ type I2C struct {
5858
}
5959

6060
var (
61-
ErrInvalidI2CBaudrate = errors.New("invalid i2c baudrate")
62-
ErrInvalidTgtAddr = errors.New("invalid target i2c address not in 0..0x80 or is reserved")
63-
ErrI2CGeneric = errors.New("i2c error")
64-
ErrRP2040I2CDisable = errors.New("i2c rp2040 peripheral timeout in disable")
65-
errInvalidI2CSDA = errors.New("invalid I2C SDA pin")
66-
errInvalidI2CSCL = errors.New("invalid I2C SCL pin")
67-
ErrI2CAlreadyListening = errors.New("i2c already listening")
68-
ErrI2CWrongMode = errors.New("i2c wrong mode")
69-
ErrI2CUnderflow = errors.New("i2c underflow")
61+
errInvalidI2CBaudrate = errors.New("i2c: invalid baudrate")
62+
errInvalidTgtAddr = errors.New("i2c: invalid target address: not in 0..0x80 or is reserved")
63+
errI2CGeneric = errors.New("i2c: generic error")
64+
errI2CDisable = errors.New("i2c: peripheral timeout in disable")
65+
errInvalidI2CSDA = errors.New("i2c: invalid SDA pin")
66+
errInvalidI2CSCL = errors.New("i2c: invalid SCL pin")
67+
errI2CAlreadyListening = errors.New("i2c: already listening")
68+
errI2CWrongMode = errors.New("i2c: wrong mode")
69+
errI2CUnderflow = errors.New("i2c: underflow")
7070
)
7171

7272
// Tx performs a write and then a read transfer placing the result in
@@ -84,7 +84,7 @@ var (
8484
// Performs only a write transfer.
8585
func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
8686
if i2c.mode != I2CModeController {
87-
return ErrI2CWrongMode
87+
return errI2CWrongMode
8888
}
8989
return i2c.tx(uint8(addr), w, r)
9090
}
@@ -94,7 +94,7 @@ func (i2c *I2C) Tx(addr uint16, w, r []byte) error {
9494
// addr is the address to listen to
9595
func (i2c *I2C) Listen(addr uint16) error {
9696
if i2c.mode != I2CModeTarget {
97-
return ErrI2CWrongMode
97+
return errI2CWrongMode
9898
}
9999

100100
return i2c.listen(uint8(addr))
@@ -155,7 +155,7 @@ func (i2c *I2C) Configure(config I2CConfig) error {
155155
func (i2c *I2C) SetBaudRate(br uint32) error {
156156

157157
if br == 0 {
158-
return ErrInvalidI2CBaudrate
158+
return errInvalidI2CBaudrate
159159
}
160160

161161
// I2C is synchronous design that runs from clk_sys
@@ -167,7 +167,7 @@ func (i2c *I2C) SetBaudRate(br uint32) error {
167167
hcnt := period - lcnt
168168
// Check for out-of-range divisors:
169169
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 {
170-
return ErrInvalidI2CBaudrate
170+
return errInvalidI2CBaudrate
171171
}
172172

173173
// Per I2C-bus specification a device in standard or fast mode must
@@ -187,7 +187,7 @@ func (i2c *I2C) SetBaudRate(br uint32) error {
187187
}
188188

189189
if sdaTxHoldCnt > lcnt-2 {
190-
return ErrInvalidI2CBaudrate
190+
return errInvalidI2CBaudrate
191191
}
192192
err := i2c.disable()
193193
if err != nil {
@@ -220,7 +220,7 @@ func (i2c *I2C) disable() error {
220220
i2c.Bus.IC_ENABLE.Set(0)
221221
for i2c.Bus.IC_ENABLE_STATUS.Get()&1 != 0 {
222222
if ticks() > deadline {
223-
return ErrRP2040I2CDisable
223+
return errI2CDisable
224224
}
225225
}
226226
return nil
@@ -286,7 +286,7 @@ func (i2c *I2C) tx(addr uint8, tx, rx []byte) (err error) {
286286
const timeout_us = 4_000
287287
deadline := ticks() + timeout_us
288288
if addr >= 0x80 || isReservedI2CAddr(addr) {
289-
return ErrInvalidTgtAddr
289+
return errInvalidTgtAddr
290290
}
291291
txlen := len(tx)
292292
rxlen := len(rx)
@@ -420,7 +420,7 @@ func (i2c *I2C) tx(addr uint8, tx, rx []byte) (err error) {
420420
case abortReason == 0 || abortReason&rp.I2C0_IC_TX_ABRT_SOURCE_ABRT_7B_ADDR_NOACK != 0:
421421
// No reported errors - seems to happen if there is nothing connected to the bus.
422422
// Address byte not acknowledged
423-
err = ErrI2CGeneric
423+
err = errI2CGeneric
424424
case abortReason&rp.I2C0_IC_TX_ABRT_SOURCE_ABRT_TXDATA_NOACK != 0:
425425
// Address acknowledged, some data not acknowledged
426426
fallthrough
@@ -434,7 +434,7 @@ func (i2c *I2C) tx(addr uint8, tx, rx []byte) (err error) {
434434
// listen sets up for async handling of requests on the I2C bus.
435435
func (i2c *I2C) listen(addr uint8) error {
436436
if addr >= 0x80 || isReservedI2CAddr(addr) {
437-
return ErrInvalidTgtAddr
437+
return errInvalidTgtAddr
438438
}
439439

440440
err := i2c.disable()
@@ -497,7 +497,7 @@ func (i2c *I2C) Reply(buf []byte) error {
497497
stat := i2c.Bus.IC_RAW_INTR_STAT.Get()
498498

499499
if stat&rp.I2C0_IC_INTR_MASK_M_RD_REQ == 0 {
500-
return ErrI2CWrongMode
500+
return errI2CWrongMode
501501
}
502502
i2c.Bus.IC_CLR_RD_REQ.Get() // clear restart
503503

0 commit comments

Comments
 (0)