1 //*****************************************************************************
2 // LPC15xx Microcontroller Startup code for use with LPCXpresso IDE
3 //
4 // Version : 140114
5 //*****************************************************************************
6 //
7 // Copyright(C) NXP Semiconductors, 2014
8 // All rights reserved.
9 //
10 // Software that is described herein is for illustrative purposes only
11 // which provides customers with programming information regarding the
12 // LPC products.  This software is supplied "AS IS" without any warranties of
13 // any kind, and NXP Semiconductors and its licensor disclaim any and
14 // all warranties, express or implied, including all implied warranties of
15 // merchantability, fitness for a particular purpose and non-infringement of
16 // intellectual property rights.  NXP Semiconductors assumes no responsibility
17 // or liability for the use of the software, conveys no license or rights under any
18 // patent, copyright, mask work right, or any other intellectual property rights in
19 // or to any products. NXP Semiconductors reserves the right to make changes
20 // in the software without notification. NXP Semiconductors also makes no
21 // representation or warranty that such application will be suitable for the
22 // specified use without further testing or modification.
23 //
24 // Permission to use, copy, modify, and distribute this software and its
25 // documentation is hereby granted, under NXP Semiconductors' and its
26 // licensor's relevant copyrights in the software, without fee, provided that it
27 // is used in conjunction with NXP Semiconductors microcontrollers.  This
28 // copyright, permission, and disclaimer notice must appear in all copies of
29 // this code.
30 //*****************************************************************************
31 
32 #if defined (__cplusplus)
33 #ifdef __REDLIB__
34 #error Redlib does not support C++
35 #else
36 //*****************************************************************************
37 //
38 // The entry point for the C++ library startup
39 //
40 //*****************************************************************************
41 extern "C" {
42     extern void __libc_init_array(void);
43 }
44 #endif
45 #endif
46 
47 #define WEAK __attribute__ ((weak))
48 #define ALIAS(f) __attribute__ ((weak, alias (#f)))
49 
50 //*****************************************************************************
51 #if defined (__cplusplus)
52 extern "C" {
53 #endif
54 
55 //*****************************************************************************
56 #if defined (__USE_CMSIS) || defined (__USE_LPCOPEN)
57 // Declaration of external SystemInit function
58 extern void SystemInit(void);
59 #endif
60 
61 //*****************************************************************************
62 //
63 // Forward declaration of the default handlers. These are aliased.
64 // When the application defines a handler (with the same name), this will
65 // automatically take precedence over these weak definitions
66 //
67 //*****************************************************************************
68 void ResetISR(void);
69 WEAK void NMI_Handler(void);
70 WEAK void HardFault_Handler(void);
71 WEAK void MemManage_Handler(void);
72 WEAK void BusFault_Handler(void);
73 WEAK void UsageFault_Handler(void);
74 WEAK void SVC_Handler(void);
75 WEAK void DebugMon_Handler(void);
76 WEAK void PendSV_Handler(void);
77 WEAK void SysTick_Handler(void);
78 WEAK void IntDefaultHandler(void);
79 
80 //*****************************************************************************
81 //
82 // Forward declaration of the specific IRQ handlers. These are aliased
83 // to the IntDefaultHandler, which is a 'forever' loop. When the application
84 // defines a handler (with the same name), this will automatically take
85 // precedence over these weak definitions
86 //
87 //*****************************************************************************
88 
89 void WDT_IRQHandler(void) ALIAS(IntDefaultHandler);
90 void BOD_IRQHandler(void) ALIAS(IntDefaultHandler);
91 void FMC_IRQHandler(void) ALIAS(IntDefaultHandler);
92 void EEPROM_IRQHandler(void) ALIAS(IntDefaultHandler);
93 void DMA_IRQHandler(void) ALIAS(IntDefaultHandler);
94 void GINT0_IRQHandler(void) ALIAS(IntDefaultHandler);
95 void GINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
96 void PIN_INT0_IRQHandler(void) ALIAS(IntDefaultHandler);
97 void PIN_INT1_IRQHandler(void) ALIAS(IntDefaultHandler);
98 void PIN_INT2_IRQHandler(void) ALIAS(IntDefaultHandler);
99 void PIN_INT3_IRQHandler(void) ALIAS(IntDefaultHandler);
100 void PIN_INT4_IRQHandler(void) ALIAS(IntDefaultHandler);
101 void PIN_INT5_IRQHandler(void) ALIAS(IntDefaultHandler);
102 void PIN_INT6_IRQHandler(void) ALIAS(IntDefaultHandler);
103 void PIN_INT7_IRQHandler(void) ALIAS(IntDefaultHandler);
104 void RIT_IRQHandler(void) ALIAS(IntDefaultHandler);
105 void SCT0_IRQHandler(void) ALIAS(IntDefaultHandler);
106 void SCT1_IRQHandler(void) ALIAS(IntDefaultHandler);
107 void SCT2_IRQHandler(void) ALIAS(IntDefaultHandler);
108 void SCT3_IRQHandler(void) ALIAS(IntDefaultHandler);
109 void MRT_IRQHandler(void) ALIAS(IntDefaultHandler);
110 void UART0_IRQHandler(void) ALIAS(IntDefaultHandler);
111 void UART1_IRQHandler(void) ALIAS(IntDefaultHandler);
112 void UART2_IRQHandler(void) ALIAS(IntDefaultHandler);
113 void I2C0_IRQHandler(void) ALIAS(IntDefaultHandler);
114 void SPI0_IRQHandler(void) ALIAS(IntDefaultHandler);
115 void SPI1_IRQHandler(void) ALIAS(IntDefaultHandler);
116 void CAN_IRQHandler(void) ALIAS(IntDefaultHandler);
117 void USB_IRQHandler(void) ALIAS(IntDefaultHandler);
118 void USB_FIQHandler(void) ALIAS(IntDefaultHandler);
119 void USBWakeup_IRQHandler(void) ALIAS(IntDefaultHandler);
120 void ADC0A_IRQHandler(void) ALIAS(IntDefaultHandler);
121 void ADC0B_IRQHandler(void) ALIAS(IntDefaultHandler);
122 void ADC0_THCMP_IRQHandler(void) ALIAS(IntDefaultHandler);
123 void ADC0_OVR_IRQHandler(void) ALIAS(IntDefaultHandler);
124 void ADC1A_IRQHandler(void) ALIAS(IntDefaultHandler);
125 void ADC1B_IRQHandler(void) ALIAS(IntDefaultHandler);
126 void ADC1_THCMP_IRQHandler(void) ALIAS(IntDefaultHandler);
127 void ADC1_OVR_IRQHandler(void) ALIAS(IntDefaultHandler);
128 void DAC_IRQHandler(void) ALIAS(IntDefaultHandler);
129 void ACMP0_IRQHandler(void) ALIAS(IntDefaultHandler);
130 void ACMP1_IRQHandler(void) ALIAS(IntDefaultHandler);
131 void ACMP2_IRQHandler(void) ALIAS(IntDefaultHandler);
132 void ACMP3_IRQHandler(void) ALIAS(IntDefaultHandler);
133 void QEI_IRQHandler(void) ALIAS(IntDefaultHandler);
134 void RTC_ALARM_IRQHandler(void) ALIAS(IntDefaultHandler);
135 void RTC_WAKE_IRQHandler(void) ALIAS(IntDefaultHandler);
136 
137 //*****************************************************************************
138 //
139 // The entry point for the application.
140 // __main() is the entry point for Redlib based applications
141 // main() is the entry point for Newlib based applications
142 //
143 //*****************************************************************************
144 #if defined (__REDLIB__)
145 extern void __main(void);
146 #endif
147 extern int main(void);
148 //*****************************************************************************
149 //
150 // External declaration for the pointer to the stack top from the Linker Script
151 //
152 //*****************************************************************************
153 extern void _vStackTop(void);
154 
155 //*****************************************************************************
156 #if defined (__cplusplus)
157 } // extern "C"
158 #endif
159 //*****************************************************************************
160 //
161 // The vector table.
162 // This relies on the linker script to place at correct location in memory.
163 //
164 //*****************************************************************************
165 extern void (* const g_pfnVectors[])(void);
166 __attribute__ ((section(".isr_vector")))
167 void (* const g_pfnVectors[])(void) = {
168 	// Core Level - CM3
169 	&_vStackTop,                       // The initial stack pointer
170 	ResetISR,                          // The reset handler
171 	NMI_Handler,                       // The NMI handler
172 	HardFault_Handler,                 // The hard fault handler
173 	MemManage_Handler,                 // The MPU fault handler
174 	BusFault_Handler,                  // The bus fault handler
175 	UsageFault_Handler,                // The usage fault handler
176 	0,                                 // Reserved
177 	0,                                 // Reserved
178 	0,                                 // Reserved
179 	0,                                 // Reserved
180 	SVC_Handler,                       // SVCall handler
181 	DebugMon_Handler,                  // Debug monitor handler
182 	0,                                 // Reserved
183 	PendSV_Handler,                    // The PendSV handler
184 	SysTick_Handler,                   // The SysTick handler
185 
186     // Chip Level - LPC15xx
187     WDT_IRQHandler,                    //  0 - Windowed watchdog timer
188     BOD_IRQHandler,                    //  1 - BOD
189     FMC_IRQHandler,                    //  2 - Flash controller
190     EEPROM_IRQHandler,                 //  3 - EEPROM controller
191     DMA_IRQHandler,                    //  4 - DMA
192     GINT0_IRQHandler,                  //  5 - GINT0
193     GINT1_IRQHandler,                  //  6 - GINT1
194     PIN_INT0_IRQHandler,               //  7 - PIO INT0
195     PIN_INT1_IRQHandler,               //  8 - PIO INT1
196     PIN_INT2_IRQHandler,               //  9 - PIO INT2
197     PIN_INT3_IRQHandler,               // 10 - PIO INT3
198     PIN_INT4_IRQHandler,               // 11 - PIO INT4
199     PIN_INT5_IRQHandler,               // 12 - PIO INT5
200     PIN_INT6_IRQHandler,               // 13 - PIO INT6
201     PIN_INT7_IRQHandler,               // 14 - PIO INT7
202     RIT_IRQHandler,                    // 15 - RIT
203     SCT0_IRQHandler,                   // 16 - State configurable timer
204     SCT1_IRQHandler,                   // 17 - State configurable timer
205     SCT2_IRQHandler,                   // 18 - State configurable timer
206     SCT3_IRQHandler,                   // 19 - State configurable timer
207     MRT_IRQHandler,                    // 20 - Multi-Rate Timer
208     UART0_IRQHandler,                  // 21 - UART0
209     UART1_IRQHandler,                  // 22 - UART1
210     UART2_IRQHandler,                  // 23 - UART2
211     I2C0_IRQHandler,                   // 24 - I2C0 controller
212     SPI0_IRQHandler,                   // 25 - SPI0 controller
213     SPI1_IRQHandler,                   // 26 - SPI1 controller
214     CAN_IRQHandler,                    // 27 - C_CAN0
215     USB_IRQHandler,                    // 28 - USB IRQ
216     USB_FIQHandler,                    // 29 - USB FIQ
217     USBWakeup_IRQHandler,              // 30 - USB wake-up
218     ADC0A_IRQHandler,              // 31 - ADC0 sequence A completion
219     ADC0B_IRQHandler,              // 32 - ADC0 sequence B completion
220     ADC0_THCMP_IRQHandler,             // 33 - ADC0 threshold compare
221     ADC0_OVR_IRQHandler,               // 34 - ADC0 overrun
222     ADC1A_IRQHandler,              // 35 - ADC1 sequence A completion
223     ADC1B_IRQHandler,              // 36 - ADC1 sequence B completion
224     ADC1_THCMP_IRQHandler,             // 37 - ADC1 threshold compare
225     ADC1_OVR_IRQHandler,               // 38 - ADC1 overrun
226     DAC_IRQHandler,                    // 39 - DAC
227     ACMP0_IRQHandler,                   // 40 - Analog Comparator 0
228     ACMP1_IRQHandler,                   // 41 - Analog Comparator 1
229     ACMP2_IRQHandler,                   // 42 - Analog Comparator 2
230     ACMP3_IRQHandler,                   // 43 - Analog Comparator 3
231     QEI_IRQHandler,                    // 44 - QEI
232     RTC_ALARM_IRQHandler,              // 45 - RTC alarm
233     RTC_WAKE_IRQHandler,               // 46 - RTC wake-up
234 
235 }; /* End of g_pfnVectors */
236 
237 //*****************************************************************************
238 // Functions to carry out the initialization of RW and BSS data sections. These
239 // are written as separate functions rather than being inlined within the
240 // ResetISR() function in order to cope with MCUs with multiple banks of
241 // memory.
242 //*****************************************************************************
243 __attribute__ ((section(".after_vectors")))
data_init(unsigned int romstart,unsigned int start,unsigned int len)244 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
245     unsigned int *pulDest = (unsigned int*) start;
246     unsigned int *pulSrc = (unsigned int*) romstart;
247     unsigned int loop;
248     for (loop = 0; loop < len; loop = loop + 4)
249         *pulDest++ = *pulSrc++;
250 }
251 
252 __attribute__ ((section(".after_vectors")))
bss_init(unsigned int start,unsigned int len)253 void bss_init(unsigned int start, unsigned int len) {
254     unsigned int *pulDest = (unsigned int*) start;
255     unsigned int loop;
256     for (loop = 0; loop < len; loop = loop + 4)
257         *pulDest++ = 0;
258 }
259 
260 //*****************************************************************************
261 // The following symbols are constructs generated by the linker, indicating
262 // the location of various points in the "Global Section Table". This table is
263 // created by the linker via the Code Red managed linker script mechanism. It
264 // contains the load address, execution address and length of each RW data
265 // section and the execution and length of each BSS (zero initialized) section.
266 //*****************************************************************************
267 extern unsigned int __data_section_table;
268 extern unsigned int __data_section_table_end;
269 extern unsigned int __bss_section_table;
270 extern unsigned int __bss_section_table_end;
271 
272 
273 //*****************************************************************************
274 // Reset entry point for your code.
275 // Sets up a simple runtime environment and initializes the C/C++
276 // library.
277 //*****************************************************************************
278 __attribute__ ((section(".after_vectors")))
279 void
ResetISR(void)280 ResetISR(void) {
281 
282     //
283     // Copy the data sections from flash to SRAM.
284     //
285     unsigned int LoadAddr, ExeAddr, SectionLen;
286     unsigned int *SectionTableAddr;
287 
288     // Load base address of Global Section Table
289     SectionTableAddr = &__data_section_table;
290 
291     // Copy the data sections from flash to SRAM.
292     while (SectionTableAddr < &__data_section_table_end) {
293         LoadAddr = *SectionTableAddr++;
294         ExeAddr = *SectionTableAddr++;
295         SectionLen = *SectionTableAddr++;
296         data_init(LoadAddr, ExeAddr, SectionLen);
297     }
298     // At this point, SectionTableAddr = &__bss_section_table;
299     // Zero fill the bss segment
300     while (SectionTableAddr < &__bss_section_table_end) {
301         ExeAddr = *SectionTableAddr++;
302         SectionLen = *SectionTableAddr++;
303         bss_init(ExeAddr, SectionLen);
304     }
305 #if defined (__USE_CMSIS) || defined (__USE_LPCOPEN)
306     SystemInit();
307 #endif
308 
309 #if defined (__cplusplus)
310     //
311     // Call C++ library initialisation
312     //
313     __libc_init_array();
314 #endif
315 
316 #if defined (__REDLIB__)
317     // Call the Redlib library, which in turn calls main()
318     __main() ;
319 #else
320     main();
321 #endif
322 
323     //
324     // main() shouldn't return, but if it does, we'll just enter an infinite loop
325     //
326     while (1) {
327         ;
328     }
329 }
330 
331 //*****************************************************************************
332 // Default exception handlers. Override the ones here by defining your own
333 // handler routines in your application code.
334 //*****************************************************************************
335 __attribute__ ((section(".after_vectors")))
NMI_Handler(void)336 void NMI_Handler(void)
337 { while(1) {}
338 }
339 
340 __attribute__ ((section(".after_vectors")))
HardFault_Handler(void)341 void HardFault_Handler(void)
342 { while(1) {}
343 }
344 
345 __attribute__ ((section(".after_vectors")))
MemManage_Handler(void)346 void MemManage_Handler(void)
347 { while(1) {}
348 }
349 
350 __attribute__ ((section(".after_vectors")))
BusFault_Handler(void)351 void BusFault_Handler(void)
352 { while(1) {}
353 }
354 
355 __attribute__ ((section(".after_vectors")))
UsageFault_Handler(void)356 void UsageFault_Handler(void)
357 { while(1) {}
358 }
359 
360 __attribute__ ((section(".after_vectors")))
SVC_Handler(void)361 void SVC_Handler(void)
362 { while(1) {}
363 }
364 
365 __attribute__ ((section(".after_vectors")))
DebugMon_Handler(void)366 void DebugMon_Handler(void)
367 { while(1) {}
368 }
369 
370 __attribute__ ((section(".after_vectors")))
PendSV_Handler(void)371 void PendSV_Handler(void)
372 { while(1) {}
373 }
374 
375 __attribute__ ((section(".after_vectors")))
SysTick_Handler(void)376 void SysTick_Handler(void)
377 { while(1) {}
378 }
379 
380 //*****************************************************************************
381 //
382 // Processor ends up here if an unexpected interrupt occurs or a specific
383 // handler is not present in the application code.
384 //
385 //*****************************************************************************
386 __attribute__ ((section(".after_vectors")))
IntDefaultHandler(void)387 void IntDefaultHandler(void)
388 { while(1) {}
389 }
390 
391 
392 
393