@@ -1904,7 +1904,7 @@ func (f flashBlockDevice) ReadAt(p []byte, off int64) (n int, err error) {
1904
1904
}
1905
1905
1906
1906
// 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 .
1908
1908
// See Atmel-42181G–SAM-D21_Datasheet–09/2015 page 359.
1909
1909
// If the length of p is not long enough it will be padded with 0xFF bytes.
1910
1910
// 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) {
1921
1921
waitWhileFlashBusy ()
1922
1922
1923
1923
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
+ }
1926
1928
1927
1929
sam .NVMCTRL .SetADDR (uint32 (address >> 1 ))
1928
1930
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 {
1944
1946
return int64 (FlashDataEnd () - FlashDataStart ())
1945
1947
}
1946
1948
1947
- const writeBlockSize = 4
1949
+ const writeBlockSize = 64
1948
1950
1949
1951
// WriteBlockSize returns the block size in which data can be written to
1950
1952
// memory. It can be used by a client to optimize writes, non-aligned writes
0 commit comments