Skip to content

Commit

Permalink
AP_Bootloader: add ecc check while in bootloader
Browse files Browse the repository at this point in the history
  • Loading branch information
bugobliterator committed Jul 18, 2024
1 parent b6e7f61 commit 7b9f8f6
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 1 deletion.
4 changes: 3 additions & 1 deletion AP_Bootloader/AP_Bootloader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,9 @@ int main(void)
can_start();
#endif
flash_init();

#ifdef STM32H7
check_ecc_errors();
#endif

#if EXT_FLASH_SIZE_MB
while (!ext_flash.init()) {
Expand Down
50 changes: 50 additions & 0 deletions AP_Bootloader/support.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -485,3 +485,53 @@ void port_setbaud(uint32_t baudrate)
#endif
}
#endif // BOOTLOADER_DEV_LIST


#ifdef STM32H7
/*
check if flash has any ECC errors and if it does then erase all of
flash
*/
#define ECC_CHECK_CHUNK_SIZE 32
void check_ecc_errors(void)
{
__disable_fault_irq();
auto *dma = dmaStreamAlloc(STM32_DMA_STREAM_ID(1, 1), 0, nullptr, nullptr);

uint32_t *buf = (uint32_t*)malloc_dma(ECC_CHECK_CHUNK_SIZE);

if (buf == nullptr) {
// DMA'ble memory not available
return;
}
uint32_t ofs = 0;
while (ofs < BOARD_FLASH_SIZE*1024) {
if (FLASH->SR1 != 0) {
break;
}
#if BOARD_FLASH_SIZE > 1024
if (FLASH->SR2 != 0) {
break;
}
#endif
dmaStartMemCopy(dma,
STM32_DMA_CR_PL(0) | STM32_DMA_CR_PSIZE_BYTE |
STM32_DMA_CR_MSIZE_BYTE,
ofs+(uint8_t*)FLASH_BASE, buf, sizeof(buf));
dmaWaitCompletion(dma);
ofs += sizeof(buf);
}
dmaStreamFree(dma);

if (ofs < BOARD_FLASH_SIZE*1024) {
// we must have ECC errors in flash
flash_set_keep_unlocked(true);
for (uint32_t i=0; i<num_pages; i++) {
stm32_flash_erasepage(flash_base_page+i);
}
flash_set_keep_unlocked(false);
}
__enable_fault_irq();
}
#endif // STM32H7

4 changes: 4 additions & 0 deletions AP_Bootloader/support.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ void port_setbaud(uint32_t baudrate);

void flash_init();

#ifdef STM32H7
void check_ecc_errors();
#endif

uint32_t flash_func_read_word(uint32_t offset);
bool flash_func_write_word(uint32_t offset, uint32_t v);
bool flash_func_write_words(uint32_t offset, uint32_t *v, uint8_t n);
Expand Down

0 comments on commit 7b9f8f6

Please sign in to comment.