void Reflash ( MEM_PTR *bigData, BANK_TYPE isBank1 ) { uint8_t readData[Quad_Word_Shift]; uint32_t currentFlashAddress = 0x08080000; // bank 2 uint32_t bankStartAddress = 0x08080000; // bank 2 FLASH_EraseInitTypeDef EraseInitStruct; // Watchdog refresh before the process starts if (HAL_IWDG_Refresh(&hiwdg) != HAL_OK) { PRINTF("Watchdog refresh failed\r\n"); Error_Handler(); } /* Disable instruction cache prior to internal cache-able memory update */ if (HAL_ICACHE_Disable() != HAL_OK) { PRINTF("Cache disable failed\r\n"); Error_Handler(); } // Unlock the flash for write access if (HAL_FLASH_Unlock() != HAL_OK) { PRINTF("Flash unlock failed\r\n"); Error_Handler(); } if (isBank1 == BANK2) { currentFlashAddress = 0x08000000; bankStartAddress = 0x08000000; } PRINTF("Erasing at Address %x\r\n",bankStartAddress); // Prepare flash erase structure EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; EraseInitStruct.Banks = GetBank(bankStartAddress); // Get the flash bank EraseInitStruct.Page = GetPage(bankStartAddress); // Get the first page to erase EraseInitStruct.NbPages = FLASH_PAGE_NB; // Get the number of pages to erase PRINTF("Erasing Bank %d, Starting at Page %d, for number of pages %d\r\n", EraseInitStruct.Banks, EraseInitStruct.Page, EraseInitStruct.NbPages); // Erase the flash memory if (HAL_FLASHEx_Erase(&EraseInitStruct, &bigData->Flash.Page) != HAL_OK) { PRINTF("Flash erase failed\r\n"); Error_Handler(); } PRINTF("Flash erase passed, starting flashing\r\n"); HAL_Delay(1000); // Refresh the watchdog again before starting the flashing process if (HAL_IWDG_Refresh(&hiwdg) != HAL_OK) { PRINTF("Watchdog refresh failed during flashing\r\n"); Error_Handler(); } if (HAL_FLASH_Lock() != HAL_OK) { PRINTF("Flash lock failed\r\n"); Error_Handler(); } // maybe due to swap bank always flash to bank2 ???? // currentFlashAddress = 0x08080000; // bankStartAddress = 0x08080000; // Program the flash memory in steps of quad-word (128 bits, 16 bytes) for (uint32_t buff = 0; buff < sizeof(bigData->Flash.Image); buff += Quad_Word_Shift) { if (HAL_FLASH_Unlock() != HAL_OK) { PRINTF("Flash unlock failed\r\n"); Error_Handler(); } // Ensure that the data pointer is aligned to 16-byte boundaries if ((currentFlashAddress % 16) != 0) { PRINTF("Error: Flash address is not 16-byte aligned.\r\n"); Error_Handler(); // Handle misalignment case } // Program flash if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, currentFlashAddress,(uint32_t)&bigData->Flash.Image[buff]) != HAL_OK) { PRINTF("HAL_FLASH_Program failed at buff = %lu\r\n", buff); Error_Handler(); } if (HAL_FLASH_Lock() != HAL_OK) { PRINTF("Flash lock failed\r\n"); Error_Handler(); } // Increment to next quad-word address currentFlashAddress += Quad_Word_Shift; // Ensure we do not exceed the allowed address range if (currentFlashAddress > (bankStartAddress + Max_Page_Shift) || buff >= (FLASH_LIMIT * MEM_FLASH_SIZE)) { break; // Exit if we reach the limit } } // Re-enable the instruction cache after flash programming if (HAL_ICACHE_Enable() != HAL_OK) { PRINTF("Cache enable failed\r\n"); Error_Handler(); } PRINTF("Flash programming completed successfully.\r\n"); PRINTF("Programming Passed, starting verification\r\n"); if (HAL_IWDG_Refresh ( &hiwdg ) != HAL_OK) { PRINTF("Watchdog refresh Failed\r\n"); Error_Handler (); } // Re-enable instruction cache if (HAL_ICACHE_Enable () != HAL_OK) { PRINTF("HAL_ICACHE_Enable Failed\r\n"); Error_Handler (); } bigData->State |= FLASH_COMPLETE; }