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