Skip to content
Snippets Groups Projects
Unverified Commit 4667b665 authored by Thibaut VIARD's avatar Thibaut VIARD
Browse files

[bl] Fixing the jump to sketch

parent f3528306
No related branches found
No related tags found
No related merge requests found
......@@ -65,9 +65,9 @@ SIZE=$(ARM_GCC_PATH)size
#-w
CFLAGS=-mthumb -mcpu=cortex-m0plus -Wall -c -std=gnu99 -ffunction-sections -fdata-sections -nostdlib -nostartfiles --param max-inline-insns-single=500
ifdef DEBUG
CFLAGS+=-g3 -O1
CFLAGS+=-g3 -O1 -DDEBUG=1
else
CFLAGS+=-Os
CFLAGS+=-Os -DDEBUG=0
endif
#CFLAGS_EXTRA?=-D__SAMD21G18A__ -DUSB_PID_LOW=0x4D -DUSB_PID_HIGH=0x00
CFLAGS_EXTRA?=-D__SAMD21J18A__ -DUSB_PID_LOW=0x01 -DUSB_PID_HIGH=0xE0
......@@ -153,8 +153,9 @@ copy_for_atmel_studio: $(EXECUTABLE)
clean_for_atmel_studio:
@echo ----------------------------------------------------------
@echo Atmel Studio detected, cleaing ELF from project root
-$(RM) $(BUILD_PATH)/$(ELF) .
@echo Atmel Studio detected, cleaning ELF from project root
# -$(RM) $(BUILD_PATH)/$(ELF)
-$(RM) ./$(ELF)
clean: $(AS_CLEAN)
@echo ----------------------------------------------------------
......
......@@ -58,7 +58,7 @@ MEMORY
* __StackLimit
* __StackTop
* __stack
* __app_start_address
* __sketch_stackptr
*/
ENTRY(Reset_Handler)
......@@ -144,7 +144,7 @@ SECTIONS
*/
__etext = .;
PROVIDE(__app_start_address = ORIGIN(FLASH) + LENGTH(FLASH));
PROVIDE(__sketch_stackptr = ORIGIN(FLASH) + LENGTH(FLASH));
.data : AT (__etext)
......
......@@ -70,10 +70,12 @@
#include "sam_ba_usb.h"
#include "sam_ba_cdc.h"
extern uint32_t __app_start_address;
extern uint32_t __sketch_stackptr; // Exported value from linker script
extern void board_init(void);
//static void check_start_application(void);
#if (defined DEBUG) && (DEBUG == 1)
volatile uint32_t* pulSketch_Start_Address;
#endif
static volatile bool main_b_cdc_enable = false;
......@@ -105,7 +107,9 @@ static void check_start_application(void)
/* First tap */
BOOT_DOUBLE_TAP_DATA = DOUBLE_TAP_MAGIC;
/* Wait 0.5sec to see if the user tap reset again */
/* Wait 0.5sec to see if the user tap reset again.
* The loop value is based on SAMD21 default 1MHz clock @ reset.
*/
for (uint32_t i=0; i<125000; i++) /* 500ms */
/* force compiler to not optimize this... */
__asm__ __volatile__("");
......@@ -115,51 +119,71 @@ static void check_start_application(void)
}
#endif
uint32_t app_start_address;
#if (!defined DEBUG) || ((defined DEBUG) && (DEBUG == 0))
uint32_t* pulSketch_Start_Address;
#endif
/* Load the Reset Handler address of the application */
app_start_address = *(uint32_t *)(&__app_start_address + 4);
/*
* Test sketch stack pointer @ &__sketch_stackptr
* Stay in SAM-BA if value @ (&__sketch_stackptr) == 0xFFFFFFFF (Erased flash cell value)
*/
if (__sketch_stackptr == 0xFFFFFFFF)
{
/* Stay in bootloader */
return;
}
/*
* Load the sketch Reset Handler address
* __sketch_stackptr is exported from linker script and point on first 32b word of sketch vector table
* First 32b word is sketch stack
* Second 32b word is sketch entry point: Reset_Handler()
*/
pulSketch_Start_Address = &__sketch_stackptr ;
pulSketch_Start_Address++ ;
/**
* Test reset vector of application @__app_start_address+4
* Stay in SAM-BA if *(__app_start_address+0x4) == 0xFFFFFFFF
* Application erased condition
/*
* Test reset vector of sketch @ &__sketch_stackptr+4
* Stay in SAM-BA if this function is not aligned enough, ie not valid
* The value 0x01 is the 'Thumb mode' bit added by linker.
*/
if (app_start_address == 0xFFFFFFFF)
if ( (*pulSketch_Start_Address & ~SCB_VTOR_TBLOFF_Msk) != 0x01 )
{
/* Stay in bootloader */
return;
}
/*
#if defined(BOOT_LOAD_PIN)
volatile PortGroup *boot_port = (volatile PortGroup *)(&(PORT->Group[BOOT_LOAD_PIN / 32]));
volatile bool boot_en;
/* Enable the input mode in Boot GPIO Pin */
// Enable the input mode in Boot GPIO Pin
boot_port->DIRCLR.reg = BOOT_PIN_MASK;
boot_port->PINCFG[BOOT_LOAD_PIN & 0x1F].reg = PORT_PINCFG_INEN | PORT_PINCFG_PULLEN;
boot_port->OUTSET.reg = BOOT_PIN_MASK;
/* Read the BOOT_LOAD_PIN status */
// Read the BOOT_LOAD_PIN status
boot_en = (boot_port->IN.reg) & BOOT_PIN_MASK;
/* Check the bootloader enable condition */
// Check the bootloader enable condition
if (!boot_en)
{
/* Stay in bootloader */
// Stay in bootloader
return;
}
#endif
*/
LED_on();
/* Rebase the Stack Pointer */
__set_MSP(*(uint32_t *) &__app_start_address);
__set_MSP( (uint32_t)(*(pulSketch_Start_Address-1)) );
/* Rebase the vector table base address */
SCB->VTOR = ((uint32_t) &__app_start_address & SCB_VTOR_TBLOFF_Msk);
SCB->VTOR = ((uint32_t)(pulSketch_Start_Address-1) & SCB_VTOR_TBLOFF_Msk);
/* Jump to application Reset Handler in the application */
asm("bx %0"::"r"(app_start_address));
asm("bx %0"::"r"(*pulSketch_Start_Address));
}
#if DEBUG_ENABLE
......
No preview for this file type
......@@ -69,10 +69,6 @@
<Value>NDEBUG</Value>
</ListValues>
</armgcc.compiler.symbols.DefSymbols>
<armgcc.compiler.directories.IncludePaths>
<ListValues>
</ListValues>
</armgcc.compiler.directories.IncludePaths>
<armgcc.compiler.optimization.level>Optimize for size (-Os)</armgcc.compiler.optimization.level>
<armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>True</armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>
<armgcc.compiler.warnings.AllWarnings>True</armgcc.compiler.warnings.AllWarnings>
......@@ -81,16 +77,8 @@
<Value>libm</Value>
</ListValues>
</armgcc.linker.libraries.Libraries>
<armgcc.linker.libraries.LibrarySearchPaths>
<ListValues>
</ListValues>
</armgcc.linker.libraries.LibrarySearchPaths>
<armgcc.linker.optimization.GarbageCollectUnusedSections>True</armgcc.linker.optimization.GarbageCollectUnusedSections>
<armgcc.linker.miscellaneous.LinkerFlags>-Tsamd21j18a_flash.ld</armgcc.linker.miscellaneous.LinkerFlags>
<armgcc.preprocessingassembler.general.IncludePaths>
<ListValues>
</ListValues>
</armgcc.preprocessingassembler.general.IncludePaths>
</ArmGcc>
</ToolchainSettings>
</PropertyGroup>
......@@ -107,10 +95,6 @@
<Value>DEBUG</Value>
</ListValues>
</armgcc.compiler.symbols.DefSymbols>
<armgcc.compiler.directories.IncludePaths>
<ListValues>
</ListValues>
</armgcc.compiler.directories.IncludePaths>
<armgcc.compiler.optimization.level>Optimize (-O1)</armgcc.compiler.optimization.level>
<armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>True</armgcc.compiler.optimization.PrepareFunctionsForGarbageCollection>
<armgcc.compiler.optimization.DebugLevel>Maximum (-g3)</armgcc.compiler.optimization.DebugLevel>
......@@ -120,17 +104,9 @@
<Value>libm</Value>
</ListValues>
</armgcc.linker.libraries.Libraries>
<armgcc.linker.libraries.LibrarySearchPaths>
<ListValues>
</ListValues>
</armgcc.linker.libraries.LibrarySearchPaths>
<armgcc.linker.optimization.GarbageCollectUnusedSections>True</armgcc.linker.optimization.GarbageCollectUnusedSections>
<armgcc.linker.miscellaneous.LinkerFlags>-Tsamd21j18a_flash.ld</armgcc.linker.miscellaneous.LinkerFlags>
<armgcc.assembler.debugging.DebugLevel>Default (-g)</armgcc.assembler.debugging.DebugLevel>
<armgcc.preprocessingassembler.general.IncludePaths>
<ListValues>
</ListValues>
</armgcc.preprocessingassembler.general.IncludePaths>
<armgcc.preprocessingassembler.debugging.DebugLevel>Default (-Wa,-g)</armgcc.preprocessingassembler.debugging.DebugLevel>
</ArmGcc>
</ToolchainSettings>
......@@ -241,4 +217,4 @@
</None>
</ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project>
</Project>
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment