1 | /* |
---|
2 | * LPC22XX/LPC21xx Startup code |
---|
3 | * |
---|
4 | * Copyright (c) 2007 by Ray Xu <rayx.cn@gmail.com> |
---|
5 | * |
---|
6 | * The license and distribution terms for this file may be |
---|
7 | * found in the file LICENSE in this distribution or at |
---|
8 | * |
---|
9 | * http://www.rtems.com/license/LICENSE. |
---|
10 | * |
---|
11 | * |
---|
12 | * $Id$ |
---|
13 | */ |
---|
14 | |
---|
15 | #include <bsp.h> |
---|
16 | #include <rtems/libcsupport.h> |
---|
17 | #include <rtems/libio.h> |
---|
18 | #include <lpc22xx.h> |
---|
19 | |
---|
20 | |
---|
21 | /*************************************************************/ |
---|
22 | /* Macros */ |
---|
23 | /*************************************************************/ |
---|
24 | |
---|
25 | /*************************************************************/ |
---|
26 | /* Data Structures */ |
---|
27 | /*************************************************************/ |
---|
28 | |
---|
29 | /*************************************************************/ |
---|
30 | /* Global Variables */ |
---|
31 | /*************************************************************/ |
---|
32 | /*FIX ME:we do not have SDRAM, but can I define mutiple SRAM?*/ |
---|
33 | |
---|
34 | extern void *_flash_size; |
---|
35 | extern void *_flash_base; |
---|
36 | extern void *_sdram_size; |
---|
37 | extern void *_sdram_base; |
---|
38 | extern void *_bss_free_start; |
---|
39 | extern void *_bss_start_; |
---|
40 | extern void *_bss_end_; |
---|
41 | |
---|
42 | unsigned long free_mem_start; |
---|
43 | unsigned long free_mem_end; |
---|
44 | |
---|
45 | /*************************************************************/ |
---|
46 | /* Function prototypes */ |
---|
47 | /*************************************************************/ |
---|
48 | extern void rtems_irq_mngt_init(void); |
---|
49 | void bsp_libc_init( void *, uint32_t, int ); |
---|
50 | extern void UART0_Ini(void); |
---|
51 | extern void printi(unsigned long); |
---|
52 | /**************************************************************************/ |
---|
53 | /* */ |
---|
54 | /* NAME: bps_pretasking_hook - Function to setup system before startup */ |
---|
55 | /* */ |
---|
56 | /* DESCRIPTION: */ |
---|
57 | /* This function is called before drivers are initialized and used */ |
---|
58 | /* to setup libc and BSP extensions. It configures non cacheable */ |
---|
59 | /* SDRAM, SRAM, AIM, and ADM regions and sets up the CPU's memory */ |
---|
60 | /* protection unit. */ |
---|
61 | /* */ |
---|
62 | /* GLOBALS USED: */ |
---|
63 | /* free_mem_start */ |
---|
64 | /* free_mem_end */ |
---|
65 | /* */ |
---|
66 | /* RESTRICTIONS/LIMITATIONS: */ |
---|
67 | /* Since this function is setting up libc, it cannot use and libc */ |
---|
68 | /* functions. */ |
---|
69 | /* */ |
---|
70 | /**************************************************************************/ |
---|
71 | void bsp_pretasking_hook(void) |
---|
72 | { |
---|
73 | uint32_t heap_start; |
---|
74 | uint32_t heap_size; |
---|
75 | |
---|
76 | /* |
---|
77 | * Set up the heap. It uses all free SDRAM except that reserved |
---|
78 | * for non-cached uses. |
---|
79 | */ |
---|
80 | heap_start = free_mem_start; |
---|
81 | |
---|
82 | /* heap_size = (free_mem_end - heap_start - MEM_NOCACHE_SIZE); */ |
---|
83 | /*the board seems to have only 512K memory, we use 256K as heap, 256K to |
---|
84 | store the .text*/ |
---|
85 | heap_size = 0x40000; |
---|
86 | |
---|
87 | bsp_libc_init((void *)heap_start, heap_size, 0); |
---|
88 | } /* bsp_pretasking_hook */ |
---|
89 | |
---|
90 | /**************************************************************************/ |
---|
91 | /* */ |
---|
92 | /* NAME: bsp_start_default - BSP initialization function */ |
---|
93 | /* */ |
---|
94 | /* DESCRIPTION: */ |
---|
95 | /* This function is called before RTEMS is initialized and used */ |
---|
96 | /* adjust the kernel's configuration. */ |
---|
97 | /* */ |
---|
98 | /* This function also configures the CPU's memory protection unit. */ |
---|
99 | /* */ |
---|
100 | /* GLOBALS USED: */ |
---|
101 | /* Configuration */ |
---|
102 | /* free_mem_start */ |
---|
103 | /* free_mem_end */ |
---|
104 | /* free_mem_nocache_start */ |
---|
105 | /* _bss_free_start */ |
---|
106 | /* mpu_region_tbl */ |
---|
107 | /* */ |
---|
108 | /* */ |
---|
109 | /* */ |
---|
110 | /* */ |
---|
111 | /* RESTRICTIONS/LIMITATIONS: */ |
---|
112 | /* Since RTEMS is not configured, no RTEMS functions can be called. */ |
---|
113 | /* */ |
---|
114 | /**************************************************************************/ |
---|
115 | void bsp_start_default( void ) |
---|
116 | { |
---|
117 | PINSEL2 =0x0f814914; |
---|
118 | BCFG0 = 0x1000ffef; |
---|
119 | BCFG1 = 0x1000ffef; |
---|
120 | |
---|
121 | MEMMAP = 0x2; //debug and excute outside chip |
---|
122 | |
---|
123 | PLLCON = 1; |
---|
124 | #if (Fpclk / (Fcclk / 4)) == 1 |
---|
125 | VPBDIV = 0; |
---|
126 | #endif |
---|
127 | #if (Fpclk / (Fcclk / 4)) == 2 |
---|
128 | VPBDIV = 2; |
---|
129 | #endif |
---|
130 | #if (Fpclk / (Fcclk / 4)) == 4 |
---|
131 | VPBDIV = 1; |
---|
132 | #endif |
---|
133 | |
---|
134 | #if (Fcco / Fcclk) == 2 |
---|
135 | PLLCFG = ((Fcclk / Fosc) - 1) | (0 << 5); |
---|
136 | #endif |
---|
137 | #if (Fcco / Fcclk) == 4 |
---|
138 | PLLCFG = ((Fcclk / Fosc) - 1) | (1 << 5); |
---|
139 | #endif |
---|
140 | #if (Fcco / Fcclk) == 8 |
---|
141 | PLLCFG = ((Fcclk / Fosc) - 1) | (2 << 5); |
---|
142 | #endif |
---|
143 | #if (Fcco / Fcclk) == 16 |
---|
144 | PLLCFG = ((Fcclk / Fosc) - 1) | (3 << 5); |
---|
145 | #endif |
---|
146 | PLLFEED = 0xaa; |
---|
147 | PLLFEED = 0x55; |
---|
148 | while((PLLSTAT & (1 << 10)) == 0); |
---|
149 | PLLCON = 3; |
---|
150 | PLLFEED = 0xaa; |
---|
151 | PLLFEED = 0x55; |
---|
152 | |
---|
153 | /* memory configure */ |
---|
154 | /* it is not needed in my formatter board */ |
---|
155 | //MAMCR = 0; |
---|
156 | // MAMTIM = 3; |
---|
157 | //MAMCR = 2; |
---|
158 | |
---|
159 | /* init VIC */ |
---|
160 | VICIntEnClr = 0xffffffff; |
---|
161 | VICVectAddr = 0; |
---|
162 | VICIntSelect = 0; |
---|
163 | |
---|
164 | /* disable interrupts */ |
---|
165 | /* Setup interrupt controller.*/ |
---|
166 | VICProtection = 0; |
---|
167 | |
---|
168 | /* Place RTEMS workspace at beginning of free memory. */ |
---|
169 | Configuration.work_space_start = (void *)&_bss_free_start; |
---|
170 | |
---|
171 | free_mem_start = ((uint32_t)&_bss_free_start + |
---|
172 | rtems_configuration_get_work_space_size()); |
---|
173 | |
---|
174 | free_mem_end = ((uint32_t)&_sdram_base + (uint32_t)&_sdram_size); |
---|
175 | |
---|
176 | UART0_Ini(); |
---|
177 | |
---|
178 | #if 0 |
---|
179 | printk(" bsp_start_default 0x%08x\n", (int)&bsp_start_defalt); |
---|
180 | printk(" _bss_free_start 0x%08x\n", (int)&_bss_free_start); |
---|
181 | printk(" free_mem_start 0x%08x\n", (int)free_mem_start); |
---|
182 | printk(" _sdram_base 0x%08x\n", (int)&_sdram_base); |
---|
183 | printk(" _sdram_size 0x%08x\n", (int)&_sdram_size); |
---|
184 | printk(" free_mem_end 0x%08x\n", (int)free_mem_end); |
---|
185 | #endif |
---|
186 | |
---|
187 | /* |
---|
188 | * Init rtems exceptions management |
---|
189 | */ |
---|
190 | rtems_exception_init_mngt(); |
---|
191 | |
---|
192 | /* |
---|
193 | * Init rtems interrupt management |
---|
194 | */ |
---|
195 | rtems_irq_mngt_init(); |
---|
196 | |
---|
197 | /* |
---|
198 | * The following information is very useful when debugging. |
---|
199 | */ |
---|
200 | #if 0 |
---|
201 | |
---|
202 | printk( "work_space_size = 0x%x\n", |
---|
203 | rtems_configuration_get_work_space_size() ); |
---|
204 | printk( "microseconds_per_tick = 0x%x\n", |
---|
205 | rtems_configuration_get_microseconds_per_tick() ); |
---|
206 | printk( "ticks_per_timeslice = 0x%x\n", |
---|
207 | rtems_configuration_get_ticks_per_timeslice() ); |
---|
208 | |
---|
209 | /* printk( "_stack_size = 0x%x\n", _stack_size );*/ |
---|
210 | printk( "work_space_start = 0x%x\n", Configuration.work_space_start ); |
---|
211 | printk( "work_space_size = 0x%x\n", rtems_configuration_get_work_space_size() ); |
---|
212 | #endif |
---|
213 | } /* bsp_start */ |
---|
214 | |
---|
215 | |
---|
216 | /** |
---|
217 | * Reset the system. |
---|
218 | * |
---|
219 | * This functions enables the watchdog and waits for it to |
---|
220 | * fire, thus resetting the system. |
---|
221 | */ |
---|
222 | /** |
---|
223 | * Reset the system. |
---|
224 | * |
---|
225 | * This functions enables the watchdog and waits for it to |
---|
226 | * fire, thus resetting the system. |
---|
227 | */ |
---|
228 | void bsp_reset(void) |
---|
229 | { |
---|
230 | rtems_interrupt_level level; |
---|
231 | |
---|
232 | rtems_interrupt_disable(level); |
---|
233 | |
---|
234 | while(1); |
---|
235 | } |
---|
236 | |
---|
237 | /* |
---|
238 | * By making this a weak alias for bsp_start_default, a brave soul |
---|
239 | * can override the actual bsp_start routine used. |
---|
240 | */ |
---|
241 | |
---|
242 | void bsp_start (void) __attribute__ ((weak, alias("bsp_start_default"))); |
---|