问题
I am working on TM4C1294 + ccs 6.0.1 + TI 5.1.9(Compiler).
Reading/Writing variables in 8MB external sdram worked fine for example g_pui16EPISdram = (uint16_t *)0x60000000;
g_pui16EPISdram[SDRAM_END_ADDRESS ] = 0xdcba;
However, as I use "attribute directive" for accessing to external sdram like below:int abab[20]__attribute__ ((section (".external")));
, the program is not even executed.
".external" is defined .cmd :
SDRAM (RWX) : origin = 0x60000000, length = 0x00800000
SECTIONS
{
.intvecs: > APP_BASE
.text : > FLASH
.const : > FLASH
.cinit : > FLASH
.pinit : > FLASH
.init_array : > FLASH
.vtable : > RAM_BASE
.data : > SRAM
.bss : > SRAM
.sysmem : > SRAM
.stack : > SRAM
.external : > SDRAM
}
Here is my .map
:
SEGMENT ALLOCATION MAP
60000000 60000000 00000050 00000000 rw-
60000000 60000000 00000050 00000000 rw- .external
GLOBAL SYMBOLS: SORTED ALPHABETICALLY BY Name
address name
-------- ----
60000000 abab
In my project, some of "structs" ,that need to be extern
variables, are declared in one .h
and defined in one .c
;however, for test I just simply used int abab[]
.
I have tried to test with/without "extern" ahead of "attribute directive", in a nutshell both did not work.
How can I access/use extern variables stored in the external sdram?
回答1:
First of all, I think there is a misunderstanding on the concepts of using an external storage medium to read/write data and usage of extern keyword in C programming language.
Here's a simple explanation:
1) According to your code, what you're doing is just setting the starting g_pui16EPISdram = (uint16_t *)0x60000000
and ending g_pui16EPISdram[SDRAM_END_ADDRESS ] = 0xdcba
addresses of SDRAM module for memory mapping to get used by the microprocessor. As you can see in the SECTIONS block, .data, .bss, .sysmem and .stack are defined in SRAM (Static RAM). Therefore, if you try to move (or to point) the required data, which is obviously needed by the microcontroller for execution, to an external medium, it will not even be executed. Because, microcontroller does not know where to find it and execute it. It is something like plug a USB memory stick into a PC and expecting to boot from it without appropriate BIOS settings. Since BIOS does not know where the bootstrap code is, the PC will not boot from that medium. Target binary address, to be executed, must be known by the processor. And in your case, it has nothing to do with extern keyword. You may want to move your program data only, (int abab[20]
array for example) to SDRAM section in your code and make your program reach that external memory region safely as being executed by microcontroller.
2) extern keyword is used for declaring external variables that are visible from files other than the one in which they are defined in C programming language. This modifier can be used with all data types like char
, short
, int
, long
, float
, double
, arrays, pointers, structures, functions etc. Basically, this keyword informs the compiler that the variable or function has an external linkage and search for that data structure in all incoming source files. Which is also another way to tell the compiler that "There is a variable or function declared in somewhere and you, compiler, should know this! (actually, linker does the dirty job and you say to compiler that let the linker find it!)" . To make an analogy, extern
can be thought of as opposite to the static
keyword.
回答2:
First, the variable you want to place into SDRAM should be statically allocated, usually a global variable.
Then in your source file, declare it as
#pragma DATA_SECTION(abab, ".abab_sect")
int abab[20];
Then if you add -m
to your linker options, a .map file will be generated and show you that abab
symbol is placed into a .abab_sect
section, which is allocated in a default memory space, probably in SRAM.
Then in your cmd file, add
SECTIONS
{
...
...
.abab_sect : > SDRAM
}
to move it to SDRAM
回答3:
the problem has been resolved. EPI set-up
for SDRAM was required in boot routine
since boot routines try to initialize global variables before calling main(). #pragma
and attribute
directive are not really a matter for using the external sdram.
Thanks for commenting on my question.
来源:https://stackoverflow.com/questions/29784102/how-to-access-variables-in-external-sdram