1 //*****************************************************************************
2 // MKE13Z7 startup code for use with MCUXpresso IDE
3 //
4 // Version : 081121
5 //*****************************************************************************
6 //
7 // Copyright 2016-2021 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 SVC_Handler(void);
74 WEAK void PendSV_Handler(void);
75 WEAK void SysTick_Handler(void);
76 WEAK void IntDefaultHandler(void);
77 
78 //*****************************************************************************
79 // Forward declaration of the application IRQ handlers. When the application
80 // defines a handler (with the same name), this will automatically take
81 // precedence over weak definitions below
82 //*****************************************************************************
83 WEAK void DMA0_04_IRQHandler(void);
84 WEAK void DMA0_15_IRQHandler(void);
85 WEAK void DMA0_26_IRQHandler(void);
86 WEAK void DMA0_37_IRQHandler(void);
87 WEAK void DMA_Error_IRQHandler(void);
88 WEAK void FTFA_IRQHandler(void);
89 WEAK void LVD_LVW_IRQHandler(void);
90 WEAK void PORTAE_IRQHandler(void);
91 WEAK void LPI2C0_IRQHandler(void);
92 WEAK void Reserved25_IRQHandler(void);
93 WEAK void LPSPI0_IRQHandler(void);
94 WEAK void Reserved27_IRQHandler(void);
95 WEAK void LPUART0_IRQHandler(void);
96 WEAK void LPUART1_IRQHandler(void);
97 WEAK void LPUART2_IRQHandler(void);
98 WEAK void ADC0_IRQHandler(void);
99 WEAK void CMP0_IRQHandler(void);
100 WEAK void FTM0_IRQHandler(void);
101 WEAK void FTM1_IRQHandler(void);
102 WEAK void FTM2_IRQHandler(void);
103 WEAK void Reserved36_IRQHandler(void);
104 WEAK void Reserved37_IRQHandler(void);
105 WEAK void LPIT0_IRQHandler(void);
106 WEAK void FLEXIO_IRQHandler(void);
107 WEAK void TSI0_IRQHandler(void);
108 WEAK void Reserved41_IRQHandler(void);
109 WEAK void PORTBCD_IRQHandler(void);
110 WEAK void SCG_IRQHandler(void);
111 WEAK void WDOG_EWM_IRQHandler(void);
112 WEAK void PWT_LPTMR0_IRQHandler(void);
113 WEAK void Reserved46_IRQHandler(void);
114 WEAK void RCM_IRQHandler(void);
115 
116 //*****************************************************************************
117 // Forward declaration of the driver IRQ handlers. These are aliased
118 // to the IntDefaultHandler, which is a 'forever' loop. When the driver
119 // defines a handler (with the same name), this will automatically take
120 // precedence over these weak definitions
121 //*****************************************************************************
122 void DMA0_04_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
123 void DMA0_15_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
124 void DMA0_26_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
125 void DMA0_37_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
126 void DMA_Error_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
127 void FTFA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
128 void LVD_LVW_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
129 void PORTAE_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
130 void LPI2C0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
131 void Reserved25_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
132 void LPSPI0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
133 void Reserved27_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
134 void LPUART0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
135 void LPUART1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
136 void LPUART2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
137 void ADC0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
138 void CMP0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
139 void FTM0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
140 void FTM1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
141 void FTM2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
142 void Reserved36_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
143 void Reserved37_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
144 void LPIT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
145 void FLEXIO_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
146 void TSI0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
147 void Reserved41_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
148 void PORTBCD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
149 void SCG_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
150 void WDOG_EWM_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
151 void PWT_LPTMR0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
152 void Reserved46_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
153 void RCM_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
154 
155 //*****************************************************************************
156 // The entry point for the application.
157 // __main() is the entry point for Redlib based applications
158 // main() is the entry point for Newlib based applications
159 //*****************************************************************************
160 #if defined (__REDLIB__)
161 extern void __main(void);
162 #endif
163 extern int main(void);
164 
165 //*****************************************************************************
166 // External declaration for the pointer to the stack top from the Linker Script
167 //*****************************************************************************
168 extern void _vStackTop(void);
169 //*****************************************************************************
170 #if defined (__cplusplus)
171 } // extern "C"
172 #endif
173 //*****************************************************************************
174 // The vector table.
175 // This relies on the linker script to place at correct location in memory.
176 //*****************************************************************************
177 
178 extern void (* const g_pfnVectors[])(void);
179 extern void * __Vectors __attribute__ ((alias ("g_pfnVectors")));
180 
181 __attribute__ ((used, section(".isr_vector")))
182 void (* const g_pfnVectors[])(void) = {
183     // Core Level - CM0P
184     &_vStackTop,                       // The initial stack pointer
185     ResetISR,                          // The reset handler
186     NMI_Handler,                       // The NMI handler
187     HardFault_Handler,                 // The hard fault handler
188     0,                                 // Reserved
189     0,                                 // Reserved
190     0,                                 // Reserved
191     0,                                 // Reserved
192     0,                                 // Reserved
193     0,                                 // Reserved
194     0,                                 // Reserved
195     SVC_Handler,                       // SVCall handler
196     0,                                 // Reserved
197     0,                                 // Reserved
198     PendSV_Handler,                    // The PendSV handler
199     SysTick_Handler,                   // The SysTick handler
200 
201     // Chip Level - MKE13Z7
202     DMA0_04_IRQHandler,      // 16: DMA channel 0 or 4 transfer complete
203     DMA0_15_IRQHandler,      // 17: DMA channel 1 or 5 transfer complete
204     DMA0_26_IRQHandler,      // 18: DMA channel 2 or 6 transfer complete
205     DMA0_37_IRQHandler,      // 19: DMA channel 3 or 7 transfer complete
206     DMA_Error_IRQHandler,    // 20: DMA error interrupt channels 0-7
207     FTFA_IRQHandler,         // 21: Single interrupt vector for all sources
208     LVD_LVW_IRQHandler,      // 22: Low-voltage detect, low-voltage warning
209     PORTAE_IRQHandler,       // 23: Pin detect (Port A, E)
210     LPI2C0_IRQHandler,       // 24: Inter-integrated circuit 0 interrupt
211     Reserved25_IRQHandler,   // 25: Reserved interrupt
212     LPSPI0_IRQHandler,       // 26: Serial peripheral Interface 0 interrupt
213     Reserved27_IRQHandler,   // 27: Reserved interrupt
214     LPUART0_IRQHandler,      // 28: Single interrupt vector for all sources
215     LPUART1_IRQHandler,      // 29: Single interrupt vector for all sources
216     LPUART2_IRQHandler,      // 30: Single interrupt vector for all sources
217     ADC0_IRQHandler,         // 31: ADC0 conversion complete interrupt
218     CMP0_IRQHandler,         // 32: CMP0 interrupt
219     FTM0_IRQHandler,         // 33: FTM0 single interrupt vector for all sources
220     FTM1_IRQHandler,         // 34: FTM1 single interrupt vector for all sources
221     FTM2_IRQHandler,         // 35: FTM2 single interrupt vector for all sources
222     Reserved36_IRQHandler,   // 36: Reserved interrupt
223     Reserved37_IRQHandler,   // 37: Reserved interrupt
224     LPIT0_IRQHandler,        // 38: LPIT interrupt
225     FLEXIO_IRQHandler,       // 39: FLEXIO interrupt
226     TSI0_IRQHandler,         // 40: TSI0 interrupt
227     Reserved41_IRQHandler,   // 41: Reserved interrupt
228     PORTBCD_IRQHandler,      // 42: Pin detect (Port B, C, D)
229     SCG_IRQHandler,          // 43: System clock generator interrupt
230     WDOG_EWM_IRQHandler,     // 44: Single interrupt vector for WDOG and EWM
231     PWT_LPTMR0_IRQHandler,   // 45: Single interrupt vector for PWT and LPTMR0
232     Reserved46_IRQHandler,   // 46: Reserved interrupt
233     RCM_IRQHandler,          // 47: RCM interrupt
234 }; /* End of g_pfnVectors */
235 
236 //*****************************************************************************
237 // Functions to carry out the initialization of RW and BSS data sections. These
238 // are written as separate functions rather than being inlined within the
239 // ResetISR() function in order to cope with MCUs with multiple banks of
240 // memory.
241 //*****************************************************************************
242 __attribute__ ((section(".after_vectors.init_data")))
data_init(unsigned int romstart,unsigned int start,unsigned int len)243 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
244     unsigned int *pulDest = (unsigned int*) start;
245     unsigned int *pulSrc = (unsigned int*) romstart;
246     unsigned int loop;
247     for (loop = 0; loop < len; loop = loop + 4)
248         *pulDest++ = *pulSrc++;
249 }
250 
251 __attribute__ ((section(".after_vectors.init_bss")))
bss_init(unsigned int start,unsigned int len)252 void bss_init(unsigned int start, unsigned int len) {
253     unsigned int *pulDest = (unsigned int*) start;
254     unsigned int loop;
255     for (loop = 0; loop < len; loop = loop + 4)
256         *pulDest++ = 0;
257 }
258 
259 //*****************************************************************************
260 // The following symbols are constructs generated by the linker, indicating
261 // the location of various points in the "Global Section Table". This table is
262 // created by the linker via the Code Red managed linker script mechanism. It
263 // contains the load address, execution address and length of each RW data
264 // section and the execution and length of each BSS (zero initialized) section.
265 //*****************************************************************************
266 extern unsigned int __data_section_table;
267 extern unsigned int __data_section_table_end;
268 extern unsigned int __bss_section_table;
269 extern unsigned int __bss_section_table_end;
270 
271 //*****************************************************************************
272 // Reset entry point for your code.
273 // Sets up a simple runtime environment and initializes the C/C++
274 // library.
275 //*****************************************************************************
276 __attribute__ ((naked, section(".after_vectors.reset")))
ResetISR(void)277 void ResetISR(void) {
278     // Disable interrupts
279     __asm volatile ("cpsid i");
280 
281 #if defined (__USE_CMSIS)
282 // If __USE_CMSIS defined, then call CMSIS SystemInit code
283     SystemInit();
284 
285 #else
286     // Disable Watchdog
287     // Write watchdog update key to unlock
288     *((volatile unsigned int *)0x40052004) = 0xD928C520;
289     // Set timeout value
290     *((volatile unsigned int *)0x40052008) = 0xFFFF;
291     // Now disable watchdog via control register
292     volatile unsigned int *WDOG_CS = (unsigned int *) 0x40052000;
293     *WDOG_CS = (*WDOG_CS & ~(1 << 7)) | (1 << 5);
294 #endif // (__USE_CMSIS)
295 
296     //
297     // Copy the data sections from flash to SRAM.
298     //
299     unsigned int LoadAddr, ExeAddr, SectionLen;
300     unsigned int *SectionTableAddr;
301 
302     // Load base address of Global Section Table
303     SectionTableAddr = &__data_section_table;
304 
305     // Copy the data sections from flash to SRAM.
306     while (SectionTableAddr < &__data_section_table_end) {
307         LoadAddr = *SectionTableAddr++;
308         ExeAddr = *SectionTableAddr++;
309         SectionLen = *SectionTableAddr++;
310         data_init(LoadAddr, ExeAddr, SectionLen);
311     }
312 
313     // At this point, SectionTableAddr = &__bss_section_table;
314     // Zero fill the bss segment
315     while (SectionTableAddr < &__bss_section_table_end) {
316         ExeAddr = *SectionTableAddr++;
317         SectionLen = *SectionTableAddr++;
318         bss_init(ExeAddr, SectionLen);
319     }
320 
321 #if !defined (__USE_CMSIS)
322 // Assume that if __USE_CMSIS defined, then CMSIS SystemInit code
323 // will setup the VTOR register
324 
325     // Check to see if we are running the code from a non-zero
326     // address (eg RAM, external flash), in which case we need
327     // to modify the VTOR register to tell the CPU that the
328     // vector table is located at a non-0x0 address.
329     unsigned int * pSCB_VTOR = (unsigned int *) 0xE000ED08;
330     if ((unsigned int *)g_pfnVectors!=(unsigned int *) 0x00000000) {
331         *pSCB_VTOR = (unsigned int)g_pfnVectors;
332     }
333 #endif // (__USE_CMSIS)
334 #if defined (__cplusplus)
335     //
336     // Call C++ library initialisation
337     //
338     __libc_init_array();
339 #endif
340 
341     // Reenable interrupts
342     __asm volatile ("cpsie i");
343 
344 #if defined (__REDLIB__)
345     // Call the Redlib library, which in turn calls main()
346     __main();
347 #else
348     main();
349 #endif
350 
351     //
352     // main() shouldn't return, but if it does, we'll just enter an infinite loop
353     //
354     while (1) {
355         ;
356     }
357 }
358 
359 //*****************************************************************************
360 // Default core exception handlers. Override the ones here by defining your own
361 // handler routines in your application code.
362 //*****************************************************************************
NMI_Handler(void)363 WEAK_AV void NMI_Handler(void)
364 { while(1) {}
365 }
366 
HardFault_Handler(void)367 WEAK_AV void HardFault_Handler(void)
368 { while(1) {}
369 }
370 
SVC_Handler(void)371 WEAK_AV void SVC_Handler(void)
372 { while(1) {}
373 }
374 
PendSV_Handler(void)375 WEAK_AV void PendSV_Handler(void)
376 { while(1) {}
377 }
378 
SysTick_Handler(void)379 WEAK_AV void SysTick_Handler(void)
380 { while(1) {}
381 }
382 
383 //*****************************************************************************
384 // Processor ends up here if an unexpected interrupt occurs or a specific
385 // handler is not present in the application code.
386 //*****************************************************************************
IntDefaultHandler(void)387 WEAK_AV void IntDefaultHandler(void)
388 { while(1) {}
389 }
390 
391 //*****************************************************************************
392 // Default application exception handlers. Override the ones here by defining
393 // your own handler routines in your application code. These routines call
394 // driver exception handlers or IntDefaultHandler() if no driver exception
395 // handler is included.
396 //*****************************************************************************
DMA0_04_IRQHandler(void)397 WEAK_AV void DMA0_04_IRQHandler(void)
398 {   DMA0_04_DriverIRQHandler();
399 }
400 
DMA0_15_IRQHandler(void)401 WEAK_AV void DMA0_15_IRQHandler(void)
402 {   DMA0_15_DriverIRQHandler();
403 }
404 
DMA0_26_IRQHandler(void)405 WEAK_AV void DMA0_26_IRQHandler(void)
406 {   DMA0_26_DriverIRQHandler();
407 }
408 
DMA0_37_IRQHandler(void)409 WEAK_AV void DMA0_37_IRQHandler(void)
410 {   DMA0_37_DriverIRQHandler();
411 }
412 
DMA_Error_IRQHandler(void)413 WEAK_AV void DMA_Error_IRQHandler(void)
414 {   DMA_Error_DriverIRQHandler();
415 }
416 
FTFA_IRQHandler(void)417 WEAK_AV void FTFA_IRQHandler(void)
418 {   FTFA_DriverIRQHandler();
419 }
420 
LVD_LVW_IRQHandler(void)421 WEAK_AV void LVD_LVW_IRQHandler(void)
422 {   LVD_LVW_DriverIRQHandler();
423 }
424 
PORTAE_IRQHandler(void)425 WEAK_AV void PORTAE_IRQHandler(void)
426 {   PORTAE_DriverIRQHandler();
427 }
428 
LPI2C0_IRQHandler(void)429 WEAK_AV void LPI2C0_IRQHandler(void)
430 {   LPI2C0_DriverIRQHandler();
431 }
432 
Reserved25_IRQHandler(void)433 WEAK_AV void Reserved25_IRQHandler(void)
434 {   Reserved25_DriverIRQHandler();
435 }
436 
LPSPI0_IRQHandler(void)437 WEAK_AV void LPSPI0_IRQHandler(void)
438 {   LPSPI0_DriverIRQHandler();
439 }
440 
Reserved27_IRQHandler(void)441 WEAK_AV void Reserved27_IRQHandler(void)
442 {   Reserved27_DriverIRQHandler();
443 }
444 
LPUART0_IRQHandler(void)445 WEAK_AV void LPUART0_IRQHandler(void)
446 {   LPUART0_DriverIRQHandler();
447 }
448 
LPUART1_IRQHandler(void)449 WEAK_AV void LPUART1_IRQHandler(void)
450 {   LPUART1_DriverIRQHandler();
451 }
452 
LPUART2_IRQHandler(void)453 WEAK_AV void LPUART2_IRQHandler(void)
454 {   LPUART2_DriverIRQHandler();
455 }
456 
ADC0_IRQHandler(void)457 WEAK_AV void ADC0_IRQHandler(void)
458 {   ADC0_DriverIRQHandler();
459 }
460 
CMP0_IRQHandler(void)461 WEAK_AV void CMP0_IRQHandler(void)
462 {   CMP0_DriverIRQHandler();
463 }
464 
FTM0_IRQHandler(void)465 WEAK_AV void FTM0_IRQHandler(void)
466 {   FTM0_DriverIRQHandler();
467 }
468 
FTM1_IRQHandler(void)469 WEAK_AV void FTM1_IRQHandler(void)
470 {   FTM1_DriverIRQHandler();
471 }
472 
FTM2_IRQHandler(void)473 WEAK_AV void FTM2_IRQHandler(void)
474 {   FTM2_DriverIRQHandler();
475 }
476 
Reserved36_IRQHandler(void)477 WEAK_AV void Reserved36_IRQHandler(void)
478 {   Reserved36_DriverIRQHandler();
479 }
480 
Reserved37_IRQHandler(void)481 WEAK_AV void Reserved37_IRQHandler(void)
482 {   Reserved37_DriverIRQHandler();
483 }
484 
LPIT0_IRQHandler(void)485 WEAK_AV void LPIT0_IRQHandler(void)
486 {   LPIT0_DriverIRQHandler();
487 }
488 
FLEXIO_IRQHandler(void)489 WEAK_AV void FLEXIO_IRQHandler(void)
490 {   FLEXIO_DriverIRQHandler();
491 }
492 
TSI0_IRQHandler(void)493 WEAK_AV void TSI0_IRQHandler(void)
494 {   TSI0_DriverIRQHandler();
495 }
496 
Reserved41_IRQHandler(void)497 WEAK_AV void Reserved41_IRQHandler(void)
498 {   Reserved41_DriverIRQHandler();
499 }
500 
PORTBCD_IRQHandler(void)501 WEAK_AV void PORTBCD_IRQHandler(void)
502 {   PORTBCD_DriverIRQHandler();
503 }
504 
SCG_IRQHandler(void)505 WEAK_AV void SCG_IRQHandler(void)
506 {   SCG_DriverIRQHandler();
507 }
508 
WDOG_EWM_IRQHandler(void)509 WEAK_AV void WDOG_EWM_IRQHandler(void)
510 {   WDOG_EWM_DriverIRQHandler();
511 }
512 
PWT_LPTMR0_IRQHandler(void)513 WEAK_AV void PWT_LPTMR0_IRQHandler(void)
514 {   PWT_LPTMR0_DriverIRQHandler();
515 }
516 
Reserved46_IRQHandler(void)517 WEAK_AV void Reserved46_IRQHandler(void)
518 {   Reserved46_DriverIRQHandler();
519 }
520 
RCM_IRQHandler(void)521 WEAK_AV void RCM_IRQHandler(void)
522 {   RCM_DriverIRQHandler();
523 }
524 
525 //*****************************************************************************
526 
527 #if defined (DEBUG)
528 #pragma GCC pop_options
529 #endif // (DEBUG)
530