LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cr_startup_lpc13xx.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 // Microcontroller Startup code for use with Red Suite
11 //
12 // Version : 120126
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 #endif
43 #endif
44 
45 #define WEAK __attribute__ ((weak))
46 #define ALIAS(f) __attribute__ ((weak, alias (#f)))
47 /* Include sys_config.h to get the CHIP_11* device identifier */
48 #include "sys_config.h"
49 
50 // Code Red - if CMSIS is being used, then SystemInit() routine
51 // will be called by startup code rather than in application's main()
52 extern void SystemInit(void);
53 
54 //*****************************************************************************
55 #if defined (__cplusplus)
56 extern "C" {
57 #endif
58 
59 //*****************************************************************************
60 //
61 // Forward declaration of the default handlers. These are aliased.
62 // When the application defines a handler (with the same name), this will
63 // automatically take precedence over these weak definitions
64 //
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 //
76 // Forward declaration of the specific IRQ handlers. These are aliased
77 // to the IntDefaultHandler, which is a 'forever' loop. When the application
78 // defines a handler (with the same name), this will automatically take
79 // precedence over these weak definitions
80 //
81 //*****************************************************************************
82 
83 #if defined(CHIP_LPC1343)
84 void WAKEUP_IRQHandler (void) ALIAS(IntDefaultHandler);
85 void I2C_IRQHandler (void) ALIAS(IntDefaultHandler);
86 void TIMER16_0_IRQHandler (void) ALIAS(IntDefaultHandler);
87 void TIMER16_1_IRQHandler (void) ALIAS(IntDefaultHandler);
88 void TIMER32_0_IRQHandler (void) ALIAS(IntDefaultHandler);
89 void TIMER32_1_IRQHandler (void) ALIAS(IntDefaultHandler);
90 void SSP0_IRQHandler (void) ALIAS(IntDefaultHandler);
91 void UART_IRQHandler (void) ALIAS(IntDefaultHandler);
92 void USB_IRQHandler (void) ALIAS(IntDefaultHandler);
93 void USB_FIQHandler (void) ALIAS(IntDefaultHandler);
94 void ADC_IRQHandler (void) ALIAS(IntDefaultHandler);
95 void WDT_IRQHandler (void) ALIAS(IntDefaultHandler);
96 void BOD_IRQHandler (void) ALIAS(IntDefaultHandler);
97 void FMC_IRQHandler (void) ALIAS(IntDefaultHandler);
98 void PIOINT3_IRQHandler (void) ALIAS(IntDefaultHandler);
99 void PIOINT2_IRQHandler (void) ALIAS(IntDefaultHandler);
100 void PIOINT1_IRQHandler (void) ALIAS(IntDefaultHandler);
101 void PIOINT0_IRQHandler (void) ALIAS(IntDefaultHandler);
102 
103 #elif defined(CHIP_LPC1347)
104 void PIN_INT0_IRQHandler (void) ALIAS(IntDefaultHandler);
105 void PIN_INT1_IRQHandler (void) ALIAS(IntDefaultHandler);
106 void PIN_INT2_IRQHandler (void) ALIAS(IntDefaultHandler);
107 void PIN_INT3_IRQHandler (void) ALIAS(IntDefaultHandler);
108 void PIN_INT4_IRQHandler (void) ALIAS(IntDefaultHandler);
109 void PIN_INT5_IRQHandler (void) ALIAS(IntDefaultHandler);
110 void PIN_INT6_IRQHandler (void) ALIAS(IntDefaultHandler);
111 void PIN_INT7_IRQHandler (void) ALIAS(IntDefaultHandler);
112 void GINT0_IRQHandler (void) ALIAS(IntDefaultHandler);
113 void GINT1_IRQHandler (void) ALIAS(IntDefaultHandler);
114 void RIT_IRQHandler (void) ALIAS(IntDefaultHandler);
115 void SSP1_IRQHandler (void) ALIAS(IntDefaultHandler);
116 void I2C_IRQHandler (void) ALIAS(IntDefaultHandler);
117 void TIMER16_0_IRQHandler (void) ALIAS(IntDefaultHandler);
118 void TIMER16_1_IRQHandler (void) ALIAS(IntDefaultHandler);
119 void TIMER32_0_IRQHandler (void) ALIAS(IntDefaultHandler);
120 void TIMER32_1_IRQHandler (void) ALIAS(IntDefaultHandler);
121 void SSP0_IRQHandler (void) ALIAS(IntDefaultHandler);
122 void UART_IRQHandler (void) ALIAS(IntDefaultHandler);
123 void USB_IRQHandler (void) ALIAS(IntDefaultHandler);
124 void USB_FIQHandler (void) ALIAS(IntDefaultHandler);
125 void ADC_IRQHandler (void) ALIAS(IntDefaultHandler);
126 void WDT_IRQHandler (void) ALIAS(IntDefaultHandler);
127 void BOD_IRQHandler (void) ALIAS(IntDefaultHandler);
128 void FMC_IRQHandler (void) ALIAS(IntDefaultHandler);
129 void OSCFAIL_IRQHandler (void) ALIAS(IntDefaultHandler);
130 void PVTCIRCUIT_IRQHandler (void) ALIAS(IntDefaultHandler);
131 void USBWakeup_IRQHandler (void) ALIAS(IntDefaultHandler);
132 
133 #else
134 #error No CHIP_134* device defined
135 #endif
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 //
145 // The entry point for the application.
146 // __main() is the entry point for Redlib based applications
147 // main() is the entry point for Newlib based applications
148 //
149 //*****************************************************************************
150 #if defined (__REDLIB__)
151 extern void __main(void);
152 #endif
153 extern int main(void);
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 #if defined (__cplusplus)
163 } // extern "C"
164 #endif
165 
166 //*****************************************************************************
167 //
168 // The vector table. Note that the proper constructs must be placed on this to
169 // ensure that it ends up at physical address 0x0000.0000.
170 //
171 //*****************************************************************************
172 extern void (* const g_pfnVectors[])(void);
173 __attribute__ ((section(".isr_vector")))
174 void (* const g_pfnVectors[])(void) = {
175  &_vStackTop, // The initial stack pointer
176  ResetISR, // The reset handler
177  NMI_Handler, // The NMI handler
178  HardFault_Handler, // The hard fault handler
179  0, // Reserved
180  0, // Reserved
181  0, // Reserved
182  0, // Reserved
183  0, // Reserved
184  0, // Reserved
185  0, // Reserved
186  SVC_Handler, // SVCall handler
187  0, // Reserved
188  0, // Reserved
189  PendSV_Handler, // The PendSV handler
190  SysTick_Handler, // The SysTick handler
191 
192 #if defined(CHIP_LPC1343)
193  WAKEUP_IRQHandler, // Wakeup PIO0.0
194  WAKEUP_IRQHandler, // Wakeup PIO0.1
195  WAKEUP_IRQHandler, // Wakeup PIO0.2
196  WAKEUP_IRQHandler, // Wakeup PIO0.3
197  WAKEUP_IRQHandler, // Wakeup PIO0.4
198  WAKEUP_IRQHandler, // Wakeup PIO0.5
199  WAKEUP_IRQHandler, // Wakeup PIO0.6
200  WAKEUP_IRQHandler, // Wakeup PIO0.7
201  WAKEUP_IRQHandler, // Wakeup PIO0.8
202  WAKEUP_IRQHandler, // Wakeup PIO0.9
203  WAKEUP_IRQHandler, // Wakeup PIO0.10
204  WAKEUP_IRQHandler, // Wakeup PIO0.11
205  WAKEUP_IRQHandler, // Wakeup PIO1.0
206  WAKEUP_IRQHandler, // Wakeup PIO1.1
207  WAKEUP_IRQHandler, // Wakeup PIO1.2
208  WAKEUP_IRQHandler, // Wakeup PIO1.3
209  WAKEUP_IRQHandler, // Wakeup PIO1.4
210  WAKEUP_IRQHandler, // Wakeup PIO1.5
211  WAKEUP_IRQHandler, // Wakeup PIO1.6
212  WAKEUP_IRQHandler, // Wakeup PIO1.7
213  WAKEUP_IRQHandler, // Wakeup PIO1.8
214  WAKEUP_IRQHandler, // Wakeup PIO1.9
215  WAKEUP_IRQHandler, // Wakeup PIO1.10
216  WAKEUP_IRQHandler, // Wakeup PIO1.11
217  WAKEUP_IRQHandler, // Wakeup PIO2.0
218  WAKEUP_IRQHandler, // Wakeup PIO2.1
219  WAKEUP_IRQHandler, // Wakeup PIO2.2
220  WAKEUP_IRQHandler, // Wakeup PIO2.3
221  WAKEUP_IRQHandler, // Wakeup PIO2.4
222  WAKEUP_IRQHandler, // Wakeup PIO2.5
223  WAKEUP_IRQHandler, // Wakeup PIO2.6
224  WAKEUP_IRQHandler, // Wakeup PIO2.7
225  WAKEUP_IRQHandler, // Wakeup PIO2.8
226  WAKEUP_IRQHandler, // Wakeup PIO2.9
227  WAKEUP_IRQHandler, // Wakeup PIO2.10
228  WAKEUP_IRQHandler, // Wakeup PIO2.11
229  WAKEUP_IRQHandler, // Wakeup PIO3.0
230  WAKEUP_IRQHandler, // Wakeup PIO3.1
231  WAKEUP_IRQHandler, // Wakeup PIO3.2
232  WAKEUP_IRQHandler, // Wakeup PIO3.3
233  I2C_IRQHandler, // I2C
234  TIMER16_0_IRQHandler, // 16-bit Counter-Timer 0
235  TIMER16_1_IRQHandler, // 16-bit Counter-Timer 1
236  TIMER32_0_IRQHandler, // 32-bit Counter-Timer 0
237  TIMER32_1_IRQHandler, // 32-bit Counter-Timer 1
238  SSP0_IRQHandler, // SSP0
239  UART_IRQHandler, // UART
240  USB_IRQHandler, // USB IRQ
241  USB_FIQHandler, // USB FIQ
242  ADC_IRQHandler, // A/D Converter
243  WDT_IRQHandler, // Watchdog Timer
244  BOD_IRQHandler, // Brown Out Detect
245  FMC_IRQHandler, // IP2111 Flash Memory Controller
246  PIOINT3_IRQHandler, // PIO INT3
247  PIOINT2_IRQHandler, // PIO INT2
248  PIOINT1_IRQHandler, // PIO INT1
249  PIOINT0_IRQHandler, // PIO INT0
250 
251 #elif defined(CHIP_LPC1347)
252  PIN_INT0_IRQHandler, // All GPIO pin can be routed to PIN_INTx
253  PIN_INT1_IRQHandler,
254  PIN_INT2_IRQHandler,
255  PIN_INT3_IRQHandler,
256  PIN_INT4_IRQHandler,
257  PIN_INT5_IRQHandler,
258  PIN_INT6_IRQHandler,
259  PIN_INT7_IRQHandler,
260  GINT0_IRQHandler,
261  GINT1_IRQHandler, // PIO0 (0:7)
262  0,
263  0,
265  0,
266  SSP1_IRQHandler, // SSP1
267  I2C_IRQHandler, // I2C
268  TIMER16_0_IRQHandler, // 16-bit Counter-Timer 0
269  TIMER16_1_IRQHandler, // 16-bit Counter-Timer 1
270  TIMER32_0_IRQHandler, // 32-bit Counter-Timer 0
271  TIMER32_1_IRQHandler, // 32-bit Counter-Timer 1
272  SSP0_IRQHandler, // SSP0
273  UART_IRQHandler, // UART
274  USB_IRQHandler, // USB IRQ
275  USB_FIQHandler, // USB FIQ
276  ADC_IRQHandler, // A/D Converter
277  WDT_IRQHandler, // Watchdog Timer
278  BOD_IRQHandler, // Brown Out Detect
279  FMC_IRQHandler, // IP2111 Flash Memory Controller
280  OSCFAIL_IRQHandler, // OSC FAIL
281  PVTCIRCUIT_IRQHandler, // PVT CIRCUIT
282  USBWakeup_IRQHandler, // USB wake up
283  0,
284 
285 #else
286 #error No CHIP_13* device defined
287 #endif
288 };
289 
290 //*****************************************************************************
291 // Functions to carry out the initialization of RW and BSS data sections. These
292 // are written as separate functions rather than being inlined within the
293 // ResetISR() function in order to cope with MCUs with multiple banks of
294 // memory.
295 //*****************************************************************************
296 __attribute__ ((section(".after_vectors")))
297 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
298  unsigned int *pulDest = (unsigned int*) start;
299  unsigned int *pulSrc = (unsigned int*) romstart;
300  unsigned int loop;
301  for (loop = 0; loop < len; loop = loop + 4)
302  *pulDest++ = *pulSrc++;
303 }
304 
305 __attribute__ ((section(".after_vectors")))
306 void bss_init(unsigned int start, unsigned int len) {
307  unsigned int *pulDest = (unsigned int*) start;
308  unsigned int loop;
309  for (loop = 0; loop < len; loop = loop + 4)
310  *pulDest++ = 0;
311 }
312 
313 #ifndef USE_OLD_STYLE_DATA_BSS_INIT
314 //*****************************************************************************
315 // The following symbols are constructs generated by the linker, indicating
316 // the location of various points in the "Global Section Table". This table is
317 // created by the linker via the Code Red managed linker script mechanism. It
318 // contains the load address, execution address and length of each RW data
319 // section and the execution and length of each BSS (zero initialized) section.
320 //*****************************************************************************
321 extern unsigned int __data_section_table;
322 extern unsigned int __data_section_table_end;
323 extern unsigned int __bss_section_table;
324 extern unsigned int __bss_section_table_end;
325 #else
326 //*****************************************************************************
327 // The following symbols are constructs generated by the linker, indicating
328 // the load address, execution address and length of the RW data section and
329 // the execution and length of the BSS (zero initialized) section.
330 // Note that these symbols are not normally used by the managed linker script
331 // mechanism in Red Suite/LPCXpresso 3.6 (Windows) and LPCXpresso 3.8 (Linux).
332 // They are provide here simply so this startup code can be used with earlier
333 // versions of Red Suite which do not support the more advanced managed linker
334 // script mechanism introduced in the above version. To enable their use,
335 // define "USE_OLD_STYLE_DATA_BSS_INIT".
336 //*****************************************************************************
337 extern unsigned int _etext;
338 extern unsigned int _data;
339 extern unsigned int _edata;
340 extern unsigned int _bss;
341 extern unsigned int _ebss;
342 #endif
343 
344 
345 //*****************************************************************************
346 // Reset entry point for your code.
347 // Sets up a simple runtime environment and initializes the C/C++
348 // library.
349 //*****************************************************************************
350 __attribute__ ((section(".after_vectors")))
351 void
352 ResetISR(void) {
353 
354 #ifndef USE_OLD_STYLE_DATA_BSS_INIT
355  //
356  // Copy the data sections from flash to SRAM.
357  //
358  unsigned int LoadAddr, ExeAddr, SectionLen;
359  unsigned int *SectionTableAddr;
360 
361  // Load base address of Global Section Table
362  SectionTableAddr = &__data_section_table;
363 
364  // Copy the data sections from flash to SRAM.
365  while (SectionTableAddr < &__data_section_table_end) {
366  LoadAddr = *SectionTableAddr++;
367  ExeAddr = *SectionTableAddr++;
368  SectionLen = *SectionTableAddr++;
369  data_init(LoadAddr, ExeAddr, SectionLen);
370  }
371  // At this point, SectionTableAddr = &__bss_section_table;
372  // Zero fill the bss segment
373  while (SectionTableAddr < &__bss_section_table_end) {
374  ExeAddr = *SectionTableAddr++;
375  SectionLen = *SectionTableAddr++;
376  bss_init(ExeAddr, SectionLen);
377  }
378 #else
379  // Use Old Style Data and BSS section initialization.
380  // This will only initialize a single RAM bank.
381  unsigned int * LoadAddr, *ExeAddr, *EndAddr, SectionLen;
382 
383  // Copy the data segment from flash to SRAM.
384  LoadAddr = &_etext;
385  ExeAddr = &_data;
386  EndAddr = &_edata;
387  SectionLen = (void*)EndAddr - (void*)ExeAddr;
388  data_init((unsigned int)LoadAddr, (unsigned int)ExeAddr, SectionLen);
389  // Zero fill the bss segment
390  ExeAddr = &_bss;
391  EndAddr = &_ebss;
392  SectionLen = (void*)EndAddr - (void*)ExeAddr;
393  bss_init ((unsigned int)ExeAddr, SectionLen);
394 #endif
395 
396  extern void SystemInit(void);
397  SystemInit();
398 
399 #if defined (__cplusplus)
400  //
401  // Call C++ library initialisation
402  //
403  __libc_init_array();
404 #endif
405 
406 #if defined (__REDLIB__)
407  // Call the Redlib library, which in turn calls main()
408  __main() ;
409 #else
410  main();
411 #endif
412  //
413  // main() shouldn't return, but if it does, we'll just enter an infinite loop
414  //
415  while (1) {
416  ;
417  }
418 }
419 
420 //*****************************************************************************
421 // Default exception handlers. Override the ones here by defining your own
422 // handler routines in your application code.
423 //*****************************************************************************
424 __attribute__ ((section(".after_vectors")))
425 void NMI_Handler(void)
426 {
427  while(1)
428  {
429  }
430 }
431 __attribute__ ((section(".after_vectors")))
432 void HardFault_Handler(void)
433 {
434  while(1)
435  {
436  }
437 }
438 __attribute__ ((section(".after_vectors")))
439 void SVC_Handler(void)
440 {
441  while(1)
442  {
443  }
444 }
445 __attribute__ ((section(".after_vectors")))
446 void PendSV_Handler(void)
447 {
448  while(1)
449  {
450  }
451 }
452 __attribute__ ((section(".after_vectors")))
453 void SysTick_Handler(void)
454 {
455  while(1)
456  {
457  }
458 }
459 
460 //*****************************************************************************
461 //
462 // Processor ends up here if an unexpected interrupt occurs or a specific
463 // handler is not present in the application code.
464 //
465 //*****************************************************************************
466 __attribute__ ((section(".after_vectors")))
467 void IntDefaultHandler(void)
468 {
469  while(1)
470  {
471  }
472 }
473