diff OtherSources/firmwareEraseProgram.c @ 1017:5924a2d1d3ba GasConsumption

Prepare custom block update function: In the flash area of the font lib some sectors may be used for custom data or a boot updater image. With this change a flash option is added to the maintainance menu. IMPORTANT: The fimwareEraseProgram.c is needed for compiling the firmware now => Add it e.g. by adding a link from the OtherSources location to your source folder.
author Ideenmodellierer
date Thu, 29 May 2025 22:04:46 +0200
parents 7801c5d8a562
children 4b6afe5551e1
line wrap: on
line diff
--- a/OtherSources/firmwareEraseProgram.c	Sun May 11 16:18:20 2025 +0200
+++ b/OtherSources/firmwareEraseProgram.c	Thu May 29 22:04:46 2025 +0200
@@ -83,6 +83,9 @@
 
 #define SECTOR_SIZE_128KB     ((uint32_t)0x00020000) 
 
+#define FLASH_BOOT_START_ADDR   ADDR_FLASH_SECTOR_0
+#define FLASH_BOOT_END_ADDR     (ADDR_FLASH_SECTOR_5 - 1)
+
 #define FLASH_FW_START_ADDR   ADDR_FLASH_SECTOR_6
 #define FLASH_FW_END_ADDR     (ADDR_FLASH_SECTOR_12 - 1)
 
@@ -447,6 +450,133 @@
 	
 }
 
+
+uint8_t bootloader_eraseFlashMemory(void)
+{
+//	HAL_StatusTypeDef answer;
+  /* Unlock the Flash to enable the flash control register access *************/
+  HAL_FLASH_Unlock();
+
+  /* Erase the user Flash area
+    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
+
+  /* Get the 1st sector to erase */
+  FirstSector = GetSector(FLASH_BOOT_START_ADDR);
+  /* Get the number of sector to erase from 1st sector*/
+  NbOfSectors = GetSector(FLASH_BOOT_END_ADDR) - FirstSector + 1;
+
+  /* Fill EraseInit structure*/
+  EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
+  EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_1;
+  EraseInitStruct.Sector = FirstSector;
+  EraseInitStruct.NbSectors = NbOfSectors;
+
+  /* Note: If an erase operation in Flash memory also concerns data in the data or instruction cache,
+     you have to make sure that these data are rewritten before they are accessed during code
+     execution. If this cannot be done safely, it is recommended to flush the caches by setting the
+     DCRST and ICRST bits in the FLASH_CR register. */
+	return HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError);
+}
+
+uint8_t bootloader_programFlashMemory(uint8_t *pBuffer1, uint32_t length1, SHardwareData* pHwInfo)
+{
+	HAL_StatusTypeDef answer;
+
+  /* Program the user Flash area word by word
+    (area defined by FLASH_USER_START_ADDR and FLASH_USER_END_ADDR) ***********/
+
+	uint32_t index = 0;
+	Address = FLASH_BOOT_START_ADDR;
+
+	uint8_t* pHardwareInfo = (uint8_t*) pHwInfo;
+	uint8_t tmp = 0;
+
+	index = 0;
+	while ((Address <= FLASH_FW_END_ADDR) && (index < length1))
+	{
+		if((Address >= HARDWAREDATA_ADDRESS) && (Address < HARDWAREDATA_ADDRESS + sizeof(SHardwareData)))
+		{
+			answer = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Address, *pHardwareInfo++);
+		}
+		else
+		{
+			answer = HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, Address, pBuffer1[index]);
+		}
+		if (answer == HAL_OK)
+		{
+			Address = Address + 1;
+			index++;
+		}
+		else
+		{
+				return answer;
+		}
+	}
+  /* Lock the Flash to disable the flash control register access (recommended
+     to protect the FLASH memory against possible unwanted operation) *********/
+	HAL_FLASH_Lock();
+
+  /* Check if the programmed data is OK
+      MemoryProgramStatus = 0: data programmed correctly
+      MemoryProgramStatus != 0: number of words not programmed correctly ******/
+	Address = FLASH_BOOT_START_ADDR;
+	MemoryProgramStatus = 0x0;
+
+	index = 0;
+	pHardwareInfo = (uint8_t*) pHwInfo;
+
+	while ((Address <= FLASH_FW_END_ADDR) && (index < length1))
+	{
+		if((Address >= HARDWAREDATA_ADDRESS) && (Address < HARDWAREDATA_ADDRESS + sizeof(SHardwareData)))
+		{
+			tmp = *pHardwareInfo++;
+		}
+		else
+		{
+			tmp = pBuffer1[index];
+		}
+
+		data32 = *(__IO uint8_t*)Address;
+		if (data32 != tmp)
+		{
+			MemoryProgramStatus++;
+		}
+
+		Address = Address + 1;
+		index++;
+	}
+  /* Check if there is an issue to program data */
+	if (MemoryProgramStatus == 0)
+	{
+		return HAL_OK;
+    /* No error detected. Switch on LED3 */
+	}
+	else
+	{
+		return 0xEE;
+	}
+}
+
+
+uint32_t CalcFletcher32(uint32_t startAddr, uint32_t endAddr)
+{
+	uint32_t fletcher = 0;
+	uint16_t* pData = (uint16_t*) startAddr;
+	uint32_t index = 0;
+
+	uint16_t sum1 = 0;
+	uint16_t sum2 = 0;
+	for(index = startAddr; index <= endAddr; index +=2)
+	{
+		sum1 = sum1 + *pData++;
+	    sum2 = (sum2 + sum1);
+	}
+	fletcher = (sum2 << 16) | sum1;
+
+	return fletcher;
+}
+
+
 /* Private functions ---------------------------------------------------------*/
 
 /**