00001 /* ---------------------------------------------------------------------------- 00002 * SAM Software Package License 00003 * ---------------------------------------------------------------------------- 00004 * Copyright (c) 2015, Atmel Corporation 00005 * 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions are met: 00010 * 00011 * - Redistributions of source code must retain the above copyright notice, 00012 * this list of conditions and the disclaimer below. 00013 * 00014 * Atmel's name may not be used to endorse or promote products derived from 00015 * this software without specific prior written permission. 00016 * 00017 * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR 00018 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 00019 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE 00020 * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT, 00021 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 00022 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, 00023 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 00024 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 00025 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 00026 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00027 * ---------------------------------------------------------------------------- 00028 */ 00029 00030 00031 /*---------------------------------------------------------------------------- 00032 * Headers 00033 *----------------------------------------------------------------------------*/ 00034 #include "board.h" 00035 #include "loader.h" 00036 00037 /*---------------------------------------------------------------------------- 00038 * Imported variables 00039 *----------------------------------------------------------------------------*/ 00040 00041 /*---------------------------------------------------------------------------- 00042 * Exported variables 00043 *----------------------------------------------------------------------------*/ 00044 00045 /*---------------------------------------------------------------------------- 00046 * Local variables 00047 *----------------------------------------------------------------------------*/ 00048 00049 /*---------------------------------------------------------------------------- 00050 * Local functions 00051 *----------------------------------------------------------------------------*/ 00052 /** 00053 * \brief Run Application which is flashed at given address. 00054 */ 00055 static void _RunApplication(uint32_t appAddress) 00056 { 00057 uint32_t __Start_SP; 00058 uint32_t (*__Start_New)(void); 00059 uint32_t *pCode = (uint32_t *)appAddress; 00060 00061 /* set PC and SP */ 00062 __Start_New = (uint32_t(*) (void) ) pCode[1]; 00063 __Start_SP = pCode[0]; 00064 00065 printf("\n\r Starting application at flash 0x%08x! \n\r", (unsigned int)appAddress); 00066 printf("========================================================= \n\r"); 00067 00068 WDT_Restart(WDT); 00069 __disable_irq(); 00070 __set_MSP(__Start_SP); 00071 __Start_New(); 00072 } 00073 00074 00075 /*---------------------------------------------------------------------------- 00076 * Exported functions 00077 *----------------------------------------------------------------------------*/ 00078 /** 00079 * \brief Judge preconditions of applications and run one of them 00080 * 00081 * \param pAppConfig pointer to AppConfig instances. 00082 * \param appNum Number of applications 00083 * \return void 00084 */ 00085 void LOADER_AppSwitch(AppConfig *pAppConfig, uint32_t appNum) 00086 { 00087 uint32_t i; 00088 uint32_t rc = 0; 00089 for (i = 0; i < appNum; i++, pAppConfig++) { 00090 printf("\n\r APP %d: %s", (int)i, (char*)pAppConfig->appInfo); 00091 00092 rc = (pAppConfig->funPreCondition) ? pAppConfig->funPreCondition() : 0; 00093 00094 if ( 0 == rc) { 00095 printf("\n\r-I- preconditions satisfied.\n\r"); 00096 _RunApplication(pAppConfig->appAddress); 00097 } else { 00098 printf("\n\r-I- preconditions not satisfied.\n\r"); 00099 } 00100 } 00101 printf("\n\r-I- None of the preconditions are satisfied, applications won't be loaded."); 00102 } 00103