finish bootloader
This commit is contained in:
98
efm32boot/src/boot.c
Normal file
98
efm32boot/src/boot.c
Normal file
@@ -0,0 +1,98 @@
|
||||
/**************************************************************************//**
|
||||
* @file boot.c
|
||||
* @brief Functions for booting another application
|
||||
* @author Silicon Labs
|
||||
* @version 1.03
|
||||
******************************************************************************
|
||||
* @section License
|
||||
* <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>
|
||||
*******************************************************************************
|
||||
*
|
||||
* Permission is granted to anyone to use this software for any purpose,
|
||||
* including commercial applications, and to alter it and redistribute it
|
||||
* freely, subject to the following restrictions:
|
||||
*
|
||||
* 1. The origin of this software must not be misrepresented; you must not
|
||||
* claim that you wrote the original software.
|
||||
* 2. Altered source versions must be plainly marked as such, and must not be
|
||||
* misrepresented as being the original software.
|
||||
* 3. This notice may not be removed or altered from any source distribution.
|
||||
*
|
||||
* DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
|
||||
* obligation to support this Software. Silicon Labs is providing the
|
||||
* Software "AS IS", with no express or implied warranties of any kind,
|
||||
* including, but not limited to, any implied warranties of merchantability
|
||||
* or fitness for any particular purpose or warranties against infringement
|
||||
* of any proprietary rights of a third party.
|
||||
*
|
||||
* Silicon Labs will not be liable for any consequential, incidental, or
|
||||
* special damages, or any other relief, or for any claim by any third party,
|
||||
* arising from your use of this Software.
|
||||
*
|
||||
******************************************************************************/
|
||||
|
||||
#include "em_device.h"
|
||||
#include "em_gpio.h"
|
||||
#include "em_cmu.h"
|
||||
#include "app.h"
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* This function sets up the Cortex-M3 with a new SP and PC.
|
||||
*****************************************************************************/
|
||||
#if defined ( __CC_ARM )
|
||||
__asm void BOOT_jump(uint32_t sp, uint32_t pc)
|
||||
{
|
||||
/* Set new MSP, PSP based on SP (r0)*/
|
||||
msr msp, r0
|
||||
msr psp, r0
|
||||
|
||||
/* Jump to PC (r1)*/
|
||||
bx r1
|
||||
}
|
||||
#else
|
||||
void BOOT_jump(uint32_t sp, uint32_t pc)
|
||||
{
|
||||
(void) sp;
|
||||
(void) pc;
|
||||
/* Set new MSP, PSP based on SP (r0)*/
|
||||
__asm("msr msp, r0");
|
||||
__asm("msr psp, r0");
|
||||
|
||||
/* Jump to PC (r1)*/
|
||||
__asm("mov pc, r1");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* Resets any peripherals that have been in use by
|
||||
* the bootloader before booting the appliation */
|
||||
static void resetPeripherals(void)
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/******************************************************************************
|
||||
* Boots the firmware. This function will activate the vector table
|
||||
* of the firmware application and set the PC and SP from this table.
|
||||
*****************************************************************************/
|
||||
void BOOT_boot(void)
|
||||
{
|
||||
uint32_t pc, sp;
|
||||
|
||||
uint32_t *bootAddress = (uint32_t *)(JUMP_LOC);
|
||||
|
||||
resetPeripherals();
|
||||
|
||||
/* Set new vector table */
|
||||
SCB->VTOR = (uint32_t)bootAddress;
|
||||
|
||||
/* Read new SP and PC from vector table */
|
||||
sp = bootAddress[0];
|
||||
pc = bootAddress[1];
|
||||
|
||||
/* Do a jump by loading the PC and SP into the CPU registers */
|
||||
BOOT_jump(sp, pc);
|
||||
}
|
@@ -1,28 +1,106 @@
|
||||
#include "em_device.h"
|
||||
#include "em_chip.h"
|
||||
|
||||
#include "device.h"
|
||||
#include "app.h"
|
||||
#include "u2f.h"
|
||||
#include "log.h"
|
||||
#include "InitDevice.h"
|
||||
|
||||
void bootloader_init(void);
|
||||
|
||||
|
||||
int main(void)
|
||||
{
|
||||
/* Chip errata */
|
||||
CHIP_Init();
|
||||
int count = 0;
|
||||
uint32_t t1 = 0;
|
||||
uint32_t t2 = 0;
|
||||
uint32_t accum = 0;
|
||||
uint32_t dt = 0;
|
||||
uint8_t hidmsg[64];
|
||||
/* Chip errata */
|
||||
CHIP_Init();
|
||||
|
||||
EMU_enter_DefaultMode_from_RESET();
|
||||
CMU_enter_DefaultMode_from_RESET();
|
||||
// ADC0_enter_DefaultMode_from_RESET();
|
||||
USART0_enter_DefaultMode_from_RESET();
|
||||
USART1_enter_DefaultMode_from_RESET();
|
||||
LDMA_enter_DefaultMode_from_RESET();
|
||||
CRYOTIMER_enter_DefaultMode_from_RESET();
|
||||
PORTIO_enter_DefaultMode_from_RESET();
|
||||
EMU_enter_DefaultMode_from_RESET();
|
||||
CMU_enter_DefaultMode_from_RESET();
|
||||
// ADC0_enter_DefaultMode_from_RESET();
|
||||
USART0_enter_DefaultMode_from_RESET();
|
||||
USART1_enter_DefaultMode_from_RESET();
|
||||
// LDMA_enter_DefaultMode_from_RESET();
|
||||
CRYOTIMER_enter_DefaultMode_from_RESET();
|
||||
PORTIO_enter_DefaultMode_from_RESET();
|
||||
|
||||
bootloader_init();
|
||||
bootloader_init();
|
||||
|
||||
set_logging_mask(
|
||||
/*0*/
|
||||
TAG_GEN|
|
||||
/*TAG_MC |*/
|
||||
/*TAG_GA |*/
|
||||
/*TAG_WALLET |*/
|
||||
TAG_STOR |
|
||||
/*TAG_CP |*/
|
||||
// TAG_CTAP|
|
||||
/*TAG_HID|*/
|
||||
/*TAG_U2F|*/
|
||||
/*TAG_PARSE |*/
|
||||
// TAG_TIME|
|
||||
/*TAG_DUMP|*/
|
||||
/*TAG_GREEN|*/
|
||||
/*TAG_RED|*/
|
||||
TAG_ERR
|
||||
);
|
||||
|
||||
printf("Bootloader init\r\n");
|
||||
|
||||
if (GPIO_PinInGet(PUSH_BUTTON) == 0)
|
||||
{
|
||||
t1 = millis();
|
||||
while(GPIO_PinInGet(PUSH_BUTTON) == 0 && (millis() - t1) < 2000)
|
||||
;
|
||||
if (GPIO_PinInGet(PUSH_BUTTON) == 0) {
|
||||
bootmode:
|
||||
printf("Reflash condition detected\n");
|
||||
ctaphid_init();
|
||||
/* Infinite loop */
|
||||
int count = 0;
|
||||
while (1) {
|
||||
if (millis() - t1 > 1000)
|
||||
{
|
||||
/*printf("heartbeat %ld\n", beat++);*/
|
||||
heartbeat();
|
||||
t1 = millis();
|
||||
}
|
||||
|
||||
if (usbhid_recv(hidmsg) > 0)
|
||||
{
|
||||
/*printf("%d>> ",count++); dump_hex1(TAG_DUMP, hidmsg,sizeof(hidmsg));*/
|
||||
// t2 = millis();
|
||||
ctaphid_handle_packet(hidmsg);
|
||||
// accum += millis() - t2;
|
||||
// printf("accum: %d\n", (uint32_t)accum);
|
||||
// printf("dt: %d\n", t2 - dt);
|
||||
// dt = t2;
|
||||
memset(hidmsg, 0, sizeof(hidmsg));
|
||||
}
|
||||
else
|
||||
{
|
||||
/*main_loop_delay();*/
|
||||
}
|
||||
ctaphid_check_timeouts();
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
printf("Normal boot\n");
|
||||
|
||||
if (is_authorized_to_boot())
|
||||
{
|
||||
BOOT_boot();
|
||||
} else {
|
||||
printf("Warning: not authorized to boot\n");
|
||||
goto bootmode;
|
||||
}
|
||||
|
||||
/* Infinite loop */
|
||||
while (1) {
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user