source: rtems/doc/bsp_howto/init.t @ bc950e87

4.104.114.84.95
Last change on this file since bc950e87 was bc950e87, checked in by Joel Sherrill <joel.sherrill@…>, on 11/19/98 at 16:02:06

Applied updates from remote work while doing class.

  • Property mode set to 100644
File size: 16.3 KB
Line 
1@c
2@c  COPYRIGHT (c) 1988-1998.
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
98@subsection boot_card() - Boot the Card
99
100The @code{boot_card()} is the first C code invoked.  Most of the BSPs
101use the same shared version of @code{boot_card()} which is located in
102the following file:
103
104@example
105c/src/lib/libbsp/shared/main.c
106@end example
107
108The @code{boot_card()} routine performs the following functions:
109
110@itemize @bullet
111
112@item initializes the shared fields of the CPU Configuration Table
113(variable name @code{Cpu_table}) to a default state,
114
115@item copies the application's RTEMS Configuration Table
116(variable name @code{Configuration}) to the BSP's Configuration
117Table (variable name @code{BSP_Configuration}) so it can be modified
118as necessary without copying the original table,
119
120@item invokes the BSP specific routine @code{bsp_start()},
121
122@item invokes the RTEMS directive @code{rtems_initialize_executive_early()}
123to initialize the executive, C Library, and all device drivers but
124return without initiating multitasking or enabling interrupts,
125
126@item invokes the shared @code{main()} in the same file as
127@code{boot_card()} which does not return until the
128@code{rtems_shutdown_executive} directive is called, and
129
130@item invokes the BSP specific routine @code{bsp_cleanup()} to perform
131any necessary board specific shutdown actions.
132
133@end itemize
134
135It is important to note that the executive and much of the
136support environment must be initialized before invoking @code{main()}.
137
138@subsection bsp_start() - BSP Specific Initialization
139
140This is the first BSP specific C routine to execute during system
141initialization.  This routine often performs required fundamental
142hardware initialization such as setting bus controller registers
143that do not have a direct impact on whether or not C code can execute.
144The source code for this routine is usually found in the following
145file:
146
147@example
148c/src/lib/libbsp/CPU/BSP/startup/bspstart.c
149@end example
150
151This routine is also responsible for overriding the default settings
152in the CPU Configuration Table and setting port specific entries
153in this table.  This may include increasing the maximum number
154of some types of RTEMS system objects to reflect the needs of
155the BSP and the base set of device drivers. This routine will
156typically also install routines for one or more of the following
157initialization hooks:
158
159@itemize @bullet
160@item BSP Pretasking Hook
161@item BSP Predriver Hook
162@item BSP Postdriver Hook
163@end itemize
164
165One of the most important functions performed by this routine
166is determining where the RTEMS Executive Work Space is to be
167located in memory.  All RTEMS objects and task stacks will be
168allocated from this Workspace.  The RTEMS Workspace is distinct
169from the application heap used for @code{malloc()}.
170
171Many BSPs place this area at the end of RAM although this is
172certainly not a requirement.
173
174After completing execution, this routine returns to the
175@code{boot_card()} routine.
176
177@subsection main() - C Main
178
179This routine is the C main entry point.  This is a special routine
180and the GNU Compiler Suite treats it as such.  The GNU C Compiler
181recognizes @code{main()} and automatically inserts a call to the
182compiler run-time support routine @code{__main()} as the first
183code executed in @code{main()}.
184
185The routine @code{__main()} initializes the compiler's basic run-time
186support library and, most importantly, invokes the C++ global
187constructors. 
188
189The precise placement of when @code{main()} is invoked in the
190RTEMS initialization sequence insures that C Library and non-blocking
191calls can be made in global C++ constructors.
192
193The shared implementation of this routine is located in the following file:
194
195@example
196c/src/lib/libbsp/shared/main.c
197@end example
198
199In addition to the implicit invocation of @code{__main}, this
200routine performs some explicit initialization.  This routine
201sets the variable @code{rtems_progname} and initiates
202multitasking via a call to the RTEMS directive
203@code{rtems_initialize_executive_late}.  It is important to note
204that the executive does not return to this routine until the
205RTEMS directive @code{rtems_shutdown_executive} is invoked.
206
207The RTEMS initialization procedure is described in the @b{Initialization
208Manager} chapter of the @b{RTEMS Application C User's Guide}.
209Please refer to that manual for more information.
210
211@subsection RTEMS Pretasking Callback
212
213The @code{pretasking_hook} entry in the RTEMS CPU Configuration
214Table may be the address of a user provided routine that is
215invoked once RTEMS API initialization is complete but before interrupts
216and tasking are enabled.  No tasks -- not even the IDLE task -- have
217been created when this hook is invoked.  The pretasking hook is optional.
218
219Although optional, most of the RTEMS BSPs provide a pretasking hook
220callback.  This routine is usually called @code{bsp_pretasking_hook}
221and is found in the file:
222
223@example
224c/src/lib/libbsp/CPU/BSP/startup/bspstart.c
225@end example
226
227The @code{bsp_pretasking_hook()} routine is the appropriate place to
228initialize any support components which depend on the RTEMS APIs.
229Most BSPs set the debug level for the system and initialize the
230RTEMS C Library support in their
231implementation of @code{bsp_pretasking_hook()}.  This initialization
232includes the application heap used by the @code{malloc} family
233of routines as well as the reentrancy support for the C Library.
234
235The routine @code{bsp_libc_init} routine invoked from the
236@code{bsp_pretasking_hook()} routine is passed the starting
237address, length, and growth amount passed to @code{sbrk}. 
238This "sbrk amount" is only used if the heap runs out of
239memory.  In this case, the RTEMS malloc implementation will
240invoked @code{sbrk} to obtain more memory.  See
241@ref{Miscellaneous Support Files sbrk() Implementation} for more details.
242
243@subsection RTEMS Predriver Callback
244
245The @code{predriver_hook} entry in the RTEMS CPU Configuration
246Table may be the address of a user provided routine that is
247is invoked immediately before the the device drivers and MPCI
248are initialized. RTEMS
249initialization is complete but interrupts and tasking are disabled.
250This field may be NULL to indicate that the hook is not utilized.
251
252Most BSPs do not use this callback.
253
254@subsection Device Driver Initialization
255
256At this point in the initialization sequence, the initialization
257routines for all of the device drivers specified in the Device
258Driver Table are invoked.  The initialization routines are invoked
259in the order they appear in the Device Driver Table.
260
261The Driver Address Table is part of the RTEMS Configuration Table. It
262defines device drivers entry points (initialization, open, close, read,
263write, and control). For more information about this table, please
264refer to the @b{Configuring a System} chapter in the
265@b{RTEMS Application C User's Guide}.
266
267The RTEMS initialization procedure calls the initialization function for
268every driver defined in the RTEMS Configuration Table (this allows
269one to include only the drivers needed by the application).
270
271All these primitives have a major and a minor number as arguments:
272
273@itemize @bullet
274
275@item the major number refers to the driver type,
276
277@item the minor number is used to control two peripherals with the same
278driver (for instance, we define only one major number for the serial
279driver, but two minor numbers for channel A and B if there are two
280channels in the UART).
281
282@end itemize
283
284@subsection RTEMS Postdriver Callback
285
286The @code{postdriver_hook} entry in the RTEMS CPU Configuration
287Table may be the address of a user provided routine that is
288invoked immediately after the the device drivers and MPCI are initialized.
289Interrupts and tasking are disabled.  The postdriver hook is optional.
290
291Although optional, most of the RTEMS BSPs provide a postdriver hook
292callback.  This routine is usually called @code{bsp_postdriver_hook}
293and is found in the file:
294
295@example
296c/src/lib/libbsp/CPU/BSP/startup/bsppost.c
297@end example
298
299The @code{bsp_postdriver_hook()} routine is the appropriate place to
300perform initialization that must be performed before the first task
301executes but requires that a device driver be initialized.  The
302shared implementation of the postdriver hook opens the default
303standard in, out, and error files and associates them with
304@code{/dev/console}.
305
306@section The Interrupt Vector Table
307
308The Interrupt Vector Table is called different things on different
309processor families but the basic functionality is the same.  Each
310entry in the Table corresponds to the handler routine for a particular
311interrupt source.  When an interrupt from that source occurs, the
312specified handler routine is invoked.  Some context information is
313saved by the processor automatically when this happens.  RTEMS saves
314enough context information so that an interrupt service routine
315can be implemented in a high level language.
316
317On some processors, the Interrupt Vector Table is at a fixed address.  If
318this address is in RAM, then usually the BSP only has to initialize
319it to contain pointers to default handlers.  If the table is in ROM,
320then the application developer will have to take special steps to
321fill in the table.
322
323If the base address of the Interrupt Vector Table can be dynamically
324changed to an arbitrary address, then the RTEMS port to that processor
325family will usually allocate its own table and install it.  For example,
326on some members of the Motorola MC68xxx family, the Vector Base Register
327(@code{vbr}) contains this base address. 
328
329@subsection Interrupt Vector Table on the gen68340 BSP
330
331The gen68340 BSP provides a default Interrupt Vector Table in the
332file @code{$BSP_ROOT/start340/start340.s}.  After the @code{entry}
333label is the definition of space reserved for the table of
334interrupts vectors.  This space is assigned the symbolic name
335of @code{__uhoh} in the @code{gen68340} BSP.
336
337At @code{__uhoh} label is the default interrupt handler routine. This
338routine is only called when an unexpected interrupts is raised.  One can
339add their own routine there (in that case there's a call to a routine -
340$BSP_ROOT/startup/dumpanic.c - that prints which address caused the
341interrupt and the contents of the registers, stack, etc.), but this should
342not return.
343
344@section Chip Select Initialization
345
346When the microprocessor accesses a memory area, address decoding is
347handled by an address decoder, so that the microprocessor knows which
348memory chip(s) to access.   The following figure illustrates this:
349
350@example
351@group
352                     +-------------------+
353         ------------|                   |
354         ------------|                   |------------
355         ------------|      Address      |------------
356         ------------|      Decoder      |------------
357         ------------|                   |------------
358         ------------|                   |
359                     +-------------------+
360           CPU Bus                           Chip Select
361@end group
362@end example
363
364
365The Chip Select registers must be programmed such that they match
366the @code{linkcmds} settings. In the gen68340 BSP, ROM and RAM
367addresses can be found in both the @code{linkcmds} and initialization
368code, but this is not a great way to do this.  It is better to
369define addresses in the linker script.
370
371@section Integrated Processor Registers Initialization
372
373The CPUs used in many embedded systems are highly complex devices
374with multiple peripherals on the CPU itself.  For these devices,
375there are always some specific integrated processor registers
376that must be initialized.  Refer to the processors' manuals for
377details on these registers and be VERY careful programming them.
378
379@section Data Section Recopy
380
381The next initialization part can be found in
382@code{$BSP340_ROOT/start340/init68340.c}. First the Interrupt
383Vector Table is copied into RAM, then the data section recopy is initiated
384(_CopyDataClearBSSAndStart in @code{$BSP340_ROOT/start340/startfor340only.s}).
385
386This code performs the following actions:
387
388@itemize @bullet
389
390@item copies the .data section from ROM to its location reserved in RAM
391(see @ref{Linker Script Initialized Data} for more details about this copy),
392
393@item clear @code{.bss} section (all the non-initialized
394data will take value 0).
395
396@end itemize
397
398@section RTEMS-Specific Initialization
399
400@section The RTEMS configuration table
401
402The RTEMS configuration table contains the maximum number of objects RTEMS
403can handle during the application (e.g. maximum number of tasks,
404semaphores, etc.). It's used to allocate the size for the RTEMS inner data
405structures.
406
407The RTEMS configuration table is application dependent, which means that
408one has to provide one per application. It is usually defined
409by defining macros and including the header file @code{<confdefs.h>}.
410In simple applications such as the tests provided with RTEMS, it is
411commonly found in the main module of the application.  For more complex
412applications, it may be in a file by itself.
413
414The header file @code{<confdefs.h>} defines a constant table named
415@code{Configuration}.  It is common practice for the BSP to copy
416this table into a modifiable copy named @code{BSP_Configuration}.
417This copy of the table is modified to define the base address of the
418RTEMS Executive Workspace as well as to reflect any BSP and
419device driver requirements not automatically handled by the application.
420
421For more information on the RTEMS Configuration Table, refer to the
422@b{RTEMS Application C User's Guide}.
423
Note: See TracBrowser for help on using the repository browser.