1 |
582 |
jeremybenn |
//*****************************************************************************
|
2 |
|
|
// +--+
|
3 |
|
|
// | ++----+
|
4 |
|
|
// +-++ |
|
5 |
|
|
// | |
|
6 |
|
|
// +-+--+ |
|
7 |
|
|
// | +--+--+
|
8 |
|
|
// +----+ Copyright (c) 2009 Code Red Technologies Ltd.
|
9 |
|
|
//
|
10 |
|
|
// Microcontroller Startup code for use with Red Suite
|
11 |
|
|
//
|
12 |
|
|
// Software License Agreement
|
13 |
|
|
//
|
14 |
|
|
// The software is owned by Code Red Technologies and/or its suppliers, and is
|
15 |
|
|
// protected under applicable copyright laws. All rights are reserved. Any
|
16 |
|
|
// use in violation of the foregoing restrictions may subject the user to criminal
|
17 |
|
|
// sanctions under applicable laws, as well as to civil liability for the breach
|
18 |
|
|
// of the terms and conditions of this license.
|
19 |
|
|
//
|
20 |
|
|
// THIS SOFTWARE IS PROVIDED "AS IS". NO WARRANTIES, WHETHER EXPRESS, IMPLIED
|
21 |
|
|
// OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
|
22 |
|
|
// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE.
|
23 |
|
|
// USE OF THIS SOFTWARE FOR COMMERCIAL DEVELOPMENT AND/OR EDUCATION IS SUBJECT
|
24 |
|
|
// TO A CURRENT END USER LICENSE AGREEMENT (COMMERCIAL OR EDUCATIONAL) WITH
|
25 |
|
|
// CODE RED TECHNOLOGIES LTD.
|
26 |
|
|
//
|
27 |
|
|
//*****************************************************************************
|
28 |
|
|
#define WEAK __attribute__ ((weak))
|
29 |
|
|
#define ALIAS(f) __attribute__ ((weak, alias (#f)))
|
30 |
|
|
|
31 |
|
|
//*****************************************************************************
|
32 |
|
|
//
|
33 |
|
|
// Forward declaration of the default handlers.
|
34 |
|
|
//
|
35 |
|
|
//*****************************************************************************
|
36 |
|
|
void Reset_Handler(void);
|
37 |
|
|
void ResetISR(void) ALIAS(Reset_Handler);
|
38 |
|
|
static void NMI_Handler(void);
|
39 |
|
|
static void HardFault_Handler(void);
|
40 |
|
|
static void MemManage_Handler(void) __attribute__((naked));
|
41 |
|
|
static void BusFault_Handler(void) __attribute__((naked));
|
42 |
|
|
static void UsageFault_Handler(void) __attribute__((naked));
|
43 |
|
|
static void DebugMon_Handler(void);
|
44 |
|
|
|
45 |
|
|
//*****************************************************************************
|
46 |
|
|
//
|
47 |
|
|
// Forward declaration of the specific IRQ handlers. These are aliased
|
48 |
|
|
// to the IntDefaultHandler, which is a 'forever' loop. When the application
|
49 |
|
|
// defines a handler (with the same name), this will automatically take
|
50 |
|
|
// precedence over these weak definitions
|
51 |
|
|
//
|
52 |
|
|
//*****************************************************************************
|
53 |
|
|
void WDT_IRQHandler(void) ALIAS(IntDefaultHandler);
|
54 |
|
|
void TIMER0_IRQHandler(void) ALIAS(IntDefaultHandler);
|
55 |
|
|
void TIMER1_IRQHandler(void) ALIAS(IntDefaultHandler);
|
56 |
|
|
void TIMER2_IRQHandler(void) ALIAS(IntDefaultHandler);
|
57 |
|
|
void TIMER3_IRQHandler(void) ALIAS(IntDefaultHandler);
|
58 |
|
|
void UART0_IRQHandler(void) ALIAS(IntDefaultHandler);
|
59 |
|
|
void UART1_IRQHandler(void) ALIAS(IntDefaultHandler);
|
60 |
|
|
void UART2_IRQHandler(void) ALIAS(IntDefaultHandler);
|
61 |
|
|
void UART3_IRQHandler(void) ALIAS(IntDefaultHandler);
|
62 |
|
|
void PWM1_IRQHandler(void) ALIAS(IntDefaultHandler);
|
63 |
|
|
void I2C0_IRQHandler(void) ALIAS(IntDefaultHandler);
|
64 |
|
|
void I2C1_IRQHandler(void) ALIAS(IntDefaultHandler);
|
65 |
|
|
void I2C2_IRQHandler(void) ALIAS(IntDefaultHandler);
|
66 |
|
|
void SPI_IRQHandler(void) ALIAS(IntDefaultHandler);
|
67 |
|
|
void SSP0_IRQHandler(void) ALIAS(IntDefaultHandler);
|
68 |
|
|
void SSP1_IRQHandler(void) ALIAS(IntDefaultHandler);
|
69 |
|
|
void PLL0_IRQHandler(void) ALIAS(IntDefaultHandler);
|
70 |
|
|
void RTC_IRQHandler(void) ALIAS(IntDefaultHandler);
|
71 |
|
|
void EINT0_IRQHandler(void) ALIAS(IntDefaultHandler);
|
72 |
|
|
void EINT1_IRQHandler(void) ALIAS(IntDefaultHandler);
|
73 |
|
|
void EINT2_IRQHandler(void) ALIAS(IntDefaultHandler);
|
74 |
|
|
void EINT3_IRQHandler(void) ALIAS(IntDefaultHandler);
|
75 |
|
|
void ADC_IRQHandler(void) ALIAS(IntDefaultHandler);
|
76 |
|
|
void BOD_IRQHandler(void) ALIAS(IntDefaultHandler);
|
77 |
|
|
void USB_IRQHandler(void) ALIAS(IntDefaultHandler);
|
78 |
|
|
void CAN_IRQHandler(void) ALIAS(IntDefaultHandler);
|
79 |
|
|
void DMA_IRQHandler(void) ALIAS(IntDefaultHandler);
|
80 |
|
|
void I2S_IRQHandler(void) ALIAS(IntDefaultHandler);
|
81 |
|
|
void ENET_IRQHandler(void) ALIAS(IntDefaultHandler);
|
82 |
|
|
void RIT_IRQHandler(void) ALIAS(IntDefaultHandler);
|
83 |
|
|
void MCPWM_IRQHandler(void) ALIAS(IntDefaultHandler);
|
84 |
|
|
void QEI_IRQHandler(void) ALIAS(IntDefaultHandler);
|
85 |
|
|
void PLL1_IRQHandler(void) ALIAS(IntDefaultHandler);
|
86 |
|
|
|
87 |
|
|
extern void xPortSysTickHandler(void);
|
88 |
|
|
extern void xPortPendSVHandler(void);
|
89 |
|
|
extern void vPortSVCHandler( void );
|
90 |
|
|
|
91 |
|
|
|
92 |
|
|
//*****************************************************************************
|
93 |
|
|
//
|
94 |
|
|
// The entry point for the C++ library startup
|
95 |
|
|
//
|
96 |
|
|
//*****************************************************************************
|
97 |
|
|
extern WEAK void __libc_init_array(void);
|
98 |
|
|
|
99 |
|
|
//*****************************************************************************
|
100 |
|
|
//
|
101 |
|
|
// The entry point for the application.
|
102 |
|
|
// __main() is the entry point for redlib based applications
|
103 |
|
|
// main() is the entry point for newlib based applications
|
104 |
|
|
//
|
105 |
|
|
//*****************************************************************************
|
106 |
|
|
extern WEAK void __main(void);
|
107 |
|
|
extern WEAK void main(void);
|
108 |
|
|
//*****************************************************************************
|
109 |
|
|
//
|
110 |
|
|
// External declaration for the pointer to the stack top from the Linker Script
|
111 |
|
|
//
|
112 |
|
|
//*****************************************************************************
|
113 |
|
|
extern void _vStackTop;
|
114 |
|
|
|
115 |
|
|
//*****************************************************************************
|
116 |
|
|
//
|
117 |
|
|
// The vector table.
|
118 |
|
|
// This relies on the linker script to place at correct location in memory.
|
119 |
|
|
//
|
120 |
|
|
//*****************************************************************************
|
121 |
|
|
__attribute__ ((section(".isr_vector")))
|
122 |
|
|
void (* const g_pfnVectors[])(void) =
|
123 |
|
|
{
|
124 |
|
|
// Core Level - CM3
|
125 |
|
|
(void *)&_vStackTop, // The initial stack pointer
|
126 |
|
|
Reset_Handler, // The reset handler
|
127 |
|
|
NMI_Handler, // The NMI handler
|
128 |
|
|
HardFault_Handler, // The hard fault handler
|
129 |
|
|
MemManage_Handler, // The MPU fault handler
|
130 |
|
|
BusFault_Handler, // The bus fault handler
|
131 |
|
|
UsageFault_Handler, // The usage fault handler
|
132 |
|
|
0, // Reserved
|
133 |
|
|
0, // Reserved
|
134 |
|
|
0, // Reserved
|
135 |
|
|
0, // Reserved
|
136 |
|
|
vPortSVCHandler, // SVCall handler
|
137 |
|
|
DebugMon_Handler, // Debug monitor handler
|
138 |
|
|
0, // Reserved
|
139 |
|
|
xPortPendSVHandler, // The PendSV handler
|
140 |
|
|
xPortSysTickHandler, // The SysTick handler
|
141 |
|
|
|
142 |
|
|
// Chip Level - LPC17
|
143 |
|
|
WDT_IRQHandler, // 16, 0x40 - WDT
|
144 |
|
|
TIMER0_IRQHandler, // 17, 0x44 - TIMER0
|
145 |
|
|
TIMER1_IRQHandler, // 18, 0x48 - TIMER1
|
146 |
|
|
TIMER2_IRQHandler, // 19, 0x4c - TIMER2
|
147 |
|
|
TIMER3_IRQHandler, // 20, 0x50 - TIMER3
|
148 |
|
|
UART0_IRQHandler, // 21, 0x54 - UART0
|
149 |
|
|
UART1_IRQHandler, // 22, 0x58 - UART1
|
150 |
|
|
UART2_IRQHandler, // 23, 0x5c - UART2
|
151 |
|
|
UART3_IRQHandler, // 24, 0x60 - UART3
|
152 |
|
|
PWM1_IRQHandler, // 25, 0x64 - PWM1
|
153 |
|
|
I2C0_IRQHandler, // 26, 0x68 - I2C0
|
154 |
|
|
I2C1_IRQHandler, // 27, 0x6c - I2C1
|
155 |
|
|
I2C2_IRQHandler, // 28, 0x70 - I2C2
|
156 |
|
|
SPI_IRQHandler, // 29, 0x74 - SPI
|
157 |
|
|
SSP0_IRQHandler, // 30, 0x78 - SSP0
|
158 |
|
|
SSP1_IRQHandler, // 31, 0x7c - SSP1
|
159 |
|
|
PLL0_IRQHandler, // 32, 0x80 - PLL0 (Main PLL)
|
160 |
|
|
RTC_IRQHandler, // 33, 0x84 - RTC
|
161 |
|
|
EINT0_IRQHandler, // 34, 0x88 - EINT0
|
162 |
|
|
EINT1_IRQHandler, // 35, 0x8c - EINT1
|
163 |
|
|
EINT2_IRQHandler, // 36, 0x90 - EINT2
|
164 |
|
|
EINT3_IRQHandler, // 37, 0x94 - EINT3
|
165 |
|
|
ADC_IRQHandler, // 38, 0x98 - ADC
|
166 |
|
|
BOD_IRQHandler, // 39, 0x9c - BOD
|
167 |
|
|
USB_IRQHandler, // 40, 0xA0 - USB
|
168 |
|
|
CAN_IRQHandler, // 41, 0xa4 - CAN
|
169 |
|
|
DMA_IRQHandler, // 42, 0xa8 - GP DMA
|
170 |
|
|
I2S_IRQHandler, // 43, 0xac - I2S
|
171 |
|
|
ENET_IRQHandler, // Ethernet.
|
172 |
|
|
RIT_IRQHandler, // 45, 0xb4 - RITINT
|
173 |
|
|
MCPWM_IRQHandler, // 46, 0xb8 - Motor Control PWM
|
174 |
|
|
QEI_IRQHandler, // 47, 0xbc - Quadrature Encoder
|
175 |
|
|
PLL1_IRQHandler, // 48, 0xc0 - PLL1 (USB PLL)
|
176 |
|
|
};
|
177 |
|
|
|
178 |
|
|
//*****************************************************************************
|
179 |
|
|
//
|
180 |
|
|
// The following are constructs created by the linker, indicating where the
|
181 |
|
|
// the "data" and "bss" segments reside in memory. The initializers for the
|
182 |
|
|
// for the "data" segment resides immediately following the "text" segment.
|
183 |
|
|
//
|
184 |
|
|
//*****************************************************************************
|
185 |
|
|
extern unsigned long _etext;
|
186 |
|
|
extern unsigned long _data;
|
187 |
|
|
extern unsigned long _edata;
|
188 |
|
|
extern unsigned long _bss;
|
189 |
|
|
extern unsigned long _ebss;
|
190 |
|
|
extern unsigned long __privileged_data_start__;
|
191 |
|
|
extern unsigned long __privileged_data_end__;
|
192 |
|
|
|
193 |
|
|
//*****************************************************************************
|
194 |
|
|
// Reset entry point for your code.
|
195 |
|
|
// Sets up a simple runtime environment and initializes the C/C++
|
196 |
|
|
// library.
|
197 |
|
|
//
|
198 |
|
|
//*****************************************************************************
|
199 |
|
|
void Reset_Handler(void)
|
200 |
|
|
{
|
201 |
|
|
unsigned long *pulSrc, *pulDest;
|
202 |
|
|
|
203 |
|
|
//
|
204 |
|
|
// Copy the data segment initializers from flash to SRAM.
|
205 |
|
|
//
|
206 |
|
|
pulSrc = &_etext;
|
207 |
|
|
for(pulDest = &_data; pulDest < &_edata; )
|
208 |
|
|
{
|
209 |
|
|
*pulDest++ = *pulSrc++;
|
210 |
|
|
}
|
211 |
|
|
|
212 |
|
|
//
|
213 |
|
|
// Zero fill the bss segment. This is done with inline assembly since this
|
214 |
|
|
// will clear the value of pulDest if it is not kept in a register.
|
215 |
|
|
//
|
216 |
|
|
__asm(" ldr r0, =_bss\n"
|
217 |
|
|
" ldr r1, =_ebss\n"
|
218 |
|
|
" mov r2, #0\n"
|
219 |
|
|
" .thumb_func\n"
|
220 |
|
|
"zero_loop_bss:\n"
|
221 |
|
|
" cmp r0, r1\n"
|
222 |
|
|
" it lt\n"
|
223 |
|
|
" strlt r2, [r0], #4\n"
|
224 |
|
|
" blt zero_loop_bss");
|
225 |
|
|
|
226 |
|
|
|
227 |
|
|
//
|
228 |
|
|
// Call C++ library initilisation, if present
|
229 |
|
|
//
|
230 |
|
|
if (__libc_init_array)
|
231 |
|
|
__libc_init_array() ;
|
232 |
|
|
|
233 |
|
|
//
|
234 |
|
|
// Call the application's entry point.
|
235 |
|
|
// __main() is the entry point for redlib based applications (which calls main())
|
236 |
|
|
// main() is the entry point for newlib based applications
|
237 |
|
|
//
|
238 |
|
|
if (__main)
|
239 |
|
|
__main() ;
|
240 |
|
|
else
|
241 |
|
|
main() ;
|
242 |
|
|
|
243 |
|
|
//
|
244 |
|
|
// main() shouldn't return, but if it does, we'll just enter an infinite loop
|
245 |
|
|
//
|
246 |
|
|
while (1) {
|
247 |
|
|
;
|
248 |
|
|
}
|
249 |
|
|
}
|
250 |
|
|
|
251 |
|
|
//*****************************************************************************
|
252 |
|
|
//
|
253 |
|
|
// This is the code that gets called when the processor receives a NMI. This
|
254 |
|
|
// simply enters an infinite loop, preserving the system state for examination
|
255 |
|
|
// by a debugger.
|
256 |
|
|
//
|
257 |
|
|
//*****************************************************************************
|
258 |
|
|
static void NMI_Handler(void)
|
259 |
|
|
{
|
260 |
|
|
while(1)
|
261 |
|
|
{
|
262 |
|
|
}
|
263 |
|
|
}
|
264 |
|
|
|
265 |
|
|
static void HardFault_Handler(void)
|
266 |
|
|
{
|
267 |
|
|
for( ;; );
|
268 |
|
|
}
|
269 |
|
|
|
270 |
|
|
void pop_registers_from_fault_stack(unsigned int * hardfault_args)
|
271 |
|
|
{
|
272 |
|
|
unsigned int stacked_r0;
|
273 |
|
|
unsigned int stacked_r1;
|
274 |
|
|
unsigned int stacked_r2;
|
275 |
|
|
unsigned int stacked_r3;
|
276 |
|
|
unsigned int stacked_r12;
|
277 |
|
|
unsigned int stacked_lr;
|
278 |
|
|
unsigned int stacked_pc;
|
279 |
|
|
unsigned int stacked_psr;
|
280 |
|
|
|
281 |
|
|
stacked_r0 = ((unsigned long) hardfault_args[0]);
|
282 |
|
|
stacked_r1 = ((unsigned long) hardfault_args[1]);
|
283 |
|
|
stacked_r2 = ((unsigned long) hardfault_args[2]);
|
284 |
|
|
stacked_r3 = ((unsigned long) hardfault_args[3]);
|
285 |
|
|
|
286 |
|
|
stacked_r12 = ((unsigned long) hardfault_args[4]);
|
287 |
|
|
stacked_lr = ((unsigned long) hardfault_args[5]);
|
288 |
|
|
stacked_pc = ((unsigned long) hardfault_args[6]);
|
289 |
|
|
stacked_psr = ((unsigned long) hardfault_args[7]);
|
290 |
|
|
|
291 |
|
|
/* Inspect stacked_pc to locate the offending instruction. */
|
292 |
|
|
for( ;; );
|
293 |
|
|
}
|
294 |
|
|
|
295 |
|
|
static void MemManage_Handler(void)
|
296 |
|
|
{
|
297 |
|
|
__asm volatile
|
298 |
|
|
(
|
299 |
|
|
" tst lr, #4 \n"
|
300 |
|
|
" ite eq \n"
|
301 |
|
|
" mrseq r0, msp \n"
|
302 |
|
|
" mrsne r0, psp \n"
|
303 |
|
|
" ldr r1, [r0, #24] \n"
|
304 |
|
|
" ldr r2, handler2_address_const \n"
|
305 |
|
|
" bx r2 \n"
|
306 |
|
|
" handler2_address_const: .word pop_registers_from_fault_stack \n"
|
307 |
|
|
);
|
308 |
|
|
}
|
309 |
|
|
|
310 |
|
|
static void BusFault_Handler(void)
|
311 |
|
|
{
|
312 |
|
|
__asm volatile
|
313 |
|
|
(
|
314 |
|
|
" tst lr, #4 \n"
|
315 |
|
|
" ite eq \n"
|
316 |
|
|
" mrseq r0, msp \n"
|
317 |
|
|
" mrsne r0, psp \n"
|
318 |
|
|
" ldr r1, [r0, #24] \n"
|
319 |
|
|
" ldr r2, handler3_address_const \n"
|
320 |
|
|
" bx r2 \n"
|
321 |
|
|
" handler3_address_const: .word pop_registers_from_fault_stack \n"
|
322 |
|
|
);
|
323 |
|
|
}
|
324 |
|
|
|
325 |
|
|
static void UsageFault_Handler(void)
|
326 |
|
|
{
|
327 |
|
|
__asm volatile
|
328 |
|
|
(
|
329 |
|
|
" tst lr, #4 \n"
|
330 |
|
|
" ite eq \n"
|
331 |
|
|
" mrseq r0, msp \n"
|
332 |
|
|
" mrsne r0, psp \n"
|
333 |
|
|
" ldr r1, [r0, #24] \n"
|
334 |
|
|
" ldr r2, handler4_address_const \n"
|
335 |
|
|
" bx r2 \n"
|
336 |
|
|
" handler4_address_const: .word pop_registers_from_fault_stack \n"
|
337 |
|
|
);
|
338 |
|
|
}
|
339 |
|
|
|
340 |
|
|
static void DebugMon_Handler(void)
|
341 |
|
|
{
|
342 |
|
|
while(1)
|
343 |
|
|
{
|
344 |
|
|
}
|
345 |
|
|
}
|
346 |
|
|
|
347 |
|
|
//*****************************************************************************
|
348 |
|
|
//
|
349 |
|
|
// Processor ends up here if an unexpected interrupt occurs or a handler
|
350 |
|
|
// is not present in the application code.
|
351 |
|
|
//
|
352 |
|
|
//*****************************************************************************
|
353 |
|
|
static void IntDefaultHandler(void)
|
354 |
|
|
{
|
355 |
|
|
//
|
356 |
|
|
// Go into an infinite loop.
|
357 |
|
|
//
|
358 |
|
|
while(1)
|
359 |
|
|
{
|
360 |
|
|
}
|
361 |
|
|
}
|