Statup code

Hi,

I have a problem with LPC 2148 (try to enable interrupts) and I don’t understand something in the startup code.

Here is header of cstartup.s (IAR)

I don’t understand how 0x18 from ldr pc,[pc,#0x18] is obtained.

I another example for lpc2378 I’ve found:

__irq_handler:

ldr pc,[pc, #-0x0120]

And what dc32 __iar_program_start lines stands for?

If someone could explain the code below would be very nice. Tnx

 MODULE  ?cstartup

    SECTION CSTACK:DATA:NOROOT(3)
    SECTION SVC_STACK:DATA:NOROOT(3)
    SECTION IRQ_STACK:DATA:NOROOT(3)
    SECTION FIQ_STACK:DATA:NOROOT(3)
    SECTION UND_STACK:DATA:NOROOT(3)
    SECTION ABT_STACK:DATA:NOROOT(3)

    SECTION .intvec:CODE:NOROOT(2)

    PUBLIC  __vector
    PUBLIC  __iar_program_start
    PUBLIC  __vector_0x14
    PUBLIC  __undef_handler
    PUBLIC  __swi_handler
    PUBLIC  __prefetch_handler
    PUBLIC  __data_handler
    PUBLIC  __irq_handler
    PUBLIC  __fiq_handler
        
    ARM

__vector:
	ldr	pc,[pc,#0x18]
__undef_handler:
    ldr	pc,[pc,#0x18]
__swi_handler:
    ldr	pc,[pc,#0x18]
__prefetch_handler:
    ldr	pc,[pc,#0x18]
__data_handler:
    ldr	pc,[pc,#0x18]
__vector_0x14:
    dc32 0xFFFFFFFF
__irq_handler:
    ldr pc,[pc, #0x18]
__fiq_handler:
    ldr	pc,[pc,#0x18]
    
    dc32	__iar_program_start
    dc32	__undef_handler
    dc32	__swi_handler
    dc32	__prefetch_handler
    dc32	__data_handler
    dc32	0xFFFFFFFF
    dc32	0xFFFFFFFF
    dc32	__fiq_handler

have you browsed around the LPC2000 user forum?

http://www.embeddedrelated.com/groups/lpc2000/1.php

lots of sub-topics, by chip type

stevech:
have you browsed around the LPC2000 user forum?

http://www.embeddedrelated.com/groups/lpc2000/1.php

lots of sub-topics, by chip type

Yes, but I never found an explanation to that cstartup code

If you posed your question there, you’ll have a larger audience

http://www.embeddedrelated.com/groups/l … /33719.php

Baiazid:
I don’t understand how 0x18 from ldr pc,[pc,#0x18] is obtained.

The exception vector table is 0x20 bytes long. PC relative addressing works with an offset of 8 (2 words), so 0x20 becomes 0x18. Each entry basically jumps to the address contained in the address table, which starts 0x20 bytes after the vector table and is defined by the dc32 directives.

Baiazid:
I another example for lpc2378 I’ve found:

__irq_handler:

ldr pc,[pc, #-0x0120]

There’s a similar trick on the Atmel AT91SAM chips as well. The interrupt controller is mapped somewhere up in the 0xffffxxxx area, and this offset loads pc directly from the register in the interrupt controller which contains the address of the ISR (with which you’ve initialized the controller previously) corresponding to the interrupt you’re servicing. In short, the exception vectors directly to the appropriate ISR.

Baiazid:
And what dc32 __iar_program_start lines stands for?

Judging from its usage, I’d say it defines a word in memory and populates it with the address supplied as an operand. These directives form the address table I referred to earlier. It probably stands for “define a 32 bit constant”.

Baiazid:
If someone could explain the code below would be very nice. Tnx

I pretty much just did.

I don’t know what the group of SECTION lines does, but SECTION .intvec probably means that the following code, up to the next SECTION line (of which there are none in this sample) is placed in the .intvec section, which is important to the linker. The linker most probably places that section at address 0x00000000 or at an address in FLASH that is mapped there on boot.

The PUBLIC lines make the specified symbols global so that other code can link against them. To place handler addresses there during runtime, for example. There is usually some remapping voodoo which involves copying the first (at least) 0x40 bytes from FLASH to RAM and remapping so that the RAM is now at 0x00000000, which is why it makes sense to write handler addresses there at runtime.

I use gcc et al, so I have to guess at the precise meaning of things like dc32, SECTION and PUBLIC. In my world, they’re .word, .section, and .global.

Thank you Lou. Now I understand how they calculate.

:wink: