RT-AICHIP-sample
cr_startup_lpc13xx.c
[詳解]
1 //*****************************************************************************
2 // LPC13xx Microcontroller Startup code for use with LPCXpresso IDE
3 //
4 // Version : 140113
5 //*****************************************************************************
6 //
7 // Copyright(C) NXP Semiconductors, 2013-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 void I2C_IRQHandler(void) ALIAS(IntDefaultHandler);
89 void TIMER16_0_IRQHandler(void) ALIAS(IntDefaultHandler);
90 void TIMER16_1_IRQHandler(void) ALIAS(IntDefaultHandler);
91 void TIMER32_0_IRQHandler(void) ALIAS(IntDefaultHandler);
92 void TIMER32_1_IRQHandler(void) ALIAS(IntDefaultHandler);
93 #if defined (__USE_LPCOPEN)
94 void SSP0_IRQHandler(void) ALIAS(IntDefaultHandler);
95 #else
96 void SSP_IRQHandler(void) ALIAS(IntDefaultHandler);
97 #endif
98 void UART_IRQHandler(void) ALIAS(IntDefaultHandler);
99 void USB_IRQHandler(void) ALIAS(IntDefaultHandler);
100 void USB_FIQHandler(void) ALIAS(IntDefaultHandler);
101 void ADC_IRQHandler(void) ALIAS(IntDefaultHandler);
102 void WDT_IRQHandler(void) ALIAS(IntDefaultHandler);
103 void BOD_IRQHandler(void) ALIAS(IntDefaultHandler);
104 void FMC_IRQHandler(void) ALIAS(IntDefaultHandler);
105 void PIOINT3_IRQHandler(void) ALIAS(IntDefaultHandler);
106 void PIOINT2_IRQHandler(void) ALIAS(IntDefaultHandler);
107 void PIOINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
108 void PIOINT0_IRQHandler(void) ALIAS(IntDefaultHandler);
109 void WAKEUP_IRQHandler(void) ALIAS(IntDefaultHandler);
110 void SSP1_IRQHandler(void) ALIAS(IntDefaultHandler);
111 
112 //*****************************************************************************
113 //
114 // The entry point for the application.
115 // __main() is the entry point for Redlib based applications
116 // main() is the entry point for Newlib based applications
117 //
118 //*****************************************************************************
119 #if defined (__REDLIB__)
120 extern void __main(void);
121 #else
122 extern int main(void);
123 #endif
124 //*****************************************************************************
125 //
126 // External declaration for the pointer to the stack top from the Linker Script
127 //
128 //*****************************************************************************
129 extern void _vStackTop(void);
130 
131 //*****************************************************************************
132 #if defined (__cplusplus)
133 } // extern "C"
134 #endif
135 //*****************************************************************************
136 //
137 // The vector table. Note that the proper constructs must be placed on this to
138 // ensure that it ends up at physical address 0x0000.0000.
139 //
140 //*****************************************************************************
141 extern void (* const g_pfnVectors[])(void);
142 __attribute__ ((section(".isr_vector")))
143 void (* const g_pfnVectors[])(void) = {
144  // Core Level - CM3
145  &_vStackTop, // The initial stack pointer
146  ResetISR, // The reset handler
147  NMI_Handler, // The NMI handler
148  HardFault_Handler, // The hard fault handler
149  MemManage_Handler, // The MPU fault handler
150  BusFault_Handler, // The bus fault handler
151  UsageFault_Handler, // The usage fault handler
152  0, // Reserved
153  0, // Reserved
154  0, // Reserved
155  0, // Reserved
156  SVC_Handler, // SVCall handler
157  DebugMon_Handler, // Debug monitor handler
158  0, // Reserved
159  PendSV_Handler, // The PendSV handler
160  SysTick_Handler, // The SysTick handler
161 
162 
163  // Wakeup sources (40 ea.) for the I/O pins:
164  // PIO0 (0:11)
165  // PIO1 (0:11)
166  // PIO2 (0:11)
167  // PIO3 (0:3)
168  WAKEUP_IRQHandler, // PIO0_0 Wakeup
169  WAKEUP_IRQHandler, // PIO0_1 Wakeup
170  WAKEUP_IRQHandler, // PIO0_2 Wakeup
171  WAKEUP_IRQHandler, // PIO0_3 Wakeup
172  WAKEUP_IRQHandler, // PIO0_4 Wakeup
173  WAKEUP_IRQHandler, // PIO0_5 Wakeup
174  WAKEUP_IRQHandler, // PIO0_6 Wakeup
175  WAKEUP_IRQHandler, // PIO0_7 Wakeup
176  WAKEUP_IRQHandler, // PIO0_8 Wakeup
177  WAKEUP_IRQHandler, // PIO0_9 Wakeup
178  WAKEUP_IRQHandler, // PIO0_10 Wakeup
179  WAKEUP_IRQHandler, // PIO0_11 Wakeup
180 
181  WAKEUP_IRQHandler, // PIO1_0 Wakeup
182  WAKEUP_IRQHandler, // PIO1_1 Wakeup
183  WAKEUP_IRQHandler, // PIO1_2 Wakeup
184  WAKEUP_IRQHandler, // PIO1_3 Wakeup
185  WAKEUP_IRQHandler, // PIO1_4 Wakeup
186  WAKEUP_IRQHandler, // PIO1_5 Wakeup
187  WAKEUP_IRQHandler, // PIO1_6 Wakeup
188  WAKEUP_IRQHandler, // PIO1_7 Wakeup
189  WAKEUP_IRQHandler, // PIO1_8 Wakeup
190  WAKEUP_IRQHandler, // PIO1_9 Wakeup
191  WAKEUP_IRQHandler, // PIO1_10 Wakeup
192  WAKEUP_IRQHandler, // PIO1_11 Wakeup
193 
194  WAKEUP_IRQHandler, // PIO2_0 Wakeup
195  WAKEUP_IRQHandler, // PIO2_1 Wakeup
196  WAKEUP_IRQHandler, // PIO2_2 Wakeup
197  WAKEUP_IRQHandler, // PIO2_3 Wakeup
198  WAKEUP_IRQHandler, // PIO2_4 Wakeup
199  WAKEUP_IRQHandler, // PIO2_5 Wakeup
200  WAKEUP_IRQHandler, // PIO2_6 Wakeup
201  WAKEUP_IRQHandler, // PIO2_7 Wakeup
202  WAKEUP_IRQHandler, // PIO2_8 Wakeup
203  WAKEUP_IRQHandler, // PIO2_9 Wakeup
204  WAKEUP_IRQHandler, // PIO2_10 Wakeup
205  WAKEUP_IRQHandler, // PIO2_11 Wakeup
206 
207  WAKEUP_IRQHandler, // PIO3_0 Wakeup
208  WAKEUP_IRQHandler, // PIO3_1 Wakeup
209  WAKEUP_IRQHandler, // PIO3_2 Wakeup
210  WAKEUP_IRQHandler, // PIO3_3 Wakeup
211 
212  I2C_IRQHandler, // I2C0
213  TIMER16_0_IRQHandler, // CT16B0 (16-bit Timer 0)
214  TIMER16_1_IRQHandler, // CT16B1 (16-bit Timer 1)
215  TIMER32_0_IRQHandler, // CT32B0 (32-bit Timer 0)
216  TIMER32_1_IRQHandler, // CT32B1 (32-bit Timer 1)
217 #if defined (__USE_LPCOPEN)
218  SSP0_IRQHandler, // SSP0
219 #else
220  SSP_IRQHandler, // SSP0
221 #endif
222  UART_IRQHandler, // UART0
223 
224  USB_IRQHandler, // USB IRQ
225  USB_FIQHandler, // USB FIQ
226 
227  ADC_IRQHandler, // ADC (A/D Converter)
228  WDT_IRQHandler, // WDT (Watchdog Timer)
229  BOD_IRQHandler, // BOD (Brownout Detect)
230  FMC_IRQHandler, // Flash (IP2111 Flash Memory Controller)
231  PIOINT3_IRQHandler, // PIO INT3
232  PIOINT2_IRQHandler, // PIO INT2
233  PIOINT1_IRQHandler, // PIO INT1
234  PIOINT0_IRQHandler, // PIO INT0
235  SSP1_IRQHandler, // SSP1
236 
237  };
238 
239 //*****************************************************************************
240 // Functions to carry out the initialization of RW and BSS data sections. These
241 // are written as separate functions rather than being inlined within the
242 // ResetISR() function in order to cope with MCUs with multiple banks of
243 // memory.
244 //*****************************************************************************
245 __attribute__ ((section(".after_vectors")))
246 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
247  unsigned int *pulDest = (unsigned int*) start;
248  unsigned int *pulSrc = (unsigned int*) romstart;
249  unsigned int loop;
250  for (loop = 0; loop < len; loop = loop + 4)
251  *pulDest++ = *pulSrc++;
252 }
253 
254 __attribute__ ((section(".after_vectors")))
255 void bss_init(unsigned int start, unsigned int len) {
256  unsigned int *pulDest = (unsigned int*) start;
257  unsigned int loop;
258  for (loop = 0; loop < len; loop = loop + 4)
259  *pulDest++ = 0;
260 }
261 
262 //*****************************************************************************
263 // The following symbols are constructs generated by the linker, indicating
264 // the location of various points in the "Global Section Table". This table is
265 // created by the linker via the Code Red managed linker script mechanism. It
266 // contains the load address, execution address and length of each RW data
267 // section and the execution and length of each BSS (zero initialized) section.
268 //*****************************************************************************
269 extern unsigned int __data_section_table;
270 extern unsigned int __data_section_table_end;
271 extern unsigned int __bss_section_table;
272 extern unsigned int __bss_section_table_end;
273 
274 //*****************************************************************************
275 // Reset entry point for your code.
276 // Sets up a simple runtime environment and initializes the C/C++
277 // library.
278 //*****************************************************************************
279 __attribute__ ((section(".after_vectors")))
280 void
281 ResetISR(void) {
282 
283  //
284  // Copy the data sections from flash to SRAM.
285  //
286  unsigned int LoadAddr, ExeAddr, SectionLen;
287  unsigned int *SectionTableAddr;
288 
289  // Load base address of Global Section Table
290  SectionTableAddr = &__data_section_table;
291 
292  // Copy the data sections from flash to SRAM.
293  while (SectionTableAddr < &__data_section_table_end) {
294  LoadAddr = *SectionTableAddr++;
295  ExeAddr = *SectionTableAddr++;
296  SectionLen = *SectionTableAddr++;
297  data_init(LoadAddr, ExeAddr, SectionLen);
298  }
299  // At this point, SectionTableAddr = &__bss_section_table;
300  // Zero fill the bss segment
301  while (SectionTableAddr < &__bss_section_table_end) {
302  ExeAddr = *SectionTableAddr++;
303  SectionLen = *SectionTableAddr++;
304  bss_init(ExeAddr, SectionLen);
305  }
306 
307 #if defined (__USE_CMSIS) || defined (__USE_LPCOPEN)
308  SystemInit();
309 #endif
310 
311 #if defined (__cplusplus)
312  //
313  // Call C++ library initialisation
314  //
315  __libc_init_array();
316 #endif
317 
318 #if defined (__REDLIB__)
319  // Call the Redlib library, which in turn calls main()
320  __main() ;
321 #else
322  main();
323 #endif
324  //
325  // main() shouldn't return, but if it does, we'll just enter an infinite loop
326  //
327  while (1) {
328  ;
329  }
330 }
331 
332 //*****************************************************************************
333 // Default exception handlers. Override the ones here by defining your own
334 // handler routines in your application code.
335 //*****************************************************************************
336 __attribute__ ((section(".after_vectors")))
337 void NMI_Handler(void) {
338  while (1) {
339  }
340 }
341 __attribute__ ((section(".after_vectors")))
342 void HardFault_Handler(void) {
343  while (1) {
344  }
345 }
346 __attribute__ ((section(".after_vectors")))
347 void MemManage_Handler(void) {
348  while (1) {
349  }
350 }
351 __attribute__ ((section(".after_vectors")))
352 void BusFault_Handler(void) {
353  while (1) {
354  }
355 }
356 __attribute__ ((section(".after_vectors")))
357 void UsageFault_Handler(void) {
358  while (1) {
359  }
360 }
361 __attribute__ ((section(".after_vectors")))
362 void SVC_Handler(void) {
363  while (1) {
364  }
365 }
366 __attribute__ ((section(".after_vectors")))
367 void DebugMon_Handler(void) {
368  while (1) {
369  }
370 }
371 __attribute__ ((section(".after_vectors")))
372 void PendSV_Handler(void) {
373  while (1) {
374  }
375 }
376 __attribute__ ((section(".after_vectors")))
377 void SysTick_Handler(void) {
378  while (1) {
379  }
380 }
381 
382 //*****************************************************************************
383 //
384 // Processor ends up here if an unexpected interrupt occurs or a handler
385 // is not present in the application code.
386 //
387 //*****************************************************************************
388 __attribute__ ((section(".after_vectors")))
389 void IntDefaultHandler(void) {
390  //
391  // Go into an infinite loop.
392  //
393  while (1) {
394  }
395 }
unsigned int __bss_section_table_end
#define WEAK
unsigned int __data_section_table_end
WEAK void MemManage_Handler(void)
void I2C_IRQHandler(void TIMER16_0_IRQHandler() ALIAS(IntDefaultHandler) void)
WEAK void UsageFault_Handler(void)
unsigned int __data_section_table
void UART_IRQHandler(void)
Definition: uart.c:32
__attribute__((section(".after_vectors")))
void TIMER32_0_IRQHandler(void)
unsigned int __bss_section_table
void ResetISR(void)
void SSP_IRQHandler(void)
Definition: ssp.c:33
void USB_IRQHandler(void)
Definition: usbhw.c:469
WEAK void SVC_Handler(void)
WEAK void NMI_Handler(void)
WEAK void IntDefaultHandler(void)
#define ALIAS(f)
WEAK void BusFault_Handler(void)
WEAK void HardFault_Handler(void)
WEAK void PendSV_Handler(void)
WEAK void DebugMon_Handler(void)
WEAK void SysTick_Handler(void)
int main(void)
Definition: main.c:48
void TIMER32_1_IRQHandler(void)