source: rtems-docs/bsp-howto/initilization_code.rst @ e52906b

5
Last change on this file since e52906b was e52906b, checked in by Sebastian Huber <sebastian.huber@…>, on 01/09/19 at 15:14:06

Simplify SPDX-License-Identifier comment

  • Property mode set to 100644
File size: 14.3 KB
Line 
1.. SPDX-License-Identifier: CC-BY-SA-4.0
2
3.. Copyright (C) 1988, 2008 On-Line Applications Research Corporation (OAR)
4
5Initialization Code
6*******************
7
8.. warning::
9
10   This chapter contains outdated and confusing information.
11
12Introduction
13============
14
15The initialization code is the first piece of code executed when there's a
16reset/reboot. Its purpose is to initialize the board for the application.  This
17chapter contains a narrative description of the initialization process followed
18by a description of each of the files and routines commonly found in the BSP
19related to initialization.  The remainder of this chapter covers special issues
20which require attention such as interrupt vector table and chip select
21initialization.
22
23Most of the examples in this chapter will be based on the SPARC/ERC32 and
24m68k/gen68340 BSP initialization code.  Like most BSPs, the initialization for
25these BSP is contained under the :file:`start` directory in the BSP source
26directory.  The BSP source code for these BSPs is in the following directories:
27
28.. code-block:: shell
29
30    bsps/m68k/gen68340
31    bsps/sparc/erc32
32
33Both BSPs contain startup code written in assembly language and C.  The
34gen68340 BSP has its early initialization start code in the ``start340``
35subdirectory and its C startup code in the ``startup`` directory.  In the
36``start340`` directory are two source files.  The file ``startfor340only.s`` is
37the simpler of these files as it only has initialization code for a MC68340
38board.  The file ``start340.s`` contains initialization for a 68349 based board
39as well.
40
41Similarly, the ERC32 BSP has startup code written in assembly language and C.
42However, this BSP shares this code with other SPARC BSPs.  Thus the
43``Makefile.am`` explicitly references the following files for this
44functionality.
45
46.. code-block:: shell
47
48    ../../sparc/shared/start.S
49
50.. note::
51
52   In most BSPs, the directory named ``start340`` in the gen68340 BSP would be
53   simply named ``start`` or start followed by a BSP designation.
54
55Required Global Variables
56=========================
57
58Although not strictly part of initialization, there are a few global variables
59assumed to exist by reusable device drivers.  These global variables should
60only defined by the BSP when using one of these device drivers.
61
62The BSP author probably should be aware of the ``Configuration`` Table
63structure generated by ``<rtems/confdefs.h>`` during debug but should not
64explicitly reference it in the source code.  There are helper routines provided
65by RTEMS to access individual fields.
66
67In older RTEMS versions, the BSP included a number of required global
68variables.  We have made every attempt to eliminate these in the interest of
69simplicity.
70
71Board Initialization
72====================
73
74This section describes the steps an application goes through from the time the
75first BSP code is executed until the first application task executes.
76
77The initialization flows from assembly language start code to the shared
78``bootcard.c`` framework then through the C Library, RTEMS, device driver
79initialization phases, and the context switch to the first application task.
80After this, the application executes until it calls ``exit``,
81``rtems_shutdown_executive``, or some other normal termination initiating
82routine and a fatal system state is reached.  The optional
83``bsp_fatal_extension`` initial extension can perform BSP specific system
84termination.
85
86The routines invoked during this will be discussed and their location in the
87RTEMS source tree pointed out as we discuss each.
88
89Start Code - Assembly Language Initialization
90---------------------------------------------
91
92The assembly language code in the directory ``start`` is the first part of the
93application to execute.  It is responsible for initializing the processor and
94board enough to execute the rest of the BSP.  This includes:
95
96- initializing the stack
97
98- zeroing out the uninitialized data section ``.bss``
99
100- disabling external interrupts
101
102- copy the initialized data from ROM to RAM
103
104The general rule of thumb is that the start code in assembly should do the
105minimum necessary to allow C code to execute to complete the initialization
106sequence.
107
108The initial assembly language start code completes its execution by invoking
109the shared routine ``boot_card()``.
110
111The label (symbolic name) associated with the starting address of the program
112is typically called ``start``.  The start object file is the first object file
113linked into the program image so it is ensured that the start code is at offset
1140 in the ``.text`` section.  It is the responsibility of the linker script in
115conjunction with the compiler specifications file to put the start code in the
116correct location in the application image.
117
118boot_card() - Boot the Card
119---------------------------
120
121The ``boot_card()`` is the first C code invoked.  This file is the core
122component in the RTEMS BSP Initialization Framework and provides the proper
123sequencing of initialization steps for the BSP, RTEMS and device drivers. All
124BSPs use the same shared version of ``boot_card()`` which is located in the
125`bsps/shared/start/bootcard.c <https://git.rtems.org/rtems/tree/bsps/shared/start/bootcard.c>`_
126file.
127
128The ``boot_card()`` routine performs the following functions:
129
130- It disables processor interrupts.
131
132- It sets the command line argument variables
133  for later use by the application.
134
135- It invokes the routine ``rtems_initialize_executive()`` which never returns.
136  This routine will perform the system initialization through a linker set.
137  The important BSP-specific steps are outlined below.
138
139- Initialization of the RTEMS Workspace and the C Program Heap.  Usually the
140  default implementation in
141  `bsps/shared/start/bspgetworkarea-default.c <https://git.rtems.org/rtems/tree/bsps/shared/start/bspgetworkarea-default.c>`_
142  should be sufficient.  Custom implementations can use
143  ``bsp_work_area_initialize_default()`` or
144  ``bsp_work_area_initialize_with_table()`` available as inline functions from
145  ``#include <bsp/bootcard.h>``.
146
147- Invocation of the BSP-specific routine ``bsp_start()`` which is written in C and
148  thus able to perform more advanced initialization.  Often MMU, bus and
149  interrupt controller initialization occurs here.  Since the RTEMS Workspace
150  and the C Program Heap was already initialized by
151  ``bsp_work_area_initialize()``, this routine may use ``malloc()``, etc.
152
153- Specific initialization steps can be registered via the
154  ``RTEMS_SYSINIT_ITEM()`` provided by ``#include <rtems/sysinit.h>``.
155
156bsp_work_area_initialize() - BSP Specific Work Area Initialization
157------------------------------------------------------------------
158
159This is the first BSP specific C routine to execute during system
160initialization.  It must initialize the support for allocating memory from the
161C Program Heap and RTEMS Workspace commonly referred to as the work areas.
162Many BSPs place the work areas at the end of RAM although this is certainly not
163a requirement.  Usually the default implementation in
164`bsps/shared/start/bspgetworkarea-default.c <https://git.rtems.org/rtems/tree/bsps/shared/start/bspgetworkarea-default.c>`_
165should be sufficient.  Custom implementations can use
166``bsp_work_area_initialize_default()`` or
167``bsp_work_area_initialize_with_table()`` available as inline functions from
168``#include <bsp/bootcard.h>``.
169
170bsp_start() - BSP Specific Initialization
171-----------------------------------------
172
173This is the second BSP specific C routine to execute during system
174initialization.  It is called right after ``bsp_work_area_initialize()``.  The
175``bsp_start()`` routine often performs required fundamental hardware
176initialization such as setting bus controller registers that do not have a
177direct impact on whether or not C code can execute.  The interrupt controllers
178are usually initialized here.  The source code for this routine is usually
179found in the file ``bsps/${RTEMS_CPU}/${RTEMS_BSP}/start.c``.
180It is not allowed to create any operating system objects, e.g. RTEMS
181semaphores.
182
183After completing execution, this routine returns to the ``boot_card()``
184routine.  In case of errors, the initialization should be terminated via
185``bsp_fatal()``.
186
187Device Driver Initialization
188----------------------------
189
190At this point in the initialization sequence, the initialization routines for
191all of the device drivers specified in the Device Driver Table are invoked.
192The initialization routines are invoked in the order they appear in the Device
193Driver Table.
194
195The Driver Address Table is part of the RTEMS Configuration Table. It defines
196device drivers entry points (initialization, open, close, read, write, and
197control). For more information about this table, please refer to the
198*Configuring a System* chapter in the *RTEMS Application C User's Guide*.
199
200The RTEMS initialization procedure calls the initialization function for every
201driver defined in the RTEMS Configuration Table (this allows one to include
202only the drivers needed by the application).
203
204All these primitives have a major and a minor number as arguments:
205
206- the major number refers to the driver type,
207
208- the minor number is used to control two peripherals with the same driver (for
209  instance, we define only one major number for the serial driver, but two
210  minor numbers for channel A and B if there are two channels in the UART).
211
212The Interrupt Vector Table
213==========================
214
215The Interrupt Vector Table is called different things on different processor
216families but the basic functionality is the same.  Each entry in the Table
217corresponds to the handler routine for a particular interrupt source.  When an
218interrupt from that source occurs, the specified handler routine is invoked.
219Some context information is saved by the processor automatically when this
220happens.  RTEMS saves enough context information so that an interrupt service
221routine can be implemented in a high level language.
222
223On some processors, the Interrupt Vector Table is at a fixed address.  If this
224address is in RAM, then usually the BSP only has to initialize it to contain
225pointers to default handlers.  If the table is in ROM, then the application
226developer will have to take special steps to fill in the table.
227
228If the base address of the Interrupt Vector Table can be dynamically changed to
229an arbitrary address, then the RTEMS port to that processor family will usually
230allocate its own table and install it.  For example, on some members of the
231Motorola MC68xxx family, the Vector Base Register (``vbr``) contains this base
232address.
233
234Interrupt Vector Table on the gen68340 BSP
235------------------------------------------
236
237The gen68340 BSP provides a default Interrupt Vector Table in the file
238``$BSP_ROOT/start340/start340.s``.  After the ``entry`` label is the definition
239of space reserved for the table of interrupts vectors.  This space is assigned
240the symbolic name of ``__uhoh`` in the ``gen68340`` BSP.
241
242At ``__uhoh`` label is the default interrupt handler routine. This routine is
243only called when an unexpected interrupts is raised.  One can add their own
244routine there (in that case there's a call to a routine -
245$BSP_ROOT/startup/dumpanic.c - that prints which address caused the interrupt
246and the contents of the registers, stack, etc.), but this should not return.
247
248Chip Select Initialization
249==========================
250
251When the microprocessor accesses a memory area, address decoding is handled by
252an address decoder, so that the microprocessor knows which memory chip(s) to
253access.  The following figure illustrates this:
254
255.. code-block:: c
256
257                +-------------------+
258    ------------|                   |
259    ------------|                   |------------
260    ------------|      Address      |------------
261    ------------|      Decoder      |------------
262    ------------|                   |------------
263    ------------|                   |
264                +-------------------+
265    CPU Bus                            Chip Select
266
267The Chip Select registers must be programmed such that they match the
268``linkcmds`` settings. In the gen68340 BSP, ROM and RAM addresses can be found
269in both the ``linkcmds`` and initialization code, but this is not a great way
270to do this.  It is better to define addresses in the linker script.
271
272Integrated Processor Registers Initialization
273=============================================
274
275The CPUs used in many embedded systems are highly complex devices with multiple
276peripherals on the CPU itself.  For these devices, there are always some
277specific integrated processor registers that must be initialized.  Refer to the
278processors' manuals for details on these registers and be VERY careful
279programming them.
280
281Data Section Recopy
282===================
283
284The next initialization part can be found in
285``$BSP340_ROOT/start340/init68340.c``. First the Interrupt Vector Table is
286copied into RAM, then the data section recopy is initiated
287(``_CopyDataClearBSSAndStart`` in ``$BSP340_ROOT/start340/startfor340only.s``).
288
289This code performs the following actions:
290
291- copies the .data section from ROM to its location reserved in RAM (see
292  :ref:`Initialized Data` for more details about this copy),
293
294- clear ``.bss`` section (all the non-initialized data will take value 0).
295
296The RTEMS Configuration Table
297=============================
298
299The RTEMS configuration table contains the maximum number of objects RTEMS can
300handle during the application (e.g. maximum number of tasks, semaphores,
301etc.). It's used to allocate the size for the RTEMS inner data structures.
302
303The RTEMS configuration table is application dependent, which means that one
304has to provide one per application. It is usually defined by defining macros
305and including the header file ``<rtems/confdefs.h>``.  In simple applications
306such as the tests provided with RTEMS, it is commonly found in the main module
307of the application.  For more complex applications, it may be in a file by
308itself.
309
310The header file ``<rtems/confdefs.h>`` defines a constant table named
311``Configuration``.  With RTEMS 4.8 and older, it was accepted practice for the
312BSP to copy this table into a modifiable copy named ``BSP_Configuration``.
313This copy of the table was modified to define the base address of the RTEMS
314Executive Workspace as well as to reflect any BSP and device driver
315requirements not automatically handled by the application.  In 4.9 and newer,
316we have eliminated the BSP copies of the configuration tables and are making
317efforts to make the configuration information generated by
318``<rtems/confdefs.h>`` constant and read only.
319
320For more information on the RTEMS Configuration Table, refer to the *RTEMS
321Application C User's Guide*.
Note: See TracBrowser for help on using the repository browser.