LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cr_startup_lpc17xx40xx.c
Go to the documentation of this file.
1 //*****************************************************************************
2 // +--+
3 // | ++----+
4 // +-++ |
5 // | |
6 // +-+--+ |
7 // | +--+--+
8 // +----+ Copyright (c) 2010-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_* 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 MemManage_Handler(void);
70 WEAK void BusFault_Handler(void);
71 WEAK void UsageFault_Handler(void);
72 WEAK void SVC_Handler(void);
73 WEAK void DebugMon_Handler(void);
74 WEAK void PendSV_Handler(void);
75 WEAK void SysTick_Handler(void);
76 WEAK void IntDefaultHandler(void);
77 
78 //*****************************************************************************
79 //
80 // Forward declaration of the specific IRQ handlers. These are aliased
81 // to the IntDefaultHandler, which is a 'forever' loop. When the application
82 // defines a handler (with the same name), this will automatically take
83 // precedence over these weak definitions
84 //
85 //*****************************************************************************
87 void TIMER0_IRQHandler(void) ALIAS(IntDefaultHandler);
88 void TIMER1_IRQHandler(void) ALIAS(IntDefaultHandler);
89 void TIMER2_IRQHandler(void) ALIAS(IntDefaultHandler);
90 void TIMER3_IRQHandler(void) ALIAS(IntDefaultHandler);
91 void UART0_IRQHandler(void) ALIAS(IntDefaultHandler);
92 void UART1_IRQHandler(void) ALIAS(IntDefaultHandler);
93 void UART2_IRQHandler(void) ALIAS(IntDefaultHandler);
94 void UART3_IRQHandler(void) ALIAS(IntDefaultHandler);
95 void PWM1_IRQHandler(void) ALIAS(IntDefaultHandler);
96 void I2C0_IRQHandler(void) ALIAS(IntDefaultHandler);
97 void I2C1_IRQHandler(void) ALIAS(IntDefaultHandler);
98 void I2C2_IRQHandler(void) ALIAS(IntDefaultHandler);
99 void SPI_IRQHandler(void) ALIAS(IntDefaultHandler);
100 void SSP0_IRQHandler(void) ALIAS(IntDefaultHandler);
101 void SSP1_IRQHandler(void) ALIAS(IntDefaultHandler);
102 void PLL0_IRQHandler(void) ALIAS(IntDefaultHandler);
103 void RTC_IRQHandler(void) ALIAS(IntDefaultHandler);
104 void EINT0_IRQHandler(void) ALIAS(IntDefaultHandler);
105 void EINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
106 void EINT2_IRQHandler(void) ALIAS(IntDefaultHandler);
107 void EINT3_IRQHandler(void) ALIAS(IntDefaultHandler);
108 void ADC_IRQHandler(void) ALIAS(IntDefaultHandler);
109 void BOD_IRQHandler(void) ALIAS(IntDefaultHandler);
110 void USB_IRQHandler(void) ALIAS(IntDefaultHandler);
111 void CAN_IRQHandler(void) ALIAS(IntDefaultHandler);
112 void DMA_IRQHandler(void) ALIAS(IntDefaultHandler);
113 void I2S_IRQHandler(void) ALIAS(IntDefaultHandler);
114 void ETH_IRQHandler(void) ALIAS(IntDefaultHandler);
115 void RIT_IRQHandler(void) ALIAS(IntDefaultHandler);
116 void MCPWM_IRQHandler(void) ALIAS(IntDefaultHandler);
117 void QEI_IRQHandler(void) ALIAS(IntDefaultHandler);
118 void PLL1_IRQHandler(void) ALIAS(IntDefaultHandler);
119 void USBActivity_IRQHandler(void) ALIAS(IntDefaultHandler);
120 void CANActivity_IRQHandler(void) ALIAS(IntDefaultHandler);
121 void SDIO_IRQHandler(void) ALIAS(IntDefaultHandler);
122 void UART4_IRQHandler(void) ALIAS(IntDefaultHandler);
123 void SSP2_IRQHandler(void) ALIAS(IntDefaultHandler);
124 void LCD_IRQHandler(void) ALIAS(IntDefaultHandler);
125 void GPIO_IRQHandler(void) ALIAS(IntDefaultHandler);
126 void PWM0_IRQHandler(void) ALIAS(IntDefaultHandler);
127 void EEPROM_IRQHandler(void) ALIAS(IntDefaultHandler);
128 
129 //*****************************************************************************
130 //
131 // The entry point for the application.
132 // __main() is the entry point for Redlib based applications
133 // main() is the entry point for Newlib based applications
134 //
135 //*****************************************************************************
136 #if defined (__REDLIB__)
137 extern void __main(void);
138 #endif
139 extern int main(void);
140 //*****************************************************************************
141 //
142 // External declaration for the pointer to the stack top from the Linker Script
143 //
144 //*****************************************************************************
145 extern void _vStackTop(void);
146 
147 //*****************************************************************************
148 #if defined (__cplusplus)
149 } // extern "C"
150 #endif
151 //*****************************************************************************
152 //
153 // The vector table.
154 // This relies on the linker script to place at correct location in memory.
155 //
156 //*****************************************************************************
157 extern void (* const g_pfnVectors[])(void);
158 __attribute__ ((section(".isr_vector")))
159 void(*const g_pfnVectors[]) (void) = {
160  // Core Level - CM4/CM3
161  &_vStackTop, // The initial stack pointer
162  ResetISR, // The reset handler
163  NMI_Handler, // The NMI handler
164  HardFault_Handler, // The hard fault handler
165  MemManage_Handler, // The MPU fault handler
166  BusFault_Handler, // The bus fault handler
167  UsageFault_Handler, // The usage fault handler
168  0, // Reserved
169  0, // Reserved
170  0, // Reserved
171  0, // Reserved
172  SVC_Handler, // SVCall handler
173  DebugMon_Handler, // Debug monitor handler
174  0, // Reserved
175  PendSV_Handler, // The PendSV handler
176  SysTick_Handler, // The SysTick handler
177 
178  // Chip Level - LPC17
179  WDT_IRQHandler, // 16, 0x40 - WDT
180  TIMER0_IRQHandler, // 17, 0x44 - TIMER0
181  TIMER1_IRQHandler, // 18, 0x48 - TIMER1
182  TIMER2_IRQHandler, // 19, 0x4c - TIMER2
183  TIMER3_IRQHandler, // 20, 0x50 - TIMER3
184  UART0_IRQHandler, // 21, 0x54 - UART0
185  UART1_IRQHandler, // 22, 0x58 - UART1
186  UART2_IRQHandler, // 23, 0x5c - UART2
187  UART3_IRQHandler, // 24, 0x60 - UART3
188  PWM1_IRQHandler, // 25, 0x64 - PWM1
189  I2C0_IRQHandler, // 26, 0x68 - I2C0
190  I2C1_IRQHandler, // 27, 0x6c - I2C1
191  I2C2_IRQHandler, // 28, 0x70 - I2C2
192 #ifndef CHIP_LPC175X_6X
193  0,
194 #else
195  SPI_IRQHandler, // 29, Not used
196 #endif
197  SSP0_IRQHandler, // 30, 0x78 - SSP0
198  SSP1_IRQHandler, // 31, 0x7c - SSP1
199  PLL0_IRQHandler, // 32, 0x80 - PLL0 (Main PLL)
200  RTC_IRQHandler, // 33, 0x84 - RTC
201  EINT0_IRQHandler, // 34, 0x88 - EINT0
202  EINT1_IRQHandler, // 35, 0x8c - EINT1
203  EINT2_IRQHandler, // 36, 0x90 - EINT2
204  EINT3_IRQHandler, // 37, 0x94 - EINT3
205  ADC_IRQHandler, // 38, 0x98 - ADC
206  BOD_IRQHandler, // 39, 0x9c - BOD
207  USB_IRQHandler, // 40, 0xA0 - USB
208  CAN_IRQHandler, // 41, 0xa4 - CAN
209  DMA_IRQHandler, // 42, 0xa8 - GP DMA
210  I2S_IRQHandler, // 43, 0xac - I2S
211  ETH_IRQHandler, // 44, 0xb0 - Ethernet
212 #ifndef CHIP_LPC175X_6X
213  SDIO_IRQHandler, // 45, 0xb4 - SD/MMC card I/F
214 #else
215  RIT_IRQHandler, // 45, 0xb4 - RIT
216 #endif
217  MCPWM_IRQHandler, // 46, 0xb8 - Motor Control PWM
218  QEI_IRQHandler, // 47, 0xbc - Quadrature Encoder
219  PLL1_IRQHandler, // 48, 0xc0 - PLL1 (USB PLL)
220  USBActivity_IRQHandler, // 49, 0xc4 - USB Activity interrupt to wakeup
221  CANActivity_IRQHandler, // 50, 0xc8 - CAN Activity interrupt to wakeup
222 #ifndef CHIP_LPC175X_6X
223  UART4_IRQHandler, // 51, 0xcc - UART4
224 
225  SSP2_IRQHandler, // 52, 0xd0 - SSP2
226  LCD_IRQHandler, // 53, 0xd4 - LCD
227  GPIO_IRQHandler, // 54, 0xd8 - GPIO
228  PWM0_IRQHandler, // 55, 0xdc - PWM0
229  EEPROM_IRQHandler, // 56, 0xe0 - EEPROM
230 #endif
231 };
232 
233 //*****************************************************************************
234 // Functions to carry out the initialization of RW and BSS data sections. These
235 // are written as separate functions rather than being inlined within the
236 // ResetISR() function in order to cope with MCUs with multiple banks of
237 // memory.
238 //*****************************************************************************
239 __attribute__ ((section(".after_vectors")))
240 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
241  unsigned int *pulDest = (unsigned int*) start;
242  unsigned int *pulSrc = (unsigned int*) romstart;
243  unsigned int loop;
244  for (loop = 0; loop < len; loop = loop + 4)
245  *pulDest++ = *pulSrc++;
246 }
247 
248 __attribute__ ((section(".after_vectors")))
249 void bss_init(unsigned int start, unsigned int len) {
250  unsigned int *pulDest = (unsigned int*) start;
251  unsigned int loop;
252  for (loop = 0; loop < len; loop = loop + 4)
253  *pulDest++ = 0;
254 }
255 
256 //*****************************************************************************
257 // The following symbols are constructs generated by the linker, indicating
258 // the location of various points in the "Global Section Table". This table is
259 // created by the linker via the Code Red managed linker script mechanism. It
260 // contains the load address, execution address and length of each RW data
261 // section and the execution and length of each BSS (zero initialized) section.
262 //*****************************************************************************
263 extern unsigned int __data_section_table;
264 extern unsigned int __data_section_table_end;
265 extern unsigned int __bss_section_table;
266 extern unsigned int __bss_section_table_end;
267 
268 extern void SystemInit(void);
269 
270 //*****************************************************************************
271 // Reset entry point for your code.
272 // Sets up a simple runtime environment and initializes the C/C++
273 // library.
274 //
275 //*****************************************************************************
276 __attribute__ ((section(".after_vectors")))
277 void
278 ResetISR(void) {
279 
280  //
281  // Copy the data sections from flash to SRAM.
282  //
283  unsigned int LoadAddr, ExeAddr, SectionLen;
284  unsigned int *SectionTableAddr;
285 
286  /* Call SystemInit() for clocking/memory setup prior to scatter load */
287  SystemInit();
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 (__cplusplus)
308  //
309  // Call C++ library initialisation
310  //
311  __libc_init_array();
312 #endif
313 
314 #if defined (__REDLIB__)
315  // Call the Redlib library, which in turn calls main()
316  __main() ;
317 #else
318  main();
319 #endif
320 
321  //
322  // main() shouldn't return, but if it does, we'll just enter an infinite loop
323  //
324  while (1) {
325  ;
326  }
327 }
328 
329 //*****************************************************************************
330 // Default exception handlers. Override the ones here by defining your own
331 // handler routines in your application code.
332 //*****************************************************************************
333 __attribute__ ((section(".after_vectors")))
334 void NMI_Handler(void)
335 {
336  while(1)
337  {
338  }
339 }
340 __attribute__ ((section(".after_vectors")))
341 void HardFault_Handler(void)
342 {
343  while(1)
344  {
345  }
346 }
347 __attribute__ ((section(".after_vectors")))
348 void MemManage_Handler(void)
349 {
350  while(1)
351  {
352  }
353 }
354 __attribute__ ((section(".after_vectors")))
355 void BusFault_Handler(void)
356 {
357  while(1)
358  {
359  }
360 }
361 __attribute__ ((section(".after_vectors")))
362 void UsageFault_Handler(void)
363 {
364  while(1)
365  {
366  }
367 }
368 __attribute__ ((section(".after_vectors")))
369 void SVC_Handler(void)
370 {
371  while(1)
372  {
373  }
374 }
375 __attribute__ ((section(".after_vectors")))
376 void DebugMon_Handler(void)
377 {
378  while(1)
379  {
380  }
381 }
382 __attribute__ ((section(".after_vectors")))
383 void PendSV_Handler(void)
384 {
385  while(1)
386  {
387  }
388 }
389 __attribute__ ((section(".after_vectors")))
390 void SysTick_Handler(void)
391 {
392  while(1)
393  {
394  }
395 }
396 
397 //*****************************************************************************
398 //
399 // Processor ends up here if an unexpected interrupt occurs or a specific
400 // handler is not present in the application code.
401 //
402 //*****************************************************************************
403 __attribute__ ((section(".after_vectors")))
404 void IntDefaultHandler(void)
405 {
406  while(1)
407  {
408  }
409 }