diff --git a/src/machine/machine_stm32_flash.go b/src/machine/machine_stm32_flash.go index 00ec718c..898c50dc 100644 --- a/src/machine/machine_stm32_flash.go +++ b/src/machine/machine_stm32_flash.go @@ -73,11 +73,15 @@ func (f flashBlockDevice) EraseBlockSize() int64 { // transparently coalesce ranges of blocks into larger bundles if the chip // supports this. The start and len parameters are in block numbers, use // EraseBlockSize to map addresses to blocks. +// Note that block 0 should map to the address of FlashDataStart(). func (f flashBlockDevice) EraseBlocks(start, len int64) error { + var address uintptr = uintptr(start*f.EraseBlockSize()) + FlashDataStart() + blk := int64(address-uintptr(memoryStart)) / f.EraseBlockSize() + unlockFlash() defer lockFlash() - for i := start; i < start+len; i++ { + for i := blk; i < blk+len; i++ { if err := eraseBlock(uint32(i)); err != nil { return err } diff --git a/src/machine/machine_stm32f4.go b/src/machine/machine_stm32f4.go index 016c1019..3b8923cb 100644 --- a/src/machine/machine_stm32f4.go +++ b/src/machine/machine_stm32f4.go @@ -891,7 +891,7 @@ func writeFlashData(address uintptr, data []byte) (int, error) { // start write operation stm32.FLASH.SetCR_PG(1) - *(*uint16)(unsafe.Pointer(address)) = binary.BigEndian.Uint16(data[i : i+writeBlockSize]) + *(*uint16)(unsafe.Pointer(address)) = binary.LittleEndian.Uint16(data[i : i+writeBlockSize]) waitUntilFlashDone() diff --git a/src/machine/machine_stm32l4.go b/src/machine/machine_stm32l4.go index 2ac00200..f60a77e7 100644 --- a/src/machine/machine_stm32l4.go +++ b/src/machine/machine_stm32l4.go @@ -595,13 +595,13 @@ func writeFlashData(address uintptr, data []byte) (int, error) { // start page write operation stm32.FLASH.SetCR_PG(1) - // write first word using double-word low order word - *(*uint32)(unsafe.Pointer(address)) = binary.BigEndian.Uint32(data[j+writeBlockSize/2 : j+writeBlockSize]) + // write second word using double-word high order word + *(*uint32)(unsafe.Pointer(address)) = binary.LittleEndian.Uint32(data[j : j+writeBlockSize/2]) address += writeBlockSize / 2 - // write second word using double-word high order word - *(*uint32)(unsafe.Pointer(address)) = binary.BigEndian.Uint32(data[j : j+writeBlockSize/2]) + // write first word using double-word low order word + *(*uint32)(unsafe.Pointer(address)) = binary.LittleEndian.Uint32(data[j+writeBlockSize/2 : j+writeBlockSize]) waitUntilFlashDone() diff --git a/src/machine/machine_stm32wlx.go b/src/machine/machine_stm32wlx.go index 7971d7a3..010d038e 100644 --- a/src/machine/machine_stm32wlx.go +++ b/src/machine/machine_stm32wlx.go @@ -484,13 +484,13 @@ func writeFlashData(address uintptr, data []byte) (int, error) { // start page write operation stm32.FLASH.SetCR_PG(1) - // write first word using double-word low order word - *(*uint32)(unsafe.Pointer(address)) = binary.BigEndian.Uint32(data[j+writeBlockSize/2 : j+writeBlockSize]) + // write first word using double-word high order word + *(*uint32)(unsafe.Pointer(address)) = binary.LittleEndian.Uint32(data[j : j+writeBlockSize/2]) address += writeBlockSize / 2 - // write second word using double-word high order word - *(*uint32)(unsafe.Pointer(address)) = binary.BigEndian.Uint32(data[j : j+writeBlockSize/2]) + // write second word using double-word low order word + *(*uint32)(unsafe.Pointer(address)) = binary.LittleEndian.Uint32(data[j+writeBlockSize/2 : j+writeBlockSize]) waitUntilFlashDone()