machine: move errors.New calls to globals
Calling errors.New in an error path causes a heap allocation at an already unfortunate moment. It is more efficient to create these error values in globals and return these constant globals. If these errors are not used (because the related code was optimized out), the globals will also be optimized out.
Этот коммит содержится в:
родитель
9f8715c143
коммит
b8f5627c9f
8 изменённых файлов: 70 добавлений и 55 удалений
|
@ -35,6 +35,8 @@ import (
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var errCycleCountTooLarge = errors.New("requested cycle count is too large, overflows 24 bit counter")
|
||||||
|
|
||||||
// Run the given assembly code. The code will be marked as having side effects,
|
// Run the given assembly code. The code will be marked as having side effects,
|
||||||
// as it doesn't produce output and thus would normally be eliminated by the
|
// as it doesn't produce output and thus would normally be eliminated by the
|
||||||
// optimizer.
|
// optimizer.
|
||||||
|
@ -232,7 +234,7 @@ func SetupSystemTimer(cyclecount uint32) error {
|
||||||
}
|
}
|
||||||
if cyclecount&SYST_RVR_RELOAD_Msk != cyclecount {
|
if cyclecount&SYST_RVR_RELOAD_Msk != cyclecount {
|
||||||
// The cycle refresh register is only 24 bits wide. The user-specified value will overflow.
|
// The cycle refresh register is only 24 bits wide. The user-specified value will overflow.
|
||||||
return errors.New("requested cycle count is too large, overflows 24 bit counter")
|
return errCycleCountTooLarge
|
||||||
}
|
}
|
||||||
|
|
||||||
// set refresh count
|
// set refresh count
|
||||||
|
|
|
@ -2,12 +2,27 @@
|
||||||
|
|
||||||
package machine
|
package machine
|
||||||
|
|
||||||
|
import (
|
||||||
|
"errors"
|
||||||
|
)
|
||||||
|
|
||||||
// TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
|
// TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
|
||||||
const (
|
const (
|
||||||
TWI_FREQ_100KHZ = 100000
|
TWI_FREQ_100KHZ = 100000
|
||||||
TWI_FREQ_400KHZ = 400000
|
TWI_FREQ_400KHZ = 400000
|
||||||
)
|
)
|
||||||
|
|
||||||
|
var (
|
||||||
|
errI2CWriteTimeout = errors.New("I2C timeout during write")
|
||||||
|
errI2CReadTimeout = errors.New("I2C timeout during read")
|
||||||
|
errI2CBusReadyTimeout = errors.New("I2C timeout on bus ready")
|
||||||
|
errI2CSignalStartTimeout = errors.New("I2C timeout on signal start")
|
||||||
|
errI2CSignalReadTimeout = errors.New("I2C timeout on signal read")
|
||||||
|
errI2CSignalStopTimeout = errors.New("I2C timeout on signal stop")
|
||||||
|
errI2CAckExpected = errors.New("I2C error: expected ACK not NACK")
|
||||||
|
errI2CBusError = errors.New("I2C bus error")
|
||||||
|
)
|
||||||
|
|
||||||
// WriteRegister transmits first the register and then the data to the
|
// WriteRegister transmits first the register and then the data to the
|
||||||
// peripheral device.
|
// peripheral device.
|
||||||
//
|
//
|
||||||
|
|
|
@ -10,7 +10,6 @@ package machine
|
||||||
import (
|
import (
|
||||||
"device/arm"
|
"device/arm"
|
||||||
"device/sam"
|
"device/sam"
|
||||||
"errors"
|
|
||||||
"runtime/interrupt"
|
"runtime/interrupt"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
@ -549,13 +548,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on ready to write data")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ACK received (0: ACK, 1: NACK)
|
// ACK received (0: ACK, 1: NACK)
|
||||||
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
||||||
return errors.New("I2C write error: expected ACK not NACK")
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
|
|
||||||
// write data
|
// write data
|
||||||
|
@ -581,13 +580,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
// In that case, send a stop condition and return error.
|
// In that case, send a stop condition and return error.
|
||||||
if i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
if i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
||||||
i2c.Bus.CTRLB.SetBits(wireCmdStop << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Stop condition
|
i2c.Bus.CTRLB.SetBits(wireCmdStop << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Stop condition
|
||||||
return errors.New("I2C read error: expected ACK not NACK")
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ACK received (0: ACK, 1: NACK)
|
// ACK received (0: ACK, 1: NACK)
|
||||||
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
||||||
return errors.New("I2C read error: expected ACK not NACK")
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
|
|
||||||
// read first byte
|
// read first byte
|
||||||
|
@ -624,16 +623,16 @@ func (i2c I2C) WriteByte(data byte) error {
|
||||||
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
||||||
// check for bus error
|
// check for bus error
|
||||||
if sam.SERCOM3_I2CM.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_BUSERR) {
|
if sam.SERCOM3_I2CM.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_BUSERR) {
|
||||||
return errors.New("I2C bus error")
|
return errI2CBusError
|
||||||
}
|
}
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on write data")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
||||||
return errors.New("I2C write error: expected ACK not NACK")
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -652,7 +651,7 @@ func (i2c I2C) sendAddress(address uint16, write bool) error {
|
||||||
!i2c.Bus.STATUS.HasBits(wireOwnerState<<sam.SERCOM_I2CM_STATUS_BUSSTATE_Pos) {
|
!i2c.Bus.STATUS.HasBits(wireOwnerState<<sam.SERCOM_I2CM_STATUS_BUSSTATE_Pos) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on bus ready")
|
return errI2CBusReadyTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i2c.Bus.ADDR.Set(uint32(data))
|
i2c.Bus.ADDR.Set(uint32(data))
|
||||||
|
@ -666,7 +665,7 @@ func (i2c I2C) signalStop() error {
|
||||||
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on signal stop")
|
return errI2CSignalStopTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -678,7 +677,7 @@ func (i2c I2C) signalRead() error {
|
||||||
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on signal read")
|
return errI2CSignalReadTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -1286,7 +1285,7 @@ func (usbcdc USBCDC) WriteByte(c byte) error {
|
||||||
for (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_EPINTFLAG_TRCPT1) == 0 {
|
for (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_EPINTFLAG_TRCPT1) == 0 {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("USBCDC write byte timeout")
|
return errUSBCDCWriteByteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,7 +10,6 @@ package machine
|
||||||
import (
|
import (
|
||||||
"device/arm"
|
"device/arm"
|
||||||
"device/sam"
|
"device/sam"
|
||||||
"errors"
|
|
||||||
"runtime/interrupt"
|
"runtime/interrupt"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
@ -925,13 +924,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on ready to write data")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ACK received (0: ACK, 1: NACK)
|
// ACK received (0: ACK, 1: NACK)
|
||||||
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
||||||
return errors.New("I2C write error: expected ACK not NACK")
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
|
|
||||||
// write data
|
// write data
|
||||||
|
@ -957,13 +956,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
// In that case, send a stop condition and return error.
|
// In that case, send a stop condition and return error.
|
||||||
if i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
if i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
||||||
i2c.Bus.CTRLB.SetBits(wireCmdStop << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Stop condition
|
i2c.Bus.CTRLB.SetBits(wireCmdStop << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Stop condition
|
||||||
return errors.New("I2C read error: expected ACK not NACK")
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// ACK received (0: ACK, 1: NACK)
|
// ACK received (0: ACK, 1: NACK)
|
||||||
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
||||||
return errors.New("I2C read error: expected ACK not NACK")
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
|
|
||||||
// read first byte
|
// read first byte
|
||||||
|
@ -1000,16 +999,16 @@ func (i2c I2C) WriteByte(data byte) error {
|
||||||
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
|
||||||
// check for bus error
|
// check for bus error
|
||||||
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_BUSERR) {
|
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_BUSERR) {
|
||||||
return errors.New("I2C bus error")
|
return errI2CBusError
|
||||||
}
|
}
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on write data")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
|
||||||
return errors.New("I2C write error: expected ACK not NACK")
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
@ -1028,7 +1027,7 @@ func (i2c I2C) sendAddress(address uint16, write bool) error {
|
||||||
!i2c.Bus.STATUS.HasBits(wireOwnerState<<sam.SERCOM_I2CM_STATUS_BUSSTATE_Pos) {
|
!i2c.Bus.STATUS.HasBits(wireOwnerState<<sam.SERCOM_I2CM_STATUS_BUSSTATE_Pos) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on bus ready")
|
return errI2CBusReadyTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
i2c.Bus.ADDR.Set(uint32(data))
|
i2c.Bus.ADDR.Set(uint32(data))
|
||||||
|
@ -1042,7 +1041,7 @@ func (i2c I2C) signalStop() error {
|
||||||
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on signal stop")
|
return errI2CSignalStopTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -1054,7 +1053,7 @@ func (i2c I2C) signalRead() error {
|
||||||
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on signal read")
|
return errI2CSignalReadTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
|
@ -1455,7 +1454,7 @@ func (usbcdc USBCDC) WriteByte(c byte) error {
|
||||||
for (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) == 0 {
|
for (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) == 0 {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("USBCDC write byte timeout")
|
return errUSBCDCWriteByteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,6 @@ package machine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"device/sifive"
|
"device/sifive"
|
||||||
"errors"
|
|
||||||
"runtime/interrupt"
|
"runtime/interrupt"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -195,8 +194,6 @@ type I2CConfig struct {
|
||||||
SDA Pin
|
SDA Pin
|
||||||
}
|
}
|
||||||
|
|
||||||
var i2cAckExpectedError error = errors.New("I2C write error: expected ACK not NACK")
|
|
||||||
|
|
||||||
// Configure is intended to setup the I2C interface.
|
// Configure is intended to setup the I2C interface.
|
||||||
func (i2c I2C) Configure(config I2CConfig) error {
|
func (i2c I2C) Configure(config I2CConfig) error {
|
||||||
var i2cClockFrequency uint32 = 32000000
|
var i2cClockFrequency uint32 = 32000000
|
||||||
|
@ -238,7 +235,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
|
|
||||||
// ACK received (0: ACK, 1: NACK)
|
// ACK received (0: ACK, 1: NACK)
|
||||||
if i2c.Bus.CR_SR.HasBits(sifive.I2C_SR_RX_ACK) {
|
if i2c.Bus.CR_SR.HasBits(sifive.I2C_SR_RX_ACK) {
|
||||||
return i2cAckExpectedError
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
|
|
||||||
// write data
|
// write data
|
||||||
|
@ -255,7 +252,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
|
|
||||||
// ACK received (0: ACK, 1: NACK)
|
// ACK received (0: ACK, 1: NACK)
|
||||||
if i2c.Bus.CR_SR.HasBits(sifive.I2C_SR_RX_ACK) {
|
if i2c.Bus.CR_SR.HasBits(sifive.I2C_SR_RX_ACK) {
|
||||||
return i2cAckExpectedError
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
|
|
||||||
// read first byte
|
// read first byte
|
||||||
|
@ -290,7 +287,7 @@ func (i2c I2C) writeByte(data byte) error {
|
||||||
|
|
||||||
// ACK received (0: ACK, 1: NACK)
|
// ACK received (0: ACK, 1: NACK)
|
||||||
if i2c.Bus.CR_SR.HasBits(sifive.I2C_SR_RX_ACK) {
|
if i2c.Bus.CR_SR.HasBits(sifive.I2C_SR_RX_ACK) {
|
||||||
return i2cAckExpectedError
|
return errI2CAckExpected
|
||||||
}
|
}
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
|
|
|
@ -6,7 +6,6 @@ package machine
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"device/stm32"
|
"device/stm32"
|
||||||
"errors"
|
|
||||||
"runtime/interrupt"
|
"runtime/interrupt"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
@ -343,7 +342,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY) {
|
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on read clear address")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -354,7 +353,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_RxNE) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_RxNE) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on read 1 byte")
|
return errI2CReadTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -382,7 +381,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY) {
|
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on read clear address")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -394,7 +393,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on read 2 bytes")
|
return errI2CReadTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -428,7 +427,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY) {
|
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on read clear address")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -440,8 +439,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
println("I2C timeout on read 3 bytes")
|
return errI2CReadTimeout
|
||||||
return errors.New("I2C timeout on read 3 bytes")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -455,7 +453,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on read 3 bytes")
|
return errI2CReadTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -483,7 +481,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY) {
|
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on read clear address")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -496,8 +494,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
println("I2C timeout on read 3 bytes")
|
return errI2CReadTimeout
|
||||||
return errors.New("I2C timeout on read 3 bytes")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -510,7 +507,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_BTF) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on read more than 3 bytes")
|
return errI2CReadTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -530,7 +527,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_RxNE) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_RxNE) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on read last byte of more than 3")
|
return errI2CReadTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -554,7 +551,7 @@ func (i2c I2C) signalStart() error {
|
||||||
for i2c.Bus.SR2.HasBits(stm32.I2C_SR2_BUSY) {
|
for i2c.Bus.SR2.HasBits(stm32.I2C_SR2_BUSY) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C busy on start")
|
return errI2CSignalStartTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -569,7 +566,7 @@ func (i2c I2C) signalStart() error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_SB) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_SB) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on start")
|
return errI2CSignalStartTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -592,8 +589,7 @@ func (i2c I2C) waitForStop() error {
|
||||||
for i2c.Bus.SR1.HasBits(stm32.I2C_SR1_STOPF) {
|
for i2c.Bus.SR1.HasBits(stm32.I2C_SR1_STOPF) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
println("I2C timeout on wait for stop signal")
|
return errI2CSignalStopTimeout
|
||||||
return errors.New("I2C timeout on wait for stop signal")
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -617,7 +613,7 @@ func (i2c I2C) sendAddress(address uint8, write bool) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_ADDR) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_ADDR) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on send write address")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -625,7 +621,7 @@ func (i2c I2C) sendAddress(address uint8, write bool) error {
|
||||||
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY | stm32.I2C_SR2_TRA) {
|
for !i2c.Bus.SR2.HasBits(stm32.I2C_SR2_MSL | stm32.I2C_SR2_BUSY | stm32.I2C_SR2_TRA) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on send write address")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
@ -633,7 +629,7 @@ func (i2c I2C) sendAddress(address uint8, write bool) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_ADDR) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_ADDR) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on send read address")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -653,7 +649,7 @@ func (i2c I2C) WriteByte(data byte) error {
|
||||||
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_TxE) {
|
for !i2c.Bus.SR1.HasBits(stm32.I2C_SR1_TxE) {
|
||||||
timeout--
|
timeout--
|
||||||
if timeout == 0 {
|
if timeout == 0 {
|
||||||
return errors.New("I2C timeout on write")
|
return errI2CWriteTimeout
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -4,6 +4,8 @@ package machine
|
||||||
|
|
||||||
import "errors"
|
import "errors"
|
||||||
|
|
||||||
|
var errUARTBufferEmpty = errors.New("UART buffer empty")
|
||||||
|
|
||||||
type UARTConfig struct {
|
type UARTConfig struct {
|
||||||
BaudRate uint32
|
BaudRate uint32
|
||||||
TX Pin
|
TX Pin
|
||||||
|
@ -60,7 +62,7 @@ func (uart UART) ReadByte() (byte, error) {
|
||||||
// check if RX buffer is empty
|
// check if RX buffer is empty
|
||||||
buf, ok := uart.Buffer.Get()
|
buf, ok := uart.Buffer.Get()
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, errors.New("Buffer empty")
|
return 0, errUARTBufferEmpty
|
||||||
}
|
}
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,11 @@ import (
|
||||||
|
|
||||||
const deviceDescriptorSize = 18
|
const deviceDescriptorSize = 18
|
||||||
|
|
||||||
|
var (
|
||||||
|
errUSBCDCBufferEmpty = errors.New("USB-CDC buffer empty")
|
||||||
|
errUSBCDCWriteByteTimeout = errors.New("USB-CDC write byte timeout")
|
||||||
|
)
|
||||||
|
|
||||||
// DeviceDescriptor implements the USB standard device descriptor.
|
// DeviceDescriptor implements the USB standard device descriptor.
|
||||||
//
|
//
|
||||||
// Table 9-8. Standard Device Descriptor
|
// Table 9-8. Standard Device Descriptor
|
||||||
|
@ -598,7 +603,7 @@ func (usbcdc USBCDC) ReadByte() (byte, error) {
|
||||||
// check if RX buffer is empty
|
// check if RX buffer is empty
|
||||||
buf, ok := usbcdc.Buffer.Get()
|
buf, ok := usbcdc.Buffer.Get()
|
||||||
if !ok {
|
if !ok {
|
||||||
return 0, errors.New("Buffer empty")
|
return 0, errUSBCDCBufferEmpty
|
||||||
}
|
}
|
||||||
return buf, nil
|
return buf, nil
|
||||||
}
|
}
|
||||||
|
|
Загрузка…
Создание таблицы
Сослаться в новой задаче