Skip to content

Bootloader rework #47

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 22 commits into from
Nov 12, 2015
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
[bl] Fixing the jump to sketch
  • Loading branch information
aethaniel committed Oct 8, 2015
commit 4667b66525b94c6b6acddcac2b5f1c8fdbc1bfc1
9 changes: 5 additions & 4 deletions bootloaders/zero/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ----------------------------------------------------------
Expand Down
4 changes: 2 additions & 2 deletions bootloaders/zero/bootloader_samd21x18.ld
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ MEMORY
* __StackLimit
* __StackTop
* __stack
* __app_start_address
* __sketch_stackptr
*/
ENTRY(Reset_Handler)

Expand Down Expand Up @@ -144,7 +144,7 @@ SECTIONS
*/

__etext = .;
PROVIDE(__app_start_address = ORIGIN(FLASH) + LENGTH(FLASH));
PROVIDE(__sketch_stackptr = ORIGIN(FLASH) + LENGTH(FLASH));


.data : AT (__etext)
Expand Down
60 changes: 42 additions & 18 deletions bootloaders/zero/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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__("");
Expand All @@ -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
Expand Down
Binary file modified bootloaders/zero/samd21_sam_ba.bin
Binary file not shown.
26 changes: 1 addition & 25 deletions bootloaders/zero/samd21_sam_ba.cproj
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand All @@ -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>
Expand Down Expand Up @@ -241,4 +217,4 @@
</None>
</ItemGroup>
<Import Project="$(AVRSTUDIO_EXE_PATH)\\Vs\\Compiler.targets" />
</Project>
</Project>