Skip to content

Commit 568c4e5

Browse files
AP_Bootloader: add ecc check while in bootloader
1 parent b6e7f61 commit 568c4e5

3 files changed

Lines changed: 57 additions & 8 deletions

File tree

AP_Bootloader/AP_Bootloader.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,9 @@ int main(void)
7373
bool try_boot = false;
7474
uint32_t timeout = HAL_BOOTLOADER_TIMEOUT;
7575

76-
#ifdef HAL_BOARD_AP_PERIPH_ZUBAXGNSS
77-
// setup remapping register for ZubaxGNSS
78-
uint32_t mapr = AFIO->MAPR;
79-
mapr &= ~AFIO_MAPR_SWJ_CFG;
80-
mapr |= AFIO_MAPR_SWJ_CFG_JTAGDISABLE;
81-
AFIO->MAPR = mapr | AFIO_MAPR_CAN_REMAP_REMAP2 | AFIO_MAPR_SPI3_REMAP;
76+
flash_init();
77+
#ifdef STM32H7
78+
check_ecc_errors();
8279
#endif
8380

8481
#if HAL_FLASH_PROTECTION
@@ -170,8 +167,6 @@ int main(void)
170167
#if HAL_USE_CAN == TRUE || HAL_NUM_CAN_IFACES
171168
can_start();
172169
#endif
173-
flash_init();
174-
175170

176171
#if EXT_FLASH_SIZE_MB
177172
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)