Skip to content

Commit 1123365

Browse files
committed
Modify TC3xx HAL flash erase to support arbitrary offsets and sizes
1 parent 95f65f8 commit 1123365

File tree

2 files changed

+78
-14
lines changed

2 files changed

+78
-14
lines changed

hal/aurix_tc3xx.c

Lines changed: 78 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -459,25 +459,91 @@ int RAMFUNCTION hal_flash_erase(uint32_t address, int len)
459459
{
460460
LED_ON(LED_ERASE);
461461

462-
const uint32_t sectorAddr = GET_SECTOR_ADDR(address);
463-
const size_t numSectors =
464-
(len == 0) ? 0 : ((len - 1) / WOLFBOOT_SECTOR_SIZE) + 1;
465-
const IfxFlash_FlashType type = getFlashTypeFromAddr(address);
462+
/* Handle zero length case */
463+
if (len <= 0) {
464+
LED_OFF(LED_ERASE);
465+
return 0;
466+
}
466467

467-
/* Disable ENDINIT protection */
468-
const uint16 endInitSafetyPassword =
468+
const uint32_t startSectorAddr = GET_SECTOR_ADDR(address);
469+
const uint32_t endAddress = address + len - 1;
470+
const uint32_t endSectorAddr = GET_SECTOR_ADDR(endAddress);
471+
const IfxFlash_FlashType type = getFlashTypeFromAddr(address);
472+
const uint16 endInitSafetyPassword =
469473
IfxScuWdt_getSafetyWatchdogPasswordInline();
470-
IfxScuWdt_clearSafetyEndinitInline(endInitSafetyPassword);
474+
uint32_t currentSectorAddr;
471475

472-
IfxFlash_eraseMultipleSectors(sectorAddr, numSectors);
476+
/* If address and len are both sector-aligned, perform simple bulk erase */
477+
if ((address == startSectorAddr) &&
478+
(endAddress == endSectorAddr + WOLFBOOT_SECTOR_SIZE - 1)) {
479+
const size_t numSectors =
480+
(endSectorAddr - startSectorAddr) / WOLFBOOT_SECTOR_SIZE + 1;
473481

474-
/* Reenable ENDINIT protection */
475-
IfxScuWdt_setSafetyEndinitInline(endInitSafetyPassword);
482+
/* Disable ENDINIT protection */
483+
IfxScuWdt_clearSafetyEndinitInline(endInitSafetyPassword);
476484

477-
IfxFlash_waitUnbusy(FLASH_MODULE, type);
485+
IfxFlash_eraseMultipleSectors(startSectorAddr, numSectors);
478486

479-
LED_OFF(LED_ERASE);
487+
/* Reenable ENDINIT protection */
488+
IfxScuWdt_setSafetyEndinitInline(endInitSafetyPassword);
480489

490+
IfxFlash_waitUnbusy(FLASH_MODULE, type);
491+
}
492+
/* For non-sector aligned erases, handle each sector carefully */
493+
else {
494+
/* Process each affected sector */
495+
for (currentSectorAddr = startSectorAddr;
496+
currentSectorAddr <= endSectorAddr;
497+
currentSectorAddr += WOLFBOOT_SECTOR_SIZE) {
498+
499+
/* Check if this is a partial sector erase */
500+
const int isFirstSector = (currentSectorAddr == startSectorAddr);
501+
const int isLastSector = (currentSectorAddr == endSectorAddr);
502+
const int isPartialStart =
503+
isFirstSector && (address > startSectorAddr);
504+
const int isPartialEnd =
505+
isLastSector &&
506+
(endAddress < (endSectorAddr + WOLFBOOT_SECTOR_SIZE - 1));
507+
508+
/* For partial sectors, need to read-modify-write */
509+
if (isPartialStart || isPartialEnd) {
510+
/* Read the sector into the sector buffer */
511+
cacheSector(currentSectorAddr, type);
512+
513+
/* Calculate which bytes within the sector to erase */
514+
uint32_t eraseStartOffset =
515+
isPartialStart ? (address - currentSectorAddr) : 0;
516+
517+
uint32_t eraseEndOffset = isPartialEnd
518+
? (endAddress - currentSectorAddr)
519+
: (WOLFBOOT_SECTOR_SIZE - 1);
520+
521+
uint32_t eraseLen = eraseEndOffset - eraseStartOffset + 1;
522+
523+
/* Fill the section to be erased with the erased byte value */
524+
memset((uint8_t*)sectorBuffer + eraseStartOffset,
525+
FLASH_BYTE_ERASED, eraseLen);
526+
527+
/* Erase the sector */
528+
IfxScuWdt_clearSafetyEndinitInline(endInitSafetyPassword);
529+
IfxFlash_eraseSector(currentSectorAddr);
530+
IfxScuWdt_setSafetyEndinitInline(endInitSafetyPassword);
531+
IfxFlash_waitUnbusy(FLASH_MODULE, type);
532+
533+
/* Program the modified buffer back */
534+
programCachedSector(currentSectorAddr, type);
535+
}
536+
/* For full sector erase, just erase directly */
537+
else {
538+
IfxScuWdt_clearSafetyEndinitInline(endInitSafetyPassword);
539+
IfxFlash_eraseSector(currentSectorAddr);
540+
IfxScuWdt_setSafetyEndinitInline(endInitSafetyPassword);
541+
IfxFlash_waitUnbusy(FLASH_MODULE, type);
542+
}
543+
}
544+
}
545+
546+
LED_OFF(LED_ERASE);
481547
return 0;
482548
}
483549

src/image.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,7 +1359,6 @@ static int copy_flash_buffered(uintptr_t src_addr, uintptr_t dst_addr,
13591359
static uint8_t buffer[FLASHBUFFER_SIZE];
13601360
#endif
13611361

1362-
#if 0
13631362
#ifdef WOLFBOOT_FLASH_MULTI_SECTOR_ERASE
13641363
/* Mass erase destination flash in one go before writing */
13651364
#ifdef EXT_FLASH
@@ -1376,7 +1375,6 @@ static int copy_flash_buffered(uintptr_t src_addr, uintptr_t dst_addr,
13761375
hal_flash_lock();
13771376
}
13781377
#endif /* WOLFBOOT_FLASH_MULTI_SECTOR_ERASE */
1379-
#endif
13801378

13811379
/* Loop until all requested bytes are copied */
13821380
while (bytes_copied < total_size) {

0 commit comments

Comments
 (0)