Skip to content

Commit 28f9be6

Browse files
machine/samd21: write to flash memory in 64 byte long chunks
1 parent cb5f7ad commit 28f9be6

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

src/machine/machine_atsamd21.go

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1904,7 +1904,7 @@ func (f flashBlockDevice) ReadAt(p []byte, off int64) (n int, err error) {
19041904
}
19051905

19061906
// WriteAt writes the given number of bytes to the block device.
1907-
// Only word (32 bits) length data can be programmed.
1907+
// Data is written to the page buffer in 4-byte chunks, then saved to flash memory.
19081908
// See Atmel-42181G–SAM-D21_Datasheet–09/2015 page 359.
19091909
// If the length of p is not long enough it will be padded with 0xFF bytes.
19101910
// This method assumes that the destination is already erased.
@@ -1921,8 +1921,10 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
19211921
waitWhileFlashBusy()
19221922

19231923
for j := 0; j < len(padded); j += int(f.WriteBlockSize()) {
1924-
// write word
1925-
*(*uint32)(unsafe.Pointer(address)) = binary.LittleEndian.Uint32(padded[j : j+int(f.WriteBlockSize())])
1924+
// page buffer is 64 bytes long, but only 4 bytes can be written at once
1925+
for k := 0; k < int(f.WriteBlockSize()); k += 4 {
1926+
*(*uint32)(unsafe.Pointer(address + uintptr(k))) = binary.LittleEndian.Uint32(padded[j+k : j+k+4])
1927+
}
19261928

19271929
sam.NVMCTRL.SetADDR(uint32(address >> 1))
19281930
sam.NVMCTRL.CTRLA.Set(sam.NVMCTRL_CTRLA_CMD_WP | (sam.NVMCTRL_CTRLA_CMDEX_KEY << sam.NVMCTRL_CTRLA_CMDEX_Pos))
@@ -1944,7 +1946,7 @@ func (f flashBlockDevice) Size() int64 {
19441946
return int64(FlashDataEnd() - FlashDataStart())
19451947
}
19461948

1947-
const writeBlockSize = 4
1949+
const writeBlockSize = 64
19481950

19491951
// WriteBlockSize returns the block size in which data can be written to
19501952
// memory. It can be used by a client to optimize writes, non-aligned writes

0 commit comments

Comments
 (0)