1 //*****************************************************************************
2 // MKE06Z4 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, 0xFFFEFFFF};
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 FTMRE_IRQHandler(void);
89 WEAK void PMC_IRQHandler(void);
90 WEAK void IRQ_IRQHandler(void);
91 WEAK void I2C0_IRQHandler(void);
92 WEAK void I2C1_IRQHandler(void);
93 WEAK void SPI0_IRQHandler(void);
94 WEAK void SPI1_IRQHandler(void);
95 WEAK void UART0_IRQHandler(void);
96 WEAK void UART1_IRQHandler(void);
97 WEAK void UART2_IRQHandler(void);
98 WEAK void ADC_IRQHandler(void);
99 WEAK void ACMP0_IRQHandler(void);
100 WEAK void FTM0_IRQHandler(void);
101 WEAK void FTM1_IRQHandler(void);
102 WEAK void FTM2_IRQHandler(void);
103 WEAK void RTC_IRQHandler(void);
104 WEAK void ACMP1_IRQHandler(void);
105 WEAK void PIT_CH0_IRQHandler(void);
106 WEAK void PIT_CH1_IRQHandler(void);
107 WEAK void KBI0_IRQHandler(void);
108 WEAK void KBI1_IRQHandler(void);
109 WEAK void Reserved42_IRQHandler(void);
110 WEAK void ICS_IRQHandler(void);
111 WEAK void WDOG_IRQHandler(void);
112 WEAK void PWT_IRQHandler(void);
113 WEAK void MSCAN_1_IRQHandler(void);
114 WEAK void MSCAN_2_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 Reserved16_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
123 void Reserved17_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
124 void Reserved18_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
125 void Reserved19_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
126 void Reserved20_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
127 void FTMRE_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
128 void PMC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
129 void IRQ_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
130 void I2C0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
131 void I2C1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
132 void SPI0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
133 void SPI1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
134 void UART0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
135 void UART1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
136 void UART2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
137 void ADC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
138 void ACMP0_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 RTC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
143 void ACMP1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
144 void PIT_CH0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
145 void PIT_CH1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
146 void KBI0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
147 void KBI1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
148 void Reserved42_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
149 void ICS_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
150 void WDOG_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
151 void PWT_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
152 void MSCAN_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
153 
154 //*****************************************************************************
155 // The entry point for the application.
156 // __main() is the entry point for Redlib based applications
157 // main() is the entry point for Newlib based applications
158 //*****************************************************************************
159 #if defined (__REDLIB__)
160 extern void __main(void);
161 #endif
162 extern int main(void);
163 
164 //*****************************************************************************
165 // External declaration for the pointer to the stack top from the Linker Script
166 //*****************************************************************************
167 extern void _vStackTop(void);
168 //*****************************************************************************
169 #if defined (__cplusplus)
170 } // extern "C"
171 #endif
172 //*****************************************************************************
173 // The vector table.
174 // This relies on the linker script to place at correct location in memory.
175 //*****************************************************************************
176 
177 
178 
179 extern void (* const g_pfnVectors[])(void);
180 extern void * __Vectors __attribute__ ((alias ("g_pfnVectors")));
181 
182 __attribute__ ((used, section(".isr_vector")))
183 void (* const g_pfnVectors[])(void) = {
184     // Core Level - CM0P
185     &_vStackTop,                       // The initial stack pointer
186     ResetISR,                          // The reset handler
187     NMI_Handler,                       // The NMI handler
188     HardFault_Handler,                 // The hard fault handler
189     0,                                 // Reserved
190     0,                                 // Reserved
191     0,                                 // Reserved
192     0,                                 // Reserved
193     0,                                 // Reserved
194     0,                                 // Reserved
195     0,                                 // Reserved
196     SVC_Handler,                       // SVCall handler
197     0,                                 // Reserved
198     0,                                 // Reserved
199     PendSV_Handler,                    // The PendSV handler
200     SysTick_Handler,                   // The SysTick handler
201 
202     // Chip Level - MKE06Z4
203     Reserved16_IRQHandler,   // 16: Reserved interrupt
204     Reserved17_IRQHandler,   // 17: Reserved interrupt
205     Reserved18_IRQHandler,   // 18: Reserved interrupt
206     Reserved19_IRQHandler,   // 19: Reserved interrupt
207     Reserved20_IRQHandler,   // 20: Reserved interrupt
208     FTMRE_IRQHandler,        // 21: Command complete
209     PMC_IRQHandler,          // 22: Low-voltage warning
210     IRQ_IRQHandler,          // 23: External interrupt
211     I2C0_IRQHandler,         // 24: Single interrupt vector for all sources
212     I2C1_IRQHandler,         // 25: Single interrupt vector for all sources
213     SPI0_IRQHandler,         // 26: Single interrupt vector for all sources
214     SPI1_IRQHandler,         // 27: Single interrupt vector for all sources
215     UART0_IRQHandler,        // 28: Status and error
216     UART1_IRQHandler,        // 29: Status and error
217     UART2_IRQHandler,        // 30: Status and error
218     ADC_IRQHandler,          // 31: ADC conversion complete interrupt
219     ACMP0_IRQHandler,        // 32: Analog comparator 0 interrupt
220     FTM0_IRQHandler,         // 33: FTM0 single interrupt vector for all sources
221     FTM1_IRQHandler,         // 34: FTM1 single interrupt vector for all sources
222     FTM2_IRQHandler,         // 35: FTM2 single interrupt vector for all sources
223     RTC_IRQHandler,          // 36: RTC overflow
224     ACMP1_IRQHandler,        // 37: Analog comparator 1 interrupt
225     PIT_CH0_IRQHandler,      // 38: PIT CH0 overflow
226     PIT_CH1_IRQHandler,      // 39: PIT CH1 overflow
227     KBI0_IRQHandler,         // 40: Keyboard interrupt0
228     KBI1_IRQHandler,         // 41: Keyboard interrupt1
229     Reserved42_IRQHandler,   // 42: Reserved interrupt
230     ICS_IRQHandler,          // 43: Clock loss of lock
231     WDOG_IRQHandler,         // 44: Watchdog timeout
232     PWT_IRQHandler,          // 45: Single interrupt vector for all sources
233     MSCAN_1_IRQHandler,      // 46: MSCAN Rx Interrupt
234     MSCAN_2_IRQHandler,      // 47: MSCAN Tx, Err and Wake-up interrupt
235 
236 
237 }; /* End of g_pfnVectors */
238 
239 //*****************************************************************************
240 // Functions to carry out the initialization of RW and BSS data sections. These
241 // are written as separate functions rather than being inlined within the
242 // ResetISR() function in order to cope with MCUs with multiple banks of
243 // memory.
244 //*****************************************************************************
245 __attribute__ ((section(".after_vectors.init_data")))
data_init(unsigned int romstart,unsigned int start,unsigned int len)246 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
247     unsigned int *pulDest = (unsigned int*) start;
248     unsigned int *pulSrc = (unsigned int*) romstart;
249     unsigned int loop;
250     for (loop = 0; loop < len; loop = loop + 4)
251         *pulDest++ = *pulSrc++;
252 }
253 
254 __attribute__ ((section(".after_vectors.init_bss")))
bss_init(unsigned int start,unsigned int len)255 void bss_init(unsigned int start, unsigned int len) {
256     unsigned int *pulDest = (unsigned int*) start;
257     unsigned int loop;
258     for (loop = 0; loop < len; loop = loop + 4)
259         *pulDest++ = 0;
260 }
261 
262 //*****************************************************************************
263 // The following symbols are constructs generated by the linker, indicating
264 // the location of various points in the "Global Section Table". This table is
265 // created by the linker via the Code Red managed linker script mechanism. It
266 // contains the load address, execution address and length of each RW data
267 // section and the execution and length of each BSS (zero initialized) section.
268 //*****************************************************************************
269 extern unsigned int __data_section_table;
270 extern unsigned int __data_section_table_end;
271 extern unsigned int __bss_section_table;
272 extern unsigned int __bss_section_table_end;
273 
274 //*****************************************************************************
275 // Reset entry point for your code.
276 // Sets up a simple runtime environment and initializes the C/C++
277 // library.
278 //*****************************************************************************
279 __attribute__ ((naked, section(".after_vectors.reset")))
ResetISR(void)280 void ResetISR(void) {
281 
282     // Disable interrupts
283     __asm volatile ("cpsid i");
284 
285 
286 #if defined (__USE_CMSIS)
287 // If __USE_CMSIS defined, then call CMSIS SystemInit code
288     SystemInit();
289 
290 #else
291     // Disable Watchdog
292     // Write watchdog update key to unlock
293     *((volatile unsigned int *)0x40052002) = 0x20;
294     *((volatile unsigned int *)0x40052003) = 0xC5;
295     *((volatile unsigned int *)0x40052002) = 0x28;
296     *((volatile unsigned int *)0x40052003) = 0xD9;
297     // Set timeout value
298     *((volatile unsigned int *)0x40052004) = 0xFF;
299     *((volatile unsigned int *)0x40052005) = 0xFF;
300     // Now disable watchdog via control register
301     volatile unsigned int *WDOG_CS1 = (unsigned int *) 0x40052000;
302     *WDOG_CS1 = (*WDOG_CS1 & ~(1 << 7)) | (1 << 5);
303 #endif // (__USE_CMSIS)
304 
305     //
306     // Copy the data sections from flash to SRAM.
307     //
308     unsigned int LoadAddr, ExeAddr, SectionLen;
309     unsigned int *SectionTableAddr;
310 
311     // Load base address of Global Section Table
312     SectionTableAddr = &__data_section_table;
313 
314     // Copy the data sections from flash to SRAM.
315     while (SectionTableAddr < &__data_section_table_end) {
316         LoadAddr = *SectionTableAddr++;
317         ExeAddr = *SectionTableAddr++;
318         SectionLen = *SectionTableAddr++;
319         data_init(LoadAddr, ExeAddr, SectionLen);
320     }
321 
322     // At this point, SectionTableAddr = &__bss_section_table;
323     // Zero fill the bss segment
324     while (SectionTableAddr < &__bss_section_table_end) {
325         ExeAddr = *SectionTableAddr++;
326         SectionLen = *SectionTableAddr++;
327         bss_init(ExeAddr, SectionLen);
328     }
329 
330 
331 #if !defined (__USE_CMSIS)
332 // Assume that if __USE_CMSIS defined, then CMSIS SystemInit code
333 // will setup the VTOR register
334 
335     // Check to see if we are running the code from a non-zero
336     // address (eg RAM, external flash), in which case we need
337     // to modify the VTOR register to tell the CPU that the
338     // vector table is located at a non-0x0 address.
339     unsigned int * pSCB_VTOR = (unsigned int *) 0xE000ED08;
340     if ((unsigned int *)g_pfnVectors!=(unsigned int *) 0x00000000) {
341         *pSCB_VTOR = (unsigned int)g_pfnVectors;
342     }
343 #endif // (__USE_CMSIS)
344 #if defined (__cplusplus)
345     //
346     // Call C++ library initialisation
347     //
348     __libc_init_array();
349 #endif
350 
351     // Reenable interrupts
352     __asm volatile ("cpsie i");
353 
354 #if defined (__REDLIB__)
355     // Call the Redlib library, which in turn calls main()
356     __main();
357 #else
358     main();
359 #endif
360 
361     //
362     // main() shouldn't return, but if it does, we'll just enter an infinite loop
363     //
364     while (1) {
365         ;
366     }
367 }
368 
369 //*****************************************************************************
370 // Default core exception handlers. Override the ones here by defining your own
371 // handler routines in your application code.
372 //*****************************************************************************
NMI_Handler(void)373 WEAK_AV void NMI_Handler(void)
374 { while(1) {}
375 }
376 
HardFault_Handler(void)377 WEAK_AV void HardFault_Handler(void)
378 { while(1) {}
379 }
380 
SVC_Handler(void)381 WEAK_AV void SVC_Handler(void)
382 { while(1) {}
383 }
384 
PendSV_Handler(void)385 WEAK_AV void PendSV_Handler(void)
386 { while(1) {}
387 }
388 
SysTick_Handler(void)389 WEAK_AV void SysTick_Handler(void)
390 { while(1) {}
391 }
392 
393 //*****************************************************************************
394 // Processor ends up here if an unexpected interrupt occurs or a specific
395 // handler is not present in the application code.
396 //*****************************************************************************
IntDefaultHandler(void)397 WEAK_AV void IntDefaultHandler(void)
398 { while(1) {}
399 }
400 
401 //*****************************************************************************
402 // Default application exception handlers. Override the ones here by defining
403 // your own handler routines in your application code. These routines call
404 // driver exception handlers or IntDefaultHandler() if no driver exception
405 // handler is included.
406 //*****************************************************************************
Reserved16_IRQHandler(void)407 WEAK_AV void Reserved16_IRQHandler(void)
408 {   Reserved16_DriverIRQHandler();
409 }
410 
Reserved17_IRQHandler(void)411 WEAK_AV void Reserved17_IRQHandler(void)
412 {   Reserved17_DriverIRQHandler();
413 }
414 
Reserved18_IRQHandler(void)415 WEAK_AV void Reserved18_IRQHandler(void)
416 {   Reserved18_DriverIRQHandler();
417 }
418 
Reserved19_IRQHandler(void)419 WEAK_AV void Reserved19_IRQHandler(void)
420 {   Reserved19_DriverIRQHandler();
421 }
422 
Reserved20_IRQHandler(void)423 WEAK_AV void Reserved20_IRQHandler(void)
424 {   Reserved20_DriverIRQHandler();
425 }
426 
FTMRE_IRQHandler(void)427 WEAK_AV void FTMRE_IRQHandler(void)
428 {   FTMRE_DriverIRQHandler();
429 }
430 
PMC_IRQHandler(void)431 WEAK_AV void PMC_IRQHandler(void)
432 {   PMC_DriverIRQHandler();
433 }
434 
IRQ_IRQHandler(void)435 WEAK_AV void IRQ_IRQHandler(void)
436 {   IRQ_DriverIRQHandler();
437 }
438 
I2C0_IRQHandler(void)439 WEAK_AV void I2C0_IRQHandler(void)
440 {   I2C0_DriverIRQHandler();
441 }
442 
I2C1_IRQHandler(void)443 WEAK_AV void I2C1_IRQHandler(void)
444 {   I2C1_DriverIRQHandler();
445 }
446 
SPI0_IRQHandler(void)447 WEAK_AV void SPI0_IRQHandler(void)
448 {   SPI0_DriverIRQHandler();
449 }
450 
SPI1_IRQHandler(void)451 WEAK_AV void SPI1_IRQHandler(void)
452 {   SPI1_DriverIRQHandler();
453 }
454 
UART0_IRQHandler(void)455 WEAK_AV void UART0_IRQHandler(void)
456 {   UART0_DriverIRQHandler();
457 }
458 
UART1_IRQHandler(void)459 WEAK_AV void UART1_IRQHandler(void)
460 {   UART1_DriverIRQHandler();
461 }
462 
UART2_IRQHandler(void)463 WEAK_AV void UART2_IRQHandler(void)
464 {   UART2_DriverIRQHandler();
465 }
466 
ADC_IRQHandler(void)467 WEAK_AV void ADC_IRQHandler(void)
468 {   ADC_DriverIRQHandler();
469 }
470 
ACMP0_IRQHandler(void)471 WEAK_AV void ACMP0_IRQHandler(void)
472 {   ACMP0_DriverIRQHandler();
473 }
474 
FTM0_IRQHandler(void)475 WEAK_AV void FTM0_IRQHandler(void)
476 {   FTM0_DriverIRQHandler();
477 }
478 
FTM1_IRQHandler(void)479 WEAK_AV void FTM1_IRQHandler(void)
480 {   FTM1_DriverIRQHandler();
481 }
482 
FTM2_IRQHandler(void)483 WEAK_AV void FTM2_IRQHandler(void)
484 {   FTM2_DriverIRQHandler();
485 }
486 
RTC_IRQHandler(void)487 WEAK_AV void RTC_IRQHandler(void)
488 {   RTC_DriverIRQHandler();
489 }
490 
ACMP1_IRQHandler(void)491 WEAK_AV void ACMP1_IRQHandler(void)
492 {   ACMP1_DriverIRQHandler();
493 }
494 
PIT_CH0_IRQHandler(void)495 WEAK_AV void PIT_CH0_IRQHandler(void)
496 {   PIT_CH0_DriverIRQHandler();
497 }
498 
PIT_CH1_IRQHandler(void)499 WEAK_AV void PIT_CH1_IRQHandler(void)
500 {   PIT_CH1_DriverIRQHandler();
501 }
502 
KBI0_IRQHandler(void)503 WEAK_AV void KBI0_IRQHandler(void)
504 {   KBI0_DriverIRQHandler();
505 }
506 
KBI1_IRQHandler(void)507 WEAK_AV void KBI1_IRQHandler(void)
508 {   KBI1_DriverIRQHandler();
509 }
510 
Reserved42_IRQHandler(void)511 WEAK_AV void Reserved42_IRQHandler(void)
512 {   Reserved42_DriverIRQHandler();
513 }
514 
ICS_IRQHandler(void)515 WEAK_AV void ICS_IRQHandler(void)
516 {   ICS_DriverIRQHandler();
517 }
518 
WDOG_IRQHandler(void)519 WEAK_AV void WDOG_IRQHandler(void)
520 {   WDOG_DriverIRQHandler();
521 }
522 
PWT_IRQHandler(void)523 WEAK_AV void PWT_IRQHandler(void)
524 {   PWT_DriverIRQHandler();
525 }
526 
MSCAN_1_IRQHandler(void)527 WEAK_AV void MSCAN_1_IRQHandler(void)
528 {   MSCAN_DriverIRQHandler();
529 }
530 
MSCAN_2_IRQHandler(void)531 WEAK_AV void MSCAN_2_IRQHandler(void)
532 {   MSCAN_DriverIRQHandler();
533 }
534 
535 //*****************************************************************************
536 
537 #if defined (DEBUG)
538 #pragma GCC pop_options
539 #endif // (DEBUG)
540