1 //*****************************************************************************
2 // MKM34Z7 startup code for use with MCUXpresso IDE
3 //
4 // Version : 251120
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 {
50     unsigned int word1;
51     unsigned int word2;
52     unsigned int word3;
53     unsigned int word4;
54 } Flash_Config = {0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFF, 0xFFFFFFFE};
55 //*****************************************************************************
56 // Declaration of external SystemInit function
57 //*****************************************************************************
58 #if defined(__USE_CMSIS)
59 extern void SystemInit(void);
60 #endif // (__USE_CMSIS)
61 
62 //*****************************************************************************
63 // Forward declaration of the core exception handlers.
64 // When the application defines a handler (with the same name), this will
65 // automatically take precedence over these weak definitions.
66 // If your application is a C++ one, then any interrupt handlers defined
67 // in C++ files within in your main application will need to have C linkage
68 // rather than C++ linkage. To do this, make sure that you are using extern "C"
69 // { .... } around the interrupt handler within your main application code.
70 //*****************************************************************************
71 void ResetISR(void);
72 WEAK void NMI_Handler(void);
73 WEAK void HardFault_Handler(void);
74 WEAK void SVC_Handler(void);
75 WEAK void PendSV_Handler(void);
76 WEAK void SysTick_Handler(void);
77 WEAK void IntDefaultHandler(void);
78 
79 //*****************************************************************************
80 // Forward declaration of the application IRQ handlers. When the application
81 // defines a handler (with the same name), this will automatically take
82 // precedence over weak definitions below
83 //*****************************************************************************
84 WEAK void DMA0_IRQHandler(void);
85 WEAK void DMA1_IRQHandler(void);
86 WEAK void DMA2_IRQHandler(void);
87 WEAK void DMA3_IRQHandler(void);
88 WEAK void SPI0_SPI1_IRQHandler(void);
89 WEAK void PDB0_IRQHandler(void);
90 WEAK void PMC_IRQHandler(void);
91 WEAK void TMR0_IRQHandler(void);
92 WEAK void TMR1_IRQHandler(void);
93 WEAK void TMR2_IRQHandler(void);
94 WEAK void TMR3_IRQHandler(void);
95 WEAK void PIT0_PIT1_IRQHandler(void);
96 WEAK void LLWU_IRQHandler(void);
97 WEAK void FTFA_IRQHandler(void);
98 WEAK void CMP0_CMP1_CMP2_IRQHandler(void);
99 WEAK void LCD_IRQHandler(void);
100 WEAK void ADC0_IRQHandler(void);
101 WEAK void PTx_IRQHandler(void);
102 WEAK void RNGA_IRQHandler(void);
103 WEAK void UART0_UART1_UART2_UART3_IRQHandler(void);
104 WEAK void MMAU_IRQHandler(void);
105 WEAK void AFE_CH0_IRQHandler(void);
106 WEAK void AFE_CH1_IRQHandler(void);
107 WEAK void AFE_CH2_IRQHandler(void);
108 WEAK void AFE_CH3_IRQHandler(void);
109 WEAK void RTC_IRQHandler(void);
110 WEAK void I2C0_I2C1_IRQHandler(void);
111 WEAK void LPUART0_IRQHandler(void);
112 WEAK void MCG_IRQHandler(void);
113 WEAK void WDOG_EWM_IRQHandler(void);
114 WEAK void LPTMR0_IRQHandler(void);
115 WEAK void XBAR_IRQHandler(void);
116 
117 //*****************************************************************************
118 // Forward declaration of the driver IRQ handlers. These are aliased
119 // to the IntDefaultHandler, which is a 'forever' loop. When the driver
120 // defines a handler (with the same name), this will automatically take
121 // precedence over these weak definitions
122 //*****************************************************************************
123 void DMA0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
124 void DMA1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
125 void DMA2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
126 void DMA3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
127 void SPI0_SPI1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
128 void PDB0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
129 void PMC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
130 void TMR0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
131 void TMR1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
132 void TMR2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
133 void TMR3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
134 void PIT0_PIT1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
135 void LLWU_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
136 void FTFA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
137 void CMP0_CMP1_CMP2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
138 void LCD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
139 void ADC0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
140 void PTx_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
141 void RNGA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
142 void UART0_UART1_UART2_UART3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
143 void MMAU_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
144 void AFE_CH0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
145 void AFE_CH1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
146 void AFE_CH2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
147 void AFE_CH3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
148 void RTC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
149 void I2C0_I2C1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
150 void LPUART0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
151 void MCG_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
152 void WDOG_EWM_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
153 void LPTMR0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
154 void XBAR_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
155 
156 //*****************************************************************************
157 // The entry point for the application.
158 // __main() is the entry point for Redlib based applications
159 // main() is the entry point for Newlib based applications
160 //*****************************************************************************
161 #if defined(__REDLIB__)
162 extern void __main(void);
163 #endif
164 extern int main(void);
165 
166 //*****************************************************************************
167 // External declaration for the pointer to the stack top from the Linker Script
168 //*****************************************************************************
169 extern void _vStackTop(void);
170 //*****************************************************************************
171 #if defined(__cplusplus)
172 } // extern "C"
173 #endif
174 //*****************************************************************************
175 // The vector table.
176 // This relies on the linker script to place at correct location in memory.
177 //*****************************************************************************
178 
179 extern void (*const g_pfnVectors[])(void);
180 extern void *__Vectors __attribute__((alias("g_pfnVectors")));
181 
182 __attribute__((used, section(".isr_vector"))) 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 - MKM34Z7
202     DMA0_IRQHandler,                    // 16: DMA channel 0 transfer complete
203     DMA1_IRQHandler,                    // 17: DMA channel 1 transfer complete
204     DMA2_IRQHandler,                    // 18: DMA channel 2 transfer complete
205     DMA3_IRQHandler,                    // 19: DMA channel 3 transfer complete
206     SPI0_SPI1_IRQHandler,               // 20: SPI0/SPI1 ORed interrupt
207     PDB0_IRQHandler,                    // 21: PDB0 ORed interrupt
208     PMC_IRQHandler,                     // 22: Low-voltage detect, low-voltage warning
209     TMR0_IRQHandler,                    // 23: Quad Timer Channel 0
210     TMR1_IRQHandler,                    // 24: Quad Timer Channel 1
211     TMR2_IRQHandler,                    // 25: Quad Timer Channel 2
212     TMR3_IRQHandler,                    // 26: Quad Timer Channel 3
213     PIT0_PIT1_IRQHandler,               // 27: PIT0/PIT1 ORed interrupt
214     LLWU_IRQHandler,                    // 28: Low Leakage Wakeup
215     FTFA_IRQHandler,                    // 29: Command complete and read collision
216     CMP0_CMP1_CMP2_IRQHandler,          // 30: CMP0/CMP1/CMP2 ORed interrupt
217     LCD_IRQHandler,                     // 31: LCD interrupt
218     ADC0_IRQHandler,                    // 32: ADC0 interrupt
219     PTx_IRQHandler,                     // 33: Single interrupt vector for
220                                         // GPIOA,GPIOB,GPIOC,GPIOD,GPIOE,GPIOF,GPIOG,GPIOH,GPIOI,GPIOJ,GPIOK,GPIOL,GPIOM
221     RNGA_IRQHandler,                    // 34: RNGA interrupt
222     UART0_UART1_UART2_UART3_IRQHandler, // 35: UART0/UART1/UART2/UART3 ORed interrupt
223     MMAU_IRQHandler,                    // 36: Memory Mapped Arithmetic Unit interrupt
224     AFE_CH0_IRQHandler,                 // 37: AFE Channel 0
225     AFE_CH1_IRQHandler,                 // 38: AFE Channel 1
226     AFE_CH2_IRQHandler,                 // 39: AFE Channel 2
227     AFE_CH3_IRQHandler,                 // 40: AFE Channel 3
228     RTC_IRQHandler,                     // 41: IRTC interrupt
229     I2C0_I2C1_IRQHandler,               // 42: I2C0/I2C1 ORed interrupt
230     LPUART0_IRQHandler,                 // 43: LPUART0 status and error interrupt
231     MCG_IRQHandler,                     // 44: MCG interrupt
232     WDOG_EWM_IRQHandler,                // 45: WDOG/EWM ORed interrupt
233     LPTMR0_IRQHandler,                  // 46: LPTMR0 interrupt
234     XBAR_IRQHandler,                    // 47: XBAR interrupt
235 
236 }; /* End of g_pfnVectors */
237 
238 //*****************************************************************************
239 // Functions to carry out the initialization of RW and BSS data sections. These
240 // are written as separate functions rather than being inlined within the
241 // ResetISR() function in order to cope with MCUs with multiple banks of
242 // memory.
243 //*****************************************************************************
data_init(unsigned int romstart,unsigned int start,unsigned int len)244 __attribute__((section(".after_vectors.init_data"))) void data_init(unsigned int romstart,
245                                                                     unsigned int start,
246                                                                     unsigned int len)
247 {
248     unsigned int *pulDest = (unsigned int *)start;
249     unsigned int *pulSrc  = (unsigned int *)romstart;
250     unsigned int loop;
251     for (loop = 0; loop < len; loop = loop + 4)
252         *pulDest++ = *pulSrc++;
253 }
254 
bss_init(unsigned int start,unsigned int len)255 __attribute__((section(".after_vectors.init_bss"))) void bss_init(unsigned int start, unsigned int len)
256 {
257     unsigned int *pulDest = (unsigned int *)start;
258     unsigned int loop;
259     for (loop = 0; loop < len; loop = loop + 4)
260         *pulDest++ = 0;
261 }
262 
263 //*****************************************************************************
264 // The following symbols are constructs generated by the linker, indicating
265 // the location of various points in the "Global Section Table". This table is
266 // created by the linker via the Code Red managed linker script mechanism. It
267 // contains the load address, execution address and length of each RW data
268 // section and the execution and length of each BSS (zero initialized) section.
269 //*****************************************************************************
270 extern unsigned int __data_section_table;
271 extern unsigned int __data_section_table_end;
272 extern unsigned int __bss_section_table;
273 extern unsigned int __bss_section_table_end;
274 
275 //*****************************************************************************
276 // Reset entry point for your code.
277 // Sets up a simple runtime environment and initializes the C/C++
278 // library.
279 //*****************************************************************************
ResetISR(void)280 __attribute__((section(".after_vectors.reset"))) void ResetISR(void)
281 {
282     // Disable interrupts
283     __asm volatile("cpsid i");
284 
285 #if defined(__USE_CMSIS)
286     // If __USE_CMSIS defined, then call CMSIS SystemInit code
287     SystemInit();
288 
289 #else
290     // Disable Watchdog
291     //  Write 0xC520 to watchdog unlock register
292     *((volatile unsigned short *)0x4005300E) = 0xC520;
293     //  Followed by 0xD928 to complete the unlock
294     *((volatile unsigned short *)0x4005300E) = 0xD928;
295     // Now disable watchdog via STCTRLH register
296     *((volatile unsigned short *)0x40053000) = 0x01D2u;
297 #endif // (__USE_CMSIS)
298 
299     //
300     // Copy the data sections from flash to SRAM.
301     //
302     unsigned int LoadAddr, ExeAddr, SectionLen;
303     unsigned int *SectionTableAddr;
304 
305     // Load base address of Global Section Table
306     SectionTableAddr = &__data_section_table;
307 
308     // Copy the data sections from flash to SRAM.
309     while (SectionTableAddr < &__data_section_table_end)
310     {
311         LoadAddr   = *SectionTableAddr++;
312         ExeAddr    = *SectionTableAddr++;
313         SectionLen = *SectionTableAddr++;
314         data_init(LoadAddr, ExeAddr, SectionLen);
315     }
316 
317     // At this point, SectionTableAddr = &__bss_section_table;
318     // Zero fill the bss segment
319     while (SectionTableAddr < &__bss_section_table_end)
320     {
321         ExeAddr    = *SectionTableAddr++;
322         SectionLen = *SectionTableAddr++;
323         bss_init(ExeAddr, SectionLen);
324     }
325 
326 #if !defined(__USE_CMSIS)
327     // Assume that if __USE_CMSIS defined, then CMSIS SystemInit code
328     // will setup the VTOR register
329 
330     // Check to see if we are running the code from a non-zero
331     // address (eg RAM, external flash), in which case we need
332     // to modify the VTOR register to tell the CPU that the
333     // vector table is located at a non-0x0 address.
334     unsigned int *pSCB_VTOR = (unsigned int *)0xE000ED08;
335     if ((unsigned int *)g_pfnVectors != (unsigned int *)0x00000000)
336     {
337         *pSCB_VTOR = (unsigned int)g_pfnVectors;
338     }
339 #endif // (__USE_CMSIS)
340 #if defined(__cplusplus)
341     //
342     // Call C++ library initialisation
343     //
344     __libc_init_array();
345 #endif
346 
347     // Reenable interrupts
348     __asm volatile("cpsie i");
349 
350 #if defined(__REDLIB__)
351     // Call the Redlib library, which in turn calls main()
352     __main();
353 #else
354     main();
355 #endif
356 
357     //
358     // main() shouldn't return, but if it does, we'll just enter an infinite loop
359     //
360     while (1)
361     {
362         ;
363     }
364 }
365 
366 //*****************************************************************************
367 // Default core exception handlers. Override the ones here by defining your own
368 // handler routines in your application code.
369 //*****************************************************************************
NMI_Handler(void)370 WEAK_AV void NMI_Handler(void)
371 {
372     while (1)
373     {
374     }
375 }
376 
HardFault_Handler(void)377 WEAK_AV void HardFault_Handler(void)
378 {
379     while (1)
380     {
381     }
382 }
383 
SVC_Handler(void)384 WEAK_AV void SVC_Handler(void)
385 {
386     while (1)
387     {
388     }
389 }
390 
PendSV_Handler(void)391 WEAK_AV void PendSV_Handler(void)
392 {
393     while (1)
394     {
395     }
396 }
397 
SysTick_Handler(void)398 WEAK_AV void SysTick_Handler(void)
399 {
400     while (1)
401     {
402     }
403 }
404 
405 //*****************************************************************************
406 // Processor ends up here if an unexpected interrupt occurs or a specific
407 // handler is not present in the application code.
408 //*****************************************************************************
IntDefaultHandler(void)409 WEAK_AV void IntDefaultHandler(void)
410 {
411     while (1)
412     {
413     }
414 }
415 
416 //*****************************************************************************
417 // Default application exception handlers. Override the ones here by defining
418 // your own handler routines in your application code. These routines call
419 // driver exception handlers or IntDefaultHandler() if no driver exception
420 // handler is included.
421 //*****************************************************************************
DMA0_IRQHandler(void)422 WEAK_AV void DMA0_IRQHandler(void)
423 {
424     DMA0_DriverIRQHandler();
425 }
426 
DMA1_IRQHandler(void)427 WEAK_AV void DMA1_IRQHandler(void)
428 {
429     DMA1_DriverIRQHandler();
430 }
431 
DMA2_IRQHandler(void)432 WEAK_AV void DMA2_IRQHandler(void)
433 {
434     DMA2_DriverIRQHandler();
435 }
436 
DMA3_IRQHandler(void)437 WEAK_AV void DMA3_IRQHandler(void)
438 {
439     DMA3_DriverIRQHandler();
440 }
441 
SPI0_SPI1_IRQHandler(void)442 WEAK_AV void SPI0_SPI1_IRQHandler(void)
443 {
444     SPI0_SPI1_DriverIRQHandler();
445 }
446 
PDB0_IRQHandler(void)447 WEAK_AV void PDB0_IRQHandler(void)
448 {
449     PDB0_DriverIRQHandler();
450 }
451 
PMC_IRQHandler(void)452 WEAK_AV void PMC_IRQHandler(void)
453 {
454     PMC_DriverIRQHandler();
455 }
456 
TMR0_IRQHandler(void)457 WEAK_AV void TMR0_IRQHandler(void)
458 {
459     TMR0_DriverIRQHandler();
460 }
461 
TMR1_IRQHandler(void)462 WEAK_AV void TMR1_IRQHandler(void)
463 {
464     TMR1_DriverIRQHandler();
465 }
466 
TMR2_IRQHandler(void)467 WEAK_AV void TMR2_IRQHandler(void)
468 {
469     TMR2_DriverIRQHandler();
470 }
471 
TMR3_IRQHandler(void)472 WEAK_AV void TMR3_IRQHandler(void)
473 {
474     TMR3_DriverIRQHandler();
475 }
476 
PIT0_PIT1_IRQHandler(void)477 WEAK_AV void PIT0_PIT1_IRQHandler(void)
478 {
479     PIT0_PIT1_DriverIRQHandler();
480 }
481 
LLWU_IRQHandler(void)482 WEAK_AV void LLWU_IRQHandler(void)
483 {
484     LLWU_DriverIRQHandler();
485 }
486 
FTFA_IRQHandler(void)487 WEAK_AV void FTFA_IRQHandler(void)
488 {
489     FTFA_DriverIRQHandler();
490 }
491 
CMP0_CMP1_CMP2_IRQHandler(void)492 WEAK_AV void CMP0_CMP1_CMP2_IRQHandler(void)
493 {
494     CMP0_CMP1_CMP2_DriverIRQHandler();
495 }
496 
LCD_IRQHandler(void)497 WEAK_AV void LCD_IRQHandler(void)
498 {
499     LCD_DriverIRQHandler();
500 }
501 
ADC0_IRQHandler(void)502 WEAK_AV void ADC0_IRQHandler(void)
503 {
504     ADC0_DriverIRQHandler();
505 }
506 
PTx_IRQHandler(void)507 WEAK_AV void PTx_IRQHandler(void)
508 {
509     PTx_DriverIRQHandler();
510 }
511 
RNGA_IRQHandler(void)512 WEAK_AV void RNGA_IRQHandler(void)
513 {
514     RNGA_DriverIRQHandler();
515 }
516 
UART0_UART1_UART2_UART3_IRQHandler(void)517 WEAK_AV void UART0_UART1_UART2_UART3_IRQHandler(void)
518 {
519     UART0_UART1_UART2_UART3_DriverIRQHandler();
520 }
521 
MMAU_IRQHandler(void)522 WEAK_AV void MMAU_IRQHandler(void)
523 {
524     MMAU_DriverIRQHandler();
525 }
526 
AFE_CH0_IRQHandler(void)527 WEAK_AV void AFE_CH0_IRQHandler(void)
528 {
529     AFE_CH0_DriverIRQHandler();
530 }
531 
AFE_CH1_IRQHandler(void)532 WEAK_AV void AFE_CH1_IRQHandler(void)
533 {
534     AFE_CH1_DriverIRQHandler();
535 }
536 
AFE_CH2_IRQHandler(void)537 WEAK_AV void AFE_CH2_IRQHandler(void)
538 {
539     AFE_CH2_DriverIRQHandler();
540 }
541 
AFE_CH3_IRQHandler(void)542 WEAK_AV void AFE_CH3_IRQHandler(void)
543 {
544     AFE_CH3_DriverIRQHandler();
545 }
546 
RTC_IRQHandler(void)547 WEAK_AV void RTC_IRQHandler(void)
548 {
549     RTC_DriverIRQHandler();
550 }
551 
I2C0_I2C1_IRQHandler(void)552 WEAK_AV void I2C0_I2C1_IRQHandler(void)
553 {
554     I2C0_I2C1_DriverIRQHandler();
555 }
556 
LPUART0_IRQHandler(void)557 WEAK_AV void LPUART0_IRQHandler(void)
558 {
559     LPUART0_DriverIRQHandler();
560 }
561 
MCG_IRQHandler(void)562 WEAK_AV void MCG_IRQHandler(void)
563 {
564     MCG_DriverIRQHandler();
565 }
566 
WDOG_EWM_IRQHandler(void)567 WEAK_AV void WDOG_EWM_IRQHandler(void)
568 {
569     WDOG_EWM_DriverIRQHandler();
570 }
571 
LPTMR0_IRQHandler(void)572 WEAK_AV void LPTMR0_IRQHandler(void)
573 {
574     LPTMR0_DriverIRQHandler();
575 }
576 
XBAR_IRQHandler(void)577 WEAK_AV void XBAR_IRQHandler(void)
578 {
579     XBAR_DriverIRQHandler();
580 }
581 
582 //*****************************************************************************
583 
584 #if defined(DEBUG)
585 #pragma GCC pop_options
586 #endif // (DEBUG)
587