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.
This commit is contained in:
Ayke van Laethem
2025-02-16 15:11:39 +01:00
committed by Ron Evans
parent ea53ace270
commit 52b9fcd57f
7 changed files with 17 additions and 65 deletions
+11
View File
@@ -64,3 +64,14 @@ type BlockDevice interface {
// EraseBlockSize to map addresses to blocks. // EraseBlockSize to map addresses to blocks.
EraseBlocks(start, len int64) error EraseBlocks(start, len int64) error
} }
// pad data if needed so it is long enough for correct byte alignment on writes.
func flashPad(p []byte, writeBlockSize int) []byte {
overflow := len(p) % writeBlockSize
if overflow != 0 {
for i := 0; i < writeBlockSize-overflow; i++ {
p = append(p, 0xff)
}
}
return p
}
+1 -13
View File
@@ -7,7 +7,6 @@
package machine package machine
import ( import (
"bytes"
"device/arm" "device/arm"
"device/sam" "device/sam"
"errors" "errors"
@@ -1917,7 +1916,7 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
f.ensureInitComplete() f.ensureInitComplete()
address := FlashDataStart() + uintptr(off) address := FlashDataStart() + uintptr(off)
padded := f.pad(p) padded := flashPad(p, int(f.WriteBlockSize()))
waitWhileFlashBusy() waitWhileFlashBusy()
@@ -1992,17 +1991,6 @@ func (f flashBlockDevice) EraseBlocks(start, len int64) error {
return nil return nil
} }
// pad data if needed so it is long enough for correct byte alignment on writes.
func (f flashBlockDevice) pad(p []byte) []byte {
overflow := int64(len(p)) % f.WriteBlockSize()
if overflow == 0 {
return p
}
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
return append(p, padding...)
}
func (f flashBlockDevice) ensureInitComplete() { func (f flashBlockDevice) ensureInitComplete() {
if f.initComplete { if f.initComplete {
return return
+1 -13
View File
@@ -7,7 +7,6 @@
package machine package machine
import ( import (
"bytes"
"device/arm" "device/arm"
"device/sam" "device/sam"
"errors" "errors"
@@ -2174,7 +2173,7 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
} }
address := FlashDataStart() + uintptr(off) address := FlashDataStart() + uintptr(off)
padded := f.pad(p) padded := flashPad(p, int(f.WriteBlockSize()))
settings := disableFlashCache() settings := disableFlashCache()
defer restoreFlashCache(settings) defer restoreFlashCache(settings)
@@ -2263,17 +2262,6 @@ func (f flashBlockDevice) EraseBlocks(start, len int64) error {
return nil return nil
} }
// pad data if needed so it is long enough for correct byte alignment on writes.
func (f flashBlockDevice) pad(p []byte) []byte {
overflow := int64(len(p)) % f.WriteBlockSize()
if overflow == 0 {
return p
}
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
return append(p, padding...)
}
func disableFlashCache() uint16 { func disableFlashCache() uint16 {
settings := sam.NVMCTRL.CTRLA.Get() settings := sam.NVMCTRL.CTRLA.Get()
+1 -13
View File
@@ -3,7 +3,6 @@
package machine package machine
import ( import (
"bytes"
"device/nrf" "device/nrf"
"internal/binary" "internal/binary"
"runtime/interrupt" "runtime/interrupt"
@@ -386,7 +385,7 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
} }
address := FlashDataStart() + uintptr(off) address := FlashDataStart() + uintptr(off)
padded := f.pad(p) padded := flashPad(p, int(f.WriteBlockSize()))
waitWhileFlashBusy() waitWhileFlashBusy()
@@ -444,17 +443,6 @@ func (f flashBlockDevice) EraseBlocks(start, len int64) error {
return nil return nil
} }
// pad data if needed so it is long enough for correct byte alignment on writes.
func (f flashBlockDevice) pad(p []byte) []byte {
overflow := int64(len(p)) % f.WriteBlockSize()
if overflow == 0 {
return p
}
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
return append(p, padding...)
}
func waitWhileFlashBusy() { func waitWhileFlashBusy() {
for nrf.NVMC.GetREADY() != nrf.NVMC_READY_READY_Ready { for nrf.NVMC.GetREADY() != nrf.NVMC_READY_READY_Ready {
} }
-12
View File
@@ -3,7 +3,6 @@
package machine package machine
import ( import (
"bytes"
"unsafe" "unsafe"
) )
@@ -101,17 +100,6 @@ func (f flashBlockDevice) EraseBlocks(start, length int64) error {
return f.eraseBlocks(start, length) return f.eraseBlocks(start, length)
} }
// pad data if needed so it is long enough for correct byte alignment on writes.
func (f flashBlockDevice) pad(p []byte) []byte {
overflow := int64(len(p)) % f.WriteBlockSize()
if overflow == 0 {
return p
}
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
return append(p, padding...)
}
// return the correct address to be used for write // return the correct address to be used for write
func writeAddress(off int64) uintptr { func writeAddress(off int64) uintptr {
return readAddress(off) - uintptr(memoryStart) return readAddress(off) - uintptr(memoryStart)
+1 -1
View File
@@ -230,7 +230,7 @@ func (f flashBlockDevice) writeAt(p []byte, off int64) (n int, err error) {
// e.g. real address 0x10003000 is written to at // e.g. real address 0x10003000 is written to at
// 0x00003000 // 0x00003000
address := writeAddress(off) address := writeAddress(off)
padded := f.pad(p) padded := flashPad(p, int(f.WriteBlockSize()))
C.flash_range_write(C.uint32_t(address), C.flash_range_write(C.uint32_t(address),
(*C.uint8_t)(unsafe.Pointer(&padded[0])), (*C.uint8_t)(unsafe.Pointer(&padded[0])),
+2 -13
View File
@@ -5,7 +5,6 @@ package machine
import ( import (
"device/stm32" "device/stm32"
"bytes"
"unsafe" "unsafe"
) )
@@ -41,7 +40,8 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
unlockFlash() unlockFlash()
defer lockFlash() defer lockFlash()
return writeFlashData(FlashDataStart()+uintptr(off), f.pad(p)) p = flashPad(p, int(f.WriteBlockSize()))
return writeFlashData(FlashDataStart()+uintptr(off), p)
} }
// Size returns the number of bytes in this block device. // Size returns the number of bytes in this block device.
@@ -90,17 +90,6 @@ func (f flashBlockDevice) EraseBlocks(start, len int64) error {
return nil return nil
} }
// pad data if needed so it is long enough for correct byte alignment on writes.
func (f flashBlockDevice) pad(p []byte) []byte {
overflow := int64(len(p)) % f.WriteBlockSize()
if overflow == 0 {
return p
}
padding := bytes.Repeat([]byte{0xff}, int(f.WriteBlockSize()-overflow))
return append(p, padding...)
}
const memoryStart = 0x08000000 const memoryStart = 0x08000000
func unlockFlash() { func unlockFlash() {