1 |
1026 |
ivang |
@c
|
2 |
|
|
@c COPYRIGHT (c) 1988-2002.
|
3 |
|
|
@c On-Line Applications Research Corporation (OAR).
|
4 |
|
|
@c All rights reserved.
|
5 |
|
|
@c
|
6 |
|
|
@c init.t,v 1.10 2002/01/17 21:47:44 joel Exp
|
7 |
|
|
@c
|
8 |
|
|
|
9 |
|
|
@chapter Initialization Code
|
10 |
|
|
|
11 |
|
|
@section Introduction
|
12 |
|
|
|
13 |
|
|
The initialization code is the first piece of code executed when there's a
|
14 |
|
|
reset/reboot. Its purpose is to initialize the board for the application.
|
15 |
|
|
This chapter contains a narrative description of the initialization
|
16 |
|
|
process followed by a description of each of the files and routines
|
17 |
|
|
commonly found in the BSP related to initialization. The remainder of
|
18 |
|
|
this chapter covers special issues which require attention such
|
19 |
|
|
as interrupt vector table and chip select initialization.
|
20 |
|
|
|
21 |
|
|
Most of the examples in this chapter will be based on the gen68340 BSP
|
22 |
|
|
initialization code. Like most BSPs, the initialization for this
|
23 |
|
|
BSP is divided into two subdirectories under the BSP source directory.
|
24 |
|
|
The gen68340 BSP source code is in the following directory:
|
25 |
|
|
|
26 |
|
|
@example
|
27 |
|
|
c/src/lib/libbsp/m68k/gen68340
|
28 |
|
|
@end example
|
29 |
|
|
|
30 |
|
|
The following source code files are in this subdirectory.
|
31 |
|
|
|
32 |
|
|
@itemize @bullet
|
33 |
|
|
|
34 |
|
|
@item @code{start340}: assembly language code which contains early
|
35 |
|
|
initialization routines
|
36 |
|
|
|
37 |
|
|
@item @code{startup}: C code with higher level routines (RTEMS
|
38 |
|
|
initialization related)
|
39 |
|
|
|
40 |
|
|
@end itemize
|
41 |
|
|
|
42 |
|
|
@b{NOTE:} The directory @code{start340} is simply named @code{start} or
|
43 |
|
|
start followed by a BSP designation.
|
44 |
|
|
|
45 |
|
|
In the @code{start340} directory are two source files. The file
|
46 |
|
|
@code{startfor340only.s} is the simpler of these files as it only has
|
47 |
|
|
initialization code for a MC68340 board. The file @code{start340.s}
|
48 |
|
|
contains initialization for a 68349 based board as well.
|
49 |
|
|
|
50 |
|
|
@section Required Global Variables
|
51 |
|
|
|
52 |
|
|
Although not strictly part of initialization, there are a few global
|
53 |
|
|
variables assumed to exist by many support components. These
|
54 |
|
|
global variables are usually declared in the file @code{startup/bspstart.c}
|
55 |
|
|
that provides most of the BSP specific initialization. The following is
|
56 |
|
|
a list of these global variables:
|
57 |
|
|
|
58 |
|
|
@itemize @bullet
|
59 |
|
|
@item @code{BSP_Configuration} is the BSP's writable copy of the RTEMS
|
60 |
|
|
Configuration Table.
|
61 |
|
|
|
62 |
|
|
@item @code{Cpu_table} is the RTEMS CPU Dependent Information Table.
|
63 |
|
|
|
64 |
|
|
@item @code{bsp_isr_level} is the interrupt level that is set at
|
65 |
|
|
system startup. It will be restored when the executive returns
|
66 |
|
|
control to the BSP.
|
67 |
|
|
|
68 |
|
|
@end itemize
|
69 |
|
|
|
70 |
|
|
@section Board Initialization
|
71 |
|
|
|
72 |
|
|
This section describes the steps an application goes through from the
|
73 |
|
|
time the first BSP code is executed until the first application task
|
74 |
|
|
executes. The routines invoked during this will be discussed and
|
75 |
|
|
their location in the RTEMS source tree pointed out.
|
76 |
|
|
|
77 |
|
|
@subsection Start Code - Assembly Language Initialization
|
78 |
|
|
|
79 |
|
|
The assembly language code in the directory @code{start} is
|
80 |
|
|
the first part of the application to execute. It is
|
81 |
|
|
responsible for initializing the processor and board enough to execute
|
82 |
|
|
the rest of the BSP. This includes:
|
83 |
|
|
|
84 |
|
|
@itemize @bullet
|
85 |
|
|
@item initializing the stack
|
86 |
|
|
@item zeroing out the uninitialized data section @code{.bss}
|
87 |
|
|
@item disabling external interrupts
|
88 |
|
|
@item copy the initialized data from ROM to RAM
|
89 |
|
|
@end itemize
|
90 |
|
|
|
91 |
|
|
The general rule of thumb is that the
|
92 |
|
|
start code in assembly should do the minimum necessary to allow C code
|
93 |
|
|
to execute to complete the initialization sequence.
|
94 |
|
|
|
95 |
|
|
The initial assembly language start code completes its execution by
|
96 |
|
|
invoking the shared routine @code{boot_card()}.
|
97 |
|
|
|
98 |
|
|
The label (symbolic name) associated with the starting address of the
|
99 |
|
|
program is typically called @code{start}. The start object file
|
100 |
|
|
is the first object file linked into the program image so it is insured
|
101 |
|
|
that the start code is at offset 0 in the @code{.text} section. It is
|
102 |
|
|
the responsibility of the linker script in conjunction with the
|
103 |
|
|
compiler specifications file to put the start code in the correct location
|
104 |
|
|
in the application image.
|
105 |
|
|
|
106 |
|
|
@subsection boot_card() - Boot the Card
|
107 |
|
|
|
108 |
|
|
The @code{boot_card()} is the first C code invoked. Most of the BSPs
|
109 |
|
|
use the same shared version of @code{boot_card()} which is located in
|
110 |
|
|
the following file:
|
111 |
|
|
|
112 |
|
|
@example
|
113 |
|
|
c/src/lib/libbsp/shared/main.c
|
114 |
|
|
@end example
|
115 |
|
|
|
116 |
|
|
The @code{boot_card()} routine performs the following functions:
|
117 |
|
|
|
118 |
|
|
@itemize @bullet
|
119 |
|
|
|
120 |
|
|
@item initializes the shared fields of the CPU Configuration Table
|
121 |
|
|
(variable name @code{Cpu_table}) to a default state,
|
122 |
|
|
|
123 |
|
|
@item copies the application's RTEMS Configuration Table
|
124 |
|
|
(variable name @code{Configuration}) to the BSP's Configuration
|
125 |
|
|
Table (variable name @code{BSP_Configuration}) so it can be modified
|
126 |
|
|
as necessary without copying the original table,
|
127 |
|
|
|
128 |
|
|
@item invokes the BSP specific routine @code{bsp_start()},
|
129 |
|
|
|
130 |
|
|
@item invokes the RTEMS directive @code{rtems_initialize_executive_early()}
|
131 |
|
|
to initialize the executive, C Library, and all device drivers but
|
132 |
|
|
return without initiating multitasking or enabling interrupts,
|
133 |
|
|
|
134 |
|
|
@item invokes the shared @code{main()} in the same file as
|
135 |
|
|
@code{boot_card()} which does not return until the
|
136 |
|
|
@code{rtems_shutdown_executive} directive is called, and
|
137 |
|
|
|
138 |
|
|
@item invokes the BSP specific routine @code{bsp_cleanup()} to perform
|
139 |
|
|
any necessary board specific shutdown actions.
|
140 |
|
|
|
141 |
|
|
@end itemize
|
142 |
|
|
|
143 |
|
|
It is important to note that the executive and much of the
|
144 |
|
|
support environment must be initialized before invoking @code{main()}.
|
145 |
|
|
|
146 |
|
|
@subsection bsp_start() - BSP Specific Initialization
|
147 |
|
|
|
148 |
|
|
This is the first BSP specific C routine to execute during system
|
149 |
|
|
initialization. This routine often performs required fundamental
|
150 |
|
|
hardware initialization such as setting bus controller registers
|
151 |
|
|
that do not have a direct impact on whether or not C code can execute.
|
152 |
|
|
The source code for this routine is usually found in the following
|
153 |
|
|
file:
|
154 |
|
|
|
155 |
|
|
@example
|
156 |
|
|
c/src/lib/libbsp/CPU/BSP/startup/bspstart.c
|
157 |
|
|
@end example
|
158 |
|
|
|
159 |
|
|
This routine is also responsible for overriding the default settings
|
160 |
|
|
in the CPU Configuration Table and setting port specific entries
|
161 |
|
|
in this table. This may include increasing the maximum number
|
162 |
|
|
of some types of RTEMS system objects to reflect the needs of
|
163 |
|
|
the BSP and the base set of device drivers. This routine will
|
164 |
|
|
typically also install routines for one or more of the following
|
165 |
|
|
initialization hooks:
|
166 |
|
|
|
167 |
|
|
@itemize @bullet
|
168 |
|
|
@item BSP Pretasking Hook
|
169 |
|
|
@item BSP Predriver Hook
|
170 |
|
|
@item BSP Postdriver Hook
|
171 |
|
|
@end itemize
|
172 |
|
|
|
173 |
|
|
One of the most important functions performed by this routine
|
174 |
|
|
is determining where the RTEMS Workspace is to be
|
175 |
|
|
located in memory. All RTEMS objects and task stacks will be
|
176 |
|
|
allocated from this Workspace. The RTEMS Workspace is distinct
|
177 |
|
|
from the application heap used for @code{malloc()}. Many BSPs
|
178 |
|
|
place the RTEMS Workspace area at the end of RAM although this is
|
179 |
|
|
certainly not a requirement.
|
180 |
|
|
|
181 |
|
|
After completing execution, this routine returns to the
|
182 |
|
|
@code{boot_card()} routine.
|
183 |
|
|
|
184 |
|
|
@subsection main() - C Main
|
185 |
|
|
|
186 |
|
|
This routine is the C main entry point. This is a special routine
|
187 |
|
|
and the GNU Compiler Suite treats it as such. The GNU C Compiler
|
188 |
|
|
recognizes @code{main()} and automatically inserts a call to the
|
189 |
|
|
compiler run-time support routine @code{__main()} as the first
|
190 |
|
|
code executed in @code{main()}.
|
191 |
|
|
|
192 |
|
|
The routine @code{__main()} initializes the compiler's basic run-time
|
193 |
|
|
support library and, most importantly, invokes the C++ global
|
194 |
|
|
constructors.
|
195 |
|
|
|
196 |
|
|
The precise placement of when @code{main()} is invoked in the
|
197 |
|
|
RTEMS initialization sequence insures that C Library and non-blocking
|
198 |
|
|
calls can be made in global C++ constructors.
|
199 |
|
|
|
200 |
|
|
The shared implementation of this routine is located in the following file:
|
201 |
|
|
|
202 |
|
|
@example
|
203 |
|
|
c/src/lib/libbsp/shared/main.c
|
204 |
|
|
@end example
|
205 |
|
|
|
206 |
|
|
In addition to the implicit invocation of @code{__main}, this
|
207 |
|
|
routine performs some explicit initialization. This routine
|
208 |
|
|
sets the variable @code{rtems_progname} and initiates
|
209 |
|
|
multitasking via a call to the RTEMS directive
|
210 |
|
|
@code{rtems_initialize_executive_late}. It is important to note
|
211 |
|
|
that the executive does not return to this routine until the
|
212 |
|
|
RTEMS directive @code{rtems_shutdown_executive} is invoked.
|
213 |
|
|
|
214 |
|
|
The RTEMS initialization procedure is described in the @b{Initialization
|
215 |
|
|
Manager} chapter of the @b{RTEMS Application C User's Guide}.
|
216 |
|
|
Please refer to that manual for more information.
|
217 |
|
|
|
218 |
|
|
@subsection RTEMS Pretasking Callback
|
219 |
|
|
|
220 |
|
|
The @code{pretasking_hook} entry in the RTEMS CPU Configuration
|
221 |
|
|
Table may be the address of a user provided routine that is
|
222 |
|
|
invoked once RTEMS API initialization is complete but before interrupts
|
223 |
|
|
and tasking are enabled. No tasks -- not even the IDLE task -- have
|
224 |
|
|
been created when this hook is invoked. The pretasking hook is optional.
|
225 |
|
|
|
226 |
|
|
Although optional, most of the RTEMS BSPs provide a pretasking hook
|
227 |
|
|
callback. This routine is usually called @code{bsp_pretasking_hook}
|
228 |
|
|
and is found in the file:
|
229 |
|
|
|
230 |
|
|
@example
|
231 |
|
|
c/src/lib/libbsp/CPU/BSP/startup/bspstart.c
|
232 |
|
|
@end example
|
233 |
|
|
|
234 |
|
|
The @code{bsp_pretasking_hook()} routine is the appropriate place to
|
235 |
|
|
initialize any support components which depend on the RTEMS APIs.
|
236 |
|
|
Most BSPs set the debug level for the system and initialize the
|
237 |
|
|
RTEMS C Library support in their
|
238 |
|
|
implementation of @code{bsp_pretasking_hook()}. This initialization
|
239 |
|
|
includes the application heap used by the @code{malloc} family
|
240 |
|
|
of routines as well as the reentrancy support for the C Library.
|
241 |
|
|
|
242 |
|
|
The routine @code{bsp_libc_init} routine invoked from the
|
243 |
|
|
@code{bsp_pretasking_hook()} routine is passed the starting
|
244 |
|
|
address, length, and growth amount passed to @code{sbrk}.
|
245 |
|
|
This "sbrk amount" is only used if the heap runs out of
|
246 |
|
|
memory. In this case, the RTEMS malloc implementation will
|
247 |
|
|
invoked @code{sbrk} to obtain more memory. See
|
248 |
|
|
@ref{Miscellaneous Support Files sbrk() Implementation} for more details.
|
249 |
|
|
|
250 |
|
|
@subsection RTEMS Predriver Callback
|
251 |
|
|
|
252 |
|
|
The @code{predriver_hook} entry in the RTEMS CPU Configuration
|
253 |
|
|
Table may be the address of a user provided routine that is
|
254 |
|
|
is invoked immediately before the the device drivers and MPCI
|
255 |
|
|
are initialized. RTEMS
|
256 |
|
|
initialization is complete but interrupts and tasking are disabled.
|
257 |
|
|
This field may be NULL to indicate that the hook is not utilized.
|
258 |
|
|
|
259 |
|
|
Most BSPs do not use this callback.
|
260 |
|
|
|
261 |
|
|
@subsection Device Driver Initialization
|
262 |
|
|
|
263 |
|
|
At this point in the initialization sequence, the initialization
|
264 |
|
|
routines for all of the device drivers specified in the Device
|
265 |
|
|
Driver Table are invoked. The initialization routines are invoked
|
266 |
|
|
in the order they appear in the Device Driver Table.
|
267 |
|
|
|
268 |
|
|
The Driver Address Table is part of the RTEMS Configuration Table. It
|
269 |
|
|
defines device drivers entry points (initialization, open, close, read,
|
270 |
|
|
write, and control). For more information about this table, please
|
271 |
|
|
refer to the @b{Configuring a System} chapter in the
|
272 |
|
|
@b{RTEMS Application C User's Guide}.
|
273 |
|
|
|
274 |
|
|
The RTEMS initialization procedure calls the initialization function for
|
275 |
|
|
every driver defined in the RTEMS Configuration Table (this allows
|
276 |
|
|
one to include only the drivers needed by the application).
|
277 |
|
|
|
278 |
|
|
All these primitives have a major and a minor number as arguments:
|
279 |
|
|
|
280 |
|
|
@itemize @bullet
|
281 |
|
|
|
282 |
|
|
@item the major number refers to the driver type,
|
283 |
|
|
|
284 |
|
|
@item the minor number is used to control two peripherals with the same
|
285 |
|
|
driver (for instance, we define only one major number for the serial
|
286 |
|
|
driver, but two minor numbers for channel A and B if there are two
|
287 |
|
|
channels in the UART).
|
288 |
|
|
|
289 |
|
|
@end itemize
|
290 |
|
|
|
291 |
|
|
@subsection RTEMS Postdriver Callback
|
292 |
|
|
|
293 |
|
|
The @code{postdriver_hook} entry in the RTEMS CPU Configuration
|
294 |
|
|
Table may be the address of a user provided routine that is
|
295 |
|
|
invoked immediately after the the device drivers and MPCI are initialized.
|
296 |
|
|
Interrupts and tasking are disabled. The postdriver hook is optional.
|
297 |
|
|
|
298 |
|
|
Although optional, most of the RTEMS BSPs provide a postdriver hook
|
299 |
|
|
callback. This routine is usually called @code{bsp_postdriver_hook}
|
300 |
|
|
and is found in the file:
|
301 |
|
|
|
302 |
|
|
@example
|
303 |
|
|
c/src/lib/libbsp/CPU/BSP/startup/bsppost.c
|
304 |
|
|
@end example
|
305 |
|
|
|
306 |
|
|
The @code{bsp_postdriver_hook()} routine is the appropriate place to
|
307 |
|
|
perform initialization that must be performed before the first task
|
308 |
|
|
executes but requires that a device driver be initialized. The
|
309 |
|
|
shared implementation of the postdriver hook opens the default
|
310 |
|
|
standard in, out, and error files and associates them with
|
311 |
|
|
@code{/dev/console}.
|
312 |
|
|
|
313 |
|
|
@section The Interrupt Vector Table
|
314 |
|
|
|
315 |
|
|
The Interrupt Vector Table is called different things on different
|
316 |
|
|
processor families but the basic functionality is the same. Each
|
317 |
|
|
entry in the Table corresponds to the handler routine for a particular
|
318 |
|
|
interrupt source. When an interrupt from that source occurs, the
|
319 |
|
|
specified handler routine is invoked. Some context information is
|
320 |
|
|
saved by the processor automatically when this happens. RTEMS saves
|
321 |
|
|
enough context information so that an interrupt service routine
|
322 |
|
|
can be implemented in a high level language.
|
323 |
|
|
|
324 |
|
|
On some processors, the Interrupt Vector Table is at a fixed address. If
|
325 |
|
|
this address is in RAM, then usually the BSP only has to initialize
|
326 |
|
|
it to contain pointers to default handlers. If the table is in ROM,
|
327 |
|
|
then the application developer will have to take special steps to
|
328 |
|
|
fill in the table.
|
329 |
|
|
|
330 |
|
|
If the base address of the Interrupt Vector Table can be dynamically
|
331 |
|
|
changed to an arbitrary address, then the RTEMS port to that processor
|
332 |
|
|
family will usually allocate its own table and install it. For example,
|
333 |
|
|
on some members of the Motorola MC68xxx family, the Vector Base Register
|
334 |
|
|
(@code{vbr}) contains this base address.
|
335 |
|
|
|
336 |
|
|
@subsection Interrupt Vector Table on the gen68340 BSP
|
337 |
|
|
|
338 |
|
|
The gen68340 BSP provides a default Interrupt Vector Table in the
|
339 |
|
|
file @code{$BSP_ROOT/start340/start340.s}. After the @code{entry}
|
340 |
|
|
label is the definition of space reserved for the table of
|
341 |
|
|
interrupts vectors. This space is assigned the symbolic name
|
342 |
|
|
of @code{__uhoh} in the @code{gen68340} BSP.
|
343 |
|
|
|
344 |
|
|
At @code{__uhoh} label is the default interrupt handler routine. This
|
345 |
|
|
routine is only called when an unexpected interrupts is raised. One can
|
346 |
|
|
add their own routine there (in that case there's a call to a routine -
|
347 |
|
|
$BSP_ROOT/startup/dumpanic.c - that prints which address caused the
|
348 |
|
|
interrupt and the contents of the registers, stack, etc.), but this should
|
349 |
|
|
not return.
|
350 |
|
|
|
351 |
|
|
@section Chip Select Initialization
|
352 |
|
|
|
353 |
|
|
When the microprocessor accesses a memory area, address decoding is
|
354 |
|
|
handled by an address decoder, so that the microprocessor knows which
|
355 |
|
|
memory chip(s) to access. The following figure illustrates this:
|
356 |
|
|
|
357 |
|
|
@example
|
358 |
|
|
@group
|
359 |
|
|
+-------------------+
|
360 |
|
|
------------| |
|
361 |
|
|
------------| |------------
|
362 |
|
|
------------| Address |------------
|
363 |
|
|
------------| Decoder |------------
|
364 |
|
|
------------| |------------
|
365 |
|
|
------------| |
|
366 |
|
|
+-------------------+
|
367 |
|
|
CPU Bus Chip Select
|
368 |
|
|
@end group
|
369 |
|
|
@end example
|
370 |
|
|
|
371 |
|
|
|
372 |
|
|
The Chip Select registers must be programmed such that they match
|
373 |
|
|
the @code{linkcmds} settings. In the gen68340 BSP, ROM and RAM
|
374 |
|
|
addresses can be found in both the @code{linkcmds} and initialization
|
375 |
|
|
code, but this is not a great way to do this. It is better to
|
376 |
|
|
define addresses in the linker script.
|
377 |
|
|
|
378 |
|
|
@section Integrated Processor Registers Initialization
|
379 |
|
|
|
380 |
|
|
The CPUs used in many embedded systems are highly complex devices
|
381 |
|
|
with multiple peripherals on the CPU itself. For these devices,
|
382 |
|
|
there are always some specific integrated processor registers
|
383 |
|
|
that must be initialized. Refer to the processors' manuals for
|
384 |
|
|
details on these registers and be VERY careful programming them.
|
385 |
|
|
|
386 |
|
|
@section Data Section Recopy
|
387 |
|
|
|
388 |
|
|
The next initialization part can be found in
|
389 |
|
|
@code{$BSP340_ROOT/start340/init68340.c}. First the Interrupt
|
390 |
|
|
Vector Table is copied into RAM, then the data section recopy is initiated
|
391 |
|
|
(_CopyDataClearBSSAndStart in @code{$BSP340_ROOT/start340/startfor340only.s}).
|
392 |
|
|
|
393 |
|
|
This code performs the following actions:
|
394 |
|
|
|
395 |
|
|
@itemize @bullet
|
396 |
|
|
|
397 |
|
|
@item copies the .data section from ROM to its location reserved in RAM
|
398 |
|
|
(see @ref{Linker Script Initialized Data} for more details about this copy),
|
399 |
|
|
|
400 |
|
|
@item clear @code{.bss} section (all the non-initialized
|
401 |
|
|
data will take value 0).
|
402 |
|
|
|
403 |
|
|
@end itemize
|
404 |
|
|
|
405 |
|
|
@section RTEMS-Specific Initialization
|
406 |
|
|
|
407 |
|
|
@section The RTEMS configuration table
|
408 |
|
|
|
409 |
|
|
The RTEMS configuration table contains the maximum number of objects RTEMS
|
410 |
|
|
can handle during the application (e.g. maximum number of tasks,
|
411 |
|
|
semaphores, etc.). It's used to allocate the size for the RTEMS inner data
|
412 |
|
|
structures.
|
413 |
|
|
|
414 |
|
|
The RTEMS configuration table is application dependent, which means that
|
415 |
|
|
one has to provide one per application. It is usually defined
|
416 |
|
|
by defining macros and including the header file @code{}.
|
417 |
|
|
In simple applications such as the tests provided with RTEMS, it is
|
418 |
|
|
commonly found in the main module of the application. For more complex
|
419 |
|
|
applications, it may be in a file by itself.
|
420 |
|
|
|
421 |
|
|
The header file @code{} defines a constant table named
|
422 |
|
|
@code{Configuration}. It is common practice for the BSP to copy
|
423 |
|
|
this table into a modifiable copy named @code{BSP_Configuration}.
|
424 |
|
|
This copy of the table is modified to define the base address of the
|
425 |
|
|
RTEMS Executive Workspace as well as to reflect any BSP and
|
426 |
|
|
device driver requirements not automatically handled by the application.
|
427 |
|
|
|
428 |
|
|
For more information on the RTEMS Configuration Table, refer to the
|
429 |
|
|
@b{RTEMS Application C User's Guide}.
|
430 |
|
|
|