• AVR Freaks

Hot!PIC18F47K42 - Bootloader erases program flash and PIC resets

Page: 12 > Showing page 1 of 2
Author
ahallaj
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2020/01/17 00:19:12
  • Location: 0
  • Status: offline
2020/05/28 23:23:15 (permalink)
0

PIC18F47K42 - Bootloader erases program flash and PIC resets

I am working on a bootloader for PIC18F47K42. I am using MPLAB X IDE v5.25 with XC8 compiler v2.05. I configured the program memory such that it has a boot block and application block ( BBEN=1, SAFEN=0).
The bootloader resides in address range 0x0 - 0x4000 in the program memory and the application program is expected to be written to address range 0x4000 - 0x1FFFF

However, I have encountered a problem that when I attempt to erase or write to Program Memory Address 0x10000 and above, the PIC gets reset!!!!

I use the MCC generated code to erase the flash block. This is how I attempt to erase the program memory in a for loop:

for (uint32_t base_addr = 0x4000 ; base_addr < 0x20000 ; base_addr += 128)
{
FLASH_EraseBlock(base_addr);
}


This is the MCC generated code to erase Flash Block.
==============================================
void FLASH_EraseBlock(uint32_t baseAddr)
{
uint8_t GIEBitValue = INTCON0bits.GIE; // Save interrupt enable

TBLPTRU = (uint8_t)((baseAddr & 0x00FF0000) >> 16);
TBLPTRH = (uint8_t)((baseAddr & 0x0000FF00)>> 8);
TBLPTRL = (uint8_t)(baseAddr & 0x000000FF);

NVMCON1bits.NVMREG = 2;
NVMCON1bits.WREN = 1;
NVMCON1bits.FREE = 1;
asm("BCF INTCON0,7");
asm("BANKSEL NVMCON1");
asm("BSF NVMCON1,2");
asm("MOVLW 0x55");
asm("MOVWF NVMCON2");
asm("MOVLW 0xAA");
asm("MOVWF NVMCON2");
asm("BSF NVMCON1,1");
asm("BSF INTCON0,7");
}
 
Any help or suggestion is much appreciated
#1

21 Replies Related Threads

    Danno
    Super Member
    • Total Posts : 287
    • Reward points : 0
    • Joined: 2005/09/07 10:12:10
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/05/29 13:37:39 (permalink)
    +1 (1)
    This sounds like the code is not restricted and erasing itself from memory.  $.02
    Is the bootloader actually restricted to 0x00000-0x03FFF?  This has to be done in the project properties/ linker options.  Add in ROM range 0-3FFF.
     
    HTH
    #2
    ahallaj
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/01/17 00:19:12
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/05/29 14:10:25 (permalink)
    0
    Yes, the bootloader is restrictedto 0x0 - 0x3FFF. This is set in the linker options as you stated.
     
    I wonder if this is a rollover issue related to the base address offset.  The program flash is 16-bit words.  Should the offset be byte offset or word offset?  For instance if I want to erase the second row of the program flash, should the offset/base address be 0x80 (128) or 0x40 (64)? 
    #3
    Danno
    Super Member
    • Total Posts : 287
    • Reward points : 0
    • Joined: 2005/09/07 10:12:10
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/05/29 16:29:25 (permalink)
    0
    The last assembly line of the erase function enables global interrupts.   Since the first line of the erase function saves the GIE state, instead of blindly enabling GIE, it would seem more correct to restore it from the saved variable.
     
    I just generated a MCC project for the PIC8F27K42  (MPLAB X 5.35, xc8 V2.20).  With the BSF  INTCON0,7 instruction commented out, the code works fine...
     
    d
    #4
    NorthGuy
    Super Member
    • Total Posts : 6161
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/05/29 20:20:04 (permalink)
    +3 (3)
    You can read the PIC back and see if you have erased anything in a wrong place.
    #5
    ahallaj
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/01/17 00:19:12
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/05/30 18:53:39 (permalink)
    0
    In a standalone application (not the booatloader), erasing and writing to the program flash above the address 0x10000 works as expected. I examined the memory and I can verify the erase and write operation works as expected.  So there must be something in my bootloader that's causing the PIC to halt when erasing the address range above 0x10000.  
     
    The bootloader interacts with a GUI application via UART port. I use function pointers in the bootloader to invoke corresponding function based on the command received from the GUI.  As I was debugging and stepping through the code, I realized that when I erase the address range above 0x10000, a function pointer becomes NULL when it is invoked in the application and that causes the PIC Program Counter becomes 0 and PIC halts.  If I don't erase the program flash above 0x10000; then, the function pointers retains their values and the bootloader works as expected.
     
    I looked at the XC8 compiler user's guide and didn't find any special considerations for function pointers.   Anyone aware of any considerations when using function pointers with XC8 compiler and PIC18?
     
     
     
    #6
    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/05/30 20:59:58 (permalink)
    0
    It's probably something in how you are using these function pointers, but we can only speculate without seeing the code.
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #7
    ahallaj
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/01/17 00:19:12
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/05/30 21:53:57 (permalink)
    0
    Here is the code snippet on how the function pointer is being used:
     
    This function is invoked from the main. All arguments except the first one are function pointers:
    parseInterface(
    INTERFACE_RS485,
    RS485_RxCount,
    RS485_ReadFromBuffer,
    RS485_WriteMessage,
    RS485_ResetBuffer
    );
     
    This is how the above function is defined:
    uint16_t parseInterface(interfaceCount_t interface,
                                       uint16_t (*funcptr_cnt)(void),
                                       uint8_t (*funcptr_read)(uint8_t *),
                                       uint8_t (*funcptr_writeMess)(empwr_p01_msg_t *),
                                       void (*funcptr_resetBuffer)(void))
    {
     
    uint16_t rxMsgCnt = funcptr_cnt();  
    ...
    }
     
    If I erase the program flash memory range 0x4000-0x1FFFF before calling this function, when I step into funcptr_cnt (i.e. RS485_RxCount),  the program counter becomes 0 and PIC halts.  If I don't erases the program flash, RS485_RxCount is called and executes correctly.
     
     
    #8
    aschen0866
    Super Member
    • Total Posts : 4564
    • Reward points : 0
    • Joined: 2006/01/08 22:18:32
    • Location: San Diego
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/05/31 15:11:05 (permalink)
    0
    ahallaj
    ... I use function pointers in the bootloader to invoke corresponding function based on the command received from the GUI.  ...

    Tell us how a function pointer gets loaded in your code. Does the serial command contain some kind of program memory address for the function to be called?
     
    The thing you need to watch for is that the xc8 compiler does not like to cast an arbitrary number into any kind of pointer. The right thing to do is, for example,

    // Tell the compiler there is a variable at 0x100 and a function at 0x2000
    extern int foo @ 0x100;
    extern void foobar(void) @ 0x2000;
    ...
    // When it comes to pointer assignment...
    var_ptr = &foo;
    func_ptr = foobar;

    #9
    ahallaj
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/01/17 00:19:12
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/03 21:05:24 (permalink)
    0
    No, the serial command does NOT send the function memory address. It sends a message id, the message id is used as an index in an array of function pointers.   Is there any consideration for passing a function as an function pointer argument?  I showed in my previous post example.
    #10
    ric
    Super Member
    • Total Posts : 27595
    • Reward points : 0
    • Joined: 2003/11/07 12:41:26
    • Location: Australia, Melbourne
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/03 21:10:12 (permalink)
    0
    Where precisely in memory is this "array of function pointers" stored?
     

    I also post at: PicForum
    Links to useful PIC information: http://picforum.ric323.co...opic.php?f=59&t=15
    NEW USERS: Posting images, links and code - workaround for restrictions.
    To get a useful answer, always state which PIC you are using!
    #11
    ahallaj
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/01/17 00:19:12
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/03 21:27:04 (permalink)
    0
    It's a global static array:
    static cmd_func_t commandHandlers[BOOT_CMD_CNT];
     
    typedef struct
    {
    uint8_t len;
    retn_status_t (*handler)(empwr_p01_msg_t *);
    } cmd_func_t;
    #12
    ahallaj
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/01/17 00:19:12
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/03 21:28:45 (permalink)
    +1 (1)
    note that this problem is not specific to the function pointers in the array.  If you see in my previous post example, passing a function as an argument fails in the same way.
    #13
    oliverb
    Super Member
    • Total Posts : 285
    • Reward points : 0
    • Joined: 2009/02/16 13:12:38
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/04 06:55:54 (permalink)
    +1 (1)
    Have you tried checking the assembly?
    Have you tried replacing function pointers with a "case" construct?
     
    I seem to recall reading that XC8 implemented pointers to code (function pointers) as an index into a jump table rather than an actual pointer to ROM. If this was true and the jump table was located in the erased block then that might explain what you are seeing.
    #14
    NorthGuy
    Super Member
    • Total Posts : 6161
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/04 08:50:07 (permalink)
    +1 (1)
    Is there anything that prevents you from looking at what it is you're actually erasing (that is comparing the flash content of the chip before and after erase)? You may spend months guessing, but only a small effort is needed to look.
    #15
    Danno
    Super Member
    • Total Posts : 287
    • Reward points : 0
    • Joined: 2005/09/07 10:12:10
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/04 12:09:37 (permalink)
    +1 (1)
    I wonder if the erase code is erasing the debug exec.  It typically installs at the end of memory.
     
    Suggest instead of erasing up to 0x1FFFF, erase up to 0x1F7FF
     
    $.02
    d
    #16
    ahallaj
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/01/17 00:19:12
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/04 17:25:01 (permalink)
    0
    I did compare the two images and they were identical.  I first programmed the bootloader and captured the program memory the entire program memory (0-0x1FFFF).  Then, I ran the bootloader and commanded it to erase the program memory above the bootloader section (0x4000 - 0x1FFFF).  I captured the program memory after the erase which basically causes the PIC halt. 
    The content of the program memory were identical. 
    #17
    NorthGuy
    Super Member
    • Total Posts : 6161
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/04 19:37:57 (permalink)
    +1 (1)
    If the image stays the same, then you can dismiss the flash damage and alike. Thus there must be something else in your code which causes the problem. I can see three possible causes:
     
    1) some sort of malfunction in Microchip flash controller
    2) timing disturbance while the chip stalls for the erase which causes some sort of reset (such as Watchdog)
    3) some sort of bug in your code.
     
    You can easily check if it's (3). Leave your code intact, but remove the actual erase, say replace this:
     
    asm("BSF NVMCON1,1");

     
    with this:
     
    asm("NOP");

     
    If you still get the problem, this is a bug in your code. If the problem is cured, you'll have to work on distinguishing (1) and (2), which is somewhat harder to do.
     
    If possible, try to avoid the debugger, as it alters the code and may be a problem in itself.
    #18
    ahallaj
    New Member
    • Total Posts : 13
    • Reward points : 0
    • Joined: 2020/01/17 00:19:12
    • Location: 0
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/05 10:35:48 (permalink)
    0
    When I remove the "erase" or if I don't write to the program flash above address 0x10000, the code works as expected.
     
    At this point, I modified the code to remove function pointers and replaced them with actual function calls and the bootloader works as expected.
     
     
    #19
    NorthGuy
    Super Member
    • Total Posts : 6161
    • Reward points : 0
    • Joined: 2014/02/23 14:23:23
    • Location: Northern Canada
    • Status: offline
    Re: PIC18F47K42 - Bootloader erases program flash and PIC resets 2020/06/05 13:05:39 (permalink)
    +1 (1)
    Tat's very strange. Function pointers are just a piece of memory. If the memory gets corrupted, whatever is there gets corrupted as well. If you don't use function pointers, this memory is used by something else, or hopefully not used at all.
    #20
    Page: 12 > Showing page 1 of 2
    Jump to:
    © 2020 APG vNext Commercial Version 4.5