1 //*****************************************************************************
2 // LPC804 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 SPI0_IRQHandler(void);
80 WEAK void Reserved17_IRQHandler(void);
81 WEAK void DAC0_IRQHandler(void);
82 WEAK void USART0_IRQHandler(void);
83 WEAK void USART1_IRQHandler(void);
84 WEAK void Reserved21_IRQHandler(void);
85 WEAK void Reserved22_IRQHandler(void);
86 WEAK void I2C1_IRQHandler(void);
87 WEAK void I2C0_IRQHandler(void);
88 WEAK void Reserved25_IRQHandler(void);
89 WEAK void MRT0_IRQHandler(void);
90 WEAK void CMP_CAPT_IRQHandler(void);
91 WEAK void WDT_IRQHandler(void);
92 WEAK void BOD_IRQHandler(void);
93 WEAK void FLASH_IRQHandler(void);
94 WEAK void WKT_IRQHandler(void);
95 WEAK void ADC_SEQA_IRQHandler(void);
96 WEAK void ADC_SEQB_IRQHandler(void);
97 WEAK void ADC_THCMP_IRQHandler(void);
98 WEAK void ADC_OVR_IRQHandler(void);
99 WEAK void Reserved36_IRQHandler(void);
100 WEAK void Reserved37_IRQHandler(void);
101 WEAK void Reserved38_IRQHandler(void);
102 WEAK void CTIMER0_IRQHandler(void);
103 WEAK void PIN_INT0_IRQHandler(void);
104 WEAK void PIN_INT1_IRQHandler(void);
105 WEAK void PIN_INT2_IRQHandler(void);
106 WEAK void PIN_INT3_IRQHandler(void);
107 WEAK void PIN_INT4_IRQHandler(void);
108 WEAK void PIN_INT5_IRQHandler(void);
109 WEAK void PIN_INT6_IRQHandler(void);
110 WEAK void PIN_INT7_IRQHandler(void);
111 
112 //*****************************************************************************
113 // Forward declaration of the driver IRQ handlers. These are aliased
114 // to the IntDefaultHandler, which is a 'forever' loop. When the driver
115 // defines a handler (with the same name), this will automatically take
116 // precedence over these weak definitions
117 //*****************************************************************************
118 void SPI0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
119 void Reserved17_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
120 void DAC0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
121 void USART0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
122 void USART1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
123 void Reserved21_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
124 void Reserved22_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
125 void I2C1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
126 void I2C0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
127 void Reserved25_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
128 void MRT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
129 void CMP_CAPT_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
130 void WDT_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
131 void BOD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
132 void FLASH_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
133 void WKT_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
134 void ADC_SEQA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
135 void ADC_SEQB_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
136 void ADC_THCMP_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
137 void ADC_OVR_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
138 void Reserved36_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
139 void Reserved37_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
140 void Reserved38_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
141 void CTIMER0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
142 void PIN_INT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
143 void PIN_INT1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
144 void PIN_INT2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
145 void PIN_INT3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
146 void PIN_INT4_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
147 void PIN_INT5_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
148 void PIN_INT6_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
149 void PIN_INT7_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
150 
151 //*****************************************************************************
152 // The entry point for the application.
153 // __main() is the entry point for Redlib based applications
154 // main() is the entry point for Newlib based applications
155 //*****************************************************************************
156 #if defined (__REDLIB__)
157 extern void __main(void);
158 #endif
159 extern int main(void);
160 
161 //*****************************************************************************
162 // External declaration for the pointer to the stack top from the Linker Script
163 //*****************************************************************************
164 extern void _vStackTop(void);
165 //*****************************************************************************
166 // External declaration for LPC MCU vector table checksum from  Linker Script
167 //*****************************************************************************
168 WEAK extern void __valid_user_code_checksum();
169 
170 //*****************************************************************************
171 //*****************************************************************************
172 #if defined (__cplusplus)
173 } // extern "C"
174 #endif
175 //*****************************************************************************
176 // The vector table.
177 // This relies on the linker script to place at correct location in memory.
178 //*****************************************************************************
179 
180 
181 
182 extern void (* const g_pfnVectors[])(void);
183 extern void * __Vectors __attribute__ ((alias ("g_pfnVectors")));
184 
185 __attribute__ ((used, section(".isr_vector")))
186 void (* const g_pfnVectors[])(void) = {
187     // Core Level - CM0P
188     &_vStackTop,                       // The initial stack pointer
189     ResetISR,                          // The reset handler
190     NMI_Handler,                       // The NMI handler
191     HardFault_Handler,                 // The hard fault handler
192     0,                                 // Reserved
193     0,                                 // Reserved
194     0,                                 // Reserved
195     __valid_user_code_checksum,        // LPC MCU checksum
196     0,                                 // ECRP
197     0,                                 // Reserved
198     0,                                 // Reserved
199     SVC_Handler,                       // SVCall handler
200     0,                                 // Reserved
201     0,                                 // Reserved
202     PendSV_Handler,                    // The PendSV handler
203     SysTick_Handler,                   // The SysTick handler
204 
205     // Chip Level - LPC804
206     SPI0_IRQHandler,         // 16: SPI0 interrupt
207     Reserved17_IRQHandler,   // 17: Reserved interrupt
208     DAC0_IRQHandler,         // 18: DAC0 interrupt
209     USART0_IRQHandler,       // 19: USART0 interrupt
210     USART1_IRQHandler,       // 20: USART1 interrupt
211     Reserved21_IRQHandler,   // 21: Reserved interrupt
212     Reserved22_IRQHandler,   // 22: Reserved interrupt
213     I2C1_IRQHandler,         // 23: I2C1 interrupt
214     I2C0_IRQHandler,         // 24: I2C0 interrupt
215     Reserved25_IRQHandler,   // 25: Reserved interrupt
216     MRT0_IRQHandler,         // 26: Multi-rate timer interrupt
217     CMP_CAPT_IRQHandler,     // 27: Analog comparator interrupt or Capacitive Touch interrupt
218     WDT_IRQHandler,          // 28: Windowed watchdog timer interrupt
219     BOD_IRQHandler,          // 29: BOD interrupts
220     FLASH_IRQHandler,        // 30: flash interrupt
221     WKT_IRQHandler,          // 31: Self-wake-up timer interrupt
222     ADC_SEQA_IRQHandler,     // 32: ADC sequence A completion.
223     ADC_SEQB_IRQHandler,     // 33: ADC sequence B completion.
224     ADC_THCMP_IRQHandler,    // 34: ADC threshold compare and error.
225     ADC_OVR_IRQHandler,      // 35: ADC overrun
226     Reserved36_IRQHandler,   // 36: Reserved interrupt
227     Reserved37_IRQHandler,   // 37: Reserved interrupt
228     Reserved38_IRQHandler,   // 38: Reserved interrupt
229     CTIMER0_IRQHandler,      // 39: Timer interrupt
230     PIN_INT0_IRQHandler,     // 40: Pin interrupt 0 or pattern match engine slice 0 interrupt
231     PIN_INT1_IRQHandler,     // 41: Pin interrupt 1 or pattern match engine slice 1 interrupt
232     PIN_INT2_IRQHandler,     // 42: Pin interrupt 2 or pattern match engine slice 2 interrupt
233     PIN_INT3_IRQHandler,     // 43: Pin interrupt 3 or pattern match engine slice 3 interrupt
234     PIN_INT4_IRQHandler,     // 44: Pin interrupt 4 or pattern match engine slice 4 interrupt
235     PIN_INT5_IRQHandler,     // 45: Pin interrupt 5 or pattern match engine slice 5 interrupt
236     PIN_INT6_IRQHandler,     // 46: Pin interrupt 6 or pattern match engine slice 6 interrupt
237     PIN_INT7_IRQHandler,     // 47: Pin interrupt 7 or pattern match engine slice 7 interrupt
238 
239 
240 }; /* End of g_pfnVectors */
241 
242 //*****************************************************************************
243 // Functions to carry out the initialization of RW and BSS data sections. These
244 // are written as separate functions rather than being inlined within the
245 // ResetISR() function in order to cope with MCUs with multiple banks of
246 // memory.
247 //*****************************************************************************
248 __attribute__ ((section(".after_vectors.init_data")))
data_init(unsigned int romstart,unsigned int start,unsigned int len)249 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
250     unsigned int *pulDest = (unsigned int*) start;
251     unsigned int *pulSrc = (unsigned int*) romstart;
252     unsigned int loop;
253     for (loop = 0; loop < len; loop = loop + 4)
254         *pulDest++ = *pulSrc++;
255 }
256 
257 __attribute__ ((section(".after_vectors.init_bss")))
bss_init(unsigned int start,unsigned int len)258 void bss_init(unsigned int start, unsigned int len) {
259     unsigned int *pulDest = (unsigned int*) start;
260     unsigned int loop;
261     for (loop = 0; loop < len; loop = loop + 4)
262         *pulDest++ = 0;
263 }
264 
265 //*****************************************************************************
266 // The following symbols are constructs generated by the linker, indicating
267 // the location of various points in the "Global Section Table". This table is
268 // created by the linker via the Code Red managed linker script mechanism. It
269 // contains the load address, execution address and length of each RW data
270 // section and the execution and length of each BSS (zero initialized) section.
271 //*****************************************************************************
272 extern unsigned int __data_section_table;
273 extern unsigned int __data_section_table_end;
274 extern unsigned int __bss_section_table;
275 extern unsigned int __bss_section_table_end;
276 
277 //*****************************************************************************
278 // Reset entry point for your code.
279 // Sets up a simple runtime environment and initializes the C/C++
280 // library.
281 //*****************************************************************************
282 __attribute__ ((naked, section(".after_vectors.reset")))
ResetISR(void)283 void ResetISR(void) {
284 
285     // Disable interrupts
286     __asm volatile ("cpsid i");
287 
288 
289     // Enable SRAM clock used by Stack
290     __asm volatile ("LDR R0, =0x40000220\n\t"
291                     "MOV R1, #56\n\t"
292                     "STR R1, [R0]");
293 
294 #if defined (__USE_CMSIS)
295 // If __USE_CMSIS defined, then call CMSIS SystemInit code
296     SystemInit();
297 
298 #endif // (__USE_CMSIS)
299 
300     //
301     // Copy the data sections from flash to SRAM.
302     //
303     unsigned int LoadAddr, ExeAddr, SectionLen;
304     unsigned int *SectionTableAddr;
305 
306     // Load base address of Global Section Table
307     SectionTableAddr = &__data_section_table;
308 
309     // Copy the data sections from flash to SRAM.
310     while (SectionTableAddr < &__data_section_table_end) {
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         ExeAddr = *SectionTableAddr++;
321         SectionLen = *SectionTableAddr++;
322         bss_init(ExeAddr, SectionLen);
323     }
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         *pSCB_VTOR = (unsigned int)g_pfnVectors;
337     }
338 #endif // (__USE_CMSIS)
339 #if defined (__cplusplus)
340     //
341     // Call C++ library initialisation
342     //
343     __libc_init_array();
344 #endif
345 
346     // Reenable interrupts
347     __asm volatile ("cpsie i");
348 
349 #if defined (__REDLIB__)
350     // Call the Redlib library, which in turn calls main()
351     __main();
352 #else
353     main();
354 #endif
355 
356     //
357     // main() shouldn't return, but if it does, we'll just enter an infinite loop
358     //
359     while (1) {
360         ;
361     }
362 }
363 
364 //*****************************************************************************
365 // Default core exception handlers. Override the ones here by defining your own
366 // handler routines in your application code.
367 //*****************************************************************************
NMI_Handler(void)368 WEAK_AV void NMI_Handler(void)
369 { while(1) {}
370 }
371 
HardFault_Handler(void)372 WEAK_AV void HardFault_Handler(void)
373 { while(1) {}
374 }
375 
SVC_Handler(void)376 WEAK_AV void SVC_Handler(void)
377 { while(1) {}
378 }
379 
PendSV_Handler(void)380 WEAK_AV void PendSV_Handler(void)
381 { while(1) {}
382 }
383 
SysTick_Handler(void)384 WEAK_AV void SysTick_Handler(void)
385 { while(1) {}
386 }
387 
388 //*****************************************************************************
389 // Processor ends up here if an unexpected interrupt occurs or a specific
390 // handler is not present in the application code.
391 //*****************************************************************************
IntDefaultHandler(void)392 WEAK_AV void IntDefaultHandler(void)
393 { while(1) {}
394 }
395 
396 //*****************************************************************************
397 // Default application exception handlers. Override the ones here by defining
398 // your own handler routines in your application code. These routines call
399 // driver exception handlers or IntDefaultHandler() if no driver exception
400 // handler is included.
401 //*****************************************************************************
SPI0_IRQHandler(void)402 WEAK_AV void SPI0_IRQHandler(void)
403 {   SPI0_DriverIRQHandler();
404 }
405 
Reserved17_IRQHandler(void)406 WEAK_AV void Reserved17_IRQHandler(void)
407 {   Reserved17_DriverIRQHandler();
408 }
409 
DAC0_IRQHandler(void)410 WEAK_AV void DAC0_IRQHandler(void)
411 {   DAC0_DriverIRQHandler();
412 }
413 
USART0_IRQHandler(void)414 WEAK_AV void USART0_IRQHandler(void)
415 {   USART0_DriverIRQHandler();
416 }
417 
USART1_IRQHandler(void)418 WEAK_AV void USART1_IRQHandler(void)
419 {   USART1_DriverIRQHandler();
420 }
421 
Reserved21_IRQHandler(void)422 WEAK_AV void Reserved21_IRQHandler(void)
423 {   Reserved21_DriverIRQHandler();
424 }
425 
Reserved22_IRQHandler(void)426 WEAK_AV void Reserved22_IRQHandler(void)
427 {   Reserved22_DriverIRQHandler();
428 }
429 
I2C1_IRQHandler(void)430 WEAK_AV void I2C1_IRQHandler(void)
431 {   I2C1_DriverIRQHandler();
432 }
433 
I2C0_IRQHandler(void)434 WEAK_AV void I2C0_IRQHandler(void)
435 {   I2C0_DriverIRQHandler();
436 }
437 
Reserved25_IRQHandler(void)438 WEAK_AV void Reserved25_IRQHandler(void)
439 {   Reserved25_DriverIRQHandler();
440 }
441 
MRT0_IRQHandler(void)442 WEAK_AV void MRT0_IRQHandler(void)
443 {   MRT0_DriverIRQHandler();
444 }
445 
CMP_CAPT_IRQHandler(void)446 WEAK_AV void CMP_CAPT_IRQHandler(void)
447 {   CMP_CAPT_DriverIRQHandler();
448 }
449 
WDT_IRQHandler(void)450 WEAK_AV void WDT_IRQHandler(void)
451 {   WDT_DriverIRQHandler();
452 }
453 
BOD_IRQHandler(void)454 WEAK_AV void BOD_IRQHandler(void)
455 {   BOD_DriverIRQHandler();
456 }
457 
FLASH_IRQHandler(void)458 WEAK_AV void FLASH_IRQHandler(void)
459 {   FLASH_DriverIRQHandler();
460 }
461 
WKT_IRQHandler(void)462 WEAK_AV void WKT_IRQHandler(void)
463 {   WKT_DriverIRQHandler();
464 }
465 
ADC_SEQA_IRQHandler(void)466 WEAK_AV void ADC_SEQA_IRQHandler(void)
467 {   ADC_SEQA_DriverIRQHandler();
468 }
469 
ADC_SEQB_IRQHandler(void)470 WEAK_AV void ADC_SEQB_IRQHandler(void)
471 {   ADC_SEQB_DriverIRQHandler();
472 }
473 
ADC_THCMP_IRQHandler(void)474 WEAK_AV void ADC_THCMP_IRQHandler(void)
475 {   ADC_THCMP_DriverIRQHandler();
476 }
477 
ADC_OVR_IRQHandler(void)478 WEAK_AV void ADC_OVR_IRQHandler(void)
479 {   ADC_OVR_DriverIRQHandler();
480 }
481 
Reserved36_IRQHandler(void)482 WEAK_AV void Reserved36_IRQHandler(void)
483 {   Reserved36_DriverIRQHandler();
484 }
485 
Reserved37_IRQHandler(void)486 WEAK_AV void Reserved37_IRQHandler(void)
487 {   Reserved37_DriverIRQHandler();
488 }
489 
Reserved38_IRQHandler(void)490 WEAK_AV void Reserved38_IRQHandler(void)
491 {   Reserved38_DriverIRQHandler();
492 }
493 
CTIMER0_IRQHandler(void)494 WEAK_AV void CTIMER0_IRQHandler(void)
495 {   CTIMER0_DriverIRQHandler();
496 }
497 
PIN_INT0_IRQHandler(void)498 WEAK_AV void PIN_INT0_IRQHandler(void)
499 {   PIN_INT0_DriverIRQHandler();
500 }
501 
PIN_INT1_IRQHandler(void)502 WEAK_AV void PIN_INT1_IRQHandler(void)
503 {   PIN_INT1_DriverIRQHandler();
504 }
505 
PIN_INT2_IRQHandler(void)506 WEAK_AV void PIN_INT2_IRQHandler(void)
507 {   PIN_INT2_DriverIRQHandler();
508 }
509 
PIN_INT3_IRQHandler(void)510 WEAK_AV void PIN_INT3_IRQHandler(void)
511 {   PIN_INT3_DriverIRQHandler();
512 }
513 
PIN_INT4_IRQHandler(void)514 WEAK_AV void PIN_INT4_IRQHandler(void)
515 {   PIN_INT4_DriverIRQHandler();
516 }
517 
PIN_INT5_IRQHandler(void)518 WEAK_AV void PIN_INT5_IRQHandler(void)
519 {   PIN_INT5_DriverIRQHandler();
520 }
521 
PIN_INT6_IRQHandler(void)522 WEAK_AV void PIN_INT6_IRQHandler(void)
523 {   PIN_INT6_DriverIRQHandler();
524 }
525 
PIN_INT7_IRQHandler(void)526 WEAK_AV void PIN_INT7_IRQHandler(void)
527 {   PIN_INT7_DriverIRQHandler();
528 }
529 
530 //*****************************************************************************
531 
532 #if defined (DEBUG)
533 #pragma GCC pop_options
534 #endif // (DEBUG)
535