• AVR Freaks

Hot!How do I get the C runtime startup label into my C code

Author
alexconway
Starting Member
  • Total Posts : 37
  • Reward points : 0
  • Joined: 2009/07/14 08:40:24
  • Location: 0
  • Status: offline
2019/10/22 08:59:57 (permalink)
0

How do I get the C runtime startup label into my C code

XC8 V2.10, MPLABX 5.25, PIC18F25K42
I can't find out how to get the address of the C runtime startup code in my code, other than in assembler:
I can do this just fine
asm("GOTO "___mkstr(start)); // goto C Runtime Startup ie the same goto that is at 0x0000

but I can't figure out how to get "start" in C so that I can generate a patch for self programming, for example:
// create the code for goto start (ie the C runtime startup code)
// assuming that "start" exists in this context
byte patchBuffer[4]
patchBuffer[0] = (byte) (start & 0x0000FF);
patchBuffer[1] = 0xEF;
patchBuffer[2] = (byte) ((start & 0x00FF00) >> 8);
patchBuffer[3] = (byte) (0xF0 + ((start & 0x0F0000) >> 16))

 
obviously, "start" would not be a helpful macro to be defined, but I'd like to know how to get a MACRO that represents the C runtime startup code start address.
Thanks
Alex
#1

10 Replies Related Threads

    du00000001
    Just Some Member
    • Total Posts : 3175
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 09:43:03 (permalink)
    0
    I can hardly understand which trick you're trying to pull...
    Why not read the start address from the HEX file generated ?

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #2
    jtemples
    عُضْوٌ جَدِيد
    • Total Posts : 11407
    • Reward points : 0
    • Joined: 2004/02/13 12:31:19
    • Location: Southern California
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 09:54:50 (permalink)
    0
    What does "a patch for self programming" mean?  Are you trying to write a bootloader?  If so, you don't need the address of the startup code.
    #3
    crosland
    Super Member
    • Total Posts : 1673
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 10:06:13 (permalink)
    +2 (2)
    Have you thought of executing a reset instruction, rather than trying to jump to the startup code?
     
    A lot safer in my opinion.
    #4
    Barbiani
    Super Member
    • Total Posts : 236
    • Reward points : 0
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 11:18:21 (permalink)
    0
    Get it from the reset vector (read from flash)?
    #5
    alexconway
    Starting Member
    • Total Posts : 37
    • Reward points : 0
    • Joined: 2009/07/14 08:40:24
    • Location: 0
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 11:53:37 (permalink)
    0
    Yes, I am writing a bootloader.
    Reading if from the generated Hex file is not automatic and after the horse has bolted.
    Reset() causes the PIC to hardware reset, not what I want.
    Reading it from flash presumes that it didn't get corrupted.
    I want the startup because that is what the reset vector jumps to and it makes sure the C runtime startup code gets run.
     
    Alex
    #6
    du00000001
    Just Some Member
    • Total Posts : 3175
    • Reward points : 0
    • Joined: 2016/05/03 13:52:42
    • Location: Germany
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 12:08:49 (permalink)
    0
    Create a function pointer, make it point to the reset verctor and call this function. full stop.

    PEBKAC / EBKAC / POBCAK / PICNIC (eventually see en.wikipedia.org)
    #7
    1and0
    Access is Denied
    • Total Posts : 9900
    • Reward points : 0
    • Joined: 2007/05/06 12:03:20
    • Location: Harry's Gray Matter
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 12:12:27 (permalink)
    +1 (1)
    Options:
    • asm("GOTO 0x0000");
    • Powerup routine.
    • Lastly, there are webinars and application notes on writing bootloaders which might help you.
    #8
    alexconway
    Starting Member
    • Total Posts : 37
    • Reward points : 0
    • Joined: 2009/07/14 08:40:24
    • Location: 0
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 12:30:24 (permalink)
    0
    du00000001
    Create a function pointer, make it point to the reset verctor and call this function. full stop.


    That is a solution to a different question. My question is specific.
    #9
    alexconway
    Starting Member
    • Total Posts : 37
    • Reward points : 0
    • Joined: 2009/07/14 08:40:24
    • Location: 0
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 12:36:44 (permalink)
    0
    1and0
    Options:
    • asm("GOTO 0x0000");
    • Powerup routine.
    • Lastly, there are webinars and application notes on writing bootloaders which might help you.



    I already have the asm licked, its in my first post.
    Could you explain what you mean by "Powerup routine"
    I'm not having a problem writing a bootloader (even though that is what I'm doing), I'm having a problem getting at a symbol which I can get in asm but not in C.
    Cheers
    Alex
    #10
    alexconway
    Starting Member
    • Total Posts : 37
    • Reward points : 0
    • Joined: 2009/07/14 08:40:24
    • Location: 0
    • Status: offline
    Re: How do I get the C runtime startup label into my C code 2019/10/22 15:18:35 (permalink)
    +1 (1)
    I've found the magic incantation, in the feindly Manual (MPLAB ® XC8 C Compiler User’s Guide for PIC ® MCU)
    section 4.15.6 Linker-Defined Symbols
    _Linit is the lower link address of "init" which is a synonym for "start"
    byte patchBuffer[4];
    extern __rom char * _Linit;  // see MPLAB ® XC8 C Compiler User’s Guide for PIC ® MCU 4.15.6
    uint32_t initvec = (uint32_t) &_Linit;   // convert to address
    initvec >>= 1;                           //for goto instr
    patchBuffer[0] = (byte) (initvec & 0x0000FF);
    patchBuffer[1] = 0xEF;            // GOTO
    patchBuffer[2] = (byte) ((initvec & 0x00FF00) >> 8);
    patchBuffer[3] = (byte) (0xF0 + ((initvec & 0x0F0000) >> 16));

    Tadaah!
    post edited by alexconway - 2019/10/28 13:58:57
    #11
    Jump to:
    © 2019 APG vNext Commercial Version 4.5