summaryrefslogtreecommitdiff
path: root/software/mpu/src/cr_startup_lpc13.c
blob: 48a97846f00980bb8f85e9c794d9d4c2788332b6 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
//*****************************************************************************
//   +--+       
//   | ++----+   
//   +-++    |  
//     |     |  
//   +-+--+  |   
//   | +--+--+  
//   +----+    Copyright (c) 2009-10 Code Red Technologies Ltd.
//
// Microcontroller Startup code for use with Red Suite
//
// Software License Agreement
// 
// The software is owned by Code Red Technologies and/or its suppliers, and is 
// protected under applicable copyright laws.  All rights are reserved.  Any 
// use in violation of the foregoing restrictions may subject the user to criminal 
// sanctions under applicable laws, as well as to civil liability for the breach 
// of the terms and conditions of this license.
// 
// THIS SOFTWARE IS PROVIDED "AS IS".  NO WARRANTIES, WHETHER EXPRESS, IMPLIED
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
// USE OF THIS SOFTWARE FOR COMMERCIAL DEVELOPMENT AND/OR EDUCATION IS SUBJECT
// TO A CURRENT END USER LICENSE AGREEMENT (COMMERCIAL OR EDUCATIONAL) WITH
// CODE RED TECHNOLOGIES LTD. 
//
//*****************************************************************************
#if defined (__cplusplus)
#ifdef __REDLIB__
#error Redlib does not support C++
#else
//*****************************************************************************
//
// The entry point for the C++ library startup
//
//*****************************************************************************
extern "C" {
	extern void __libc_init_array(void);
}
#endif
#endif

#define WEAK __attribute__ ((weak))
#define ALIAS(f) __attribute__ ((weak, alias (#f)))

// Code Red - if CMSIS is being used, then SystemInit() routine
// will be called by startup code rather than in application's main()
#if defined (__USE_CMSIS)
#include "system_LPC13xx.h"
#endif

//*****************************************************************************
#if defined (__cplusplus)
extern "C" {
#endif

//*****************************************************************************
//
// Forward declaration of the default handlers. These are aliased.
// When the application defines a handler (with the same name), this will 
// automatically take precedence over these weak definitions
//
//*****************************************************************************
void ResetISR(void);
WEAK void NMI_Handler(void);
WEAK void HardFault_Handler(void);
WEAK void MemManage_Handler(void);
WEAK void BusFault_Handler(void);
WEAK void UsageFault_Handler(void);
WEAK void SVCall_Handler(void);
WEAK void DebugMon_Handler(void);
WEAK void PendSV_Handler(void);
WEAK void SysTick_Handler(void);
WEAK void IntDefaultHandler(void);
//*****************************************************************************
//
// Forward declaration of the specific IRQ handlers. These are aliased
// to the IntDefaultHandler, which is a 'forever' loop. When the application
// defines a handler (with the same name), this will automatically take 
// precedence over these weak definitions
//
//*****************************************************************************

void I2C_IRQHandler(void) ALIAS(IntDefaultHandler);
void TIMER16_0_IRQHandler(void) ALIAS(IntDefaultHandler);
void TIMER16_1_IRQHandler(void) ALIAS(IntDefaultHandler);
void TIMER32_0_IRQHandler(void) ALIAS(IntDefaultHandler);
void TIMER32_1_IRQHandler(void) ALIAS(IntDefaultHandler);
void SSP_IRQHandler(void) ALIAS(IntDefaultHandler);
void UART_IRQHandler(void) ALIAS(IntDefaultHandler);
void USB_IRQHandler(void) ALIAS(IntDefaultHandler);
void USB_FIQHandler(void) ALIAS(IntDefaultHandler);
void ADC_IRQHandler(void) ALIAS(IntDefaultHandler);
void WDT_IRQHandler(void) ALIAS(IntDefaultHandler);
void BOD_IRQHandler(void) ALIAS(IntDefaultHandler);
void FMC_IRQHandler(void) ALIAS(IntDefaultHandler);
void PIOINT3_IRQHandler(void) ALIAS(IntDefaultHandler);
void PIOINT2_IRQHandler(void) ALIAS(IntDefaultHandler);
void PIOINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
void PIOINT0_IRQHandler(void) ALIAS(IntDefaultHandler);
void WAKEUP_IRQHandler(void) ALIAS(IntDefaultHandler);

extern void xPortSysTickHandler(void);
extern void xPortPendSVHandler(void);
extern void vPortSVCHandler( void );

//*****************************************************************************
//
// The entry point for the application.
// __main() is the entry point for Redlib based applications
// main() is the entry point for Newlib based applications
//
//*****************************************************************************
#if defined (__REDLIB__)
extern void __main(void);
#endif
extern int main(void);
//*****************************************************************************
//
// External declaration for the pointer to the stack top from the Linker Script
//
//*****************************************************************************
extern void _vStackTop(void);

//*****************************************************************************
#if defined (__cplusplus)
} // extern "C"
#endif
//*****************************************************************************
//
// The vector table.  Note that the proper constructs must be placed on this to
// ensure that it ends up at physical address 0x0000.0000.
//
//*****************************************************************************
extern void (* const g_pfnVectors[])(void);
__attribute__ ((section(".isr_vector")))
void (* const g_pfnVectors[])(void) = {
	// Core Level - CM3
		&_vStackTop, // The initial stack pointer
		ResetISR, // The reset handler
		NMI_Handler, // The NMI handler
		HardFault_Handler, // The hard fault handler
		MemManage_Handler, // The MPU fault handler
		BusFault_Handler, // The bus fault handler
		UsageFault_Handler, // The usage fault handler
		0, // Reserved
		0, // Reserved
		0, // Reserved
		0, // Reserved
		vPortSVCHandler,                        // SVCall handler
		DebugMon_Handler, // Debug monitor handler
		0, // Reserved
		xPortPendSVHandler,                     // The PendSV handler
		xPortSysTickHandler,                    // The SysTick handler


		// Wakeup sources (40 ea.) for the I/O pins:
		//   PIO0 (0:11)
		//   PIO1 (0:11)
		//   PIO2 (0:11)
		//   PIO3 (0:3)
		WAKEUP_IRQHandler, // PIO0_0  Wakeup
		WAKEUP_IRQHandler, // PIO0_1  Wakeup
		WAKEUP_IRQHandler, // PIO0_2  Wakeup
		WAKEUP_IRQHandler, // PIO0_3  Wakeup
		WAKEUP_IRQHandler, // PIO0_4  Wakeup
		WAKEUP_IRQHandler, // PIO0_5  Wakeup
		WAKEUP_IRQHandler, // PIO0_6  Wakeup
		WAKEUP_IRQHandler, // PIO0_7  Wakeup
		WAKEUP_IRQHandler, // PIO0_8  Wakeup
		WAKEUP_IRQHandler, // PIO0_9  Wakeup
		WAKEUP_IRQHandler, // PIO0_10 Wakeup
		WAKEUP_IRQHandler, // PIO0_11 Wakeup

		WAKEUP_IRQHandler, // PIO1_0  Wakeup
		WAKEUP_IRQHandler, // PIO1_1  Wakeup
		WAKEUP_IRQHandler, // PIO1_2  Wakeup
		WAKEUP_IRQHandler, // PIO1_3  Wakeup
		WAKEUP_IRQHandler, // PIO1_4  Wakeup
		WAKEUP_IRQHandler, // PIO1_5  Wakeup
		WAKEUP_IRQHandler, // PIO1_6  Wakeup
		WAKEUP_IRQHandler, // PIO1_7  Wakeup
		WAKEUP_IRQHandler, // PIO1_8  Wakeup
		WAKEUP_IRQHandler, // PIO1_9  Wakeup
		WAKEUP_IRQHandler, // PIO1_10 Wakeup
		WAKEUP_IRQHandler, // PIO1_11 Wakeup

		WAKEUP_IRQHandler, // PIO2_0  Wakeup
		WAKEUP_IRQHandler, // PIO2_1  Wakeup
		WAKEUP_IRQHandler, // PIO2_2  Wakeup
		WAKEUP_IRQHandler, // PIO2_3  Wakeup
		WAKEUP_IRQHandler, // PIO2_4  Wakeup
		WAKEUP_IRQHandler, // PIO2_5  Wakeup
		WAKEUP_IRQHandler, // PIO2_6  Wakeup
		WAKEUP_IRQHandler, // PIO2_7  Wakeup
		WAKEUP_IRQHandler, // PIO2_8  Wakeup
		WAKEUP_IRQHandler, // PIO2_9  Wakeup
		WAKEUP_IRQHandler, // PIO2_10 Wakeup
		WAKEUP_IRQHandler, // PIO2_11 Wakeup

		WAKEUP_IRQHandler, // PIO3_0  Wakeup
		WAKEUP_IRQHandler, // PIO3_1  Wakeup
		WAKEUP_IRQHandler, // PIO3_2  Wakeup
		WAKEUP_IRQHandler, // PIO3_3  Wakeup

		I2C_IRQHandler, // I2C0
		TIMER16_0_IRQHandler, // CT16B0 (16-bit Timer 0)
		TIMER16_1_IRQHandler, // CT16B1 (16-bit Timer 1)
		TIMER32_0_IRQHandler, // CT32B0 (32-bit Timer 0)
		TIMER32_1_IRQHandler, // CT32B1 (32-bit Timer 1)
		SSP_IRQHandler, // SSP0
		UART_IRQHandler, // UART0

		USB_IRQHandler, // USB IRQ
		USB_FIQHandler, // USB FIQ

		ADC_IRQHandler, // ADC   (A/D Converter)
		WDT_IRQHandler, // WDT   (Watchdog Timer)
		BOD_IRQHandler, // BOD   (Brownout Detect)
		FMC_IRQHandler, // Flash (IP2111 Flash Memory Controller)
		PIOINT3_IRQHandler, // PIO INT3
		PIOINT2_IRQHandler, // PIO INT2
		PIOINT1_IRQHandler, // PIO INT1
		PIOINT0_IRQHandler, // PIO INT0

	};

//*****************************************************************************
//
// The following are constructs created by the linker, indicating where the
// the "data" and "bss" segments reside in memory.  The initializers for the
// for the "data" segment resides immediately following the "text" segment.
//
//*****************************************************************************
extern unsigned long _etext;
extern unsigned long _data;
extern unsigned long _edata;
extern unsigned long _bss;
extern unsigned long _ebss;

//*****************************************************************************
//
// This is the code that gets called when the processor first starts execution
// following a reset event.  Only the absolutely necessary set is performed,
// after which the application supplied entry() routine is called.  Any fancy
// actions (such as making decisions based on the reset cause register, and
// resetting the bits in that register) are left solely in the hands of the
// application.
//
//*****************************************************************************
void
ResetISR(void) {
	unsigned long *pulSrc, *pulDest;

	//
	// Copy the data segment initializers from flash to SRAM.
	//
	pulSrc = &_etext;
	for (pulDest = &_data; pulDest < &_edata;) {
		*pulDest++ = *pulSrc++;
	}

	//
	// Zero fill the bss segment.  This is done with inline assembly since this
	// will clear the value of pulDest if it is not kept in a register.
	//
	__asm("    ldr     r0, =_bss\n"
			"    ldr     r1, =_ebss\n"
			"    mov     r2, #0\n"
			"    .thumb_func\n"
			"zero_loop:\n"
			"        cmp     r0, r1\n"
			"        it      lt\n"
			"        strlt   r2, [r0], #4\n"
			"        blt     zero_loop");

#ifdef __USE_CMSIS
	SystemInit();
#endif

#if defined (__cplusplus)
	//
	// Call C++ library initialisation
	//
	__libc_init_array();
#endif

#if defined (__REDLIB__)
	// Call the Redlib library, which in turn calls main()
	__main() ;
#else
	main();
#endif
	//
	// main() shouldn't return, but if it does, we'll just enter an infinite loop 
	//
	while (1) {
		;
	}
}

//*****************************************************************************
//
// This is the code that gets called when the processor receives a NMI.  This
// simply enters an infinite loop, preserving the system state for examination
// by a debugger.
//
//*****************************************************************************
void NMI_Handler(void) {
	while (1) {
	}
}

void HardFault_Handler(void) {
	while (1) {
	}
}

void MemManage_Handler(void) {
	while (1) {
	}
}

void BusFault_Handler(void) {
	while (1) {
	}
}

void UsageFault_Handler(void) {
	while (1) {
	}
}

void SVCall_Handler(void) {
	while (1) {
	}
}

void DebugMon_Handler(void) {
	while (1) {
	}
}

void PendSV_Handler(void) {
	while (1) {
	}
}

void SysTick_Handler(void) {
	while (1) {
	}
}

//*****************************************************************************
//
// Processor ends up here if an unexpected interrupt occurs or a handler
// is not present in the application code.
//
//*****************************************************************************

void IntDefaultHandler(void) {
	//
	// Go into an infinite loop.
	//
	while (1) {
	}
}