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