Forum Discussion

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

Running from Flash

Hello

I have a basic question: can I run sofftware from Flash memory (i.e. define the program memory and the ROM memory in Flash while leaving the read-write memory in sram for example)? I know the IDE lets you do that, but when I try it doesn't work. I used the Cyclone evaluation board and the full featured design. Then I created a hello world example. I verified that it worked when running from SRAM, but when I use the flash memory for program memory and ROM memory, It does not pront anything.

I would also like to know whether I can debug such a program when it runs from Flash.

Regards

Nir

6 Replies

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

    Do you need to regenerate the hardware platform to let the processor start execution from the flash address after reset?

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

    No, I don't think so, because this setting in the SOPC builder second tab just telss the processor where to go after a reset. When I donwload a new elf file to the processor using the IDE, it tells it to start from another address (the one the program starts from). During the download process I see that the data is being downloaded into the flash (seeing the addresses being written). However, nothing gets printed. Has anyone have experience with running from flash memory ?

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

    The bad news is that to get true "push button" run from flash support, you will need to wait for the 1.1 version of the kit. However the good news is that you can get it working for yourself in the 1.0 version without too much extra effort.

    The problem is that currently when you assign the text and read only data sections to flash, no boot loader will be present in the system to load the initial values of the read/write data into RAM, which is why your code failed.

    Here's a recipe for creating a run from flash system using the 1.0 kit:

    1. Create a Nios II system where the reset and exception vectors both point to flash.

    2. Assign the .text and .rodata sections to the flash device that contains the reset vector, and the .rwdata section to an appropriate RAM device.

    3. Build the project so that you create the auto-generated linker script (generated.x). Copy this linker script to another location within the system library project, and then edit it to replace the current .rwdata section with:

    .rwdata :AT (LOADADDR (.rodata) + SIZEOF (.rodata))

    {

    PROVIDE (__ram_rwdata_start = ABSOLUTE(.));

    . = ALIGN(32 / 8);

    *(.got.plt) *(.got)

    *(.data1)

    *(.data .data.* .gnu.linkonce.d.*)

    _gp = ABSOLUTE(. + 0x8000);

    PROVIDE(gp = _gp);

    *(.sdata .sdata.* .gnu.linkonce.s.*)

    *(.sdata2 .sdata2.* .gnu.linkonce.s2.*)

    . = ALIGN(32 / 8);

    _edata = ABSOLUTE(.);

    PROVIDE (edata = ABSOLUTE(.));

    PROVIDE (__ram_rwdata_end = ABSOLUTE(.));

    } > sdram

    PROVIDE (__flash_rwdata_start = LOADADDR(.rwdata));

    (Note: you need to replace "sdram" with the name of the RAM component you're using for the .rwdata section).

    This causes the initial contents of the .rwdata section to be stored in flash, but be linked as if it is located in RAM.

    4. Add the line:

    . = ALIGN(32 / 8);

    to the end of the .rodata section (just like in the .rwdata section above). This ensures that the start of the .rwdata section is word aligned.

    5. Select "Custom Linker Script" on the system library properties page, and then set the linker script to be the one you've just created (Note that the linker script must be part of your system library project for it to be found by the IDE).

    6. Create a source file in your system library project which contains the following function:

    # include "alt_types.h"

    void alt_load_section (alt_u32* from, alt_u32* to, alt_u32* end)

    {

    if (to != from)

    {

    while( to != end )

    {

    *to++ = *from++;

    }

    }

    }

    7. Copy alt_main.c from the altera_hal component into your system library project, and then add the following call to the top of the alt_main function:

    alt_load_section (&__flash_rwdata_start,

    &__ram_rwdata_start,

    &__ram_rwdata_end);

    you will also need to add the following external declarations to alt_main.c:

    extern void __flash_rwdata_start;

    extern void __ram_rwdata_start;

    extern void __ram_rwdata_end;

    8. Rebuild the projects, and program the executable as normal using the flash programmer.

    The only remaining gotcha is that you won't be able to use software breakpoints when debugging code in flash. Instead you'll need to use hardware breakpoints.

    Hope that helps, and good luck!
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Thanks monkeyboy

    It looks like quite a lengthly procedure. I will try it anyway. However, I have two other questions:

    1. Is it possible to run the program from flash without running the Flash Programmer ? i.e I want to just run it from the IDE, have the IDE download it to the flash and run, the same way it runs from RAM.

    2. Will software breakpoints be supported in the future ? Isn't it possible to use software breakpoints that are determined before the compilation and download ?
  • Altera_Forum's avatar
    Altera_Forum
    Icon for Honored Contributor rankHonored Contributor

    Hi,

    --- Quote Start ---

    originally posted by nir@Oct 28 2004, 12:42 AM

    1. is it possible to run the program from flash without running the flash programmer ? i.e i want to just run it from the ide, have the ide download it to the flash and run, the same way it runs from ram.

    --- Quote End ---

    For 1.1 this will almost be possible. You will need to download the program using the flash programmer but will be able to ask Eclipse to reset the processor and start debugging without doing a download.

    For 1.0 and 1.01 you can't do it with Eclipse so you'll have to use insight. Here are the steps:

    1) Use the flash programmer to set up the flash contents.

    2) Start two SDK shells and for both of them cd into <your project>/Release (or debug).

    3) In the first shell run `nios2-download --reset-target --tcpport=2342 --cable=usb-blaster` (change the cable argument as appropriate or omit it if you only have one cable).

    4) In the second shell run `nios2-elf-insight <project.elf>`

    5) Open an insight console window if there isn&#39;t one (view -> console)

    6) In the console type `target remote :2342`

    7) Now you should be able to step through your code. Step over won&#39;t work because that sets breakpoints, and soft breakpoints don&#39;t work in flash.

    <div class='quotetop'>QUOTE </div>

    --- Quote Start ---

    2. Will software breakpoints be supported in the future ? Isn&#39;t it possible to use software breakpoints that are determined before the compilation and download ?[/b]

    --- Quote End ---

    For 1.1 you will be able to compile in a BREAK instruction and have the debugger stop there. In C code use __asm__ volatile ("break"); in the appropriate place.

    You can use the same syntax in 1.0 but the 1.0 debugger sometimes gets confused by compiled in BREAK instructions - you may not be able to continue after hitting a compiled in break without hacking the registers somewhat.