Atmel website | ARM Community | AVR freaks | Technical Support
Banner
 FAQ •  Search •  Register •  Login 

All times are UTC + 1 hour [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: SAM3S Bootloader - Jump to Main App
PostPosted: Thu Jun 14, 2012 10:43 am 
Offline

Joined: Tue Mar 13, 2012 5:19 pm
Posts: 12
Hi,
I have a bootloader + main app memory layout. What needs to be done when bootloader is giving control to main app? I have read couple posts here, but nobody gave any definitive answers so far.
To be more specific, I am using Keil 4.21 and my memory image specs is following

boot 0x00400000 0x00404000
app 0x00404000 and on

Now I am able to load the main app to flash and verify it, but then I get stuck

1) What needs to be done before the jump itself? I found two suggestions here in the forums, to set offset of the interrupt vector table and to modify stack pointer. Do I really need to do that + why?

2) The jump itself. I have tried jump with assembler function in separate file Jump2Main.s, but I got this weird linker error.
Code:
   PRESERVE8
   AREA Jump, CODE, READONLY
   EXPORT JumpToProgram

JumpToProgram               
   B 0x00404004         
   END

.\obj\BootSAM3S.axf: Error: L6471E: Relocation #REL:0 in jump2main.o(Jump) with respect to [Anonymous Symbol]. Branch refers to ARM Absolute Symbol defined in jump2main.o, Suppress error to treat as a Thumb address.


When I suppress it as suggested, I can compile and load it into the device, but when executing B instruction, I end up in HardFault_Handler loop, instead of in main app.

I am sorry for all the noobie questions, but I haven't found any rock-solid answers anywhere else :)

Thanks
Entik


Top
 Profile  
 
 Post subject: Re: SAM3S Bootloader - Jump to Main App
PostPosted: Sun Jun 17, 2012 4:58 pm 
Offline

Joined: Wed May 26, 2010 9:46 am
Posts: 8
Hi Entik,

Like you said, you should set the offset for interrupt vector table to the PC and modify SP in order to perform the jump.

this code worked for me on the SAM3U:

Code:

// Define pointer to function
typedef void (* func)(void);

static void _jumpToMain()
{
  register func main_entry;
  int stackPointerAddress =  MAIN_ADDRESS; // in your case 0x00404000 
  int resetPointerAddress =  MAIN_ADDRESS + 4;

  // Set stack pointer with the first word of the run mode program
  // just for remainder: Vector table's first entry is the stack pointer value
  __set_MSP((*(int*)stackPointerAddress));

  // Set the program counter to the application start address
  // just for remainder: Vector table's second entry is the system reset value
  main_entry = * ((func *)resetPointerAddress);
  main_entry();

}



Good luck,
iT


Top
 Profile  
 
 Post subject: Re: SAM3S Bootloader - Jump to Main App
PostPosted: Sun Jun 17, 2012 6:44 pm 
Offline

Joined: Sat Oct 30, 2010 6:04 pm
Posts: 784
entik wrote:
2) The jump itself. I have tried jump with assembler function in separate file Jump2Main.s, but I got this weird linker error.


You've got two problems here, first you can't branch to arbitrary addresses using relative instructions when you don't control the address they are situated at, and second the value at 0x00404004 is an address you want to load another address from, not an address to which you want to jump, ie indirectly

ldr r0, =0x00404000
ldr sp, [r0, #0]
ldr r0, [r0, #4]
bx r0

Branching to an explicit Thumb code address would look like

ldr r0, =0x00404101 // Must be ODD for THUMB
bx r0


Top
 Profile  
 
 Post subject: Re: SAM3S Bootloader - Jump to Main App
PostPosted: Tue Jun 19, 2012 10:33 am 
Offline

Joined: Tue Mar 13, 2012 5:19 pm
Posts: 12
Hi guys,
thank you for code and little assembler insight. I found out that for SAM3S, interrupt vector table reallocation has to be added, so the final (and working :)) function looks like this

Code:
typedef void (* func)(void);
#define MAIN_ADDRESS   0x00404000

static void _jumpToMain()
{
  register func main_entry;
  int stackPointerAddress =  MAIN_ADDRESS;
  int resetPointerAddress =  MAIN_ADDRESS + 4;

  // Set stack pointer with the first word of the run mode program
  // just for remainder: Vector table's first entry is the stack pointer value
  __set_MSP((*(int*)stackPointerAddress));

   //set the NVIC's VTOR to the beginning of main app
   *(int volatile*)0xE000ED08 = MAIN_ADDRESS;

  // Set the program counter to the application start address
  // just for remainder: Vector table's second entry is the system reset value
  main_entry = * ((func *)resetPointerAddress);
  main_entry();

}


Thanks again for your help
Entik


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: