source: trunk/firmware/FSBL/SES/STM32N6xx/Source/STM32N6xx_Startup.s@ 27

Last change on this file since 27 was 24, checked in by Zed, 4 weeks ago

First Stage Bootloader now can be debugged and flashed directly in SES.
RTT is also working.

File size: 10.1 KB
Line 
1/*********************************************************************
2* SEGGER Microcontroller GmbH *
3* The Embedded Experts *
4**********************************************************************
5* *
6* (c) 2014 - 2026 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
39File : STM32N6xx_Startup.s
40Purpose : Startup and exception handlers for STM32N6xx devices.
41
42Additional 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#ifndef __STACK_LIMIT_MSP
88 #define __STACK_LIMIT_MSP __stack_start__
89#endif
90#ifndef __STACK_LIMIT_PSP
91 #define __STACK_LIMIT_PSP 0x0
92#endif
93
94/*********************************************************************
95*
96* Global functions
97*
98**********************************************************************
99*/
100/*********************************************************************
101*
102* Reset_Handler
103*
104* Function description
105* Exception handler for reset.
106* Generic bringup of a Cortex-M system.
107*
108* Additional information
109* The stack pointer is expected to be initialized by hardware,
110* i.e. read from vectortable[0].
111* For manual initialization add
112* ldr R0, =__stack_end__
113* mov SP, R0
114*/
115 .global reset_handler
116 .global Reset_Handler
117 .equ reset_handler, Reset_Handler
118 .section .init.Reset_Handler, "ax"
119 .balign 2
120 .thumb_func
121Reset_Handler:
122#ifdef __SET_STACK_LIMITS
123 ldr r1, =__STACK_LIMIT_MSP
124 msr msplim, r1
125 ldr r1, =__STACK_LIMIT_PSP
126 msr psplim, r1
127#endif
128#ifdef __SEGGER_STOP
129 .extern __SEGGER_STOP_Limit_MSP
130 //
131 // Initialize main stack limit to 0 to disable stack checks before runtime init
132 //
133 movs R0, #0
134 ldr R1, =__SEGGER_STOP_Limit_MSP
135 str R0, [R1]
136#endif
137#ifndef __NO_SYSTEM_INIT
138 //
139 // Call SystemInit
140 //
141 bl SystemInit
142#endif
143#ifdef __MEMORY_INIT
144 //
145 // Call MemoryInit
146 //
147 bl MemoryInit
148#endif
149#ifdef __VECTORS_IN_RAM
150 //
151 // Copy vector table (from Flash) to RAM
152 //
153 ldr R0, =__vectors_start__
154 ldr R1, =__vectors_end__
155 ldr R2, =__vectors_ram_start__
1561:
157 cmp R0, R1
158 beq 2f
159 ldr R3, [R0]
160 str R3, [R2]
161 adds R0, R0, #4
162 adds R2, R2, #4
163 b 1b
1642:
165#endif
166
167#if defined(__VTOR_CONFIG) || defined(__VECTORS_IN_RAM)
168 //
169 // Configure vector table offset register
170 //
171#ifdef __ARM_ARCH_6M__
172 ldr R0, =0xE000ED08 // VTOR_REG
173#else
174 movw R0, 0xED08 // VTOR_REG
175 movt R0, 0xE000
176#endif
177#ifdef __VECTORS_IN_RAM
178 ldr R1, =_vectors_ram
179#else
180 ldr R1, =_vectors
181#endif
182 str R1, [R0]
183#endif
184#if !defined(__SOFTFP__) && !defined(__NO_FPU_ENABLE)
185 //
186 // Enable CP11 and CP10 with CPACR |= (0xf<<20)
187 //
188 movw R0, 0xED88 // CPACR
189 movt R0, 0xE000
190 ldr R1, [R0]
191 orrs R1, R1, #(0xf << 20)
192 str R1, [R0]
193#endif
194
195 // Open the debug access port to the Cortex-M55 by writing 0xB4 to BSEC_AP_UNLOCK
196 ldr r0, = 0x56009E90
197 mov r1, #0xB4
198 str r1, [r0]
199
200 // Enable the non-secure/secure debug by writing 0xB451B400 to BSEC_DBGCR
201 ldr r0, = 0x56009E8C
202 ldr r1, = 0xB451B400
203 str r1, [r0]
204
205 //
206 // Call runtime initialization, which calls main().
207 //
208 bl _start
209
210 //
211 // Weak only declaration of SystemInit enables Linker to replace bl SystemInit with a NOP,
212 // when there is no strong definition of SystemInit.
213 //
214 .weak SystemInit
215 //
216 // Place SystemCoreClockUpdate in .init_array
217 // to be called after runtime initialization
218 //
219#if !defined(__NO_SYSTEM_INIT) && !defined(__NO_SYSTEM_CLK_UPDATE)
220 .section .init_array, "aw"
221 .balign 4
222 .word SystemCoreClockUpdate
223#endif
224
225/*********************************************************************
226*
227* HardFault_Handler
228*
229* Function description
230* Simple exception handler for HardFault.
231* In case of a HardFault caused by BKPT instruction without
232* debugger attached, return execution, otherwise stay in loop.
233*
234* Additional information
235* The stack pointer is expected to be initialized by hardware,
236* i.e. read from vectortable[0].
237* For manual initialization add
238* ldr R0, =__stack_end__
239* mov SP, R0
240*/
241
242#undef L
243#define L(label) .LHardFault_Handler_##label
244
245 .weak HardFault_Handler
246 .section .init.HardFault_Handler, "ax"
247 .balign 2
248 .thumb_func
249HardFault_Handler:
250 //
251 // Check if HardFault is caused by BKPT instruction
252 //
253 ldr R1, =0xE000ED2C // Load NVIC_HFSR
254 ldr R2, [R1]
255 cmp R2, #0 // Check NVIC_HFSR[31]
256
257L(hfLoop):
258 bmi L(hfLoop) // Not set? Stay in HardFault Handler.
259 //
260 // Continue execution after BKPT instruction
261 //
262#if defined(__thumb__) && !defined(__thumb2__)
263 movs R0, #4
264 mov R1, LR
265 tst R0, R1 // Check EXC_RETURN in Link register bit 2.
266 bne L(Uses_PSP)
267 mrs R0, MSP // Stacking was using MSP.
268 b L(Pass_StackPtr)
269L(Uses_PSP):
270 mrs R0, PSP // Stacking was using PSP.
271L(Pass_StackPtr):
272#else
273 tst LR, #4 // Check EXC_RETURN[2] in link register to get the return stack
274 ite eq
275 mrseq R0, MSP // Frame stored on MSP
276 mrsne R0, PSP // Frame stored on PSP
277#endif
278 //
279 // Reset HardFault Status
280 //
281#if defined(__thumb__) && !defined(__thumb2__)
282 movs R3, #1
283 lsls R3, R3, #31
284 orrs R2, R3
285 str R2, [R1]
286#else
287 orr R2, R2, #0x80000000
288 str R2, [R1]
289#endif
290 //
291 // Adjust return address
292 //
293 ldr R1, [R0, #24] // Get stored PC from stack
294 adds R1, #2 // Adjust PC by 2 to skip current BKPT
295 str R1, [R0, #24] // Write back adjusted PC to stack
296 //
297 bx LR // Return
298
299/*************************** End of file ****************************/
Note: See TracBrowser for help on using the repository browser.