6

I am a beginner... I would like to write to a specific memory location in my embedded flash...How do I mention it in my C header file? And then link it with the specific memory location using linker scripts. Right now I have declared the array as extern and it compiles properly. While liking, I need to tell the linker that I need it at this particular location. Should it be given in .ld file? What is a .dld file? This is not for GCC, for diab compiler. I have seen a sample code bubble.dld for bubble sort. But in some projects .dld files are created while making the project. In what step is it actually created?

Christy Wald
  • 329
  • 3
  • 14
  • 1
    What? Do you have a memory-mapped flash drive? That sounds very unusual ... what hardware platform is this? – unwind Nov 30 '16 at 09:32
  • Yes, I do have a memory mapped flash.. Its a ST micro controller... – Christy Wald Nov 30 '16 at 09:40
  • @unwind Or help me with how you just write it to a normal ram location... – Christy Wald Nov 30 '16 at 09:42
  • 2
    The embedded flash on a microcontroller is not generally referred to as a "flash drive", that's what confused me. Also, it's not always easily writable, i.e. you can't always expect to just move data there, there's sometimes other things you need to do (peripheral registers to poke) in order to control the write process. You need to specify exactly which controller you're talking about for this question to be even remotely answerable. – unwind Nov 30 '16 at 10:02
  • SPC56EC64 is the micro controller used.. Thanks for the suggestion.. I have edited it. – Christy Wald Nov 30 '16 at 10:10
  • The below answer is what I was expecting... – Christy Wald Dec 08 '16 at 09:10
  • You can achieve the same thing as described in this answer, which is **not** just for GCC: https://stackoverflow.com/questions/4067811/how-to-place-a-variable-at-a-given-absolute-address-in-memory-with-gcc/4067819#4067819 *(This is without using the linker, but it may actually be simpler to leave the linker out of it.)* – Prof. Falken Dec 08 '16 at 09:17
  • @Prof.Falken The pointed solution works only if you want to store data inside ram or if you want to access to a peripheral. For flash location, you simply can't do that, because flash is a read only memory and you have to initialize it at linker step (Of course, some embedded devices offer you possibility to rewrite flash, but it a non trivial procedure) – Garf365 Dec 09 '16 at 15:11
  • @Garf365, sorry I don't follow. You use absolute addresses in your answer too? – Prof. Falken Dec 10 '16 at 21:51
  • @prof.falken The issue isn't absolute address but the access of memory. The issue here is to locate data inside flash memory, which is read only. So you can't assign data at an address located inside RO memory at runtime... you can only access it, not assign it – Garf365 Dec 11 '16 at 08:50
  • @Garf365, I still don't understand how the linker will help you there. If the memory is RO, how will the linker make it not readonly? – Prof. Falken Dec 11 '16 at 17:45
  • @prof.falken Linker generate the executable. After that, you will use a specific tool with a specific procedure to load program into the flash. All this step are done before program is executed, before runtime. – Garf365 Dec 11 '16 at 18:30
  • @Garf365, thank you, now I understand! – Prof. Falken Dec 12 '16 at 08:31

1 Answers1

6

First solution

in ".c":

// Talk to linker to place this in ".mysection"
__attribute__((section(".mysection"))) char MyArrray[52];

in ".ld":

MEMORY {
   m_interrupts (RX)    : ORIGIN = 0x00040000, LENGTH = 0x000001E8
   m_text      (RX)     : ORIGIN = 0x00050000, LENGTH = 0x000BFE18
   /* memory which will contain secion ".mysection" */
   m_my_memory (RX)       : ORIGIN = 0x00045000, LENGTH = 0x00000100
}

SECTIONS
{
  /***** Other sections *****/

  /* place "mysection" inside "m_my_memory" */
  .mysection :
  {
    . = ALIGN(4);
    KEEP(*(.mysection));
    . = ALIGN(4);
  } > m_my_memory

  /***** Other sections *****/
}

Second solution

in ".c"

extern char myArray[52];

in ".ld"

MEMORY {
   m_interrupts (RX)    : ORIGIN = 0x00040000, LENGTH = 0x000001E8
   m_text      (RX)     : ORIGIN = 0x00050000, LENGTH = 0x000BFE18
   /* memory which will contain secion "myArray" */
   m_my_memory (RX)       : ORIGIN = 0x00045000, LENGTH = 0x00000100
}

SECTIONS
{
  /***** Other sections *****/

  /* place "myArray" inside "m_my_memory" */
  .mysection :
  {
    . = ALIGN(4);
    myArray = .; /* Place myArray at current address, ie first address of m_my_memory */
    . = ALIGN(4);
  } > m_my_memory

  /***** Other sections *****/
}

See this good manual to learn more how to place elements where you want

Garf365
  • 3,619
  • 5
  • 29
  • 41
  • Thanks a lot :) . I have an exe file for my linker.. i.e., I am building it in a Windows environment. Should I rebuild the exe with my new ld file reference? – Christy Wald Nov 30 '16 at 12:10
  • You don't have to rebuild your linker, only your final app – Garf365 Nov 30 '16 at 12:44