Bootloader PC application questions

mikroC, mikroBasic and mikroPascal PRO for Microchip’s 32-bit PIC MCUs
Post Reply
Author
Message
peini
Posts: 46
Joined: 28 Dec 2011 09:56

Bootloader PC application questions

#1 Post by peini » 18 Aug 2017 10:12

Hi there,

once more i try to understand the PIC32 USB HID Bootloader because i want to create a Bootloader for live updating my PIC32.
The normal user app should receive the new program which will be stored in an external flash. Then, after a reset, the Bootloader should copy over the firmware from the external flash to its internal program flash.

Since a few days i'am trying to reverse engineer how the PC application of the HID Bootloader is working to better understand which modifications to the hex file need to be made in order to run a user-program uploaded by the bootloader.

So i need some help please:
1. I know mikroe wont provide the full source code of the PC application but can you please tell me which modifications of the user-app are necessary. Or maybe you can provide just that piece of source. :)

2. The example folder contains a PDF called "PIC32 USB HID BootLoader Memory Layout.pdf" which shows the memory layout. In this file there's an annotation which says: "Application code must have some configuration word settings as bootloader firmware".
What does that mean? What are these configuration word settings?

User avatar
darko.ilijevski
Posts: 581
Joined: 21 Mar 2017 16:57

Re: Bootloader PC application questions

#2 Post by darko.ilijevski » 21 Aug 2017 17:17

Hello

- If you are asking about how to modify the .hex file to get loaded into the MCU via the HID bootloader, I have to say that .hex is not getting modified at all - it's uploaded as it is.

- When you write the user code, you have to follow the same configuration as originally in the bootloader. That is because you can't change the contents of certain config registers during the runtime - user app is actually an extension of the bootloader app in the MCU - There's a function which starts the code loaded in a certain area of the memory - simply jumps there and starts that part of the code - so actually - you are finishing the half-written app - if you understand what I am saying. In doing so - you are not allowed to change the config registers, since there's not access to them programmatically.

- There are various bootloader app examples you can find in the compiler's examples folder and see how they are made. The easiest to understand are UART bootloaders, as they use the simple solution - they write a chunk of code to a specified flash address, which they receive via the UART and after it's done, bootloader continues by executing a "StartProgram()" function placed on a newly written program's location, with the "orgall" directive. That approach could be a lot better if you do not use a PC, but only chip to chip communication.

You can find UART bootloader examples in the 8 bit PIC compiler, although they are written for 8 bit PIC, you can still use them as a reference - to get an idea what you should do.

Best regards
BR,
Darko

peini
Posts: 46
Joined: 28 Dec 2011 09:56

Re: Bootloader PC application questions

#3 Post by peini » 26 Aug 2017 11:41

Hi

thank you for your reply but in the meantime i think i've found out how your bootloader is working.
The hex file is indeed transferred to the PIC as it is but there are some things you need to know and i'll try to write that down for others.

Inspecting the code on the PIC32 before and after transferring a hex-file via the bootloader showed me, that the mikroe Bootloader has some big disadvantage.
It "can" brick it's own bootloader if there is something going wrong while uploading a hex-file. More details later.

This was the point where i've decided to design my own bootloader.

After studying a lot of datasheets i've got it to work and i'll describe a few important steps you need to know.

1. On a Reset, a PIC32 starts program execution at 0xBFC00000 (KSEG1) or 0x1FC00000 (PHY). Knowing this is extremely usefull.

2. Now that we know where our PIC is starting we need to place a method there which starts (jumps to) our real bootloader application which can then be placed anywhere.
If we take a look at the function-table in the statistics dialog of a standard mikroC-project we will see that there is a method called "__BootStartUp" placed at 0xBFC00000. So this is the first method that will be executed after startup. This method will initialize some basic settings (memory setup) of the MCU.
But, as i've told before, we need to place a method there which jumps to our bootloader. So i've started to try to move this method around and this works very well. 0xBFC00000 is located in the Bootflash-Area of the processor but you can also move it to the program flash.

3. Ok. So i've moved my whole bootloader-code and the __BootStartUp-method to one of the last pages in the program flash by using the orgall directive.
Then i've placed a small method at 0xBFC00000 which simply jumps to the address of the __BootStartUp-method which looks like this:

Code: Select all

void BootResetVector ( void )
{
  // This is the first pice of code executed on a POR.
  R30 = BOOTLOADER_START; // Address of __BootStartUp
  asm JR R30;
  asm NOP;
}
Now our PIC32 will directly jump to out bootloader-application.

4. So now our bootloader is working but we have another problem: The user-app has it's own __BootStartUp-method so we need to decide where to put this one.
And this one is the part where the mikroe-bootloader is doing something else. The mikroe-bootloader also has placed the BootResetVector-method at 0xBFC00000 when initially flashing the bootloader to the PIC.
But when uploading a user-app via the bootloader it simply overrides the boot-flash starting at 0xBFC00000 and places it's own __BootStartUp-method here.
I'am not 100% sure but i think it then modifies the jump-command which originally jumped to the main()-method of the user-app so it'll jump to the bootloader again.
So this modification is the critical part here. If there is something going wrong in the meantime of writing to 0xBFC00000 and updating the jump-to-bootloader command you've broken your bootloader because it will never jump to the bootloader anymore after startup.

5. So my bootloader will allways leave the BootResetVector-method alone an will never touch it when uploading a user-app.
Instead i've decided to org the user-app's __BootStartUp-method one page beyond 0xBFC00000 (0xBFC00000 + _FLASH_ERASE).
This has a little bit of disadvantage because you need to do a small modification of your user-app to org it's own __BootStartUp-method to this address. But for me this is totally acceptable.

6. The rest of the user-app is regularly placed at 0x9D000000 (KSEG0) as it comes from the hex-file.

7. To start our user-app from the bootloader we simply need to jump to 0xBFC00000 + _FLASH_ERASE. Remember this is the address of the user-app's __BootStartUp-method.

So, for me this is working great but it still needs a bit of testing but at the moment it looks good, even using interrupts in the user-app.

feuerwolf
Posts: 95
Joined: 09 Nov 2012 16:00
Location: Switzerland

Re: Bootloader PC application questions

#4 Post by feuerwolf » 21 Oct 2017 00:31


Post Reply

Return to “PIC32 PRO Compilers”