@@ -2163,7 +2163,7 @@ func (f flashBlockDevice) ReadAt(p []byte, off int64) (n int, err error) {
2163
2163
}
2164
2164
2165
2165
// WriteAt writes the given number of bytes to the block device.
2166
- // Only word (32 bits) length data can be programmed .
2166
+ // Data is written to the page buffer in 4-byte chunks, then saved to flash memory .
2167
2167
// See SAM-D5x-E5x-Family-Data-Sheet-DS60001507.pdf page 591-592.
2168
2168
// If the length of p is not long enough it will be padded with 0xFF bytes.
2169
2169
// This method assumes that the destination is already erased.
@@ -2185,16 +2185,13 @@ func (f flashBlockDevice) WriteAt(p []byte, off int64) (n int, err error) {
2185
2185
waitWhileFlashBusy ()
2186
2186
2187
2187
for j := 0 ; j < len (padded ); j += int (f .WriteBlockSize ()) {
2188
- // write first word using double-word low order word
2189
- * (* uint32 )(unsafe .Pointer (address )) = binary .LittleEndian .Uint32 (padded [j : j + int (f .WriteBlockSize ()/ 2 )])
2190
-
2191
- // write second word using double-word high order word
2192
- * (* uint32 )(unsafe .Add (unsafe .Pointer (address ), uintptr (f .WriteBlockSize ())/ 2 )) = binary .LittleEndian .Uint32 (padded [j + int (f .WriteBlockSize ()/ 2 ) : j + int (f .WriteBlockSize ())])
2193
-
2194
- waitWhileFlashBusy ()
2188
+ // page buffer is 512 bytes long, but only 4 bytes can be written at once
2189
+ for k := 0 ; k < int (f .WriteBlockSize ()); k += 4 {
2190
+ * (* uint32 )(unsafe .Pointer (address + uintptr (k ))) = binary .LittleEndian .Uint32 (padded [j + k : j + k + 4 ])
2191
+ }
2195
2192
2196
2193
sam .NVMCTRL .SetADDR (uint32 (address ))
2197
- sam .NVMCTRL .CTRLB .Set (sam .NVMCTRL_CTRLB_CMD_WQW | (sam .NVMCTRL_CTRLB_CMDEX_KEY << sam .NVMCTRL_CTRLB_CMDEX_Pos ))
2194
+ sam .NVMCTRL .CTRLB .Set (sam .NVMCTRL_CTRLB_CMD_WP | (sam .NVMCTRL_CTRLB_CMDEX_KEY << sam .NVMCTRL_CTRLB_CMDEX_Pos ))
2198
2195
2199
2196
waitWhileFlashBusy ()
2200
2197
@@ -2213,7 +2210,7 @@ func (f flashBlockDevice) Size() int64 {
2213
2210
return int64 (FlashDataEnd () - FlashDataStart ())
2214
2211
}
2215
2212
2216
- const writeBlockSize = 8
2213
+ const writeBlockSize = 512
2217
2214
2218
2215
// WriteBlockSize returns the block size in which data can be written to
2219
2216
// memory. It can be used by a client to optimize writes, non-aligned writes
0 commit comments