changeset 421:3f7d80f37bfc ImprovmentNVM_2

Enable sequentionel writing of device data: DeviceData was always written to the start of the the DD ringbuffer causing everytime a sector erase delay (~200ms). To avoid this the ring buffer functionality has been activated. To be backward compatible the latest DD set will be written to DD ring buffer start at shutdown time. In case of a reset the firmware scans for the latest DD block and restores its content giving the same data consistency intervall (10 minutes) as the previous implementation without having the 200ms penality for sector erases
author ideenmodellierer
date Mon, 10 Feb 2020 19:25:09 +0100
parents 2174fb133dbe
children b67327177159
files Discovery/Inc/externLogbookFlash.h Discovery/Src/data_exchange_main.c Discovery/Src/externLogbookFlash.c
diffstat 3 files changed, 41 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/Discovery/Inc/externLogbookFlash.h	Mon Feb 10 19:15:27 2020 +0100
+++ b/Discovery/Inc/externLogbookFlash.h	Mon Feb 10 19:25:09 2020 +0100
@@ -105,7 +105,7 @@
 void ext_flash_write_settings(void);
 uint8_t ext_flash_read_settings(void);
 
-void ext_flash_write_devicedata(void);
+void ext_flash_write_devicedata(uint8_t resetRing);
 uint16_t ext_flash_read_devicedata(uint8_t *buffer, uint16_t max_length);
 void ext_flash_read_fixed_16_devicedata_blocks_formated_128byte_total(uint8_t *buffer);
 
--- a/Discovery/Src/data_exchange_main.c	Mon Feb 10 19:15:27 2020 +0100
+++ b/Discovery/Src/data_exchange_main.c	Mon Feb 10 19:25:09 2020 +0100
@@ -640,7 +640,7 @@
 
 	if(dataLengthRead == 0)
 	{
-		ext_flash_write_devicedata();
+		ext_flash_write_devicedata(false);
 		return;
 	}
 
@@ -682,7 +682,7 @@
 	}
 	
 	DataEX_check_DeviceData	();
-	ext_flash_write_devicedata();
+	ext_flash_write_devicedata(false);
 }
 
 
--- a/Discovery/Src/externLogbookFlash.c	Mon Feb 10 19:15:27 2020 +0100
+++ b/Discovery/Src/externLogbookFlash.c	Mon Feb 10 19:25:09 2020 +0100
@@ -121,7 +121,8 @@
 static uint32_t	actualPointerHeader = 0;
 static uint32_t	actualPointerSample = 0;
 static uint32_t	LengthLeftSampleRead = 0;
-static uint32_t	actualPointerDevicedata = 0;
+static uint32_t	actualPointerDevicedata = DDSTART;
+static uint32_t	actualPointerDevicedata_Read = DDSTART;
 static uint32_t	actualPointerVPM = 0;
 static uint32_t	actualPointerSettings = 0;
 static uint32_t	actualPointerFirmware = 0;
@@ -392,18 +393,24 @@
 
 #ifndef BOOTLOADER_STANDALONE
 
-void ext_flash_write_devicedata(void)
+void ext_flash_write_devicedata(uint8_t resetRing)
 {
 	uint8_t *pData;
 	const uint16_t length = sizeof(SDevice);
 	uint8_t length_lo, length_hi;
 	uint8_t dataLength[2] = { 0 };
+	uint32_t tmpBlockStart;
 
 	ext_flash_disable_protection();
 
 	pData = (uint8_t *)stateDeviceGetPointer();
 
-	actualPointerDevicedata = DDSTART;
+	/* Reset the Ring to the start address if requested (e.g. because we write the default block during shutdown) */
+	if((resetRing) || ((actualPointerDevicedata + length) >= DDSTOP))
+	{
+		actualPointerDevicedata = DDSTART;
+	}
+	tmpBlockStart = actualPointerDevicedata;
 
 	length_lo = (uint8_t)(length & 0xFF);
 	length_hi = (uint8_t)(length >> 8);
@@ -412,6 +419,9 @@
 
 	ef_write_block(dataLength,2, EF_DEVICEDATA, 0);
 	ef_write_block(pData,length, EF_DEVICEDATA, 0);
+
+	actualPointerDevicedata_Read = tmpBlockStart;
+
 }
 
 
@@ -420,20 +430,37 @@
 	uint16_t length;
 	uint8_t length_lo, length_hi;
 
-	actualAddress = DDSTART;
+	actualAddress = actualPointerDevicedata_Read; 
 
+	length = 0;
+	length_lo = 0;
+	length_hi = 0;
 	ext_flash_read_block_start();
+
+
 	ext_flash_read_block(&length_lo, EF_DEVICEDATA);
 	ext_flash_read_block(&length_hi, EF_DEVICEDATA);
 	
-	length = (length_hi * 256) + length_lo;
-	
-	if(length > max_length)
-		return 0;
-	
-	ext_flash_read_block_multi(buffer,length,EF_DEVICEDATA);
+	while ((length_lo != 0xFF) && (length_hi != 0xFF))
+	{
+		length = (length_hi * 256) + length_lo;
+
+		if(length > max_length)
+			return 0;
+
+		ext_flash_read_block_multi(buffer,length,EF_DEVICEDATA);
+
+		ext_flash_read_block(&length_lo, EF_DEVICEDATA);	/* check if another devicedata set is available */
+		ext_flash_read_block(&length_hi, EF_DEVICEDATA);	/* length will be 0xFFFF if a empty memory is read */
+	}
+	ext_flash_decf_address_ring(EF_DEVICEDATA);				/* set pointer back to empty address */
+	ext_flash_decf_address_ring(EF_DEVICEDATA);
 	ext_flash_read_block_stop();
-	
+
+	if(actualAddress > actualPointerDevicedata)				/* the write pointer has not yet been set up probably */
+	{
+		actualPointerDevicedata = actualAddress;
+	}
 	return length;
 }