I am working on a huge project based on PIC17F67K40 MCU. I am writing a relatively complex firmware, using several replacement librariues and also CRTOS, a cooperative multitasking system. This allows me to do 20 things in parallel.
What I need to implement is a bootloader for unattended self-flashing. The principle is simple, I download the new firmware into an external SPI F-RAM chip. After download I verify the CRC32 checksum, When checksum is correct, I set a flag in FRAM and reboot the MCU.
The bootloader connects to this F-RAM and tests the flag. If the flag is found, it starts the self-flashing process. When done, it clears the flag and does another reboot. Now the flag is not detected any more, so the bootloader starts up the main program.
Where I please for your kindly help:
I have absolutely no knowledge in assembly language, I code always (and only) in mikroPascal PRO for PIC. So it is a bit hard to me to figure out how to implement the bootloader.
I want to place the bootloader in uppermost range of FLASH memory. Let's say it starts at $1F000. Everything before this address is main firmware, everything above is bootloader. At top end of main firmware (so directly before $1F000) I place a little code, a jump to the normal entry point of main firmware. So this code is part of the main firmware and outside bootloader, but on a fixed location.
Sop the bootloader will jump to this location, the code there jumps to the real entry point. The benefit of this is:I do not have to modify the bootloader when I change the firmware.
Possibly I can also place a dummy procedure in the main firmware at $1F000, being initially executed. If this is possible, it could behave like the real bootloader, but I do not have to patch startup pointers every time?
It would be very kind if you can help.
I see two options:
- I use a bootloader written with orgall to be placed completely above $1F000, and I lock out the main program from $1F000...$1FFFF by allocating that memory by a const array.
In this case, I have to patch the compiled program from external program, because I have to replace the original jump vector by a jump to $1F000, and I have to patch the original jump into last bytes before $1F000 (the jump back to main firmware). - I can write a dummy "bootloader" procedure at $1F000 (using org), which is executed first (after restart) and doing nothing else than jumping to the address stored right before of it (where a jump to the real main program is located). All memory above this dummy bootloader will be allocated by a const array, so mikroPascal has to place the entire code below..
This way I can easily combile the software of main program and bootloader. I merge code from $00000-$1EFFF from main program and code from $1F000-$1FFFF from bootloader.