| [6] | 1 | /********************************************************************* | 
|---|
|  | 2 | *                    SEGGER Microcontroller GmbH                     * | 
|---|
|  | 3 | *                        The Embedded Experts                        * | 
|---|
|  | 4 | ********************************************************************** | 
|---|
|  | 5 | *                                                                    * | 
|---|
|  | 6 | *            (c) 2014 - 2020 SEGGER Microcontroller GmbH             * | 
|---|
|  | 7 | *                                                                    * | 
|---|
|  | 8 | *       www.segger.com     Support: support@segger.com               * | 
|---|
|  | 9 | *                                                                    * | 
|---|
|  | 10 | ********************************************************************** | 
|---|
|  | 11 | *                                                                    * | 
|---|
|  | 12 | * All rights reserved.                                               * | 
|---|
|  | 13 | *                                                                    * | 
|---|
|  | 14 | * Redistribution and use in source and binary forms, with or         * | 
|---|
|  | 15 | * without modification, are permitted provided that the following    * | 
|---|
|  | 16 | * condition is met:                                                  * | 
|---|
|  | 17 | *                                                                    * | 
|---|
|  | 18 | * - Redistributions of source code must retain the above copyright   * | 
|---|
|  | 19 | *   notice, this condition and the following disclaimer.             * | 
|---|
|  | 20 | *                                                                    * | 
|---|
|  | 21 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND             * | 
|---|
|  | 22 | * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,        * | 
|---|
|  | 23 | * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF           * | 
|---|
|  | 24 | * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE           * | 
|---|
|  | 25 | * DISCLAIMED. IN NO EVENT SHALL SEGGER Microcontroller BE LIABLE FOR * | 
|---|
|  | 26 | * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR           * | 
|---|
|  | 27 | * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT  * | 
|---|
|  | 28 | * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;    * | 
|---|
|  | 29 | * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF      * | 
|---|
|  | 30 | * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT          * | 
|---|
|  | 31 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE  * | 
|---|
|  | 32 | * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH   * | 
|---|
|  | 33 | * DAMAGE.                                                            * | 
|---|
|  | 34 | *                                                                    * | 
|---|
|  | 35 | ********************************************************************** | 
|---|
|  | 36 |  | 
|---|
|  | 37 | -------------------------- END-OF-HEADER ----------------------------- | 
|---|
|  | 38 |  | 
|---|
|  | 39 | File      : SEGGER_THUMB_Startup.s | 
|---|
|  | 40 | Purpose   : Generic runtime init startup code for ARM CPUs running | 
|---|
|  | 41 | in THUMB mode. | 
|---|
|  | 42 | Designed to work with the SEGGER linker to produce | 
|---|
|  | 43 | smallest possible executables. | 
|---|
|  | 44 |  | 
|---|
|  | 45 | This file does not normally require any customization. | 
|---|
|  | 46 |  | 
|---|
|  | 47 | Additional information: | 
|---|
|  | 48 | Preprocessor Definitions | 
|---|
|  | 49 | FULL_LIBRARY | 
|---|
|  | 50 | If defined then | 
|---|
|  | 51 | - argc, argv are setup by the debug_getargs. | 
|---|
|  | 52 | - the exit symbol is defined and executes on return from main. | 
|---|
|  | 53 | - the exit symbol calls destructors, atexit functions and then debug_exit. | 
|---|
|  | 54 |  | 
|---|
|  | 55 | If not defined then | 
|---|
|  | 56 | - argc and argv are not valid (main is assumed to not take parameters) | 
|---|
|  | 57 | - the exit symbol is defined, executes on return from main and loops | 
|---|
|  | 58 | */ | 
|---|
|  | 59 |  | 
|---|
|  | 60 | .syntax unified | 
|---|
|  | 61 |  | 
|---|
|  | 62 | /********************************************************************* | 
|---|
|  | 63 | * | 
|---|
|  | 64 | *       Defines, configurable | 
|---|
|  | 65 | * | 
|---|
|  | 66 | ********************************************************************** | 
|---|
|  | 67 | */ | 
|---|
|  | 68 |  | 
|---|
|  | 69 | #ifndef   APP_ENTRY_POINT | 
|---|
|  | 70 | #define APP_ENTRY_POINT main | 
|---|
|  | 71 | #endif | 
|---|
|  | 72 |  | 
|---|
|  | 73 | #ifndef   ARGSSPACE | 
|---|
|  | 74 | #define ARGSSPACE 128 | 
|---|
|  | 75 | #endif | 
|---|
|  | 76 |  | 
|---|
|  | 77 | /********************************************************************* | 
|---|
|  | 78 | * | 
|---|
|  | 79 | *       Externals | 
|---|
|  | 80 | * | 
|---|
|  | 81 | ********************************************************************** | 
|---|
|  | 82 | */ | 
|---|
|  | 83 | .extern APP_ENTRY_POINT     // typically main | 
|---|
|  | 84 |  | 
|---|
|  | 85 | /********************************************************************* | 
|---|
|  | 86 | * | 
|---|
|  | 87 | *       Global functions | 
|---|
|  | 88 | * | 
|---|
|  | 89 | ********************************************************************** | 
|---|
|  | 90 | */ | 
|---|
|  | 91 | /********************************************************************* | 
|---|
|  | 92 | * | 
|---|
|  | 93 | *       _start | 
|---|
|  | 94 | * | 
|---|
|  | 95 | *  Function description | 
|---|
|  | 96 | *    Entry point for the startup code. | 
|---|
|  | 97 | *    Usually called by the reset handler. | 
|---|
|  | 98 | *    Performs all initialisation, based on the entries in the | 
|---|
|  | 99 | *    linker-generated init table, then calls main(). | 
|---|
|  | 100 | *    It is device independent, so there should not be any need for an | 
|---|
|  | 101 | *    end-user to modify it. | 
|---|
|  | 102 | * | 
|---|
|  | 103 | *  Additional information | 
|---|
|  | 104 | *    At this point, the stack pointer should already have been | 
|---|
|  | 105 | *    initialized: | 
|---|
|  | 106 | *      - For typical programs executiong from flash, the stack pointer | 
|---|
|  | 107 | *        has already been loaded from vector table at offset 0. | 
|---|
|  | 108 | *      - In other cases, such as RAM Code, this is done by the debugger | 
|---|
|  | 109 | *        or the device-specific startup code / reset handler. | 
|---|
|  | 110 | */ | 
|---|
|  | 111 | .section .init, "ax" | 
|---|
|  | 112 | .balign 2 | 
|---|
|  | 113 | .global _start | 
|---|
|  | 114 | .thumb_func | 
|---|
|  | 115 | .code 16 | 
|---|
|  | 116 | _start: | 
|---|
|  | 117 | // | 
|---|
|  | 118 | // Call linker init functions which in turn performs the following: | 
|---|
|  | 119 | // * Perform segment init | 
|---|
|  | 120 | // * Perform heap init (if used) | 
|---|
|  | 121 | // * Call constructors of global Objects (if any exist) | 
|---|
|  | 122 | // | 
|---|
|  | 123 | ldr     R4, =__SEGGER_init_table__      // Set table pointer to start of initialization table | 
|---|
|  | 124 | __SEGGER_init_run_loop: | 
|---|
|  | 125 | ldr     R0, [R4]                        // Get next initialization function from table | 
|---|
|  | 126 | adds    R4, R4, #4                      // Increment table pointer to point to function arguments | 
|---|
|  | 127 | blx     R0                              // Call initialization function | 
|---|
|  | 128 | b.n     __SEGGER_init_run_loop | 
|---|
|  | 129 | // | 
|---|
|  | 130 | .thumb_func | 
|---|
|  | 131 | .global __SEGGER_init_done | 
|---|
|  | 132 | __SEGGER_init_done: | 
|---|
|  | 133 | // | 
|---|
|  | 134 | // Time to call main(), the application entry point. | 
|---|
|  | 135 | // | 
|---|
|  | 136 | #ifndef FULL_LIBRARY | 
|---|
|  | 137 | // | 
|---|
|  | 138 | // In a real embedded application ("Free-standing environment"), | 
|---|
|  | 139 | // main() does not get any arguments, | 
|---|
|  | 140 | // which means it is not necessary to init R0 and R1. | 
|---|
|  | 141 | // | 
|---|
|  | 142 | bl      APP_ENTRY_POINT                 // Call to application entry point (usually main()) | 
|---|
|  | 143 | // | 
|---|
|  | 144 | // Fall-through to exit if main ever returns. | 
|---|
|  | 145 | // | 
|---|
|  | 146 | .global exit | 
|---|
|  | 147 | .thumb_func | 
|---|
|  | 148 | exit: | 
|---|
|  | 149 | // | 
|---|
|  | 150 | // In a free-standing environment, if returned from application: | 
|---|
|  | 151 | // Loop forever. | 
|---|
|  | 152 | // | 
|---|
|  | 153 | b       . | 
|---|
|  | 154 | #else | 
|---|
|  | 155 | // | 
|---|
|  | 156 | // In a hosted environment, | 
|---|
|  | 157 | // we need to load R0 and R1 with argc and argv, in order to handle | 
|---|
|  | 158 | // the command line arguments. | 
|---|
|  | 159 | // This is required for some programs running under control of a | 
|---|
|  | 160 | // debugger, such as automated tests. | 
|---|
|  | 161 | // | 
|---|
|  | 162 | movs    R0, #ARGSSPACE | 
|---|
|  | 163 | ldr     R1, =args | 
|---|
|  | 164 | bl      SEGGER_SEMIHOST_GetArgs | 
|---|
|  | 165 | ldr     R1, =args | 
|---|
|  | 166 | bl      APP_ENTRY_POINT                 // Call to application entry point (usually main()) | 
|---|
|  | 167 | // | 
|---|
|  | 168 | // Fall-through to exit if main ever returns. | 
|---|
|  | 169 | // | 
|---|
|  | 170 | .global exit | 
|---|
|  | 171 | .thumb_func | 
|---|
|  | 172 | exit: | 
|---|
|  | 173 | // | 
|---|
|  | 174 | // In a hosted environment exit gracefully, by | 
|---|
|  | 175 | // saving the return value, | 
|---|
|  | 176 | // calling destructurs of global objects, | 
|---|
|  | 177 | // calling registered atexit functions, | 
|---|
|  | 178 | // and notifying the host/debugger. | 
|---|
|  | 179 | // | 
|---|
|  | 180 | mov     R5, R0                  // Save the exit parameter/return result | 
|---|
|  | 181 | // | 
|---|
|  | 182 | // Call destructors | 
|---|
|  | 183 | // | 
|---|
|  | 184 | ldr     R0, =__dtors_start__    // Pointer to destructor list | 
|---|
|  | 185 | ldr     R1, =__dtors_end__ | 
|---|
|  | 186 | dtorLoop: | 
|---|
|  | 187 | cmp     R0, R1 | 
|---|
|  | 188 | beq     dtorEnd                 // Reached end of destructor list? => Done | 
|---|
|  | 189 | ldr     R2, [R0]                // Load current destructor address into R2 | 
|---|
|  | 190 | add     R0, #4                  // Increment pointer | 
|---|
|  | 191 | push    {R0-R1}                 // Save R0 and R1 | 
|---|
|  | 192 | blx     R2                      // Call destructor | 
|---|
|  | 193 | pop     {R0-R1}                 // Restore R0 and R1 | 
|---|
|  | 194 | b       dtorLoop | 
|---|
|  | 195 | dtorEnd: | 
|---|
|  | 196 | // | 
|---|
|  | 197 | // Call atexit functions | 
|---|
|  | 198 | // | 
|---|
|  | 199 | bl      __SEGGER_RTL_execute_at_exit_fns | 
|---|
|  | 200 | // | 
|---|
|  | 201 | // Call debug_exit with return result/exit parameter | 
|---|
|  | 202 | // | 
|---|
|  | 203 | mov     R0, R5 | 
|---|
|  | 204 | bl      SEGGER_SEMIHOST_Exit | 
|---|
|  | 205 | // | 
|---|
|  | 206 | // If execution is not terminated, loop forever | 
|---|
|  | 207 | // | 
|---|
|  | 208 | exit_loop: | 
|---|
|  | 209 | b       exit_loop // Loop forever. | 
|---|
|  | 210 | #endif | 
|---|
|  | 211 |  | 
|---|
|  | 212 | #ifdef FULL_LIBRARY | 
|---|
|  | 213 | .bss | 
|---|
|  | 214 | args: | 
|---|
|  | 215 | .space ARGSSPACE | 
|---|
|  | 216 | #endif | 
|---|
|  | 217 |  | 
|---|
|  | 218 | /*************************** End of file ****************************/ | 
|---|