Skip to content

Commit a87e1c6

Browse files
committed
fix init order to prevent reset loop in some cases
1 parent e91dff3 commit a87e1c6

File tree

2 files changed

+16
-13
lines changed

2 files changed

+16
-13
lines changed

targets/stm32l432/src/device.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -304,12 +304,6 @@ void device_init()
304304

305305
hw_init(LOW_FREQUENCY);
306306

307-
#if BOOT_TO_DFU
308-
flash_option_bytes_init(1);
309-
#else
310-
flash_option_bytes_init(0);
311-
#endif
312-
313307
if (! tsc_sensor_exists())
314308
{
315309
_NFC_status = nfc_init();
@@ -329,13 +323,18 @@ void device_init()
329323
device_init_button();
330324
}
331325

326+
device_migrate();
327+
328+
#if BOOT_TO_DFU
329+
flash_option_bytes_init(1);
330+
#else
331+
flash_option_bytes_init(0);
332+
#endif
333+
332334
usbhid_init();
333335
ctaphid_init();
334336
ctap_init();
335337

336-
device_migrate();
337-
338-
339338
}
340339

341340
int device_is_nfc(void)

targets/stm32l432/src/flash.c

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ void flash_option_bytes_init(int boot_from_dfu)
4747
val &= ~(1<<25); // SRAM2_RST = 1 (erase sram on reset)
4848
val &= ~(1<<24); // SRAM2_PE = 1 (parity check en)
4949

50-
if (FLASH->OPTR == val)
50+
if ((FLASH->OPTR & 0xb3f77ff) == (val & 0xb3f77ff))
5151
{
5252
return;
5353
}
@@ -68,13 +68,17 @@ void flash_option_bytes_init(int boot_from_dfu)
6868
while (FLASH->SR & (1<<16))
6969
;
7070

71-
flash_lock();
72-
71+
if (FLASH->CR & (1<<30))
72+
{
73+
FLASH->OPTKEYR = 0x08192A3B;
74+
FLASH->OPTKEYR = 0x4C5D6E7F;
75+
}
7376

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

77-
while (true);
80+
while (true)
81+
;
7882
}
7983

8084
void flash_erase_page(uint8_t page)

0 commit comments

Comments
 (0)