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