LPCOpen Platform  v1.03
LPCOpen Platform for NXP LPC Microcontrollers
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
cr_startup_lpc43xx_m0.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 // LPC43xx Microcontroller Startup code for use with Red Suite
11 //
12 // Version : 120430
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 }
43 #endif
44 #endif
45 
46 #include "lpc43xx_dualcore_config.h"
47 
48 #define WEAK __attribute__ ((weak))
49 #define ALIAS(f) __attribute__ ((weak, alias(# f)))
50 
51 // Code Red - if CMSIS is being used, then SystemInit() routine
52 // will be called by startup code rather than in application's main()
53 extern void SystemInit(void);
54 #if defined(OS_UCOS_III)
55 extern void OS_CPU_PendSVHandler(void);
56 #endif
57 
58 // *****************************************************************************
59 #if defined(__cplusplus)
60 extern "C" {
61 #endif
62 
63 // *****************************************************************************
64 //
65 // Forward declaration of the default handlers. These are aliased.
66 // When the application defines a handler (with the same name), this will
67 // automatically take precedence over these weak definitions
68 //
69 // *****************************************************************************
70 void ResetISR(void);
71 WEAK void NMI_Handler(void);
72 WEAK void HardFault_Handler(void);
73 WEAK void MemManage_Handler(void);
74 WEAK void BusFault_Handler(void);
75 WEAK void UsageFault_Handler(void);
76 WEAK void SVC_Handler(void);
77 WEAK void DebugMon_Handler(void);
78 WEAK void PendSV_Handler(void);
79 WEAK void SysTick_Handler(void);
80 WEAK void IntDefaultHandler(void);
81 
82 // *****************************************************************************
83 //
84 // Forward declaration of the specific IRQ handlers. These are aliased
85 // to the IntDefaultHandler, which is a 'forever' loop. When the application
86 // defines a handler (with the same name), this will automatically take
87 // precedence over these weak definitions
88 //
89 // *****************************************************************************
91 void MX_CORE_IRQHandler(void) ALIAS(IntDefaultHandler);
92 void DMA_IRQHandler(void) ALIAS(IntDefaultHandler);
93 void FLASHEEPROM_IRQHandler(void) ALIAS(IntDefaultHandler);
94 void ETH_IRQHandler(void) ALIAS(IntDefaultHandler);
95 void SDIO_IRQHandler(void) ALIAS(IntDefaultHandler);
96 void LCD_IRQHandler(void) ALIAS(IntDefaultHandler);
97 void USB0_IRQHandler(void) ALIAS(IntDefaultHandler);
98 void USB1_IRQHandler(void) ALIAS(IntDefaultHandler);
99 void SCT_IRQHandler(void) ALIAS(IntDefaultHandler);
100 void RIT_IRQHandler(void) ALIAS(IntDefaultHandler);
101 void TIMER0_IRQHandler(void) ALIAS(IntDefaultHandler);
102 void GINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
103 void GPIO4_IRQHandler(void) ALIAS(IntDefaultHandler);
104 void TIMER3_IRQHandler(void) ALIAS(IntDefaultHandler);
105 void MCPWM_IRQHandler(void) ALIAS(IntDefaultHandler);
106 void ADC0_IRQHandler(void) ALIAS(IntDefaultHandler);
107 void I2C0_IRQHandler(void) ALIAS(IntDefaultHandler);
108 void SGPIO_IRQHandler(void) ALIAS(IntDefaultHandler);
109 void SPI_IRQHandler (void) ALIAS(IntDefaultHandler);
110 void ADC1_IRQHandler(void) ALIAS(IntDefaultHandler);
111 void SSP0_IRQHandler(void) ALIAS(IntDefaultHandler);
112 void EVRT_IRQHandler(void) ALIAS(IntDefaultHandler);
113 void UART0_IRQHandler(void) ALIAS(IntDefaultHandler);
114 void UART1_IRQHandler(void) ALIAS(IntDefaultHandler);
115 void UART2_IRQHandler(void) ALIAS(IntDefaultHandler);
116 void UART3_IRQHandler(void) ALIAS(IntDefaultHandler);
117 void I2S0_IRQHandler(void) ALIAS(IntDefaultHandler);
118 void CAN0_IRQHandler(void) ALIAS(IntDefaultHandler);
119 
120 // *****************************************************************************
121 //
122 // The entry point for the application.
123 // __main() is the entry point for Redlib based applications
124 // main() is the entry point for Newlib based applications
125 //
126 // *****************************************************************************
127 #if defined(__REDLIB__)
128 extern void __main(void);
129 
130 #endif
131 extern int main(void);
132 
133 // *****************************************************************************
134 //
135 // External declaration for the pointer to the stack top from the Linker Script
136 //
137 // *****************************************************************************
138 extern void _vStackTop(void);
139 
140 // *****************************************************************************
141 //
142 // Application can define Stack size (If not defined, default stack size will
143 // used
144 //
145 // *****************************************************************************
146 #ifndef STACK_SIZE
147 #define STACK_SIZE (0x200)
148 #endif
149 
150 // *****************************************************************************
151 //
152 // Application can define Heap size (If not defined, default Heap size will
153 // used
154 //
155 // *****************************************************************************
156 #ifndef HEAP_SIZE
157 #define HEAP_SIZE (0x4000)
158 #endif
159 
160 unsigned int __vStack[STACK_SIZE / sizeof(unsigned int)] __attribute__((section("STACK,\"aw\",%nobits@")));
161 unsigned int __vHeap[HEAP_SIZE / sizeof(unsigned int)] __attribute__((section("HEAP,\"aw\",%nobits@")));
162 
163 // *****************************************************************************
164 #if defined(__cplusplus)
165 } // extern "C"
166 #endif
167 
168 #define M0_Image_Magic 0xAA55DEAD
169 extern const struct image_sig __M0Signature;
170 
171 // *****************************************************************************
172 //
173 // The vector table.
174 // This relies on the linker script to place at correct location in memory.
175 //
176 // *****************************************************************************
177 extern void(*const g_pfnVectors[]) (void);
178 __attribute__ ((section(".isr_vector")))
179 void(*const g_pfnVectors[]) (void) = {
180  // Core Level - CM4/CM3
181  &_vStackTop, // The initial stack pointer
182  ResetISR, // The reset handler
183  NMI_Handler, // The NMI handler
184  HardFault_Handler, // The hard fault handler
185  MemManage_Handler, // The MPU fault handler
186  BusFault_Handler, // The bus fault handler
187  UsageFault_Handler, // The usage fault handler
188  0, // Reserved
189  (void (*)(void))M0_Image_Magic, // 8 Reserved
190  (void (*)(void))&__M0Signature, // 9 Reserved
191  0, // Reserved
192  SVC_Handler, // SVCall handler
193  DebugMon_Handler, // Debug monitor handler
194  0, // Reserved
195 #if defined(OS_UCOS_III)
196  OS_CPU_PendSVHandler, // uCOS-III PendSV handler
197 #else
198  PendSV_Handler, // The PendSV handler
199 #endif
200  SysTick_Handler, // The SysTick handler
201 
202  // Chip Level - 43xx M0 core
203  RTC_IRQHandler, // 16 RTC
204  MX_CORE_IRQHandler, // 17 CortexM4/M0 (LPC43XX ONLY)
205  DMA_IRQHandler, // 18 General Purpose DMA
206  0, // 19 Reserved
207  FLASHEEPROM_IRQHandler, // 20 ORed flash Bank A, flash Bank B, EEPROM interrupts
208  ETH_IRQHandler, // 21 Ethernet
209  SDIO_IRQHandler, // 22 SD/MMC
210  LCD_IRQHandler, // 23 LCD
211  USB0_IRQHandler, // 24 USB0
212  USB1_IRQHandler, // 25 USB1
213  SCT_IRQHandler, // 26 State Configurable Timer
214  RIT_IRQHandler, // 27 ORed Repetitive Interrupt Timer, WWDT
215  TIMER0_IRQHandler, // 28 Timer0
216  GINT1_IRQHandler, // 29 GINT1
217  GPIO4_IRQHandler, // 30 GPIO4
218  TIMER3_IRQHandler, // 31 Timer 3
219  MCPWM_IRQHandler, // 32 Motor Control PWM
220  ADC0_IRQHandler, // 33 A/D Converter 0
221  I2C0_IRQHandler, // 34 ORed I2C0, I2C1
222  SGPIO_IRQHandler, // 35 SGPIO (LPC43XX ONLY)
223  SPI_IRQHandler, // 36 ORed SPI, DAC (LPC43XX ONLY)
224  ADC1_IRQHandler, // 37 A/D Converter 1
225  SSP0_IRQHandler, // 38 ORed SSP0, SSP1
226  EVRT_IRQHandler, // 39 Event Router
227  UART0_IRQHandler, // 40 UART0
228  UART1_IRQHandler, // 41 UART1
229  UART2_IRQHandler, // 42 UART2
230  UART3_IRQHandler, // 43 USRT3
231  I2S0_IRQHandler, // 44 ORed I2S0, I2S1
232  CAN0_IRQHandler, // 45 C_CAN0
233  0, // 46 Reserved
234  0, // 47 Reserved
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 extern unsigned int __bss_section_end;
272 
273 // *****************************************************************************
274 // Reset entry point for your code.
275 // Sets up a simple runtime environment and initializes the C/C++
276 // library.
277 //
278 // *****************************************************************************
279 void
280 ResetISR(void) {
281 
282  //
283  // Copy the data sections from flash to SRAM.
284  //
285  unsigned int LoadAddr, ExeAddr, SectionLen;
286  unsigned int *SectionTableAddr;
287 
288  /* Call SystemInit() for clocking/memory setup prior to scatter load */
289  SystemInit();
290 
291  // Load base address of Global Section Table
292  SectionTableAddr = &__data_section_table;
293 
294  // Copy the data sections from flash to SRAM.
295  while (SectionTableAddr < &__data_section_table_end) {
296  LoadAddr = *SectionTableAddr++;
297  ExeAddr = *SectionTableAddr++;
298  SectionLen = *SectionTableAddr++;
299  data_init(LoadAddr, ExeAddr, SectionLen);
300  }
301  // At this point, SectionTableAddr = &__bss_section_table;
302  // Zero fill the bss segment
303  while (SectionTableAddr < &__bss_section_end) { /* Not __bss_section_table_end */
304  ExeAddr = *SectionTableAddr++;
305  SectionLen = *SectionTableAddr++;
306  bss_init(ExeAddr, SectionLen);
307  }
308 
309  #if defined(__cplusplus)
310  //
311  // Call C++ library initialisation
312  //
313  __libc_init_array();
314  #endif
315 
316  #if defined(__REDLIB__)
317  // Call the Redlib library, which in turn calls main()
318  __main();
319  #else
320  main();
321  #endif
322 
323  //
324  // main() shouldn't return, but if it does, we'll just enter an infinite loop
325  //
326  while (1) {}
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 __attribute__ ((section(".after_vectors")))
340 void HardFault_Handler(void)
341 {
342  while (1) {}
343 }
344 
345 __attribute__ ((section(".after_vectors")))
346 void MemManage_Handler(void)
347 {
348  while (1) {}
349 }
350 
351 __attribute__ ((section(".after_vectors")))
352 void BusFault_Handler(void)
353 {
354  while (1) {}
355 }
356 
357 __attribute__ ((section(".after_vectors")))
358 void UsageFault_Handler(void)
359 {
360  while (1) {}
361 }
362 
363 __attribute__ ((section(".after_vectors")))
364 void SVC_Handler(void)
365 {
366  while (1) {}
367 }
368 
369 __attribute__ ((section(".after_vectors")))
370 void DebugMon_Handler(void)
371 {
372  while (1) {}
373 }
374 
375 __attribute__ ((section(".after_vectors")))
376 void PendSV_Handler(void)
377 {
378  while (1) {}
379 }
380 
381 __attribute__ ((section(".after_vectors")))
382 void SysTick_Handler(void)
383 {
384  while (1) {}
385 }
386 
387 // *****************************************************************************
388 //
389 // Processor ends up here if an unexpected interrupt occurs or a specific
390 // handler is not present in the application code.
391 //
392 // *****************************************************************************
393 __attribute__ ((section(".after_vectors")))
394 void IntDefaultHandler(void)
395 {
396  while (1) {}
397 }
398 
399 // *****************************************************************************
400 //
401 // Heap overflow check function required by REDLib_V2 library
402 //
403 // *****************************************************************************
404 extern unsigned int *_pvHeapStart;
405 unsigned int __check_heap_overflow (void * new_end_of_heap)
406 {
407  return (new_end_of_heap >= (void *)&__vHeap[HEAP_SIZE/sizeof(unsigned int)]);
408 }
409