Skip to content

Commit

Permalink
Develop (#18)
Browse files Browse the repository at this point in the history
* How to build details

* Backmerge (#14)

* Own watchdog file, wd delay helper, disable charger wakeup, SdFileExists to helper
  • Loading branch information
SciLor authored Feb 2, 2021
1 parent 34e827a commit 4887535
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 72 deletions.
1 change: 1 addition & 0 deletions sd-bootloader-ng/bootmanager/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 4 additions & 0 deletions sd-bootloader-ng/bootmanager/globalDefines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
22 changes: 21 additions & 1 deletion sd-bootloader-ng/bootmanager/helper.c
Original file line number Diff line number Diff line change
@@ -1,12 +1,23 @@
#include "helper.h"
#include <stdint.h>

#include <stdbool.h>
#include "watchdog.h"
#include "ff.h"

void UtilsDelayUs(unsigned long delayUs) {
UtilsDelay(UTILS_DELAY_US_TO_COUNT(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";
Expand All @@ -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;
}
4 changes: 4 additions & 0 deletions sd-bootloader-ng/bootmanager/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ extern "C"
#endif
#include "globalDefines.h"
#include "utils.h"
#include <stdbool.h>
#include <stdint.h>

#define max(a,b) \
Expand All @@ -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
Expand Down
89 changes: 19 additions & 70 deletions sd-bootloader-ng/bootmanager/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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!

Expand All @@ -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!
Expand Down Expand Up @@ -115,8 +114,6 @@ static char* GetImagePathById(uint8_t number) {
// Vector Table
extern void (*const g_pfnVectors[])(void);



//*****************************************************************************
//
//! Board Initialization & Configuration
Expand Down Expand Up @@ -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);
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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) {
Expand All @@ -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) {
Expand All @@ -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();
}

Expand All @@ -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<COUNT_OF(colors)-1) {
curColorId++;
Expand All @@ -427,7 +377,7 @@ static uint8_t Selector(uint8_t startNumber) {
counter = (counter+1) % COUNT_OF(Config_imageInfos);
} while (!Config_imageInfos[counter].fileExists);
while (EarSmallPressed()) {
UtilsDelayMs(10); //Wait while pressed
UtilsDelayMsWD(10); //Wait while pressed
}
}
if (counter < 3) {
Expand All @@ -437,7 +387,7 @@ static uint8_t Selector(uint8_t startNumber) {
} else /*if (counter < 9)*/ {
prebootmgr_blink_color((counter+1)-6, 100, COLOR_CYAN);
}
UtilsDelayMs(500);
UtilsDelayMsWD(500);
watchdog_feed();
}
return counter;
Expand Down Expand Up @@ -525,10 +475,9 @@ int main()
Config_InitImageInfos();
#endif

UtilsDelayMs(100);
UtilsDelayMsWD(100);
ffs_result = f_mount(&fatfs, "0", 1);
if (ffs_result == FR_OK)
{
if (ffs_result == FR_OK) {
#ifdef FIXED_BOOT_IMAGE
char* image = IMG_SD_BOOTLOADER_PATH;
if (SdFileExists(image)) {
Expand Down Expand Up @@ -647,24 +596,24 @@ int main()
BoardDeinitCustom();
Run((unsigned long)pImgRun);
} else {
UtilsDelayMs(1000);
UtilsDelayMsWD(1000);
prebootmgr_blink_error(4, 500);
UtilsDelayMs(2000);
UtilsDelayMsWD(2000);
prebootmgr_blink_error(ffs_result, 1000);
}
} else {
UtilsDelayMs(1000);
UtilsDelayMsWD(1000);
prebootmgr_blink_error(3, 500);
UtilsDelayMs(2000);
UtilsDelayMsWD(2000);
prebootmgr_blink_error(ffs_result, 1000);
}
}
UtilsDelayMs(2000);
UtilsDelayMsWD(2000);
}

UtilsDelayMs(500);
UtilsDelayMsWD(500);
prebootmgr_blink_error(2, 500);
UtilsDelayMs(500);
UtilsDelayMsWD(500);
prebootmgr_blink_error(ffs_result, 1000);

SlFsFileInfo_t pFsFileInfo;
Expand Down
2 changes: 1 addition & 1 deletion sd-bootloader-ng/bootmanager/sd/revvox/boot/ngCfg.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"hashFile": false,
"watchdog": true,
"ofwFix": true,
"patches": ["uidCheck.305", "blockCheck.308", "noPass.305", "noPrivacy.305", "noChargWake.305"]
"patches": ["blockCheck.308", "noChargWake.305", "noPass.305", "noPrivacy.305", "uidCheck.305"]
},
"cfw1": {
"checkHash": true,
Expand Down
58 changes: 58 additions & 0 deletions sd-bootloader-ng/bootmanager/watchdog.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
#include "watchdog.h"

#include "hw_types.h"
#include "hw_ints.h"
#include "hw_memmap.h"
#include "prcm.h"
#include "wdt.h"
#include "rom.h"
#include "rom_map.h"
#include "interrupt.h"

volatile uint8_t watchdog_feed_state;

void watchdog_feed(void) {
watchdog_feed_state = WATCHDOG_TIMEOUT_S;
}
static void watchdog_unfeed(void) {
watchdog_feed_state = 0;
}
void watchdog_eat(void) {
if (watchdog_feed_state > 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);
}
19 changes: 19 additions & 0 deletions sd-bootloader-ng/bootmanager/watchdog.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#ifndef __WATCHDOG_H__
#define __WATCHDOG_H__
#ifdef __cplusplus
extern "C"
{
#endif
#include <stdint.h>
#include <stdbool.h>

#include "globalDefines.h"

void watchdog_feed(void);
bool watchdog_start(void);
void watchdog_stop(void);

#ifdef __cplusplus
}
#endif
#endif

0 comments on commit 4887535

Please sign in to comment.