1 /**
2  * \file
3  *
4  * \brief gcc starttup file for SAMD20
5  *
6  * Copyright (c) 2015 Atmel Corporation. All rights reserved.
7  *
8  * \asf_license_start
9  *
10  * \page License
11  *
12  * Redistribution and use in source and binary forms, with or without
13  * modification, are permitted provided that the following conditions are met:
14  *
15  * 1. Redistributions of source code must retain the above copyright notice,
16  *    this list of conditions and the following disclaimer.
17  *
18  * 2. Redistributions in binary form must reproduce the above copyright notice,
19  *    this list of conditions and the following disclaimer in the documentation
20  *    and/or other materials provided with the distribution.
21  *
22  * 3. The name of Atmel may not be used to endorse or promote products derived
23  *    from this software without specific prior written permission.
24  *
25  * 4. This software may only be redistributed and used in connection with an
26  *    Atmel microcontroller product.
27  *
28  * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
29  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
31  * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
32  * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
33  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
34  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
35  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
36  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
37  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
38  * POSSIBILITY OF SUCH DAMAGE.
39  *
40  * \asf_license_stop
41  *
42  */
43 /*
44  * Support and FAQ: visit <a href="http://www.atmel.com/design-support/">Atmel Support</a>
45  */
46 
47 #include "samd20.h"
48 
49 /* Initialize segments */
50 extern uint32_t _sfixed;
51 extern uint32_t _efixed;
52 extern uint32_t _etext;
53 extern uint32_t _srelocate;
54 extern uint32_t _erelocate;
55 extern uint32_t _szero;
56 extern uint32_t _ezero;
57 extern uint32_t _sstack;
58 extern uint32_t _estack;
59 
60 /** \cond DOXYGEN_SHOULD_SKIP_THIS */
61 int main(void);
62 /** \endcond */
63 
64 void __libc_init_array(void);
65 
66 /* Default empty handler */
67 void Dummy_Handler(void);
68 
69 /* Cortex-M0+ core handlers */
70 void NMI_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
71 void HardFault_Handler       ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
72 void SVC_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
73 void PendSV_Handler          ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
74 void SysTick_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
75 
76 /* Peripherals handlers */
77 void PM_Handler              ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
78 void SYSCTRL_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
79 void WDT_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
80 void RTC_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
81 void EIC_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
82 void NVMCTRL_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
83 void EVSYS_Handler           ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
84 void SERCOM0_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
85 void SERCOM1_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
86 void SERCOM2_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
87 void SERCOM3_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
88 #ifdef ID_SERCOM4
89 void SERCOM4_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
90 #endif
91 #ifdef ID_SERCOM5
92 void SERCOM5_Handler         ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
93 #endif
94 void TC0_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
95 void TC1_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
96 void TC2_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
97 void TC3_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
98 void TC4_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
99 void TC5_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
100 #ifdef ID_TC6
101 void TC6_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
102 #endif
103 #ifdef ID_TC7
104 void TC7_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
105 #endif
106 #ifdef ID_ADC
107 void ADC_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
108 #endif
109 #ifdef ID_AC
110 void AC_Handler              ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
111 #endif
112 #ifdef ID_DAC
113 void DAC_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
114 #endif
115 #ifdef ID_PTC
116 void PTC_Handler             ( void ) __attribute__ ((weak, alias("Dummy_Handler")));
117 #endif
118 
119 /* Exception Table */
120 __attribute__ ((section(".vectors")))
121 const DeviceVectors exception_table = {
122 
123         /* Configure Initial Stack Pointer, using linker-generated symbols */
124         (void*) (&_estack),
125 
126         (void*) Reset_Handler,
127         (void*) NMI_Handler,
128         (void*) HardFault_Handler,
129         (void*) (0UL), /* Reserved */
130         (void*) (0UL), /* Reserved */
131         (void*) (0UL), /* Reserved */
132         (void*) (0UL), /* Reserved */
133         (void*) (0UL), /* Reserved */
134         (void*) (0UL), /* Reserved */
135         (void*) (0UL), /* Reserved */
136         (void*) SVC_Handler,
137         (void*) (0UL), /* Reserved */
138         (void*) (0UL), /* Reserved */
139         (void*) PendSV_Handler,
140         (void*) SysTick_Handler,
141 
142         /* Configurable interrupts */
143         (void*) PM_Handler,             /*  0 Power Manager */
144         (void*) SYSCTRL_Handler,        /*  1 System Control */
145         (void*) WDT_Handler,            /*  2 Watchdog Timer */
146         (void*) RTC_Handler,            /*  3 Real-Time Counter */
147         (void*) EIC_Handler,            /*  4 External Interrupt Controller */
148         (void*) NVMCTRL_Handler,        /*  5 Non-Volatile Memory Controller */
149         (void*) EVSYS_Handler,          /*  6 Event System Interface */
150         (void*) SERCOM0_Handler,        /*  7 Serial Communication Interface 0 */
151         (void*) SERCOM1_Handler,        /*  8 Serial Communication Interface 1 */
152         (void*) SERCOM2_Handler,        /*  9 Serial Communication Interface 2 */
153         (void*) SERCOM3_Handler,        /* 10 Serial Communication Interface 3 */
154 #ifdef ID_SERCOM4
155         (void*) SERCOM4_Handler,        /* 11 Serial Communication Interface 4 */
156 #else
157         (void*) (0UL), /* Reserved */
158 #endif
159 #ifdef ID_SERCOM5
160         (void*) SERCOM5_Handler,        /* 12 Serial Communication Interface 5 */
161 #else
162         (void*) (0UL), /* Reserved */
163 #endif
164         (void*) TC0_Handler,            /* 13 Basic Timer Counter 0 */
165         (void*) TC1_Handler,            /* 14 Basic Timer Counter 1 */
166         (void*) TC2_Handler,            /* 15 Basic Timer Counter 2 */
167         (void*) TC3_Handler,            /* 16 Basic Timer Counter 3 */
168         (void*) TC4_Handler,            /* 17 Basic Timer Counter 4 */
169         (void*) TC5_Handler,            /* 18 Basic Timer Counter 5 */
170 #ifdef ID_TC6
171         (void*) TC6_Handler,            /* 19 Basic Timer Counter 6 */
172 #else
173         (void*) (0UL), /* Reserved */
174 #endif
175 #ifdef ID_TC7
176         (void*) TC7_Handler,            /* 20 Basic Timer Counter 7 */
177 #else
178         (void*) (0UL), /* Reserved */
179 #endif
180 #ifdef ID_ADC
181         (void*) ADC_Handler,            /* 21 Analog Digital Converter */
182 #else
183         (void*) (0UL), /* Reserved */
184 #endif
185 #ifdef ID_AC
186         (void*) AC_Handler,             /* 22 Analog Comparators */
187 #else
188         (void*) (0UL), /* Reserved */
189 #endif
190 #ifdef ID_DAC
191         (void*) DAC_Handler,            /* 23 Digital Analog Converter */
192 #else
193         (void*) (0UL), /* Reserved */
194 #endif
195 #ifdef ID_PTC
196         (void*) PTC_Handler             /* 24 Peripheral Touch Controller */
197 #else
198         (void*) (0UL)  /* Reserved */
199 #endif
200 };
201 
202 /**
203  * \brief This is the code that gets called on processor reset.
204  * To initialize the device, and call the main() routine.
205  */
Reset_Handler(void)206 void Reset_Handler(void)
207 {
208         uint32_t *pSrc, *pDest;
209 
210         /* Initialize the relocate segment */
211         pSrc = &_etext;
212         pDest = &_srelocate;
213 
214         if (pSrc != pDest) {
215                 for (; pDest < &_erelocate;) {
216                         *pDest++ = *pSrc++;
217                 }
218         }
219 
220         /* Clear the zero segment */
221         for (pDest = &_szero; pDest < &_ezero;) {
222                 *pDest++ = 0;
223         }
224 
225         /* Set the vector table base address */
226         pSrc = (uint32_t *) & _sfixed;
227         SCB->VTOR = ((uint32_t) pSrc & SCB_VTOR_TBLOFF_Msk);
228 
229         /* Overwriting the default value of the NVMCTRL.CTRLB.MANW bit (errata reference 13134) */
230         NVMCTRL->CTRLB.bit.MANW = 1;
231 
232         /* Initialize the C library */
233         __libc_init_array();
234 
235         /* Branch to main function */
236         main();
237 
238         /* Infinite loop */
239         while (1);
240 }
241 
242 /**
243  * \brief Default interrupt handler for unused IRQs.
244  */
Dummy_Handler(void)245 void Dummy_Handler(void)
246 {
247         while (1) {
248         }
249 }
250