Skip to content

Commit

Permalink
fix init order to prevent reset loop in some cases
Browse files Browse the repository at this point in the history
  • Loading branch information
conorpp committed Feb 3, 2021
1 parent e91dff3 commit a87e1c6
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 13 deletions.
17 changes: 8 additions & 9 deletions targets/stm32l432/src/device.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,12 +304,6 @@ void device_init()

hw_init(LOW_FREQUENCY);

#if BOOT_TO_DFU
flash_option_bytes_init(1);
#else
flash_option_bytes_init(0);
#endif

if (! tsc_sensor_exists())
{
_NFC_status = nfc_init();
Expand All @@ -329,13 +323,18 @@ void device_init()
device_init_button();
}

device_migrate();

#if BOOT_TO_DFU
flash_option_bytes_init(1);
#else
flash_option_bytes_init(0);
#endif

usbhid_init();
ctaphid_init();
ctap_init();

device_migrate();


}

int device_is_nfc(void)
Expand Down
12 changes: 8 additions & 4 deletions targets/stm32l432/src/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void flash_option_bytes_init(int boot_from_dfu)
val &= ~(1<<25); // SRAM2_RST = 1 (erase sram on reset)
val &= ~(1<<24); // SRAM2_PE = 1 (parity check en)

if (FLASH->OPTR == val)
if ((FLASH->OPTR & 0xb3f77ff) == (val & 0xb3f77ff))
{
return;
}
Expand All @@ -68,13 +68,17 @@ void flash_option_bytes_init(int boot_from_dfu)
while (FLASH->SR & (1<<16))
;

flash_lock();

if (FLASH->CR & (1<<30))
{
FLASH->OPTKEYR = 0x08192A3B;
FLASH->OPTKEYR = 0x4C5D6E7F;
}

/* Perform option byte loading which triggers a device reset. */
FLASH->CR |= FLASH_CR_OBL_LAUNCH;

while (true);
while (true)
;
}

void flash_erase_page(uint8_t page)
Expand Down

0 comments on commit a87e1c6

Please sign in to comment.