]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_MPU_LM3Sxxxx_Rowley/thumb_crt0.s
10fc4cd033f9a0e9cb671111ee0e5a360ef4e67c
[freertos] / FreeRTOS / Demo / CORTEX_MPU_LM3Sxxxx_Rowley / thumb_crt0.s
1 /*****************************************************************************\r
2  * Copyright (c) 2009 Rowley Associates Limited.                             *\r
3  *                                                                           *\r
4  * This file may be distributed under the terms of the License Agreement     *\r
5  * provided with this software.                                              *\r
6  *                                                                           *\r
7  * THIS FILE IS PROVIDED AS IS WITH NO WARRANTY OF ANY KIND, INCLUDING THE   *\r
8  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *\r
9  *****************************************************************************/\r
10 \r
11 /*****************************************************************************\r
12  *                           Preprocessor Definitions\r
13  *                           ------------------------\r
14  * APP_ENTRY_POINT\r
15  *\r
16  *   Defines the application entry point function, if undefined this setting\r
17  *   defaults to "main".\r
18  *\r
19  * USE_PROCESS_STACK\r
20  *\r
21  *   If defined, thread mode will be configured to use the process stack if\r
22  *   the size of the process stack is greater than zero bytes in length.\r
23  *\r
24  * INITIALIZE_STACK\r
25  *\r
26  *   If defined, the contents of the stack will be initialized to a the\r
27  *   value 0xCC.\r
28  *\r
29  * FULL_LIBRARY\r
30  *\r
31  *  If defined then\r
32  *    - argc, argv are setup by the debug_getargs.\r
33  *    - the exit symbol is defined and executes on return from main.\r
34  *    - the exit symbol calls destructors, atexit functions and then debug_exit.\r
35  *\r
36  *  If not defined then\r
37  *    - argc and argv are zero.\r
38  *    - no exit symbol, code loops on return from main.\r
39  *****************************************************************************/\r
40 \r
41 #ifndef APP_ENTRY_POINT\r
42 #define APP_ENTRY_POINT main\r
43 #endif\r
44 \r
45 #ifndef ARGSSPACE\r
46 #define ARGSSPACE 128\r
47 #endif\r
48 \r
49   .global _start\r
50   .syntax unified\r
51   .extern APP_ENTRY_POINT\r
52 #ifdef FULL_LIBRARY\r
53   .global exit\r
54 #endif\r
55 \r
56   .section .init, "ax"\r
57   .code 16\r
58   .align 2\r
59   .thumb_func\r
60 \r
61 _start:\r
62 #ifdef __RAM_BUILD\r
63   ldr r1, =__stack_end__\r
64   mov sp, r1\r
65 #endif\r
66 #ifdef INITIALIZE_STACK\r
67   mov r2, #0xCC\r
68   ldr r0, =__stack_start__\r
69 #ifndef __RAM_BUILD\r
70   mov r1, sp\r
71 #endif\r
72   bl memory_set\r
73 #endif\r
74 \r
75 #ifdef USE_PROCESS_STACK\r
76   /* Set up process stack if size > 0 */\r
77   ldr r1, =__stack_process_end__\r
78   ldr r0, =__stack_process_start__\r
79   subs r2, r1, r0\r
80   beq 1f\r
81   msr psp, r1\r
82   mov r2, #2\r
83   msr control, r2\r
84 #ifdef INITIALIZE_STACK\r
85   mov r2, #0xCC\r
86   bl memory_set\r
87 #endif\r
88 1:\r
89 #endif\r
90   /* Copy initialised memory sections into RAM (if necessary). */\r
91   ldr r0, =__data_load_start__\r
92   ldr r1, =__data_start__\r
93   ldr r2, =__data_end__\r
94   bl memory_copy\r
95   ldr r0, =__text_load_start__\r
96   ldr r1, =__text_start__\r
97   ldr r2, =__text_end__\r
98   bl memory_copy\r
99   ldr r0, =__fast_load_start__\r
100   ldr r1, =__fast_start__\r
101   ldr r2, =__fast_end__\r
102   bl memory_copy\r
103   ldr r0, =__ctors_load_start__\r
104   ldr r1, =__ctors_start__\r
105   ldr r2, =__ctors_end__\r
106   bl memory_copy\r
107   ldr r0, =__dtors_load_start__\r
108   ldr r1, =__dtors_start__\r
109   ldr r2, =__dtors_end__\r
110   bl memory_copy\r
111   ldr r0, =__rodata_load_start__\r
112   ldr r1, =__rodata_start__\r
113   ldr r2, =__rodata_end__\r
114   bl memory_copy\r
115 \r
116   /* Zero the bss. */\r
117   ldr r0, =__bss_start__\r
118   ldr r1, =__bss_end__\r
119   mov r2, #0\r
120   bl memory_set\r
121 \r
122   /* Zero the privileged data. */\r
123   ldr r0, =__privileged_data_start__\r
124   ldr r1, =__privileged_data_end__\r
125   mov r2, #0\r
126   bl memory_set\r
127 \r
128   /* Initialise the heap */\r
129   ldr r0, = __heap_start__\r
130   ldr r1, = __heap_end__\r
131   sub r1, r1, r0\r
132   mov r2, #0\r
133   str r2, [r0]\r
134   add r0, r0, #4\r
135   str r1, [r0]\r
136 \r
137   /* Call constructors */\r
138   ldr r0, =__ctors_start__\r
139   ldr r1, =__ctors_end__\r
140 ctor_loop:\r
141   cmp r0, r1\r
142   beq ctor_end\r
143   ldr r2, [r0]\r
144   add r0, #4\r
145   push {r0-r1}\r
146   blx r2\r
147   pop {r0-r1}\r
148   b ctor_loop\r
149 ctor_end:\r
150 \r
151   /* Setup initial call frame */\r
152   mov r0, #0\r
153   mov lr, r0\r
154   mov r12, sp\r
155 \r
156 start:\r
157   /* Jump to application entry point */\r
158 #ifdef FULL_LIBRARY\r
159   mov r0, #ARGSSPACE\r
160   ldr r1, =args\r
161   ldr r2, =debug_getargs\r
162   blx r2\r
163   ldr r1, =args\r
164 #else\r
165   mov r0, #0\r
166   mov r1, #0\r
167 #endif\r
168   ldr r2, =APP_ENTRY_POINT\r
169   blx r2\r
170 \r
171 #ifdef FULL_LIBRARY\r
172   .thumb_func\r
173 exit:\r
174   mov r5, r0 // save the exit parameter/return result\r
175 \r
176   /* Call destructors */\r
177   ldr r0, =__dtors_start__\r
178   ldr r1, =__dtors_end__\r
179 dtor_loop:\r
180   cmp r0, r1\r
181   beq dtor_end\r
182   ldr r2, [r0]\r
183   add r0, #4\r
184   push {r0-r1}\r
185   blx r2\r
186   pop {r0-r1}\r
187   b dtor_loop\r
188 dtor_end:\r
189 \r
190   /* Call atexit functions */\r
191   ldr r2, =_execute_at_exit_fns\r
192   blx r2\r
193 \r
194   /* Call debug_exit with return result/exit parameter */\r
195   mov r0, r5\r
196   ldr r2, =debug_exit\r
197   blx r2\r
198 #endif\r
199 \r
200   /* Returned from application entry point, loop forever. */\r
201 exit_loop:\r
202   b exit_loop\r
203 \r
204 memory_copy:\r
205   cmp r0, r1\r
206   beq 2f\r
207   subs r2, r2, r1\r
208   beq 2f\r
209 1:\r
210   ldrb r3, [r0]\r
211   add r0, r0, #1\r
212   strb r3, [r1]\r
213   add r1, r1, #1\r
214   subs r2, r2, #1\r
215   bne 1b\r
216 2:\r
217   bx lr\r
218 \r
219 memory_set:\r
220   cmp r0, r1\r
221   beq 1f\r
222   strb r2, [r0]\r
223   add r0, r0, #1\r
224   b memory_set\r
225 1:\r
226   bx lr\r
227 \r
228 #ifdef FULL_LIBRARY\r
229   .bss\r
230 args:\r
231   .space ARGSSPACE\r
232 #endif\r
233 \r