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