• AVR Freaks

Hot!__at( flash address) function attribute

Author
Rickyz
New Member
  • Total Posts : 22
  • Reward points : 0
  • Joined: 2013/10/18 04:52:48
  • Location: 0
  • Status: offline
2020/02/20 03:39:38 (permalink)
0

__at( flash address) function attribute

Using XC8 (V2.05)
on PIC16F18877
I have been able to use the __at(0xfe00) attribute to place a function in a specific memory location.
I tried the same on another project using the PIC18F57K42, unfortunately even though it did locate the function at the required location all the rest of the code was allocated in memory following it.
 
What is the difference between the linking for 16F and 18F.  Is there a directive I can use to fix this?
Thanks
 
#1

12 Replies Related Threads

    JPortici
    Super Member
    • Total Posts : 1023
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/20 04:29:14 (permalink)
    0
    Yes. I believe the way the linker works is very different, between 18F and the rest of the 8bit PICs.
    What could you do: you could try defining a section and placing the function in that section (Section 4.8.3, or look at "absolute function" in the index)
    #2
    crosland
    Super Member
    • Total Posts : 1940
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/20 04:30:27 (permalink)
    +2 (2)
    Rickyz all the rest of the code was allocated in memory following it.

     
    Why is that a problem?
    #3
    Rickyz
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2013/10/18 04:52:48
    • Location: 0
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/20 05:03:45 (permalink)
    0
    I want my Flash functions high up in Flash to keep them away from the application code.
     
     
    #4
    Rickyz
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2013/10/18 04:52:48
    • Location: 0
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/20 05:06:13 (permalink)
    0
    Good call, I tried to use a section but it does not appear the compiler is using the lkr file I created...., off I go down another rabbit hole. 
     
    __attribute__((section("FLASH_CODE"))) uint16_t PFM_ReadWord(uint32_t address)
    {
    uint16_t pfmWord = 0;
    NVMCON1bits.REG0 = 0; // Select Flash Memory
    NVMCON1bits.REG1 = 1; // Allow access to flash memory

    TBLPTRU = (address & 0xf0000) >> 16;
    TBLPTRH = (address & 0xff00) >> 8;
    TBLPTRL = (address & 0xff);
    asm("TBLRD*+");
    pfmWord = (uint16_t)(TABLAT <<8);
    asm("TBLRD*+");
    pfmWord |= TABLAT;
    return (uint16_t) pfmWord;
    }
     
    #5
    JPortici
    Super Member
    • Total Posts : 1023
    • Reward points : 0
    • Joined: 2012/11/17 06:27:45
    • Location: Grappaland
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/20 08:20:16 (permalink)
    +3 (3)
    XC8 for PIC doesn't use linker files. You pass options to the linker specifying section names and allocations
    Have you read the documentation about code sections and relocation? It worked just fine for me
    #6
    crosland
    Super Member
    • Total Posts : 1940
    • Reward points : 0
    • Joined: 2005/05/10 10:55:05
    • Location: Warks, UK
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/20 13:24:08 (permalink)
    +1 (1)
    Rickyz
    I want my Flash functions high up in Flash to keep them away from the application code.

     
    I have to ask why? :)
     
    I am sure I read in another thread, quite recently, that XC8 allocates FLASH from top down.
     
    That seems to be borne out by the project I am working on currently. It's a bootloadable application so the reset and ISR prologue code is at CODEOFFSET, then there's a large gap, then the rest of my code up to (almost) top of memory. I think (but may be wrong) that the linker is also reserving memory for the debug executive which I have also done manually with the linker memory range. I am used to having to do this from C18 lonker scripts.
     
    [Edit] I RTFM and indeed XC8 reserves space for the debug executive.
    Google search found a post stating that XC8 allocates from bottom up and top down with a gap in the middle.[/edit]
     
    You may be just fighting the tools trying to separate functions. You can create your own sections but it's difficult to predict how large they need to be.
    post edited by crosland - 2020/02/20 13:35:24
    #7
    Rickyz
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2013/10/18 04:52:48
    • Location: 0
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/21 00:43:51 (permalink)
    0
    Thanks... 
    I am begging to think the compiler hates me.
     
    in Extra linker options 
    -L-AFL=4000h-4FFFh -Lpflash=FL
    and code.
     uint16_t __section("flash") PFM_ReadWord(uint32_t address)
     
    The function is not at 4000, I have read the documentation about sending boxes to france etc. I have tried every combination. 
     
     
    Looking at the MAP file whatever name I give to __section("name") it just defines the allocation for that memory used by the function as "name".  There seems to be no link (excuse the pun) between the __section and psect.
     
    I have used this on hundreds of other types of processor and never struggled like this.  Spent all yesterday trying to get this working.
     
     
     
     
     
    #8
    Rickyz
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2013/10/18 04:52:48
    • Location: 0
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/21 01:59:20 (permalink)
    0
    So linker options seem to work with Constants but not functions.  Also when the constant was placed in memory all code followed the code space allocated to the constant.  This was the same issue I had with the __at() .
     
    So it looks like code does not get placed in the psect but constants (or maybe variables) do.  And I suspect if it would work with code then other code gets allocated behind it.
     
    This is feeling like a bug, but no point posting to Microchip support.
     
     
    #9
    Rickyz
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2013/10/18 04:52:48
    • Location: 0
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/21 02:00:59 (permalink)
    0
    Information
     
    MPLAB X IDE V5.30
    Compiler Version 2.05 (but tried others).
    Processor PIC18F57K42
     
     
     
    #10
    Rickyz
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2013/10/18 04:52:48
    • Location: 0
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/21 03:38:28 (permalink)
    0
    I agree it has worked fine for me too on other processors.  Have you tried on the PIC18F57K42?
     
    #11
    Rickyz
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2013/10/18 04:52:48
    • Location: 0
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/21 05:38:49 (permalink)
    +1 (1)
    ** Update ** compiling with V1.45 the __section("name") works.  So its broken in version 2.x or you just have to use the __at() directive.
     
    That said the code still follows the re-located function.  So after 2 days I am back to square 1 battling with XC8....
     
    Why can't this just be easy?
    #12
    Rickyz
    New Member
    • Total Posts : 22
    • Reward points : 0
    • Joined: 2013/10/18 04:52:48
    • Location: 0
    • Status: offline
    Re: __at( flash address) function attribute 2020/02/24 08:28:35 (permalink)
    0
    So
    1. With XC8 Version 2.10 the __section() only works with data not code.
    2. Using CODEOFFSET allows functions to be placed below the offset using the __at() however the RESET Vector remains 00 but the Interrupt vectors are still loaded.
    3. As soon as CODEOFFSET is used code is populated from top of memory down.
     
    The problem is the Reset Vector and Interrupt vectors are going to change with code updates.  You need to be able to update them, and without the __at() working correctly its impossible to place. 
     
    In the END I used Memory Model and set the ROM range to half the flash size, then the __at allowed me to place functions high in memory out of the way just the way I intended.
     
     
    #13
    Jump to:
    © 2020 APG vNext Commercial Version 4.5