Hi Alban,
I post here my source code, please move it in another thread if you think that so it will be more visible.
The code is in part developed from Rocco's source; it implements a selectable RS232-CAN bootloader which resides in protected memory. It's all except perfect, especially in the asm part (I'm nothing more than a rookie in assembler); if you have any advice it will be well accepted.
At startup it checks the flash (sum and xor); if it's ok it jump to the normal "_entrypoint" of the application, otherwise it enter the boot mode and waits for incoming messages. The external application has the ability to write, erase and read the flash, plus some other little options (reboot, read firmware ID...). For now I tested it with an external application in VB, and I'm able to reprogram the firmware via both rs232 and CAN.
The .asm file contains the low-low level initial check, and the init to start the essential functions of the micro, including obviously FLASH, SCI and CAN. The clock is given by an external 4MHz oscillator, requested by the tight timing tolerance of CAN controller. I then enabled the protection of last 3K of flash, and redirection of vector table.
The main loop waits for incoming messages in a polling fashion.
The .prm file contains the memory allocation for the bootloader: I put it in PROTECTED_ROM section. All the bootloader is in protected rom (0xF400-0xFFB0); please note that I didn't manage to put the .asm code in a relocatable section, so I had to assign it at address 0xF800 by mean of a ORG directive.
An important thing is where you place the "_EntryPoint" of the normal application in the non protected rom. It MUST be always in the same location to allow the bootloader to jump to it. I did this allocating the "_EntryPoint" function in MY_ENTRYPOINT.
Then I added here the code to automatically write the SUM and XOR in the right locations. Finally, note the entry point of the application (bootEntry). This, along with the reset interrupt vector, should point to label "bootEntry" in the asm file.
The .c file contains the routine to manage the CAN messages. I wrote them in C because I found it less difficult. Please pay attention to double check your code when you modify the C code; infact the compiler can try to optimize your code calling some low-level library function, which resides in non-protected rom! It result in application crashing when it runs!! To check if the compiler has used the library you have to look in the .map file. An example is visible in the code, where there are the splitted "switch" instructions: I had to split it because if it's all in one it requires a library function (try it to see).
If you have questions, or if I wasn't clear enough, feel free to ask!
Simone