From 4975b13b74ec29828ce880f6a02786d494c2e915 Mon Sep 17 00:00:00 2001
From: Andy Shaw <andy-git@gloomy-place.com>
Date: Thu, 22 Nov 2018 07:03:32 +0000
Subject: [PATCH] Fix problems with LPC1768 EEPROM flash emulation (#12503)

* Remove duplicate calls to PrepareSector

Some flash memory API calls require that a call to `PrepareSector` is done before use. However this call is already made by the LPC1768 framework so the calls in this code are not required.

* Ensure correct alignment of RAM buffer

The LPC176X flash API requires that the RAM buffer used for write operations must be word-aligned. This change ensures that this is the case.
---
 Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp | 4 +---
 1 file changed, 1 insertion(+), 3 deletions(-)

diff --git a/Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp b/Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp
index ce6ab7de65..e08da20904 100644
--- a/Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp
+++ b/Marlin/src/HAL/HAL_LPC1768/persistent_store_flash.cpp
@@ -57,7 +57,7 @@ extern "C" {
 #define EEPROM_ERASE (0xff)
 #define SLOT_ADDRESS(sector, slot) (((uint8_t *)SECTOR_START(sector)) + slot * EEPROM_SIZE)
 
-static uint8_t ram_eeprom[EEPROM_SIZE];
+static uint8_t ram_eeprom[EEPROM_SIZE] __attribute__((aligned(4))) = {0};
 static bool eeprom_dirty = false;
 static int current_slot = 0;
 
@@ -92,7 +92,6 @@ bool PersistentStore::access_finish() {
     if (--current_slot < 0) {
       // all slots have been used, erase everything and start again
       __disable_irq();
-      PrepareSector(EEPROM_SECTOR, EEPROM_SECTOR);
       status = EraseSector(EEPROM_SECTOR, EEPROM_SECTOR);
       __enable_irq();
 
@@ -100,7 +99,6 @@ bool PersistentStore::access_finish() {
     }
 
     __disable_irq();
-    PrepareSector(EEPROM_SECTOR, EEPROM_SECTOR);
     status = CopyRAM2Flash(SLOT_ADDRESS(EEPROM_SECTOR, current_slot), ram_eeprom, IAP_WRITE_4096);
     __enable_irq();
 
-- 
GitLab