| 1 | /*********************************************************************
|
|---|
| 2 | * SEGGER Microcontroller GmbH *
|
|---|
| 3 | * The Embedded Experts *
|
|---|
| 4 | **********************************************************************
|
|---|
| 5 | * *
|
|---|
| 6 | * (c) 2014 - 2024 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 : STM32G4xx_Startup.s
|
|---|
| 40 | Purpose : Startup and exception handlers for STM32G4xx devices.
|
|---|
| 41 |
|
|---|
| 42 | Additional information:
|
|---|
| 43 | Preprocessor Definitions
|
|---|
| 44 | __NO_SYSTEM_INIT
|
|---|
| 45 | If defined,
|
|---|
| 46 | SystemInit is not called.
|
|---|
| 47 | If not defined,
|
|---|
| 48 | SystemInit is called.
|
|---|
| 49 | SystemInit is usually supplied by the CMSIS files.
|
|---|
| 50 | This file declares a weak implementation as fallback.
|
|---|
| 51 |
|
|---|
| 52 | __NO_SYSTEM_CLK_UPDATE
|
|---|
| 53 | If defined,
|
|---|
| 54 | SystemCoreClockUpdate is not automatically called.
|
|---|
| 55 | Should be defined if SystemCoreClockUpdate must not be called before main().
|
|---|
| 56 | If not defined,
|
|---|
| 57 | SystemCoreClockUpdate is called before the application entry point.
|
|---|
| 58 |
|
|---|
| 59 | __MEMORY_INIT
|
|---|
| 60 | If defined,
|
|---|
| 61 | MemoryInit is called after SystemInit.
|
|---|
| 62 | void MemoryInit(void) can be implemented to enable external
|
|---|
| 63 | memory controllers.
|
|---|
| 64 |
|
|---|
| 65 | __VECTORS_IN_RAM
|
|---|
| 66 | If defined,
|
|---|
| 67 | the vector table will be copied from Flash to RAM,
|
|---|
| 68 | and the vector table offset register is adjusted.
|
|---|
| 69 |
|
|---|
| 70 | __VTOR_CONFIG
|
|---|
| 71 | If defined,
|
|---|
| 72 | the vector table offset register is set to point to the
|
|---|
| 73 | application's vector table.
|
|---|
| 74 |
|
|---|
| 75 | __NO_FPU_ENABLE
|
|---|
| 76 | If defined, the FPU is explicitly not enabled,
|
|---|
| 77 | even if the compiler could use floating point operations.
|
|---|
| 78 |
|
|---|
| 79 | __SOFTFP__
|
|---|
| 80 | Defined by the build system.
|
|---|
| 81 | If not defined, the FPU is enabled for floating point operations.
|
|---|
| 82 | */
|
|---|
| 83 |
|
|---|
| 84 | .syntax unified
|
|---|
| 85 |
|
|---|
| 86 |
|
|---|
| 87 | /*********************************************************************
|
|---|
| 88 | *
|
|---|
| 89 | * Global functions
|
|---|
| 90 | *
|
|---|
| 91 | **********************************************************************
|
|---|
| 92 | */
|
|---|
| 93 | /*********************************************************************
|
|---|
| 94 | *
|
|---|
| 95 | * Reset_Handler
|
|---|
| 96 | *
|
|---|
| 97 | * Function description
|
|---|
| 98 | * Exception handler for reset.
|
|---|
| 99 | * Generic bringup of a Cortex-M system.
|
|---|
| 100 | *
|
|---|
| 101 | * Additional information
|
|---|
| 102 | * The stack pointer is expected to be initialized by hardware,
|
|---|
| 103 | * i.e. read from vectortable[0].
|
|---|
| 104 | * For manual initialization add
|
|---|
| 105 | * ldr R0, =__stack_end__
|
|---|
| 106 | * mov SP, R0
|
|---|
| 107 | */
|
|---|
| 108 | .global reset_handler
|
|---|
| 109 | .global Reset_Handler
|
|---|
| 110 | .equ reset_handler, Reset_Handler
|
|---|
| 111 | .section .init.Reset_Handler, "ax"
|
|---|
| 112 | .balign 2
|
|---|
| 113 | .thumb_func
|
|---|
| 114 | Reset_Handler:
|
|---|
| 115 | #ifdef __SEGGER_STOP
|
|---|
| 116 | .extern __SEGGER_STOP_Limit_MSP
|
|---|
| 117 | //
|
|---|
| 118 | // Initialize main stack limit to 0 to disable stack checks before runtime init
|
|---|
| 119 | //
|
|---|
| 120 | movs R0, #0
|
|---|
| 121 | ldr R1, =__SEGGER_STOP_Limit_MSP
|
|---|
| 122 | str R0, [R1]
|
|---|
| 123 | #endif
|
|---|
| 124 | #ifndef __NO_SYSTEM_INIT
|
|---|
| 125 | //
|
|---|
| 126 | // Call SystemInit
|
|---|
| 127 | //
|
|---|
| 128 | bl SystemInit
|
|---|
| 129 | #endif
|
|---|
| 130 | #ifdef __MEMORY_INIT
|
|---|
| 131 | //
|
|---|
| 132 | // Call MemoryInit
|
|---|
| 133 | //
|
|---|
| 134 | //bl MemoryInit
|
|---|
| 135 | ldr r1, =0x20000000 // start address of SRAM
|
|---|
| 136 | ldr r2, =0x20008000 // end address of SRAM
|
|---|
| 137 | movs r3, #0 // we want to write 0 to all address
|
|---|
| 138 | SRAM1:
|
|---|
| 139 | str r3, [r1] // store 0 at address
|
|---|
| 140 | adds r1, r1, #4 // increment to next address
|
|---|
| 141 | cmp r1, r2 // update flags
|
|---|
| 142 | bne SRAM1 // branch if write address equals end of SRAM
|
|---|
| 143 |
|
|---|
| 144 | ldr r1, =0x20018000 // start address of CCM-SRAM
|
|---|
| 145 | ldr r2, =0x20020000 // end address of CCM-SRAM
|
|---|
| 146 | movs r3, #0 // we want to write 0 to all address
|
|---|
| 147 | CCM_SRAM:
|
|---|
| 148 | str r3, [r1] // store 0 at address
|
|---|
| 149 | adds r1, r1, #4 // increment to next address
|
|---|
| 150 | cmp r1, r2 // update flags
|
|---|
| 151 | bne CCM_SRAM // branch if write address equals end of CCM-SRAM
|
|---|
| 152 |
|
|---|
| 153 | #endif
|
|---|
| 154 | #ifdef __VECTORS_IN_RAM
|
|---|
| 155 | //
|
|---|
| 156 | // Copy vector table (from Flash) to RAM
|
|---|
| 157 | //
|
|---|
| 158 | ldr R0, =__vectors_start__
|
|---|
| 159 | ldr R1, =__vectors_end__
|
|---|
| 160 | ldr R2, =__vectors_ram_start__
|
|---|
| 161 | 1:
|
|---|
| 162 | cmp R0, R1
|
|---|
| 163 | beq 2f
|
|---|
| 164 | ldr R3, [R0]
|
|---|
| 165 | str R3, [R2]
|
|---|
| 166 | adds R0, R0, #4
|
|---|
| 167 | adds R2, R2, #4
|
|---|
| 168 | b 1b
|
|---|
| 169 | 2:
|
|---|
| 170 | #endif
|
|---|
| 171 |
|
|---|
| 172 | #if defined(__VTOR_CONFIG) || defined(__VECTORS_IN_RAM)
|
|---|
| 173 | //
|
|---|
| 174 | // Configure vector table offset register
|
|---|
| 175 | //
|
|---|
| 176 | #ifdef __ARM_ARCH_6M__
|
|---|
| 177 | ldr R0, =0xE000ED08 // VTOR_REG
|
|---|
| 178 | #else
|
|---|
| 179 | movw R0, 0xED08 // VTOR_REG
|
|---|
| 180 | movt R0, 0xE000
|
|---|
| 181 | #endif
|
|---|
| 182 | #ifdef __VECTORS_IN_RAM
|
|---|
| 183 | ldr R1, =_vectors_ram
|
|---|
| 184 | #else
|
|---|
| 185 | ldr R1, =_vectors
|
|---|
| 186 | #endif
|
|---|
| 187 | str R1, [R0]
|
|---|
| 188 | #endif
|
|---|
| 189 | #if !defined(__SOFTFP__) && !defined(__NO_FPU_ENABLE)
|
|---|
| 190 | //
|
|---|
| 191 | // Enable CP11 and CP10 with CPACR |= (0xf<<20)
|
|---|
| 192 | //
|
|---|
| 193 | movw R0, 0xED88 // CPACR
|
|---|
| 194 | movt R0, 0xE000
|
|---|
| 195 | ldr R1, [R0]
|
|---|
| 196 | orrs R1, R1, #(0xf << 20)
|
|---|
| 197 | str R1, [R0]
|
|---|
| 198 | #endif
|
|---|
| 199 | //
|
|---|
| 200 | // Call runtime initialization, which calls main().
|
|---|
| 201 | //
|
|---|
| 202 | bl _start
|
|---|
| 203 |
|
|---|
| 204 | //
|
|---|
| 205 | // Weak only declaration of SystemInit enables Linker to replace bl SystemInit with a NOP,
|
|---|
| 206 | // when there is no strong definition of SystemInit.
|
|---|
| 207 | //
|
|---|
| 208 | .weak SystemInit
|
|---|
| 209 | //
|
|---|
| 210 | // Place SystemCoreClockUpdate in .init_array
|
|---|
| 211 | // to be called after runtime initialization
|
|---|
| 212 | //
|
|---|
| 213 | #if !defined(__NO_SYSTEM_INIT) && !defined(__NO_SYSTEM_CLK_UPDATE)
|
|---|
| 214 | .section .init_array, "aw"
|
|---|
| 215 | .balign 4
|
|---|
| 216 | .word SystemCoreClockUpdate
|
|---|
| 217 | #endif
|
|---|
| 218 |
|
|---|
| 219 | /*********************************************************************
|
|---|
| 220 | *
|
|---|
| 221 | * HardFault_Handler
|
|---|
| 222 | *
|
|---|
| 223 | * Function description
|
|---|
| 224 | * Simple exception handler for HardFault.
|
|---|
| 225 | * In case of a HardFault caused by BKPT instruction without
|
|---|
| 226 | * debugger attached, return execution, otherwise stay in loop.
|
|---|
| 227 | *
|
|---|
| 228 | * Additional information
|
|---|
| 229 | * The stack pointer is expected to be initialized by hardware,
|
|---|
| 230 | * i.e. read from vectortable[0].
|
|---|
| 231 | * For manual initialization add
|
|---|
| 232 | * ldr R0, =__stack_end__
|
|---|
| 233 | * mov SP, R0
|
|---|
| 234 | */
|
|---|
| 235 |
|
|---|
| 236 | #undef L
|
|---|
| 237 | #define L(label) .LHardFault_Handler_##label
|
|---|
| 238 |
|
|---|
| 239 | .weak HardFault_Handler
|
|---|
| 240 | .section .init.HardFault_Handler, "ax"
|
|---|
| 241 | .balign 2
|
|---|
| 242 | .thumb_func
|
|---|
| 243 | HardFault_Handler:
|
|---|
| 244 | //
|
|---|
| 245 | // Check if HardFault is caused by BKPT instruction
|
|---|
| 246 | //
|
|---|
| 247 | ldr R1, =0xE000ED2C // Load NVIC_HFSR
|
|---|
| 248 | ldr R2, [R1]
|
|---|
| 249 | cmp R2, #0 // Check NVIC_HFSR[31]
|
|---|
| 250 |
|
|---|
| 251 | L(hfLoop):
|
|---|
| 252 | bmi L(hfLoop) // Not set? Stay in HardFault Handler.
|
|---|
| 253 | //
|
|---|
| 254 | // Continue execution after BKPT instruction
|
|---|
| 255 | //
|
|---|
| 256 | #if defined(__thumb__) && !defined(__thumb2__)
|
|---|
| 257 | movs R0, #4
|
|---|
| 258 | mov R1, LR
|
|---|
| 259 | tst R0, R1 // Check EXC_RETURN in Link register bit 2.
|
|---|
| 260 | bne L(Uses_PSP)
|
|---|
| 261 | mrs R0, MSP // Stacking was using MSP.
|
|---|
| 262 | b L(Pass_StackPtr)
|
|---|
| 263 | L(Uses_PSP):
|
|---|
| 264 | mrs R0, PSP // Stacking was using PSP.
|
|---|
| 265 | L(Pass_StackPtr):
|
|---|
| 266 | #else
|
|---|
| 267 | tst LR, #4 // Check EXC_RETURN[2] in link register to get the return stack
|
|---|
| 268 | ite eq
|
|---|
| 269 | mrseq R0, MSP // Frame stored on MSP
|
|---|
| 270 | mrsne R0, PSP // Frame stored on PSP
|
|---|
| 271 | #endif
|
|---|
| 272 | //
|
|---|
| 273 | // Reset HardFault Status
|
|---|
| 274 | //
|
|---|
| 275 | #if defined(__thumb__) && !defined(__thumb2__)
|
|---|
| 276 | movs R3, #1
|
|---|
| 277 | lsls R3, R3, #31
|
|---|
| 278 | orrs R2, R3
|
|---|
| 279 | str R2, [R1]
|
|---|
| 280 | #else
|
|---|
| 281 | orr R2, R2, #0x80000000
|
|---|
| 282 | str R2, [R1]
|
|---|
| 283 | #endif
|
|---|
| 284 | //
|
|---|
| 285 | // Adjust return address
|
|---|
| 286 | //
|
|---|
| 287 | ldr R1, [R0, #24] // Get stored PC from stack
|
|---|
| 288 | adds R1, #2 // Adjust PC by 2 to skip current BKPT
|
|---|
| 289 | str R1, [R0, #24] // Write back adjusted PC to stack
|
|---|
| 290 | //
|
|---|
| 291 | bx LR // Return
|
|---|
| 292 |
|
|---|
| 293 | /*************************** End of file ****************************/
|
|---|