1 //*****************************************************************************
\r
2 // LPC51U68 startup code for use with MCUXpresso IDE
\r
5 //*****************************************************************************
\r
7 // Copyright 2016-2018 NXP
\r
9 // SPDX-License-Identifier: BSD-3-Clause
\r
10 //*****************************************************************************
\r
13 #pragma GCC push_options
\r
14 #pragma GCC optimize ("Og")
\r
17 #if defined (__cplusplus)
\r
19 #error Redlib does not support C++
\r
21 //*****************************************************************************
\r
23 // The entry point for the C++ library startup
\r
25 //*****************************************************************************
\r
27 extern void __libc_init_array(void);
\r
32 #define WEAK __attribute__ ((weak))
\r
33 #define WEAK_AV __attribute__ ((weak, section(".after_vectors")))
\r
34 #define ALIAS(f) __attribute__ ((weak, alias (#f)))
\r
36 //*****************************************************************************
\r
37 #if defined (__cplusplus)
\r
41 //*****************************************************************************
\r
42 // Variable to store CRP value in. Will be placed automatically
\r
43 // by the linker when "Enable Code Read Protect" selected.
\r
44 // See crp.h header for more information
\r
45 //*****************************************************************************
\r
46 //*****************************************************************************
\r
47 // Declaration of external SystemInit function
\r
48 //*****************************************************************************
\r
49 #if defined (__USE_CMSIS)
\r
50 extern void SystemInit(void);
\r
51 #endif // (__USE_CMSIS)
\r
53 //*****************************************************************************
\r
54 // Forward declaration of the core exception handlers.
\r
55 // When the application defines a handler (with the same name), this will
\r
56 // automatically take precedence over these weak definitions.
\r
57 // If your application is a C++ one, then any interrupt handlers defined
\r
58 // in C++ files within in your main application will need to have C linkage
\r
59 // rather than C++ linkage. To do this, make sure that you are using extern "C"
\r
60 // { .... } around the interrupt handler within your main application code.
\r
61 //*****************************************************************************
\r
62 void ResetISR(void);
\r
63 WEAK void NMI_Handler(void);
\r
64 WEAK void HardFault_Handler(void);
\r
65 WEAK void SVC_Handler(void);
\r
66 WEAK void PendSV_Handler(void);
\r
67 WEAK void SysTick_Handler(void);
\r
68 WEAK void IntDefaultHandler(void);
\r
70 //*****************************************************************************
\r
71 // Forward declaration of the application IRQ handlers. When the application
\r
72 // defines a handler (with the same name), this will automatically take
\r
73 // precedence over weak definitions below
\r
74 //*****************************************************************************
\r
75 WEAK void WDT_BOD_IRQHandler(void);
\r
76 WEAK void DMA0_IRQHandler(void);
\r
77 WEAK void GINT0_IRQHandler(void);
\r
78 WEAK void GINT1_IRQHandler(void);
\r
79 WEAK void PIN_INT0_IRQHandler(void);
\r
80 WEAK void PIN_INT1_IRQHandler(void);
\r
81 WEAK void PIN_INT2_IRQHandler(void);
\r
82 WEAK void PIN_INT3_IRQHandler(void);
\r
83 WEAK void UTICK0_IRQHandler(void);
\r
84 WEAK void MRT0_IRQHandler(void);
\r
85 WEAK void CTIMER0_IRQHandler(void);
\r
86 WEAK void CTIMER1_IRQHandler(void);
\r
87 WEAK void SCT0_IRQHandler(void);
\r
88 WEAK void CTIMER3_IRQHandler(void);
\r
89 WEAK void FLEXCOMM0_IRQHandler(void);
\r
90 WEAK void FLEXCOMM1_IRQHandler(void);
\r
91 WEAK void FLEXCOMM2_IRQHandler(void);
\r
92 WEAK void FLEXCOMM3_IRQHandler(void);
\r
93 WEAK void FLEXCOMM4_IRQHandler(void);
\r
94 WEAK void FLEXCOMM5_IRQHandler(void);
\r
95 WEAK void FLEXCOMM6_IRQHandler(void);
\r
96 WEAK void FLEXCOMM7_IRQHandler(void);
\r
97 WEAK void ADC0_SEQA_IRQHandler(void);
\r
98 WEAK void ADC0_SEQB_IRQHandler(void);
\r
99 WEAK void ADC0_THCMP_IRQHandler(void);
\r
100 WEAK void Reserved41_IRQHandler(void);
\r
101 WEAK void Reserved42_IRQHandler(void);
\r
102 WEAK void USB0_NEEDCLK_IRQHandler(void);
\r
103 WEAK void USB0_IRQHandler(void);
\r
104 WEAK void RTC_IRQHandler(void);
\r
106 //*****************************************************************************
\r
107 // Forward declaration of the driver IRQ handlers. These are aliased
\r
108 // to the IntDefaultHandler, which is a 'forever' loop. When the driver
\r
109 // defines a handler (with the same name), this will automatically take
\r
110 // precedence over these weak definitions
\r
111 //*****************************************************************************
\r
112 void WDT_BOD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
113 void DMA0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
114 void GINT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
115 void GINT1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
116 void PIN_INT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
117 void PIN_INT1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
118 void PIN_INT2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
119 void PIN_INT3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
120 void UTICK0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
121 void MRT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
122 void CTIMER0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
123 void CTIMER1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
124 void SCT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
125 void CTIMER3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
126 void FLEXCOMM0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
127 void FLEXCOMM1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
128 void FLEXCOMM2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
129 void FLEXCOMM3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
130 void FLEXCOMM4_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
131 void FLEXCOMM5_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
132 void FLEXCOMM6_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
133 void FLEXCOMM7_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
134 void ADC0_SEQA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
135 void ADC0_SEQB_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
136 void ADC0_THCMP_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
137 void Reserved41_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
138 void Reserved42_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
139 void USB0_NEEDCLK_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
140 void USB0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
141 void RTC_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
\r
143 //*****************************************************************************
\r
144 // The entry point for the application.
\r
145 // __main() is the entry point for Redlib based applications
\r
146 // main() is the entry point for Newlib based applications
\r
147 //*****************************************************************************
\r
148 #if defined (__REDLIB__)
\r
149 extern void __main(void);
\r
151 extern int main(void);
\r
153 //*****************************************************************************
\r
154 // External declaration for the pointer to the stack top from the Linker Script
\r
155 //*****************************************************************************
\r
156 extern void _vStackTop(void);
\r
157 //*****************************************************************************
\r
158 // External declaration for LPC MCU vector table checksum from Linker Script
\r
159 //*****************************************************************************
\r
160 WEAK extern void __valid_user_code_checksum();
\r
162 //*****************************************************************************
\r
163 //*****************************************************************************
\r
164 #if defined (__cplusplus)
\r
167 //*****************************************************************************
\r
168 // The vector table.
\r
169 // This relies on the linker script to place at correct location in memory.
\r
170 //*****************************************************************************
\r
171 extern void (* const g_pfnVectors[])(void);
\r
172 extern void * __Vectors __attribute__ ((alias ("g_pfnVectors")));
\r
174 __attribute__ ((used, section(".isr_vector")))
\r
175 void (* const g_pfnVectors[])(void) = {
\r
176 // Core Level - CM0P
\r
177 &_vStackTop, // The initial stack pointer
\r
178 ResetISR, // The reset handler
\r
179 NMI_Handler, // The NMI handler
\r
180 HardFault_Handler, // The hard fault handler
\r
184 __valid_user_code_checksum, // LPC MCU checksum
\r
188 SVC_Handler, // SVCall handler
\r
191 PendSV_Handler, // The PendSV handler
\r
192 SysTick_Handler, // The SysTick handler
\r
194 // Chip Level - LPC51U68
\r
195 WDT_BOD_IRQHandler, // 16: Windowed watchdog timer, Brownout detect
\r
196 DMA0_IRQHandler, // 17: DMA controller
\r
197 GINT0_IRQHandler, // 18: GPIO group 0
\r
198 GINT1_IRQHandler, // 19: GPIO group 1
\r
199 PIN_INT0_IRQHandler, // 20: Pin interrupt 0 or pattern match engine slice 0
\r
200 PIN_INT1_IRQHandler, // 21: Pin interrupt 1or pattern match engine slice 1
\r
201 PIN_INT2_IRQHandler, // 22: Pin interrupt 2 or pattern match engine slice 2
\r
202 PIN_INT3_IRQHandler, // 23: Pin interrupt 3 or pattern match engine slice 3
\r
203 UTICK0_IRQHandler, // 24: Micro-tick Timer
\r
204 MRT0_IRQHandler, // 25: Multi-rate timer
\r
205 CTIMER0_IRQHandler, // 26: Standard counter/timer CTIMER0
\r
206 CTIMER1_IRQHandler, // 27: Standard counter/timer CTIMER1
\r
207 SCT0_IRQHandler, // 28: SCTimer/PWM
\r
208 CTIMER3_IRQHandler, // 29: Standard counter/timer CTIMER3
\r
209 FLEXCOMM0_IRQHandler, // 30: Flexcomm Interface 0 (USART, SPI, I2C)
\r
210 FLEXCOMM1_IRQHandler, // 31: Flexcomm Interface 1 (USART, SPI, I2C)
\r
211 FLEXCOMM2_IRQHandler, // 32: Flexcomm Interface 2 (USART, SPI, I2C)
\r
212 FLEXCOMM3_IRQHandler, // 33: Flexcomm Interface 3 (USART, SPI, I2C)
\r
213 FLEXCOMM4_IRQHandler, // 34: Flexcomm Interface 4 (USART, SPI, I2C)
\r
214 FLEXCOMM5_IRQHandler, // 35: Flexcomm Interface 5 (USART, SPI, I2C)
\r
215 FLEXCOMM6_IRQHandler, // 36: Flexcomm Interface 6 (USART, SPI, I2C, I2S)
\r
216 FLEXCOMM7_IRQHandler, // 37: Flexcomm Interface 7 (USART, SPI, I2C, I2S)
\r
217 ADC0_SEQA_IRQHandler, // 38: ADC0 sequence A completion.
\r
218 ADC0_SEQB_IRQHandler, // 39: ADC0 sequence B completion.
\r
219 ADC0_THCMP_IRQHandler, // 40: ADC0 threshold compare and error.
\r
220 Reserved41_IRQHandler, // 41: Reserved interrupt
\r
221 Reserved42_IRQHandler, // 42: Reserved interrupt
\r
222 USB0_NEEDCLK_IRQHandler, // 43: USB Activity Wake-up Interrupt
\r
223 USB0_IRQHandler, // 44: USB device
\r
224 RTC_IRQHandler, // 45: RTC alarm and wake-up interrupts
\r
225 }; /* End of g_pfnVectors */
\r
227 //*****************************************************************************
\r
228 // Functions to carry out the initialization of RW and BSS data sections. These
\r
229 // are written as separate functions rather than being inlined within the
\r
230 // ResetISR() function in order to cope with MCUs with multiple banks of
\r
232 //*****************************************************************************
\r
233 __attribute__ ((section(".after_vectors.init_data")))
\r
234 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
\r
235 unsigned int *pulDest = (unsigned int*) start;
\r
236 unsigned int *pulSrc = (unsigned int*) romstart;
\r
238 for (loop = 0; loop < len; loop = loop + 4)
\r
239 *pulDest++ = *pulSrc++;
\r
242 __attribute__ ((section(".after_vectors.init_bss")))
\r
243 void bss_init(unsigned int start, unsigned int len) {
\r
244 unsigned int *pulDest = (unsigned int*) start;
\r
246 for (loop = 0; loop < len; loop = loop + 4)
\r
250 //*****************************************************************************
\r
251 // The following symbols are constructs generated by the linker, indicating
\r
252 // the location of various points in the "Global Section Table". This table is
\r
253 // created by the linker via the Code Red managed linker script mechanism. It
\r
254 // contains the load address, execution address and length of each RW data
\r
255 // section and the execution and length of each BSS (zero initialized) section.
\r
256 //*****************************************************************************
\r
257 extern unsigned int __data_section_table;
\r
258 extern unsigned int __data_section_table_end;
\r
259 extern unsigned int __bss_section_table;
\r
260 extern unsigned int __bss_section_table_end;
\r
262 //*****************************************************************************
\r
263 // Reset entry point for your code.
\r
264 // Sets up a simple runtime environment and initializes the C/C++
\r
266 //*****************************************************************************
\r
267 __attribute__ ((section(".after_vectors.reset")))
\r
268 void ResetISR(void) {
\r
270 // Disable interrupts
\r
271 __asm volatile ("cpsid i");
\r
273 // Enable SRAM clock used by Stack
\r
274 __asm volatile ("LDR R0, =0x40000220\n\t"
\r
278 #if defined (__USE_CMSIS)
\r
279 // If __USE_CMSIS defined, then call CMSIS SystemInit code
\r
282 #endif // (__USE_CMSIS)
\r
285 // Copy the data sections from flash to SRAM.
\r
287 unsigned int LoadAddr, ExeAddr, SectionLen;
\r
288 unsigned int *SectionTableAddr;
\r
290 // Load base address of Global Section Table
\r
291 SectionTableAddr = &__data_section_table;
\r
293 // Copy the data sections from flash to SRAM.
\r
294 while (SectionTableAddr < &__data_section_table_end) {
\r
295 LoadAddr = *SectionTableAddr++;
\r
296 ExeAddr = *SectionTableAddr++;
\r
297 SectionLen = *SectionTableAddr++;
\r
298 data_init(LoadAddr, ExeAddr, SectionLen);
\r
301 // At this point, SectionTableAddr = &__bss_section_table;
\r
302 // Zero fill the bss segment
\r
303 while (SectionTableAddr < &__bss_section_table_end) {
\r
304 ExeAddr = *SectionTableAddr++;
\r
305 SectionLen = *SectionTableAddr++;
\r
306 bss_init(ExeAddr, SectionLen);
\r
309 #if !defined (__USE_CMSIS)
\r
310 // Assume that if __USE_CMSIS defined, then CMSIS SystemInit code
\r
311 // will setup the VTOR register
\r
313 // Check to see if we are running the code from a non-zero
\r
314 // address (eg RAM, external flash), in which case we need
\r
315 // to modify the VTOR register to tell the CPU that the
\r
316 // vector table is located at a non-0x0 address.
\r
317 unsigned int * pSCB_VTOR = (unsigned int *) 0xE000ED08;
\r
318 if ((unsigned int *)g_pfnVectors!=(unsigned int *) 0x00000000) {
\r
319 *pSCB_VTOR = (unsigned int)g_pfnVectors;
\r
321 #endif // (__USE_CMSIS)
\r
323 #if defined (__cplusplus)
\r
325 // Call C++ library initialisation
\r
327 __libc_init_array();
\r
330 // Reenable interrupts
\r
331 __asm volatile ("cpsie i");
\r
333 #if defined (__REDLIB__)
\r
334 // Call the Redlib library, which in turn calls main()
\r
341 // main() shouldn't return, but if it does, we'll just enter an infinite loop
\r
348 //*****************************************************************************
\r
349 // Default core exception handlers. Override the ones here by defining your own
\r
350 // handler routines in your application code.
\r
351 //*****************************************************************************
\r
352 WEAK_AV void NMI_Handler(void)
\r
356 WEAK_AV void HardFault_Handler(void)
\r
360 WEAK_AV void SVC_Handler(void)
\r
364 WEAK_AV void PendSV_Handler(void)
\r
368 WEAK_AV void SysTick_Handler(void)
\r
372 //*****************************************************************************
\r
373 // Processor ends up here if an unexpected interrupt occurs or a specific
\r
374 // handler is not present in the application code.
\r
375 //*****************************************************************************
\r
376 WEAK_AV void IntDefaultHandler(void)
\r
380 //*****************************************************************************
\r
381 // Default application exception handlers. Override the ones here by defining
\r
382 // your own handler routines in your application code. These routines call
\r
383 // driver exception handlers or IntDefaultHandler() if no driver exception
\r
384 // handler is included.
\r
385 //*****************************************************************************
\r
386 WEAK_AV void WDT_BOD_IRQHandler(void)
\r
387 { WDT_BOD_DriverIRQHandler();
\r
390 WEAK_AV void DMA0_IRQHandler(void)
\r
391 { DMA0_DriverIRQHandler();
\r
394 WEAK_AV void GINT0_IRQHandler(void)
\r
395 { GINT0_DriverIRQHandler();
\r
398 WEAK_AV void GINT1_IRQHandler(void)
\r
399 { GINT1_DriverIRQHandler();
\r
402 WEAK_AV void PIN_INT0_IRQHandler(void)
\r
403 { PIN_INT0_DriverIRQHandler();
\r
406 WEAK_AV void PIN_INT1_IRQHandler(void)
\r
407 { PIN_INT1_DriverIRQHandler();
\r
410 WEAK_AV void PIN_INT2_IRQHandler(void)
\r
411 { PIN_INT2_DriverIRQHandler();
\r
414 WEAK_AV void PIN_INT3_IRQHandler(void)
\r
415 { PIN_INT3_DriverIRQHandler();
\r
418 WEAK_AV void UTICK0_IRQHandler(void)
\r
419 { UTICK0_DriverIRQHandler();
\r
422 WEAK_AV void MRT0_IRQHandler(void)
\r
423 { MRT0_DriverIRQHandler();
\r
426 WEAK_AV void CTIMER0_IRQHandler(void)
\r
427 { CTIMER0_DriverIRQHandler();
\r
430 WEAK_AV void CTIMER1_IRQHandler(void)
\r
431 { CTIMER1_DriverIRQHandler();
\r
434 WEAK_AV void SCT0_IRQHandler(void)
\r
435 { SCT0_DriverIRQHandler();
\r
438 WEAK_AV void CTIMER3_IRQHandler(void)
\r
439 { CTIMER3_DriverIRQHandler();
\r
442 WEAK_AV void FLEXCOMM0_IRQHandler(void)
\r
443 { FLEXCOMM0_DriverIRQHandler();
\r
446 WEAK_AV void FLEXCOMM1_IRQHandler(void)
\r
447 { FLEXCOMM1_DriverIRQHandler();
\r
450 WEAK_AV void FLEXCOMM2_IRQHandler(void)
\r
451 { FLEXCOMM2_DriverIRQHandler();
\r
454 WEAK_AV void FLEXCOMM3_IRQHandler(void)
\r
455 { FLEXCOMM3_DriverIRQHandler();
\r
458 WEAK_AV void FLEXCOMM4_IRQHandler(void)
\r
459 { FLEXCOMM4_DriverIRQHandler();
\r
462 WEAK_AV void FLEXCOMM5_IRQHandler(void)
\r
463 { FLEXCOMM5_DriverIRQHandler();
\r
466 WEAK_AV void FLEXCOMM6_IRQHandler(void)
\r
467 { FLEXCOMM6_DriverIRQHandler();
\r
470 WEAK_AV void FLEXCOMM7_IRQHandler(void)
\r
471 { FLEXCOMM7_DriverIRQHandler();
\r
474 WEAK_AV void ADC0_SEQA_IRQHandler(void)
\r
475 { ADC0_SEQA_DriverIRQHandler();
\r
478 WEAK_AV void ADC0_SEQB_IRQHandler(void)
\r
479 { ADC0_SEQB_DriverIRQHandler();
\r
482 WEAK_AV void ADC0_THCMP_IRQHandler(void)
\r
483 { ADC0_THCMP_DriverIRQHandler();
\r
486 WEAK_AV void Reserved41_IRQHandler(void)
\r
487 { Reserved41_DriverIRQHandler();
\r
490 WEAK_AV void Reserved42_IRQHandler(void)
\r
491 { Reserved42_DriverIRQHandler();
\r
494 WEAK_AV void USB0_NEEDCLK_IRQHandler(void)
\r
495 { USB0_NEEDCLK_DriverIRQHandler();
\r
498 WEAK_AV void USB0_IRQHandler(void)
\r
499 { USB0_DriverIRQHandler();
\r
502 WEAK_AV void RTC_IRQHandler(void)
\r
503 { RTC_DriverIRQHandler();
\r
506 //*****************************************************************************
\r
508 #if defined (DEBUG)
\r
509 #pragma GCC pop_options
\r