diff --git a/sd-bootloader-ng/bootmanager/Makefile b/sd-bootloader-ng/bootmanager/Makefile index 4b5fbe57..3b624f10 100755 --- a/sd-bootloader-ng/bootmanager/Makefile +++ b/sd-bootloader-ng/bootmanager/Makefile @@ -112,6 +112,7 @@ ${BINDIR}/bootmgr.axf: ${OBJDIR}/main.o ${BINDIR}/bootmgr.axf: ${OBJDIR}/helper.o ${BINDIR}/bootmgr.axf: ${OBJDIR}/config.o ${BINDIR}/bootmgr.axf: ${OBJDIR}/patch.o +${BINDIR}/bootmgr.axf: ${OBJDIR}/watchdog.o ${BINDIR}/bootmgr.axf: ${OBJDIR}/udma_if.o diff --git a/sd-bootloader-ng/bootmanager/globalDefines.h b/sd-bootloader-ng/bootmanager/globalDefines.h index 510d4bcb..a4ee3b76 100644 --- a/sd-bootloader-ng/bootmanager/globalDefines.h +++ b/sd-bootloader-ng/bootmanager/globalDefines.h @@ -2,8 +2,11 @@ #define __GLOBALDEFINES_H__ //#define FIXED_BOOT_IMAGE +//#define DISABLE_WATCHDOG +#define WATCHDOG_CHECK_S 2 #define WATCHDOG_TIMEOUT_S 15 +#define WATCHDOG_UtilsDelayMS_MAX 25 #define IMG_OFW_ID_1 0 #define IMG_OFW_ID_2 1 @@ -49,6 +52,7 @@ #define HAL_FCPU_HZ (1000000U * HAL_FCPU_MHZ) #define HAL_SYSTICK_PERIOD_US 1000U #define UTILS_DELAY_US_TO_COUNT(us) (((us)*HAL_FCPU_MHZ) / 6) +#define MILLISECONDS_TO_TICKS(ms) ((HAL_FCPU_HZ / 1000) * (ms)) #define APP_IMG_SRAM_OFFSET 0x20004000 #define CFG_SRAM_OFFSET 0x20000000 diff --git a/sd-bootloader-ng/bootmanager/helper.c b/sd-bootloader-ng/bootmanager/helper.c index df6081aa..bd93a666 100644 --- a/sd-bootloader-ng/bootmanager/helper.c +++ b/sd-bootloader-ng/bootmanager/helper.c @@ -1,5 +1,8 @@ #include "helper.h" -#include + +#include +#include "watchdog.h" +#include "ff.h" void UtilsDelayUs(unsigned long delayUs) { UtilsDelay(UTILS_DELAY_US_TO_COUNT(delayUs)); @@ -7,6 +10,14 @@ void UtilsDelayUs(unsigned long delayUs) { void UtilsDelayMs(unsigned long delayMs) { UtilsDelayUs(1000*delayMs); } +void UtilsDelayMsWD(unsigned long delayMs) { + while (delayMs > WATCHDOG_UtilsDelayMS_MAX) { + delayMs -= WATCHDOG_UtilsDelayMS_MAX; + UtilsDelayMs(WATCHDOG_UtilsDelayMS_MAX); + watchdog_feed(); + } + UtilsDelayMs(delayMs); +} void btox(char *hexstr, const char *binarr, int hexstrlen) { const char characters[]= "0123456789abcdef"; @@ -23,4 +34,13 @@ uint8_t xtob_split(char a, char b) { } uint8_t xtob(char* hexByte) { return xtob_split(hexByte[0], hexByte[1]); +} + +bool SdFileExists(char* filename) { + FIL ffile; + if (f_open(&ffile, filename, FA_READ) == FR_OK) { + f_close(&ffile); + return true; + } + return false; } \ No newline at end of file diff --git a/sd-bootloader-ng/bootmanager/helper.h b/sd-bootloader-ng/bootmanager/helper.h index 27e1a128..23deb68d 100644 --- a/sd-bootloader-ng/bootmanager/helper.h +++ b/sd-bootloader-ng/bootmanager/helper.h @@ -6,6 +6,7 @@ extern "C" #endif #include "globalDefines.h" #include "utils.h" +#include #include #define max(a,b) \ @@ -26,10 +27,13 @@ extern "C" void UtilsDelayUs(unsigned long delayUs); void UtilsDelayMs(unsigned long delayMs); +void UtilsDelayMsWD(unsigned long delayMs); void btox(char *hexstr, const char *binarr, int hexstrlen); uint8_t xtob(char* hexByte); +bool SdFileExists(char* filename); + #ifdef __cplusplus } #endif diff --git a/sd-bootloader-ng/bootmanager/main.c b/sd-bootloader-ng/bootmanager/main.c index 7184959b..75e9237a 100755 --- a/sd-bootloader-ng/bootmanager/main.c +++ b/sd-bootloader-ng/bootmanager/main.c @@ -56,7 +56,6 @@ #include "hw_memmap.h" #include "hw_gprcm.h" #include "hw_common_reg.h" -#include "hw_ints.h" #include "hw_nvic.h" #include "hw_shamd5.h" #include "hw_dthe.h" @@ -73,7 +72,6 @@ #include "bootmgr.h" #include "shamd5.h" #include "adc.h" -#include "wdt.h" #include "sdhost.h" //TODO: fixes some compiler warnings, even diskio.h should load it! @@ -87,6 +85,7 @@ static FATFS fatfs; #include "config.h" #include "patch.h" #include "globalDefines.h" +#include "watchdog.h" char imagePath[] = IMG_SD_PATH; //TODO! @@ -115,8 +114,6 @@ static char* GetImagePathById(uint8_t number) { // Vector Table extern void (*const g_pfnVectors[])(void); - - //***************************************************************************** // //! Board Initialization & Configuration @@ -224,9 +221,9 @@ static void prebootmgr_blink_color(int times, int wait_ms, uint8_t color) for (int i = 0; i < times; i++) { LedSet(color); - UtilsDelayMs(wait_ms); + UtilsDelayMsWD(wait_ms); LedSet(COLOR_BLACK); - UtilsDelayMs(wait_ms); + UtilsDelayMsWD(wait_ms); } } @@ -242,46 +239,6 @@ static void prebootmgr_blink_error(int times, int wait_ms) { #endif } -uint8_t watchdog_feed_state; -static void watchdog_feed() { - watchdog_feed_state = WATCHDOG_TIMEOUT_S; -} -static void watchdog_unfeed() { - watchdog_feed_state = 0; -} -static void watchdog_eat() { - watchdog_feed_state--; -} -void watchdog_handler() { - if (watchdog_feed_state > 0) { - MAP_WatchdogIntClear(WDT_BASE); - watchdog_eat(); - } -} -bool watchdogInitialized = false; -static bool watchdog_start() { - watchdogInitialized = true; - watchdog_feed(); - - MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK); - MAP_WatchdogUnlock(WDT_BASE); - MAP_IntPrioritySet(INT_WDT, INT_PRIORITY_LVL_1); - MAP_WatchdogIntRegister(WDT_BASE, watchdog_handler); - MAP_WatchdogReloadSet(WDT_BASE, 80000000*1); //Tick every second - MAP_WatchdogEnable(WDT_BASE); - - return MAP_WatchdogRunning(WDT_BASE); -} -static void watchdog_stop() { - if (!watchdogInitialized) - return; - MAP_WatchdogUnlock(WDT_BASE); - MAP_WatchdogStallDisable(WDT_BASE); - MAP_WatchdogIntClear(WDT_BASE); - MAP_WatchdogIntUnregister(WDT_BASE); - watchdogInitialized = false; -} - static void SdInit(void) { //Power SD @@ -359,6 +316,8 @@ static void BoardDeinitCustom(void) MAP_GPIOPinWrite(POWER_SD_PORT, POWER_SD_PORT_MASK, POWER_SD_PORT_MASK); //SIC! //Power off other peripherals MAP_GPIOPinWrite(POWER_PORT, POWER_PORT_MASK, 0x00); + + PRCMHibernateWakeupSourceDisable(PRCM_HIB_GPIO17);//Disable charger Wakeup } static volatile bool EarSmallPressed(void) { @@ -368,15 +327,6 @@ static volatile bool EarBigPressed(void) { return !(EAR_BIG_PORT_MASK & MAP_GPIOPinRead(EAR_BIG_PORT, EAR_BIG_PORT_MASK)); } -static bool SdFileExists(char* filename) { - FIL ffile; - if (f_open(&ffile, filename, FA_READ) == FR_OK) { - f_close(&ffile); - return true; - } - return false; -} - //#pragma GCC push_options //#pragma GCC optimize ("O0") //Workaround to fix counter behaving weird and allow being set to 8 despite the slot has no file. static uint8_t Selector(uint8_t startNumber) { @@ -389,7 +339,7 @@ static uint8_t Selector(uint8_t startNumber) { LedSet(COLOR_GREEN); while (EarSmallPressed()) { - UtilsDelayMs(10); //Wait while pressed + UtilsDelayMsWD(10); //Wait while pressed watchdog_feed(); } @@ -398,14 +348,14 @@ static uint8_t Selector(uint8_t startNumber) { if (Config_generalSettings.waitForPress) { LedSet(COLOR_BLUE); while (EarSmallPressed() || EarBigPressed()) { - UtilsDelayMs(10); //Wait while pressed + UtilsDelayMsWD(10); //Wait while pressed watchdog_feed(); } } while (Config_generalSettings.waitForPress) { LedSet(colors[curColorId]); - UtilsDelayMs(250); + UtilsDelayMsWD(250); if (curColorId WATCHDOG_CHECK_S) { + watchdog_feed_state -= WATCHDOG_CHECK_S; + } else { + watchdog_feed_state = 0; + } +} +static void watchdog_handler(void) { + if (watchdog_feed_state > 0) { + MAP_WatchdogIntClear(WDT_BASE); + watchdog_eat(); + } +} +static bool initWatchdog(unsigned long ulLoadVal, void (*pfnHandler)(void)) { + #ifndef DISABLE_WATCHDOG + watchdog_feed(); + + MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK); + MAP_WatchdogUnlock(WDT_BASE); + MAP_IntPrioritySet(INT_WDT, INT_PRIORITY_LVL_1); + MAP_WatchdogStallEnable(WDT_BASE); //Allow Debugging + MAP_WatchdogIntRegister(WDT_BASE, pfnHandler); + MAP_WatchdogReloadSet(WDT_BASE, ulLoadVal); + MAP_WatchdogEnable(WDT_BASE); + + return MAP_WatchdogRunning(WDT_BASE); + #endif +} + +bool watchdog_start(void) { + return initWatchdog(MILLISECONDS_TO_TICKS(1000*WATCHDOG_CHECK_S), watchdog_handler); +} + +static void watchdog_handler_always(void) { + MAP_WatchdogIntClear(WDT_BASE); +} +void watchdog_stop(void) { + initWatchdog(0xFFFFFFFF, watchdog_handler_always); +} diff --git a/sd-bootloader-ng/bootmanager/watchdog.h b/sd-bootloader-ng/bootmanager/watchdog.h new file mode 100644 index 00000000..723e2c2c --- /dev/null +++ b/sd-bootloader-ng/bootmanager/watchdog.h @@ -0,0 +1,19 @@ +#ifndef __WATCHDOG_H__ +#define __WATCHDOG_H__ +#ifdef __cplusplus +extern "C" +{ +#endif +#include +#include + +#include "globalDefines.h" + +void watchdog_feed(void); +bool watchdog_start(void); +void watchdog_stop(void); + +#ifdef __cplusplus +} +#endif +#endif \ No newline at end of file