Skip to content

Commit b8f5627

Browse files
aykevldeadprogram
authored andcommitted
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.
1 parent 9f8715c commit b8f5627

File tree

8 files changed

+70
-55
lines changed

8 files changed

+70
-55
lines changed

src/device/arm/arm.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ import (
3535
"unsafe"
3636
)
3737

38+
var errCycleCountTooLarge = errors.New("requested cycle count is too large, overflows 24 bit counter")
39+
3840
// Run the given assembly code. The code will be marked as having side effects,
3941
// as it doesn't produce output and thus would normally be eliminated by the
4042
// optimizer.
@@ -232,7 +234,7 @@ func SetupSystemTimer(cyclecount uint32) error {
232234
}
233235
if cyclecount&SYST_RVR_RELOAD_Msk != cyclecount {
234236
// The cycle refresh register is only 24 bits wide. The user-specified value will overflow.
235-
return errors.New("requested cycle count is too large, overflows 24 bit counter")
237+
return errCycleCountTooLarge
236238
}
237239

238240
// set refresh count

src/machine/i2c.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,27 @@
22

33
package machine
44

5+
import (
6+
"errors"
7+
)
8+
59
// TWI_FREQ is the I2C bus speed. Normally either 100 kHz, or 400 kHz for high-speed bus.
610
const (
711
TWI_FREQ_100KHZ = 100000
812
TWI_FREQ_400KHZ = 400000
913
)
1014

15+
var (
16+
errI2CWriteTimeout = errors.New("I2C timeout during write")
17+
errI2CReadTimeout = errors.New("I2C timeout during read")
18+
errI2CBusReadyTimeout = errors.New("I2C timeout on bus ready")
19+
errI2CSignalStartTimeout = errors.New("I2C timeout on signal start")
20+
errI2CSignalReadTimeout = errors.New("I2C timeout on signal read")
21+
errI2CSignalStopTimeout = errors.New("I2C timeout on signal stop")
22+
errI2CAckExpected = errors.New("I2C error: expected ACK not NACK")
23+
errI2CBusError = errors.New("I2C bus error")
24+
)
25+
1126
// WriteRegister transmits first the register and then the data to the
1227
// peripheral device.
1328
//

src/machine/machine_atsamd21.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package machine
1010
import (
1111
"device/arm"
1212
"device/sam"
13-
"errors"
1413
"runtime/interrupt"
1514
"unsafe"
1615
)
@@ -549,13 +548,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
549548
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
550549
timeout--
551550
if timeout == 0 {
552-
return errors.New("I2C timeout on ready to write data")
551+
return errI2CWriteTimeout
553552
}
554553
}
555554

556555
// ACK received (0: ACK, 1: NACK)
557556
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
558-
return errors.New("I2C write error: expected ACK not NACK")
557+
return errI2CAckExpected
559558
}
560559

561560
// write data
@@ -581,13 +580,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
581580
// In that case, send a stop condition and return error.
582581
if i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
583582
i2c.Bus.CTRLB.SetBits(wireCmdStop << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Stop condition
584-
return errors.New("I2C read error: expected ACK not NACK")
583+
return errI2CAckExpected
585584
}
586585
}
587586

588587
// ACK received (0: ACK, 1: NACK)
589588
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
590-
return errors.New("I2C read error: expected ACK not NACK")
589+
return errI2CAckExpected
591590
}
592591

593592
// read first byte
@@ -624,16 +623,16 @@ func (i2c I2C) WriteByte(data byte) error {
624623
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
625624
// check for bus error
626625
if sam.SERCOM3_I2CM.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_BUSERR) {
627-
return errors.New("I2C bus error")
626+
return errI2CBusError
628627
}
629628
timeout--
630629
if timeout == 0 {
631-
return errors.New("I2C timeout on write data")
630+
return errI2CWriteTimeout
632631
}
633632
}
634633

635634
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
636-
return errors.New("I2C write error: expected ACK not NACK")
635+
return errI2CAckExpected
637636
}
638637

639638
return nil
@@ -652,7 +651,7 @@ func (i2c I2C) sendAddress(address uint16, write bool) error {
652651
!i2c.Bus.STATUS.HasBits(wireOwnerState<<sam.SERCOM_I2CM_STATUS_BUSSTATE_Pos) {
653652
timeout--
654653
if timeout == 0 {
655-
return errors.New("I2C timeout on bus ready")
654+
return errI2CBusReadyTimeout
656655
}
657656
}
658657
i2c.Bus.ADDR.Set(uint32(data))
@@ -666,7 +665,7 @@ func (i2c I2C) signalStop() error {
666665
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
667666
timeout--
668667
if timeout == 0 {
669-
return errors.New("I2C timeout on signal stop")
668+
return errI2CSignalStopTimeout
670669
}
671670
}
672671
return nil
@@ -678,7 +677,7 @@ func (i2c I2C) signalRead() error {
678677
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
679678
timeout--
680679
if timeout == 0 {
681-
return errors.New("I2C timeout on signal read")
680+
return errI2CSignalReadTimeout
682681
}
683682
}
684683
return nil
@@ -1286,7 +1285,7 @@ func (usbcdc USBCDC) WriteByte(c byte) error {
12861285
for (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_EPINTFLAG_TRCPT1) == 0 {
12871286
timeout--
12881287
if timeout == 0 {
1289-
return errors.New("USBCDC write byte timeout")
1288+
return errUSBCDCWriteByteTimeout
12901289
}
12911290
}
12921291
}

src/machine/machine_atsamd51.go

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ package machine
1010
import (
1111
"device/arm"
1212
"device/sam"
13-
"errors"
1413
"runtime/interrupt"
1514
"unsafe"
1615
)
@@ -925,13 +924,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
925924
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
926925
timeout--
927926
if timeout == 0 {
928-
return errors.New("I2C timeout on ready to write data")
927+
return errI2CWriteTimeout
929928
}
930929
}
931930

932931
// ACK received (0: ACK, 1: NACK)
933932
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
934-
return errors.New("I2C write error: expected ACK not NACK")
933+
return errI2CAckExpected
935934
}
936935

937936
// write data
@@ -957,13 +956,13 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
957956
// In that case, send a stop condition and return error.
958957
if i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
959958
i2c.Bus.CTRLB.SetBits(wireCmdStop << sam.SERCOM_I2CM_CTRLB_CMD_Pos) // Stop condition
960-
return errors.New("I2C read error: expected ACK not NACK")
959+
return errI2CAckExpected
961960
}
962961
}
963962

964963
// ACK received (0: ACK, 1: NACK)
965964
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
966-
return errors.New("I2C read error: expected ACK not NACK")
965+
return errI2CAckExpected
967966
}
968967

969968
// read first byte
@@ -1000,16 +999,16 @@ func (i2c I2C) WriteByte(data byte) error {
1000999
for !i2c.Bus.INTFLAG.HasBits(sam.SERCOM_I2CM_INTFLAG_MB) {
10011000
// check for bus error
10021001
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_BUSERR) {
1003-
return errors.New("I2C bus error")
1002+
return errI2CBusError
10041003
}
10051004
timeout--
10061005
if timeout == 0 {
1007-
return errors.New("I2C timeout on write data")
1006+
return errI2CWriteTimeout
10081007
}
10091008
}
10101009

10111010
if i2c.Bus.STATUS.HasBits(sam.SERCOM_I2CM_STATUS_RXNACK) {
1012-
return errors.New("I2C write error: expected ACK not NACK")
1011+
return errI2CAckExpected
10131012
}
10141013

10151014
return nil
@@ -1028,7 +1027,7 @@ func (i2c I2C) sendAddress(address uint16, write bool) error {
10281027
!i2c.Bus.STATUS.HasBits(wireOwnerState<<sam.SERCOM_I2CM_STATUS_BUSSTATE_Pos) {
10291028
timeout--
10301029
if timeout == 0 {
1031-
return errors.New("I2C timeout on bus ready")
1030+
return errI2CBusReadyTimeout
10321031
}
10331032
}
10341033
i2c.Bus.ADDR.Set(uint32(data))
@@ -1042,7 +1041,7 @@ func (i2c I2C) signalStop() error {
10421041
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
10431042
timeout--
10441043
if timeout == 0 {
1045-
return errors.New("I2C timeout on signal stop")
1044+
return errI2CSignalStopTimeout
10461045
}
10471046
}
10481047
return nil
@@ -1054,7 +1053,7 @@ func (i2c I2C) signalRead() error {
10541053
for i2c.Bus.SYNCBUSY.HasBits(sam.SERCOM_I2CM_SYNCBUSY_SYSOP) {
10551054
timeout--
10561055
if timeout == 0 {
1057-
return errors.New("I2C timeout on signal read")
1056+
return errI2CSignalReadTimeout
10581057
}
10591058
}
10601059
return nil
@@ -1455,7 +1454,7 @@ func (usbcdc USBCDC) WriteByte(c byte) error {
14551454
for (getEPINTFLAG(usb_CDC_ENDPOINT_IN) & sam.USB_DEVICE_ENDPOINT_EPINTFLAG_TRCPT1) == 0 {
14561455
timeout--
14571456
if timeout == 0 {
1458-
return errors.New("USBCDC write byte timeout")
1457+
return errUSBCDCWriteByteTimeout
14591458
}
14601459
}
14611460
}

src/machine/machine_fe310.go

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ package machine
44

55
import (
66
"device/sifive"
7-
"errors"
87
"runtime/interrupt"
98
)
109

@@ -195,8 +194,6 @@ type I2CConfig struct {
195194
SDA Pin
196195
}
197196

198-
var i2cAckExpectedError error = errors.New("I2C write error: expected ACK not NACK")
199-
200197
// Configure is intended to setup the I2C interface.
201198
func (i2c I2C) Configure(config I2CConfig) error {
202199
var i2cClockFrequency uint32 = 32000000
@@ -238,7 +235,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
238235

239236
// ACK received (0: ACK, 1: NACK)
240237
if i2c.Bus.CR_SR.HasBits(sifive.I2C_SR_RX_ACK) {
241-
return i2cAckExpectedError
238+
return errI2CAckExpected
242239
}
243240

244241
// write data
@@ -255,7 +252,7 @@ func (i2c I2C) Tx(addr uint16, w, r []byte) error {
255252

256253
// ACK received (0: ACK, 1: NACK)
257254
if i2c.Bus.CR_SR.HasBits(sifive.I2C_SR_RX_ACK) {
258-
return i2cAckExpectedError
255+
return errI2CAckExpected
259256
}
260257

261258
// read first byte
@@ -290,7 +287,7 @@ func (i2c I2C) writeByte(data byte) error {
290287

291288
// ACK received (0: ACK, 1: NACK)
292289
if i2c.Bus.CR_SR.HasBits(sifive.I2C_SR_RX_ACK) {
293-
return i2cAckExpectedError
290+
return errI2CAckExpected
294291
}
295292

296293
return nil

0 commit comments

Comments
 (0)