reallocate main program

dumarjo wrote on Wednesday, April 01, 2009:

Hi all,

We use Freertos on an ARM7 (SAM7S from ATMEL) and we have somting that work pretty well. First we have developped an bootloader. try some in house code on the sam7s and all works fine. But if I try to use the sample from freertos, the program hangs when it’s not located to 0 in flash. IF I test the program and place it to address 0, everythings work. If I put an offset in the linker file, The program start but as soon as the task is started, the program hang and the Watchdog reset the CPU.

I have use the sample sam7x sample file for GCC.

Does anyone here have try to offset an Freertos project ? We have us ucos/II in the past without any problem . I have some problem finding what cause this kind of things.



aturowski wrote on Thursday, April 02, 2009:

The beginning of ARM memory is occupied by reset/exceptions vectors. They have to be always there because CPU is forced to executes the code from there when reset/exception occurs. Please check first if these vectors placed at 0x00000 address after you have made your offset.

Best regards,

dumarjo wrote on Thursday, April 02, 2009:


This is what i conclude. The SWI handler is the problem I think. I need to be able to put this in RAM. When we use ucos/II, we don’t have this problem because they don’t use this SWI interrupt.

Now we try to figure out how to get the SWI handler remaped to RAM.

If someone have done this and would liek to share their startup asm file, ld + makefile, I’ll appreciate.


aturowski wrote on Thursday, April 02, 2009:

I am using this kind of assembly file:

/* exceptions and interrupt vectors table setup */
.section .startup,"ax"
         .align 0

    b     start                             /* jump from here just after reset */
    b     vUndefinedInstructionException
    b     vPortYieldProcessor               /* used for manual calling SWI instruction to call the scheduler and task yielding */
    b     vPrefetchAbortException
    b     vDataAbortException
    b     vReservedException
    ldr   pc,[pc, # -0xF20]                 /* jump to the interrupt exception handler address given in AIC register*/
    b     vFiqException

This is setting up exception/reset vector table in section called ".startup". In linker script I simply put this section where it belongs:

   FLASH  : ORIGIN = 0x0, LENGTH = 256K
   RAM : ORIGIN = 0x200000, LENGTH = 64K   /* length of RAM */

__ram_end__ = RAM_ORIGIN + RAM_SIZE;

/* setup TEXT section */
  . = 0;
  __text_beg__ = .;
  startup :
  } > FLASH

Of course you can hardcode this section origin address in assembly, but I prefer to have that control in linker file.

Hope that helps.


dumarjo wrote on Thursday, April 02, 2009:

HI Adam,

This is very interesting.

With this setup, you can have your vector in flash, but the vector a realocated in the linker script ? I see that in you ld script, you have you startup section to addr 0, but if I change the FLASH ORIGIN to 0x1780 for exemple, the startup section will be place at 0x1780 ?

am I wrong ?


aturowski wrote on Thursday, April 02, 2009:

Yes, if you change FLASH origin, the section .startup address will change accordingly. Please enable and check your *.map file generated during linking stage for all needed information.

incrediball wrote on Tuesday, April 07, 2009:

The problem with a bootloader is that it defines the default vector table and you would need to get the bootloader interrupt handling to relay all the interrupts through to the main program. This is messy, may not support all of the interrupt types (e.g. SWI in your case) and adds latency to the interrupt handling. To use get around this problem modify your main program so:

1. Define your ROM or FLASH sector at 0x00100000 plus the size of the bootloader sector. For example if you reserve 4K for the bootloader (this corresponds to the first lockable page on the AT91SAM7S64) then define it at 0x00101000.

Note that the flash memory on the AT91SAM7S is really located at 0x00100000 so doing this is not a trick. This memory is only mirrored to 0x00000000 by default.

2. Locate your vector table at 0x00200000, for example:

/* Exception vectors located at 0x00200000. These are re-mapped to 0x00000000 by the re-map command */
/* Note that undf, pabt, dabt just execute a null loop. */

        .section    .vectors
        b    .        /* 0x00 reset - not used since the processor has already reset when it re-maps the memory */
        b    .                    /* 0x04 undef */
        ldr    pc, _swi_ram        /* 0x08 swi */
        b    .                    /* 0x0C pabt */
        b    .                    /* 0x10 dabt */
        nop                        /* 0x14 rsvd */
        ldr    pc, [pc,#-0xF20]    /* 0x18 irq - load PC with the interrupt vector in AT91C_BASE_AIC->AIC_IVR */
        ldr    pc, [pc,#-0xF20]    /* 0x1c fiq - load PC with the interrupt vector in AT91C_BASE_AIC->AIC_FVR */
_swi_ram:    .word vPortYieldProcessor

You will need to modify your linker script to tell the linker to put the vectors section first in the RAM.

3. Perform a remap command as soon as the main program starts up. This mirrors the vector table defined at 0x00200000 to 0x00000000 thus replacing the vector table defined by the bootloader.