-
Notifications
You must be signed in to change notification settings - Fork 3
/
startup.c
127 lines (104 loc) · 2.76 KB
/
startup.c
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
#include <stdint.h>
#include "LPC1114.h"
// Sections as defined in the linker script
extern uint32_t __data_load;
extern uint32_t __data_start;
extern uint32_t __data_end;
extern uint32_t __bss_start;
extern uint32_t __bss_end;
extern uint32_t __ram_end;
// Application entry point main() declaration
int main(void);
// Default Handler
void Default_Handler(void) {
while(1);
}
// Interrupt/Exception Handlers
void Reset_Handler(void);
void SysTick_Handler(void) __attribute__((weak, alias("Default_Handler")));
void I2C_Handler(void) __attribute__ ((weak, alias("Default_Handler")));
void UART_Handler(void) __attribute__ ((weak, alias("Default_Handler")));
void GPIO1_Handler(void) __attribute__ ((weak, alias("Default_Handler")));
// Interrupt Vector at address 0x00000000
void (* __vectors[]) (void) __attribute__ ((section(".vectors"))) = {
(void (*) (void)) &__ram_end,
Reset_Handler, // RESET
0, // NMI
0, // Hard Fault
0, // Reserved
0, // Reserved
0, // Reserved
0, // Reserved
0, // Reserved
0, // Reserved
0, // Reserved
0, // SVCall
0, // Reserved
0, // Reserved
0, // PendSV
SysTick_Handler, // SysTick
0, // IRQ0
0, // IRQ1
0, // IRQ2
0, // IRQ3
0, // IRQ4
0, // IRQ5
0, // IRQ6
0, // IRQ7
0, // IRQ8
0, // IRQ9
0, // IRQ10
0, // IRQ11
0, // IRQ12
0, // IRQ13
0, // IRQ14
I2C_Handler, // IRQ15
0, // IRQ16
0, // IRQ17
0, // IRQ18
0, // IRQ19
0, // IRQ20
UART_Handler, // IRQ21
0, // IRQ22
0, // IRQ23
0, // IRQ24
0, // IRQ25
0, // IRQ26
0, // IRQ27
0, // IRQ28
0, // IRQ29
GPIO1_Handler, // IRQ30
0 // IRQ31
};
void SysPLL_init(void) {
// Set Post and Feedback dividers (Input=12Mhz, Output=48Mhz, M=4, MSEL,M-1=3 , P=2)
SYSPLLCTRL = (0x03 << SYSPLLCTRL_MDIV_BIT) | (SYSPLLCTRL_PDIV_02 << SYSPLLCTRL_PDIV_BIT);
// Power-up PLL block
PDCONFIG = (PDCONFIG_SYSPLL_ON << PDCONFIG_SYSPLL_BIT) | (PDCONFIG & ~(1 << PDCONFIG_SYSPLL_BIT));
// Wait for PLL lock
while(SYSPLLSTAT != SYSPLLSTAT_LOCKED);
// Select PLL as main clock source
MAINCLKSEL = MAINCLKSEL_PLLOUT;
MAINCLKUEN = MAINCLKUEN_OFF;
MAINCLKUEN = MAINCLKUEN_ON;
}
void Reset_Handler(void) {
uint32_t *src, *dst;
// Copy .data from Flash to RAM
src = &__data_load;
dst = &__data_start;
while (dst < &__data_end) {
*dst++ = *src++;
}
// Clear .BSS section
dst = &__bss_start;
while (dst < &__bss_end) {
*dst++ = 0;
}
// Initialize System PLL to 48Mhz
SysPLL_init();
// Call main()
main();
// Infinite loop
while(1);
}