]> begriffs open source - cmsis/blob - CMSIS/Core/Template/Device_A/Source/startup_Device.c
Possible bugs in MMU_MemorySection(), MMU_MemoryPage() (#219)
[cmsis] / CMSIS / Core / Template / Device_A / Source / startup_Device.c
1 /******************************************************************************
2  * @file     startup_<Device>.c
3  * @brief    CMSIS Cortex-A Device Startup
4  * @version  V1.00
5  * @date     10. January 2018
6  ******************************************************************************/
7 /*
8  * Copyright (c) 2009-2018 Arm Limited. All rights reserved.
9  *
10  * SPDX-License-Identifier: Apache-2.0
11  *
12  * Licensed under the Apache License, Version 2.0 (the License); you may
13  * not use this file except in compliance with the License.
14  * You may obtain a copy of the License at
15  *
16  * www.apache.org/licenses/LICENSE-2.0
17  *
18  * Unless required by applicable law or agreed to in writing, software
19  * distributed under the License is distributed on an AS IS BASIS, WITHOUT
20  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
21  * See the License for the specific language governing permissions and
22  * limitations under the License.
23  */
24
25 #include "<Device>.h" /* ToDo: replace '<Device>' with your device name */
26
27 /*----------------------------------------------------------------------------
28   Definitions
29  *----------------------------------------------------------------------------*/
30 #define USR_MODE 0x10            // User mode
31 #define FIQ_MODE 0x11            // Fast Interrupt Request mode
32 #define IRQ_MODE 0x12            // Interrupt Request mode
33 #define SVC_MODE 0x13            // Supervisor mode
34 #define ABT_MODE 0x17            // Abort mode
35 #define UND_MODE 0x1B            // Undefined Instruction mode
36 #define SYS_MODE 0x1F            // System mode
37
38 /*----------------------------------------------------------------------------
39   Internal References
40  *----------------------------------------------------------------------------*/
41 void Vectors       (void) __attribute__ ((section("RESET")));
42 void Reset_Handler (void);
43
44 /*----------------------------------------------------------------------------
45   Exception / Interrupt Handler
46  *----------------------------------------------------------------------------*/
47 void Undef_Handler (void) __attribute__ ((weak, alias("Default_Handler")));
48 void SVC_Handler   (void) __attribute__ ((weak, alias("Default_Handler")));
49 void PAbt_Handler  (void) __attribute__ ((weak, alias("Default_Handler")));
50 void DAbt_Handler  (void) __attribute__ ((weak, alias("Default_Handler")));
51 void IRQ_Handler   (void) __attribute__ ((weak, alias("Default_Handler")));
52 void FIQ_Handler   (void) __attribute__ ((weak, alias("Default_Handler")));
53
54 /*----------------------------------------------------------------------------
55   Exception / Interrupt Vector Table
56  *----------------------------------------------------------------------------*/
57 __ASM void Vectors(void) {
58   IMPORT Undef_Handler
59   IMPORT SVC_Handler
60   IMPORT PAbt_Handler
61   IMPORT DAbt_Handler
62   IMPORT IRQ_Handler
63   IMPORT FIQ_Handler
64   LDR    PC, =Reset_Handler
65   LDR    PC, =Undef_Handler
66   LDR    PC, =SVC_Handler
67   LDR    PC, =PAbt_Handler
68   LDR    PC, =DAbt_Handler
69   NOP
70   LDR    PC, =IRQ_Handler
71   LDR    PC, =FIQ_Handler
72 }
73
74 /*----------------------------------------------------------------------------
75   Reset Handler called on controller reset
76  *----------------------------------------------------------------------------*/
77 __ASM void Reset_Handler(void) {
78
79   // Mask interrupts
80   CPSID   if                           
81
82   // Put any cores other than 0 to sleep
83   MRC     p15, 0, R0, c0, c0, 5       // Read MPIDR
84   ANDS    R0, R0, #3
85 goToSleep
86   WFINE
87   BNE     goToSleep
88
89   // Reset SCTLR Settings
90   MRC     p15, 0, R0, c1, c0, 0       // Read CP15 System Control register
91   BIC     R0, R0, #(0x1 << 12)        // Clear I bit 12 to disable I Cache
92   BIC     R0, R0, #(0x1 <<  2)        // Clear C bit  2 to disable D Cache
93   BIC     R0, R0, #0x1                // Clear M bit  0 to disable MMU
94   BIC     R0, R0, #(0x1 << 11)        // Clear Z bit 11 to disable branch prediction
95   BIC     R0, R0, #(0x1 << 13)        // Clear V bit 13 to disable hivecs
96   MCR     p15, 0, R0, c1, c0, 0       // Write value back to CP15 System Control register
97   ISB
98
99   // Configure ACTLR
100   MRC     p15, 0, r0, c1, c0, 1       // Read CP15 Auxiliary Control Register
101   ORR     r0, r0, #(1 <<  1)          // Enable L2 prefetch hint (UNK/WI since r4p1)
102   MCR     p15, 0, r0, c1, c0, 1       // Write CP15 Auxiliary Control Register
103
104   // Set Vector Base Address Register (VBAR) to point to this application's vector table
105   LDR    R0, =Vectors
106   MCR    p15, 0, R0, c12, c0, 0
107
108   // Setup Stack for each exceptional mode
109   IMPORT |Image$$FIQ_STACK$$ZI$$Limit|
110   IMPORT |Image$$IRQ_STACK$$ZI$$Limit|
111   IMPORT |Image$$SVC_STACK$$ZI$$Limit|
112   IMPORT |Image$$ABT_STACK$$ZI$$Limit|
113   IMPORT |Image$$UND_STACK$$ZI$$Limit|
114   IMPORT |Image$$ARM_LIB_STACK$$ZI$$Limit|
115   CPS    #0x11
116   LDR    SP, =|Image$$FIQ_STACK$$ZI$$Limit|
117   CPS    #0x12
118   LDR    SP, =|Image$$IRQ_STACK$$ZI$$Limit|
119   CPS    #0x13
120   LDR    SP, =|Image$$SVC_STACK$$ZI$$Limit|
121   CPS    #0x17
122   LDR    SP, =|Image$$ABT_STACK$$ZI$$Limit|
123   CPS    #0x1B
124   LDR    SP, =|Image$$UND_STACK$$ZI$$Limit|
125   CPS    #0x1F
126   LDR    SP, =|Image$$ARM_LIB_STACK$$ZI$$Limit|
127
128   // Call SystemInit
129   IMPORT SystemInit
130   BL     SystemInit
131
132   // Unmask interrupts
133   CPSIE  if
134
135   // Call __main
136   IMPORT __main
137   BL     __main
138 }
139
140 /*----------------------------------------------------------------------------
141   Default Handler for Exceptions / Interrupts
142  *----------------------------------------------------------------------------*/
143 void Default_Handler(void) {
144   while(1);
145 }