Statistics
| Branch: | Tag: | Revision:

mongoose / examples / NXP_LPC4088 / LPCXpresso / Mongoose_LPC4088_QSB_BM / example / src / cr_startup_lpc40xx.c @ eaef5bd1

History | View | Annotate | Download (17.2 KB)

1
/* clang-format off */
2
//*****************************************************************************
3
// LPC407x_8x Microcontroller Startup code for use with LPCXpresso IDE
4
//
5
// Version : 140114
6
//*****************************************************************************
7
//
8
// Copyright(C) NXP Semiconductors, 2014
9
// All rights reserved.
10
//
11
// Software that is described herein is for illustrative purposes only
12
// which provides customers with programming information regarding the
13
// LPC products.  This software is supplied "AS IS" without any warranties of
14
// any kind, and NXP Semiconductors and its licensor disclaim any and
15
// all warranties, express or implied, including all implied warranties of
16
// merchantability, fitness for a particular purpose and non-infringement of
17
// intellectual property rights.  NXP Semiconductors assumes no responsibility
18
// or liability for the use of the software, conveys no license or rights under any
19
// patent, copyright, mask work right, or any other intellectual property rights in
20
// or to any products. NXP Semiconductors reserves the right to make changes
21
// in the software without notification. NXP Semiconductors also makes no
22
// representation or warranty that such application will be suitable for the
23
// specified use without further testing or modification.
24
//
25
// Permission to use, copy, modify, and distribute this software and its
26
// documentation is hereby granted, under NXP Semiconductors' and its
27
// licensor's relevant copyrights in the software, without fee, provided that it
28
// is used in conjunction with NXP Semiconductors microcontrollers.  This
29
// copyright, permission, and disclaimer notice must appear in all copies of
30
// this code.
31
//*****************************************************************************
32
#if defined (__cplusplus)
33
#ifdef __REDLIB__
34
#error Redlib does not support C++
35
#else
36
//*****************************************************************************
37
//
38
// The entry point for the C++ library startup
39
//
40
//*****************************************************************************
41
extern "C" {
42
    extern void __libc_init_array(void);
43
}
44
#endif
45
#endif
46

    
47
#define WEAK __attribute__ ((weak))
48
#define ALIAS(f) __attribute__ ((weak, alias (#f)))
49

    
50
//*****************************************************************************
51
#if defined (__cplusplus)
52
extern "C" {
53
#endif
54

    
55
//*****************************************************************************
56
#if defined (__USE_CMSIS) || defined (__USE_LPCOPEN)
57
// Declaration of external SystemInit function
58
extern void SystemInit(void);
59
#endif
60

    
61
//*****************************************************************************
62
//
63
// Forward declaration of the default handlers. These are aliased.
64
// When the application defines a handler (with the same name), this will 
65
// automatically take precedence over these weak definitions
66
//
67
//*****************************************************************************
68
     void ResetISR(void);
69
WEAK void NMI_Handler(void);
70
WEAK void HardFault_Handler(void);
71
WEAK void MemManage_Handler(void);
72
WEAK void BusFault_Handler(void);
73
WEAK void UsageFault_Handler(void);
74
WEAK void SVC_Handler(void);
75
WEAK void DebugMon_Handler(void);
76
WEAK void PendSV_Handler(void);
77
WEAK void SysTick_Handler(void);
78
WEAK void IntDefaultHandler(void);
79

    
80
//*****************************************************************************
81
//
82
// Forward declaration of the specific IRQ handlers. These are aliased
83
// to the IntDefaultHandler, which is a 'forever' loop. When the application
84
// defines a handler (with the same name), this will automatically take 
85
// precedence over these weak definitions
86
//
87
//*****************************************************************************
88
void WDT_IRQHandler(void) ALIAS(IntDefaultHandler);
89
void TIMER0_IRQHandler(void) ALIAS(IntDefaultHandler);
90
void TIMER1_IRQHandler(void) ALIAS(IntDefaultHandler);
91
void TIMER2_IRQHandler(void) ALIAS(IntDefaultHandler);
92
void TIMER3_IRQHandler(void) ALIAS(IntDefaultHandler);
93
void UART0_IRQHandler(void) ALIAS(IntDefaultHandler);
94
void UART1_IRQHandler(void) ALIAS(IntDefaultHandler);
95
void UART2_IRQHandler(void) ALIAS(IntDefaultHandler);
96
void UART3_IRQHandler(void) ALIAS(IntDefaultHandler);
97
void PWM1_IRQHandler(void) ALIAS(IntDefaultHandler);
98
void I2C0_IRQHandler(void) ALIAS(IntDefaultHandler);
99
void I2C1_IRQHandler(void) ALIAS(IntDefaultHandler);
100
void I2C2_IRQHandler(void) ALIAS(IntDefaultHandler);
101
void SPI_IRQHandler(void) ALIAS(IntDefaultHandler);
102
void SSP0_IRQHandler(void) ALIAS(IntDefaultHandler);
103
void SSP1_IRQHandler(void) ALIAS(IntDefaultHandler);
104
void PLL0_IRQHandler(void) ALIAS(IntDefaultHandler);
105
void RTC_IRQHandler(void) ALIAS(IntDefaultHandler);
106
void EINT0_IRQHandler(void) ALIAS(IntDefaultHandler);
107
void EINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
108
void EINT2_IRQHandler(void) ALIAS(IntDefaultHandler);
109
void EINT3_IRQHandler(void) ALIAS(IntDefaultHandler);
110
void ADC_IRQHandler(void) ALIAS(IntDefaultHandler);
111
void BOD_IRQHandler(void) ALIAS(IntDefaultHandler);
112
void USB_IRQHandler(void) ALIAS(IntDefaultHandler);
113
void CAN_IRQHandler(void) ALIAS(IntDefaultHandler);
114
void DMA_IRQHandler(void) ALIAS(IntDefaultHandler);
115
void I2S_IRQHandler(void) ALIAS(IntDefaultHandler);
116
#if defined (__USE_LPCOPEN)
117
void ETH_IRQHandler(void) ALIAS(IntDefaultHandler);
118
#else
119
void ENET_IRQHandler(void) ALIAS(IntDefaultHandler);
120
#endif
121
void RIT_IRQHandler(void) ALIAS(IntDefaultHandler);
122
void MCPWM_IRQHandler(void) ALIAS(IntDefaultHandler);
123
void QEI_IRQHandler(void) ALIAS(IntDefaultHandler);
124
void PLL1_IRQHandler(void) ALIAS(IntDefaultHandler);
125
void USBActivity_IRQHandler(void) ALIAS(IntDefaultHandler);
126
void CANActivity_IRQHandler(void) ALIAS(IntDefaultHandler);
127
#if defined (__USE_LPCOPEN)
128
void SDIO_IRQHandler(void) ALIAS(IntDefaultHandler);
129
#else
130
void MCI_IRQHandler(void) ALIAS(IntDefaultHandler);
131
#endif
132
void UART4_IRQHandler(void) ALIAS(IntDefaultHandler);
133
void SSP2_IRQHandler(void) ALIAS(IntDefaultHandler);
134
void LCD_IRQHandler(void) ALIAS(IntDefaultHandler);
135
void GPIO_IRQHandler(void) ALIAS(IntDefaultHandler);
136
void PWM0_IRQHandler(void) ALIAS(IntDefaultHandler);
137
void EEPROM_IRQHandler(void) ALIAS(IntDefaultHandler);
138

    
139
//*****************************************************************************
140
//
141
// The entry point for the application.
142
// __main() is the entry point for Redlib based applications
143
// main() is the entry point for Newlib based applications
144
//
145
//*****************************************************************************
146
#if defined (__REDLIB__)
147
extern void __main(void);
148
#endif
149
extern int main(void);
150
//*****************************************************************************
151
//
152
// External declaration for the pointer to the stack top from the Linker Script
153
//
154
//*****************************************************************************
155
extern void _vStackTop(void);
156

    
157
//*****************************************************************************
158
#if defined (__cplusplus)
159
} // extern "C"
160
#endif
161
//*****************************************************************************
162
//
163
// The vector table.
164
// This relies on the linker script to place at correct location in memory.
165
//
166
//*****************************************************************************
167
extern void (* const g_pfnVectors[])(void);
168
__attribute__ ((section(".isr_vector")))
169
void (* const g_pfnVectors[])(void) = {
170
    // Core Level - CM4
171
    &_vStackTop,                        // The initial stack pointer
172
    ResetISR,                           // The reset handler
173
    NMI_Handler,                        // The NMI handler
174
    HardFault_Handler,                  // The hard fault handler
175
    MemManage_Handler,                  // The MPU fault handler
176
    BusFault_Handler,                   // The bus fault handler
177
    UsageFault_Handler,                 // The usage fault handler
178
    0,                                  // Reserved
179
    0,                                  // Reserved
180
    0,                                  // Reserved
181
    0,                                  // Reserved
182
    SVC_Handler,                        // SVCall handler
183
    DebugMon_Handler,                   // Debug monitor handler
184
    0,                                  // Reserved
185
    PendSV_Handler,                     // The PendSV handler
186
    SysTick_Handler,                    // The SysTick handler
187

    
188
    // Chip Level - LPC40xx
189
    WDT_IRQHandler,                     // 16, 0x40 - WDT
190
    TIMER0_IRQHandler,                  // 17, 0x44 - TIMER0
191
    TIMER1_IRQHandler,                  // 18, 0x48 - TIMER1
192
    TIMER2_IRQHandler,                  // 19, 0x4c - TIMER2
193
    TIMER3_IRQHandler,                  // 20, 0x50 - TIMER3
194
    UART0_IRQHandler,                   // 21, 0x54 - UART0
195
    UART1_IRQHandler,                   // 22, 0x58 - UART1
196
    UART2_IRQHandler,                   // 23, 0x5c - UART2
197
    UART3_IRQHandler,                   // 24, 0x60 - UART3
198
    PWM1_IRQHandler,                    // 25, 0x64 - PWM1
199
    I2C0_IRQHandler,                    // 26, 0x68 - I2C0
200
    I2C1_IRQHandler,                    // 27, 0x6c - I2C1
201
    I2C2_IRQHandler,                    // 28, 0x70 - I2C2
202
    IntDefaultHandler,                  // 29, Not used
203
    SSP0_IRQHandler,                    // 30, 0x78 - SSP0
204
    SSP1_IRQHandler,                    // 31, 0x7c - SSP1
205
    PLL0_IRQHandler,                    // 32, 0x80 - PLL0 (Main PLL)
206
    RTC_IRQHandler,                     // 33, 0x84 - RTC
207
    EINT0_IRQHandler,                   // 34, 0x88 - EINT0
208
    EINT1_IRQHandler,                   // 35, 0x8c - EINT1
209
    EINT2_IRQHandler,                   // 36, 0x90 - EINT2
210
    EINT3_IRQHandler,                   // 37, 0x94 - EINT3
211
    ADC_IRQHandler,                     // 38, 0x98 - ADC
212
    BOD_IRQHandler,                     // 39, 0x9c - BOD
213
    USB_IRQHandler,                     // 40, 0xA0 - USB
214
    CAN_IRQHandler,                     // 41, 0xa4 - CAN
215
    DMA_IRQHandler,                     // 42, 0xa8 - GP DMA
216
    I2S_IRQHandler,                     // 43, 0xac - I2S
217
#if defined (__USE_LPCOPEN)
218
    ETH_IRQHandler,                     // 44, 0xb0 - Ethernet
219
    SDIO_IRQHandler,                    // 45, 0xb4 - SD/MMC card I/F
220
#else
221
    ENET_IRQHandler,                    // 44, 0xb0 - Ethernet
222
    MCI_IRQHandler,                     // 45, 0xb4 - SD/MMC card I/F
223
#endif                               
224
    MCPWM_IRQHandler,                   // 46, 0xb8 - Motor Control PWM
225
    QEI_IRQHandler,                     // 47, 0xbc - Quadrature Encoder
226
    PLL1_IRQHandler,                    // 48, 0xc0 - PLL1 (USB PLL)
227
    USBActivity_IRQHandler,             // 49, 0xc4 - USB Activity interrupt to wakeup
228
    CANActivity_IRQHandler,             // 50, 0xc8 - CAN Activity interrupt to wakeup
229
    UART4_IRQHandler,                   // 51, 0xcc - UART4
230
    SSP2_IRQHandler,                    // 52, 0xd0 - SSP2
231
    LCD_IRQHandler,                     // 53, 0xd4 - LCD
232
    GPIO_IRQHandler,                    // 54, 0xd8 - GPIO
233
    PWM0_IRQHandler,                    // 55, 0xdc - PWM0
234
    EEPROM_IRQHandler,                  // 56, 0xe0 - EEPROM
235

    
236
};
237

    
238
//*****************************************************************************
239
// Functions to carry out the initialization of RW and BSS data sections. These
240
// are written as separate functions rather than being inlined within the
241
// ResetISR() function in order to cope with MCUs with multiple banks of
242
// memory.
243
//*****************************************************************************
244
__attribute__ ((section(".after_vectors")))
245
void data_init(unsigned int romstart, unsigned int start, unsigned int len) {
246
    unsigned int *pulDest = (unsigned int*) start;
247
    unsigned int *pulSrc = (unsigned int*) romstart;
248
    unsigned int loop;
249
    for (loop = 0; loop < len; loop = loop + 4)
250
        *pulDest++ = *pulSrc++;
251
}
252

    
253
__attribute__ ((section(".after_vectors")))
254
void bss_init(unsigned int start, unsigned int len) {
255
    unsigned int *pulDest = (unsigned int*) start;
256
    unsigned int loop;
257
    for (loop = 0; loop < len; loop = loop + 4)
258
        *pulDest++ = 0;
259
}
260

    
261
//*****************************************************************************
262
// The following symbols are constructs generated by the linker, indicating
263
// the location of various points in the "Global Section Table". This table is
264
// created by the linker via the Code Red managed linker script mechanism. It
265
// contains the load address, execution address and length of each RW data
266
// section and the execution and length of each BSS (zero initialized) section.
267
//*****************************************************************************
268
extern unsigned int __data_section_table;
269
extern unsigned int __data_section_table_end;
270
extern unsigned int __bss_section_table;
271
extern unsigned int __bss_section_table_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
__attribute__ ((section(".after_vectors")))
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
    // 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 (__VFP_FP__) && !defined (__SOFTFP__)
307
/*
308
 * Code to enable the Cortex-M4 FPU only included
309
 * if appropriate build options have been selected.
310
 * Code taken from Section 7.1, Cortex-M4 TRM (DDI0439C)
311
 */  
312
    // Read CPACR (located at address 0xE000ED88)
313
    // Set bits 20-23 to enable CP10 and CP11 coprocessors
314
    // Write back the modified value to the CPACR
315
    asm volatile ("LDR.W R0, =0xE000ED88\n\t"
316
                  "LDR R1, [R0]\n\t"
317
                  "ORR R1, R1, #(0xF << 20)\n\t"
318
                  "STR R1, [R0]");  
319
#endif // (__VFP_FP__) && !(__SOFTFP__)
320

    
321
    // Check to see if we are running the code from a non-zero
322
    // address (eg RAM, external flash), in which case we need
323
    // to modify the VTOR register to tell the CPU that the
324
    // vector table is located at a non-0x0 address.
325

    
326
    // Note that we do not use the CMSIS register access mechanism,
327
    // as there is no guarantee that the project has been configured
328
    // to use CMSIS.
329
    unsigned int * pSCB_VTOR = (unsigned int *) 0xE000ED08;
330
    if ((unsigned int *)g_pfnVectors!=(unsigned int *) 0x00000000) {
331
        // CMSIS : SCB->VTOR = <address of vector table>
332
        *pSCB_VTOR = (unsigned int)g_pfnVectors;
333
    }
334

    
335
#if defined (__USE_CMSIS) || defined (__USE_LPCOPEN)
336
    SystemInit();
337
#endif
338

    
339
#if defined (__cplusplus)
340
    //
341
    // Call C++ library initialisation
342
    //
343
    __libc_init_array();
344
#endif
345

    
346
#if defined (__REDLIB__)
347
    // Call the Redlib library, which in turn calls main()
348
    __main() ;
349
#else
350
    main();
351
#endif
352

    
353
    //
354
    // main() shouldn't return, but if it does, we'll just enter an infinite loop 
355
    //
356
    while (1) {
357
        ;
358
    }
359
}
360

    
361
//*****************************************************************************
362
// Default exception handlers. Override the ones here by defining your own
363
// handler routines in your application code.
364
//*****************************************************************************
365
__attribute__ ((section(".after_vectors")))
366
void NMI_Handler(void)
367
{ while(1) {}
368
}
369

    
370
__attribute__ ((section(".after_vectors")))
371
void HardFault_Handler(void)
372
{ while(1) {}
373
}
374

    
375
__attribute__ ((section(".after_vectors")))
376
void MemManage_Handler(void)
377
{ while(1) {}
378
}
379

    
380
__attribute__ ((section(".after_vectors")))
381
void BusFault_Handler(void)
382
{ while(1) {}
383
}
384

    
385
__attribute__ ((section(".after_vectors")))
386
void UsageFault_Handler(void)
387
{ while(1) {}
388
}
389

    
390
__attribute__ ((section(".after_vectors")))
391
void SVC_Handler(void)
392
{ while(1) {}
393
}
394

    
395
__attribute__ ((section(".after_vectors")))
396
void DebugMon_Handler(void)
397
{ while(1) {}
398
}
399

    
400
__attribute__ ((section(".after_vectors")))
401
void PendSV_Handler(void)
402
{ while(1) {}
403
}
404

    
405
__attribute__ ((section(".after_vectors")))
406
void SysTick_Handler(void)
407
{ while(1) {}
408
}
409

    
410
//*****************************************************************************
411
//
412
// Processor ends up here if an unexpected interrupt occurs or a specific
413
// handler is not present in the application code.
414
//
415
//*****************************************************************************
416
__attribute__ ((section(".after_vectors")))
417
void IntDefaultHandler(void)
418
{ while(1) {}
419
}