1 //*****************************************************************************
2 // MKM34ZA5 startup code for use with MCUXpresso IDE
3 //
4 // Version : 090321
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 //*****************************************************************************
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_IRQHandler(void);
89 WEAK void SPI1_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_IRQHandler(void);
99 WEAK void LCD_IRQHandler(void);
100 WEAK void ADC_IRQHandler(void);
101 WEAK void PTx_IRQHandler(void);
102 WEAK void RNGA_IRQHandler(void);
103 WEAK void UART0_UART1_IRQHandler(void);
104 WEAK void UART2_UART3_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 EWM_IRQHandler(void);
112 WEAK void MCG_IRQHandler(void);
113 WEAK void WDOG_IRQHandler(void);
114 WEAK void LPTMR_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_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
128 void SPI1_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_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
138 void LCD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
139 void ADC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
140 void PTx_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
141 void RNGA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
142 void UART0_UART1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
143 void UART2_UART3_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 EWM_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
151 void MCG_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
152 void WDOG_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
153 void LPTMR_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 
180 
181 extern void (* const g_pfnVectors[])(void);
182 extern void * __Vectors __attribute__ ((alias ("g_pfnVectors")));
183 
184 __attribute__ ((used, section(".isr_vector")))
185 void (* const g_pfnVectors[])(void) = {
186     // Core Level - CM0P
187     &_vStackTop,                       // The initial stack pointer
188     ResetISR,                          // The reset handler
189     NMI_Handler,                       // The NMI handler
190     HardFault_Handler,                 // The hard fault handler
191     0,                                 // Reserved
192     0,                                 // Reserved
193     0,                                 // Reserved
194     0,                                 // Reserved
195     0,                                 // Reserved
196     0,                                 // Reserved
197     0,                                 // Reserved
198     SVC_Handler,                       // SVCall handler
199     0,                                 // Reserved
200     0,                                 // Reserved
201     PendSV_Handler,                    // The PendSV handler
202     SysTick_Handler,                   // The SysTick handler
203 
204     // Chip Level - MKM34ZA5
205     DMA0_IRQHandler,         // 16: DMA channel 0 transfer complete
206     DMA1_IRQHandler,         // 17: DMA channel 1 transfer complete
207     DMA2_IRQHandler,         // 18: DMA channel 2 transfer complete
208     DMA3_IRQHandler,         // 19: DMA channel 3 transfer complete
209     SPI0_IRQHandler,         // 20: SPI0 ORed interrupt
210     SPI1_IRQHandler,         // 21: SPI1 ORed interrupt
211     PMC_IRQHandler,          // 22: Low-voltage detect, low-voltage warning
212     TMR0_IRQHandler,         // 23: Quad Timer Channel 0
213     TMR1_IRQHandler,         // 24: Quad Timer Channel 1
214     TMR2_IRQHandler,         // 25: Quad Timer Channel 2
215     TMR3_IRQHandler,         // 26: Quad Timer Channel 3
216     PIT0_PIT1_IRQHandler,    // 27: PIT0/PIT1 ORed interrupt
217     LLWU_IRQHandler,         // 28: Low Leakage Wakeup
218     FTFA_IRQHandler,         // 29: Command complete and read collision
219     CMP0_CMP1_IRQHandler,    // 30: CMP0/CMP1 ORed interrupt
220     LCD_IRQHandler,          // 31: LCD interrupt
221     ADC_IRQHandler,          // 32: ADC interrupt
222     PTx_IRQHandler,          // 33: Single interrupt vector for GPIOA,GPIOB,GPIOC,GPIOD,GPIOE,GPIOF,GPIOG,GPIOH,GPIOI
223     RNGA_IRQHandler,         // 34: RNGA interrupt
224     UART0_UART1_IRQHandler,  // 35: UART0/UART1 ORed interrupt
225     UART2_UART3_IRQHandler,  // 36: UART2/UART3 ORed interrupt
226     AFE_CH0_IRQHandler,      // 37: AFE Channel 0
227     AFE_CH1_IRQHandler,      // 38: AFE Channel 1
228     AFE_CH2_IRQHandler,      // 39: AFE Channel 2
229     AFE_CH3_IRQHandler,      // 40: AFE Channel 3
230     RTC_IRQHandler,          // 41: IRTC interrupt
231     I2C0_I2C1_IRQHandler,    // 42: I2C0/I2C1 ORed interrupt
232     EWM_IRQHandler,          // 43: External Watchdog Monitor
233     MCG_IRQHandler,          // 44: MCG interrupt
234     WDOG_IRQHandler,         // 45: WDOG ORed interrupt
235     LPTMR_IRQHandler,        // 46: LPTMR interrupt
236     XBAR_IRQHandler,         // 47: XBAR interrupt
237 
238 
239 }; /* End of g_pfnVectors */
240 
241 //*****************************************************************************
242 // Functions to carry out the initialization of RW and BSS data sections. These
243 // are written as separate functions rather than being inlined within the
244 // ResetISR() function in order to cope with MCUs with multiple banks of
245 // memory.
246 //*****************************************************************************
247 __attribute__ ((section(".after_vectors.init_data")))
data_init(unsigned int romstart,unsigned int start,unsigned int len)248 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
249     unsigned int *pulDest = (unsigned int*) start;
250     unsigned int *pulSrc = (unsigned int*) romstart;
251     unsigned int loop;
252     for (loop = 0; loop < len; loop = loop + 4)
253         *pulDest++ = *pulSrc++;
254 }
255 
256 __attribute__ ((section(".after_vectors.init_bss")))
bss_init(unsigned int start,unsigned int len)257 void bss_init(unsigned int start, unsigned int len) {
258     unsigned int *pulDest = (unsigned int*) start;
259     unsigned int loop;
260     for (loop = 0; loop < len; loop = loop + 4)
261         *pulDest++ = 0;
262 }
263 
264 //*****************************************************************************
265 // The following symbols are constructs generated by the linker, indicating
266 // the location of various points in the "Global Section Table". This table is
267 // created by the linker via the Code Red managed linker script mechanism. It
268 // contains the load address, execution address and length of each RW data
269 // section and the execution and length of each BSS (zero initialized) section.
270 //*****************************************************************************
271 extern unsigned int __data_section_table;
272 extern unsigned int __data_section_table_end;
273 extern unsigned int __bss_section_table;
274 extern unsigned int __bss_section_table_end;
275 
276 //*****************************************************************************
277 // Reset entry point for your code.
278 // Sets up a simple runtime environment and initializes the C/C++
279 // library.
280 //*****************************************************************************
281 __attribute__ ((naked, section(".after_vectors.reset")))
ResetISR(void)282 void ResetISR(void) {
283 
284 
285     // Disable interrupts
286     __asm volatile ("cpsid i");
287 
288 
289 
290 
291 #if defined (__USE_CMSIS)
292 // If __USE_CMSIS defined, then call CMSIS SystemInit code
293     SystemInit();
294 
295 #else
296     // Disable Watchdog
297     //  Write 0xC520 to watchdog unlock register
298     *((volatile unsigned short *)0x4005300E) = 0xC520;
299     //  Followed by 0xD928 to complete the unlock
300     *((volatile unsigned short *)0x4005300E) = 0xD928;
301     // Now disable watchdog via STCTRLH register
302     *((volatile unsigned short *)0x40053000) = 0x01D2u;
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 //*****************************************************************************
DMA0_IRQHandler(void)407 WEAK_AV void DMA0_IRQHandler(void)
408 {   DMA0_DriverIRQHandler();
409 }
410 
DMA1_IRQHandler(void)411 WEAK_AV void DMA1_IRQHandler(void)
412 {   DMA1_DriverIRQHandler();
413 }
414 
DMA2_IRQHandler(void)415 WEAK_AV void DMA2_IRQHandler(void)
416 {   DMA2_DriverIRQHandler();
417 }
418 
DMA3_IRQHandler(void)419 WEAK_AV void DMA3_IRQHandler(void)
420 {   DMA3_DriverIRQHandler();
421 }
422 
SPI0_IRQHandler(void)423 WEAK_AV void SPI0_IRQHandler(void)
424 {   SPI0_DriverIRQHandler();
425 }
426 
SPI1_IRQHandler(void)427 WEAK_AV void SPI1_IRQHandler(void)
428 {   SPI1_DriverIRQHandler();
429 }
430 
PMC_IRQHandler(void)431 WEAK_AV void PMC_IRQHandler(void)
432 {   PMC_DriverIRQHandler();
433 }
434 
TMR0_IRQHandler(void)435 WEAK_AV void TMR0_IRQHandler(void)
436 {   TMR0_DriverIRQHandler();
437 }
438 
TMR1_IRQHandler(void)439 WEAK_AV void TMR1_IRQHandler(void)
440 {   TMR1_DriverIRQHandler();
441 }
442 
TMR2_IRQHandler(void)443 WEAK_AV void TMR2_IRQHandler(void)
444 {   TMR2_DriverIRQHandler();
445 }
446 
TMR3_IRQHandler(void)447 WEAK_AV void TMR3_IRQHandler(void)
448 {   TMR3_DriverIRQHandler();
449 }
450 
PIT0_PIT1_IRQHandler(void)451 WEAK_AV void PIT0_PIT1_IRQHandler(void)
452 {   PIT0_PIT1_DriverIRQHandler();
453 }
454 
LLWU_IRQHandler(void)455 WEAK_AV void LLWU_IRQHandler(void)
456 {   LLWU_DriverIRQHandler();
457 }
458 
FTFA_IRQHandler(void)459 WEAK_AV void FTFA_IRQHandler(void)
460 {   FTFA_DriverIRQHandler();
461 }
462 
CMP0_CMP1_IRQHandler(void)463 WEAK_AV void CMP0_CMP1_IRQHandler(void)
464 {   CMP0_CMP1_DriverIRQHandler();
465 }
466 
LCD_IRQHandler(void)467 WEAK_AV void LCD_IRQHandler(void)
468 {   LCD_DriverIRQHandler();
469 }
470 
ADC_IRQHandler(void)471 WEAK_AV void ADC_IRQHandler(void)
472 {   ADC_DriverIRQHandler();
473 }
474 
PTx_IRQHandler(void)475 WEAK_AV void PTx_IRQHandler(void)
476 {   PTx_DriverIRQHandler();
477 }
478 
RNGA_IRQHandler(void)479 WEAK_AV void RNGA_IRQHandler(void)
480 {   RNGA_DriverIRQHandler();
481 }
482 
UART0_UART1_IRQHandler(void)483 WEAK_AV void UART0_UART1_IRQHandler(void)
484 {   UART0_UART1_DriverIRQHandler();
485 }
486 
UART2_UART3_IRQHandler(void)487 WEAK_AV void UART2_UART3_IRQHandler(void)
488 {   UART2_UART3_DriverIRQHandler();
489 }
490 
AFE_CH0_IRQHandler(void)491 WEAK_AV void AFE_CH0_IRQHandler(void)
492 {   AFE_CH0_DriverIRQHandler();
493 }
494 
AFE_CH1_IRQHandler(void)495 WEAK_AV void AFE_CH1_IRQHandler(void)
496 {   AFE_CH1_DriverIRQHandler();
497 }
498 
AFE_CH2_IRQHandler(void)499 WEAK_AV void AFE_CH2_IRQHandler(void)
500 {   AFE_CH2_DriverIRQHandler();
501 }
502 
AFE_CH3_IRQHandler(void)503 WEAK_AV void AFE_CH3_IRQHandler(void)
504 {   AFE_CH3_DriverIRQHandler();
505 }
506 
RTC_IRQHandler(void)507 WEAK_AV void RTC_IRQHandler(void)
508 {   RTC_DriverIRQHandler();
509 }
510 
I2C0_I2C1_IRQHandler(void)511 WEAK_AV void I2C0_I2C1_IRQHandler(void)
512 {   I2C0_I2C1_DriverIRQHandler();
513 }
514 
EWM_IRQHandler(void)515 WEAK_AV void EWM_IRQHandler(void)
516 {   EWM_DriverIRQHandler();
517 }
518 
MCG_IRQHandler(void)519 WEAK_AV void MCG_IRQHandler(void)
520 {   MCG_DriverIRQHandler();
521 }
522 
WDOG_IRQHandler(void)523 WEAK_AV void WDOG_IRQHandler(void)
524 {   WDOG_DriverIRQHandler();
525 }
526 
LPTMR_IRQHandler(void)527 WEAK_AV void LPTMR_IRQHandler(void)
528 {   LPTMR_DriverIRQHandler();
529 }
530 
XBAR_IRQHandler(void)531 WEAK_AV void XBAR_IRQHandler(void)
532 {   XBAR_DriverIRQHandler();
533 }
534 
535 //*****************************************************************************
536 
537 #if defined (DEBUG)
538 #pragma GCC pop_options
539 #endif // (DEBUG)
540