Skip to content

Commit a3ba40c

Browse files
committed
machine: remove bytes package dependency in flash code
This also moves flash padding code to a single place, since it was copied 5 times. This change is necessary in Go 1.24 to avoid an import cycle.
1 parent 3b2f911 commit a3ba40c

7 files changed

+17
-65
lines changed

src/machine/flash.go

+11
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,14 @@ type BlockDevice interface {
6464
// EraseBlockSize to map addresses to blocks.
6565
EraseBlocks(start, len int64) error
6666
}
67+
68+
// pad data if needed so it is long enough for correct byte alignment on writes.
69+
func flashPad(p []byte, writeBlockSize int) []byte {
70+
overflow := len(p) % writeBlockSize
71+
if overflow != 0 {
72+
for i := 0; i < writeBlockSize-overflow; i++ {
73+
p = append(p, 0xff)
74+
}
75+
}
76+
return p
77+
}

src/machine/machine_atsamd21.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package machine
88

99
import (
10-
"bytes"
1110
"device/arm"
1211
"device/sam"
1312
"errors"
@@ -1917,7 +1916,7 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
19171916
f.ensureInitComplete()
19181917

19191918
address := FlashDataStart() + uintptr(off)
1920-
padded := f.pad(p)
1919+
padded := flashPad(p, int(f.WriteBlockSize()))
19211920

19221921
waitWhileFlashBusy()
19231922

@@ -1992,17 +1991,6 @@ func (f flashBlockDevice) EraseBlocks(start, len int64) error {
19921991
return nil
19931992
}
19941993

1995-
// pad data if needed so it is long enough for correct byte alignment on writes.
1996-
func (f flashBlockDevice) pad(p []byte) []byte {
1997-
overflow := int64(len(p)) % f.WriteBlockSize()
1998-
if overflow == 0 {
1999-
return p
2000-
}
2001-
2002-
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
2003-
return append(p, padding...)
2004-
}
2005-
20061994
func (f flashBlockDevice) ensureInitComplete() {
20071995
if f.initComplete {
20081996
return

src/machine/machine_atsamd51.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
package machine
88

99
import (
10-
"bytes"
1110
"device/arm"
1211
"device/sam"
1312
"errors"
@@ -2174,7 +2173,7 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
21742173
}
21752174

21762175
address := FlashDataStart() + uintptr(off)
2177-
padded := f.pad(p)
2176+
padded := flashPad(p, int(f.WriteBlockSize()))
21782177

21792178
settings := disableFlashCache()
21802179
defer restoreFlashCache(settings)
@@ -2263,17 +2262,6 @@ func (f flashBlockDevice) EraseBlocks(start, len int64) error {
22632262
return nil
22642263
}
22652264

2266-
// pad data if needed so it is long enough for correct byte alignment on writes.
2267-
func (f flashBlockDevice) pad(p []byte) []byte {
2268-
overflow := int64(len(p)) % f.WriteBlockSize()
2269-
if overflow == 0 {
2270-
return p
2271-
}
2272-
2273-
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
2274-
return append(p, padding...)
2275-
}
2276-
22772265
func disableFlashCache() uint16 {
22782266
settings := sam.NVMCTRL.CTRLA.Get()
22792267

src/machine/machine_nrf.go

+1-13
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package machine
44

55
import (
6-
"bytes"
76
"device/nrf"
87
"internal/binary"
98
"runtime/interrupt"
@@ -386,7 +385,7 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
386385
}
387386

388387
address := FlashDataStart() + uintptr(off)
389-
padded := f.pad(p)
388+
padded := flashPad(p, int(f.WriteBlockSize()))
390389

391390
waitWhileFlashBusy()
392391

@@ -444,17 +443,6 @@ func (f flashBlockDevice) EraseBlocks(start, len int64) error {
444443
return nil
445444
}
446445

447-
// pad data if needed so it is long enough for correct byte alignment on writes.
448-
func (f flashBlockDevice) pad(p []byte) []byte {
449-
overflow := int64(len(p)) % f.WriteBlockSize()
450-
if overflow == 0 {
451-
return p
452-
}
453-
454-
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
455-
return append(p, padding...)
456-
}
457-
458446
func waitWhileFlashBusy() {
459447
for nrf.NVMC.GetREADY() != nrf.NVMC_READY_READY_Ready {
460448
}

src/machine/machine_rp2040_flash.go

-12
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
package machine
44

55
import (
6-
"bytes"
76
"unsafe"
87
)
98

@@ -101,17 +100,6 @@ func (f flashBlockDevice) EraseBlocks(start, length int64) error {
101100
return f.eraseBlocks(start, length)
102101
}
103102

104-
// pad data if needed so it is long enough for correct byte alignment on writes.
105-
func (f flashBlockDevice) pad(p []byte) []byte {
106-
overflow := int64(len(p)) % f.WriteBlockSize()
107-
if overflow == 0 {
108-
return p
109-
}
110-
111-
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
112-
return append(p, padding...)
113-
}
114-
115103
// return the correct address to be used for write
116104
func writeAddress(off int64) uintptr {
117105
return readAddress(off) - uintptr(memoryStart)

src/machine/machine_rp2040_rom.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ func (f flashBlockDevice) writeAt(p []byte, off int64) (n int, err error) {
230230
// e.g. real address 0x10003000 is written to at
231231
// 0x00003000
232232
address := writeAddress(off)
233-
padded := f.pad(p)
233+
padded := flashPad(p, int(f.WriteBlockSize()))
234234

235235
C.flash_range_write(C.uint32_t(address),
236236
(*C.uint8_t)(unsafe.Pointer(&padded[0])),

src/machine/machine_stm32_flash.go

+2-13
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package machine
55
import (
66
"device/stm32"
77

8-
"bytes"
98
"unsafe"
109
)
1110

@@ -41,7 +40,8 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
4140
unlockFlash()
4241
defer lockFlash()
4342

44-
return writeFlashData(FlashDataStart()+uintptr(off), f.pad(p))
43+
p = flashPad(p, int(f.WriteBlockSize()))
44+
return writeFlashData(FlashDataStart()+uintptr(off), p)
4545
}
4646

4747
// Size returns the number of bytes in this block device.
@@ -90,17 +90,6 @@ func (f flashBlockDevice) EraseBlocks(start, len int64) error {
9090
return nil
9191
}
9292

93-
// pad data if needed so it is long enough for correct byte alignment on writes.
94-
func (f flashBlockDevice) pad(p []byte) []byte {
95-
overflow := int64(len(p)) % f.WriteBlockSize()
96-
if overflow == 0 {
97-
return p
98-
}
99-
100-
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
101-
return append(p, padding...)
102-
}
103-
10493
const memoryStart = 0x08000000
10594

10695
func unlockFlash() {

0 commit comments

Comments
 (0)