EmbDev.net

Forum: ARM programming with GCC/GNU tools Trying to get C++ projct running on AT91SAM9G20-EK on SDRAM


von Robin Mueller (Guest)



Rate this post
useful
not useful
Hi there,

I've been trying for some days to get a C++ project running. 
Unfortunately, there appears to be a problem with the board startup 
script and the linker script which were provided and are propably 
generated. These files we initially not intended to incorporate C++ code 
so now we're trying to adapt them to do just that. We are also using 
freeRTOS. I attached the code of the two files
1
/* ----------------------------------------------------------------------------
2
 *         ATMEL Microcontroller Software Support
3
 * ----------------------------------------------------------------------------
4
 * Copyright (c) 2008, Atmel Corporation
5
6
 * ----------------------------------------------------------------------------
7
 */
8
9
/*------------------------------------------------------------------------------
10
 *      Linker script for running in external SDRAM on the AT91SAM9G20
11
 *----------------------------------------------------------------------------*/
12
 
13
_Min_Heap_Size = 0x400;      /* required amount of heap. Added manually  */
14
_Min_Stack_Size = 0x800;    /* required amount of stack. Added manually */
15
16
OUTPUT_FORMAT("elf32-littlearm", "elf32-littlearm", "elf32-littlearm")
17
OUTPUT_ARCH(arm)
18
ENTRY(entry)
19
20
MEMORY
21
{
22
    sram0 (W!RX) : ORIGIN = 0x200000, LENGTH = 16K
23
    sram1 (W!RX) : ORIGIN = 0x300000, LENGTH = 16K
24
    sdram (W!RX) : ORIGIN = 0x20000000, LENGTH = 32M /* this board provided 64M, but we are just using  32M to model another device which only has 32M external SDRAM */
25
 }
26
27
SECTIONS
28
{
29
    .fixed :
30
  {
31
        . = ALIGN(4);      /* tells the linker that these section ought to be word aligned */
32
        _sfixed = .;
33
    KEEP(*(.startup));
34
        *(.text*)        /* Code is placed in .text */
35
        *(.rodata*)        /* global variables marked as const. are placed in .rodata */
36
        *(.glue_7)        /* glue arm to thumb code */
37
        *(.glue_7t)        /* glue thumb to arm code */
38
        *(.eh_frame)
39
        *(.data)
40
        *(.data*)        /* contains all initialized global and static variables */
41
        *(.CP15_*)  /* required for system control coprocessor? */
42
        KEEP (*(.init))
43
      KEEP (*(.fini))
44
        . = ALIGN(4);
45
    /* preinit data */
46
    PROVIDE_HIDDEN (__preinit_array_start = .);
47
    KEEP(*(.preinit_array))
48
    PROVIDE_HIDDEN (__preinit_array_end = .);
49
    
50
    . = ALIGN(4);
51
    /* init data, the constructors are placed in this sections */
52
    PROVIDE_HIDDEN (__init_array_start = .);
53
    KEEP(*(SORT(.init_array.)))
54
    KEEP(*(.init_array))
55
    PROVIDE_HIDDEN (__init_array_end = .);
56
    
57
    . = ALIGN(4);
58
    /* finit data, destructors are placed in this section */
59
    PROVIDE_HIDDEN (__fini_array_start = .);
60
    KEEP(*(SORT(.fini_array.)))
61
    KEEP(*(.fini_array))
62
    PROVIDE_HIDDEN (__fini_array_end = .);
63
  } >sdram
64
65
  .ARM.extab (NOLOAD) : {
66
    *(.ARM.extab* .gnu.linkonce.armextab.*)
67
  } >sdram 
68
    
69
  __exidx_start = .;
70
    .ARM.exidx (NOLOAD) : {
71
    *(.ARM.exidx* .gnu.linkonce.armexidx.*)
72
  } > sdram
73
    __exidx_end = .;
74
    _efixed = .;
75
76
    .prerelocate : AT (_efixed) /* all relocation stuff is pretty much taken from atmel */
77
    {
78
        . = ALIGN(4);
79
        _sprerelocate = .;
80
        . = ALIGN(4);
81
        _eprerelocate = .;
82
    }
83
84
    .postrelocate : AT (_sprerelocate + SIZEOF(.prerelocate))
85
    {
86
        . = ALIGN(4);
87
        _spostrelocate = .;
88
        KEEP(*(.vectors));
89
    *(.sramfunc)
90
        *(.ramfunc)
91
        . = ALIGN(4);
92
        _epostrelocate = .;
93
    } >sram0
94
    
95
96
    /* this section holds uninitialized global and static data  */
97
   . = ALIGN(4);
98
  .bss :
99
    {
100
      /* This is used by the startup in order to initialize the .bss secion */
101
      _sbss = .;         /* define a global symbol at bss start */
102
      __bss_start__ = _sbss;
103
      *(.bss)
104
      *(.bss*)
105
      *(COMMON)
106
  
107
      . = ALIGN(4);
108
      _ebss = .;         /* define a global symbol at bss end */
109
      __bss_end__ = _ebss;
110
    } >sdram
111
112
    /* User_heap_stack section, used to check that there is enough RAM left */
113
    ._user_heap_stack :
114
    {
115
      . = ALIGN(4);
116
      PROVIDE ( end = . );
117
      PROVIDE ( _end = . );
118
      . = . + _Min_Heap_Size;
119
      . = . + _Min_Stack_Size;
120
      . = ALIGN(4);
121
    } >sdram
122
123
  /* top of stack */
124
    _sstack = 0x22000000 - 1024;
125
}


And the start up script:
1
/* ----------------------------------------------------------------------------
2
 *         ATMEL Microcontroller Software Support 
3
 * --------------------------------------------------------------------------
4
  * Copyright (c) 2008, Atmel Corporation
5
* ----------------------------------------------------------------------------
6
 */
7
8
//------------------------------------------------------------------------------
9
//         Headers
10
//------------------------------------------------------------------------------
11
12
#include "board.h"
13
14
//------------------------------------------------------------------------------
15
//         Definitions
16
//------------------------------------------------------------------------------
17
18
#define IRQ_STACK_SIZE   8*3*4
19
20
#define ARM_MODE_ABT     0x17
21
#define ARM_MODE_FIQ     0x11
22
#define ARM_MODE_IRQ     0x12
23
#define ARM_MODE_SVC     0x13
24
25
#define I_BIT            0x80
26
#define F_BIT            0x40
27
28
//------------------------------------------------------------------------------
29
//         Startup routine
30
//------------------------------------------------------------------------------
31
32
            .align      4
33
            .arm
34
        
35
/* Exception vectors
36
 *******************/
37
            .section    .vectors, "a", %progbits
38
39
resetVector:
40
        ldr     pc, =resetHandler       /* Reset */
41
undefVector:
42
        b       undefVector             /* Undefined instruction */
43
swiVector:
44
    ldr     pc, =vPortYieldProcessor /* Software interrupt */
45
46
prefetchAbortVector:
47
        b       prefetchAbortVector     /* Prefetch abort */
48
dataAbortVector:
49
        b       dataAbortVector         /* Data abort */
50
reservedVector:
51
        b       reservedVector          /* Reserved for future use */
52
irqVector:
53
        b       irqHandler              /* Interrupt */
54
fiqVector:
55
                                        /* Fast interrupt */
56
//------------------------------------------------------------------------------
57
/// Handles a fast interrupt request by branching to the address defined in the
58
/// AIC.
59
//------------------------------------------------------------------------------
60
fiqHandler:
61
        b       fiqHandler
62
  
63
//------------------------------------------------------------------------------
64
/// Handles incoming interrupt requests by branching to the corresponding
65
/// handler, as defined in the AIC. Supports interrupt nesting.
66
//------------------------------------------------------------------------------
67
irqHandler:
68
69
/* Save interrupt context on the stack to allow nesting */
70
        sub     lr, lr, #4
71
        stmfd   sp!, {lr}
72
        mrs     lr, SPSR
73
        stmfd   sp!, {r0, lr}
74
75
/* Write in the IVR to support Protect Mode */
76
        ldr     lr, =AT91C_BASE_AIC
77
        ldr     r0, [lr, #AIC_IVR]
78
        str     lr, [lr, #AIC_IVR]
79
80
/* Branch to interrupt handler in Supervisor mode */
81
        msr     CPSR_c, #ARM_MODE_SVC
82
        stmfd   sp!, {r1-r3, r4, r12, lr}
83
        blx     r0
84
        
85
/* Restore scratch/used registers and LR from User Stack */
86
/* Disable Interrupt and switch back in IRQ mode */      
87
        ldmia   sp!, {r1-r3, r4, r12, lr}
88
        msr     CPSR_c, #ARM_MODE_IRQ | I_BIT
89
90
/* Acknowledge interrupt */
91
        ldr     lr, =AT91C_BASE_AIC
92
        str     lr, [lr, #AIC_EOICR]
93
94
/* Restore interrupt context and branch back to calling code */
95
        ldmia   sp!, {r0, lr}
96
        msr     SPSR_cxsf, lr
97
        ldmia   sp!, {pc}^
98
99
//------------------------------------------------------------------------------
100
/// Initializes the chip and branches to the main() function.
101
//------------------------------------------------------------------------------
102
            .section    .text
103
            .global     entry
104
105
entry:
106
resetHandler:
107
108
/* Useless instruction for referencing the .vectors section */
109
        ldr     r0, =resetVector
110
111
/* Set pc to actual code location (i.e. not in remap zone) */
112
      ldr     pc, =1f
113
114
/* Initialize the prerelocate segment */
115
1:
116
        ldr     r0, =_efixed
117
        ldr     r1, =_sprerelocate
118
        ldr     r2, =_eprerelocate
119
1:
120
        cmp     r1, r2
121
        ldrcc   r3, [r0], #4
122
        strcc   r3, [r1], #4
123
        bcc     1b
124
125
/* Perform low-level initialization of the chip using LowLevelInit() */
126
        ldr     sp, =_sstack
127
        stmfd   sp!, {r0}
128
      ldr     r0, =LowLevelInit
129
        blx     r0
130
131
/* Initialize the postrelocate segment */
132
133
        ldmfd   sp!, {r0}
134
        ldr     r1, =_spostrelocate
135
        ldr     r2, =_epostrelocate
136
1:
137
        cmp     r1, r2
138
        ldrcc   r3, [r0], #4
139
        strcc   r3, [r1], #4
140
        bcc     1b
141
142
/* Clear the zero segment */
143
      ldr     r0, =__bss_start__
144
        ldr     r1, =__bss_end__
145
        mov     r2, #0
146
1:
147
        cmp     r0, r1
148
        strcc   r2, [r0], #4
149
        bcc     1b
150
151
/* Setup stacks
152
 **************/
153
/* IRQ mode */
154
        msr     CPSR_c, #ARM_MODE_IRQ | I_BIT | F_BIT
155
        ldr     sp, =_sstack
156
        sub     r4, sp, #IRQ_STACK_SIZE
157
158
/* Supervisor mode (interrupts enabled) */
159
        msr     CPSR_c, #ARM_MODE_SVC | F_BIT
160
        mov     sp, r4
161
162
/* Call static constructors */
163
      ldr r0,=__libc_init_array
164
    mov lr,pc /* set the return address */
165
    bx r0 /* the target code can be ARM or THUMB */
166
/* Branch to main()
167
 ******************/
168
        ldr     r0, =main
169
        blx     r0
170
171
/* Loop indefinitely when program is finished */
172
1:
173
        b       1b

Do you have any idea why are program is crashing randomly? Right now it 
always crashes and jumps to an adress it is not intended to be. I 
attached the dissassembly when  this happends while debugging.
My guess is that this relates to the two files shown above. Maybe 
something is missing so that C++ is working properly? The program 
crashes when the freeRTOS task scheduler has already started. I hope 
someone has a clue how  to get this working. Especially someone who 
managed to get C++ working on ARM processors. Thanks a lot in advance !

Kind Regards,
Robin

von Robin M. (spacefish)


Rate this post
useful
not useful
The program does not crash anymore when I comment out code related to 
hardware drivers. The program does not crash when performing these 
functions but crashes at the adresses I have shown in the 2 pictures. 
This is really confusing.
I use the AT91SAM9G20-EK board and provided drivers. This is inside a 
function which is called by performOperation() which crashed
1
//ATMEL
2
AT91S_USART * rs232; // do not initialize this register handle, causes problems !
3
USART_Configure(rs232, AT91C_US_USMODE_NORMAL,9600,BOARD_MCK);
4
USART_SetReceiverEnabled(rs232, true);
5
USART_SetTransmitterEnabled(rs232,true);
6
result = USART_IsDataAvailable(rs232);
7
if(result==1) {
8
  info << "Data available !" << std::endl;
9
}

: Edited by User
Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.