LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cr_ucos_iii_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 
48 // Code Red - if CMSIS is being used, then SystemInit() routine
49 // will be called by startup code rather than in application's main()
50 extern void SystemInit(void);
51 extern void OS_CPU_PendSVHandler(void);
52 extern void OS_CPU_SysTickHandler (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  OS_CPU_PendSVHandler, // The PendSV handler
176  OS_CPU_SysTickHandler, // The SysTick handler
177 
178 
179  // Chip Level - LPC17
180  WDT_IRQHandler, // 16, 0x40 - WDT
181  TIMER0_IRQHandler, // 17, 0x44 - TIMER0
182  TIMER1_IRQHandler, // 18, 0x48 - TIMER1
183  TIMER2_IRQHandler, // 19, 0x4c - TIMER2
184  TIMER3_IRQHandler, // 20, 0x50 - TIMER3
185  UART0_IRQHandler, // 21, 0x54 - UART0
186  UART1_IRQHandler, // 22, 0x58 - UART1
187  UART2_IRQHandler, // 23, 0x5c - UART2
188  UART3_IRQHandler, // 24, 0x60 - UART3
189  PWM1_IRQHandler, // 25, 0x64 - PWM1
190  I2C0_IRQHandler, // 26, 0x68 - I2C0
191  I2C1_IRQHandler, // 27, 0x6c - I2C1
192  I2C2_IRQHandler, // 28, 0x70 - I2C2
193  SPI_IRQHandler, // 29, Not used
194  SSP0_IRQHandler, // 30, 0x78 - SSP0
195  SSP1_IRQHandler, // 31, 0x7c - SSP1
196  PLL0_IRQHandler, // 32, 0x80 - PLL0 (Main PLL)
197  RTC_IRQHandler, // 33, 0x84 - RTC
198  EINT0_IRQHandler, // 34, 0x88 - EINT0
199  EINT1_IRQHandler, // 35, 0x8c - EINT1
200  EINT2_IRQHandler, // 36, 0x90 - EINT2
201  EINT3_IRQHandler, // 37, 0x94 - EINT3
202  ADC_IRQHandler, // 38, 0x98 - ADC
203  BOD_IRQHandler, // 39, 0x9c - BOD
204  USB_IRQHandler, // 40, 0xA0 - USB
205  CAN_IRQHandler, // 41, 0xa4 - CAN
206  DMA_IRQHandler, // 42, 0xa8 - GP DMA
207  I2S_IRQHandler, // 43, 0xac - I2S
208  ETH_IRQHandler, // 44, 0xb0 - Ethernet
209  SDIO_IRQHandler, // 45, 0xb4 - SD/MMC card I/F
210  MCPWM_IRQHandler, // 46, 0xb8 - Motor Control PWM
211  QEI_IRQHandler, // 47, 0xbc - Quadrature Encoder
212  PLL1_IRQHandler, // 48, 0xc0 - PLL1 (USB PLL)
213  USBActivity_IRQHandler, // 49, 0xc4 - USB Activity interrupt to wakeup
214  CANActivity_IRQHandler, // 50, 0xc8 - CAN Activity interrupt to wakeup
215  UART4_IRQHandler, // 51, 0xcc - UART4
216 
217  SSP2_IRQHandler, // 52, 0xd0 - SSP2
218  LCD_IRQHandler, // 53, 0xd4 - LCD
219  GPIO_IRQHandler, // 54, 0xd8 - GPIO
220  PWM0_IRQHandler, // 55, 0xdc - PWM0
221  EEPROM_IRQHandler, // 56, 0xe0 - EEPROM
222 
223 };
224 
225 //*****************************************************************************
226 // Functions to carry out the initialization of RW and BSS data sections. These
227 // are written as separate functions rather than being inlined within the
228 // ResetISR() function in order to cope with MCUs with multiple banks of
229 // memory.
230 //*****************************************************************************
231 __attribute__ ((section(".after_vectors")))
232 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
233  unsigned int *pulDest = (unsigned int*) start;
234  unsigned int *pulSrc = (unsigned int*) romstart;
235  unsigned int loop;
236  for (loop = 0; loop < len; loop = loop + 4)
237  *pulDest++ = *pulSrc++;
238 }
239 
240 __attribute__ ((section(".after_vectors")))
241 void bss_init(unsigned int start, unsigned int len) {
242  unsigned int *pulDest = (unsigned int*) start;
243  unsigned int loop;
244  for (loop = 0; loop < len; loop = loop + 4)
245  *pulDest++ = 0;
246 }
247 
248 //*****************************************************************************
249 // The following symbols are constructs generated by the linker, indicating
250 // the location of various points in the "Global Section Table". This table is
251 // created by the linker via the Code Red managed linker script mechanism. It
252 // contains the load address, execution address and length of each RW data
253 // section and the execution and length of each BSS (zero initialized) section.
254 //*****************************************************************************
255 extern unsigned int __data_section_table;
256 extern unsigned int __data_section_table_end;
257 extern unsigned int __bss_section_table;
258 extern unsigned int __bss_section_table_end;
259 
260 extern void SystemInit(void);
261 
262 //*****************************************************************************
263 // Reset entry point for your code.
264 // Sets up a simple runtime environment and initializes the C/C++
265 // library.
266 //
267 //*****************************************************************************
268 __attribute__ ((section(".after_vectors")))
269 void
270 ResetISR(void) {
271 
272  //
273  // Copy the data sections from flash to SRAM.
274  //
275  unsigned int LoadAddr, ExeAddr, SectionLen;
276  unsigned int *SectionTableAddr;
277 
278  /* Call SystemInit() for clocking/memory setup prior to scatter load */
279  SystemInit();
280 
281  // Load base address of Global Section Table
282  SectionTableAddr = &__data_section_table;
283 
284  // Copy the data sections from flash to SRAM.
285  while (SectionTableAddr < &__data_section_table_end) {
286  LoadAddr = *SectionTableAddr++;
287  ExeAddr = *SectionTableAddr++;
288  SectionLen = *SectionTableAddr++;
289  data_init(LoadAddr, ExeAddr, SectionLen);
290  }
291  // At this point, SectionTableAddr = &__bss_section_table;
292  // Zero fill the bss segment
293  while (SectionTableAddr < &__bss_section_table_end) {
294  ExeAddr = *SectionTableAddr++;
295  SectionLen = *SectionTableAddr++;
296  bss_init(ExeAddr, SectionLen);
297  }
298 
299 #if defined (__cplusplus)
300  //
301  // Call C++ library initialisation
302  //
303  __libc_init_array();
304 #endif
305 
306 #if defined (__REDLIB__)
307  // Call the Redlib library, which in turn calls main()
308  __main() ;
309 #else
310  main();
311 #endif
312 
313  //
314  // main() shouldn't return, but if it does, we'll just enter an infinite loop
315  //
316  while (1) {
317  ;
318  }
319 }
320 
321 //*****************************************************************************
322 // Default exception handlers. Override the ones here by defining your own
323 // handler routines in your application code.
324 //*****************************************************************************
325 __attribute__ ((section(".after_vectors")))
326 void NMI_Handler(void)
327 {
328  while(1)
329  {
330  }
331 }
332 __attribute__ ((section(".after_vectors")))
333 void HardFault_Handler(void)
334 {
335  while(1)
336  {
337  }
338 }
339 __attribute__ ((section(".after_vectors")))
340 void MemManage_Handler(void)
341 {
342  while(1)
343  {
344  }
345 }
346 __attribute__ ((section(".after_vectors")))
347 void BusFault_Handler(void)
348 {
349  while(1)
350  {
351  }
352 }
353 __attribute__ ((section(".after_vectors")))
354 void UsageFault_Handler(void)
355 {
356  while(1)
357  {
358  }
359 }
360 __attribute__ ((section(".after_vectors")))
361 void SVC_Handler(void)
362 {
363  while(1)
364  {
365  }
366 }
367 __attribute__ ((section(".after_vectors")))
368 void DebugMon_Handler(void)
369 {
370  while(1)
371  {
372  }
373 }
374 __attribute__ ((section(".after_vectors")))
375 void PendSV_Handler(void)
376 {
377  while(1)
378  {
379  }
380 }
381 __attribute__ ((section(".after_vectors")))
382 void SysTick_Handler(void)
383 {
384  while(1)
385  {
386  }
387 }
388 
389 //*****************************************************************************
390 //
391 // Processor ends up here if an unexpected interrupt occurs or a specific
392 // handler is not present in the application code.
393 //
394 //*****************************************************************************
395 __attribute__ ((section(".after_vectors")))
396 void IntDefaultHandler(void)
397 {
398  while(1)
399  {
400  }
401 }