source: rtems/doc/bsp_howto/init.t @ 29910dec

4.104.114.95
Last change on this file since 29910dec was 29910dec, checked in by Joel Sherrill <joel.sherrill@…>, on 08/22/08 at 16:58:53

2008-08-22 Joel Sherrill <joel.sherrill@…>

  • bsp_howto/init.t: Complete update.
  • Property mode set to 100644
File size: 18.9 KB
Line 
1@c
2@c  COPYRIGHT (c) 1988-2008.
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 SPARC/ERC32 and
22m68k/gen68340 BSP initialization code.  Like most BSPs, the initialization
23for these BSP is divided into two subdirectories under the BSP source
24directory.  The BSP source code for these BSPs is in the following
25directories:
26
27@example
28c/src/lib/libbsp/m68k/gen68340
29c/src/lib/libbsp/sparc/erc32
30@end example
31
32Both BSPs contain startup code written in assembly language and C.
33The gen68340 BSP has its early initialization start code in the
34@code{start340} subdirectory and its C startup code in the @code{startup}
35directory.  In the @code{start340} directory are two source files.
36The file @code{startfor340only.s} is the simpler of these files as it only
37has initialization code for a MC68340 board.  The file @code{start340.s}
38contains initialization for a 68349 based board as well.
39
40Similarly, the ERC32 BSP has startup code written in assembly language
41and C.  However, this BSP shares this code with other SPARC BSPs.
42Thus the @code{Makefile.am} explicitly references the following files
43for this functionality.
44
45@example
46../../sparc/shared/start.S
47../../sparc/shared/bspclean.c
48@end example
49
50@b{NOTE:} In most BSPs, the directory named @code{start340} in the
51gen68340 BSP would be simply named @code{start} or start followed by a
52BSP designation.
53
54@section Required Global Variables
55
56Although not strictly part of initialization, there are a few global
57variables assumed to exist by reusable device drivers.  These global
58variables should only defined by the BSP when using one of these device
59drivers.
60
61The BSP author probably should be aware of the @code{Configuration}
62Table structure generated by @code{<rtems/confdefs.h>} during debug but
63should not explicitly reference it in the source code.  There are helper
64routines provided by RTEMS to access individual fields.
65
66In older RTEMS versions, the BSP included a number of required global
67variables.  We have made every attempt to eliminate these in the interest
68of simplicity.
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 following figure illustrates the program flow during
75this sequence:
76
77@ifset use-ascii
78IMAGE NOT AVAILABLE IN ASCII VERSION
79@end ifset
80
81@ifset use-tex
82@image{BSPInitFlowchart-49,6in,,Initialization Sequence,.png}
83@c      @image{FILENAME[, WIDTH[, HEIGHT[, ALTTEXT[, EXTENSION]]]]}
84@end ifset
85
86@ifset use-html
87@html
88<center>
89<IMG SRC="BSPInitFlowchart-49.png" WIDTH=800 ALT="Initialization Sequence">
90</center>
91@end html
92@end ifset
93
94The above figure illustrates the flow from assembly language start code
95to the shared @code{bootcard.c} framework then through the C Library,
96RTEMS, device driver initialization phases, and the context switch
97to the first application task.  After this, the application executes
98until it calls @code{exit}, @code{rtems_shutdown_executive}, or some
99other normal termination initiating routine and control is returned
100to @code{bootcard.c} which allows the BSP to perform some clanup in C
101(@code{bsp_cleanup}) and then @code{boot_card} returns to the assembly
102language which initially invoked it.
103
104The routines invoked during this will be discussed and their location
105in the RTEMS source tree pointed out as we discuss each.
106
107@subsection Start Code - Assembly Language Initialization
108
109The assembly language code in the directory @code{start} is the first part
110of the application to execute.  It is responsible for initializing the
111processor and board enough to execute the rest of the BSP.  This includes:
112
113@itemize @bullet
114@item initializing the stack
115@item zeroing out the uninitialized data section @code{.bss}
116@item disabling external interrupts
117@item copy the initialized data from ROM to RAM
118@end itemize
119
120The general rule of thumb is that the start code in assembly should
121do the minimum necessary to allow C code to execute to complete the
122initialization sequence.
123
124The initial assembly language start code completes its execution by
125invoking the shared routine @code{boot_card()}.
126
127The label (symbolic name) associated with the starting address of the
128program is typically called @code{start}.  The start object file is the
129first object file linked into the program image so it is ensured that
130the start code is at offset 0 in the @code{.text} section.  It is the
131responsibility of the linker script in conjunction with the compiler
132specifications file to put the start code in the correct location in
133the application image.
134
135@subsection boot_card() - Boot the Card
136
137The @code{boot_card()} is the first C code invoked.  This file is the
138core component in the RTEMS BSP Initialization Framework and provides
139the proper sequencing of initialization steps for the BSP, RTEMS and
140device drivers. All BSPs use the same shared version of @code{boot_card()}
141which is located in the following file:
142
143@example
144c/src/lib/libbsp/shared/bootcard.c
145@end example
146
147The @code{boot_card()} routine performs the following functions:
148
149@itemize @bullet
150
151@item It disables processor interrupts.
152
153@item It sets the global program name and command line argument variables
154for later use by the application.
155
156@item If the macro is BSP_BOOTCARD_HANDLES_RAM_ALLOCATION is defined, it
157will invoke the BSP specific @code{bsp_get_work_area} function to obtain
158information on the amount and location of BSP RAM that is available to
159be allocated to the C Program Heap and RTEMS Workspace.  If the amount
160of memory available for the RTEMS Workspace is less than that required
161by the application (e.g. @code{rtems_configuration_get_work_space_size()},
162then a message is printed using @code{printk}, @code{bsp_cleanup} is
163invoked, and -1 is return to the assembly language start code.  BSPs which
164use this memory allocation functionality in @code{bootcard.c}
165must invoke the RTEMS specific autoconf macro
166@code{RTEMS_BSP_BOOTCARD_HANDLES_RAM_ALLOCATION} in the BSP's
167@code{configure.ac} file.
168
169@item It invokes the BSP specific routine @code{bsp_start()} which is
170written in C and thus able to perform more advanced initialization.
171Often MMU and bus initialization occurs here.
172
173@item It invokes the RTEMS directive
174@code{rtems_initialize_data_structures()} to initialize the RTEMS
175executive to a state where objects can be created but tasking is not
176enabled.
177
178@item If the macro is BSP_BOOTCARD_HANDLES_RAM_ALLOCATION is defined,
179it will calculate the memory available for the C Program Heap and invoke
180the initialization routine for the C Library with this information.
181
182@item It invokes the BSP specific routine @code{bsp_pretasking_hook}. On
183most BSPs which utilize the framework, this routine does nothing.
184
185@item If @code{RTEMS_DEBUG} is enabled, then the RTEMS debug mask level
186is inialized appropriately.
187
188@item It invokes the RTEMS directive
189@code{rtems_initialize_before_drivers()} to initialize the MPCI Server
190thread in a multiprocessor configuration and execute API specific
191extensions.
192
193@item It invokes the BSP specific routine @code{bsp_predriver_hook}. For
194most BSPs, the implementation of this routine does nothing.  However,
195on some BSPs, required subsystems which utilize the C Library
196(e.g. @code{malloc} in particular) may be initialized at this point.
197
198@item It invokes the RTEMS directive
199@code{rtems_initialize_device_drivers()} to initialize the statically
200configured set of device drivers in the order they were specified in
201the Configuration Table.
202
203@item It invokes the BSP specific routine @code{bsp_postdriver_hook}. For
204most BSPs, the implementation of this routine does nothing.  However, some
205BSPs use this hook and perform some initialization which must be done at
206this point in the initialization sequence.  This is the last opportunity
207for the BSP to insert BSP specific code into the initialization sequence.
208
209@item It invokes the RTEMS directive
210@code{rtems_initialize_start_multitasking()} which starts multitasking and context switches to the first task.  @code{boot_card()} will not return until the application is shutdown.  As part of this sequence the following actions occur:
211
212@itemize @bullet
213
214@item RTEMS will context switch to the first application task.  As a
215side-effect of this context switch, processor interrupts will be enabled.
216This is often the source of a fatal error during BSP development because
217the BSP did not clear and/or disable all interrupt sources and a spurious
218interrupt will occur .
219
220@item When in the context of the first task but before its body has been
221entered, any C++ Global Constructors will be invoked.
222
223@end itemize
224
225@item Finally after the application shutsdown RTEMS and control is
226return to @code{boot_card()} from RTEMS, it invokes the BSP specific
227routine @code{bsp_cleanup()} to perform any necessary board specific
228shutdown actions.
229
230@end itemize
231
232That's it.  We just went through the entire sequence.
233
234@subsection bsp_start() - BSP Specific Initialization
235
236This is the first BSP specific C routine to execute during system
237initialization.  This routine often performs required fundamental
238hardware initialization such as setting bus controller registers
239that do not have a direct impact on whether or not C code can execute.
240The source code for this routine is usually found in the following
241file:
242
243@example
244c/src/lib/libbsp/CPU/BSP/startup/bspstart.c
245@end example
246
247On older BSPs not using @code{boot_card()}'s support for allocating memory
248to the C Program Heap and RTEMS Workspace, one of the most important
249functions performed by this routine is determining where the RTEMS
250Workspace is to be located in memory.  All RTEMS objects and task stacks
251will be allocated from this Workspace.  The RTEMS Workspace is distinct
252from the application heap used for @code{malloc()}.  Many BSPs place
253the RTEMS Workspace area at the end of RAM although this is certainly
254not a requirement.
255
256After completing execution, this routine returns to the @code{boot_card()}
257routine.
258
259@subsection RTEMS Pretasking Callback
260
261The method @code{bsp_pretasking_hook()} is the BSP specific routine
262invoked once RTEMS API initialization is complete but before interrupts
263and tasking are enabled.  No tasks -- not even the IDLE task -- have
264been created when this hook is invoked.  The pretasking hook is optional
265and the user may use the shared version.
266
267The @code{bsp_pretasking_hook()} routine is the appropriate place to
268initialize any support components which depend on the RTEMS APIs.
269Older BSPs that do not take full advantage of @code{boot_card()}
270may initialize the RTEMS C Library in their implementation of
271@code{bsp_pretasking_hook()}.  This initialization includes the
272application heap used by the @code{malloc} family of routines as well
273as the reentrancy support for the C Library.
274
275The routine @code{bsp_libc_init} routine invoked from the
276either @code{boot_card()} or (less preferable) the BSP specific
277@code{bsp_pretasking_hook()} routine is passed the starting address,
278length, and growth amount passed to @code{sbrk}.  This "sbrk amount"
279is only used if the heap runs out of memory.  In this case, the RTEMS
280malloc implementation will invoked @code{sbrk} to obtain more memory.
281See @ref{Miscellaneous Support Files sbrk() Implementation} for more
282details.
283
284@subsection RTEMS Predriver Callback
285
286The @code{bsp_predriver_hook()} method is the BSP specific routine that
287is is invoked immediately before the the device drivers and MPCI are
288initialized. RTEMS initialization is complete but interrupts and tasking
289are disabled.
290
291The BSP may use the shared version of this routine which is empty.
292Most BSPs do not provide a specific implementation of this callback.
293
294@subsection Device Driver Initialization
295
296At this point in the initialization sequence, the initialization
297routines for all of the device drivers specified in the Device
298Driver Table are invoked.  The initialization routines are invoked
299in the order they appear in the Device Driver Table.
300
301The Driver Address Table is part of the RTEMS Configuration Table. It
302defines device drivers entry points (initialization, open, close, read,
303write, and control). For more information about this table, please
304refer to the @b{Configuring a System} chapter in the
305@b{RTEMS Application C User's Guide}.
306
307The RTEMS initialization procedure calls the initialization function for
308every driver defined in the RTEMS Configuration Table (this allows
309one to include only the drivers needed by the application).
310
311All these primitives have a major and a minor number as arguments:
312
313@itemize @bullet
314
315@item the major number refers to the driver type,
316
317@item the minor number is used to control two peripherals with the same
318driver (for instance, we define only one major number for the serial
319driver, but two minor numbers for channel A and B if there are two
320channels in the UART).
321
322@end itemize
323
324@subsection RTEMS Postdriver Callback
325
326The @code{bsp_postdriver_hook()} BSP specific routine is invoked
327immediately after the the device drivers and MPCI are initialized.
328Interrupts and tasking are disabled.
329
330Most BSPs use the shared implementation of this routine which is responsible for opening the device @code{/dev/console} for standard input, output and error if the application has configured the Console Device Driver.  This file is located at:
331
332@example
333c/src/lib/libbsp/shared/bsppost.c
334@end example
335
336@section The Interrupt Vector Table
337
338The Interrupt Vector Table is called different things on different
339processor families but the basic functionality is the same.  Each
340entry in the Table corresponds to the handler routine for a particular
341interrupt source.  When an interrupt from that source occurs, the
342specified handler routine is invoked.  Some context information is
343saved by the processor automatically when this happens.  RTEMS saves
344enough context information so that an interrupt service routine
345can be implemented in a high level language.
346
347On some processors, the Interrupt Vector Table is at a fixed address.  If
348this address is in RAM, then usually the BSP only has to initialize
349it to contain pointers to default handlers.  If the table is in ROM,
350then the application developer will have to take special steps to
351fill in the table.
352
353If the base address of the Interrupt Vector Table can be dynamically
354changed to an arbitrary address, then the RTEMS port to that processor
355family will usually allocate its own table and install it.  For example,
356on some members of the Motorola MC68xxx family, the Vector Base Register
357(@code{vbr}) contains this base address. 
358
359@subsection Interrupt Vector Table on the gen68340 BSP
360
361The gen68340 BSP provides a default Interrupt Vector Table in the
362file @code{$BSP_ROOT/start340/start340.s}.  After the @code{entry}
363label is the definition of space reserved for the table of
364interrupts vectors.  This space is assigned the symbolic name
365of @code{__uhoh} in the @code{gen68340} BSP.
366
367At @code{__uhoh} label is the default interrupt handler routine. This
368routine is only called when an unexpected interrupts is raised.  One can
369add their own routine there (in that case there's a call to a routine -
370$BSP_ROOT/startup/dumpanic.c - that prints which address caused the
371interrupt and the contents of the registers, stack, etc.), but this should
372not return.
373
374@section Chip Select Initialization
375
376When the microprocessor accesses a memory area, address decoding is
377handled by an address decoder, so that the microprocessor knows which
378memory chip(s) to access.   The following figure illustrates this:
379
380@example
381@group
382                     +-------------------+
383         ------------|                   |
384         ------------|                   |------------
385         ------------|      Address      |------------
386         ------------|      Decoder      |------------
387         ------------|                   |------------
388         ------------|                   |
389                     +-------------------+
390           CPU Bus                           Chip Select
391@end group
392@end example
393
394
395The Chip Select registers must be programmed such that they match
396the @code{linkcmds} settings. In the gen68340 BSP, ROM and RAM
397addresses can be found in both the @code{linkcmds} and initialization
398code, but this is not a great way to do this.  It is better to
399define addresses in the linker script.
400
401@section Integrated Processor Registers Initialization
402
403The CPUs used in many embedded systems are highly complex devices
404with multiple peripherals on the CPU itself.  For these devices,
405there are always some specific integrated processor registers
406that must be initialized.  Refer to the processors' manuals for
407details on these registers and be VERY careful programming them.
408
409@section Data Section Recopy
410
411The next initialization part can be found in
412@code{$BSP340_ROOT/start340/init68340.c}. First the Interrupt
413Vector Table is copied into RAM, then the data section recopy is initiated
414(_CopyDataClearBSSAndStart in @code{$BSP340_ROOT/start340/startfor340only.s}).
415
416This code performs the following actions:
417
418@itemize @bullet
419
420@item copies the .data section from ROM to its location reserved in RAM
421(see @ref{Linker Script Initialized Data} for more details about this copy),
422
423@item clear @code{.bss} section (all the non-initialized
424data will take value 0).
425
426@end itemize
427
428@section RTEMS-Specific Initialization
429
430@section The RTEMS configuration table
431
432The RTEMS configuration table contains the maximum number of objects RTEMS
433can handle during the application (e.g. maximum number of tasks,
434semaphores, etc.). It's used to allocate the size for the RTEMS inner data
435structures.
436
437The RTEMS configuration table is application dependent, which means that
438one has to provide one per application. It is usually defined by defining
439macros and including the header file @code{<rtems/confdefs.h>}.  In simple
440applications such as the tests provided with RTEMS, it is commonly found
441in the main module of the application.  For more complex applications,
442it may be in a file by itself.
443
444The header file @code{<rtems/confdefs.h>} defines a constant table
445named @code{Configuration}.  With RTEMS 4.8 and older, it was accepted
446practice for the BSP to copy this table into a modifiable copy named
447@code{BSP_Configuration}.  This copy of the table was modified to define
448the base address of the RTEMS Executive Workspace as well as to reflect
449any BSP and device driver requirements not automatically handled by the
450application.  In 4.9 and newer, we have eliminated the BSP copies of the
451configuration tables and are making efforts to make the configuration
452information generated by @code{<rtems/confdefs.h>} constant and read only.
453
454For more information on the RTEMS Configuration Table, refer to the
455@b{RTEMS Application C User's Guide}.
456
Note: See TracBrowser for help on using the repository browser.