Mike's Bootloader integrated
This commit is contained in:
@@ -177,37 +177,66 @@ static uint32_t BOARD_ConfigurePmc(void)
|
||||
// timeout = 0;
|
||||
// while (!(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT));
|
||||
|
||||
// Switch to PLLA as main clock 36MHz
|
||||
// Switch to PLLA as main clock 36MHz
|
||||
PMC->PMC_MCKR = BOARD_MCKR;
|
||||
timeout = 0;
|
||||
while (!(PMC->PMC_SR & PMC_SR_MCKRDY) && (timeout++ < CLOCK_TIMEOUT));
|
||||
|
||||
#endif
|
||||
return 36000000L ; // Master_frequency
|
||||
return 36000000L; // Master_frequency
|
||||
}
|
||||
|
||||
void revert_osc()
|
||||
{
|
||||
register uint32_t timeout = 0 ;
|
||||
register Pmc *pmcptr ;
|
||||
register uint32_t timeout = 0;
|
||||
register Pmc *pmcptr;
|
||||
|
||||
pmcptr = PMC ;
|
||||
pmcptr = PMC;
|
||||
|
||||
|
||||
// Switch back to the internal oscillator
|
||||
pmcptr->CKGR_MOR = (0x37 << 16) | BOARD_OSCOUNT | CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN ;
|
||||
// Switch back to the internal oscillator
|
||||
pmcptr->CKGR_MOR = (0x37 << 16) | BOARD_OSCOUNT | CKGR_MOR_MOSCRCEN | CKGR_MOR_MOSCXTEN;
|
||||
timeout = 0;
|
||||
while (!(pmcptr->PMC_SR & PMC_SR_MOSCSELS) && (++timeout < CLOCK_TIMEOUT));
|
||||
|
||||
pmcptr->PMC_MCKR = (PMC->PMC_MCKR & ~(uint32_t)PMC_MCKR_CSS) | PMC_MCKR_CSS_SLOW_CLK;
|
||||
timeout = 0;
|
||||
while (!(pmcptr->PMC_SR & PMC_SR_MCKRDY) && (++timeout < CLOCK_TIMEOUT));
|
||||
|
||||
|
||||
pmcptr->CKGR_PLLAR = 1 << 29 ; // Stop PLLA
|
||||
|
||||
pmcptr->CKGR_PLLAR = 1 << 29; // Stop PLLA
|
||||
}
|
||||
|
||||
#if defined(BOOT)
|
||||
// extern void sam_bootx( void ) ;
|
||||
// extern void dispUSB( void ) ;
|
||||
// extern uint32_t init
|
||||
// ReadTrims( void ) ;
|
||||
// extern void run_application( void ) ;
|
||||
|
||||
static void lowLevelUsbCheck( void )
|
||||
{
|
||||
PMC->PMC_PCER0 = (1<<ID_PIOC) ; // Enable clock to PIOC
|
||||
PIOC->PIO_PER = PIO_PC25 ; // Enable bit C25 (USB-detect)
|
||||
|
||||
uint32_t i ;
|
||||
for ( i = 0 ; i < 50 ; i += 1 ) {
|
||||
__asm("nop") ;
|
||||
}
|
||||
|
||||
for ( i = 0 ; i < 10 ; i += 1 ) {
|
||||
if ( PIOC->PIO_PDSR & 0x02000000 ) {
|
||||
PMC->PMC_PCDR0 = (1<<ID_PIOC) ; // Disable clock to PIOC
|
||||
dispUSB() ;
|
||||
sam_bootx() ;
|
||||
}
|
||||
}
|
||||
|
||||
uint32_t x = initReadTrims() ;
|
||||
if ((x & 0x42) != 0x42) {
|
||||
run_application();
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
/*----------------------------------------------------------------------------
|
||||
* Exported functions
|
||||
*----------------------------------------------------------------------------*/
|
||||
@@ -220,10 +249,14 @@ void revert_osc()
|
||||
/*----------------------------------------------------------------------------*/
|
||||
uint32_t SystemInit (void)
|
||||
{
|
||||
/** Set 2 cycle (1 WS) for Embedded Flash Access */
|
||||
// Max clock is 38 MHz (1.8V VVDCORE)
|
||||
EFC->EEFC_FMR = (1 << 8) ;
|
||||
#if defined(BOOT)
|
||||
lowLevelUsbCheck() ;
|
||||
#endif
|
||||
|
||||
/** Configure PMC */
|
||||
// Set 2 cycle (1 WS) for Embedded Flash Access
|
||||
// Max clock is 38 MHz (1.8V VVDCORE)
|
||||
EFC->EEFC_FMR = (1 << 8) ;
|
||||
|
||||
// Configure PMC
|
||||
return BOARD_ConfigurePmc();
|
||||
}
|
||||
|
||||
@@ -556,8 +556,19 @@ void i2cInit()
|
||||
NVIC_EnableIRQ(TWI0_IRQn) ;
|
||||
}
|
||||
|
||||
extern const uint8_t BootCode[];
|
||||
|
||||
void boardInit()
|
||||
{
|
||||
// TODO this is not clean, completely unuseful, but prevents the bootloader from being optimized away...
|
||||
uint8_t dummy[3];
|
||||
memcpy(&dummy, BootCode, sizeof(dummy));
|
||||
for (unsigned int i=0; i<sizeof(dummy); i++) {
|
||||
if (dummy[i] == 0) {
|
||||
__asm("nop");
|
||||
}
|
||||
}
|
||||
|
||||
register Pio *pioptr ;
|
||||
|
||||
ResetReason = RSTC->RSTC_SR;
|
||||
|
||||
@@ -68,6 +68,8 @@ uint32_t __REV(uint32_t value) ;
|
||||
|
||||
void revert_osc( void ) ;
|
||||
void sam_boot( void ) ;
|
||||
void sam_bootx( void ) ;
|
||||
void run_application( void ) ;
|
||||
|
||||
|
||||
/* ################### Compiler specific Intrinsics ########################### */
|
||||
@@ -459,6 +461,7 @@ uint32_t __STREXW(uint32_t value, uint32_t *addr)
|
||||
#elif (defined (__GNUC__)) /*------------------ GNU Compiler ---------------------*/
|
||||
/* GNU gcc specific functions */
|
||||
|
||||
#if !defined(BOOT)
|
||||
/**
|
||||
* @brief Return the Process Stack Pointer
|
||||
*
|
||||
@@ -633,7 +636,7 @@ void __set_CONTROL(uint32_t control)
|
||||
{
|
||||
__ASM volatile ("MSR control, %0" : : "r" (control) );
|
||||
}
|
||||
|
||||
#endif
|
||||
|
||||
/**
|
||||
* @brief Reverse byte order in integer value
|
||||
@@ -651,6 +654,7 @@ uint32_t __REV(uint32_t value)
|
||||
return(result);
|
||||
}
|
||||
|
||||
#if !defined(BOOT)
|
||||
/**
|
||||
* @brief Reverse byte order in unsigned short value
|
||||
*
|
||||
@@ -797,10 +801,28 @@ uint32_t __STREXW(uint32_t value, uint32_t *addr)
|
||||
__ASM volatile ("strex %0, %2, [%1]" : "=r" (result) : "r" (addr), "r" (value) );
|
||||
return(result);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(BOOT)
|
||||
void run_application()
|
||||
{
|
||||
__ASM(" mov.w r1, #4227072"); // 0x408000
|
||||
__ASM(" movw r0, #60680"); // 0xED08
|
||||
__ASM(" movt r0, #57344"); // 0xE000
|
||||
__ASM(" str r1, [r0, #0]"); // Set the VTOR
|
||||
__ASM("ldr r0, [r1, #0]"); // Stack pointer value
|
||||
__ASM("msr msp, r0"); // Set it
|
||||
__ASM("ldr r0, [r1, #4]"); // Reset address
|
||||
__ASM("mov.w r1, #1");
|
||||
__ASM("orr r0, r1"); // Set lsbit
|
||||
__ASM("bx r0"); // Execute application
|
||||
}
|
||||
#endif
|
||||
|
||||
#if !defined(BOOT)
|
||||
void sam_boot()
|
||||
{
|
||||
revert_osc() ;
|
||||
revert_osc() ;
|
||||
|
||||
__ASM(" mov.w r1, #8388608");
|
||||
__ASM(" movw r0, #60680");
|
||||
@@ -816,8 +838,26 @@ void sam_boot()
|
||||
__ASM("orr r0, r3");
|
||||
__ASM("bx r0");
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
#if defined(BOOT)
|
||||
void sam_bootx()
|
||||
{
|
||||
__ASM(" mov.w r1, #8388608");
|
||||
__ASM(" movw r0, #60680");
|
||||
__ASM(" movt r0, #57344");
|
||||
__ASM(" str r1, [r0, #0]"); // Set the VTOR
|
||||
|
||||
__ASM("mov.w r3, #0");
|
||||
__ASM("movt r3, #128");
|
||||
__ASM("ldr r0, [r3, #0]");
|
||||
__ASM("msr msp, r0");
|
||||
__ASM("ldr r0, [r3, #4]");
|
||||
__ASM("mov.w r3, #1");
|
||||
__ASM("orr r0, r3");
|
||||
__ASM("bx r0");
|
||||
}
|
||||
#endif
|
||||
|
||||
#elif (defined (__TASKING__)) /*------------------ TASKING Compiler ---------------------*/
|
||||
/* TASKING carm specific functions */
|
||||
|
||||
@@ -71,15 +71,16 @@ SECTIONS
|
||||
*/
|
||||
.text :
|
||||
{
|
||||
FILL(0xFFFF)
|
||||
FILL(0xFFFF)
|
||||
|
||||
CREATE_OBJECT_SYMBOLS
|
||||
CREATE_OBJECT_SYMBOLS
|
||||
|
||||
*(.bootrodata)
|
||||
*(.bootrodata.*)
|
||||
|
||||
. = ALIGN(32768); /* Align the start of the exidx part */
|
||||
_stext = .; /* Provide the name for the start of this section */
|
||||
|
||||
KEEP(*(.vectors))
|
||||
|
||||
*(.text)
|
||||
|
||||
@@ -71,9 +71,15 @@ SECTIONS
|
||||
*/
|
||||
.text :
|
||||
{
|
||||
_stext = .; /* Provide the name for the start of this section */
|
||||
FILL(0xFFFF)
|
||||
|
||||
CREATE_OBJECT_SYMBOLS
|
||||
|
||||
*(.bootrodata)
|
||||
*(.bootrodata.*)
|
||||
|
||||
. = ALIGN(32768); /* Align the start of the exidx part */
|
||||
_stext = .; /* Provide the name for the start of this section */
|
||||
KEEP(*(.vectors))
|
||||
|
||||
*(.text)
|
||||
|
||||
Reference in New Issue
Block a user