LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 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 #define WEAK __attribute__ ((weak))
47 #define ALIAS(f) __attribute__ ((weak, alias(# f)))
48 
49 // Code Red - if CMSIS is being used, then SystemInit() routine
50 // will be called by startup code rather than in application's main()
51 extern void SystemInit(void);
52 
53 // *****************************************************************************
54 #if defined(__cplusplus)
55 extern "C" {
56 #endif
57 
58 // *****************************************************************************
59 //
60 // Forward declaration of the default handlers. These are aliased.
61 // When the application defines a handler (with the same name), this will
62 // automatically take precedence over these weak definitions
63 //
64 // *****************************************************************************
65 void ResetISR(void);
66 WEAK void NMI_Handler(void);
67 WEAK void HardFault_Handler(void);
68 WEAK void MemManage_Handler(void);
69 WEAK void BusFault_Handler(void);
70 WEAK void UsageFault_Handler(void);
71 WEAK void SVC_Handler(void);
72 WEAK void DebugMon_Handler(void);
73 WEAK void PendSV_Handler(void);
74 WEAK void SysTick_Handler(void);
75 WEAK void IntDefaultHandler(void);
76 
77 // *****************************************************************************
78 //
79 // Forward declaration of the specific IRQ handlers. These are aliased
80 // to the IntDefaultHandler, which is a 'forever' loop. When the application
81 // defines a handler (with the same name), this will automatically take
82 // precedence over these weak definitions
83 //
84 // *****************************************************************************
86 void MX_CORE_IRQHandler(void) ALIAS(IntDefaultHandler);
87 void DMA_IRQHandler(void) ALIAS(IntDefaultHandler);
88 void FLASHEEPROM_IRQHandler(void) ALIAS(IntDefaultHandler);
89 void ETH_IRQHandler(void) ALIAS(IntDefaultHandler);
90 void SDIO_IRQHandler(void) ALIAS(IntDefaultHandler);
91 void LCD_IRQHandler(void) ALIAS(IntDefaultHandler);
92 void USB0_IRQHandler(void) ALIAS(IntDefaultHandler);
93 void USB1_IRQHandler(void) ALIAS(IntDefaultHandler);
94 void SCT_IRQHandler(void) ALIAS(IntDefaultHandler);
95 void RIT_IRQHandler(void) ALIAS(IntDefaultHandler);
96 void TIMER0_IRQHandler(void) ALIAS(IntDefaultHandler);
97 void TIMER1_IRQHandler(void) ALIAS(IntDefaultHandler);
98 void TIMER2_IRQHandler(void) ALIAS(IntDefaultHandler);
99 void TIMER3_IRQHandler(void) ALIAS(IntDefaultHandler);
100 void MCPWM_IRQHandler(void) ALIAS(IntDefaultHandler);
101 void ADC0_IRQHandler(void) ALIAS(IntDefaultHandler);
102 void I2C0_IRQHandler(void) ALIAS(IntDefaultHandler);
103 void I2C1_IRQHandler(void) ALIAS(IntDefaultHandler);
104 void SPI_IRQHandler (void) ALIAS(IntDefaultHandler);
105 void ADC1_IRQHandler(void) ALIAS(IntDefaultHandler);
106 void SSP0_IRQHandler(void) ALIAS(IntDefaultHandler);
107 void SSP1_IRQHandler(void) ALIAS(IntDefaultHandler);
108 void UART0_IRQHandler(void) ALIAS(IntDefaultHandler);
109 void UART1_IRQHandler(void) ALIAS(IntDefaultHandler);
110 void UART2_IRQHandler(void) ALIAS(IntDefaultHandler);
111 void UART3_IRQHandler(void) ALIAS(IntDefaultHandler);
112 void I2S0_IRQHandler(void) ALIAS(IntDefaultHandler);
113 void I2S1_IRQHandler(void) ALIAS(IntDefaultHandler);
114 void SPIFI_IRQHandler(void) ALIAS(IntDefaultHandler);
115 void SGPIO_IRQHandler(void) ALIAS(IntDefaultHandler);
116 void GPIO0_IRQHandler(void) ALIAS(IntDefaultHandler);
117 void GPIO1_IRQHandler(void) ALIAS(IntDefaultHandler);
118 void GPIO2_IRQHandler(void) ALIAS(IntDefaultHandler);
119 void GPIO3_IRQHandler(void) ALIAS(IntDefaultHandler);
120 void GPIO4_IRQHandler(void) ALIAS(IntDefaultHandler);
121 void GPIO5_IRQHandler(void) ALIAS(IntDefaultHandler);
122 void GPIO6_IRQHandler(void) ALIAS(IntDefaultHandler);
123 void GPIO7_IRQHandler(void) ALIAS(IntDefaultHandler);
124 void GINT0_IRQHandler(void) ALIAS(IntDefaultHandler);
125 void GINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
126 void EVRT_IRQHandler(void) ALIAS(IntDefaultHandler);
127 void CAN1_IRQHandler(void) ALIAS(IntDefaultHandler);
128 void ATIMER_IRQHandler(void) ALIAS(IntDefaultHandler);
129 void RTC_IRQHandler(void) ALIAS(IntDefaultHandler);
130 void WDT_IRQHandler(void) ALIAS(IntDefaultHandler);
131 void CAN0_IRQHandler(void) ALIAS(IntDefaultHandler);
132 void QEI_IRQHandler(void) ALIAS(IntDefaultHandler);
133 
134 // *****************************************************************************
135 //
136 // The entry point for the application.
137 // __main() is the entry point for Redlib based applications
138 // main() is the entry point for Newlib based applications
139 //
140 // *****************************************************************************
141 #if defined(__REDLIB__)
142 extern void __main(void);
143 
144 #endif
145 extern int main(void);
146 
147 // *****************************************************************************
148 //
149 // External declaration for the pointer to the stack top from the Linker Script
150 //
151 // *****************************************************************************
152 extern void _vStackTop(void);
153 
154 // *****************************************************************************
155 #if defined(__cplusplus)
156 } // extern "C"
157 #endif
158 // *****************************************************************************
159 //
160 // The vector table.
161 // This relies on the linker script to place at correct location in memory.
162 //
163 // *****************************************************************************
164 extern void(*const g_pfnVectors[]) (void);
165 __attribute__ ((section(".isr_vector")))
166 void(*const g_pfnVectors[]) (void) = {
167  // Core Level - CM4/CM3
168  &_vStackTop, // The initial stack pointer
169  ResetISR, // The reset handler
170  NMI_Handler, // The NMI handler
171  HardFault_Handler, // The hard fault handler
172  MemManage_Handler, // The MPU fault handler
173  BusFault_Handler, // The bus fault handler
174  UsageFault_Handler, // The usage fault handler
175  0, // Reserved
176  0, // Reserved
177  0, // Reserved
178  0, // Reserved
179  SVC_Handler, // SVCall handler
180  DebugMon_Handler, // Debug monitor handler
181  0, // Reserved
182  PendSV_Handler, // The PendSV handler
183  SysTick_Handler, // The SysTick handler
184 
185  // Chip Level - LPC18xx/43xx
186  DAC_IRQHandler, // 16 D/A Converter
187  MX_CORE_IRQHandler, // 17 CortexM4/M0 (LPC43XX ONLY)
188  DMA_IRQHandler, // 18 General Purpose DMA
189  0, // 19 Reserved
190  FLASHEEPROM_IRQHandler, // 20 ORed flash Bank A, flash Bank B, EEPROM interrupts
191  ETH_IRQHandler, // 21 Ethernet
192  SDIO_IRQHandler, // 22 SD/MMC
193  LCD_IRQHandler, // 23 LCD
194  USB0_IRQHandler, // 24 USB0
195  USB1_IRQHandler, // 25 USB1
196  SCT_IRQHandler, // 26 State Configurable Timer
197  RIT_IRQHandler, // 27 Repetitive Interrupt Timer
198  TIMER0_IRQHandler, // 28 Timer0
199  TIMER1_IRQHandler, // 29 Timer 1
200  TIMER2_IRQHandler, // 30 Timer 2
201  TIMER3_IRQHandler, // 31 Timer 3
202  MCPWM_IRQHandler, // 32 Motor Control PWM
203  ADC0_IRQHandler, // 33 A/D Converter 0
204  I2C0_IRQHandler, // 34 I2C0
205  I2C1_IRQHandler, // 35 I2C1
206  SPI_IRQHandler, // 36 SPI (LPC43XX ONLY)
207  ADC1_IRQHandler, // 37 A/D Converter 1
208  SSP0_IRQHandler, // 38 SSP0
209  SSP1_IRQHandler, // 39 SSP1
210  UART0_IRQHandler, // 40 UART0
211  UART1_IRQHandler, // 41 UART1
212  UART2_IRQHandler, // 42 UART2
213  UART3_IRQHandler, // 43 USRT3
214  I2S0_IRQHandler, // 44 I2S0
215  I2S1_IRQHandler, // 45 I2S1
216  SPIFI_IRQHandler, // 46 SPI Flash Interface
217  SGPIO_IRQHandler, // 47 SGPIO (LPC43XX ONLY)
218  GPIO0_IRQHandler, // 48 GPIO0
219  GPIO1_IRQHandler, // 49 GPIO1
220  GPIO2_IRQHandler, // 50 GPIO2
221  GPIO3_IRQHandler, // 51 GPIO3
222  GPIO4_IRQHandler, // 52 GPIO4
223  GPIO5_IRQHandler, // 53 GPIO5
224  GPIO6_IRQHandler, // 54 GPIO6
225  GPIO7_IRQHandler, // 55 GPIO7
226  GINT0_IRQHandler, // 56 GINT0
227  GINT1_IRQHandler, // 57 GINT1
228  EVRT_IRQHandler, // 58 Event Router
229  CAN1_IRQHandler, // 59 C_CAN1
230  0, // 60 Reserved
231  0, // 61 Reserved
232  ATIMER_IRQHandler, // 62 ATIMER
233  RTC_IRQHandler, // 63 RTC
234  0, // 64 Reserved
235  WDT_IRQHandler, // 65 WDT
236  0, // 66 Reserved
237  CAN0_IRQHandler, // 67 C_CAN0
238  QEI_IRQHandler, // 68 QEI
239 };
240 
241 // *****************************************************************************
242 // Functions to carry out the initialization of RW and BSS data sections. These
243 // are written as separate functions rather than being inlined within the
244 // ResetISR() function in order to cope with MCUs with multiple banks of
245 // memory.
246 // *****************************************************************************
247 __attribute__ ((section(".after_vectors")))
248 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
249  unsigned int *pulDest = (unsigned int *) start;
250  unsigned int *pulSrc = (unsigned int *) romstart;
251  unsigned int loop;
252  for (loop = 0; loop < len; loop = loop + 4)
253  *pulDest++ = *pulSrc++;
254 }
255 
256 __attribute__ ((section(".after_vectors")))
257 void bss_init(unsigned int start, unsigned int len) {
258  unsigned int *pulDest = (unsigned int *) start;
259  unsigned int loop;
260  for (loop = 0; loop < len; loop = loop + 4)
261  *pulDest++ = 0;
262 }
263 
264 // *****************************************************************************
265 // The following symbols are constructs generated by the linker, indicating
266 // the location of various points in the "Global Section Table". This table is
267 // created by the linker via the Code Red managed linker script mechanism. It
268 // contains the load address, execution address and length of each RW data
269 // section and the execution and length of each BSS (zero initialized) section.
270 // *****************************************************************************
271 extern unsigned int __data_section_table;
272 extern unsigned int __data_section_table_end;
273 extern unsigned int __bss_section_table;
274 extern unsigned int __bss_section_table_end;
275 
276 // *****************************************************************************
277 // Reset entry point for your code.
278 // Sets up a simple runtime environment and initializes the C/C++
279 // library.
280 //
281 // *****************************************************************************
282 void
283 ResetISR(void) {
284 
285  //
286  // Copy the data sections from flash to SRAM.
287  //
288  unsigned int LoadAddr, ExeAddr, SectionLen;
289  unsigned int *SectionTableAddr;
290 
291  /* Call SystemInit() for clocking/memory setup prior to scatter load */
292  SystemInit();
293 
294  // Load base address of Global Section Table
295  SectionTableAddr = &__data_section_table;
296 
297  // Copy the data sections from flash to SRAM.
298  while (SectionTableAddr < &__data_section_table_end) {
299  LoadAddr = *SectionTableAddr++;
300  ExeAddr = *SectionTableAddr++;
301  SectionLen = *SectionTableAddr++;
302  data_init(LoadAddr, ExeAddr, SectionLen);
303  }
304  // At this point, SectionTableAddr = &__bss_section_table;
305  // Zero fill the bss segment
306  while (SectionTableAddr < &__bss_section_table_end) {
307  ExeAddr = *SectionTableAddr++;
308  SectionLen = *SectionTableAddr++;
309  bss_init(ExeAddr, SectionLen);
310  }
311 
312  #if defined(__cplusplus)
313  //
314  // Call C++ library initialisation
315  //
316  __libc_init_array();
317  #endif
318 
319  #if defined(__REDLIB__)
320  // Call the Redlib library, which in turn calls main()
321  __main();
322  #else
323  main();
324  #endif
325 
326  //
327  // main() shouldn't return, but if it does, we'll just enter an infinite loop
328  //
329  while (1) {}
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 {
339  while (1) {}
340 }
341 
342 __attribute__ ((section(".after_vectors")))
343 void HardFault_Handler(void)
344 {
345  while (1) {}
346 }
347 
348 __attribute__ ((section(".after_vectors")))
349 void MemManage_Handler(void)
350 {
351  while (1) {}
352 }
353 
354 __attribute__ ((section(".after_vectors")))
355 void BusFault_Handler(void)
356 {
357  while (1) {}
358 }
359 
360 __attribute__ ((section(".after_vectors")))
361 void UsageFault_Handler(void)
362 {
363  while (1) {}
364 }
365 
366 __attribute__ ((section(".after_vectors")))
367 void SVC_Handler(void)
368 {
369  while (1) {}
370 }
371 
372 __attribute__ ((section(".after_vectors")))
373 void DebugMon_Handler(void)
374 {
375  while (1) {}
376 }
377 
378 __attribute__ ((section(".after_vectors")))
379 void PendSV_Handler(void)
380 {
381  while (1) {}
382 }
383 
384 __attribute__ ((section(".after_vectors")))
385 void SysTick_Handler(void)
386 {
387  while (1) {}
388 }
389 
390 // *****************************************************************************
391 //
392 // Processor ends up here if an unexpected interrupt occurs or a specific
393 // handler is not present in the application code.
394 //
395 // *****************************************************************************
396 __attribute__ ((section(".after_vectors")))
397 void IntDefaultHandler(void)
398 {
399  while (1) {}
400 }
401 
402 
403