diff --git a/src/main/drivers/persistent.h b/src/main/drivers/persistent.h index aaf4f7535f..c47f804eae 100644 --- a/src/main/drivers/persistent.h +++ b/src/main/drivers/persistent.h @@ -29,7 +29,7 @@ typedef enum { PERSISTENT_OBJECT_MAGIC = 0, PERSISTENT_OBJECT_HSE_VALUE, PERSISTENT_OBJECT_OVERCLOCK_LEVEL, - PERSISTENT_OBJECT_BOOTLOADER_REQUEST, + PERSISTENT_OBJECT_BOOTMODE_REQUEST, PERSISTENT_OBJECT_RTC_HIGH, // high 32 bits of rtcTime_t PERSISTENT_OBJECT_RTC_LOW, // low 32 bits of rtcTime_t PERSISTENT_OBJECT_COUNT, diff --git a/src/main/drivers/system.h b/src/main/drivers/system.h index 8f66593060..8c2ba5a68a 100644 --- a/src/main/drivers/system.h +++ b/src/main/drivers/system.h @@ -53,6 +53,7 @@ bool isMPUSoftReset(void); void cycleCounterInit(void); #define BOOTLOADER_REQUEST_COOKIE 0xDEADBEEF +#define MSC_REQUEST_COOKIE 0xDDDD1010 void enableGPIOPowerUsageAndNoiseReductions(void); // current crystal frequency - 8 or 12MHz diff --git a/src/main/drivers/system_stm32f4xx.c b/src/main/drivers/system_stm32f4xx.c index 17c1da9983..3b7b38ef79 100644 --- a/src/main/drivers/system_stm32f4xx.c +++ b/src/main/drivers/system_stm32f4xx.c @@ -41,7 +41,7 @@ void systemReset(void) void systemResetToBootloader(void) { - persistentObjectWrite(PERSISTENT_OBJECT_BOOTLOADER_REQUEST, BOOTLOADER_REQUEST_COOKIE); + persistentObjectWrite(PERSISTENT_OBJECT_BOOTMODE_REQUEST, BOOTLOADER_REQUEST_COOKIE); __disable_irq(); NVIC_SystemReset(); @@ -56,13 +56,12 @@ typedef struct isrVector_s { void checkForBootLoaderRequest(void) { - uint32_t bootloaderRequest = persistentObjectRead(PERSISTENT_OBJECT_BOOTLOADER_REQUEST); - - persistentObjectWrite(PERSISTENT_OBJECT_BOOTLOADER_REQUEST, 0); + uint32_t bootloaderRequest = persistentObjectRead(PERSISTENT_OBJECT_BOOTMODE_REQUEST); if (bootloaderRequest != BOOTLOADER_REQUEST_COOKIE) { return; } + persistentObjectWrite(PERSISTENT_OBJECT_BOOTMODE_REQUEST, 0); extern isrVector_t system_isr_vector_table_base; diff --git a/src/main/drivers/system_stm32f7xx.c b/src/main/drivers/system_stm32f7xx.c index 6d0471be5f..7668ddbdc1 100644 --- a/src/main/drivers/system_stm32f7xx.c +++ b/src/main/drivers/system_stm32f7xx.c @@ -45,7 +45,7 @@ void systemReset(void) void systemResetToBootloader(void) { - persistentObjectWrite(PERSISTENT_OBJECT_BOOTLOADER_REQUEST, BOOTLOADER_REQUEST_COOKIE); + persistentObjectWrite(PERSISTENT_OBJECT_BOOTMODE_REQUEST, BOOTLOADER_REQUEST_COOKIE); __disable_irq(); NVIC_SystemReset(); } @@ -191,13 +191,12 @@ void(*bootJump)(void); void checkForBootLoaderRequest(void) { - uint32_t bootloaderRequest = persistentObjectRead(PERSISTENT_OBJECT_BOOTLOADER_REQUEST); - - persistentObjectWrite(PERSISTENT_OBJECT_BOOTLOADER_REQUEST, 0); + uint32_t bootloaderRequest = persistentObjectRead(PERSISTENT_OBJECT_BOOTMODE_REQUEST); if (bootloaderRequest != BOOTLOADER_REQUEST_COOKIE) { return; } + persistentObjectWrite(PERSISTENT_OBJECT_BOOTMODE_REQUEST, 0); void (*SysMemBootJump)(void); diff --git a/src/main/drivers/usb_msc.h b/src/main/drivers/usb_msc.h index 542f5e9c6f..72ef175307 100644 --- a/src/main/drivers/usb_msc.h +++ b/src/main/drivers/usb_msc.h @@ -24,8 +24,6 @@ #pragma once -#define MSC_MAGIC 0xDDDD1010 - void mscInit(void); bool mscCheckBoot(void); uint8_t mscStart(void); diff --git a/src/main/drivers/usb_msc_f4xx.c b/src/main/drivers/usb_msc_f4xx.c index 0fb400456a..8c4c3c25c2 100644 --- a/src/main/drivers/usb_msc_f4xx.c +++ b/src/main/drivers/usb_msc_f4xx.c @@ -40,7 +40,9 @@ #include "drivers/io.h" #include "drivers/light_led.h" #include "drivers/nvic.h" +#include "drivers/persistent.h" #include "drivers/sdmmc_sdio.h" +#include "drivers/system.h" #include "drivers/time.h" #include "drivers/usb_msc.h" @@ -123,10 +125,11 @@ uint8_t mscStart(void) bool mscCheckBoot(void) { - if (*((uint32_t *)0x2001FFF0) == MSC_MAGIC) { - return true; - } - return false; + const uint32_t bootModeRequest = persistentObjectRead(PERSISTENT_OBJECT_BOOTMODE_REQUEST); + return bootModeRequest == MSC_REQUEST_COOKIE; + // Note that we can't clear the persisent object after checking here. This is because + // this function is called multiple times during initialization. So we clear on a reset + // out of MSC mode. } bool mscCheckButton(void) @@ -159,7 +162,7 @@ void mscWaitForButton(void) void systemResetToMsc(int timezoneOffsetMinutes) { - *((uint32_t *)0x2001FFF0) = MSC_MAGIC; + persistentObjectWrite(PERSISTENT_OBJECT_BOOTMODE_REQUEST, MSC_REQUEST_COOKIE); __disable_irq(); @@ -174,8 +177,7 @@ void systemResetToMsc(int timezoneOffsetMinutes) void systemResetFromMsc(void) { - *((uint32_t *)0x2001FFF0) = 0xFFFFFFFF; - delay(1); + persistentObjectWrite(PERSISTENT_OBJECT_BOOTMODE_REQUEST, 0); __disable_irq(); NVIC_SystemReset(); } diff --git a/src/main/drivers/usb_msc_f7xx.c b/src/main/drivers/usb_msc_f7xx.c index 29633f8942..90c73c5500 100644 --- a/src/main/drivers/usb_msc_f7xx.c +++ b/src/main/drivers/usb_msc_f7xx.c @@ -39,7 +39,9 @@ #include "drivers/io.h" #include "drivers/light_led.h" #include "drivers/nvic.h" +#include "drivers/persistent.h" #include "drivers/serial_usb_vcp.h" +#include "drivers/system.h" #include "drivers/time.h" #include "drivers/usb_msc.h" @@ -128,10 +130,11 @@ uint8_t mscStart(void) bool mscCheckBoot(void) { - if (*((__IO uint32_t *)BKPSRAM_BASE + 16) == MSC_MAGIC) { - return true; - } - return false; + const uint32_t bootModeRequest = persistentObjectRead(PERSISTENT_OBJECT_BOOTMODE_REQUEST); + return bootModeRequest == MSC_REQUEST_COOKIE; + // Note that we can't clear the persisent object after checking here. This is because + // this function is called multiple times during initialization. So we clear on a reset + // out of MSC mode. } bool mscCheckButton(void) @@ -164,7 +167,7 @@ void mscWaitForButton(void) void systemResetToMsc(int timezoneOffsetMinutes) { - *((__IO uint32_t*) BKPSRAM_BASE + 16) = MSC_MAGIC; + persistentObjectWrite(PERSISTENT_OBJECT_BOOTMODE_REQUEST, MSC_REQUEST_COOKIE); __disable_irq(); @@ -179,8 +182,7 @@ void systemResetToMsc(int timezoneOffsetMinutes) void systemResetFromMsc(void) { - *((__IO uint32_t*) BKPSRAM_BASE + 16) = 0xFFFFFFFF; - delay(1); + persistentObjectWrite(PERSISTENT_OBJECT_BOOTMODE_REQUEST, 0); __disable_irq(); NVIC_SystemReset(); }