Working on SAMG55 bootloader. Need advice

Discussion around product based on ARM Cortex M4 core.

Moderators: nferre, ncollot

jonavarque
Posts: 29
Joined: Fri Jul 10, 2015 6:35 pm

Working on SAMG55 bootloader. Need advice

Sat Sep 05, 2015 3:56 am

I am working on a USB HID bootloader for the SAMG55. I am not very GCC savvy and I need some hints on how to relocate my firmware to live above the bootloader, or anywhere.

I see in the G55 that there is a register to set an offset for the vector table. But I am not sure how to relocate my code, data, text, etc. I imagine it is settings in the linker file?

Any advice welcome! I am using Atmel Studio 6
cwunder
Posts: 34
Joined: Fri Jul 01, 2011 9:39 pm

Re: Working on SAMG55 bootloader. Need advice

Sun Sep 06, 2015 7:55 pm

I would recommend two separate programs (meaning two separate linker files), one for the bootloader and the other for the application. If you need to share some information between them via the ram then you can create a section in both linker files.

The bootloader memory size should be made such that the section is equal to the sector sizes at the beginning of the flash memory. Then modify the rom size in the linker to match. For example if the bootloader uses 8K
rom (rx) : ORIGIN = 0x00400000, LENGTH = 0x00002000
Then the application linker for the flash should start with an 8k offset and the total memory available is the chip flash minus the bootloader. For the same example as above
rom (rx) : ORIGIN = 0x00402000, LENGTH = 0x0007E000

You can also add a section at the end for the CRC of the memory and or version information.

Hope this helps
jonavarque
Posts: 29
Joined: Fri Jul 10, 2015 6:35 pm

Re: Working on SAMG55 bootloader. Need advice

Mon Sep 07, 2015 2:34 am

Wow! A response! THANKS !

I am using a pair of code bases, derived from the main firmware with the HID USB drivers for communication.
I'm writing the bootloader with default linker settings.
I am using the User Signature area of the G55 flash to store anything that needs to be shared between them or sent to the host from either of them. At the moment, either the bootloader or the main app will send this data to the host on attach. Bootloader and Firmware Versions, serial numbers, etc.

I see the length is set in your examples. Excellent.

Is that the only linker tweak I need to make? None for data or any other segments ? That's too easy!

In the bootloader example from atmel, I am still trying to understand what all the code pertaining to regions was about.

So, for the app:
Change the app linker file
Build a .hex file
Open it and parse it, send binary to bootloader
Bootloader writes pages in flash at new address

Then, the vector table?
Will it show up at the new base address and I only have to set the G55's Vector Table offset register before jumping to the app?

Sorry if my questions are rough..

Thanks again,

Jona
cwunder
Posts: 34
Joined: Fri Jul 01, 2011 9:39 pm

Re: Working on SAMG55 bootloader. Need advice

Mon Sep 07, 2015 3:34 am

Is that the only linker tweak I need to make? None for data or any other segments ? That's too easy!
It can be but it depends on your desired program behavior.
In the bootloader example from atmel, I am still trying to understand what all the code pertaining to regions was about.
Not sure what your question is?

You have to understand that the hardware of the device will cause the bootloader code to run (i.e reset etc..) since you have not written the Vector Table Offset Register. So you need code in your bootloader that would change the program execution to the application when you want. The code could be something like the snippet below:

Code: Select all

      /* Pointer to the Application Section */
      void (*application_code_entry)(void);

      /* Rebase the Stack Pointer */
      __set_MSP(*(uint32_t *) APP_START_ADDRESS);

      /* Rebase the vector table base address */
      SCB->VTOR = ((uint32_t) APP_START_ADDRESS & SCB_VTOR_TBLOFF_Msk);

      /* Load the Reset Handler address of the application */
      application_code_entry = (void (*)(void))(unsigned *)(*(unsigned *)
                               (APP_START_RESET_VEC_ADDRESS));

      /* Jump to user Reset Handler in the application */
      application_code_entry();
-Chris
jonavarque
Posts: 29
Joined: Fri Jul 10, 2015 6:35 pm

Re: Working on SAMG55 bootloader. Need advice

Mon Sep 07, 2015 4:34 pm

I am using AtmelStudio 6.2. I am having a little trouble determining where I would enter the Address ORIGIN and LENGTH, or something like it. Would it be in the project's "Memory Settings" ?

I don't even see the default value anywhere.. hmm...

edit: OK, I think I see, it is in a linker script somewhere...

edit: HA! Found it./linker_scripts/samg/samg55j19/gcc/ flash.ld

There's the RAM and ROM settings staring me in the face. Thank you for your help.
bandtank
Posts: 21
Joined: Tue Oct 29, 2013 10:35 pm

Re: Working on SAMG55 bootloader. Need advice

Fri Sep 18, 2015 7:32 pm

Did you get this to work? If you share the code on Github, I'll work on it with you. I'd like to use a USB bootloader, so I'll contribute to the project.
jonavarque
Posts: 29
Joined: Fri Jul 10, 2015 6:35 pm

Re: Working on SAMG55 bootloader. Need advice

Fri Sep 18, 2015 11:07 pm

Yes, I did. I can't share the code because it is a proprietary project for a client. Happy to answer questions though.
bandtank
Posts: 21
Joined: Tue Oct 29, 2013 10:35 pm

Re: Working on SAMG55 bootloader. Need advice

Sat Sep 19, 2015 1:37 am

There's too much to ask without being able to see the code to make a conversation worthwhile. As an aside, my clients have never cared about releasing bootloader code especially considering there's pretty much nothing proprietary about it. Have you asked? This would help tons of people, myself included. My last bootloader (link) has helped thousands of people and it started as a proprietary project.
jonavarque
Posts: 29
Joined: Fri Jul 10, 2015 6:35 pm

Re: Working on SAMG55 bootloader. Need advice

Sat Sep 19, 2015 4:49 am

Where are you stuck?

Basics are

Generic HID driver
Internal Flash driver

I used the BL example for the SAMG. It was written to use the SD card. Removed all SD Card code and got it to run.
Then I added the Generic HID. Oddly, when I used ASF to add it, it was a completely different version than the one I had added to another project 2 months ago. So I used the one I already had code for from the other project. I had to write a bit lengthy USB protocol for Windows to set Versions, serial numbers and send Hex data, verify hex data, etc. BL is probably the smallest part.

This bootloader receives an INIT_BOOTLOADER packet via USB. Addresses and indexes are reset and the memory range is erased. Then, packets are sent to the device, each containing a binary version of the hex file line and the destination address and data length. When the 512 byte input buffer is full, each is written to iFLASH . Don't forget to get the straggler, the last buffer, at the end of download. It won't necessarily be full. (you'll probably do it differently, but that one got me again, for the thousandth time. )

Edit the linker file in your project for the app you're bootloading. The ORIGIN and LENGTH are at the very top of the file. In the BL example is a routine for executing the app from the BL. I think it is called app_exec() .. The Vector Table offset register in the G55 is set to the base address of the loaded app. in my case, 0x420000 the bootloader runs at 0x400000 (0x00000000).

I did take the time to implement a modified version of the example's "INFO" system. Which is the CRC for the downloaded app, the app start, size, and the trigger directive.

If all is well, it jumps to 0x420004, the reset vector, after the other housekeeping. Look in bootloader.c for that stuff.
I used software RESETS to switch between the BL and the app. I could not get the BL to release its USB and the app to enumerate correctly. I setup a SW reset USB packet that does this trick from the host.

After reset, When the BL runs, it checks that INFO for the trigger directive. It can be set to force BL to run, APP to run or an active switch on power up. This is my method for invoking either BL or APP from Windows via USB SetTrigger packet. If BL is supposed to run the app, it reads the CRC form the INFO area, does a CRC on the APP area and and if they're equal, it jumps to the app.

I know you know all this stuff, cause you've written BLs. I posted it in case it helps you come up with questions.
bandtank
Posts: 21
Joined: Tue Oct 29, 2013 10:35 pm

Re: Working on SAMG55 bootloader. Need advice

Mon Sep 21, 2015 1:47 am

I can't figure out how to private message you, so I'll ask here. I'm in need of a bootloader like this for a current project, which doesn't really leave me enough time to figure it out on my own. How do I become one of your clients to have you recreate the same bootloader and front end application for me?

Edit: I appreciate the time you spent writing that post and it's basically what I expected, but, as I said above, I just don't have enough time to work through the entire process. I've been down this road many times and I'd rather pay for this one if at all possible.
jonavarque
Posts: 29
Joined: Fri Jul 10, 2015 6:35 pm

Re: Working on SAMG55 bootloader. Need advice

Mon Sep 21, 2015 3:35 am

Sounds possible.
I tried in vain to send you a msg as well. Added you as a friend but could not find anywhere to actually send a PM. In the user control panel I do have it set to allow PMs. Perhaps someone else can offer a solution.
bandtank
Posts: 21
Joined: Tue Oct 29, 2013 10:35 pm

Re: Working on SAMG55 bootloader. Need advice

Mon Sep 21, 2015 4:19 pm

What a crappy piece of forum software. I don't like to post my email address on public forums, so I made an alias. I'll give you my real email address after you contact me here: bootloader@inboxalias.com
jonavarque
Posts: 29
Joined: Fri Jul 10, 2015 6:35 pm

Re: Working on SAMG55 bootloader. Need advice

Mon Sep 21, 2015 5:40 pm

Yes, it is pretty horrific. But it is one step better than Atmel documentation.
blue_z
Location: USA
Posts: 1759
Joined: Thu Apr 19, 2007 10:15 pm

Re: Working on SAMG55 bootloader. Need advice

Mon Sep 21, 2015 9:25 pm

bandtank wrote:What a crappy piece of forum software.
Apparently the PM capability has been broken for about a year or more. It was still working in late 2013 (the last time I was able to use it).

Posting attachments was also broken for a while, but was recently re-enabled.

Regards
bandtank
Posts: 21
Joined: Tue Oct 29, 2013 10:35 pm

Re: Working on SAMG55 bootloader. Need advice

Mon Sep 21, 2015 9:34 pm

blue_z wrote:
bandtank wrote:What a crappy piece of forum software.
Apparently the PM capability has been broken for about a year or more. It was still working in late 2013 (the last time I was able to use it).

Posting attachments was also broken for a while, but was recently re-enabled.

Regards
Thanks for the information. I'm glad, on some level, that we weren't simply missing it.

Return to “SAM4 Cortex-M4 MCU”

Who is online

Users browsing this forum: No registered users and 4 guests