update credits
[librepilot.git] / flight / pios / stm32f30x / startup.c
blobe4299b60f8023be0266c5d94cf84d90f67623172
1 /**
2 ******************************************************************************
3 * @addtogroup PIOS PIOS Core hardware abstraction layer
4 * @{
6 * @file startup.c
7 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2012.
8 * @author Tau Labs, http://taulabs.org, Copyright (C) 2012-2013
9 * @brief C based startup of F3
10 * @see The GNU Public License (GPL) Version 3
12 *****************************************************************************/
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.
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.
24 * You should have received a copy of the GNU General Public License along
25 * with this program; if not, write to the Free Software Foundation, Inc.,
26 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30 #include <string.h>
31 #include <stm32f30x.h>
33 /* prototype for main() that tells us not to worry about it possibly returning */
34 extern int main(void) __attribute__((noreturn));
36 /* prototype our _main() to avoid prolog/epilog insertion and other related junk */
37 void _main(void) __attribute__((noreturn, naked, no_instrument_function));
39 /** default handler for CPU exceptions */
40 static void default_cpu_handler(void) __attribute__((noreturn, naked, no_instrument_function));
42 /** BSS symbols XXX should have a header that defines all of these */
43 extern char _sbss, _ebss;
45 /** DATA symbols XXX should have a header that defines all of these */
46 extern char _sidata, _sdata, _edata, _sfast, _efast;
48 /** The bootstrap/IRQ stack XXX should define size somewhere else */
49 char irq_stack[1024] __attribute__((section(".irqstack")));
51 /** exception handler */
52 typedef void (vector)(void);
54 /** CortexM3 CPU vectors */
55 struct cm3_vectors {
56 void *initial_stack;
57 vector *entry;
58 vector *vectors[14];
61 /**
62 * Initial startup code.
64 void _main(void)
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]) :);
70 /* enable usage, bus and memory faults */
71 SCB->SHCSR |= SCB_SHCSR_USGFAULTENA_Msk | SCB_SHCSR_BUSFAULTENA_Msk | SCB_SHCSR_MEMFAULTENA_Msk;
73 /* configure FP state save behaviour - automatic, lazy save */
74 FPU->FPCCR |= FPU_FPCCR_ASPEN_Msk | FPU_FPCCR_LSPEN_Msk;
76 /* configure default FPU state */
77 FPU->FPDSCR |= FPU_FPDSCR_DN_Msk; /* enable Default NaN */
78 FPU->FPDSCR |= FPU_FPDSCR_FZ_Msk; /* Use flush to zero for very
79 * small values. */
81 /* enable the FPU */
82 SCB->CPACR |= (0xf << 20); // turn on CP10/11 for FP support on cores that implement it
84 /* copy initialised data from flash to RAM */
85 memcpy(&_sdata, &_sidata, &_edata - &_sdata);
87 /* zero the BSS */
88 memset(&_sbss, 0, &_ebss - &_sbss);
90 /* zero any 'fast' RAM that's been used */
91 memset(&_sfast, 0, &_efast - &_sfast);
93 /* fill most of the IRQ/bootstrap stack with a watermark pattern so we can measure how much is used */
94 /* leave a little space at the top in case memset() isn't a leaf with no locals */
95 memset(&irq_stack, 0xa5, sizeof(irq_stack) - 64);
97 /* call main */
98 (void)main();
102 * Default handler for CPU exceptions.
104 static void default_cpu_handler(void)
106 for (;;) {
111 static void default_NMI_Handler()
113 default_cpu_handler();
115 static void default_HardFault_Handler()
117 default_cpu_handler();
119 static void default_MemManage_Handler()
121 default_cpu_handler();
123 static void default_BusFault_Handler()
125 default_cpu_handler();
127 static void default_UsageFault_Handler()
129 default_cpu_handler();
131 static void default_DebugMon_Handler()
133 default_cpu_handler();
135 static void default_vPortSVCHandler()
137 default_cpu_handler();
139 static void default_xPortPendSVHandler()
141 default_cpu_handler();
143 static void default_xPortSysTickHandler()
145 default_cpu_handler();
149 /** Prototype for optional exception vector handlers */
150 #define HANDLER(_name) extern vector _name __attribute__((weak, alias("default_" #_name)))
152 /* standard CMSIS vector names */
153 HANDLER(NMI_Handler);
154 HANDLER(HardFault_Handler);
155 HANDLER(MemManage_Handler);
156 HANDLER(BusFault_Handler);
157 HANDLER(UsageFault_Handler);
158 HANDLER(DebugMon_Handler);
159 /* these vectors point directly to the relevant FreeRTOS functions if they are defined */
160 HANDLER(vPortSVCHandler);
161 HANDLER(xPortPendSVHandler);
162 HANDLER(xPortSysTickHandler);
164 /** CortexM3 vector table */
165 struct cm3_vectors cpu_vectors __attribute((section(".cpu_vectors"))) =
167 .initial_stack = &irq_stack[sizeof(irq_stack)],
168 .entry = (vector *)_main,
169 .vectors = {
170 NMI_Handler,
171 HardFault_Handler,
172 MemManage_Handler,
173 BusFault_Handler,
174 UsageFault_Handler,
179 vPortSVCHandler,
180 DebugMon_Handler, // Maybe
182 xPortPendSVHandler,
183 xPortSysTickHandler,
188 * @}