Fixed address variable in C

前端 未结 10 1053
迷失自我
迷失自我 2020-12-01 12:47

For embedded applications, it is often necessary to access fixed memory locations for peripheral registers. The standard way I have found to do this is something like the f

相关标签:
10条回答
  • 2020-12-01 13:34

    Often, for embedded software, you can define within the linker file one area of RAM for linker-assigned variables, and a separate area for variables at absolute locations, which the linker won't touch.

    Failing to do this should cause a linker error, as it should spot that it's trying to place a variable at a location already being used by a variable with absolute address.

    0 讨论(0)
  • 2020-12-01 13:35

    A linker would typically use a linker script to determine where variables would be allocated. This is called the "data" section and of course should point to a RAM location. Therefore it is impossible for a variable to be allocated at an address not in RAM.

    You can read more about linker scripts in GCC here.

    0 讨论(0)
  • 2020-12-01 13:37

    Your linker handles the placement of data and variables. It knows about your target system through a linker script. The linker script defines regions in a memory layout such as .text (for constant data and code) and .bss (for your global variables and the heap), and also creates a correlation between a virtual and physical address (if one is needed). It is the job of the linker script's maintainer to make sure that the sections usable by the linker do not override your IO addresses.

    0 讨论(0)
  • 2020-12-01 13:43

    To expand on litb's answer, you can also use the --just-symbols={symbolfile} option to define several symbols, in case you have more than a couple of memory-mapped devices. The symbol file needs to be in the format

    symbolname1 = address;
    symbolname2 = address;
    ...
    

    (The spaces around the equals sign seem to be required.)

    0 讨论(0)
提交回复
热议问题