changeset 427:b1091e183d52 ImprovmentNVM_2

Activated ringbuffer for settings: In previous versions the settings have always been writte to the ring start address causing additional ~200ms for sector erase. The settings are now continously written (~8ms). At shutdown the settings are written to ring start for compability reasons. In case of a reset the SW will scan the ringbuffer for the latest available block and restore it.
author ideenmodellierer
date Sun, 16 Feb 2020 22:04:52 +0100
parents 514e6269256f
children 0e5341951079
files Discovery/Src/base.c Discovery/Src/externLogbookFlash.c Discovery/Src/logbook.c Discovery/Src/tMenuEdit.c
diffstat 4 files changed, 40 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/Discovery/Src/base.c	Sat Feb 15 20:50:58 2020 +0100
+++ b/Discovery/Src/base.c	Sun Feb 16 22:04:52 2020 +0100
@@ -455,7 +455,7 @@
     if( settingsGetPointer()->debugModeOnStart )
     {
         settingsGetPointer()->debugModeOnStart = 0;
-        ext_flash_write_settings();
+        ext_flash_write_settings(0);
         setDebugMode();
         openInfo( StIDEBUG );
     }
@@ -481,14 +481,14 @@
         {
             createDiveSettings();
             updateMenu();
-            ext_flash_write_settings();
+            ext_flash_write_settings(0);
         }
 
         /* check if tasks depending on global state are pending */
         get_globalStateList(&status);
         if(status.base == BaseHome)
         {
-            tMenuEdit_writeSettingsToFlash(); // takes 900 ms!!
+            tMenuEdit_writeSettingsToFlash();
         }
 
         DataEX_merge_devicedata(); 	/* data is exchanged at startup and every 10 minutes => check if something changed */
@@ -920,6 +920,7 @@
 //	ext_flash_erase_firmware_if_not_empty();
     GFX_logoAutoOff();
     ext_flash_write_devicedata(true);	/* write data at default position */
+    ext_flash_write_settings(true);		/* write data at default position */
     set_globalState(StStop);
 }
 
--- a/Discovery/Src/externLogbookFlash.c	Sat Feb 15 20:50:58 2020 +0100
+++ b/Discovery/Src/externLogbookFlash.c	Sun Feb 16 22:04:52 2020 +0100
@@ -126,7 +126,7 @@
 static uint32_t	actualPointerDevicedata = DDSTART;
 static uint32_t	actualPointerDevicedata_Read = DDSTART;
 static uint32_t	actualPointerVPM = 0;
-static uint32_t	actualPointerSettings = 0;
+static uint32_t	actualPointerSettings = SETTINGSSTART;
 static uint32_t	actualPointerFirmware = 0;
 static uint32_t	actualPointerFirmware2 = 0;
 
@@ -522,7 +522,7 @@
 	return;
 }
 #else
-void ext_flash_write_settings(void)
+void ext_flash_write_settings(uint8_t resetRing)
 {
 	uint8_t *pData;
 	const uint16_t length = sizeof(SSettings);
@@ -540,7 +540,11 @@
 
 	pData = (uint8_t *)settingsGetPointer();
 
-	actualPointerSettings = SETTINGSSTART;
+	/* Reset the Ring to the start address if requested (e.g. because we write the default block during shutdown) */
+	if((resetRing) || ((actualPointerSettings + length) >= SETTINGSSTOP))
+	{
+		actualPointerSettings = SETTINGSSTART;
+	}
 
 	length_lo = (uint8_t)(length & 0xFF);
 	length_hi = (uint8_t)(length >> 8);
@@ -577,23 +581,36 @@
 	ext_flash_read_block(&length_lo, EF_SETTINGS);
 	ext_flash_read_block(&length_hi, EF_SETTINGS);
 	
-	lengthOnEEPROM = length_hi * 256;
-	lengthOnEEPROM += length_lo;
-	if(lengthOnEEPROM <= lengthStandardNow)
+	while ((length_lo != 0xFF) && (length_hi != 0xFF))		/* get the latest stored setting block */
 	{
-		ext_flash_read_block_multi(&header, 4, EF_SETTINGS);
-		if((header <= pSettings->header) && (header >= pSettings->updateSettingsAllowedFromHeader))
+		lengthOnEEPROM = length_hi * 256;
+		lengthOnEEPROM += length_lo;
+		if(lengthOnEEPROM <= lengthStandardNow)
 		{
-			returnValue = HAL_OK;
-			pSettings->header = header;
-			pData = (uint8_t *)pSettings + 4; /* header */
-			for(uint16_t i = 0; i < (lengthOnEEPROM-4); i++)
-				ext_flash_read_block(&pData[i], EF_SETTINGS);
+			ext_flash_read_block_multi(&header, 4, EF_SETTINGS);
+			if((header <= pSettings->header) && (header >= pSettings->updateSettingsAllowedFromHeader))
+			{
+				returnValue = HAL_OK;
+				pSettings->header = header;
+				pData = (uint8_t *)pSettings + 4; /* header */
+				for(uint16_t i = 0; i < (lengthOnEEPROM-4); i++)
+					ext_flash_read_block(&pData[i], EF_SETTINGS);
+			}
+			else
+			{
+				returnValue = HAL_ERROR;
+			}
 		}
-		else
-		{
-			returnValue = HAL_ERROR;
-		}
+		ext_flash_read_block(&length_lo, EF_SETTINGS);
+		ext_flash_read_block(&length_hi, EF_SETTINGS);
+	}
+	ext_flash_decf_address_ring(EF_SETTINGS);				/* set pointer back to empty address */
+	ext_flash_decf_address_ring(EF_SETTINGS);
+	ext_flash_read_block_stop();
+
+	if(actualAddress > actualPointerSettings)				/* the write pointer has not yet been set up probably */
+	{
+		actualPointerSettings = actualAddress;
 	}
 	ext_flash_read_block_stop();
 	return returnValue;
--- a/Discovery/Src/logbook.c	Sat Feb 15 20:50:58 2020 +0100
+++ b/Discovery/Src/logbook.c	Sun Feb 16 22:04:52 2020 +0100
@@ -1187,7 +1187,7 @@
 				{
 					pSettings->logbookOffset++;
 				}
-				ext_flash_write_settings();
+				ext_flash_write_settings(0);
 				ext_flash_disable_protection_for_logbook();
 
 				ext_flash_CloseSector();	/* this is just a repair function which invalidates a not used sector in case a log maintenance was called before dive */
--- a/Discovery/Src/tMenuEdit.c	Sat Feb 15 20:50:58 2020 +0100
+++ b/Discovery/Src/tMenuEdit.c	Sun Feb 16 22:04:52 2020 +0100
@@ -272,7 +272,7 @@
     if(WriteSettings)
     {
         GFX_logoAutoOff();
-        ext_flash_write_settings();
+        ext_flash_write_settings(0);
         WriteSettings = 0;
     }
 }