From 2399ce19567f4e49936a8c4a8949300d3f8256a9 Mon Sep 17 00:00:00 2001 From: SciLor Date: Wed, 3 Feb 2021 16:16:19 +0100 Subject: [PATCH] Develop (#19) * How to build details * Backmerge (#14) * Own watchdog file, wd delay helper, disable charger wakeup, SdFileExists to helper * MAP_UtilsDelay * Add wiring from Energia for millis() * Extend new config entries (battery/timeout) * Rename SysTickHandler / Rearrange variables * Fix memory length of AppTarget * Optimize buffer * disable running watchdog if one is running (#define) * Make use of SRAM2 region (got overflow) * Remove unneded code * Implement checkBattery/idle Hibernation Rearrange * Replace MAP_UtilsDelay with UtilsDelay again (is too fast?!) * disable opti for relocator * allow some heap * Several attempts to get the watchdog working as it should * Don't move .data around. * fix watchdog, fix Run optimization, WDT recovery seq --- sd-bootloader-ng/bootmanager/Makefile | 276 ++-- sd-bootloader-ng/bootmanager/bootmgr.debug.ld | 12 +- sd-bootloader-ng/bootmanager/bootmgr.h | 21 - sd-bootloader-ng/bootmanager/bootmgr.ld | 8 +- sd-bootloader-ng/bootmanager/config.c | 19 +- sd-bootloader-ng/bootmanager/config.h | 3 + sd-bootloader-ng/bootmanager/globalDefines.h | 5 +- sd-bootloader-ng/bootmanager/helper.c | 3 + sd-bootloader-ng/bootmanager/main.c | 1428 +++++++++-------- .../bootmanager/sd/revvox/boot/ngCfg.json | 4 +- sd-bootloader-ng/bootmanager/startup_gcc.c | 6 +- sd-bootloader-ng/bootmanager/watchdog.c | 38 +- sd-bootloader-ng/bootmanager/watchdog.h | 3 +- sd-bootloader-ng/bootmanager/wiring.c | 153 ++ sd-bootloader-ng/bootmanager/wiring.h | 15 + sd-bootloader-ng/relocator/Makefile | 2 +- 16 files changed, 1125 insertions(+), 871 deletions(-) create mode 100644 sd-bootloader-ng/bootmanager/wiring.c create mode 100644 sd-bootloader-ng/bootmanager/wiring.h diff --git a/sd-bootloader-ng/bootmanager/Makefile b/sd-bootloader-ng/bootmanager/Makefile index 3b624f10..73ce7d43 100755 --- a/sd-bootloader-ng/bootmanager/Makefile +++ b/sd-bootloader-ng/bootmanager/Makefile @@ -1,137 +1,139 @@ -#****************************************************************************** -# -# Makefile - Rules for building the bootmgr application. -# -# Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ -# -# -# Redistribution and use in source and binary forms, with or without -# modification, are permitted provided that the following conditions -# are met: -# -# Redistributions of source code must retain the above copyright -# notice, this list of conditions and the following disclaimer. -# -# Redistributions in binary form must reproduce the above copyright -# notice, this list of conditions and the following disclaimer in the -# documentation and/or other materials provided with the -# distribution. -# -# Neither the name of Texas Instruments Incorporated nor the names of -# its contributors may be used to endorse or promote products derived -# from this software without specific prior written permission. -# -# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -# -#***************************************************************************** - -#VERBOSE=x - -# -# The base directory. -# -ROOT=../.. - -# -# Set optimization level and linkscript -# -#OPT_LEVEL=s -ADD_LINK_SCRIPT=${PWD}/concatBootmanagerRelocator.sh - -ifndef LINK_FILE -LINK_FILE=bootmgr.ld -endif - -# -# Include the common make definitions. -# -include ${ROOT}/common/make/makeCommon - -# -# Where to find source files that do not live in this directory. -# -#VPATH=.. -VPATH+=${SDKROOT}/example/common - -VPATH+=lib/FatFs -VPATH+=lib/jsmn-stream - -# -# Where to find header files that do not live in the source directory. -# -#IPATH=.. -#IPATH+=../../.. -IPATH+=${SDKROOT}/inc -IPATH+=${SDKROOT}/driverlib -IPATH+=${SDKROOT}/simplelink -IPATH+=${SDKROOT}/simplelink/source -IPATH+=${SDKROOT}/simplelink/include -IPATH+=${SDKROOT}/simplelink_extlib/flc -IPATH+=${SDKROOT}/simplelink_extlib/provisioninglib - -IPATH+=lib/FatFs -IPATH+=lib/jsmn-stream - -# -# The default rule, which causes the driver library to be built. -# -all: ${OBJDIR} ${BINDIR} -all: ${BINDIR}/bootmgr.axf - -# -# The rule to clean out all the build products. -# -clean: - @rm -rf ${OBJDIR} ${wildcard *~} - @rm -rf ${BINDIR} ${wildcard *~} - - -# -# The rule to create the target directories. -# -${OBJDIR}: - @mkdir -p ${OBJDIR} - -${BINDIR}: - @mkdir -p ${BINDIR} - -# -# Rules for building the bootmgr example. -# -${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 - -${BINDIR}/bootmgr.axf: ${OBJDIR}/ff.o -${BINDIR}/bootmgr.axf: ${OBJDIR}/diskio.o -${BINDIR}/bootmgr.axf: ${OBJDIR}/jsmn_stream.o - -${BINDIR}/bootmgr.axf: ${OBJDIR}/ccsbcs.o -${BINDIR}/bootmgr.axf: ${OBJDIR}/network_common.o -${BINDIR}/bootmgr.axf: ${OBJDIR}/startup_${COMPILER}.o -${BINDIR}/bootmgr.axf: ${SDKROOT}/simplelink/${COMPILER}/${BINDIR}/libsimplelink_nonos_opt.a -${BINDIR}/bootmgr.axf: ${SDKROOT}/driverlib/${COMPILER}/${BINDIR}/libdriver.a - -SCATTERgcc_bootmgr=${LINK_FILE} -ENTRY_bootmgr=ResetISR - -# -# Include the automatically generated dependency files. -# -ifneq (${MAKECMDGOALS},clean) --include ${wildcard ${COMPILER}/*.d} __dummy__ -endif +#****************************************************************************** +# +# Makefile - Rules for building the bootmgr application. +# +# Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ +# +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# +# Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in the +# documentation and/or other materials provided with the +# distribution. +# +# Neither the name of Texas Instruments Incorporated nor the names of +# its contributors may be used to endorse or promote products derived +# from this software without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# +#***************************************************************************** + +#VERBOSE=x + +# +# The base directory. +# +ROOT=../.. + +# +# Set optimization level and linkscript +# +#OPT_LEVEL=s +ADD_LINK_SCRIPT=${PWD}/concatBootmanagerRelocator.sh + +ifndef LINK_FILE +LINK_FILE=bootmgr.ld +endif + +# +# Include the common make definitions. +# +include ${ROOT}/common/make/makeCommon + +# +# Where to find source files that do not live in this directory. +# +#VPATH=.. +VPATH+=${SDKROOT}/example/common + +VPATH+=lib/FatFs +VPATH+=lib/jsmn-stream + +# +# Where to find header files that do not live in the source directory. +# +#IPATH=.. +#IPATH+=../../.. +IPATH+=${SDKROOT}/inc +IPATH+=${SDKROOT}/driverlib +IPATH+=${SDKROOT}/simplelink +IPATH+=${SDKROOT}/simplelink/source +IPATH+=${SDKROOT}/simplelink/include +IPATH+=${SDKROOT}/simplelink_extlib/flc +IPATH+=${SDKROOT}/simplelink_extlib/provisioninglib + +IPATH+=lib/FatFs +IPATH+=lib/jsmn-stream + +# +# The default rule, which causes the driver library to be built. +# +all: ${OBJDIR} ${BINDIR} +all: ${BINDIR}/bootmgr.axf + +# +# The rule to clean out all the build products. +# +clean: + @rm -rf ${OBJDIR} ${wildcard *~} + @rm -rf ${BINDIR} ${wildcard *~} + + +# +# The rule to create the target directories. +# +${OBJDIR}: + @mkdir -p ${OBJDIR} + +${BINDIR}: + @mkdir -p ${BINDIR} + +# +# Rules for building the bootmgr example. +# +${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}/wiring.o + +${BINDIR}/bootmgr.axf: ${OBJDIR}/udma_if.o + +${BINDIR}/bootmgr.axf: ${OBJDIR}/ff.o +${BINDIR}/bootmgr.axf: ${OBJDIR}/diskio.o +${BINDIR}/bootmgr.axf: ${OBJDIR}/jsmn_stream.o + +${BINDIR}/bootmgr.axf: ${OBJDIR}/ccsbcs.o +${BINDIR}/bootmgr.axf: ${OBJDIR}/network_common.o +${BINDIR}/bootmgr.axf: ${OBJDIR}/startup_${COMPILER}.o +${BINDIR}/bootmgr.axf: ${SDKROOT}/simplelink/${COMPILER}/${BINDIR}/libsimplelink_nonos_opt.a +${BINDIR}/bootmgr.axf: ${SDKROOT}/driverlib/${COMPILER}/${BINDIR}/libdriver.a + +SCATTERgcc_bootmgr=${LINK_FILE} +ENTRY_bootmgr=ResetISR + +# +# Include the automatically generated dependency files. +# +ifneq (${MAKECMDGOALS},clean) +-include ${wildcard ${COMPILER}/*.d} __dummy__ +endif diff --git a/sd-bootloader-ng/bootmanager/bootmgr.debug.ld b/sd-bootloader-ng/bootmanager/bootmgr.debug.ld index 0239241e..d3e15dbb 100755 --- a/sd-bootloader-ng/bootmanager/bootmgr.debug.ld +++ b/sd-bootloader-ng/bootmanager/bootmgr.debug.ld @@ -37,13 +37,13 @@ ******************************************************************************/ -HEAP_SIZE = 0x00000000; +HEAP_SIZE = 0x00000200; MEMORY { /*SRAM (rwx) : ORIGIN = 0x20038000, LENGTH = 0x8000*/ SRAM (rwx) : ORIGIN = 0x20034000, LENGTH = 0x12000 - APPTARGET (rwx) : ORIGIN = 0x20004000, LENGTH = 0x34000 + APPTARGET (rwx) : ORIGIN = 0x20004000, LENGTH = 0x30000 SRAM2 (rwx) : ORIGIN = 0x20000000, LENGTH = 0x4000 } @@ -71,9 +71,9 @@ SECTIONS __exidx_end = .; } > SRAM - __init_data = .; - - .data : AT(__init_data) + /*__init_data = .;*/ + + .data : /*AT(__init_data)*/ { _data = .; *(.data*) @@ -96,6 +96,6 @@ SECTIONS . = ALIGN(8); _eheap = .; - } + } > SRAM2 } diff --git a/sd-bootloader-ng/bootmanager/bootmgr.h b/sd-bootloader-ng/bootmanager/bootmgr.h index 6110718c..7c6f2c1e 100755 --- a/sd-bootloader-ng/bootmanager/bootmgr.h +++ b/sd-bootloader-ng/bootmanager/bootmgr.h @@ -61,27 +61,6 @@ extern "C" #define USER_IMG_2_TOKEN 0x55AAAA55 #define USER_BOOT_INFO_TOKEN 0xA5A55A5A - - -//***************************************************************************** -// Macros -//***************************************************************************** -#ifndef FAST_BOOT -#define APP_IMG_SRAM_OFFSET 0x20004000 -#else -#define APP_IMG_SRAM_OFFSET 0x20008000 -#endif - -#define DEVICE_IS_CC3101RS 0x18 -#define DEVICE_IS_CC3101S 0x1B - - -//***************************************************************************** -// Fucntion prototype -//***************************************************************************** -extern void Run(unsigned long); - - //**************************************************************************** // // Mark the end of the C bindings section for C++ compilers. diff --git a/sd-bootloader-ng/bootmanager/bootmgr.ld b/sd-bootloader-ng/bootmanager/bootmgr.ld index 6e230796..7b27db7b 100755 --- a/sd-bootloader-ng/bootmanager/bootmgr.ld +++ b/sd-bootloader-ng/bootmanager/bootmgr.ld @@ -37,7 +37,7 @@ ******************************************************************************/ -HEAP_SIZE = 0x00000000; +HEAP_SIZE = 0x00000200; MEMORY { @@ -70,9 +70,9 @@ SECTIONS __exidx_end = .; } > SRAM - __init_data = .; + /*__init_data = .;*/ - .data : AT(__init_data) + .data : /*AT(__init_data)*/ { _data = .; *(.data*) @@ -95,6 +95,6 @@ SECTIONS . = ALIGN(8); _eheap = .; - } + } > SRAM2 } diff --git a/sd-bootloader-ng/bootmanager/config.c b/sd-bootloader-ng/bootmanager/config.c index ab19777b..2c1d04f1 100644 --- a/sd-bootloader-ng/bootmanager/config.c +++ b/sd-bootloader-ng/bootmanager/config.c @@ -1,6 +1,13 @@ #include "config.h" +#include -sGeneralSettings Config_generalSettings = {0, false}; +sGeneralSettings Config_generalSettings = { + 0, //activeImage + false, //waitForPress + 60, //waitTimeoutInS + 2100, //minBatteryLevel (Divide through around 700 to get voltage, so 3V should be save) + false, //Serial Log +}; sImageInfo Config_imageInfos[IMG_MAX_COUNT]; static jsmn_stream_parser parser; @@ -98,6 +105,12 @@ static void jsmn_primitive(const char *value, size_t len, void *user_arg) { if (strcmp("general", jsonGroupName) == 0) { if (strcmp("waitForPress", jsonValueName) == 0) { Config_generalSettings.waitForPress = (value[0] == 't'); + } else if (strcmp("waitTimeoutInS", jsonValueName) == 0) { + Config_generalSettings.waitTimeoutInS = (uint16_t)strtoul(value, NULL, 0); + } else if (strcmp("minBatteryLevel", jsonValueName) == 0) { + Config_generalSettings.minBatteryLevel = (uint16_t)strtoul(value, NULL, 0); + } else if (strcmp("serialLog", jsonValueName) == 0) { + Config_generalSettings.serialLog = (value[0] == 't'); } } else if (strncmp(jsonGroupName, "ofw", 3) == 0 || strncmp(jsonGroupName, "cfw", 3) @@ -154,14 +167,14 @@ void Config_ReadJsonCfg(void) { uint32_t allBytesRead = 0; jsmn_stream_init(&parser, &cbs, NULL); - char buffer[128]; + char buffer[512]; while (allBytesRead +#include "rom.h" +#include "rom_map.h" #include "watchdog.h" #include "ff.h" void UtilsDelayUs(unsigned long delayUs) { + //Using the chip integrated via MAP will get too fast?! UtilsDelay(UTILS_DELAY_US_TO_COUNT(delayUs)); } void UtilsDelayMs(unsigned long delayMs) { diff --git a/sd-bootloader-ng/bootmanager/main.c b/sd-bootloader-ng/bootmanager/main.c index 75e9237a..a9823974 100755 --- a/sd-bootloader-ng/bootmanager/main.c +++ b/sd-bootloader-ng/bootmanager/main.c @@ -1,687 +1,743 @@ -//***************************************************************************** -// -// Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ -// -// -// Redistribution and use in source and binary forms, with or without -// modification, are permitted provided that the following conditions -// are met: -// -// Redistributions of source code must retain the above copyright -// notice, this list of conditions and the following disclaimer. -// -// Redistributions in binary form must reproduce the above copyright -// notice, this list of conditions and the following disclaimer in the -// documentation and/or other materials provided with the -// distribution. -// -// Neither the name of Texas Instruments Incorporated nor the names of -// its contributors may be used to endorse or promote products derived -// from this software without specific prior written permission. -// -// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS -// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT -// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR -// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT -// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT -// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, -// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY -// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT -// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -// -//***************************************************************************** - -//***************************************************************************** -// -// Application Name - Application Bootloader -// Application Overview - This application showcases the secondary bootloader operations to manage -// updates to application image. -// -//***************************************************************************** - -//**************************************************************************** -// -//! \addtogroup application_bootloader -//! @{ -// -//**************************************************************************** - -#include -#include - -#include "hw_ints.h" -#include "hw_types.h" -#include "hw_memmap.h" -#include "hw_gprcm.h" -#include "hw_common_reg.h" -#include "hw_nvic.h" -#include "hw_shamd5.h" -#include "hw_dthe.h" -#include "hw_adc.h" -#include "rom.h" -#include "rom_map.h" -#include "prcm.h" -#include "simplelink.h" -#include "interrupt.h" -#include "gpio.h" -#include "pin.h" -#include "udma_if.h" -#include "flc.h" -#include "bootmgr.h" -#include "shamd5.h" -#include "adc.h" - -#include "sdhost.h" //TODO: fixes some compiler warnings, even diskio.h should load it! - -#include "ff.h" -#include "diskio.h" - -static FATFS fatfs; - -#include "jsmn_stream.h" - -#include "config.h" -#include "patch.h" -#include "globalDefines.h" -#include "watchdog.h" - - -char imagePath[] = IMG_SD_PATH; //TODO! -static char* GetImagePathById(uint8_t number) { - //char* imagePath; - char id = (char)((number%3) + 0x31); //See Ascii Table - 1 starts at 0x31 - char* name; - - if (number < 3) { - name = IMG_OFW_NAME; - } else if (number < 6) { - name = IMG_CFW_NAME; - } else /*if (number < 9)*/ { - name = IMG_ADD_NAME; - } - - for (uint8_t i=0; i<3; i++) - { - imagePath[IMG_SD_PATH_REPL1_POS+i] = name[i]; - } - imagePath[IMG_SD_PATH_REPL2_POS] = id; - - return imagePath; -} -//***************************************************************************** -// Vector Table -extern void (*const g_pfnVectors[])(void); - -//***************************************************************************** -// -//! Board Initialization & Configuration -//! -//! \param None -//! -//! \return None -// -//***************************************************************************** -static void -BoardInitBase(void) -{ - - // - // Set vector table base - // - MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]); - - // - // Enable Processor Interrupts - // - IntMasterEnable(); - IntEnable(FAULT_SYSTICK); - - // - // Mandatory MCU Initialization - // - PRCMCC3200MCUInit(); -} - -//***************************************************************************** -// -//! Executed the application from given location -//! -//! \param ulBaseLoc is the base address of the application -//! -//! This function execution the application loaded at \e ulBaseLoc. It assumes -//! the vector table is placed at the base address thus sets the new Stack -//! pointer from the first word. -//! -//! \return None. -// -//***************************************************************************** -void Run(unsigned long ulBaseLoc) -{ - - // - // Set the SP - // - __asm(" ldr sp,[r0]\n" - " add r0,r0,#4"); //Get address of ResetISR from .intvecs - - // - // Jump to entry code - // - __asm(" ldr r1,[r0]\n" - " bx r1"); -} - - - - -static void LedGreenOn() { - MAP_GPIOPinWrite(LED_GREEN_PORT, LED_GREEN_PORT_MASK, LED_GREEN_PORT_MASK); -} -static void LedGreenOff() { - MAP_GPIOPinWrite(LED_GREEN_PORT, LED_GREEN_PORT_MASK, 0x00); -} -static void LedBlueOn() { - MAP_GPIOPinWrite(LED_BLUE_PORT, LED_BLUE_PORT_MASK, LED_BLUE_PORT_MASK); -} -static void LedBlueOff() { - MAP_GPIOPinWrite(LED_BLUE_PORT, LED_BLUE_PORT_MASK, 0x00); -} - -static void LedSet(uint8_t color) { - if (color & COLOR_GREEN) { - LedGreenOn(); - } else { - LedGreenOff(); - } - if (color & COLOR_BLUE) { - LedBlueOn(); - } else { - LedBlueOff(); - } -} -static void LedOn(uint8_t color) { - if (color & COLOR_GREEN) { - LedGreenOn(); - } if (color & COLOR_BLUE) { - LedBlueOn(); - } -} -static void LedOff(uint8_t color) { - if (color & COLOR_GREEN) { - LedGreenOff(); - } if (color & COLOR_BLUE) { - LedBlueOff(); - } -} - -static void prebootmgr_blink_color(int times, int wait_ms, uint8_t color) -{ - for (int i = 0; i < times; i++) - { - LedSet(color); - UtilsDelayMsWD(wait_ms); - LedSet(COLOR_BLACK); - UtilsDelayMsWD(wait_ms); - } -} - -static void prebootmgr_blink(int times, int wait_ms) { - prebootmgr_blink_color(times, wait_ms, COLOR_GREEN); -} - -static void prebootmgr_blink_error(int times, int wait_ms) { - #ifdef FIXED_BOOT_IMAGE - prebootmgr_blink_color(times, wait_ms, COLOR_BLUE); - #else - prebootmgr_blink_color(times, wait_ms, COLOR_GREEN); - #endif -} - -static void SdInit(void) -{ - //Power SD - MAP_PinTypeGPIO(POWER_SD_PIN_NUM, PIN_MODE_0, false); - MAP_GPIODirModeSet(POWER_SD_PORT, POWER_SD_PORT_MASK, GPIO_DIR_MODE_OUT); - - MAP_PinTypeSDHost(PIN_64, PIN_MODE_6); //SDHost_D0 - MAP_PinTypeSDHost(PIN_01, PIN_MODE_6); //SDHost_CLK - MAP_PinTypeSDHost(PIN_02, PIN_MODE_6); //SDHost_CMD - - MAP_GPIOPinWrite(POWER_SD_PORT, POWER_SD_PORT_MASK, 0x00); //SIC! - - // Set the SD card clock as output pin - MAP_PinDirModeSet(PIN_01, PIN_DIR_MODE_OUT); - // Enable Pull up on data - MAP_PinConfigSet(PIN_64, PIN_STRENGTH_4MA, PIN_TYPE_STD_PU); - // Enable Pull up on CMD - MAP_PinConfigSet(PIN_02, PIN_STRENGTH_4MA, PIN_TYPE_STD_PU); - - // Enable SD peripheral clock - MAP_PRCMPeripheralClkEnable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); - // Reset MMCHS - MAP_PRCMPeripheralReset(PRCM_SDHOST); - // Configure MMCHS - MAP_SDHostInit(SDHOST_BASE); - // Configure card clock - MAP_SDHostSetExpClk(SDHOST_BASE, MAP_PRCMPeripheralClockGet(PRCM_SDHOST), 15000000); - MAP_SDHostBlockSizeSet(SDHOST_BASE, 512); //SD_SECTOR_SIZE - -} - -static void BoardInitCustom(void) -{ - MAP_PRCMPeripheralClkEnable(PRCM_GPIOA0, PRCM_RUN_MODE_CLK); //Clock for GPIOA0 (Ear Buttons / SD Power / Power) - MAP_PRCMPeripheralClkEnable(PRCM_GPIOA2, PRCM_RUN_MODE_CLK); //Clock for GPIOAA (Charger) - MAP_PRCMPeripheralClkEnable(PRCM_GPIOA3, PRCM_RUN_MODE_CLK); //Clock for GPIOA3 (Green/Blue LED) - - //MAP_PRCMPeripheralClkEnable(PRCM_UARTA0, PRCM_RUN_MODE_CLK); - //MAP_PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK); - //MAP_PRCMPeripheralClkEnable(PRCM_GSPI, PRCM_RUN_MODE_CLK); - - //Green LED - MAP_PinTypeGPIO(LED_GREEN_PIN_NUM, PIN_MODE_0, false); - MAP_GPIODirModeSet(LED_GREEN_PORT, LED_GREEN_PORT_MASK, GPIO_DIR_MODE_OUT); - //Blue LED - MAP_PinTypeGPIO(LED_BLUE_PIN_NUM, PIN_MODE_0, false); - MAP_GPIODirModeSet(LED_BLUE_PORT, LED_BLUE_PORT_MASK, GPIO_DIR_MODE_OUT); - - //Big Ear - MAP_PinTypeGPIO(EAR_BIG_PIN_NUM, PIN_MODE_0, false); - MAP_GPIODirModeSet(EAR_BIG_PORT, EAR_BIG_PORT_MASK, GPIO_DIR_MODE_IN); - //Small Ear - MAP_PinTypeGPIO(EAR_SMALL_PIN_NUM, PIN_MODE_0, false); - MAP_GPIODirModeSet(EAR_SMALL_PORT, EAR_SMALL_PORT_MASK, GPIO_DIR_MODE_IN); - - //Charger - MAP_PinTypeGPIO(CHARGER_PIN_NUM, PIN_MODE_0, false); - MAP_GPIODirModeSet(CHARGER_PORT, CHARGER_PORT_MASK, GPIO_DIR_MODE_IN); - - //Power other peripherals - MAP_PinTypeGPIO(POWER_PIN_NUM, PIN_MODE_0, false); - MAP_GPIODirModeSet(POWER_PORT, POWER_PORT_MASK, GPIO_DIR_MODE_OUT); - MAP_GPIOPinWrite(POWER_PORT, POWER_PORT_MASK, POWER_PORT_MASK); - - //Init SHAMD5 - MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); - MAP_PRCMPeripheralReset(PRCM_DTHE); - - SdInit(); -} -static void BoardDeinitCustom(void) -{ - watchdog_feed(); - //Power off SD - 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) { - return !(EAR_SMALL_PORT_MASK & MAP_GPIOPinRead(EAR_SMALL_PORT, EAR_SMALL_PORT_MASK)); -} -static volatile bool EarBigPressed(void) { - return !(EAR_BIG_PORT_MASK & MAP_GPIOPinRead(EAR_BIG_PORT, EAR_BIG_PORT_MASK)); -} - -//#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) { - int8_t counter = startNumber; - - while (!Config_imageInfos[counter].fileExists) - { - counter = (counter+1) % COUNT_OF(Config_imageInfos); - } - - LedSet(COLOR_GREEN); - while (EarSmallPressed()) { - UtilsDelayMsWD(10); //Wait while pressed - watchdog_feed(); - } - - uint8_t colors[] = { COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN }; - uint8_t curColorId = 0; - if (Config_generalSettings.waitForPress) { - LedSet(COLOR_BLUE); - while (EarSmallPressed() || EarBigPressed()) { - UtilsDelayMsWD(10); //Wait while pressed - watchdog_feed(); - } - } - while (Config_generalSettings.waitForPress) - { - LedSet(colors[curColorId]); - UtilsDelayMsWD(250); - - if (curColorId> 2 ) & 0x0FFF; -} -static bool isChargerConnected() -{ - return (CHARGER_PORT_MASK & MAP_GPIOPinRead(CHARGER_PORT, CHARGER_PORT_MASK)); -} - -static void hibernate() { - watchdog_stop(); - BoardDeinitCustom(); - - PRCMHibernateWakeupSourceEnable(PRCM_HIB_GPIO2 | PRCM_HIB_GPIO4); //enable ear wakeup interrupt - PRCMHibernateEnter(); -} - -int main() -{ - - sBootInfo_t sBootInfo; - FIL ffile; - uint8_t ffs_result; - - - BoardInitBase(); - BoardInitCustom(); - watchdog_start(); -/* - uint16_t battery; - bool charger ; - battery = getBatteryLevel(); - charger = isChargerConnected(); - */ - if (PRCM_WDT_RESET == MAP_PRCMSysResetCauseGet()) { - prebootmgr_blink_error(5, 33); - prebootmgr_blink_error(5, 66); - prebootmgr_blink_error(5, 33); - - hibernate(); - } - - #ifndef FIXED_BOOT_IMAGE - Config_InitImageInfos(); - #endif - - UtilsDelayMsWD(100); - ffs_result = f_mount(&fatfs, "0", 1); - if (ffs_result == FR_OK) { - #ifdef FIXED_BOOT_IMAGE - char* image = IMG_SD_BOOTLOADER_PATH; - if (SdFileExists(image)) { - #else - if (CheckSdImages()) { - Config_ReadJsonCfg(); - - retrySelection: - Config_generalSettings.activeImage = Selector(Config_generalSettings.activeImage); - - uint8_t selectedImgNum = Config_generalSettings.activeImage; - char* image = GetImagePathById(selectedImgNum); - #endif - - ffs_result = f_open(&ffile, image, FA_READ); - if (ffs_result == FR_OK) { - uint32_t filesize = f_size(&ffile); - - char* pImgRun = (char*)APP_IMG_SRAM_OFFSET; - ffs_result = f_read(&ffile, pImgRun, filesize, &filesize); - if (ffs_result == FR_OK) { - f_close(&ffile); - - #ifndef FIXED_BOOT_IMAGE - if (Config_imageInfos[selectedImgNum].checkHash) { - char hashExp[65]; - char hashAct[65]; - uint8_t hashActRaw[32]; - - hashExp[64] = '\0'; - hashAct[64] = '\0'; - - if (Config_imageInfos[selectedImgNum].hashFile) { - char* shaFile = HASH_SD_PATH; - memcpy(shaFile+IMG_SD_PATH_REPL1_POS, image+IMG_SD_PATH_REPL1_POS, 4); - ffs_result = f_open(&ffile, shaFile, FA_READ); - if (ffs_result == FR_OK) { - ffs_result = f_read(&ffile, hashExp, 64, NULL); - if (ffs_result == FR_OK) { - - } else { - //TODO - } - } else { - //TODO - } - } else { - filesize -= 64; //sha256 ist 64bytes long. - memcpy(hashExp, (char*)(pImgRun + filesize), 64); - } - - MAP_SHAMD5ConfigSet(SHAMD5_BASE, SHAMD5_ALGO_SHA256); - MAP_SHAMD5DataProcess(SHAMD5_BASE, pImgRun, filesize, hashActRaw); - btox(hashAct, hashActRaw, 64); - - if (strncmp(hashAct, hashExp, 64) != 0) { - //ERROR - - prebootmgr_blink_error(10, 50); - - Config_generalSettings.waitForPress = true; - goto retrySelection; - } - } - if (Config_imageInfos[selectedImgNum].ofwFix) { - uint32_t* pCheck1 = (uint32_t*)(pImgRun+filesize-0x04); - uint32_t* pCheck2 = (uint32_t*)(pImgRun+filesize-0x04-0x6c); - uint32_t* pTarget = (uint32_t*)(pImgRun+filesize-0x04-0x04); - - if (*pCheck1 == 0xBEAC0005 && *pCheck1 == *pCheck2) { - *pTarget = 0x0010014C; - } - } - - for (uint8_t i=0; ilength > 0) { - bool doPatch = false; - uint32_t offset = 0; - for (offset=0; offsetlength; offset++) { - if (patch->searchMask[0] == 0x00) - continue; - if (patch->search[0] != pImgRun[offset]) - continue; - - uint32_t offset2; - for (offset2=1; offset2length; offset2++) { - if (patch->searchMask[offset2] == 0x00) - continue; - if (patch->search[offset2] != pImgRun[offset+offset2]) { - offset2 = 0; - break; - } - } - if (offset2 == patch->length) { - doPatch = true; - break; - } - } - if (doPatch) { - for (uint32_t replaceOffset=0; replaceOffsetlength; replaceOffset++) { - if (patch->replaceMask[replaceOffset] == 0x00) - continue; - pImgRun[offset+replaceOffset] = patch->replace[replaceOffset]; - } - } - } - } - if (!Config_imageInfos[selectedImgNum].watchdog) - watchdog_stop(); - #endif - - BoardDeinitCustom(); - Run((unsigned long)pImgRun); - } else { - UtilsDelayMsWD(1000); - prebootmgr_blink_error(4, 500); - UtilsDelayMsWD(2000); - prebootmgr_blink_error(ffs_result, 1000); - } - } else { - UtilsDelayMsWD(1000); - prebootmgr_blink_error(3, 500); - UtilsDelayMsWD(2000); - prebootmgr_blink_error(ffs_result, 1000); - } - } - UtilsDelayMsWD(2000); - } - - UtilsDelayMsWD(500); - prebootmgr_blink_error(2, 500); - UtilsDelayMsWD(500); - prebootmgr_blink_error(ffs_result, 1000); - - SlFsFileInfo_t pFsFileInfo; - _i32 fhandle; - sl_Start(NULL, NULL, NULL); //for reading flash - if (!sl_FsOpen(IMG_FLASH_PATH, FS_MODE_OPEN_READ, NULL, &fhandle)) { - if (!sl_FsGetInfo(IMG_FLASH_PATH, 0, &pFsFileInfo)) { - if (pFsFileInfo.FileLen == sl_FsRead(fhandle, 0, (unsigned char *)APP_IMG_SRAM_OFFSET, pFsFileInfo.FileLen)) { - sl_FsClose(fhandle, 0, 0, 0); - sl_Stop(30); - BoardDeinitCustom(); - Run(APP_IMG_SRAM_OFFSET); - } - } - } - - prebootmgr_blink_error(3, 33); - prebootmgr_blink_error(3, 66); - prebootmgr_blink_error(3, 33); - - sl_Stop(30); - - hibernate(); -} -//***************************************************************************** -// WLAN Event handler callback hookup function -//***************************************************************************** -void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent) -{ -} - -//***************************************************************************** -// -//! \brief This function handles General Events -//! -//! \param[in] pDevEvent - Pointer to General Event Info -//! -//! \return None -//! -//***************************************************************************** -void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent) -{ -} - -//***************************************************************************** -// HTTP Server callback hookup function -//***************************************************************************** -void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pHttpEvent, - SlHttpServerResponse_t *pHttpResponse) -{ -} - -//***************************************************************************** -// Net APP Event callback hookup function -//***************************************************************************** -void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent) -{ -} - -//***************************************************************************** -// -//! This function handles socket events indication -//! -//! \param[in] pSock - Pointer to Socket Event Info -//! -//! \return None -//! -//***************************************************************************** -void SimpleLinkSockEventHandler(SlSockEvent_t *pSock) -{ +//***************************************************************************** +// +// Copyright (C) 2014 Texas Instruments Incorporated - http://www.ti.com/ +// +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions +// are met: +// +// Redistributions of source code must retain the above copyright +// notice, this list of conditions and the following disclaimer. +// +// Redistributions in binary form must reproduce the above copyright +// notice, this list of conditions and the following disclaimer in the +// documentation and/or other materials provided with the +// distribution. +// +// Neither the name of Texas Instruments Incorporated nor the names of +// its contributors may be used to endorse or promote products derived +// from this software without specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR +// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT +// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT +// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY +// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT +// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +// +//***************************************************************************** + +//***************************************************************************** +// +// Application Name - Application Bootloader +// Application Overview - This application showcases the secondary bootloader operations to manage +// updates to application image. +// +//***************************************************************************** + +//**************************************************************************** +// +//! \addtogroup application_bootloader +//! @{ +// +//**************************************************************************** + +#include +#include + +#include "hw_ints.h" +#include "hw_types.h" +#include "hw_memmap.h" +#include "hw_gprcm.h" +#include "hw_common_reg.h" +#include "hw_nvic.h" +#include "hw_shamd5.h" +#include "hw_dthe.h" +#include "hw_adc.h" +#include "rom.h" +#include "rom_map.h" +#include "prcm.h" +#include "simplelink.h" +#include "interrupt.h" +#include "gpio.h" +#include "pin.h" +#include "udma_if.h" +#include "flc.h" +#include "bootmgr.h" +#include "shamd5.h" +#include "adc.h" + +#include "sdhost.h" //TODO: fixes some compiler warnings, even diskio.h should load it! + +#include "ff.h" +#include "diskio.h" + +static FATFS fatfs; + +#include "jsmn_stream.h" + +#include "config.h" +#include "patch.h" +#include "globalDefines.h" +#include "watchdog.h" + +#include "wiring.h" + + +static char imagePath[] = IMG_SD_PATH; +//***************************************************************************** +// Vector Table +extern void (*const g_pfnVectors[])(void); + +//***************************************************************************** +// +//! Board Initialization & Configuration +//! +//! \param None +//! +//! \return None +// +//***************************************************************************** +static void +BoardInitBase(void) +{ + + // + // Set vector table base + // + MAP_IntVTableBaseSet((unsigned long)&g_pfnVectors[0]); + + // + // Enable Processor Interrupts + // + IntMasterEnable(); + IntEnable(FAULT_SYSTICK); + + // + // Mandatory MCU Initialization + // + PRCMCC3200MCUInit(); +} + +//***************************************************************************** +// +//! Executed the application from given location +//! +//! \param ulBaseLoc is the base address of the application +//! +//! This function execution the application loaded at \e ulBaseLoc. It assumes +//! the vector table is placed at the base address thus sets the new Stack +//! pointer from the first word. +//! +//! \return None. +// +//***************************************************************************** +#pragma GCC push_options +#pragma GCC optimize ("O0") //Don't optimize or it may fail +static void Run(unsigned long ulBaseLoc) +{ + + // + // Set the SP + // + __asm(" ldr sp,[r0]\n" + " add r0,r0,#4"); //Get address of ResetISR from .intvecs + + // + // Jump to entry code + // + __asm(" ldr r1,[r0]\n" + " bx r1"); +} +#pragma GCC pop_options + + + + +static void LedGreenOn() { + MAP_GPIOPinWrite(LED_GREEN_PORT, LED_GREEN_PORT_MASK, LED_GREEN_PORT_MASK); +} +static void LedGreenOff() { + MAP_GPIOPinWrite(LED_GREEN_PORT, LED_GREEN_PORT_MASK, 0x00); +} +static void LedBlueOn() { + MAP_GPIOPinWrite(LED_BLUE_PORT, LED_BLUE_PORT_MASK, LED_BLUE_PORT_MASK); +} +static void LedBlueOff() { + MAP_GPIOPinWrite(LED_BLUE_PORT, LED_BLUE_PORT_MASK, 0x00); +} + +static void LedSet(uint8_t color) { + if (color & COLOR_GREEN) { + LedGreenOn(); + } else { + LedGreenOff(); + } + if (color & COLOR_BLUE) { + LedBlueOn(); + } else { + LedBlueOff(); + } +} +static void LedOn(uint8_t color) { + if (color & COLOR_GREEN) { + LedGreenOn(); + } if (color & COLOR_BLUE) { + LedBlueOn(); + } +} +static void LedOff(uint8_t color) { + if (color & COLOR_GREEN) { + LedGreenOff(); + } if (color & COLOR_BLUE) { + LedBlueOff(); + } +} + +static void prebootmgr_blink_color(int times, int wait_ms, uint8_t color) +{ + for (int i = 0; i < times; i++) + { + LedSet(color); + UtilsDelayMsWD(wait_ms); + LedSet(COLOR_BLACK); + UtilsDelayMsWD(wait_ms); + } +} + +static void prebootmgr_blink(int times, int wait_ms) { + prebootmgr_blink_color(times, wait_ms, COLOR_GREEN); +} + +static void prebootmgr_blink_error(int times, int wait_ms) { + #ifdef FIXED_BOOT_IMAGE + prebootmgr_blink_color(times, wait_ms, COLOR_BLUE); + #else + prebootmgr_blink_color(times, wait_ms, COLOR_GREEN); + #endif +} + +static void SdInit(void) +{ + //Power SD + MAP_PinTypeGPIO(POWER_SD_PIN_NUM, PIN_MODE_0, false); + MAP_GPIODirModeSet(POWER_SD_PORT, POWER_SD_PORT_MASK, GPIO_DIR_MODE_OUT); + + MAP_PinTypeSDHost(PIN_64, PIN_MODE_6); //SDHost_D0 + MAP_PinTypeSDHost(PIN_01, PIN_MODE_6); //SDHost_CLK + MAP_PinTypeSDHost(PIN_02, PIN_MODE_6); //SDHost_CMD + + MAP_GPIOPinWrite(POWER_SD_PORT, POWER_SD_PORT_MASK, 0x00); //SIC! + + // Set the SD card clock as output pin + MAP_PinDirModeSet(PIN_01, PIN_DIR_MODE_OUT); + // Enable Pull up on data + MAP_PinConfigSet(PIN_64, PIN_STRENGTH_4MA, PIN_TYPE_STD_PU); + // Enable Pull up on CMD + MAP_PinConfigSet(PIN_02, PIN_STRENGTH_4MA, PIN_TYPE_STD_PU); + + // Enable SD peripheral clock + MAP_PRCMPeripheralClkEnable(PRCM_SDHOST, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); + // Reset MMCHS + MAP_PRCMPeripheralReset(PRCM_SDHOST); + // Configure MMCHS + MAP_SDHostInit(SDHOST_BASE); + // Configure card clock + MAP_SDHostSetExpClk(SDHOST_BASE, MAP_PRCMPeripheralClockGet(PRCM_SDHOST), 15000000); + MAP_SDHostBlockSizeSet(SDHOST_BASE, 512); //SD_SECTOR_SIZE + +} + +static void BoardInitCustom(void) +{ + MAP_PRCMPeripheralClkEnable(PRCM_GPIOA0, PRCM_RUN_MODE_CLK); //Clock for GPIOA0 (Ear Buttons / SD Power / Power) + MAP_PRCMPeripheralClkEnable(PRCM_GPIOA2, PRCM_RUN_MODE_CLK); //Clock for GPIOAA (Charger) + MAP_PRCMPeripheralClkEnable(PRCM_GPIOA3, PRCM_RUN_MODE_CLK); //Clock for GPIOA3 (Green/Blue LED) + + //MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK); + + //MAP_PRCMPeripheralClkEnable(PRCM_UARTA0, PRCM_RUN_MODE_CLK); + //MAP_PRCMPeripheralClkEnable(PRCM_I2CA0, PRCM_RUN_MODE_CLK); + //MAP_PRCMPeripheralClkEnable(PRCM_GSPI, PRCM_RUN_MODE_CLK); + + //Green LED + MAP_PinTypeGPIO(LED_GREEN_PIN_NUM, PIN_MODE_0, false); + MAP_GPIODirModeSet(LED_GREEN_PORT, LED_GREEN_PORT_MASK, GPIO_DIR_MODE_OUT); + //Blue LED + MAP_PinTypeGPIO(LED_BLUE_PIN_NUM, PIN_MODE_0, false); + MAP_GPIODirModeSet(LED_BLUE_PORT, LED_BLUE_PORT_MASK, GPIO_DIR_MODE_OUT); + + //Big Ear + MAP_PinTypeGPIO(EAR_BIG_PIN_NUM, PIN_MODE_0, false); + MAP_GPIODirModeSet(EAR_BIG_PORT, EAR_BIG_PORT_MASK, GPIO_DIR_MODE_IN); + //Small Ear + MAP_PinTypeGPIO(EAR_SMALL_PIN_NUM, PIN_MODE_0, false); + MAP_GPIODirModeSet(EAR_SMALL_PORT, EAR_SMALL_PORT_MASK, GPIO_DIR_MODE_IN); + + //Charger + MAP_PinTypeGPIO(CHARGER_PIN_NUM, PIN_MODE_0, false); + MAP_GPIODirModeSet(CHARGER_PORT, CHARGER_PORT_MASK, GPIO_DIR_MODE_IN); + + //Power other peripherals + MAP_PinTypeGPIO(POWER_PIN_NUM, PIN_MODE_0, false); + MAP_GPIODirModeSet(POWER_PORT, POWER_PORT_MASK, GPIO_DIR_MODE_OUT); + MAP_GPIOPinWrite(POWER_PORT, POWER_PORT_MASK, POWER_PORT_MASK); + + //Init SHAMD5 + MAP_PRCMPeripheralClkEnable(PRCM_DTHE, PRCM_RUN_MODE_CLK | PRCM_SLP_MODE_CLK); + MAP_PRCMPeripheralReset(PRCM_DTHE); + + SdInit(); +} +static void BoardDeinitCustom(void) +{ + watchdog_feed(); + //Power off SD + 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) { + return !(EAR_SMALL_PORT_MASK & MAP_GPIOPinRead(EAR_SMALL_PORT, EAR_SMALL_PORT_MASK)); +} +static volatile bool EarBigPressed(void) { + return !(EAR_BIG_PORT_MASK & MAP_GPIOPinRead(EAR_BIG_PORT, EAR_BIG_PORT_MASK)); +} + +static char* GetImagePathById(uint8_t number) { + char id = (char)((number%3) + 0x31); //See Ascii Table - 1 starts at 0x31 + char* name; + + if (number < 3) { + name = IMG_OFW_NAME; + } else if (number < 6) { + name = IMG_CFW_NAME; + } else /*if (number < 9)*/ { + name = IMG_ADD_NAME; + } + + for (uint8_t i=0; i<3; i++) + { + imagePath[IMG_SD_PATH_REPL1_POS+i] = name[i]; + } + imagePath[IMG_SD_PATH_REPL2_POS] = id; + + return imagePath; +} +static bool SdImageExists(uint8_t number) { + char* image = GetImagePathById(number); + return SdFileExists(image); +} +static bool CheckSdImages() { + bool hasValidImage = false; + for (uint8_t i=0; i> 2 ) & 0x0FFF; +} + +static void hibernate() { + watchdog_stop(); + BoardDeinitCustom(); + + PRCMHibernateWakeupSourceEnable(PRCM_HIB_GPIO2 | PRCM_HIB_GPIO4); //enable ear wakeup interrupt + PRCMHibernateEnter(); +} + +static void checkBattery() { + #ifndef FIXED_BOOT_IMAGE + if (isChargerConnected()) + return; + + if (getBatteryLevel() < Config_generalSettings.minBatteryLevel) { + prebootmgr_blink_error(2, 66); + prebootmgr_blink_error(2, 133); + prebootmgr_blink_error(2, 66); + + hibernate(); + } + #endif +} + +static uint8_t Selector(uint8_t startNumber) { + int8_t counter = startNumber; + + while (!Config_imageInfos[counter].fileExists) + { + counter = (counter+1) % COUNT_OF(Config_imageInfos); + } + + LedSet(COLOR_GREEN); + while (EarSmallPressed()) { + UtilsDelayMsWD(10); //Wait while pressed + watchdog_feed(); + } + + uint8_t colors[] = { COLOR_BLACK, COLOR_BLUE, COLOR_GREEN, COLOR_CYAN }; + uint8_t curColorId = 0; + if (Config_generalSettings.waitForPress) { + LedSet(COLOR_BLUE); + while (EarSmallPressed() || EarBigPressed()) { + UtilsDelayMsWD(10); //Wait while pressed + watchdog_feed(); + } + } + + //uint32_t millisStart = millis(); + uint32_t millisState = 0; + uint32_t secondsDelta = 0; + while (Config_generalSettings.waitForPress) + { + LedSet(colors[curColorId]); + UtilsDelayMsWD(250); + + if (curColorId Config_generalSettings.waitTimeoutInS) + hibernate(); + + watchdog_feed(); + checkBattery(); + } + + LedSet(COLOR_BLACK); + while (EarBigPressed()) { + if (EarSmallPressed()) { + do + { + counter = (counter+1) % COUNT_OF(Config_imageInfos); + } while (!Config_imageInfos[counter].fileExists); + while (EarSmallPressed()) { + UtilsDelayMsWD(10); //Wait while pressed + } + } + if (counter < 3) { + prebootmgr_blink_color((counter+1)-0, 100, COLOR_GREEN); + } else if (counter < 6) { + prebootmgr_blink_color((counter+1)-3, 100, COLOR_BLUE); + } else /*if (counter < 9)*/ { + prebootmgr_blink_color((counter+1)-6, 100, COLOR_CYAN); + } + UtilsDelayMsWD(500); + watchdog_feed(); + } + return counter; +} + +static void watchdog_recovery_sequence() { + //force NWP to idle State + HWREG(0x400F70B8) = 0x1; + UtilsDelay(800000/5); + + //Clear the interrupt + HWREG(0x400F70B0) = 0x1; + UtilsDelay(800000/5); + + //reset NWP, WLAN domains + HWREG(0x4402E16C) |= 0x2; + UtilsDelay(800); + + //Wnsure ANA DCDC is moved to PFM mode before envoking hibernate + HWREG(0x4402F024) &= 0xF7FFFFFF; +} + +int main() +{ + sBootInfo_t sBootInfo; + FIL ffile; + uint8_t ffs_result; + + BoardInitBase(); + BoardInitCustom(); + watchdog_start(); + + if (PRCM_WDT_RESET == MAP_PRCMSysResetCauseGet()) { + watchdog_recovery_sequence(); + + prebootmgr_blink_error(5, 33); + prebootmgr_blink_error(5, 66); + prebootmgr_blink_error(5, 33); + + hibernate(); + } + + #ifndef FIXED_BOOT_IMAGE + Config_InitImageInfos(); + #endif + + UtilsDelayMsWD(100); + ffs_result = f_mount(&fatfs, "0", 1); + if (ffs_result == FR_OK) { + #ifdef FIXED_BOOT_IMAGE + char* image = IMG_SD_BOOTLOADER_PATH; + if (SdFileExists(image)) { + #else + if (CheckSdImages()) { + Config_ReadJsonCfg(); + checkBattery(); + + retrySelection: + Config_generalSettings.activeImage = Selector(Config_generalSettings.activeImage); + + uint8_t selectedImgNum = Config_generalSettings.activeImage; + char* image = GetImagePathById(selectedImgNum); + #endif + + ffs_result = f_open(&ffile, image, FA_READ); + if (ffs_result == FR_OK) { + uint32_t filesize = f_size(&ffile); + + char* pImgRun = (char*)APP_IMG_SRAM_OFFSET; + ffs_result = f_read(&ffile, pImgRun, filesize, &filesize); + if (ffs_result == FR_OK) { + f_close(&ffile); + + #ifndef FIXED_BOOT_IMAGE + if (Config_imageInfos[selectedImgNum].checkHash) { + char hashExp[65]; + char hashAct[65]; + uint8_t hashActRaw[32]; + + hashExp[64] = '\0'; + hashAct[64] = '\0'; + + if (Config_imageInfos[selectedImgNum].hashFile) { + char* shaFile = HASH_SD_PATH; + memcpy(shaFile+IMG_SD_PATH_REPL1_POS, image+IMG_SD_PATH_REPL1_POS, 4); + ffs_result = f_open(&ffile, shaFile, FA_READ); + if (ffs_result == FR_OK) { + ffs_result = f_read(&ffile, hashExp, 64, NULL); + if (ffs_result == FR_OK) { + + } else { + //TODO + } + } else { + //TODO + } + } else { + filesize -= 64; //sha256 ist 64bytes long. + memcpy(hashExp, (char*)(pImgRun + filesize), 64); + } + + MAP_SHAMD5ConfigSet(SHAMD5_BASE, SHAMD5_ALGO_SHA256); + MAP_SHAMD5DataProcess(SHAMD5_BASE, pImgRun, filesize, hashActRaw); + btox(hashAct, hashActRaw, 64); + + if (strncmp(hashAct, hashExp, 64) != 0) { + //ERROR + + prebootmgr_blink_error(10, 50); + + Config_generalSettings.waitForPress = true; + goto retrySelection; + } + } + if (Config_imageInfos[selectedImgNum].ofwFix) { + uint32_t* pCheck1 = (uint32_t*)(pImgRun+filesize-0x04); + uint32_t* pCheck2 = (uint32_t*)(pImgRun+filesize-0x04-0x6c); + uint32_t* pTarget = (uint32_t*)(pImgRun+filesize-0x04-0x04); + + if (*pCheck1 == 0xBEAC0005 && *pCheck1 == *pCheck2) { + *pTarget = 0x0010014C; + } + } + + for (uint8_t i=0; ilength > 0) { + bool doPatch = false; + uint32_t offset = 0; + for (offset=0; offsetlength; offset++) { + if (patch->searchMask[0] == 0x00) + continue; + if (patch->search[0] != pImgRun[offset]) + continue; + + uint32_t offset2; + for (offset2=1; offset2length; offset2++) { + if (patch->searchMask[offset2] == 0x00) + continue; + if (patch->search[offset2] != pImgRun[offset+offset2]) { + offset2 = 0; + break; + } + } + if (offset2 == patch->length) { + doPatch = true; + break; + } + } + if (doPatch) { + for (uint32_t replaceOffset=0; replaceOffsetlength; replaceOffset++) { + if (patch->replaceMask[replaceOffset] == 0x00) + continue; + pImgRun[offset+replaceOffset] = patch->replace[replaceOffset]; + } + } + } + } + #endif + + checkBattery(); + BoardDeinitCustom(); + + #ifdef FIXED_BOOT_IMAGE + watchdog_start_slow(); + #else + if (!Config_imageInfos[selectedImgNum].watchdog) { + watchdog_stop(); + } else { + watchdog_start_slow(); + } + #endif + watchdog_feed(); + + Run((unsigned long)pImgRun); + } else { + UtilsDelayMsWD(1000); + prebootmgr_blink_error(4, 500); + UtilsDelayMsWD(2000); + prebootmgr_blink_error(ffs_result, 1000); + } + } else { + UtilsDelayMsWD(1000); + prebootmgr_blink_error(3, 500); + UtilsDelayMsWD(2000); + prebootmgr_blink_error(ffs_result, 1000); + } + } + UtilsDelayMsWD(2000); + } + + UtilsDelayMsWD(500); + prebootmgr_blink_error(2, 500); + UtilsDelayMsWD(500); + prebootmgr_blink_error(ffs_result, 1000); + + SlFsFileInfo_t pFsFileInfo; + _i32 fhandle; + sl_Start(NULL, NULL, NULL); //for reading flash + if (!sl_FsOpen(IMG_FLASH_PATH, FS_MODE_OPEN_READ, NULL, &fhandle)) { + if (!sl_FsGetInfo(IMG_FLASH_PATH, 0, &pFsFileInfo)) { + if (pFsFileInfo.FileLen == sl_FsRead(fhandle, 0, (unsigned char *)APP_IMG_SRAM_OFFSET, pFsFileInfo.FileLen)) { + sl_FsClose(fhandle, 0, 0, 0); + sl_Stop(30); + BoardDeinitCustom(); + Run(APP_IMG_SRAM_OFFSET); + } + } + } + + prebootmgr_blink_error(3, 33); + prebootmgr_blink_error(3, 66); + prebootmgr_blink_error(3, 33); + + sl_Stop(30); + + hibernate(); +} +//***************************************************************************** +// WLAN Event handler callback hookup function +//***************************************************************************** +void SimpleLinkWlanEventHandler(SlWlanEvent_t *pWlanEvent) +{ +} + +//***************************************************************************** +// +//! \brief This function handles General Events +//! +//! \param[in] pDevEvent - Pointer to General Event Info +//! +//! \return None +//! +//***************************************************************************** +void SimpleLinkGeneralEventHandler(SlDeviceEvent_t *pDevEvent) +{ +} + +//***************************************************************************** +// HTTP Server callback hookup function +//***************************************************************************** +void SimpleLinkHttpServerCallback(SlHttpServerEvent_t *pHttpEvent, + SlHttpServerResponse_t *pHttpResponse) +{ +} + +//***************************************************************************** +// Net APP Event callback hookup function +//***************************************************************************** +void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *pNetAppEvent) +{ +} + +//***************************************************************************** +// +//! This function handles socket events indication +//! +//! \param[in] pSock - Pointer to Socket Event Info +//! +//! \return None +//! +//***************************************************************************** +void SimpleLinkSockEventHandler(SlSockEvent_t *pSock) +{ } \ No newline at end of file diff --git a/sd-bootloader-ng/bootmanager/sd/revvox/boot/ngCfg.json b/sd-bootloader-ng/bootmanager/sd/revvox/boot/ngCfg.json index 0047e1c4..a6ce252e 100644 --- a/sd-bootloader-ng/bootmanager/sd/revvox/boot/ngCfg.json +++ b/sd-bootloader-ng/bootmanager/sd/revvox/boot/ngCfg.json @@ -1,7 +1,9 @@ { "general": { "activeImg": "ofw3", - "waitForPress": false + "waitForPress": false, + "waitTimeoutInS": 60, + "minBatteryLevel": 2100 }, "ofw1": { "checkHash": true, diff --git a/sd-bootloader-ng/bootmanager/startup_gcc.c b/sd-bootloader-ng/bootmanager/startup_gcc.c index be0e2231..b4b39aae 100755 --- a/sd-bootloader-ng/bootmanager/startup_gcc.c +++ b/sd-bootloader-ng/bootmanager/startup_gcc.c @@ -83,7 +83,7 @@ extern int main(void); // Reserve space for the system stack. // //***************************************************************************** -static uint32_t pui32Stack[128]; +static uint32_t pui32Stack[256]; //***************************************************************************** // @@ -202,11 +202,13 @@ ResetISR(void) // // Copy the data segment initializers // + + /* pui32Src = &__init_data; for(pui32Dest = &_data; pui32Dest < &_edata; ) { *pui32Dest++ = *pui32Src++; - } + }*/ // // Zero fill the bss segment. diff --git a/sd-bootloader-ng/bootmanager/watchdog.c b/sd-bootloader-ng/bootmanager/watchdog.c index d53589cf..61f9cf7a 100644 --- a/sd-bootloader-ng/bootmanager/watchdog.c +++ b/sd-bootloader-ng/bootmanager/watchdog.c @@ -9,10 +9,16 @@ #include "rom_map.h" #include "interrupt.h" -volatile uint8_t watchdog_feed_state; +volatile static uint8_t watchdog_feed_state; +volatile static bool watchdog_init = false; +static void watchdog_clear(void) { + if (watchdog_init) + MAP_WatchdogIntClear(WDT_BASE); +} void watchdog_feed(void) { watchdog_feed_state = WATCHDOG_TIMEOUT_S; + watchdog_clear(); } static void watchdog_unfeed(void) { watchdog_feed_state = 0; @@ -26,33 +32,49 @@ void watchdog_eat(void) { } static void watchdog_handler(void) { if (watchdog_feed_state > 0) { - MAP_WatchdogIntClear(WDT_BASE); + watchdog_clear(); watchdog_eat(); } } static bool initWatchdog(unsigned long ulLoadVal, void (*pfnHandler)(void)) { #ifndef DISABLE_WATCHDOG + if (watchdog_init) { + watchdog_stop(); + } watchdog_feed(); MAP_PRCMPeripheralClkEnable(PRCM_WDT, PRCM_RUN_MODE_CLK); + + watchdog_init = MAP_WatchdogRunning(WDT_BASE); + 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); + if (!watchdog_init) + MAP_WatchdogEnable(WDT_BASE); - return MAP_WatchdogRunning(WDT_BASE); + watchdog_init = MAP_WatchdogRunning(WDT_BASE); #endif + return watchdog_init; } bool watchdog_start(void) { return initWatchdog(MILLISECONDS_TO_TICKS(1000*WATCHDOG_CHECK_S), watchdog_handler); } +bool watchdog_start_slow(void) { + return initWatchdog(MILLISECONDS_TO_TICKS(1000*WATCHDOG_TIMEOUT_SLOW_S), watchdog_clear); +} +bool watchdog_stop(void) { + watchdog_init = false; -static void watchdog_handler_always(void) { + MAP_WatchdogUnlock(WDT_BASE); + MAP_WatchdogReloadSet(WDT_BASE, 0xFFFFFFFF); //set timer to high value MAP_WatchdogIntClear(WDT_BASE); -} -void watchdog_stop(void) { - initWatchdog(0xFFFFFFFF, watchdog_handler_always); + MAP_WatchdogIntUnregister(WDT_BASE); + + //watchdog_init = MAP_WatchdogRunning(WDT_BASE); + + //MAP_PRCMPeripheralClkDisable(PRCM_WDT, PRCM_RUN_MODE_CLK); } diff --git a/sd-bootloader-ng/bootmanager/watchdog.h b/sd-bootloader-ng/bootmanager/watchdog.h index 723e2c2c..93b622ce 100644 --- a/sd-bootloader-ng/bootmanager/watchdog.h +++ b/sd-bootloader-ng/bootmanager/watchdog.h @@ -11,7 +11,8 @@ extern "C" void watchdog_feed(void); bool watchdog_start(void); -void watchdog_stop(void); +bool watchdog_start_slow(void); +bool watchdog_stop(void); #ifdef __cplusplus } diff --git a/sd-bootloader-ng/bootmanager/wiring.c b/sd-bootloader-ng/bootmanager/wiring.c new file mode 100644 index 00000000..1876ccef --- /dev/null +++ b/sd-bootloader-ng/bootmanager/wiring.c @@ -0,0 +1,153 @@ +/* + ************************************************************************ + * wiring.c + * + * Energia core files for cc3200 + * Copyright (c) 2014 Robert Wessels. All right reserved. + * + * + *********************************************************************** + Derived from: + wiring.c - Partial implementation of the Wiring API for the ATmega8. + Part of Arduino - http://www.arduino.cc/ + + Copyright (c) 2005-2006 David A. Mellis + + This library is free software; you can redistribute it and/or + modify it under the terms of the GNU Lesser General Public + License as published by the Free Software Foundation; either + version 2.1 of the License, or (at your option) any later version. + + This library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Lesser General Public License for more details. + + You should have received a copy of the GNU Lesser General + Public License along with this library; if not, write to the + Free Software Foundation, Inc., 59 Temple Place, Suite 330, + Boston, MA 02111-1307 USA + */ + + +#include +#include + +#include "hw_ints.h" +#include "hw_nvic.h" +#include "hw_timer.h" +#include "hw_types.h" +#include "rom_map.h" +#include "systick.h" +#include "timer.h" + +#include "globalDefines.h" + + +static void (*SysTickCbFuncs[8])(uint32_t ui32TimeMS); + +#define SYSTICKHZ 1000 +#define SYSTICKMS (1000 / SYSTICKHZ) + +static unsigned long milliseconds = 0; +#define SYSTICK_INT_PRIORITY 0x80 + + +unsigned long micros(void) +{ + return (milliseconds * 1000) + ( ((HAL_FCPU_HZ / SYSTICKHZ) - MAP_SysTickValueGet()) / (HAL_FCPU_HZ/1000000)); +} + +unsigned long millis(void) +{ + return milliseconds; +} +/* +void delayMicroseconds(unsigned int us) +{ + // Systick timer rolls over every 1000000/SYSTICKHZ microseconds + if (us > (1000000UL / SYSTICKHZ - 1)) { + delay(us / 1000); // delay milliseconds + us = us % 1000; // handle remainder of delay + }; + + // 24 bit timer - mask off undefined bits + unsigned long startTime = HWREG(NVIC_ST_CURRENT) & NVIC_ST_CURRENT_M; + + unsigned long ticks = (unsigned long)us * (HAL_FCPU_HZ/1000000UL); + volatile unsigned long elapsedTime; + + if (ticks > startTime) { + ticks = (ticks + (NVIC_ST_CURRENT_M - (unsigned long)HAL_FCPU_HZ / SYSTICKHZ)) & NVIC_ST_CURRENT_M; + } + + do { + elapsedTime = (startTime-(HWREG(NVIC_ST_CURRENT) & NVIC_ST_CURRENT_M )) & NVIC_ST_CURRENT_M; + } while(elapsedTime <= ticks); +} + +void delay(uint32_t millis) +{ + unsigned long i; + for(i=0; i