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