1 //*****************************************************************************
2 // LPC865 startup code for use with MCUXpresso IDE
3 //
4 // Version : 010323
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 // LPC865 Strengthened Code Read Protection (SCRP) modes
44 // See appropriate documentation for the MCU to determine CRP values.
45 //*****************************************************************************
46 #define SCRP_NO_ISP 0x536AAC95
47 #define SCRP_CRP0_NO_CRP 0xFFFFFFFF
48 #define SCRP_CRP1 0x5963A69C
49 #define SCRP_CRP2 0x963569CA
50 #define SCRP_CRP3 0x63599CA6
51 //*****************************************************************************
52 // Variable to store CRP value in. Will be placed automatically
53 // by the linker when "Enable Code Read Protect" selected.
54 // See crp.h header for more information
55 //*****************************************************************************
56 // CAUTION
57 // USE the SCRP codes defined above with ATTENTION!!!
58 // DO NOT USE the CRP modes from crp.h
59 //*****************************************************************************
60 #include <NXP/crp.h>
61 __CRP const unsigned int CRP_WORD = SCRP_CRP0_NO_CRP;
62
63 //*****************************************************************************
64 // Declaration of external SystemInit function
65 //*****************************************************************************
66 #if defined (__USE_CMSIS)
67 extern void SystemInit(void);
68 #endif // (__USE_CMSIS)
69
70 //*****************************************************************************
71 // Forward declaration of the core exception handlers.
72 // When the application defines a handler (with the same name), this will
73 // automatically take precedence over these weak definitions.
74 // If your application is a C++ one, then any interrupt handlers defined
75 // in C++ files within in your main application will need to have C linkage
76 // rather than C++ linkage. To do this, make sure that you are using extern "C"
77 // { .... } around the interrupt handler within your main application code.
78 //*****************************************************************************
79 void ResetISR(void);
80 WEAK void NMI_Handler(void);
81 WEAK void HardFault_Handler(void);
82 WEAK void SVC_Handler(void);
83 WEAK void PendSV_Handler(void);
84 WEAK void SysTick_Handler(void);
85 WEAK void IntDefaultHandler(void);
86
87 //*****************************************************************************
88 // Forward declaration of the application IRQ handlers. When the application
89 // defines a handler (with the same name), this will automatically take
90 // precedence over weak definitions below
91 //*****************************************************************************
92 WEAK void SPI0_IRQHandler(void);
93 WEAK void SPI1_IRQHandler(void);
94 WEAK void Reserved18_IRQHandler(void);
95 WEAK void USART0_IRQHandler(void);
96 WEAK void USART1_IRQHandler(void);
97 WEAK void USART2_IRQHandler(void);
98 WEAK void FTM0_IRQHandler(void);
99 WEAK void FTM1_IRQHandler(void);
100 WEAK void I2C0_IRQHandler(void);
101 WEAK void Reserved25_IRQHandler(void);
102 WEAK void MRT0_IRQHandler(void);
103 WEAK void ACMP_IRQHandler(void);
104 WEAK void WDT_IRQHandler(void);
105 WEAK void BOD_IRQHandler(void);
106 WEAK void FLASH_IRQHandler(void);
107 WEAK void WKT_IRQHandler(void);
108 WEAK void ADC0_SEQA_IRQHandler(void);
109 WEAK void ADC0_SEQB_IRQHandler(void);
110 WEAK void ADC0_THCMP_IRQHandler(void);
111 WEAK void ADC0_OVR_IRQHandler(void);
112 WEAK void DMA0_IRQHandler(void);
113 WEAK void I3C0_IRQHandler(void);
114 WEAK void GPIO_HS_IRQ0_IRQHandler(void);
115 WEAK void GPIO_HS_IRQ1_IRQHandler(void);
116 WEAK void PIN_INT0_IRQHandler(void);
117 WEAK void PIN_INT1_IRQHandler(void);
118 WEAK void PIN_INT2_IRQHandler(void);
119 WEAK void PIN_INT3_IRQHandler(void);
120 WEAK void PIN_INT4_IRQHandler(void);
121 WEAK void PIN_INT5_IRQHandler(void);
122 WEAK void PIN_INT6_IRQHandler(void);
123 WEAK void PIN_INT7_IRQHandler(void);
124
125 //*****************************************************************************
126 // Forward declaration of the driver IRQ handlers. These are aliased
127 // to the IntDefaultHandler, which is a 'forever' loop. When the driver
128 // defines a handler (with the same name), this will automatically take
129 // precedence over these weak definitions
130 //*****************************************************************************
131 void SPI0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
132 void SPI1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
133 void Reserved18_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
134 void USART0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
135 void USART1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
136 void USART2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
137 void FTM0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
138 void FTM1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
139 void I2C0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
140 void Reserved25_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
141 void MRT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
142 void ACMP_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
143 void WDT_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
144 void BOD_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
145 void FLASH_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
146 void WKT_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
147 void ADC0_SEQA_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
148 void ADC0_SEQB_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
149 void ADC0_THCMP_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
150 void ADC0_OVR_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
151 void DMA0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
152 void I3C0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
153 void GPIO_HS_IRQ0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
154 void GPIO_HS_IRQ1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
155 void PIN_INT0_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
156 void PIN_INT1_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
157 void PIN_INT2_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
158 void PIN_INT3_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
159 void PIN_INT4_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
160 void PIN_INT5_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
161 void PIN_INT6_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
162 void PIN_INT7_DriverIRQHandler(void) ALIAS(IntDefaultHandler);
163
164 //*****************************************************************************
165 // The entry point for the application.
166 // __main() is the entry point for Redlib based applications
167 // main() is the entry point for Newlib based applications
168 //*****************************************************************************
169 #if defined (__REDLIB__)
170 extern void __main(void);
171 #endif
172 extern int main(void);
173
174 //*****************************************************************************
175 // External declaration for the pointer to the stack top from the Linker Script
176 //*****************************************************************************
177 extern void _vStackTop(void);
178 //*****************************************************************************
179 // External declaration for LPC MCU vector table checksum from Linker Script
180 //*****************************************************************************
181 WEAK extern void __valid_user_code_checksum();
182
183 //*****************************************************************************
184 //*****************************************************************************
185 #if defined (__cplusplus)
186 } // extern "C"
187 #endif
188 //*****************************************************************************
189 // The vector table.
190 // This relies on the linker script to place at correct location in memory.
191 //*****************************************************************************
192
193 extern void (* const g_pfnVectors[])(void);
194 extern void * __Vectors __attribute__ ((alias ("g_pfnVectors")));
195
196 __attribute__ ((used, section(".isr_vector")))
197 void (* const g_pfnVectors[])(void) = {
198 // Core Level - CM0P
199 &_vStackTop, // The initial stack pointer
200 ResetISR, // The reset handler
201 NMI_Handler, // NMI Handler
202 HardFault_Handler, // Hard Fault Handler
203 0, // Reserved
204 0, // Reserved
205 0, // Reserved
206 __valid_user_code_checksum, // LPC MCU checksum
207 0, // ECRP
208 0, // Reserved
209 0, // Reserved
210 SVC_Handler, // SVCall Handler
211 0, // Reserved
212 0, // Reserved
213 PendSV_Handler, // PendSV Handler
214 SysTick_Handler, // SysTick Handler
215
216 // Chip Level - LPC865
217 SPI0_IRQHandler, // 16: SPI0 interrupt
218 SPI1_IRQHandler, // 17: SPI1 interrupt
219 Reserved18_IRQHandler, // 18: Reserved interrupt
220 USART0_IRQHandler, // 19: USART0 interrupt
221 USART1_IRQHandler, // 20: USART1 interrupt
222 USART2_IRQHandler, // 21: USART2 interrupt
223 FTM0_IRQHandler, // 22: FlexTimer0 interrupt
224 FTM1_IRQHandler, // 23: FlexTimer1 interrupt
225 I2C0_IRQHandler, // 24: I2C0 interrupt
226 Reserved25_IRQHandler, // 25: Reserved interrupt
227 MRT0_IRQHandler, // 26: Multi-rate timer interrupt
228 ACMP_IRQHandler, // 27: Analog comparator interrupt
229 WDT_IRQHandler, // 28: Windowed watchdog timer interrupt
230 BOD_IRQHandler, // 29: BOD interrupts
231 FLASH_IRQHandler, // 30: flash interrupt
232 WKT_IRQHandler, // 31: Self-wake-up timer interrupt
233 ADC0_SEQA_IRQHandler, // 32: ADC0 sequence A completion.
234 ADC0_SEQB_IRQHandler, // 33: ADC0 sequence B completion.
235 ADC0_THCMP_IRQHandler, // 34: ADC0 threshold compare and error.
236 ADC0_OVR_IRQHandler, // 35: ADC0 overrun
237 DMA0_IRQHandler, // 36: DMA0 interrupt
238 I3C0_IRQHandler, // 37: I3C0 interrupt
239 GPIO_HS_IRQ0_IRQHandler, // 38: GPIO group A interrupt
240 GPIO_HS_IRQ1_IRQHandler, // 39: GPIO group B interrupt
241 PIN_INT0_IRQHandler, // 40: Pin interrupt 0 or pattern match engine slice 0 interrupt
242 PIN_INT1_IRQHandler, // 41: Pin interrupt 1 or pattern match engine slice 1 interrupt
243 PIN_INT2_IRQHandler, // 42: Pin interrupt 2 or pattern match engine slice 2 interrupt
244 PIN_INT3_IRQHandler, // 43: Pin interrupt 3 or pattern match engine slice 3 interrupt
245 PIN_INT4_IRQHandler, // 44: Pin interrupt 4 or pattern match engine slice 4 interrupt
246 PIN_INT5_IRQHandler, // 45: Pin interrupt 5 or pattern match engine slice 5 interrupt
247 PIN_INT6_IRQHandler, // 46: Pin interrupt 6 or pattern match engine slice 6 interrupt
248 PIN_INT7_IRQHandler, // 47: Pin interrupt 7 or pattern match engine slice 7 interrupt
249 }; /* End of g_pfnVectors */
250
251 //*****************************************************************************
252 // Functions to carry out the initialization of RW and BSS data sections. These
253 // are written as separate functions rather than being inlined within the
254 // ResetISR() function in order to cope with MCUs with multiple banks of
255 // memory.
256 //*****************************************************************************
257 __attribute__ ((section(".after_vectors.init_data")))
data_init(unsigned int romstart,unsigned int start,unsigned int len)258 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
259 unsigned int *pulDest = (unsigned int*) start;
260 unsigned int *pulSrc = (unsigned int*) romstart;
261 unsigned int loop;
262 for (loop = 0; loop < len; loop = loop + 4)
263 *pulDest++ = *pulSrc++;
264 }
265
266 __attribute__ ((section(".after_vectors.init_bss")))
bss_init(unsigned int start,unsigned int len)267 void bss_init(unsigned int start, unsigned int len) {
268 unsigned int *pulDest = (unsigned int*) start;
269 unsigned int loop;
270 for (loop = 0; loop < len; loop = loop + 4)
271 *pulDest++ = 0;
272 }
273
274 //*****************************************************************************
275 // The following symbols are constructs generated by the linker, indicating
276 // the location of various points in the "Global Section Table". This table is
277 // created by the linker via the Code Red managed linker script mechanism. It
278 // contains the load address, execution address and length of each RW data
279 // section and the execution and length of each BSS (zero initialized) section.
280 //*****************************************************************************
281 extern unsigned int __data_section_table;
282 extern unsigned int __data_section_table_end;
283 extern unsigned int __bss_section_table;
284 extern unsigned int __bss_section_table_end;
285
286 //*****************************************************************************
287 // Reset entry point for your code.
288 // Sets up a simple runtime environment and initializes the C/C++
289 // library.
290 //*****************************************************************************
291 __attribute__ ((naked, section(".after_vectors.reset")))
ResetISR(void)292 void ResetISR(void) {
293 // Disable interrupts
294 __asm volatile ("cpsid i");
295 // Enable SRAM clock used by Stack
296 __asm volatile ("LDR R0, =0x40000220\n\t"
297 "MOV R1, #56\n\t"
298 "STR R1, [R0]");
299
300 #if defined (__USE_CMSIS)
301 // If __USE_CMSIS defined, then call CMSIS SystemInit code
302 SystemInit();
303
304 #endif // (__USE_CMSIS)
305
306 //
307 // Copy the data sections from flash to SRAM.
308 //
309 unsigned int LoadAddr, ExeAddr, SectionLen;
310 unsigned int *SectionTableAddr;
311
312 // Load base address of Global Section Table
313 SectionTableAddr = &__data_section_table;
314
315 // Copy the data sections from flash to SRAM.
316 while (SectionTableAddr < &__data_section_table_end) {
317 LoadAddr = *SectionTableAddr++;
318 ExeAddr = *SectionTableAddr++;
319 SectionLen = *SectionTableAddr++;
320 data_init(LoadAddr, ExeAddr, SectionLen);
321 }
322
323 // At this point, SectionTableAddr = &__bss_section_table;
324 // Zero fill the bss segment
325 while (SectionTableAddr < &__bss_section_table_end) {
326 ExeAddr = *SectionTableAddr++;
327 SectionLen = *SectionTableAddr++;
328 bss_init(ExeAddr, SectionLen);
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 //*****************************************************************************
SPI0_IRQHandler(void)407 WEAK_AV void SPI0_IRQHandler(void)
408 { SPI0_DriverIRQHandler();
409 }
410
SPI1_IRQHandler(void)411 WEAK_AV void SPI1_IRQHandler(void)
412 { SPI1_DriverIRQHandler();
413 }
414
Reserved18_IRQHandler(void)415 WEAK_AV void Reserved18_IRQHandler(void)
416 { Reserved18_DriverIRQHandler();
417 }
418
USART0_IRQHandler(void)419 WEAK_AV void USART0_IRQHandler(void)
420 { USART0_DriverIRQHandler();
421 }
422
USART1_IRQHandler(void)423 WEAK_AV void USART1_IRQHandler(void)
424 { USART1_DriverIRQHandler();
425 }
426
USART2_IRQHandler(void)427 WEAK_AV void USART2_IRQHandler(void)
428 { USART2_DriverIRQHandler();
429 }
430
FTM0_IRQHandler(void)431 WEAK_AV void FTM0_IRQHandler(void)
432 { FTM0_DriverIRQHandler();
433 }
434
FTM1_IRQHandler(void)435 WEAK_AV void FTM1_IRQHandler(void)
436 { FTM1_DriverIRQHandler();
437 }
438
I2C0_IRQHandler(void)439 WEAK_AV void I2C0_IRQHandler(void)
440 { I2C0_DriverIRQHandler();
441 }
442
Reserved25_IRQHandler(void)443 WEAK_AV void Reserved25_IRQHandler(void)
444 { Reserved25_DriverIRQHandler();
445 }
446
MRT0_IRQHandler(void)447 WEAK_AV void MRT0_IRQHandler(void)
448 { MRT0_DriverIRQHandler();
449 }
450
ACMP_IRQHandler(void)451 WEAK_AV void ACMP_IRQHandler(void)
452 { ACMP_DriverIRQHandler();
453 }
454
WDT_IRQHandler(void)455 WEAK_AV void WDT_IRQHandler(void)
456 { WDT_DriverIRQHandler();
457 }
458
BOD_IRQHandler(void)459 WEAK_AV void BOD_IRQHandler(void)
460 { BOD_DriverIRQHandler();
461 }
462
FLASH_IRQHandler(void)463 WEAK_AV void FLASH_IRQHandler(void)
464 { FLASH_DriverIRQHandler();
465 }
466
WKT_IRQHandler(void)467 WEAK_AV void WKT_IRQHandler(void)
468 { WKT_DriverIRQHandler();
469 }
470
ADC0_SEQA_IRQHandler(void)471 WEAK_AV void ADC0_SEQA_IRQHandler(void)
472 { ADC0_SEQA_DriverIRQHandler();
473 }
474
ADC0_SEQB_IRQHandler(void)475 WEAK_AV void ADC0_SEQB_IRQHandler(void)
476 { ADC0_SEQB_DriverIRQHandler();
477 }
478
ADC0_THCMP_IRQHandler(void)479 WEAK_AV void ADC0_THCMP_IRQHandler(void)
480 { ADC0_THCMP_DriverIRQHandler();
481 }
482
ADC0_OVR_IRQHandler(void)483 WEAK_AV void ADC0_OVR_IRQHandler(void)
484 { ADC0_OVR_DriverIRQHandler();
485 }
486
DMA0_IRQHandler(void)487 WEAK_AV void DMA0_IRQHandler(void)
488 { DMA0_DriverIRQHandler();
489 }
490
I3C0_IRQHandler(void)491 WEAK_AV void I3C0_IRQHandler(void)
492 { I3C0_DriverIRQHandler();
493 }
494
GPIO_HS_IRQ0_IRQHandler(void)495 WEAK_AV void GPIO_HS_IRQ0_IRQHandler(void)
496 { GPIO_HS_IRQ0_DriverIRQHandler();
497 }
498
GPIO_HS_IRQ1_IRQHandler(void)499 WEAK_AV void GPIO_HS_IRQ1_IRQHandler(void)
500 { GPIO_HS_IRQ1_DriverIRQHandler();
501 }
502
PIN_INT0_IRQHandler(void)503 WEAK_AV void PIN_INT0_IRQHandler(void)
504 { PIN_INT0_DriverIRQHandler();
505 }
506
PIN_INT1_IRQHandler(void)507 WEAK_AV void PIN_INT1_IRQHandler(void)
508 { PIN_INT1_DriverIRQHandler();
509 }
510
PIN_INT2_IRQHandler(void)511 WEAK_AV void PIN_INT2_IRQHandler(void)
512 { PIN_INT2_DriverIRQHandler();
513 }
514
PIN_INT3_IRQHandler(void)515 WEAK_AV void PIN_INT3_IRQHandler(void)
516 { PIN_INT3_DriverIRQHandler();
517 }
518
PIN_INT4_IRQHandler(void)519 WEAK_AV void PIN_INT4_IRQHandler(void)
520 { PIN_INT4_DriverIRQHandler();
521 }
522
PIN_INT5_IRQHandler(void)523 WEAK_AV void PIN_INT5_IRQHandler(void)
524 { PIN_INT5_DriverIRQHandler();
525 }
526
PIN_INT6_IRQHandler(void)527 WEAK_AV void PIN_INT6_IRQHandler(void)
528 { PIN_INT6_DriverIRQHandler();
529 }
530
PIN_INT7_IRQHandler(void)531 WEAK_AV void PIN_INT7_IRQHandler(void)
532 { PIN_INT7_DriverIRQHandler();
533 }
534
535 //*****************************************************************************
536
537 #if defined (DEBUG)
538 #pragma GCC pop_options
539 #endif // (DEBUG)
540