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_lpc8xx.c
Go to the documentation of this file.
1 //*****************************************************************************
2 // +--+
3 // | ++----+
4 // +-++ |
5 // | |
6 // +-+--+ |
7 // | +--+--+
8 // +----+ Copyright (c) 2012 Code Red Technologies Ltd.
9 //
10 // LPC8xx Microcontroller Startup code for use with Red Suite
11 //
12 // Version : 121107
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 //#if defined (__USE_CMSIS)
49 //#include "LPC8xx.h"
50 //#endif
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 SVC_Handler(void);
68 WEAK void PendSV_Handler(void);
69 WEAK void SysTick_Handler(void);
70 WEAK void IntDefaultHandler(void);
71 
72 void vPortSVCHandler(void);
73 void xPortPendSVHandler(void);
74 void xPortSysTickHandler(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 //*****************************************************************************
84 void SPI0_IRQHandler(void) ALIAS(IntDefaultHandler);
85 void SPI1_IRQHandler(void) ALIAS(IntDefaultHandler);
86 void UART0_IRQHandler(void) ALIAS(IntDefaultHandler);
87 void UART1_IRQHandler(void) ALIAS(IntDefaultHandler);
88 void UART2_IRQHandler(void) ALIAS(IntDefaultHandler);
89 void I2C_IRQHandler(void) ALIAS(IntDefaultHandler);
90 void SCT_IRQHandler(void) ALIAS(IntDefaultHandler);
91 void MRT_IRQHandler(void) ALIAS(IntDefaultHandler);
92 void CMP_IRQHandler(void) ALIAS(IntDefaultHandler);
93 void WDT_IRQHandler(void) ALIAS(IntDefaultHandler);
94 void BOD_IRQHandler(void) ALIAS(IntDefaultHandler);
95 void WKT_IRQHandler(void) ALIAS(IntDefaultHandler);
96 void PININT0_IRQHandler(void) ALIAS(IntDefaultHandler);
97 void PININT1_IRQHandler(void) ALIAS(IntDefaultHandler);
98 void PININT2_IRQHandler(void) ALIAS(IntDefaultHandler);
99 void PININT3_IRQHandler(void) ALIAS(IntDefaultHandler);
100 void PININT4_IRQHandler(void) ALIAS(IntDefaultHandler);
101 void PININT5_IRQHandler(void) ALIAS(IntDefaultHandler);
102 void PININT6_IRQHandler(void) ALIAS(IntDefaultHandler);
103 void PININT7_IRQHandler(void) ALIAS(IntDefaultHandler);
104 //*****************************************************************************
105 //
106 // The entry point for the application.
107 // __main() is the entry point for Redlib based applications
108 // main() is the entry point for Newlib based applications
109 //
110 //*****************************************************************************
111 #if defined (__REDLIB__)
112 extern void __main(void);
113 #endif
114 extern int main(void);
115 //*****************************************************************************
116 //
117 // External declaration for the pointer to the stack top from the Linker Script
118 //
119 //*****************************************************************************
120 extern void _vStackTop(void);
121 
122 //*****************************************************************************
123 #if defined (__cplusplus)
124 } // extern "C"
125 #endif
126 //*****************************************************************************
127 //
128 // The vector table.
129 // This relies on the linker script to place at correct location in memory.
130 //
131 //*****************************************************************************
132 extern void (* const g_pfnVectors[])(void);
133 __attribute__ ((section(".isr_vector")))
134 void (* const g_pfnVectors[])(void) = {
135  // Core Level - CM0plus
136  &_vStackTop, // The initial stack pointer
137  ResetISR, // The reset handler
138  NMI_Handler, // The NMI handler
139  HardFault_Handler, // The hard fault handler
140  0, // Reserved
141  0, // Reserved
142  0, // Reserved
143  0, // Reserved
144  0, // Reserved
145  0, // Reserved
146  0, // Reserved
147  vPortSVCHandler, // FreeRTOS SVCall Handler
148  0, // Reserved
149  0, // Reserved
150  xPortPendSVHandler, // FreeRTOS PendSV Handler
151  xPortSysTickHandler, // FreeRTOS SysTick Handler
152 
153  // Chip Level - LPC8xx
154  SPI0_IRQHandler, // SPI0 controller
155  SPI1_IRQHandler, // SPI1 controller
156  0, // Reserved
157  UART0_IRQHandler, // UART0
158  UART1_IRQHandler, // UART1
159  UART2_IRQHandler, // UART2
160  0, // Reserved
161  0, // Reserved
162  I2C_IRQHandler, // I2C controller
163  SCT_IRQHandler, // Smart Counter Timer
164  MRT_IRQHandler, // Multi-Rate Timer
165  CMP_IRQHandler, // Comparator
166  WDT_IRQHandler, // PIO1 (0:11)
167  BOD_IRQHandler, // Brown Out Detect
168  0, // Reserved
169  WKT_IRQHandler, // Wakeup timer
170  0, // Reserved
171  0, // Reserved
172  0, // Reserved
173  0, // Reserved
174  0, // Reserved
175  0, // Reserved
176  0, // Reserved
177  0, // Reserved
178  PININT0_IRQHandler, // PIO INT0
179  PININT1_IRQHandler, // PIO INT1
180  PININT2_IRQHandler, // PIO INT2
181  PININT3_IRQHandler, // PIO INT3
182  PININT4_IRQHandler, // PIO INT4
183  PININT5_IRQHandler, // PIO INT5
184  PININT6_IRQHandler, // PIO INT6
185  PININT7_IRQHandler, // PIO INT7
186 
187 }; /* End of g_pfnVectors */
188 
189 //*****************************************************************************
190 // Functions to carry out the initialization of RW and BSS data sections. These
191 // are written as separate functions rather than being inlined within the
192 // ResetISR() function in order to cope with MCUs with multiple banks of
193 // memory.
194 //*****************************************************************************
195 __attribute__ ((section(".after_vectors")))
196 void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
197  unsigned int *pulDest = (unsigned int*) start;
198  unsigned int *pulSrc = (unsigned int*) romstart;
199  unsigned int loop;
200  for (loop = 0; loop < len; loop = loop + 4)
201  *pulDest++ = *pulSrc++;
202 }
203 
204 __attribute__ ((section(".after_vectors")))
205 void bss_init(unsigned int start, unsigned int len) {
206  unsigned int *pulDest = (unsigned int*) start;
207  unsigned int loop;
208  for (loop = 0; loop < len; loop = loop + 4)
209  *pulDest++ = 0;
210 }
211 
212 //*****************************************************************************
213 // The following symbols are constructs generated by the linker, indicating
214 // the location of various points in the "Global Section Table". This table is
215 // created by the linker via the Code Red managed linker script mechanism. It
216 // contains the load address, execution address and length of each RW data
217 // section and the execution and length of each BSS (zero initialized) section.
218 //*****************************************************************************
219 extern unsigned int __data_section_table;
220 extern unsigned int __data_section_table_end;
221 extern unsigned int __bss_section_table;
222 extern unsigned int __bss_section_table_end;
223 
224 
225 //*****************************************************************************
226 // Reset entry point for your code.
227 // Sets up a simple runtime environment and initializes the C/C++
228 // library.
229 //*****************************************************************************
230 __attribute__ ((section(".after_vectors")))
231 void
232 ResetISR(void) {
233 
234  //
235  // Copy the data sections from flash to SRAM.
236  //
237  unsigned int LoadAddr, ExeAddr, SectionLen;
238  unsigned int *SectionTableAddr;
239 
240  // Load base address of Global Section Table
241  SectionTableAddr = &__data_section_table;
242 
243  // Copy the data sections from flash to SRAM.
244  while (SectionTableAddr < &__data_section_table_end) {
245  LoadAddr = *SectionTableAddr++;
246  ExeAddr = *SectionTableAddr++;
247  SectionLen = *SectionTableAddr++;
248  data_init(LoadAddr, ExeAddr, SectionLen);
249  }
250  // At this point, SectionTableAddr = &__bss_section_table;
251  // Zero fill the bss segment
252  while (SectionTableAddr < &__bss_section_table_end) {
253  ExeAddr = *SectionTableAddr++;
254  SectionLen = *SectionTableAddr++;
255  bss_init(ExeAddr, SectionLen);
256  }
257  extern void SystemInit(void);
258  SystemInit();
259 
260 #if defined (__cplusplus)
261  //
262  // Call C++ library initialisation
263  //
264  __libc_init_array();
265 #endif
266 
267 #if defined (__REDLIB__)
268  // Call the Redlib library, which in turn calls main()
269  __main() ;
270 #else
271  main();
272 #endif
273 
274  //
275  // main() shouldn't return, but if it does, we'll just enter an infinite loop
276  //
277  while (1) {
278  ;
279  }
280 }
281 
282 //*****************************************************************************
283 // Default exception handlers. Override the ones here by defining your own
284 // handler routines in your application code.
285 //*****************************************************************************
286 __attribute__ ((section(".after_vectors")))
287 void NMI_Handler(void)
288 {
289  while(1)
290  {
291  }
292 }
293 __attribute__ ((section(".after_vectors")))
294 void HardFault_Handler(void)
295 {
296  while(1)
297  {
298  }
299 }
300 __attribute__ ((section(".after_vectors")))
301 void SVCall_Handler(void)
302 {
303  while(1)
304  {
305  }
306 }
307 __attribute__ ((section(".after_vectors")))
308 void PendSV_Handler(void)
309 {
310  while(1)
311  {
312  }
313 }
314 
315 //*****************************************************************************
316 //
317 // Processor ends up here if an unexpected interrupt occurs or a specific
318 // handler is not present in the application code.
319 //
320 //*****************************************************************************
321 __attribute__ ((section(".after_vectors")))
322 void IntDefaultHandler(void)
323 {
324  while(1)
325  {
326  }
327 }
328 
329