Forum Discussion
How can I allocate a variable to a specific section?
I want a variable to be allocated in the on-chip RAM. I have all sections
(.text, .rodata, .rwdata, .heap, and .stack) in DDR2_SDRAM in the auto-generated linker script. I define a global variable "rawVideo" as I have seen in many examples: --- Quote Start --- int rawVideo __attribute__ ((section (".onchip_ram_100Kbytes.rwdata"))); --- Quote End --- where the on-chip is specified in the system.h:onchip_ram_100kbytes_name "/dev/onchip_ram_100kbytes" Then I copy some data from the flash device to it: --- Quote Start --- alt_read_flash( flash, offset, (void*) ( &rawVideo ), size); --- Quote End --- but the program execution suspends with error when applying the reading command! I made sure that the on-chip RAM size fits on the copy. So am I allocating the variable in a non-existed section or using a wrong syntax?
16 Replies
- Altera_Forum
Honored Contributor
Are you reading from a CFI flash device? If so, then there's no need to use the HAL's flash layer to read from it. You can use memcpy (or the like) directly.
If you're reading from a serial flash, are you checking the return code of alt_read_flash? Are you "opening" your flash properly...and checking the return codes there, as well? Best Regards and Good Luck! - Brendan - Altera_Forum
Honored Contributor
I am reading from two EPCS64 flash memory serial configuration devices. And I have tried to do this READ operation before and it was correct when assertion the return code. The opening always goes well, but when I read from the flash device to the on-chip RAM something goes wrong.
I am not sure if the target memory section specification is correct or it needs a previous definition in the Nios II IDE. Thanks Brendan - Altera_Forum
Honored Contributor
It could be something more like:
The special .* sections are only created for the main memory that you select for text heap and stack. Could you show us the begining of your linker script? It is in the Debug/system_description folder of your syslib and is called generated.x You can also add at the begining of your code this line:int rawVideo __attribute__ ((section ("onchip_ram_100Kbytes")));
It will display the variable address and you can check if it has been placed in the correct section. Why you want this variable there instead of the DDR?printf("Address of rawVideo is 0x%8.8x\n",&rawVideo); - Altera_Forum
Honored Contributor
I want this variable to represent a raw frame which the algorithm will access frequently. So allocating this frame in the onchip RAM will increase the processing speed.
Here you the linker script: --- Quote Start --- MEMORY { reset : ORIGIN = 0x04120000, LENGTH = 32 epcs_controller : ORIGIN = 0x04141800, LENGTH = 2048 onchip_ram_100Kbytes : ORIGIN = 0x04120020, LENGTH = 99968 ssram : ORIGIN = 0x04000000, LENGTH = 1048576 ddr2_sdram : ORIGIN = 0x00000000, LENGTH = 67108864 } /* Define symbols for each memory base-address */ __alt_mem_epcs_controller = 0x04141800 ; __alt_mem_onchip_ram_100Kbytes = 0x04120000 ; __alt_mem_ssram = 0x04000000 ; __alt_mem_ddr2_sdram = 0x00000000 ; --- Quote End --- I displayed the the variable address and it is correct: --- Quote Start --- Address of rawVideo is 0x041201c8 --- Quote End --- but the read operation cannot be done. The question is must I define the corresponding section (i.e. .onchip_ram_100Kbytes.rwdata), and if so, how can that be done? It seems that I have to modify the linker memory regions using the BSP Build Tools? If there is another way to do so, I will be appreciated if you mention it because I have a deadline to finish my work and I am afraid that the BSP Build Tools software will take much time. Thanks a lot.http://www.qtl.co.il/img/copy.png http://www.qtl.co.il/img/copy.png - Altera_Forum
Honored Contributor
From what I can see, the rawVideo variable is indeed in the onchip_ram_100Kbytes, so your line worked. You don't need to create a section or modify the linker script.
The fact that the read operation fails must have another reason. - Altera_Forum
Honored Contributor
Yes you are right.
It's a little bit confusing that I could do the transfer operation from the flash to the SDRAM, but I couldn't do so to the on-chip RAM! Anyway thanks for giving your time. - Altera_Forum
Honored Contributor
Could you compile the memory test example in the IDE (putting rodata/rwdata, text stack and heap in the SDRAM) and have it test the onchip ram? That way you'll at least know if the hardware is ok.
- Altera_Forum
Honored Contributor
Now I know where the failure happens. I did the test for the full span of the onchip RAM but it failed!
The console just show the first msg: --- Quote Start --- Testing RAM from 0x4120000 to 0x4120100 --- Quote End --- then the program stops responding. So I retested it starting with offset equal to 16 KByte (which is the 8 KByte instruction cache and 8 KByte data cache), and the test was successful. I didn't know that the cache and the onchip RAM defined in SOPC builder are dependent (so the 16 KByte cache are included in the 100 KByte RAM !) However, I still can't read from the flash device into a well allocated variable on the onchip RAM. So I am thinking of separating the onchip RAM into two pieces and store my variable in one of them. And still the question why the attribution doesn't allocate the variable properly and overlapped the cache? - Altera_Forum
Honored Contributor
The cache and the on-chip memory are not overlapping and are independant so I don't think that this is the reason. What offset are you talking about?
- Altera_Forum
Honored Contributor
Sorry for miss-explanation. I was wrong about the cache issue.
The memtest fails between the following two addresses: --- Quote Start --- 0x04120000 - 0x0412013F (0x04120000 is the OnChip base address and its span is 100000) --- Quote End --- and passes for the rest of the ram range.