Forum Discussion

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

HELP assigning function to specific address

Hi there.

I have coded my own bootloader and i need to assign the loaded applications main function and a variable to a specific address that my bootloader calls.

Is there a way to force the linker/compiler to place a variable or function at a specific memory address by using the __attribute__ command in my c code.

I know that :

__attribute__ ((section (".my_mem_name.rwdata")));

assigns the varable or function to a specific memory section, but not a memory specific address.

And I have jet another question

My bootloader app is placed in internal memory so it executes every time I reset the nois, then it loads a user app into sdram. This works fine but when I jump to "entry" (as defined in the objdump file) nothing happens. If I jump to "main" it seems to work and starts the user program, but if I do this, the user program still uses the exceptions table, heap and stack asignments from the bootloader app.

How do I init the assignments from user program?

Hopes this makes sense to someone.

12 Replies

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

    Hi Rasdan,

    > Yes I guess this would be fatal, but only if the bootloader .exception segment

    > is placed is internal ram.

    Correct. If the exception address is in SDRAM, the bootloader _should_ always

    copy its .exception section to the exception address -- so no problem.

    > After this the bootloader (my code) probably overwrites the exception segment with

    > the user app and calls it. My user application doesn’t need to copy anything because

    > all segments (except reset) are defined in sdram so they are already there.

    > If I, at this point, reset the cpu the bootloader should work just fine.

    >

    > Is this correct?

    Correct.

    > This only works IF the bootloader is freestanding and it doesn’t call :

    >

    > alt_irq_init (ALT_IRQ_BASE);

    >

    > Any use of this function in the bootloader will somehow crash the user application

    > when i call (_start).

    > I don't understand why?

    > Do u have any clue to this?

    This all gets back to alt_main -- when your app is "hosted" the HAL alt_main is

    called ... which in turn calls alt_sys_init. And alt_sys_init is the most likely culprit.

    alt_irq_init just clears the ienable (disables all external hardware interrupts), then

    sets status.PIE (enables CPU response to external interrupts) -- not a problem ...

    ... at least not yet:

    When alt_sys_init is called, the drivers for your hardware are initialized and

    interrupts get enabled. My guess is: (1) you have at least 1 timer, (2) it's selected

    as the system clock in your syslib properties, (3) the interrupt gets enabled, and

    finally (4) the interrupt is asserted and causes problems while you are downloading

    (and overwriting the exception trampoline). You can test this by selecting "none"

    for your system clock timer -- see if things get better ;-)

    Let us know how you make out :-)

    Regards,

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

    Hi Scott,

    >Let us know how you make out :-)

    Status today: Everything seems to work.

    I tested the interrupt system using a timer in my user application

    and it works perfect.

    At this time I have no further problems.

    I can see a lot of people have been looking at this topic.

    So for anyone reading this topic and reaching all the way down to this post.

    I would like to sum up everything I did to get this far.

    I will not comment on why the settings are the way they are.

    This info can be found in the text above (replys).

    So here goes:

    TARGET:

    1. Make an independent bootloader that loads a user application from a nios hardware interface into SDRAM.

    (this interface could be anything rs232, parallel port, usb, TCP/IP or custom something).

    My project uses a custom interface between a PowerPC and the nios.

    2. Bootloader has to call and execute the user application at the end of user application transfer.

    3. A nios reset should restart the bootloader so the user application

    can be updated or reloaded.

    NIOS HARDWARE minimum:

    Nios cpu (no comment)

    Internal on chip memory (program memory for bootloader)

    User application memory (SDRAM in my case)

    Interface hardware (my PowerPC - NIOS interface, anything could be used).

    SOPC SETTINGS:

    Reset address: on chip ram

    Exception address: user application memory (sdram)

    Break location: whatever

    SOFTWARE SETTINGS (Nios IDE)

    1. Bootloader:

    Program mem (.text): on chip ram

    Read only mem (.rodata) : on chip ram

    Read/write mem (.rwdata) : on chip ram

    Heap mem: on chip ram

    Stack mem: on chip ram

    System clock timer: select none

    2. User application

    Program mem (.text): SDRAM

    Read only mem (.rodata) : SDRAM

    Read/write mem (.rwdata) : SDRAM

    Heap mem: SDRAM

    Stack mem: SDRAM

    SOFTWARE BOOTLOADER

    This should be a freestanding application.

    Use hallo freestanding as template.

    Delete any init functions calls in this code

    (These will probably cause something bad to happen).

    //alt_sys_init();

    //alt_sys_init();

    //alt_io_redirect();

    The first thing the bootloader should do is to

    copy the user program into the user application memory.

    Can’t help with this. This depends on the interface you are using)

    When this is done all that is left is to call the user application, flush icache and dcache.

    alt_icache_flush_all();

    alt_dcache_flush_all();

    AppStartP = (void*)0x10001c8; // _start address (found in objdump file)

    AppStartP(); // call application

    SOFTWARE USER APP:

    Do whatever you need to do.

    GENERATION OF USER APPLICATION UPLOAD FILE

    Click Start->All programs->Altera->Nios II development kit -> nios II SDK shell

    In the console use elf2hex:

    elf2hex --input=userapp.elf --start=0x1000000 --end=0x1ffffff –width= 8

    This creates a hex file named userapp.hex that contains the user program.

    This can be changed to binary or whatever are needed using normal hex2bin tools.

    It’s not needed to set end addr to the end of the sdram(my case) only to the end of user application (look in objdump file)

    ---------------------------------------------

    This seems real simple when you know how.

    -comments to Scott

    >My guess is: (1) you have at least 1 timer

    Correct! What a great guess

    > It’s selected as the system clock in your syslib properties

    Correct again!

    Now Scott how do you know all this (all of it)? Your understanding of the nios system seems amazing,

    you deserve the NIOS GOD status of your profile.

    Your help has been amazing and I’m very grateful to you, but also to the nios

    community for making this possible.

    Thank you all

    Feel free to comment on anything.

    I will be reading any comments.

    Regards,

    --Rasdan