Forum Discussion

Altera_Forum's avatar
Altera_Forum
Icon for Honored Contributor rankHonored Contributor
16 years ago

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's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Aren't you using the onchip_ram_100Kbytes for something else? Check that the Nios exception vectors are not set to that memory (double click on the Nios CPU in SOPC builder).

  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Yes I am using it for the reset vectors which starts at 0x04120000 and ends at 0x0412001f, and the exception vectors which starts at 0x04120020 and seems to end at 0x0412013f. So if the exception vectors ends at this address and the variable is located after it, there must not be a problem writing to it!

    I still can't find a clear memory map for the design.

    http://www.qtl.co.il/img/copy.png
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Could you try and put the reset and exceptions vectors to the SDRAM and see if it still hangs? This could help us determine if the problem comes from here or not.

    Even if the variable is away from the vectors list, a software bug (such as a negative offset) could overwrite something important.

    I have another question. In your line
    alt_read_flash( flash, offset, (void*) ( &rawVideo ), size); 
    what is the value of "size"? If its greater than 4, you overwrite unnallocated memory space.
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Well it seems that you were right, a negative offset is being added to the variable address!

    The "size" value is less than a region block size ( which is 65535 Byte ) and I have tried the read operation before using the same size, to different memory device than the onchip RAM, and it passed successfully.

    Maybe I will change my strategy.

    Thanks for paying attention
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    There is something else. With your

    int rawVideo __attribute__ ((section (".onchip_ram_100Kbytes.rwdata"))); 
    line, you are only allocating an integer, which is 4 bytes. This means that each time you do a read of more than 4 bytes, you overwrite a part of memory that could be used for something else, causing random crashes. You need to allocate a buffer big enough for what you want to do. Something like:
    char rawVideo __attribute__ ((section (".onchip_ram_100Kbytes.rwdata"))); 
    and then remove the & in front of rawVideo in the read call.