Skip to content

Commit 7b9f8f6

Browse files
AP_Bootloader: add ecc check while in bootloader
1 parent b6e7f61 commit 7b9f8f6

File tree

3 files changed

+57
-1
lines changed

3 files changed

+57
-1
lines changed

AP_Bootloader/AP_Bootloader.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -171,7 +171,9 @@ int main(void)
171171
can_start();
172172
#endif
173173
flash_init();
174-
174+
#ifdef STM32H7
175+
check_ecc_errors();
176+
#endif
175177

176178
#if EXT_FLASH_SIZE_MB
177179
while (!ext_flash.init()) {

AP_Bootloader/support.cpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,53 @@ void port_setbaud(uint32_t baudrate)
485485
#endif
486486
}
487487
#endif // BOOTLOADER_DEV_LIST
488+
489+
490+
#ifdef STM32H7
491+
/*
492+
check if flash has any ECC errors and if it does then erase all of
493+
flash
494+
*/
495+
#define ECC_CHECK_CHUNK_SIZE 32
496+
void check_ecc_errors(void)
497+
{
498+
__disable_fault_irq();
499+
auto *dma = dmaStreamAlloc(STM32_DMA_STREAM_ID(1, 1), 0, nullptr, nullptr);
500+
501+
uint32_t *buf = (uint32_t*)malloc_dma(ECC_CHECK_CHUNK_SIZE);
502+
503+
if (buf == nullptr) {
504+
// DMA'ble memory not available
505+
return;
506+
}
507+
uint32_t ofs = 0;
508+
while (ofs < BOARD_FLASH_SIZE*1024) {
509+
if (FLASH->SR1 != 0) {
510+
break;
511+
}
512+
#if BOARD_FLASH_SIZE > 1024
513+
if (FLASH->SR2 != 0) {
514+
break;
515+
}
516+
#endif
517+
dmaStartMemCopy(dma,
518+
STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
519+
STM32_DMA_CR_MSIZE_BYTE,
520+
ofs+(uint8_t*)FLASH_BASE, buf, sizeof(buf));
521+
dmaWaitCompletion(dma);
522+
ofs += sizeof(buf);
523+
}
524+
dmaStreamFree(dma);
525+
526+
if (ofs < BOARD_FLASH_SIZE*1024) {
527+
// we must have ECC errors in flash
528+
flash_set_keep_unlocked(true);
529+
for (uint32_t i=0; i<num_pages; i++) {
530+
stm32_flash_erasepage(flash_base_page+i);
531+
}
532+
flash_set_keep_unlocked(false);
533+
}
534+
__enable_fault_irq();
535+
}
536+
#endif // STM32H7
537+

AP_Bootloader/support.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ void port_setbaud(uint32_t baudrate);
2121

2222
void flash_init();
2323

24+
#ifdef STM32H7
25+
void check_ecc_errors();
26+
#endif
27+
2428
uint32_t flash_func_read_word(uint32_t offset);
2529
bool flash_func_write_word(uint32_t offset, uint32_t v);
2630
bool flash_func_write_words(uint32_t offset, uint32_t *v, uint8_t n);

0 commit comments

Comments
 (0)