Working with a bootloader

General discussion on mikroBasic PRO for PIC32.
Post Reply
Author
Message
serge87
Posts: 45
Joined: 12 Sep 2012 17:55

Working with a bootloader

#1 Post by serge87 » 12 Feb 2017 23:34

About a year ago I received a project for an electronic circuit which had to communicate with a server to post some data.
The problem was that I had to make the program that runs in the microcontroller upgradable remotely via a command received from the server.
I’ve studied how to best do that over-the-air upgrade and I came up with an external flash memory that holds 2 versions of the firmware, the current version and the old one.
When the server initiate the upgrade process, the uC will rewrite the old program version in the external flash with the new version, ensuring that all the data was written successfully, after the writing in the external flash is done the bootloader program takes over (via an reset) and rewrites the internal flash with the new version.
When I first tried to write the bootloader and to separate it from the main program I hit some problems regarding the memory allocation.
The uC of choice was PIC32MX795F512H with an external 32Mbit SPI flash. The compiler is Mikrobasic for PIC32.
In the uC datasheet says that this chip has 8k of boot flash which can be used for booloading purposes, and for me was enough to fit my custom bootloader.
The bootloader had to do a couple of things:
1. To check if there is a new version in the external flash and upgrade the software, if there isn’t
2. To activate the UART module and wait for the software, the UART communication was put in place to have a means to write the first software version.
3. To roll back to previous version in case of a crash.
After a while, while learning how the uC memory was structured and how the mE compiler worked with it, I came up with the following solution to the problem.
DISCLAIMER: I cannot post the entire bootloader because it’s part of a product, however I will post the steps on how I achieved the result.
The bootloader program:
The bootloader is located entirely in boot flash.
You can do this as follows:

Code: Select all

main:
orgall (0xBFC00000)  ‘ directive to instruct the compiler to put all program starting at this address
‘the rest of the programm
After that directive (if you don’t exceed the boot flash) all of the program will reside in boot flash.
My main program started at address 0x9D000008, so I put the following at the end of the program.

Code: Select all

program_jump:
DisableInterrupts()
  R30 = 0x9D000008 ' Load R30 with bootloader main address.
  asm
    JR R30               ' Perform indirect jump to bootloader application,
    NOP                  ' thus changing the kseg as well.
  end asm
end.
The code above was taken form mE’s USB bootloader, which instructs the uC to jump at the address loaded in R30 register.
The main program:
In the main program we have the following:

Code: Select all

main:
setorg(__BootStartUp,0x9D000008 )
‘the rest of the program
end.
The _BootStartUp routine is the actual beginning of the program, which the compiler usually puts it at the beginning of the boot flash address.
Second we have to relocate the IVT which the compiler puts it in the boot flash, this can be done from the Edit project menu by changing the IVT base address (EBASE):
main program settings.PNG
main program settings.PNG (16.45 KiB) Viewed 2734 times
The default EBASE value is: 0x9FC01000.
Third we need to disable all the interrupts that we used in the bootloader that are not used anymore, because the SFR registers will not reset when we are jumping to the main program.

I hope this will help anyone who is willing to write their own bootloaders
Just tell me....

User avatar
filip
mikroElektronika team
Posts: 11874
Joined: 25 Jan 2008 09:56

Re: Working with a bootloader

#2 Post by filip » 13 Feb 2017 17:32

Hi,

Thanks for sharing this, it sure will be helpful to our users.

Regards,
Filip.

Post Reply

Return to “mikroBasic PRO for PIC32 General”