dRonin  adbada4
dRonin firmware
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
startup.c
Go to the documentation of this file.
1 
13 /*
14  * This program is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 3 of the License, or
17  * (at your option) any later version.
18  *
19  * This program is distributed in the hope that it will be useful, but
20  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21  * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22  * for more details.
23  *
24  * You should have received a copy of the GNU General Public License along
25  * with this program; if not, see <http://www.gnu.org/licenses/>
26  */
27 
28 
29 #include <string.h>
30 #include <stm32f30x.h>
31 
32 /* prototype for main() that tells us not to worry about it possibly returning */
33 extern int main(void) __attribute__((noreturn));
34 
35 /* prototype our _main() to avoid prolog/epilog insertion and other related junk */
36 void _main(void) __attribute__((noreturn, naked, no_instrument_function));
37 
39 static void default_cpu_handler(void) __attribute__((noreturn, naked, no_instrument_function));
40 
42 extern char _sbss, _ebss;
43 
45 extern char _sidata, _sdata, _edata, _sfast, _efast;
46 
48 char irq_stack[1024] __attribute__((section(".irqstack")));
49 
51 typedef const void (vector)(void);
52 
54 struct cm3_vectors {
55  void *initial_stack;
56  vector *entry;
57  vector *vectors[14];
58 };
59 
63 void
64 _main(void)
65 {
66  // load the stack base for the current stack before we attempt to branch to any function
67  // that might bounds-check the stack
68  asm volatile ("mov r10, %0" : : "r" (&irq_stack[0]) : );
69 
70  /* Disable all interrupts, until proper table etc is installed. */
71  __disable_irq();
72 
73  /* enable usage, bus and memory faults */
74  SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_BUSFAULTENA_Msk | SCB_SHCSR_MEMFAULTENA_Msk;
75 
76  /* configure FP state save behaviour - automatic, lazy save */
77  FPU->FPCCR |= FPU_FPCCR_ASPEN_Msk | FPU_FPCCR_LSPEN_Msk;
78 
79  /* configure default FPU state */
80  FPU->FPDSCR |= FPU_FPDSCR_DN_Msk; /* enable Default NaN */
81  FPU->FPDSCR |= FPU_FPDSCR_FZ_Msk; /* Use flush to zero for very
82  * small values. */
83 
84  /* enable the FPU */
85  SCB->CPACR |= (0xf << 20); // turn on CP10/11 for FP support on cores that implement it
86 
87  /* copy initialised data from flash to RAM */
88  memcpy(&_sdata, &_sidata, &_edata - &_sdata);
89 
90  /* zero the BSS */
91  memset(&_sbss, 0, &_ebss - &_sbss);
92 
93  /* zero any 'fast' RAM that's been used */
94  memset(&_sfast, 0, &_efast - &_sfast);
95 
96  /* fill most of the IRQ/bootstrap stack with a watermark pattern so we can measure how much is used */
97  /* leave a little space at the top in case memset() isn't a leaf with no locals */
98  memset(&irq_stack, 0xa5, sizeof(irq_stack) - 64);
99 
100  /* call main */
101  (void)main();
102 }
103 
107 static void
109 {
110  for (;;) ;
111 }
112 
114 #define HANDLER(_name) extern vector _name __attribute__((weak, alias("default_cpu_handler")))
115 
116 /* standard CMSIS vector names */
117 HANDLER(NMI_Handler);
118 HANDLER(HardFault_Handler);
119 HANDLER(MemManage_Handler);
120 HANDLER(BusFault_Handler);
121 HANDLER(UsageFault_Handler);
122 HANDLER(DebugMon_Handler);
123 
125 struct cm3_vectors cpu_vectors __attribute((section(".cpu_vectors"))) = {
126  .initial_stack = &irq_stack[sizeof(irq_stack)],
127  .entry = (vector *)_main,
128  .vectors = {
129  NMI_Handler,
130  HardFault_Handler,
131  MemManage_Handler,
132  BusFault_Handler,
133  UsageFault_Handler,
134  0,
135  0,
136  0,
137  0,
138  0,
139  DebugMon_Handler,
140  0,
141  0,
142  0,
143  }
144 };
145 
void _main(void)
Definition: startup.c:64
char _ebss
const void( vector)(void)
Definition: startup.c:32
char _sidata
char _sbss
#define HANDLER(_name)
Definition: startup.c:114
char _sdata
const void *_vectors[] __attribute((section(".vectors")))
int main(void)
Definition: chibi_main.c:69
char _edata
static void default_cpu_handler(void)
Definition: startup.c:108
static void entry()
Definition: loadabletest.c:40