1 //*****************************************************************************
2 // MK22F12 startup code for use with MCUXpresso IDE
3 //
4 // Version : 160420
5 //*****************************************************************************
6 //
7 // Copyright 2016-2020 NXP
8 // All rights reserved.
9 //
10 // SPDX-License-Identifier: BSD-3-Clause
11 //*****************************************************************************
12 
13 #if defined (DEBUG)
14 #pragma GCC push_options
15 #pragma GCC optimize ("Og")
16 #endif // (DEBUG)
17 
18 #if defined (__cplusplus)
19 #ifdef __REDLIB__
20 #error Redlib does not support C++
21 #else
22 //*****************************************************************************
23 //
24 // The entry point for the C++ library startup
25 //
26 //*****************************************************************************
27 extern "C" {
28     extern void __libc_init_array(void);
29 }
30 #endif
31 #endif
32 
33 #define WEAK __attribute__ ((weak))
34 #define WEAK_AV __attribute__ ((weak, section(".after_vectors")))
35 #define ALIAS(f) __attribute__ ((weak, alias (#f)))
36 
37 //*****************************************************************************
38 #if defined (__cplusplus)
39 extern "C" {
40 #endif
41 
42 //*****************************************************************************
43 // Flash Configuration block : 16-byte flash configuration field that stores
44 // default protection settings (loaded on reset) and security information that
45 // allows the MCU to restrict access to the Flash Memory module.
46 // Placed at address 0x400 by the linker script.
47 //*****************************************************************************
48 __attribute__ ((used,section(".FlashConfig"))) const struct {
49     unsigned int word1;
50     unsigned int word2;
51     unsigned int word3;
52     unsigned int word4;
53 } Flash_Config = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE};
54 //*****************************************************************************
55 // Declaration of external SystemInit function
56 //*****************************************************************************
57 #if defined (__USE_CMSIS)
58 extern void SystemInit(void);
59 #endif // (__USE_CMSIS)
60 
61 //*****************************************************************************
62 // Forward declaration of the core exception handlers.
63 // When the application defines a handler (with the same name), this will
64 // automatically take precedence over these weak definitions.
65 // If your application is a C++ one, then any interrupt handlers defined
66 // in C++ files within in your main application will need to have C linkage
67 // rather than C++ linkage. To do this, make sure that you are using extern "C"
68 // { .... } around the interrupt handler within your main application code.
69 //*****************************************************************************
70      void ResetISR(void);
71 WEAK void NMI_Handler(void);
72 WEAK void HardFault_Handler(void);
73 WEAK void MemManage_Handler(void);
74 WEAK void BusFault_Handler(void);
75 WEAK void UsageFault_Handler(void);
76 WEAK void SVC_Handler(void);
77 WEAK void DebugMon_Handler(void);
78 WEAK void PendSV_Handler(void);
79 WEAK void SysTick_Handler(void);
80 WEAK void IntDefaultHandler(void);
81 
82 //*****************************************************************************
83 // Forward declaration of the application IRQ handlers. When the application
84 // defines a handler (with the same name), this will automatically take
85 // precedence over weak definitions below
86 //*****************************************************************************
87 WEAK void DMA0_IRQHandler(void);
88 WEAK void DMA1_IRQHandler(void);
89 WEAK void DMA2_IRQHandler(void);
90 WEAK void DMA3_IRQHandler(void);
91 WEAK void DMA4_IRQHandler(void);
92 WEAK void DMA5_IRQHandler(void);
93 WEAK void DMA6_IRQHandler(void);
94 WEAK void DMA7_IRQHandler(void);
95 WEAK void DMA8_IRQHandler(void);
96 WEAK void DMA9_IRQHandler(void);
97 WEAK void DMA10_IRQHandler(void);
98 WEAK void DMA11_IRQHandler(void);
99 WEAK void DMA12_IRQHandler(void);
100 WEAK void DMA13_IRQHandler(void);
101 WEAK void DMA14_IRQHandler(void);
102 WEAK void DMA15_IRQHandler(void);
103 WEAK void DMA_Error_IRQHandler(void);
104 WEAK void MCM_IRQHandler(void);
105 WEAK void FTFE_IRQHandler(void);
106 WEAK void Read_Collision_IRQHandler(void);
107 WEAK void LVD_LVW_IRQHandler(void);
108 WEAK void LLWU_IRQHandler(void);
109 WEAK void WDOG_EWM_IRQHandler(void);
110 WEAK void Reserved39_IRQHandler(void);
111 WEAK void I2C0_IRQHandler(void);
112 WEAK void I2C1_IRQHandler(void);
113 WEAK void SPI0_IRQHandler(void);
114 WEAK void SPI1_IRQHandler(void);
115 WEAK void I2S0_Tx_IRQHandler(void);
116 WEAK void I2S0_Rx_IRQHandler(void);
117 WEAK void Reserved46_IRQHandler(void);
118 WEAK void UART0_RX_TX_IRQHandler(void);
119 WEAK void UART0_ERR_IRQHandler(void);
120 WEAK void UART1_RX_TX_IRQHandler(void);
121 WEAK void UART1_ERR_IRQHandler(void);
122 WEAK void UART2_RX_TX_IRQHandler(void);
123 WEAK void UART2_ERR_IRQHandler(void);
124 WEAK void UART3_RX_TX_IRQHandler(void);
125 WEAK void UART3_ERR_IRQHandler(void);
126 WEAK void ADC0_IRQHandler(void);
127 WEAK void CMP0_IRQHandler(void);
128 WEAK void CMP1_IRQHandler(void);
129 WEAK void FTM0_IRQHandler(void);
130 WEAK void FTM1_IRQHandler(void);
131 WEAK void FTM2_IRQHandler(void);
132 WEAK void CMT_IRQHandler(void);
133 WEAK void RTC_IRQHandler(void);
134 WEAK void RTC_Seconds_IRQHandler(void);
135 WEAK void PIT0_IRQHandler(void);
136 WEAK void PIT1_IRQHandler(void);
137 WEAK void PIT2_IRQHandler(void);
138 WEAK void PIT3_IRQHandler(void);
139 WEAK void PDB0_IRQHandler(void);
140 WEAK void USB0_IRQHandler(void);
141 WEAK void USBDCD_IRQHandler(void);
142 WEAK void Reserved71_IRQHandler(void);
143 WEAK void DAC0_IRQHandler(void);
144 WEAK void MCG_IRQHandler(void);
145 WEAK void LPTMR0_IRQHandler(void);
146 WEAK void PORTA_IRQHandler(void);
147 WEAK void PORTB_IRQHandler(void);
148 WEAK void PORTC_IRQHandler(void);
149 WEAK void PORTD_IRQHandler(void);
150 WEAK void PORTE_IRQHandler(void);
151 WEAK void SWI_IRQHandler(void);
152 WEAK void SPI2_IRQHandler(void);
153 WEAK void UART4_RX_TX_IRQHandler(void);
154 WEAK void UART4_ERR_IRQHandler(void);
155 WEAK void UART5_RX_TX_IRQHandler(void);
156 WEAK void UART5_ERR_IRQHandler(void);
157 WEAK void CMP2_IRQHandler(void);
158 WEAK void FTM3_IRQHandler(void);
159 WEAK void DAC1_IRQHandler(void);
160 WEAK void ADC1_IRQHandler(void);
161 WEAK void I2C2_IRQHandler(void);
162 WEAK void CAN0_ORed_Message_buffer_IRQHandler(void);
163 WEAK void CAN0_Bus_Off_IRQHandler(void);
164 WEAK void CAN0_Error_IRQHandler(void);
165 WEAK void CAN0_Tx_Warning_IRQHandler(void);
166 WEAK void CAN0_Rx_Warning_IRQHandler(void);
167 WEAK void CAN0_Wake_Up_IRQHandler(void);
168 WEAK void SDHC_IRQHandler(void);
169 
170 //*****************************************************************************
171 // Forward declaration of the driver IRQ handlers. These are aliased
172 // to the IntDefaultHandler, which is a 'forever' loop. When the driver
173 // defines a handler (with the same name), this will automatically take
174 // precedence over these weak definitions
175 //*****************************************************************************
176 void DMA0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
177 void DMA1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
178 void DMA2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
179 void DMA3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
180 void DMA4_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
181 void DMA5_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
182 void DMA6_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
183 void DMA7_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
184 void DMA8_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
185 void DMA9_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
186 void DMA10_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
187 void DMA11_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
188 void DMA12_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
189 void DMA13_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
190 void DMA14_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
191 void DMA15_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
192 void DMA_Error_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
193 void MCM_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
194 void FTFE_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
195 void Read_Collision_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
196 void LVD_LVW_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
197 void LLWU_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
198 void WDOG_EWM_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
199 void Reserved39_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
200 void I2C0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
201 void I2C1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
202 void SPI0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
203 void SPI1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
204 void I2S0_Tx_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
205 void I2S0_Rx_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
206 void Reserved46_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
207 void UART0_RX_TX_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
208 void UART0_ERR_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
209 void UART1_RX_TX_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
210 void UART1_ERR_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
211 void UART2_RX_TX_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
212 void UART2_ERR_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
213 void UART3_RX_TX_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
214 void UART3_ERR_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
215 void ADC0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
216 void CMP0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
217 void CMP1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
218 void FTM0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
219 void FTM1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
220 void FTM2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
221 void CMT_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
222 void RTC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
223 void RTC_Seconds_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
224 void PIT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
225 void PIT1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
226 void PIT2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
227 void PIT3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
228 void PDB0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
229 void USB0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
230 void USBDCD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
231 void Reserved71_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
232 void DAC0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
233 void MCG_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
234 void LPTMR0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
235 void PORTA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
236 void PORTB_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
237 void PORTC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
238 void PORTD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
239 void PORTE_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
240 void SWI_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
241 void SPI2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
242 void UART4_RX_TX_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
243 void UART4_ERR_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
244 void UART5_RX_TX_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
245 void UART5_ERR_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
246 void CMP2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
247 void FTM3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
248 void DAC1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
249 void ADC1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
250 void I2C2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
251 void CAN0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
252 void SDHC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
253 
254 //*****************************************************************************
255 // The entry point for the application.
256 // __main() is the entry point for Redlib based applications
257 // main() is the entry point for Newlib based applications
258 //*****************************************************************************
259 #if defined (__REDLIB__)
260 extern void __main(void);
261 #endif
262 extern int main(void);
263 
264 //*****************************************************************************
265 // External declaration for the pointer to the stack top from the Linker Script
266 //*****************************************************************************
267 extern void _vStackTop(void);
268 //*****************************************************************************
269 #if defined (__cplusplus)
270 } // extern "C"
271 #endif
272 //*****************************************************************************
273 // The vector table.
274 // This relies on the linker script to place at correct location in memory.
275 //*****************************************************************************
276 
277 
278 
279 extern void (* const g_pfnVectors[])(void);
280 extern void * __Vectors __attribute__ ((alias ("g_pfnVectors")));
281 
282 __attribute__ ((used, section(".isr_vector")))
283 void (* const g_pfnVectors[])(void) = {
284     // Core Level - CM4
285     &_vStackTop,                       // The initial stack pointer
286     ResetISR,                          // The reset handler
287     NMI_Handler,                       // The NMI handler
288     HardFault_Handler,                 // The hard fault handler
289     MemManage_Handler,                 // The MPU fault handler
290     BusFault_Handler,                  // The bus fault handler
291     UsageFault_Handler,                // The usage fault handler
292     0,                                 // Reserved
293     0,                                 // Reserved
294     0,                                 // Reserved
295     0,                                 // Reserved
296     SVC_Handler,                       // SVCall handler
297     DebugMon_Handler,                  // Debug monitor handler
298     0,                                 // Reserved
299     PendSV_Handler,                    // The PendSV handler
300     SysTick_Handler,                   // The SysTick handler
301 
302     // Chip Level - MK22F12
303     DMA0_IRQHandler,                      // 16: DMA Channel 0 Transfer Complete
304     DMA1_IRQHandler,                      // 17: DMA Channel 1 Transfer Complete
305     DMA2_IRQHandler,                      // 18: DMA Channel 2 Transfer Complete
306     DMA3_IRQHandler,                      // 19: DMA Channel 3 Transfer Complete
307     DMA4_IRQHandler,                      // 20: DMA Channel 4 Transfer Complete
308     DMA5_IRQHandler,                      // 21: DMA Channel 5 Transfer Complete
309     DMA6_IRQHandler,                      // 22: DMA Channel 6 Transfer Complete
310     DMA7_IRQHandler,                      // 23: DMA Channel 7 Transfer Complete
311     DMA8_IRQHandler,                      // 24: DMA Channel 8 Transfer Complete
312     DMA9_IRQHandler,                      // 25: DMA Channel 9 Transfer Complete
313     DMA10_IRQHandler,                     // 26: DMA Channel 10 Transfer Complete
314     DMA11_IRQHandler,                     // 27: DMA Channel 11 Transfer Complete
315     DMA12_IRQHandler,                     // 28: DMA Channel 12 Transfer Complete
316     DMA13_IRQHandler,                     // 29: DMA Channel 13 Transfer Complete
317     DMA14_IRQHandler,                     // 30: DMA Channel 14 Transfer Complete
318     DMA15_IRQHandler,                     // 31: DMA Channel 15 Transfer Complete
319     DMA_Error_IRQHandler,                 // 32: DMA Error Interrupt
320     MCM_IRQHandler,                       // 33: Normal Interrupt
321     FTFE_IRQHandler,                      // 34: FTFE Command complete interrupt
322     Read_Collision_IRQHandler,            // 35: Read Collision Interrupt
323     LVD_LVW_IRQHandler,                   // 36: Low Voltage Detect, Low Voltage Warning
324     LLWU_IRQHandler,                      // 37: Low Leakage Wakeup Unit
325     WDOG_EWM_IRQHandler,                  // 38: WDOG Interrupt
326     Reserved39_IRQHandler,                // 39: Reserved interrupt
327     I2C0_IRQHandler,                      // 40: I2C0 interrupt
328     I2C1_IRQHandler,                      // 41: I2C1 interrupt
329     SPI0_IRQHandler,                      // 42: SPI0 Interrupt
330     SPI1_IRQHandler,                      // 43: SPI1 Interrupt
331     I2S0_Tx_IRQHandler,                   // 44: I2S0 transmit interrupt
332     I2S0_Rx_IRQHandler,                   // 45: I2S0 receive interrupt
333     Reserved46_IRQHandler,                // 46: Reserved interrupt
334     UART0_RX_TX_IRQHandler,               // 47: UART0 Receive/Transmit interrupt
335     UART0_ERR_IRQHandler,                 // 48: UART0 Error interrupt
336     UART1_RX_TX_IRQHandler,               // 49: UART1 Receive/Transmit interrupt
337     UART1_ERR_IRQHandler,                 // 50: UART1 Error interrupt
338     UART2_RX_TX_IRQHandler,               // 51: UART2 Receive/Transmit interrupt
339     UART2_ERR_IRQHandler,                 // 52: UART2 Error interrupt
340     UART3_RX_TX_IRQHandler,               // 53: UART3 Receive/Transmit interrupt
341     UART3_ERR_IRQHandler,                 // 54: UART3 Error interrupt
342     ADC0_IRQHandler,                      // 55: ADC0 interrupt
343     CMP0_IRQHandler,                      // 56: CMP0 interrupt
344     CMP1_IRQHandler,                      // 57: CMP1 interrupt
345     FTM0_IRQHandler,                      // 58: FTM0 fault, overflow and channels interrupt
346     FTM1_IRQHandler,                      // 59: FTM1 fault, overflow and channels interrupt
347     FTM2_IRQHandler,                      // 60: FTM2 fault, overflow and channels interrupt
348     CMT_IRQHandler,                       // 61: CMT interrupt
349     RTC_IRQHandler,                       // 62: RTC interrupt
350     RTC_Seconds_IRQHandler,               // 63: RTC seconds interrupt
351     PIT0_IRQHandler,                      // 64: PIT timer channel 0 interrupt
352     PIT1_IRQHandler,                      // 65: PIT timer channel 1 interrupt
353     PIT2_IRQHandler,                      // 66: PIT timer channel 2 interrupt
354     PIT3_IRQHandler,                      // 67: PIT timer channel 3 interrupt
355     PDB0_IRQHandler,                      // 68: PDB0 Interrupt
356     USB0_IRQHandler,                      // 69: USB0 interrupt
357     USBDCD_IRQHandler,                    // 70: USBDCD Interrupt
358     Reserved71_IRQHandler,                // 71: Reserved interrupt
359     DAC0_IRQHandler,                      // 72: DAC0 interrupt
360     MCG_IRQHandler,                       // 73: MCG Interrupt
361     LPTMR0_IRQHandler,                    // 74: LPTimer interrupt
362     PORTA_IRQHandler,                     // 75: Port A interrupt
363     PORTB_IRQHandler,                     // 76: Port B interrupt
364     PORTC_IRQHandler,                     // 77: Port C interrupt
365     PORTD_IRQHandler,                     // 78: Port D interrupt
366     PORTE_IRQHandler,                     // 79: Port E interrupt
367     SWI_IRQHandler,                       // 80: Software interrupt
368     SPI2_IRQHandler,                      // 81: SPI2 Interrupt
369     UART4_RX_TX_IRQHandler,               // 82: UART4 Receive/Transmit interrupt
370     UART4_ERR_IRQHandler,                 // 83: UART4 Error interrupt
371     UART5_RX_TX_IRQHandler,               // 84: UART5 Receive/Transmit interrupt
372     UART5_ERR_IRQHandler,                 // 85: UART5 Error interrupt
373     CMP2_IRQHandler,                      // 86: CMP2 interrupt
374     FTM3_IRQHandler,                      // 87: FTM3 fault, overflow and channels interrupt
375     DAC1_IRQHandler,                      // 88: DAC1 interrupt
376     ADC1_IRQHandler,                      // 89: ADC1 interrupt
377     I2C2_IRQHandler,                      // 90: I2C2 interrupt
378     CAN0_ORed_Message_buffer_IRQHandler,  // 91: CAN0 OR'd message buffers interrupt
379     CAN0_Bus_Off_IRQHandler,              // 92: CAN0 bus off interrupt
380     CAN0_Error_IRQHandler,                // 93: CAN0 error interrupt
381     CAN0_Tx_Warning_IRQHandler,           // 94: CAN0 Tx warning interrupt
382     CAN0_Rx_Warning_IRQHandler,           // 95: CAN0 Rx warning interrupt
383     CAN0_Wake_Up_IRQHandler,              // 96: CAN0 wake up interrupt
384     SDHC_IRQHandler,                      // 97: SDHC interrupt
385 
386 
387 }; /* End of g_pfnVectors */
388 
389 //*****************************************************************************
390 // Functions to carry out the initialization of RW and BSS data sections. These
391 // are written as separate functions rather than being inlined within the
392 // ResetISR() function in order to cope with MCUs with multiple banks of
393 // memory.
394 //*****************************************************************************
395 __attribute__ ((section(".after_vectors.init_data")))
data_init(unsigned int romstart,unsigned int start,unsigned int len)396 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
397     unsigned int *pulDest = (unsigned int*) start;
398     unsigned int *pulSrc = (unsigned int*) romstart;
399     unsigned int loop;
400     for (loop = 0; loop < len; loop = loop + 4)
401         *pulDest++ = *pulSrc++;
402 }
403 
404 __attribute__ ((section(".after_vectors.init_bss")))
bss_init(unsigned int start,unsigned int len)405 void bss_init(unsigned int start, unsigned int len) {
406     unsigned int *pulDest = (unsigned int*) start;
407     unsigned int loop;
408     for (loop = 0; loop < len; loop = loop + 4)
409         *pulDest++ = 0;
410 }
411 
412 //*****************************************************************************
413 // The following symbols are constructs generated by the linker, indicating
414 // the location of various points in the "Global Section Table". This table is
415 // created by the linker via the Code Red managed linker script mechanism. It
416 // contains the load address, execution address and length of each RW data
417 // section and the execution and length of each BSS (zero initialized) section.
418 //*****************************************************************************
419 extern unsigned int __data_section_table;
420 extern unsigned int __data_section_table_end;
421 extern unsigned int __bss_section_table;
422 extern unsigned int __bss_section_table_end;
423 
424 //*****************************************************************************
425 // Reset entry point for your code.
426 // Sets up a simple runtime environment and initializes the C/C++
427 // library.
428 //*****************************************************************************
429 __attribute__ ((naked, section(".after_vectors.reset")))
ResetISR(void)430 void ResetISR(void) {
431 
432     // Disable interrupts
433     __asm volatile ("cpsid i");
434 
435 
436 #if defined (__USE_CMSIS)
437 // If __USE_CMSIS defined, then call CMSIS SystemInit code
438     SystemInit();
439 
440 #else
441     // Disable Watchdog
442     //  Write 0xC520 to watchdog unlock register
443     *((volatile unsigned short *)0x4005200E) = 0xC520;
444     //  Followed by 0xD928 to complete the unlock
445     *((volatile unsigned short *)0x4005200E) = 0xD928;
446     // Now disable watchdog via STCTRLH register
447     *((volatile unsigned short *)0x40052000) = 0x01D2u;
448 #endif // (__USE_CMSIS)
449 
450     //
451     // Copy the data sections from flash to SRAM.
452     //
453     unsigned int LoadAddr, ExeAddr, SectionLen;
454     unsigned int *SectionTableAddr;
455 
456     // Load base address of Global Section Table
457     SectionTableAddr = &__data_section_table;
458 
459     // Copy the data sections from flash to SRAM.
460     while (SectionTableAddr < &__data_section_table_end) {
461         LoadAddr = *SectionTableAddr++;
462         ExeAddr = *SectionTableAddr++;
463         SectionLen = *SectionTableAddr++;
464         data_init(LoadAddr, ExeAddr, SectionLen);
465     }
466 
467     // At this point, SectionTableAddr = &__bss_section_table;
468     // Zero fill the bss segment
469     while (SectionTableAddr < &__bss_section_table_end) {
470         ExeAddr = *SectionTableAddr++;
471         SectionLen = *SectionTableAddr++;
472         bss_init(ExeAddr, SectionLen);
473     }
474 
475 #if !defined (__USE_CMSIS)
476 // Assume that if __USE_CMSIS defined, then CMSIS SystemInit code
477 // will enable the FPU
478 #if defined (__VFP_FP__) && !defined (__SOFTFP__)
479     //
480     // Code to enable the Cortex-M4 FPU only included
481     // if appropriate build options have been selected.
482     // Code taken from Section 7.1, Cortex-M4 TRM (DDI0439C)
483     //
484     // Read CPACR (located at address 0xE000ED88)
485     // Set bits 20-23 to enable CP10 and CP11 coprocessors
486     // Write back the modified value to the CPACR
487     asm volatile ("LDR.W R0, =0xE000ED88\n\t"
488                   "LDR R1, [R0]\n\t"
489                   "ORR R1, R1, #(0xF << 20)\n\t"
490                   "STR R1, [R0]");
491 #endif // (__VFP_FP__) && !(__SOFTFP__)
492 #endif // (__USE_CMSIS)
493 
494 
495 #if !defined (__USE_CMSIS)
496 // Assume that if __USE_CMSIS defined, then CMSIS SystemInit code
497 // will setup the VTOR register
498 
499     // Check to see if we are running the code from a non-zero
500     // address (eg RAM, external flash), in which case we need
501     // to modify the VTOR register to tell the CPU that the
502     // vector table is located at a non-0x0 address.
503     unsigned int * pSCB_VTOR = (unsigned int *) 0xE000ED08;
504     if ((unsigned int *)g_pfnVectors!=(unsigned int *) 0x00000000) {
505         *pSCB_VTOR = (unsigned int)g_pfnVectors;
506     }
507 #endif // (__USE_CMSIS)
508 #if defined (__cplusplus)
509     //
510     // Call C++ library initialisation
511     //
512     __libc_init_array();
513 #endif
514 
515     // Reenable interrupts
516     __asm volatile ("cpsie i");
517 
518 #if defined (__REDLIB__)
519     // Call the Redlib library, which in turn calls main()
520     __main();
521 #else
522     main();
523 #endif
524 
525     //
526     // main() shouldn't return, but if it does, we'll just enter an infinite loop
527     //
528     while (1) {
529         ;
530     }
531 }
532 
533 //*****************************************************************************
534 // Default core exception handlers. Override the ones here by defining your own
535 // handler routines in your application code.
536 //*****************************************************************************
NMI_Handler(void)537 WEAK_AV void NMI_Handler(void)
538 { while(1) {}
539 }
540 
HardFault_Handler(void)541 WEAK_AV void HardFault_Handler(void)
542 { while(1) {}
543 }
544 
MemManage_Handler(void)545 WEAK_AV void MemManage_Handler(void)
546 { while(1) {}
547 }
548 
BusFault_Handler(void)549 WEAK_AV void BusFault_Handler(void)
550 { while(1) {}
551 }
552 
UsageFault_Handler(void)553 WEAK_AV void UsageFault_Handler(void)
554 { while(1) {}
555 }
556 
SVC_Handler(void)557 WEAK_AV void SVC_Handler(void)
558 { while(1) {}
559 }
560 
DebugMon_Handler(void)561 WEAK_AV void DebugMon_Handler(void)
562 { while(1) {}
563 }
564 
PendSV_Handler(void)565 WEAK_AV void PendSV_Handler(void)
566 { while(1) {}
567 }
568 
SysTick_Handler(void)569 WEAK_AV void SysTick_Handler(void)
570 { while(1) {}
571 }
572 
573 //*****************************************************************************
574 // Processor ends up here if an unexpected interrupt occurs or a specific
575 // handler is not present in the application code.
576 //*****************************************************************************
IntDefaultHandler(void)577 WEAK_AV void IntDefaultHandler(void)
578 { while(1) {}
579 }
580 
581 //*****************************************************************************
582 // Default application exception handlers. Override the ones here by defining
583 // your own handler routines in your application code. These routines call
584 // driver exception handlers or IntDefaultHandler() if no driver exception
585 // handler is included.
586 //*****************************************************************************
DMA0_IRQHandler(void)587 WEAK void DMA0_IRQHandler(void)
588 {   DMA0_DriverIRQHandler();
589 }
590 
DMA1_IRQHandler(void)591 WEAK void DMA1_IRQHandler(void)
592 {   DMA1_DriverIRQHandler();
593 }
594 
DMA2_IRQHandler(void)595 WEAK void DMA2_IRQHandler(void)
596 {   DMA2_DriverIRQHandler();
597 }
598 
DMA3_IRQHandler(void)599 WEAK void DMA3_IRQHandler(void)
600 {   DMA3_DriverIRQHandler();
601 }
602 
DMA4_IRQHandler(void)603 WEAK void DMA4_IRQHandler(void)
604 {   DMA4_DriverIRQHandler();
605 }
606 
DMA5_IRQHandler(void)607 WEAK void DMA5_IRQHandler(void)
608 {   DMA5_DriverIRQHandler();
609 }
610 
DMA6_IRQHandler(void)611 WEAK void DMA6_IRQHandler(void)
612 {   DMA6_DriverIRQHandler();
613 }
614 
DMA7_IRQHandler(void)615 WEAK void DMA7_IRQHandler(void)
616 {   DMA7_DriverIRQHandler();
617 }
618 
DMA8_IRQHandler(void)619 WEAK void DMA8_IRQHandler(void)
620 {   DMA8_DriverIRQHandler();
621 }
622 
DMA9_IRQHandler(void)623 WEAK void DMA9_IRQHandler(void)
624 {   DMA9_DriverIRQHandler();
625 }
626 
DMA10_IRQHandler(void)627 WEAK void DMA10_IRQHandler(void)
628 {   DMA10_DriverIRQHandler();
629 }
630 
DMA11_IRQHandler(void)631 WEAK void DMA11_IRQHandler(void)
632 {   DMA11_DriverIRQHandler();
633 }
634 
DMA12_IRQHandler(void)635 WEAK void DMA12_IRQHandler(void)
636 {   DMA12_DriverIRQHandler();
637 }
638 
DMA13_IRQHandler(void)639 WEAK void DMA13_IRQHandler(void)
640 {   DMA13_DriverIRQHandler();
641 }
642 
DMA14_IRQHandler(void)643 WEAK void DMA14_IRQHandler(void)
644 {   DMA14_DriverIRQHandler();
645 }
646 
DMA15_IRQHandler(void)647 WEAK void DMA15_IRQHandler(void)
648 {   DMA15_DriverIRQHandler();
649 }
650 
DMA_Error_IRQHandler(void)651 WEAK void DMA_Error_IRQHandler(void)
652 {   DMA_Error_DriverIRQHandler();
653 }
654 
MCM_IRQHandler(void)655 WEAK void MCM_IRQHandler(void)
656 {   MCM_DriverIRQHandler();
657 }
658 
FTFE_IRQHandler(void)659 WEAK void FTFE_IRQHandler(void)
660 {   FTFE_DriverIRQHandler();
661 }
662 
Read_Collision_IRQHandler(void)663 WEAK void Read_Collision_IRQHandler(void)
664 {   Read_Collision_DriverIRQHandler();
665 }
666 
LVD_LVW_IRQHandler(void)667 WEAK void LVD_LVW_IRQHandler(void)
668 {   LVD_LVW_DriverIRQHandler();
669 }
670 
LLWU_IRQHandler(void)671 WEAK void LLWU_IRQHandler(void)
672 {   LLWU_DriverIRQHandler();
673 }
674 
WDOG_EWM_IRQHandler(void)675 WEAK void WDOG_EWM_IRQHandler(void)
676 {   WDOG_EWM_DriverIRQHandler();
677 }
678 
Reserved39_IRQHandler(void)679 WEAK void Reserved39_IRQHandler(void)
680 {   Reserved39_DriverIRQHandler();
681 }
682 
I2C0_IRQHandler(void)683 WEAK void I2C0_IRQHandler(void)
684 {   I2C0_DriverIRQHandler();
685 }
686 
I2C1_IRQHandler(void)687 WEAK void I2C1_IRQHandler(void)
688 {   I2C1_DriverIRQHandler();
689 }
690 
SPI0_IRQHandler(void)691 WEAK void SPI0_IRQHandler(void)
692 {   SPI0_DriverIRQHandler();
693 }
694 
SPI1_IRQHandler(void)695 WEAK void SPI1_IRQHandler(void)
696 {   SPI1_DriverIRQHandler();
697 }
698 
I2S0_Tx_IRQHandler(void)699 WEAK void I2S0_Tx_IRQHandler(void)
700 {   I2S0_Tx_DriverIRQHandler();
701 }
702 
I2S0_Rx_IRQHandler(void)703 WEAK void I2S0_Rx_IRQHandler(void)
704 {   I2S0_Rx_DriverIRQHandler();
705 }
706 
Reserved46_IRQHandler(void)707 WEAK void Reserved46_IRQHandler(void)
708 {   Reserved46_DriverIRQHandler();
709 }
710 
UART0_RX_TX_IRQHandler(void)711 WEAK void UART0_RX_TX_IRQHandler(void)
712 {   UART0_RX_TX_DriverIRQHandler();
713 }
714 
UART0_ERR_IRQHandler(void)715 WEAK void UART0_ERR_IRQHandler(void)
716 {   UART0_ERR_DriverIRQHandler();
717 }
718 
UART1_RX_TX_IRQHandler(void)719 WEAK void UART1_RX_TX_IRQHandler(void)
720 {   UART1_RX_TX_DriverIRQHandler();
721 }
722 
UART1_ERR_IRQHandler(void)723 WEAK void UART1_ERR_IRQHandler(void)
724 {   UART1_ERR_DriverIRQHandler();
725 }
726 
UART2_RX_TX_IRQHandler(void)727 WEAK void UART2_RX_TX_IRQHandler(void)
728 {   UART2_RX_TX_DriverIRQHandler();
729 }
730 
UART2_ERR_IRQHandler(void)731 WEAK void UART2_ERR_IRQHandler(void)
732 {   UART2_ERR_DriverIRQHandler();
733 }
734 
UART3_RX_TX_IRQHandler(void)735 WEAK void UART3_RX_TX_IRQHandler(void)
736 {   UART3_RX_TX_DriverIRQHandler();
737 }
738 
UART3_ERR_IRQHandler(void)739 WEAK void UART3_ERR_IRQHandler(void)
740 {   UART3_ERR_DriverIRQHandler();
741 }
742 
ADC0_IRQHandler(void)743 WEAK void ADC0_IRQHandler(void)
744 {   ADC0_DriverIRQHandler();
745 }
746 
CMP0_IRQHandler(void)747 WEAK void CMP0_IRQHandler(void)
748 {   CMP0_DriverIRQHandler();
749 }
750 
CMP1_IRQHandler(void)751 WEAK void CMP1_IRQHandler(void)
752 {   CMP1_DriverIRQHandler();
753 }
754 
FTM0_IRQHandler(void)755 WEAK void FTM0_IRQHandler(void)
756 {   FTM0_DriverIRQHandler();
757 }
758 
FTM1_IRQHandler(void)759 WEAK void FTM1_IRQHandler(void)
760 {   FTM1_DriverIRQHandler();
761 }
762 
FTM2_IRQHandler(void)763 WEAK void FTM2_IRQHandler(void)
764 {   FTM2_DriverIRQHandler();
765 }
766 
CMT_IRQHandler(void)767 WEAK void CMT_IRQHandler(void)
768 {   CMT_DriverIRQHandler();
769 }
770 
RTC_IRQHandler(void)771 WEAK void RTC_IRQHandler(void)
772 {   RTC_DriverIRQHandler();
773 }
774 
RTC_Seconds_IRQHandler(void)775 WEAK void RTC_Seconds_IRQHandler(void)
776 {   RTC_Seconds_DriverIRQHandler();
777 }
778 
PIT0_IRQHandler(void)779 WEAK void PIT0_IRQHandler(void)
780 {   PIT0_DriverIRQHandler();
781 }
782 
PIT1_IRQHandler(void)783 WEAK void PIT1_IRQHandler(void)
784 {   PIT1_DriverIRQHandler();
785 }
786 
PIT2_IRQHandler(void)787 WEAK void PIT2_IRQHandler(void)
788 {   PIT2_DriverIRQHandler();
789 }
790 
PIT3_IRQHandler(void)791 WEAK void PIT3_IRQHandler(void)
792 {   PIT3_DriverIRQHandler();
793 }
794 
PDB0_IRQHandler(void)795 WEAK void PDB0_IRQHandler(void)
796 {   PDB0_DriverIRQHandler();
797 }
798 
USB0_IRQHandler(void)799 WEAK void USB0_IRQHandler(void)
800 {   USB0_DriverIRQHandler();
801 }
802 
USBDCD_IRQHandler(void)803 WEAK void USBDCD_IRQHandler(void)
804 {   USBDCD_DriverIRQHandler();
805 }
806 
Reserved71_IRQHandler(void)807 WEAK void Reserved71_IRQHandler(void)
808 {   Reserved71_DriverIRQHandler();
809 }
810 
DAC0_IRQHandler(void)811 WEAK void DAC0_IRQHandler(void)
812 {   DAC0_DriverIRQHandler();
813 }
814 
MCG_IRQHandler(void)815 WEAK void MCG_IRQHandler(void)
816 {   MCG_DriverIRQHandler();
817 }
818 
LPTMR0_IRQHandler(void)819 WEAK void LPTMR0_IRQHandler(void)
820 {   LPTMR0_DriverIRQHandler();
821 }
822 
PORTA_IRQHandler(void)823 WEAK void PORTA_IRQHandler(void)
824 {   PORTA_DriverIRQHandler();
825 }
826 
PORTB_IRQHandler(void)827 WEAK void PORTB_IRQHandler(void)
828 {   PORTB_DriverIRQHandler();
829 }
830 
PORTC_IRQHandler(void)831 WEAK void PORTC_IRQHandler(void)
832 {   PORTC_DriverIRQHandler();
833 }
834 
PORTD_IRQHandler(void)835 WEAK void PORTD_IRQHandler(void)
836 {   PORTD_DriverIRQHandler();
837 }
838 
PORTE_IRQHandler(void)839 WEAK void PORTE_IRQHandler(void)
840 {   PORTE_DriverIRQHandler();
841 }
842 
SWI_IRQHandler(void)843 WEAK void SWI_IRQHandler(void)
844 {   SWI_DriverIRQHandler();
845 }
846 
SPI2_IRQHandler(void)847 WEAK void SPI2_IRQHandler(void)
848 {   SPI2_DriverIRQHandler();
849 }
850 
UART4_RX_TX_IRQHandler(void)851 WEAK void UART4_RX_TX_IRQHandler(void)
852 {   UART4_RX_TX_DriverIRQHandler();
853 }
854 
UART4_ERR_IRQHandler(void)855 WEAK void UART4_ERR_IRQHandler(void)
856 {   UART4_ERR_DriverIRQHandler();
857 }
858 
UART5_RX_TX_IRQHandler(void)859 WEAK void UART5_RX_TX_IRQHandler(void)
860 {   UART5_RX_TX_DriverIRQHandler();
861 }
862 
UART5_ERR_IRQHandler(void)863 WEAK void UART5_ERR_IRQHandler(void)
864 {   UART5_ERR_DriverIRQHandler();
865 }
866 
CMP2_IRQHandler(void)867 WEAK void CMP2_IRQHandler(void)
868 {   CMP2_DriverIRQHandler();
869 }
870 
FTM3_IRQHandler(void)871 WEAK void FTM3_IRQHandler(void)
872 {   FTM3_DriverIRQHandler();
873 }
874 
DAC1_IRQHandler(void)875 WEAK void DAC1_IRQHandler(void)
876 {   DAC1_DriverIRQHandler();
877 }
878 
ADC1_IRQHandler(void)879 WEAK void ADC1_IRQHandler(void)
880 {   ADC1_DriverIRQHandler();
881 }
882 
I2C2_IRQHandler(void)883 WEAK void I2C2_IRQHandler(void)
884 {   I2C2_DriverIRQHandler();
885 }
886 
CAN0_ORed_Message_buffer_IRQHandler(void)887 WEAK void CAN0_ORed_Message_buffer_IRQHandler(void)
888 {   CAN0_DriverIRQHandler();
889 }
890 
CAN0_Bus_Off_IRQHandler(void)891 WEAK void CAN0_Bus_Off_IRQHandler(void)
892 {   CAN0_DriverIRQHandler();
893 }
894 
CAN0_Error_IRQHandler(void)895 WEAK void CAN0_Error_IRQHandler(void)
896 {   CAN0_DriverIRQHandler();
897 }
898 
CAN0_Tx_Warning_IRQHandler(void)899 WEAK void CAN0_Tx_Warning_IRQHandler(void)
900 {   CAN0_DriverIRQHandler();
901 }
902 
CAN0_Rx_Warning_IRQHandler(void)903 WEAK void CAN0_Rx_Warning_IRQHandler(void)
904 {   CAN0_DriverIRQHandler();
905 }
906 
CAN0_Wake_Up_IRQHandler(void)907 WEAK void CAN0_Wake_Up_IRQHandler(void)
908 {   CAN0_DriverIRQHandler();
909 }
910 
SDHC_IRQHandler(void)911 WEAK void SDHC_IRQHandler(void)
912 {   SDHC_DriverIRQHandler();
913 }
914 
915 //*****************************************************************************
916 
917 #if defined (DEBUG)
918 #pragma GCC pop_options
919 #endif // (DEBUG)
920