| LPCOpen Platform v1.03 LPCOpen Platform for NXP LPC Microcontrollers

- Main Page
- Modules
- Data Structures
- Files
- Related Pages

- File List
- Globals

All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cr_startup_lpc18xx43xx.c
Go to the documentation of this file.
1 // *****************************************************************************
2 // +--+
3 // | ++----+
4 // +-++ |
5 // | |
6 // +-+--+ |
7 // | +--+--+
8 // +----+ Copyright (c) 2011-12 Code Red Technologies Ltd.
9 //
10 // LPC43xx Microcontroller Startup code for use with Red Suite
11 //
12 // Version : 120430
13 //
14 // Software License Agreement
15 //
16 // The software is owned by Code Red Technologies and/or its suppliers, and is
17 // protected under applicable copyright laws. All rights are reserved. Any
18 // use in violation of the foregoing restrictions may subject the user to criminal
19 // sanctions under applicable laws, as well as to civil liability for the breach
20 // of the terms and conditions of this license.
21 //
22 // THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
23 // OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
24 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
25 // USE OF THIS SOFTWARE FOR COMMERCIAL DEVELOPMENT AND/OR EDUCATION IS SUBJECT
26 // TO A CURRENT END USER LICENSE AGREEMENT (COMMERCIAL OR EDUCATIONAL) WITH
27 // CODE RED TECHNOLOGIES LTD.
28 //
29 // *****************************************************************************
30 #if defined(__cplusplus)
31 #ifdef __REDLIB__
32 #error Redlib does not support C++
33 #else
34 // *****************************************************************************
35 //
36 // The entry point for the C++ library startup
37 //
38 // *****************************************************************************
39 extern "C" {
40 extern void __libc_init_array(void);
41
42 }
43 #endif
44 #endif
45
46 #include "lpc43xx_dualcore_config.h"
47
48 #define WEAK __attribute__ ((weak))
49 #define ALIAS(f) __attribute__ ((weak, alias(# f)))
50
51 // Code Red - if CMSIS is being used, then SystemInit() routine
52 // will be called by startup code rather than in application's main()
53 extern void SystemInit(void);
54
55 #if defined(OS_UCOS_III)
56 extern void OS_CPU_PendSVHandler(void);
57 extern void OS_CPU_SysTickHandler (void);
58 #endif
59
60 // *****************************************************************************
61 #if defined(__cplusplus)
62 extern "C" {
63 #endif
64
65 // *****************************************************************************
66 //
67 // Forward declaration of the default handlers. These are aliased.
68 // When the application defines a handler (with the same name), this will
69 // automatically take precedence over these weak definitions
70 //
71 // *****************************************************************************
72 void ResetISR(void);
73 WEAK void NMI_Handler(void);
74 WEAK void HardFault_Handler(void);
75 WEAK void MemManage_Handler(void);
76 WEAK void BusFault_Handler(void);
77 WEAK void UsageFault_Handler(void);
78 WEAK void SVC_Handler(void);
79 WEAK void DebugMon_Handler(void);
80 WEAK void PendSV_Handler(void);
81 WEAK void SysTick_Handler(void);
82 WEAK void IntDefaultHandler(void);
83
84 // *****************************************************************************
85 //
86 // Forward declaration of the specific IRQ handlers. These are aliased
87 // to the IntDefaultHandler, which is a 'forever' loop. When the application
88 // defines a handler (with the same name), this will automatically take
89 // precedence over these weak definitions
90 //
91 // *****************************************************************************
92 void DAC_IRQHandler(void) ALIAS(IntDefaultHandler);
93 void MX_CORE_IRQHandler(void) ALIAS(IntDefaultHandler);
94 void DMA_IRQHandler(void) ALIAS(IntDefaultHandler);
95 void FLASHEEPROM_IRQHandler(void) ALIAS(IntDefaultHandler);
96 void ETH_IRQHandler(void) ALIAS(IntDefaultHandler);
97 void SDIO_IRQHandler(void) ALIAS(IntDefaultHandler);
98 void LCD_IRQHandler(void) ALIAS(IntDefaultHandler);
99 void USB0_IRQHandler(void) ALIAS(IntDefaultHandler);
100 void USB1_IRQHandler(void) ALIAS(IntDefaultHandler);
101 void SCT_IRQHandler(void) ALIAS(IntDefaultHandler);
102 void RIT_IRQHandler(void) ALIAS(IntDefaultHandler);
103 void TIMER0_IRQHandler(void) ALIAS(IntDefaultHandler);
104 void TIMER1_IRQHandler(void) ALIAS(IntDefaultHandler);
105 void TIMER2_IRQHandler(void) ALIAS(IntDefaultHandler);
106 void TIMER3_IRQHandler(void) ALIAS(IntDefaultHandler);
107 void MCPWM_IRQHandler(void) ALIAS(IntDefaultHandler);
108 void ADC0_IRQHandler(void) ALIAS(IntDefaultHandler);
109 void I2C0_IRQHandler(void) ALIAS(IntDefaultHandler);
110 void I2C1_IRQHandler(void) ALIAS(IntDefaultHandler);
111 void SPI_IRQHandler (void) ALIAS(IntDefaultHandler);
112 void ADC1_IRQHandler(void) ALIAS(IntDefaultHandler);
113 void SSP0_IRQHandler(void) ALIAS(IntDefaultHandler);
114 void SSP1_IRQHandler(void) ALIAS(IntDefaultHandler);
115 void UART0_IRQHandler(void) ALIAS(IntDefaultHandler);
116 void UART1_IRQHandler(void) ALIAS(IntDefaultHandler);
117 void UART2_IRQHandler(void) ALIAS(IntDefaultHandler);
118 void UART3_IRQHandler(void) ALIAS(IntDefaultHandler);
119 void I2S0_IRQHandler(void) ALIAS(IntDefaultHandler);
120 void I2S1_IRQHandler(void) ALIAS(IntDefaultHandler);
121 void SPIFI_IRQHandler(void) ALIAS(IntDefaultHandler);
122 void SGPIO_IRQHandler(void) ALIAS(IntDefaultHandler);
123 void GPIO0_IRQHandler(void) ALIAS(IntDefaultHandler);
124 void GPIO1_IRQHandler(void) ALIAS(IntDefaultHandler);
125 void GPIO2_IRQHandler(void) ALIAS(IntDefaultHandler);
126 void GPIO3_IRQHandler(void) ALIAS(IntDefaultHandler);
127 void GPIO4_IRQHandler(void) ALIAS(IntDefaultHandler);
128 void GPIO5_IRQHandler(void) ALIAS(IntDefaultHandler);
129 void GPIO6_IRQHandler(void) ALIAS(IntDefaultHandler);
130 void GPIO7_IRQHandler(void) ALIAS(IntDefaultHandler);
131 void GINT0_IRQHandler(void) ALIAS(IntDefaultHandler);
132 void GINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
133 void EVRT_IRQHandler(void) ALIAS(IntDefaultHandler);
134 void CAN1_IRQHandler(void) ALIAS(IntDefaultHandler);
135 void ATIMER_IRQHandler(void) ALIAS(IntDefaultHandler);
136 void RTC_IRQHandler(void) ALIAS(IntDefaultHandler);
137 void WDT_IRQHandler(void) ALIAS(IntDefaultHandler);
138 void CAN0_IRQHandler(void) ALIAS(IntDefaultHandler);
139 void QEI_IRQHandler(void) ALIAS(IntDefaultHandler);
140
141 // *****************************************************************************
142 //
143 // The entry point for the application.
144 // __main() is the entry point for Redlib based applications
145 // main() is the entry point for Newlib based applications
146 //
147 // *****************************************************************************
148 #if defined(__REDLIB__)
149 extern void __main(void);
150
151 #endif
152 extern int main(void);
153
154 // *****************************************************************************
155 //
156 // External declaration for the pointer to the stack top from the Linker Script
157 //
158 // *****************************************************************************
159 extern void _vStackTop(void);
160
161 // *****************************************************************************
162 //
163 // Application can define Stack size (If not defined, default stack size will
164 // used
165 //
166 // *****************************************************************************
167 #ifndef STACK_SIZE
168 #define STACK_SIZE (0x200)
169 #endif
170
171 // *****************************************************************************
172 //
173 // Application can define Heap size (If not defined, default Heap size will
174 // used
175 //
176 // *****************************************************************************
177 #ifndef HEAP_SIZE
178 #define HEAP_SIZE (0x4000)
179 #endif
180
181 unsigned int __vStack[STACK_SIZE / sizeof(unsigned int)] __attribute__((section("STACK,\"aw\",%nobits@")));
182 unsigned int __vHeap[HEAP_SIZE / sizeof(unsigned int)] __attribute__((section("HEAP,\"aw\",%nobits@")));
183
184 // *****************************************************************************
185 #if defined(__cplusplus)
186 } // extern "C"
187 #endif
188 // *****************************************************************************
189 //
190 // The vector table.
191 // This relies on the linker script to place at correct location in memory.
192 //
193 // *****************************************************************************
194 extern void(*const g_pfnVectors[]) (void);
195 __attribute__ ((section(".isr_vector")))
196 void(*const g_pfnVectors[]) (void) = {
197 // Core Level - CM4/CM3
198 &_vStackTop, // The initial stack pointer
199 ResetISR, // The reset handler
200 NMI_Handler, // The NMI handler
201 HardFault_Handler, // The hard fault handler
202 MemManage_Handler, // The MPU fault handler
203 BusFault_Handler, // The bus fault handler
204 UsageFault_Handler, // The usage fault handler
205 0, // Reserved
206 0, // Reserved
207 0, // Reserved
208 0, // Reserved
209 SVC_Handler, // SVCall handler
210 DebugMon_Handler, // Debug monitor handler
211 0, // Reserved
212 #if defined(OS_UCOS_III)
213 OS_CPU_PendSVHandler, // uCOS-III PendSV handler
214 OS_CPU_SysTickHandler, // uCOS-III SysTick handler
215 #else
216 PendSV_Handler, // The PendSV handler
217 SysTick_Handler, // The SysTick handler
218 #endif
219
220 // Chip Level - LPC18xx/43xx
221 DAC_IRQHandler, // 16 D/A Converter
222 MX_CORE_IRQHandler, // 17 CortexM4/M0 (LPC43XX ONLY)
223 DMA_IRQHandler, // 18 General Purpose DMA
224 0, // 19 Reserved
225 FLASHEEPROM_IRQHandler, // 20 ORed flash Bank A, flash Bank B, EEPROM interrupts
226 ETH_IRQHandler, // 21 Ethernet
227 SDIO_IRQHandler, // 22 SD/MMC
228 LCD_IRQHandler, // 23 LCD
229 USB0_IRQHandler, // 24 USB0
230 USB1_IRQHandler, // 25 USB1
231 SCT_IRQHandler, // 26 State Configurable Timer
232 RIT_IRQHandler, // 27 Repetitive Interrupt Timer
233 TIMER0_IRQHandler, // 28 Timer0
234 TIMER1_IRQHandler, // 29 Timer 1
235 TIMER2_IRQHandler, // 30 Timer 2
236 TIMER3_IRQHandler, // 31 Timer 3
237 MCPWM_IRQHandler, // 32 Motor Control PWM
238 ADC0_IRQHandler, // 33 A/D Converter 0
239 I2C0_IRQHandler, // 34 I2C0
240 I2C1_IRQHandler, // 35 I2C1
241 SPI_IRQHandler, // 36 SPI (LPC43XX ONLY)
242 ADC1_IRQHandler, // 37 A/D Converter 1
243 SSP0_IRQHandler, // 38 SSP0
244 SSP1_IRQHandler, // 39 SSP1
245 UART0_IRQHandler, // 40 UART0
246 UART1_IRQHandler, // 41 UART1
247 UART2_IRQHandler, // 42 UART2
248 UART3_IRQHandler, // 43 USRT3
249 I2S0_IRQHandler, // 44 I2S0
250 I2S1_IRQHandler, // 45 I2S1
251 SPIFI_IRQHandler, // 46 SPI Flash Interface
252 SGPIO_IRQHandler, // 47 SGPIO (LPC43XX ONLY)
253 GPIO0_IRQHandler, // 48 GPIO0
254 GPIO1_IRQHandler, // 49 GPIO1
255 GPIO2_IRQHandler, // 50 GPIO2
256 GPIO3_IRQHandler, // 51 GPIO3
257 GPIO4_IRQHandler, // 52 GPIO4
258 GPIO5_IRQHandler, // 53 GPIO5
259 GPIO6_IRQHandler, // 54 GPIO6
260 GPIO7_IRQHandler, // 55 GPIO7
261 GINT0_IRQHandler, // 56 GINT0
262 GINT1_IRQHandler, // 57 GINT1
263 EVRT_IRQHandler, // 58 Event Router
264 CAN1_IRQHandler, // 59 C_CAN1
265 0, // 60 Reserved
266 0, // 61 Reserved
267 ATIMER_IRQHandler, // 62 ATIMER
268 RTC_IRQHandler, // 63 RTC
269 0, // 64 Reserved
270 WDT_IRQHandler, // 65 WDT
271 0, // 66 Reserved
272 CAN0_IRQHandler, // 67 C_CAN0
273 QEI_IRQHandler, // 68 QEI
274 };
275
276 // *****************************************************************************
277 // Functions to carry out the initialization of RW and BSS data sections. These
278 // are written as separate functions rather than being inlined within the
279 // ResetISR() function in order to cope with MCUs with multiple banks of
280 // memory.
281 // *****************************************************************************
282 __attribute__ ((section(".after_vectors")))
283 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
284 unsigned int *pulDest = (unsigned int *) start;
285 unsigned int *pulSrc = (unsigned int *) romstart;
286 unsigned int loop;
287 for (loop = 0; loop < len; loop = loop + 4)
288 *pulDest++ = *pulSrc++;
289 }
290
291 __attribute__ ((section(".after_vectors")))
292 void bss_init(unsigned int start, unsigned int len) {
293 unsigned int *pulDest = (unsigned int *) start;
294 unsigned int loop;
295 for (loop = 0; loop < len; loop = loop + 4)
296 *pulDest++ = 0;
297 }
298
299 // *****************************************************************************
300 // The following symbols are constructs generated by the linker, indicating
301 // the location of various points in the "Global Section Table". This table is
302 // created by the linker via the Code Red managed linker script mechanism. It
303 // contains the load address, execution address and length of each RW data
304 // section and the execution and length of each BSS (zero initialized) section.
305 // *****************************************************************************
306 extern unsigned int __data_section_table;
307 extern unsigned int __data_section_table_end;
308 extern unsigned int __bss_section_table;
309 extern unsigned int __bss_section_table_end;
310
311 // *****************************************************************************
312 // Reset entry point for your code.
313 // Sets up a simple runtime environment and initializes the C/C++
314 // library.
315 //
316 // *****************************************************************************
317 void
318 ResetISR(void) {
319
320 //
321 // Copy the data sections from flash to SRAM.
322 //
323 unsigned int LoadAddr, ExeAddr, SectionLen;
324 unsigned int *SectionTableAddr;
325
326 /* Call SystemInit() for clocking/memory setup prior to scatter load */
327 SystemInit();
328
329 // Load base address of Global Section Table
330 SectionTableAddr = &__data_section_table;
331
332 // Copy the data sections from flash to SRAM.
333 while (SectionTableAddr < &__data_section_table_end) {
334 LoadAddr = *SectionTableAddr++;
335 ExeAddr = *SectionTableAddr++;
336 SectionLen = *SectionTableAddr++;
337 data_init(LoadAddr, ExeAddr, SectionLen);
338 }
339 // At this point, SectionTableAddr = &__bss_section_table;
340 // Zero fill the bss segment
341 while (SectionTableAddr < &__bss_section_table_end) {
342 ExeAddr = *SectionTableAddr++;
343 SectionLen = *SectionTableAddr++;
344 bss_init(ExeAddr, SectionLen);
345 }
346
347 #if defined(__cplusplus)
348 //
349 // Call C++ library initialisation
350 //
351 __libc_init_array();
352 #endif
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 // Default exception handlers. Override the ones here by defining your own
369 // handler routines in your application code.
370 // *****************************************************************************
371 __attribute__ ((section(".after_vectors")))
372 void NMI_Handler(void)
373 {
374 while (1) {}
375 }
376
377 __attribute__ ((section(".after_vectors")))
378 void HardFault_Handler(void)
379 {
380 while (1) {}
381 }
382
383 __attribute__ ((section(".after_vectors")))
384 void MemManage_Handler(void)
385 {
386 while (1) {}
387 }
388
389 __attribute__ ((section(".after_vectors")))
390 void BusFault_Handler(void)
391 {
392 while (1) {}
393 }
394
395 __attribute__ ((section(".after_vectors")))
396 void UsageFault_Handler(void)
397 {
398 while (1) {}
399 }
400
401 __attribute__ ((section(".after_vectors")))
402 void SVC_Handler(void)
403 {
404 while (1) {}
405 }
406
407 __attribute__ ((section(".after_vectors")))
408 void DebugMon_Handler(void)
409 {
410 while (1) {}
411 }
412
413 __attribute__ ((section(".after_vectors")))
414 void PendSV_Handler(void)
415 {
416 while (1) {}
417 }
418
419 __attribute__ ((section(".after_vectors")))
420 void SysTick_Handler(void)
421 {
422 while (1) {}
423 }
424
425 // *****************************************************************************
426 //
427 // Processor ends up here if an unexpected interrupt occurs or a specific
428 // handler is not present in the application code.
429 //
430 // *****************************************************************************
431 __attribute__ ((section(".after_vectors")))
432 void IntDefaultHandler(void)
433 {
434 while (1) {}
435 }
436
437 // *****************************************************************************
438 //
439 // Heap overflow check function required by REDLib_V2 library
440 //
441 // *****************************************************************************
442 extern unsigned int *_pvHeapStart;
443 unsigned int __check_heap_overflow (void * new_end_of_heap)
444 {
445 return (new_end_of_heap >= (void *)&__vHeap[HEAP_SIZE/sizeof(unsigned int)]);
446 }
447