source: rtems/doc/bsp_howto/init.t @ 0660b4f8

4.104.114.84.95
Last change on this file since 0660b4f8 was 0660b4f8, checked in by Joel Sherrill <joel.sherrill@…>, on 11/16/99 at 19:50:56

Changed copyright date to 1999.

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