1 //*****************************************************************************
2 // LPC51U68 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 // Variable to store CRP value in. Will be placed automatically
44 // by the linker when "Enable Code Read Protect" selected.
45 // See crp.h header for more information
46 //*****************************************************************************
47 #include <NXP/crp.h>
48 __CRP const unsigned int CRP_WORD = CRP_NO_CRP ;
49 
50 //*****************************************************************************
51 // Declaration of external SystemInit function
52 //*****************************************************************************
53 #if defined (__USE_CMSIS)
54 extern void SystemInit(void);
55 #endif // (__USE_CMSIS)
56 
57 //*****************************************************************************
58 // Forward declaration of the core exception handlers.
59 // When the application defines a handler (with the same name), this will
60 // automatically take precedence over these weak definitions.
61 // If your application is a C++ one, then any interrupt handlers defined
62 // in C++ files within in your main application will need to have C linkage
63 // rather than C++ linkage. To do this, make sure that you are using extern "C"
64 // { .... } around the interrupt handler within your main application code.
65 //*****************************************************************************
66      void ResetISR(void);
67 WEAK void NMI_Handler(void);
68 WEAK void HardFault_Handler(void);
69 WEAK void SVC_Handler(void);
70 WEAK void PendSV_Handler(void);
71 WEAK void SysTick_Handler(void);
72 WEAK void IntDefaultHandler(void);
73 
74 //*****************************************************************************
75 // Forward declaration of the application IRQ handlers. When the application
76 // defines a handler (with the same name), this will automatically take
77 // precedence over weak definitions below
78 //*****************************************************************************
79 WEAK void WDT_BOD_IRQHandler(void);
80 WEAK void DMA0_IRQHandler(void);
81 WEAK void GINT0_IRQHandler(void);
82 WEAK void GINT1_IRQHandler(void);
83 WEAK void PIN_INT0_IRQHandler(void);
84 WEAK void PIN_INT1_IRQHandler(void);
85 WEAK void PIN_INT2_IRQHandler(void);
86 WEAK void PIN_INT3_IRQHandler(void);
87 WEAK void UTICK0_IRQHandler(void);
88 WEAK void MRT0_IRQHandler(void);
89 WEAK void CTIMER0_IRQHandler(void);
90 WEAK void CTIMER1_IRQHandler(void);
91 WEAK void SCT0_IRQHandler(void);
92 WEAK void CTIMER3_IRQHandler(void);
93 WEAK void FLEXCOMM0_IRQHandler(void);
94 WEAK void FLEXCOMM1_IRQHandler(void);
95 WEAK void FLEXCOMM2_IRQHandler(void);
96 WEAK void FLEXCOMM3_IRQHandler(void);
97 WEAK void FLEXCOMM4_IRQHandler(void);
98 WEAK void FLEXCOMM5_IRQHandler(void);
99 WEAK void FLEXCOMM6_IRQHandler(void);
100 WEAK void FLEXCOMM7_IRQHandler(void);
101 WEAK void ADC0_SEQA_IRQHandler(void);
102 WEAK void ADC0_SEQB_IRQHandler(void);
103 WEAK void ADC0_THCMP_IRQHandler(void);
104 WEAK void Reserved41_IRQHandler(void);
105 WEAK void Reserved42_IRQHandler(void);
106 WEAK void USB0_NEEDCLK_IRQHandler(void);
107 WEAK void USB0_IRQHandler(void);
108 WEAK void RTC_IRQHandler(void);
109 
110 //*****************************************************************************
111 // Forward declaration of the driver IRQ handlers. These are aliased
112 // to the IntDefaultHandler, which is a 'forever' loop. When the driver
113 // defines a handler (with the same name), this will automatically take
114 // precedence over these weak definitions
115 //*****************************************************************************
116 void WDT_BOD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
117 void DMA0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
118 void GINT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
119 void GINT1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
120 void PIN_INT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
121 void PIN_INT1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
122 void PIN_INT2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
123 void PIN_INT3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
124 void UTICK0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
125 void MRT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
126 void CTIMER0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
127 void CTIMER1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
128 void SCT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
129 void CTIMER3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
130 void FLEXCOMM0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
131 void FLEXCOMM1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
132 void FLEXCOMM2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
133 void FLEXCOMM3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
134 void FLEXCOMM4_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
135 void FLEXCOMM5_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
136 void FLEXCOMM6_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
137 void FLEXCOMM7_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
138 void ADC0_SEQA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
139 void ADC0_SEQB_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
140 void ADC0_THCMP_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
141 void Reserved41_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
142 void Reserved42_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
143 void USB0_NEEDCLK_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
144 void USB0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
145 void RTC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
146 
147 //*****************************************************************************
148 // The entry point for the application.
149 // __main() is the entry point for Redlib based applications
150 // main() is the entry point for Newlib based applications
151 //*****************************************************************************
152 #if defined (__REDLIB__)
153 extern void __main(void);
154 #endif
155 extern int main(void);
156 
157 //*****************************************************************************
158 // External declaration for the pointer to the stack top from the Linker Script
159 //*****************************************************************************
160 extern void _vStackTop(void);
161 //*****************************************************************************
162 // External declaration for LPC MCU vector table checksum from  Linker Script
163 //*****************************************************************************
164 WEAK extern void __valid_user_code_checksum();
165 
166 //*****************************************************************************
167 //*****************************************************************************
168 #if defined (__cplusplus)
169 } // extern "C"
170 #endif
171 //*****************************************************************************
172 // The vector table.
173 // This relies on the linker script to place at correct location in memory.
174 //*****************************************************************************
175 
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,                       // The NMI handler
187     HardFault_Handler,                 // The hard fault handler
188     0,                                 // Reserved
189     0,                                 // Reserved
190     0,                                 // Reserved
191     __valid_user_code_checksum,        // LPC MCU checksum
192     0,                                 // ECRP
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 - LPC51U68
202     WDT_BOD_IRQHandler,       // 16: Windowed watchdog timer, Brownout detect
203     DMA0_IRQHandler,          // 17: DMA controller
204     GINT0_IRQHandler,         // 18: GPIO group 0
205     GINT1_IRQHandler,         // 19: GPIO group 1
206     PIN_INT0_IRQHandler,      // 20: Pin interrupt 0 or pattern match engine slice 0
207     PIN_INT1_IRQHandler,      // 21: Pin interrupt 1or pattern match engine slice 1
208     PIN_INT2_IRQHandler,      // 22: Pin interrupt 2 or pattern match engine slice 2
209     PIN_INT3_IRQHandler,      // 23: Pin interrupt 3 or pattern match engine slice 3
210     UTICK0_IRQHandler,        // 24: Micro-tick Timer
211     MRT0_IRQHandler,          // 25: Multi-rate timer
212     CTIMER0_IRQHandler,       // 26: Standard counter/timer CTIMER0
213     CTIMER1_IRQHandler,       // 27: Standard counter/timer CTIMER1
214     SCT0_IRQHandler,          // 28: SCTimer/PWM
215     CTIMER3_IRQHandler,       // 29: Standard counter/timer CTIMER3
216     FLEXCOMM0_IRQHandler,     // 30: Flexcomm Interface 0 (USART, SPI, I2C)
217     FLEXCOMM1_IRQHandler,     // 31: Flexcomm Interface 1 (USART, SPI, I2C)
218     FLEXCOMM2_IRQHandler,     // 32: Flexcomm Interface 2 (USART, SPI, I2C)
219     FLEXCOMM3_IRQHandler,     // 33: Flexcomm Interface 3 (USART, SPI, I2C)
220     FLEXCOMM4_IRQHandler,     // 34: Flexcomm Interface 4 (USART, SPI, I2C)
221     FLEXCOMM5_IRQHandler,     // 35: Flexcomm Interface 5 (USART, SPI, I2C)
222     FLEXCOMM6_IRQHandler,     // 36: Flexcomm Interface 6 (USART, SPI, I2C, I2S)
223     FLEXCOMM7_IRQHandler,     // 37: Flexcomm Interface 7 (USART, SPI, I2C, I2S)
224     ADC0_SEQA_IRQHandler,     // 38: ADC0 sequence A completion.
225     ADC0_SEQB_IRQHandler,     // 39: ADC0 sequence B completion.
226     ADC0_THCMP_IRQHandler,    // 40: ADC0 threshold compare and error.
227     Reserved41_IRQHandler,    // 41: Reserved interrupt
228     Reserved42_IRQHandler,    // 42: Reserved interrupt
229     USB0_NEEDCLK_IRQHandler,  // 43: USB Activity Wake-up Interrupt
230     USB0_IRQHandler,          // 44: USB device
231     RTC_IRQHandler,           // 45: RTC alarm and wake-up interrupts
232 
233 
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 
279     // Disable interrupts
280     __asm volatile ("cpsid i");
281 
282 
283     // Enable SRAM clock used by Stack
284     __asm volatile ("LDR R0, =0x40000220\n\t"
285                     "MOV R1, #56\n\t"
286                     "STR R1, [R0]");
287 
288 #if defined (__USE_CMSIS)
289 // If __USE_CMSIS defined, then call CMSIS SystemInit code
290     SystemInit();
291 
292 #endif // (__USE_CMSIS)
293 
294     //
295     // Copy the data sections from flash to SRAM.
296     //
297     unsigned int LoadAddr, ExeAddr, SectionLen;
298     unsigned int *SectionTableAddr;
299 
300     // Load base address of Global Section Table
301     SectionTableAddr = &__data_section_table;
302 
303     // Copy the data sections from flash to SRAM.
304     while (SectionTableAddr < &__data_section_table_end) {
305         LoadAddr = *SectionTableAddr++;
306         ExeAddr = *SectionTableAddr++;
307         SectionLen = *SectionTableAddr++;
308         data_init(LoadAddr, ExeAddr, SectionLen);
309     }
310 
311     // At this point, SectionTableAddr = &__bss_section_table;
312     // Zero fill the bss segment
313     while (SectionTableAddr < &__bss_section_table_end) {
314         ExeAddr = *SectionTableAddr++;
315         SectionLen = *SectionTableAddr++;
316         bss_init(ExeAddr, SectionLen);
317     }
318 
319 
320 #if !defined (__USE_CMSIS)
321 // Assume that if __USE_CMSIS defined, then CMSIS SystemInit code
322 // will setup the VTOR register
323 
324     // Check to see if we are running the code from a non-zero
325     // address (eg RAM, external flash), in which case we need
326     // to modify the VTOR register to tell the CPU that the
327     // vector table is located at a non-0x0 address.
328     unsigned int * pSCB_VTOR = (unsigned int *) 0xE000ED08;
329     if ((unsigned int *)g_pfnVectors!=(unsigned int *) 0x00000000) {
330         *pSCB_VTOR = (unsigned int)g_pfnVectors;
331     }
332 #endif // (__USE_CMSIS)
333 #if defined (__cplusplus)
334     //
335     // Call C++ library initialisation
336     //
337     __libc_init_array();
338 #endif
339 
340     // Reenable interrupts
341     __asm volatile ("cpsie i");
342 
343 #if defined (__REDLIB__)
344     // Call the Redlib library, which in turn calls main()
345     __main();
346 #else
347     main();
348 #endif
349 
350     //
351     // main() shouldn't return, but if it does, we'll just enter an infinite loop
352     //
353     while (1) {
354         ;
355     }
356 }
357 
358 //*****************************************************************************
359 // Default core exception handlers. Override the ones here by defining your own
360 // handler routines in your application code.
361 //*****************************************************************************
NMI_Handler(void)362 WEAK_AV void NMI_Handler(void)
363 { while(1) {}
364 }
365 
HardFault_Handler(void)366 WEAK_AV void HardFault_Handler(void)
367 { while(1) {}
368 }
369 
SVC_Handler(void)370 WEAK_AV void SVC_Handler(void)
371 { while(1) {}
372 }
373 
PendSV_Handler(void)374 WEAK_AV void PendSV_Handler(void)
375 { while(1) {}
376 }
377 
SysTick_Handler(void)378 WEAK_AV void SysTick_Handler(void)
379 { while(1) {}
380 }
381 
382 //*****************************************************************************
383 // Processor ends up here if an unexpected interrupt occurs or a specific
384 // handler is not present in the application code.
385 //*****************************************************************************
IntDefaultHandler(void)386 WEAK_AV void IntDefaultHandler(void)
387 { while(1) {}
388 }
389 
390 //*****************************************************************************
391 // Default application exception handlers. Override the ones here by defining
392 // your own handler routines in your application code. These routines call
393 // driver exception handlers or IntDefaultHandler() if no driver exception
394 // handler is included.
395 //*****************************************************************************
WDT_BOD_IRQHandler(void)396 WEAK_AV void WDT_BOD_IRQHandler(void)
397 {   WDT_BOD_DriverIRQHandler();
398 }
399 
DMA0_IRQHandler(void)400 WEAK_AV void DMA0_IRQHandler(void)
401 {   DMA0_DriverIRQHandler();
402 }
403 
GINT0_IRQHandler(void)404 WEAK_AV void GINT0_IRQHandler(void)
405 {   GINT0_DriverIRQHandler();
406 }
407 
GINT1_IRQHandler(void)408 WEAK_AV void GINT1_IRQHandler(void)
409 {   GINT1_DriverIRQHandler();
410 }
411 
PIN_INT0_IRQHandler(void)412 WEAK_AV void PIN_INT0_IRQHandler(void)
413 {   PIN_INT0_DriverIRQHandler();
414 }
415 
PIN_INT1_IRQHandler(void)416 WEAK_AV void PIN_INT1_IRQHandler(void)
417 {   PIN_INT1_DriverIRQHandler();
418 }
419 
PIN_INT2_IRQHandler(void)420 WEAK_AV void PIN_INT2_IRQHandler(void)
421 {   PIN_INT2_DriverIRQHandler();
422 }
423 
PIN_INT3_IRQHandler(void)424 WEAK_AV void PIN_INT3_IRQHandler(void)
425 {   PIN_INT3_DriverIRQHandler();
426 }
427 
UTICK0_IRQHandler(void)428 WEAK_AV void UTICK0_IRQHandler(void)
429 {   UTICK0_DriverIRQHandler();
430 }
431 
MRT0_IRQHandler(void)432 WEAK_AV void MRT0_IRQHandler(void)
433 {   MRT0_DriverIRQHandler();
434 }
435 
CTIMER0_IRQHandler(void)436 WEAK_AV void CTIMER0_IRQHandler(void)
437 {   CTIMER0_DriverIRQHandler();
438 }
439 
CTIMER1_IRQHandler(void)440 WEAK_AV void CTIMER1_IRQHandler(void)
441 {   CTIMER1_DriverIRQHandler();
442 }
443 
SCT0_IRQHandler(void)444 WEAK_AV void SCT0_IRQHandler(void)
445 {   SCT0_DriverIRQHandler();
446 }
447 
CTIMER3_IRQHandler(void)448 WEAK_AV void CTIMER3_IRQHandler(void)
449 {   CTIMER3_DriverIRQHandler();
450 }
451 
FLEXCOMM0_IRQHandler(void)452 WEAK_AV void FLEXCOMM0_IRQHandler(void)
453 {   FLEXCOMM0_DriverIRQHandler();
454 }
455 
FLEXCOMM1_IRQHandler(void)456 WEAK_AV void FLEXCOMM1_IRQHandler(void)
457 {   FLEXCOMM1_DriverIRQHandler();
458 }
459 
FLEXCOMM2_IRQHandler(void)460 WEAK_AV void FLEXCOMM2_IRQHandler(void)
461 {   FLEXCOMM2_DriverIRQHandler();
462 }
463 
FLEXCOMM3_IRQHandler(void)464 WEAK_AV void FLEXCOMM3_IRQHandler(void)
465 {   FLEXCOMM3_DriverIRQHandler();
466 }
467 
FLEXCOMM4_IRQHandler(void)468 WEAK_AV void FLEXCOMM4_IRQHandler(void)
469 {   FLEXCOMM4_DriverIRQHandler();
470 }
471 
FLEXCOMM5_IRQHandler(void)472 WEAK_AV void FLEXCOMM5_IRQHandler(void)
473 {   FLEXCOMM5_DriverIRQHandler();
474 }
475 
FLEXCOMM6_IRQHandler(void)476 WEAK_AV void FLEXCOMM6_IRQHandler(void)
477 {   FLEXCOMM6_DriverIRQHandler();
478 }
479 
FLEXCOMM7_IRQHandler(void)480 WEAK_AV void FLEXCOMM7_IRQHandler(void)
481 {   FLEXCOMM7_DriverIRQHandler();
482 }
483 
ADC0_SEQA_IRQHandler(void)484 WEAK_AV void ADC0_SEQA_IRQHandler(void)
485 {   ADC0_SEQA_DriverIRQHandler();
486 }
487 
ADC0_SEQB_IRQHandler(void)488 WEAK_AV void ADC0_SEQB_IRQHandler(void)
489 {   ADC0_SEQB_DriverIRQHandler();
490 }
491 
ADC0_THCMP_IRQHandler(void)492 WEAK_AV void ADC0_THCMP_IRQHandler(void)
493 {   ADC0_THCMP_DriverIRQHandler();
494 }
495 
Reserved41_IRQHandler(void)496 WEAK_AV void Reserved41_IRQHandler(void)
497 {   Reserved41_DriverIRQHandler();
498 }
499 
Reserved42_IRQHandler(void)500 WEAK_AV void Reserved42_IRQHandler(void)
501 {   Reserved42_DriverIRQHandler();
502 }
503 
USB0_NEEDCLK_IRQHandler(void)504 WEAK_AV void USB0_NEEDCLK_IRQHandler(void)
505 {   USB0_NEEDCLK_DriverIRQHandler();
506 }
507 
USB0_IRQHandler(void)508 WEAK_AV void USB0_IRQHandler(void)
509 {   USB0_DriverIRQHandler();
510 }
511 
RTC_IRQHandler(void)512 WEAK_AV void RTC_IRQHandler(void)
513 {   RTC_DriverIRQHandler();
514 }
515 
516 //*****************************************************************************
517 
518 #if defined (DEBUG)
519 #pragma GCC pop_options
520 #endif // (DEBUG)
521