• AVR Freaks

Hot!PIC18F45K50 bootloader

New Member
  • Total Posts : 15
  • Reward points : 0
  • Joined: 2006/05/26 14:06:05
  • Location: 0
  • Status: offline
2020/10/25 08:26:35 (permalink)

PIC18F45K50 bootloader

I am building a bootloader for a project with a 18F45K50. The board has an external flash which is used to store the hex file (uploaded through USB CDC). To trigger the bootloader, a magic value is written in eeprom, and the PIC is reset. The bootloader looks for the magic value, and if found, it takes control of the external flash, validates the content and if valid, erases memory range above 0x800, programs flash/eeprom, and finally jump to 0x800.
The unified code works fine. The bootloader seems to work fine, i.e. I can read the flash after the bootloader has run, and I get the expected content. My issue is that something is wrong and the code doesn't run after being flashed internally. I am not sure if I can trust the "read device memory" from MPLab (I tried not reprogramming the eeprom and having the bootloader writes another value in eeprom, and I indeed find this, so I tent to trust the read... but if there is no programming error, why does the code fail ?)
The "app" code is an MCC project. The bootloader is my own, it looks at ranges 0x800-0x7fff and 0xf00000-0xf000ff to program flash/eeprom (so no attempt to mess with config). The bootloader includes device_config.c so app/bootloader both have the same config. The config (xc8 linker) has the offset set to 0x800.
Some code maybe:
- the bootloader stole some parts from pic18f_bootloader.c (the one generated by mcc)

#define NEW_RESET_VECTOR 0x800
asm ("psect intcode,global,reloc=2,class=CODE,delta=1");
asm ("psect intcodelo,global,reloc=2,class=CODE,delta=1");

- the decision to erase/flash or not:
    // check if update should be done
    EEADR = 0;
    EECON1bits.CFGS = 0;
    EECON1bits.EEPGD = 0;
    EECON1bits.RD = 1;
    if (EEDATA == 0xbb) {
        // clear update flag
        EEDATA = 0xaa;
        // take control of the flash
        A16_LAT = 0;
        if (process(1) == 0) {

    STKPTR = 0;
    BSR = 0;
    asm ("goto " str(NEW_RESET_VECTOR));

Any suggestion is good at this point. I have written other bootloaders in the past (pic18 and pic32) and this one should work just like the others, but it doesn't.  Note that I (unfortunately) use RB6/RB7 as output to control some address lines of the external flash chip, so running the bootloader under the debugger is not easy.

1 Reply Related Threads

    New Member
    • Total Posts : 15
    • Reward points : 0
    • Joined: 2006/05/26 14:06:05
    • Location: 0
    • Status: offline
    Re: PIC18F45K50 bootloader 2020/10/26 06:45:42 (permalink)
    +2 (2)
    Problem fixed. It was a logic bug.
    Jump to:
    © 2020 APG vNext Commercial Version 4.5