@@ -2163,7 +2163,7 @@ func (f flashBlockDevice) ReadAt(p []byte, off int64) (n int, err error) {
21632163}
21642164
21652165// 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 .
21672167// See SAM-D5x-E5x-Family-Data-Sheet-DS60001507.pdf page 591-592.
21682168// If the length of p is not long enough it will be padded with 0xFF bytes.
21692169// 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) {
21852185 waitWhileFlashBusy ()
21862186
21872187 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+ }
21952192
21962193 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 ))
21982195
21992196 waitWhileFlashBusy ()
22002197
@@ -2213,7 +2210,7 @@ func (f flashBlockDevice) Size() int64 {
22132210 return int64 (FlashDataEnd () - FlashDataStart ())
22142211}
22152212
2216- const writeBlockSize = 8
2213+ const writeBlockSize = 512
22172214
22182215// WriteBlockSize returns the block size in which data can be written to
22192216// memory. It can be used by a client to optimize writes, non-aligned writes
0 commit comments