1 //*****************************************************************************
2 // K32L2B11A startup code for use with MCUXpresso IDE
3 //
4 // Version : 220823
5 //*****************************************************************************
6 //
7 // Copyright 2016-2023 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, 0xFFFF3FFE};
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_IRQHandler(void);
84 WEAK void DMA1_IRQHandler(void);
85 WEAK void DMA2_IRQHandler(void);
86 WEAK void DMA3_IRQHandler(void);
87 WEAK void Reserved20_IRQHandler(void);
88 WEAK void FTFA_IRQHandler(void);
89 WEAK void PMC_IRQHandler(void);
90 WEAK void LLWU_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 LPUART0_IRQHandler(void);
96 WEAK void LPUART1_IRQHandler(void);
97 WEAK void UART2_FLEXIO_IRQHandler(void);
98 WEAK void ADC0_IRQHandler(void);
99 WEAK void CMP0_IRQHandler(void);
100 WEAK void TPM0_IRQHandler(void);
101 WEAK void TPM1_IRQHandler(void);
102 WEAK void TPM2_IRQHandler(void);
103 WEAK void RTC_IRQHandler(void);
104 WEAK void RTC_Seconds_IRQHandler(void);
105 WEAK void PIT_IRQHandler(void);
106 WEAK void Reserved39_IRQHandler(void);
107 WEAK void USB0_IRQHandler(void);
108 WEAK void DAC0_IRQHandler(void);
109 WEAK void Reserved42_IRQHandler(void);
110 WEAK void Reserved43_IRQHandler(void);
111 WEAK void LPTMR0_IRQHandler(void);
112 WEAK void LCD_IRQHandler(void);
113 WEAK void PORTA_IRQHandler(void);
114 WEAK void PORTC_PORTD_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_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
123 void DMA1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
124 void DMA2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
125 void DMA3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
126 void Reserved20_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
127 void FTFA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
128 void PMC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
129 void LLWU_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 LPUART0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
135 void LPUART1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
136 void UART2_FLEXIO_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
137 void ADC0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
138 void CMP0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
139 void TPM0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
140 void TPM1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
141 void TPM2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
142 void RTC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
143 void RTC_Seconds_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
144 void PIT_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
145 void Reserved39_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
146 void USB0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
147 void DAC0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
148 void Reserved42_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
149 void Reserved43_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
150 void LPTMR0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
151 void LCD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
152 void PORTA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
153 void PORTC_PORTD_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,                       // NMI Handler
187     HardFault_Handler,                 // 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,                    // PendSV Handler
199     SysTick_Handler,                   // SysTick Handler
200 
201     // Chip Level - K32L2B11A
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     Reserved20_IRQHandler,    // 20: Reserved interrupt
207     FTFA_IRQHandler,          // 21: Command complete and read collision
208     PMC_IRQHandler,           // 22: Low-voltage detect, low-voltage warning
209     LLWU_IRQHandler,          // 23: Low leakage wakeup
210     I2C0_IRQHandler,          // 24: I2C0 interrupt
211     I2C1_IRQHandler,          // 25: I2C1 interrupt
212     SPI0_IRQHandler,          // 26: SPI0 single interrupt vector for all sources
213     SPI1_IRQHandler,          // 27: SPI1 single interrupt vector for all sources
214     LPUART0_IRQHandler,       // 28: LPUART0 status and error
215     LPUART1_IRQHandler,       // 29: LPUART1 status and error
216     UART2_FLEXIO_IRQHandler,  // 30: UART2 or FLEXIO
217     ADC0_IRQHandler,          // 31: ADC0 interrupt
218     CMP0_IRQHandler,          // 32: CMP0 interrupt
219     TPM0_IRQHandler,          // 33: TPM0 single interrupt vector for all sources
220     TPM1_IRQHandler,          // 34: TPM1 single interrupt vector for all sources
221     TPM2_IRQHandler,          // 35: TPM2 single interrupt vector for all sources
222     RTC_IRQHandler,           // 36: RTC alarm
223     RTC_Seconds_IRQHandler,   // 37: RTC seconds
224     PIT_IRQHandler,           // 38: PIT interrupt
225     Reserved39_IRQHandler,    // 39: Reserved interrupt
226     USB0_IRQHandler,          // 40: USB0 interrupt
227     DAC0_IRQHandler,          // 41: DAC0 interrupt
228     Reserved42_IRQHandler,    // 42: Reserved interrupt
229     Reserved43_IRQHandler,    // 43: Reserved interrupt
230     LPTMR0_IRQHandler,        // 44: LPTMR0 interrupt
231     LCD_IRQHandler,           // 45: LCD interrupt
232     PORTA_IRQHandler,         // 46: PORTA Pin detect
233     PORTC_PORTD_IRQHandler,   // 47: Single interrupt vector for PORTC; PORTD Pin detect
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 #if (DISABLE_WDOG)
287     // Disable Watchdog
288     // SIM->COPC register: COPT=0,COPCLKS=0,COPW=0
289     *((volatile unsigned int *)0x40048100) = 0x00u;
290 #endif // (DISABLE_WDOG)
291 #endif // (__USE_CMSIS)
292 
293     //
294     // Copy the data sections from flash to SRAM.
295     //
296     unsigned int LoadAddr, ExeAddr, SectionLen;
297     unsigned int *SectionTableAddr;
298 
299     // Load base address of Global Section Table
300     SectionTableAddr = &__data_section_table;
301 
302     // Copy the data sections from flash to SRAM.
303     while (SectionTableAddr < &__data_section_table_end) {
304         LoadAddr = *SectionTableAddr++;
305         ExeAddr = *SectionTableAddr++;
306         SectionLen = *SectionTableAddr++;
307         data_init(LoadAddr, ExeAddr, SectionLen);
308     }
309 
310     // At this point, SectionTableAddr = &__bss_section_table;
311     // Zero fill the bss segment
312     while (SectionTableAddr < &__bss_section_table_end) {
313         ExeAddr = *SectionTableAddr++;
314         SectionLen = *SectionTableAddr++;
315         bss_init(ExeAddr, SectionLen);
316     }
317 
318 #if !defined (__USE_CMSIS)
319 // Assume that if __USE_CMSIS defined, then CMSIS SystemInit code
320 // will setup the VTOR register
321 
322     // Check to see if we are running the code from a non-zero
323     // address (eg RAM, external flash), in which case we need
324     // to modify the VTOR register to tell the CPU that the
325     // vector table is located at a non-0x0 address.
326     unsigned int * pSCB_VTOR = (unsigned int *) 0xE000ED08;
327     if ((unsigned int *)g_pfnVectors!=(unsigned int *) 0x00000000) {
328         *pSCB_VTOR = (unsigned int)g_pfnVectors;
329     }
330 #endif // (__USE_CMSIS)
331 #if defined (__cplusplus)
332     //
333     // Call C++ library initialisation
334     //
335     __libc_init_array();
336 #endif
337 
338     // Reenable interrupts
339     __asm volatile ("cpsie i");
340 
341 #if defined (__REDLIB__)
342     // Call the Redlib library, which in turn calls main()
343     __main();
344 #else
345     main();
346 #endif
347 
348     //
349     // main() shouldn't return, but if it does, we'll just enter an infinite loop
350     //
351     while (1) {
352         ;
353     }
354 }
355 
356 //*****************************************************************************
357 // Default core exception handlers. Override the ones here by defining your own
358 // handler routines in your application code.
359 //*****************************************************************************
NMI_Handler(void)360 WEAK_AV void NMI_Handler(void)
361 { while(1) {}
362 }
363 
HardFault_Handler(void)364 WEAK_AV void HardFault_Handler(void)
365 { while(1) {}
366 }
367 
SVC_Handler(void)368 WEAK_AV void SVC_Handler(void)
369 { while(1) {}
370 }
371 
PendSV_Handler(void)372 WEAK_AV void PendSV_Handler(void)
373 { while(1) {}
374 }
375 
SysTick_Handler(void)376 WEAK_AV void SysTick_Handler(void)
377 { while(1) {}
378 }
379 
380 //*****************************************************************************
381 // Processor ends up here if an unexpected interrupt occurs or a specific
382 // handler is not present in the application code.
383 //*****************************************************************************
IntDefaultHandler(void)384 WEAK_AV void IntDefaultHandler(void)
385 { while(1) {}
386 }
387 
388 //*****************************************************************************
389 // Default application exception handlers. Override the ones here by defining
390 // your own handler routines in your application code. These routines call
391 // driver exception handlers or IntDefaultHandler() if no driver exception
392 // handler is included.
393 //*****************************************************************************
DMA0_IRQHandler(void)394 WEAK_AV void DMA0_IRQHandler(void)
395 {   DMA0_DriverIRQHandler();
396 }
397 
DMA1_IRQHandler(void)398 WEAK_AV void DMA1_IRQHandler(void)
399 {   DMA1_DriverIRQHandler();
400 }
401 
DMA2_IRQHandler(void)402 WEAK_AV void DMA2_IRQHandler(void)
403 {   DMA2_DriverIRQHandler();
404 }
405 
DMA3_IRQHandler(void)406 WEAK_AV void DMA3_IRQHandler(void)
407 {   DMA3_DriverIRQHandler();
408 }
409 
Reserved20_IRQHandler(void)410 WEAK_AV void Reserved20_IRQHandler(void)
411 {   Reserved20_DriverIRQHandler();
412 }
413 
FTFA_IRQHandler(void)414 WEAK_AV void FTFA_IRQHandler(void)
415 {   FTFA_DriverIRQHandler();
416 }
417 
PMC_IRQHandler(void)418 WEAK_AV void PMC_IRQHandler(void)
419 {   PMC_DriverIRQHandler();
420 }
421 
LLWU_IRQHandler(void)422 WEAK_AV void LLWU_IRQHandler(void)
423 {   LLWU_DriverIRQHandler();
424 }
425 
I2C0_IRQHandler(void)426 WEAK_AV void I2C0_IRQHandler(void)
427 {   I2C0_DriverIRQHandler();
428 }
429 
I2C1_IRQHandler(void)430 WEAK_AV void I2C1_IRQHandler(void)
431 {   I2C1_DriverIRQHandler();
432 }
433 
SPI0_IRQHandler(void)434 WEAK_AV void SPI0_IRQHandler(void)
435 {   SPI0_DriverIRQHandler();
436 }
437 
SPI1_IRQHandler(void)438 WEAK_AV void SPI1_IRQHandler(void)
439 {   SPI1_DriverIRQHandler();
440 }
441 
LPUART0_IRQHandler(void)442 WEAK_AV void LPUART0_IRQHandler(void)
443 {   LPUART0_DriverIRQHandler();
444 }
445 
LPUART1_IRQHandler(void)446 WEAK_AV void LPUART1_IRQHandler(void)
447 {   LPUART1_DriverIRQHandler();
448 }
449 
UART2_FLEXIO_IRQHandler(void)450 WEAK_AV void UART2_FLEXIO_IRQHandler(void)
451 {   UART2_FLEXIO_DriverIRQHandler();
452 }
453 
ADC0_IRQHandler(void)454 WEAK_AV void ADC0_IRQHandler(void)
455 {   ADC0_DriverIRQHandler();
456 }
457 
CMP0_IRQHandler(void)458 WEAK_AV void CMP0_IRQHandler(void)
459 {   CMP0_DriverIRQHandler();
460 }
461 
TPM0_IRQHandler(void)462 WEAK_AV void TPM0_IRQHandler(void)
463 {   TPM0_DriverIRQHandler();
464 }
465 
TPM1_IRQHandler(void)466 WEAK_AV void TPM1_IRQHandler(void)
467 {   TPM1_DriverIRQHandler();
468 }
469 
TPM2_IRQHandler(void)470 WEAK_AV void TPM2_IRQHandler(void)
471 {   TPM2_DriverIRQHandler();
472 }
473 
RTC_IRQHandler(void)474 WEAK_AV void RTC_IRQHandler(void)
475 {   RTC_DriverIRQHandler();
476 }
477 
RTC_Seconds_IRQHandler(void)478 WEAK_AV void RTC_Seconds_IRQHandler(void)
479 {   RTC_Seconds_DriverIRQHandler();
480 }
481 
PIT_IRQHandler(void)482 WEAK_AV void PIT_IRQHandler(void)
483 {   PIT_DriverIRQHandler();
484 }
485 
Reserved39_IRQHandler(void)486 WEAK_AV void Reserved39_IRQHandler(void)
487 {   Reserved39_DriverIRQHandler();
488 }
489 
USB0_IRQHandler(void)490 WEAK_AV void USB0_IRQHandler(void)
491 {   USB0_DriverIRQHandler();
492 }
493 
DAC0_IRQHandler(void)494 WEAK_AV void DAC0_IRQHandler(void)
495 {   DAC0_DriverIRQHandler();
496 }
497 
Reserved42_IRQHandler(void)498 WEAK_AV void Reserved42_IRQHandler(void)
499 {   Reserved42_DriverIRQHandler();
500 }
501 
Reserved43_IRQHandler(void)502 WEAK_AV void Reserved43_IRQHandler(void)
503 {   Reserved43_DriverIRQHandler();
504 }
505 
LPTMR0_IRQHandler(void)506 WEAK_AV void LPTMR0_IRQHandler(void)
507 {   LPTMR0_DriverIRQHandler();
508 }
509 
LCD_IRQHandler(void)510 WEAK_AV void LCD_IRQHandler(void)
511 {   LCD_DriverIRQHandler();
512 }
513 
PORTA_IRQHandler(void)514 WEAK_AV void PORTA_IRQHandler(void)
515 {   PORTA_DriverIRQHandler();
516 }
517 
PORTC_PORTD_IRQHandler(void)518 WEAK_AV void PORTC_PORTD_IRQHandler(void)
519 {   PORTC_PORTD_DriverIRQHandler();
520 }
521 
522 //*****************************************************************************
523 
524 #if defined (DEBUG)
525 #pragma GCC pop_options
526 #endif // (DEBUG)
527