OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [doc/] [sgml/] [user-guide/] [programming.sgml] - Blame information for rev 842

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 786 skrzyp
2
 
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
 
28
29
 
30
31
Programming With <productname>eCos</productname>
32
 
33
34
Programming With <productname>eCos</productname>
35
 
36
The following chapters of this manual comprise a simple tutorial
37
for configuring and building eCos, building and running eCos tests,
38
and finally building three stand-alone example programs which use
39
the  eCos API to perform some simple tasks.
40
 
41
You will need a properly installed eCos system, with the correct
42
versions of the GNU toolchain. On Windows
43
you will be using the bash command line interpreter that comes with
44
Cygwin, with the environment variables set as described in the
45
toolchain documentation.
46
 
47
48
The Development Process
49
 
50
Most development projects using eCos would contain some (or
51
most) of  the following:
52
 
53
54
<productname>eCos</productname> Configuration
55
 
56
eCos is configured to provide the desired API (the inclusion
57
of libc, uitron, and the disabling of certain undesired funtions,
58
etc.), and semantics (selecting scheduler, mutex behavior, etc.).
59
See .
60
 
61
It would normally make sense to enable eCos assertion checking
62
at this time as well, to catch as many programming errors during
63
the development phase as possible.
64
 
65
Note that it should not be necessary to spend much time on
66
eCos configuration initially. It may be important to perform fine
67
tuning to reduce the memory footprint and to improve performance
68
later when the product reaches a testable state.
69
70
 
71
72
    Integrity check of the <productname>eCos</productname> configuration
73
 
74
While we strive to thoroughly test eCos, the vast number
75
of configuration permutations mean that the particular configuration
76
parameters used for your project may not have been tested. Therefore,
77
we advise running the eCos tests after the project's
78
eCos configuration has been determined. See .
79
 
80
Obviously, this should be repeated if the configuration changes
81
later on in the development process.
82
83
 
84
85
    Application Development - Target Neutral Part
86
 
87
While your project is probably targeting a specific architecture
88
and platform, possibly custom hardware, it may be possible to perform
89
part of the application development using simulated or synthetic
90
targets.
91
 
92
There are three good reasons for doing this:
93
 
94
95
 
96
97
It may be possible by this means to perform application
98
development in parallel with the design/implementation
99
of the target hardware, thus providing more time for developing
100
and testing functionality, and reducing time-to-market.
101
102
 
103
104
The build-run-debug-cycle may be faster when the application
105
does not have to be downloaded to a target via a serial interface.
106
Debugging is also likely to be more responsive when you do not have to
107
to communicate with the remote GDB stubs in RedBoot via serial. It
108
also removes the need for manually or automatically resetting the
109
target hardware.
110
111
 
112
113
114
New hardware can often be buggy. Comparing the behaviour of the
115
program on the hardware and in the simulator or synthetic target may
116
allow you to identify where the problems lie.
117
118
119
 
120
121
 
122
This approach is possible because all targets (including
123
simulators and synthetic ones) provide the same basic API: that
124
is, kernel, libc, libm, uitron, infra, and to some extent, HAL and
125
IO.
126
 
127
Synthetic targets are especially suitable as they allow you
128
to construct simulations of elaborate devices by interaction with
129
the host system, where an IO device API can hide the details from
130
the application. When switching to hardware later in the development
131
cycle, the IO driver is properly implemented.
132
133
 
134
135
Simulators can also do this, but it all depends on the
136
design and capabilities of the simulator you use. Some, like
137
SID or
138
Bochs provide
139
complete hardware emulation, while others just support enough of the
140
instruction set to run compiled code.
141
142
 
143
Therefore, select a simulator or synthetic target and use
144
it for as long as possible for application development. That is,
145
configure for the selected target, build eCos, build the application
146
and link with eCos, run and debug. Repeat the latter two steps until
147
you are happy with it.
148
 
149
Obviously, at some time you will have to switch to the intended
150
target hardware, for example when adding target specific feature
151
support, for memory footprint/performance characterization,
152
and for final tuning of eCos and the application.
153
 
154
155
 
156
157
    Application Development - Target Specific Part
158
 
159
Repeat the build-run-debug-cycle while performing final tuning
160
and debugging of application. Remember to disable eCos assertion
161
checking if you are testing any performance-related aspects, it can
162
make a big difference.
163
 
164
It may be useful to switch between this and the previous step
165
repeatedly through the development process; use the simulator/synthetic
166
target for actual development, and use the target hardware to continually
167
check memory footprint and performance. There should be little cost
168
in switching between the two targets when using two separate build
169
trees. 
170
171
 
172
173
 
174
175
 
176
177
 
178
179
<!-- <xref> --><!-- <index></index> -->Configuring and Building <productname>eCos</productname> from Source
180
 
181
This chapter documents the configuration of eCos. The process is
182
the same for any of the supported targets: you may select a
183
hardware target (if you have a board available), any one of the
184
simulators, or a synthetic target (if your host platform has synthetic
185
target support).
186
 
187
188
 
189
190
<!-- <xref> --><productname>eCos</productname> Start-up Configurations
191
 
192
There are various ways to download an executable image to a
193
target board, and these involve different ways of preparing the
194
executable image. In the eCos Hardware Abstraction Layer (HAL package)
195
there are configuration options to support the different download
196
methods.  summarizes the
197
ways in which an eCos image can be prepared for different types of
198
download. This is not an exhaustive list, some targets define
199
additional start-up types of their own. Where a ROM Monitor is
200
mentioned, this will usually be RedBoot, although on some older, or
201
low resource, targets you may need to use CygMon or the GDB stubs ROM,
202
see the target documentation for details.
203
 
204
 
205
206
Configuration for various download methods
207
208
209
210
Download method
211
HAL configuration
212
213
214
215
216
Burn hardware ROM
217
 ROM or ROMRAM start-up
218
219
220
Download to ROM emulator
221
 ROM or ROMRAM start-up
222
223
224
Download to board with ROM Monitor
225
 RAM start-up
226
227
228
Download to simulator without ROM Monitor
229
 ROM start-up
230
231
232
Download to simulator with ROM Monitor
233
 RAM start-up
234
235
236
Download to simulator ignoring devices
237
 SIM configuration
238
239
240
Run synthetic target
241
 RAM start-up
242
243
244
245
246
 
247
248
 
249
You cannot run an application configured for RAM start-up
250
on the simulator directly: it will fail during start-up. You can
251
only download it to the simulator if
252
you are already running RedBoot in the simulator,
253
as described in the toolchain documentation
254
or you load through the
255
SID 
256
GDB debugging component.  This is not the same as the simulated
257
stub, since it does not require a target program to be running to
258
get GDB to talk to it.  It can be done before letting the simulator
259
run
260
or you use the ELF loader component to get a program into memory.
261
 
262
263
 
264
265
Configuring eCos' HAL package for simulation should
266
rarely be needed for real development; binaries built with such
267
a kernel will not run on target boards at all,
268
and the MN10300 and
269
TX39 simulators can run binaries built for stdeval1 and jmr3904
270
target boards.
271
The main use for a ``simulation'' configuration
272
is if you are trying to work around problems with the device drivers
273
or with the simulator.  Also note that when using a TX39 system configured
274
for simulator start-up you should then invoke the simulator with
275
the 
276
option instead of
277
278
279
 
280
281
If your chosen architecture does not have simulator support,
282
then the combinations above that refer to the simulator do not apply.
283
Similarly, if your chosen platform does not have RedBoot
284
ROM support, the combinations listed above that use
285
RedBoot do not apply.
286
287
 
288
The debugging environment for most developers will be either
289
a hardware board or the simulator, in which case they will be able
290
to select a single HAL configuration.
291
 
292
293
 
294
295
 
296
297
<!-- <index></index> --></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>298</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>Configuration Tool on Windows and Linux Quick Start
299
 
300
301
 
302
This section described the GUI based configuration tool. This
303
tool is probably more suited to users who prefer GUI's. The next
304
section describes a CLI based tool which Unix users may
305
prefer. 
306
 
307
Note that the use of the Configuration Tool
308
is described in detail in 
309
LINKEND="THE-ECOS-CONFIGURATION-TOOL">.
310
 
311
The Configuration Tool (see )
312
has five main elements: the configuration window,
313
the conflicts window,
314
the properties window, the short
315
description window,
316
and the output window.
317
 
318
319
Configuration Tool
320
321
322
 
323
Start by opening the templates window via Build->Templates.
324
Select the desired target (see ).
325
 
326
327
Template selection
328
329
330
 
331
Make sure that the configuration is correct for the target
332
in terms of endianness, CPU model, Startup type, etc. (see ).
333
 
334
335
<!-- <conditionaltext> --><!-- <xref> -->Configuring</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>336</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>for the target
337
338
339
 
340
Next, select the Build->Library menu
341
item to start building eCos (see 
342
LINKEND="FIGURE-SELECTING-THE-BUILD-LIBRARY-MENU-ITEM">).  The
343
application will configure the sources, prepare a build tree, and
344
build the libtarget.a library, which contains the
345
eCos kernel and other packages.
346
 
347
348
Selecting the Build Library menu item
349
350
351
 
352
 
353
The Save As dialog box will appear, asking
354
you to specify a directory in which to place your save file. You
355
can use the default, but it is a good idea to make a subdirectory,
356
called ecos-work for example. 
357
 
358
359
Save file dialog
360
361
362
 
363
The first time you build an eCos library for a specific
364
architecture, the Configuration Tool may prompt
365
you for the location of the appropriate build tools (including
366
make and
367
TARGET-gcc) using a
368
Build Tools dialog box (as shown in 
369
LINKEND="FIGURE-BUILD-TOOLS-DIALOG">). You can select a location from
370
the drop down list, browse to the directory using the
371
Browse button, or type in the location of the
372
build tools manually.
373
 
374
375
Build tools dialog
376
377
378
 
379
The Configuration Tool may also prompt you
380
for the location of the user tools (such as cat and
381
ls) using a User Tools dialog
382
box (as shown in ). As with
383
the Build Tools dialog, you can select a location
384
from the drop down list, browse to the directory using the
385
Browse button, or type in the location of the
386
user tools manually. Note that on Linux, this will often be
387
unnecessary as the tools will already be on your PATH.
388
 
389
390
User tools dialog
391
392
393
 
394
When the tool locations have been entered, the Configuration
395
Tool will configure the sources, prepare a build tree,
396
and build the libtarget.a library, which contains
397
the eCos kernel and other packages.
398
 
399
The output from the configuration process and the building
400
of libtarget.a will be shown in the output
401
window.
402
 
403
Once the build process has finished you will have a kernel
404
with other packages in libtarget.a. You should
405
now build the eCos tests for your particular configuration. 
406
 
407
You can do this by selecting Build -> Tests.
408
Notice that you could have selected Tests instead
409
of Library in the earlier step and it would
410
have built both the library and the tests,
411
but this would increase the build time substantially, and if you
412
do not need to build the tests it is unnecessary.
413
 
414
415
Selecting the Build Tests menu item
416
417
418
 
419
 will guide you through running one
420
            of the test cases you just built on the selected target,
421
            using GDB. 
422
423
 
424
425
 
426
427
<!-- <index></index> --></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>428</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>Ecosconfig on Windows and Linux Quick Start
429
 
430
As an alternative to using the graphical
431
Configuration Tool, it is possible to
432
configure and build a kernel by editing a configuration file manually
433
and using the ecosconfig command. Users with a Unix
434
background may find this tool more suitable than the GUI tool
435
described in the previous section.
436
 
437
438
Manual configuration and the ecosconfig command are
439
described in detail in .
440
441
 
442
443
To use the ecosconfig command you need to start a
444
shell. In Windows you need to start a
445
CygWin bash shell, not a
446
DOS command line.
447
448
 
449
455
 
456
The following instructions assume that the
457
PATH and ECOS_REPOSITORY
458
environment variables have been setup correctly as described in 
459
LINKEND="user-guide-installation-linux">.  They also assume Linux
460
usage but equally well apply to Windows running Cygwin.
461
 
462
Before invoking ecosconfig you need to
463
choose a directory in which to work. For the purposes of this tutorial,
464
the default path will be BASE_DIR/ecos-work.
465
Create this directory and change to it by typing: 
466
 
467
468
$ mkdir BASE_DIR/ecos-work
469
$ cd BASE_DIR/ecos-work
470
471
 
472
To see what options can be used with ecosconfig,
473
type: 
474
 
475
$ ecosconfig --help
476
 
477
The available packages, targets and templates may be listed
478
as follows:
479
 
480
481
$ ecosconfig list
482
483
 
484
Here is sample output from ecosconfig showing
485
the usage message.
486
 
487
488
Getting <!-- <index></index> --> help from ecosconfig
489
 
490
491
$ ecosconfig --help
492
Usage: ecosconfig [ qualifier ... ] [ command ]
493
  commands are:
494
    list                                       : list repository contents
495
    new TARGET [ TEMPLATE [ VERSION ] ]        : create a configuration
496
    target TARGET                              : change the target hardware
497
    template TEMPLATE [ VERSION ]              : change the template
498
    add PACKAGE [ PACKAGE ... ]                : add package(s)
499
    remove PACKAGE [ PACKAGE ... ]             : remove package(s)
500
    version VERSION PACKAGE [ PACKAGE ... ]    : change version of package(s)
501
    export FILE                                : export minimal config info
502
    import FILE                                : import additional config info
503
    check                                      : check the configuration
504
    resolve                                    : resolve conflicts
505
    tree                                       : create a build tree
506
  qualifiers are:
507
    --config=FILE                              : the configuration file
508
    --prefix=DIRECTORY                         : the install prefix
509
    --srcdir=DIRECTORY                         : the source repository
510
    --no-resolve                               : disable conflict
511
resolution
512
    --version                                  : show version and copyright
513
$
514
515
516
 
517
518
 
519
ecosconfig output — <!-- <index></index> --></code></pre></td>
      </tr>
      <tr valign="middle">
         <td>520</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>list of available packages, targets and templates
521
 
522
523
$ ecosconfig list
524
Package CYGPKG_CYGMON (CygMon support via eCos):
525
aliases: cygmon
526
versions: &Version;
527
Package CYGPKG_DEVICES_WALLCLOCK_DALLAS_DS1742 (Wallclock driver for Dallas 1742):
528
aliases: devices_wallclock_ds1742 device_wallclock_ds1742
529
versions: &Version;
530
Package CYGPKG_DEVICES_WALLCLOCK_SH3 (Wallclock driver for SH3 RTC module):
531
aliases: devices_wallclock_sh3 device_wallclock_sh3
532
versions: &Version;
533
Package CYGPKG_DEVICES_WATCHDOG_ARM_AEB (Watchdog driver for ARM/AEB board):
534
aliases: devices_watchdog_aeb device_watchdog_aeb
535
versions: &Version;
536
Package CYGPKG_DEVICES_WATCHDOG_ARM_EBSA285 (Watchdog driver for ARM/EBSA285 board):
537
aliases: devices_watchdog_ebsa285 device_watchdog_ebsa285
538
versions: &Version;
539
540
541
542
 
543
 
544
545
Selecting a <!-- <index></index> --> Target
546
 
547
To configure for a listed target, type: 
548
 
549
550
$ ecosconfig new <target>
551
552
 
553
For example, to configure for the ARM PID development board,
554
type: 
555
 
556
557
$ ecosconfig new pid
558
559
 
560
You can then edit the generated file,
561
ecos.ecc, setting the options as required for the
562
target (endianess, CPU model, Startup type, etc.).  For detailed
563
information about how to edit the ecos.ecc file,
564
see the CDL Writer's Guide and 
565
LINKEND="editing-an-ecos-savefile">.
566
567
 
568
Create a build tree for the configured target by typing:
569
 
570
571
$ ecosconfig tree
572
573
 
574
575
If there are any problem with the configuration,
576
ecosconfig will tell you. The most likely cause of
577
this is mistakes when editing the ecos.ecc file.
578
You can check whether the configuration you have made is correct,
579
without building the tree with the following command:
580
581
 
582
583
$ ecosconfig check
584
585
 
586
587
If this reports any conflicts you can get
588
ecosconfig to try and resolve them itself by typing:
589
590
 
591
592
$ ecosconfig resolve
593
594
 
595
596
See  for more details.
597
598
 
599
You can now run the command make or make
600
tests, after which you will be at the same point you
601
would be after running the Configuration Tool
602
— you can start developing your own applications,
603
following the steps in . 
604
 
605
The procedure shown above allows you to do very coarse-grained
606
configuration of the eCos kernel: you can select which packages
607
to include in your kernel, and give target and start-up options.
608
But you cannot select components within a package, or set the very
609
fine-grained options. 
610
 
611
To select fine-grained configuration options you will need to
612
edit the configuration file ecos.ecc in the
613
current directory and regenerate the build tree.
614
 
615
616
You should follow the manual configuration process described
617
above very carefully, and you should read the comments in each file
618
to see when one option depends on other options or packages being
619
enabled or disabled. If you do not, you might end up with an inconsistently
620
configured kernel which could fail to build or might execute
621
incorrectly.
622
623
 
624
625
626
 
627
628
 
629
630
 
631
632
Running an <productname>eCos</productname> Test Case
633
 
634
In  or 
635
LINKEND="using-ecosconfig-on-linux"> you created the eCos test cases
636
as part of the build process. Now it is time to try and run one.
637
638
 
639
640
 
641
642
Using the <application>Configuration Tool</application>
643
 
644
Test executables that have been linked using the
645
Build->Tests operation against the current
646
configuration can be executed by selecting Tools->Run
647
Tests.
648
 
649
When a test run is invoked, a property sheet is displayed, see
650
. Press the Uncheck
651
All button and then find and check just one test,
652
bin_sem0 for example.
653
654
 
655
656
Run tests
657
658
659
 
660
661
Now press the Properties button to set up
662
communications with the target. This will bring up a properties dialog
663
shown in . If you have
664
connected the target board via a serial cable, check the
665
Serial radio button, and select the serial port
666
and baud rate for the board. If the target is connected via the
667
network select the TCP/IP button and enter the IP
668
address that the board has been given, and the port number (usually
669
9000).
670
671
 
672
673
Properties dialog box
674
675
676
 
677
678
Click OK on this dialog and go back to the Run
679
Tests dialog. Press the Run button and
680
the selected test will be downloaded and run. The
681
Output tab will show you how this is
682
progressing. If it seems to stop for a long time, check that the
683
target board is correctly connected, and that eCos has been correctly
684
configured -- especially the start-up type.
685
686
 
687
688
When the program runs you should see a couple of line similar to this appear:
689
690
 
691
692
PASS:<Binary Semaphore 0 OK>
693
EXIT:<done>
694
695
 
696
697
This indicates that the test has run successfully.
698
699
 
700
See  for
701
further details.
702
 
703
704
 
705
706
 
707
708
Using the command line
709
 
710
Start a command shell (such as a Cygwin shell window in Windows)
711
with the environment variables set as described in the toolchain
712
documentation.  Change to the directory in which you set up your build
713
tree, and invoke  GDB on the test
714
program.
715
 
716
To run the bin_sem0 test (which will
717
test the kernel for the correct creation and destruction of binary
718
semaphores) type: 
719
 
720
721
$ TARGET-gdb -nw install/tests/kernel/&Version;/tests/bin_sem0
722
723
 
724
You should see output similar to the following in the command
725
window:
726
 
727
728
GNU gdb THIS-GDB-VERSION
729
Copyright 2001 Free Software Foundation, Inc.
730
GDB is free software, covered by the GNU General Public License, and you are
731
welcome to change it and/or distribute copies of it under certain conditions.
732
Type "show copying" to see the conditions.
733
There is absolutely no warranty for GDB.  Type "show warranty" for details.
734
This GDB was configured as "--host=THIS-HOST --target=THIS-TARGET".
735
(gdb)
736
737
 
738
If you are trying to run a synthetic target test on Linux, skip the following connection and download
740
steps. Otherwise, connect to the target by typing: 
741
 
742
743
(gdb) set remotebaud 38400
744
(gdb) target remote /dev/ttyS0
745
746
on Linux or
747
748
(gdb) set remotebaud 38400
749
(gdb) target remote com1
750
751
on Windows or
752
753
(gdb) target sim
754
755
to use a simulator in either host O/S.
756
 
757
758
Check the documentation for the target board for the actual baud rate
759
to use when connecting to real targets.
760
761
 
762
763
You will see output similar to the following: 
764
 
765
766
Remote debugging using /dev/ttyS1
767
0x0000d50c in ?? ()
768
    at BASE_DIR/kernel/&Version;/src/common/kapi.cxx:345
769
 
770
Current language:  auto; currently c++
771
(gdb)
772
773
 
774
775
Or if you are using the simulator:
776
777
 
778
779
Connected to the simulator.
780
(gdb)
781
782
 
783
Now download the program to the target with
784
 
785
786
(gdb) load
787
788
 
789
You should see output similar to the following on your screen: 
790
 
791
792
Loading section .text, size 0x4b04 lma 0x108000
793
Loading section .rodata, size 0x738 lma 0x10cb08
794
Loading section .data, size 0x1c0 lma 0x10d240
795
Start address 0x108000, load size 21500
796
Transfer rate: 24571 bits/sec, 311 bytes/write.
797
(gdb)
798
799
 
800
You are now ready to run your program. If you type: 
801
 
802
803
(gdb) continue
804
805
 
806
you will see output similar to the following: 
807
 
808
809
Continuing.
810
PASS:<Binary Semaphore 0 OK>
811
EXIT:<done>
812
813
 
814
815
 If you are using a simulator or the synthetic target rather
816
            than real hardware, you must use the GDB command
817
            “run” rather than “continue” to
818
            start your program.
819
820
 
821
You can terminate your GDB session with
822
Control+C, otherwise it will sit in the
823
“idle” thread and use up CPU time. This is not a problem
824
with real targets, but may have undesirable effects in simulated or
825
synthetic targets. Type quit and you are
826
done. 
827
 
828
829
 
830
831
 
832
833
Testing Filters
834
 
835
While most test cases today run solely in the target environment,
836
some packages may require external testing infrastructure and/or
837
feedback from the external environment to do complete testing.
838
 
839
The serial package is an example of this. The network package
840
also contains some tests that require programs to be run on a
841
host. See the network Tests and Demonstrations
842
section in the network documentation in the eCos Reference
843
Guide. Here we will concentrate on the serial tests since
844
these are applicable to more targets.
845
846
 
847
Since the serial line is also used for communication with
848
GDB, a  filter is inserted in the communication pathway between
849
GDB and the serial device which is connected to the hardware target.
850
The filter forwards all communication between the two, but also
851
listens for special commands embedded in the data stream from the
852
target.
853
 
854
When such a command is seen, the filter stops forwarding data
855
to GDB from the target and enters a special mode. In this mode
856
the test case running on the target is able to control the filter,
857
commanding it to run various tests. While these tests run, GDB is
858
isolated from the target.
859
 
860
As the test completes (or if the filter detects a target crash)
861
the communication path between GDB and the hardware target is re-established,
862
allowing GDB to resume control.
863
 
864
In theory, it is possible to extend the filter to provide
865
a generic framework for other target-external testing components,
866
thus decoupling the testing infrastructure from the (possibly limited)
867
communication means provided by the target (serial, JTAG, Ethernet,
868
etc). 
869
 
870
Another advantage is that the host tools do not need to
871
know about the various testing environments required by the eCos
872
packages, since all contact with the target continues to happen
873
via GDB.
874
 
875
876
 
877
878
 
879
 
880
881
 
882
883
<!-- <xref> -->Building and <!-- <index></index> -->Running Sample Applications
884
 
885
The example programs in this tutorial are included, along
886
with a Makefile, in the examples directory
887
of the eCos distribution. The first program you will run is a hello
888
world-style application, then you will run a more complex
889
application that demonstrates the creation of threads and the use
890
of cyg_thread_delay(), and finally you will run
891
one that uses clocks and alarm handlers.
892
 
893
The Makefile depends on an externally
894
defined variable to find the eCos library and header files. This
895
variable is INSTALL_DIR and must be set to the
896
pathname of the install directory created in 
897
linkend="using-configtool-windows-linux">.
898
899
 
900
901
INSTALL_DIR may be either be set in the shell
902
environment or may be supplied on the command line. To set it in the
903
shell do the following in a bash shell:
904
905
 
906
907
$ export INSTALL_DIR=BASE_DIR/ecos-work/arm_install
908
909
 
910
911
You can then run make without any extra parameters
912
to build the examples.
913
914
 
915
916
Alternatively, if you can do the following:
917
918
 
919
920
$ make INSTALL_DIR=BASE_DIR/ecos-work/arm_install
921
922
 
923
924
 
925
926
<productname>eCos</productname> Hello World
927
 
928
The following code is found in the file hello.c
929
in the examples directory: 
930
 
931
932
<productname>eCos</productname><!-- <index></index> --> hello world program listing
933
 
934
935
/* this is a simple hello world program */
936
#include <stdio.h>
937
int main(void)
938
{
939
 printf("Hello, eCos world!\n");
940
 return 0;
941
}
942
943
 
944
To compile this or any other program that is not part of the
945
eCos distribution, you can follow the procedures described below. Type
946
this explicit compilation command (assuming your current working
947
directory is also where you built the eCos kernel):
948
 
949
950
$ TARGET-gcc -g -IBASE_DIR/ecos-work/install/include hello.c -LBASE_DIR/ecos-work/install/lib -Ttarget.ld -nostdlib
951
952
 
953
The compilation command above contains some standard GCC
954
options (for example,  enables debugging), as well
955
as some mention of paths
956
( allows files
957
like cyg/kernel/kapi.h to be found, and
958
 allows the linker to
959
find ). 
960
 
961
The executable program will be called a.out. 
962
 
963
964
Some target systems require special options to be passed to
965
gcc to compile correctly for that system. Please examine the Makefile
966
in the examples directory to see if this applies to your target.
967
968
 
969
You can now run the resulting program using GDB in exactly the
970
same the way you ran the test case before. The procedure will be the
971
same, but this time run
972
TARGET-gdb specifying
973
 on the command line:
974
 
975
976
$ TARGET-gdb -nw a.out
977
978
 
979
For targets other than the synthetic linux target, you should
980
now run the usual GDB commands described earlier. Once this is done,
981
typing the command "continue" at the (gdb) prompt ("run" for
982
simulators) will allow the program to execute and print the string
983
"Hello, eCos world!" on your screen.
984
 
985
On the synthetic linux target, you may use the "run" command
986
immediately - you do not need to connect to the target, nor use the
987
"load" command.
988
 
989
990
991
 
992
993
 
994
995
A Sample Program with Two Threads
996
 
997
Below is a program that uses some of eCos' system calls. It
998
creates two threads, each of which goes into an infinite loop in which
999
it sleeps for a while (using cyg_thread_delay()).  This code is found
1000
in the file twothreads.c
1001
in the examples directory.
1002
 
1003
1004
<productname>eCos</productname> <!-- <index></index> -->two-threaded program listing
1005
 
1006
1007
#include <cyg/kernel/kapi.h>
1008
#include <stdio.h>
1009
#include <math.h>
1010
#include <stdlib.h>
1011
 
1012
/* now declare (and allocate space for) some kernel objects,
1013
  like the two threads we will use */
1014
cyg_thread thread_s[2]; /* space for two thread objects */
1015
 
1016
char stack[2][4096];    /* space for two 4K stacks */
1017
 
1018
/* now the handles for the threads */
1019
cyg_handle_t simple_threadA, simple_threadB;
1020
 
1021
/* and now variables for the procedure which is the thread */
1022
cyg_thread_entry_t simple_program;
1023
 
1024
/* and now a mutex to protect calls to the C library */
1025
cyg_mutex_t cliblock;
1026
 
1027
/* we install our own startup routine which sets up threads */
1028
void cyg_user_start(void)
1029
{
1030
 printf("Entering twothreads' cyg_user_start() function\n");
1031
 
1032
 cyg_mutex_init(&cliblock);
1033
 
1034
 cyg_thread_create(4, simple_program, (cyg_addrword_t) 0,
1035
        "Thread A", (void *) stack[0], 4096,
1036
        &simple_threadA, &thread_s[0]);
1037
 cyg_thread_create(4, simple_program, (cyg_addrword_t) 1,
1038
        "Thread B", (void *) stack[1], 4096,
1039
        &simple_threadB, &thread_s[1]);
1040
 
1041
 cyg_thread_resume(simple_threadA);
1042
 cyg_thread_resume(simple_threadB);
1043
}
1044
 
1045
/* this is a simple program which runs in a thread */
1046
void simple_program(cyg_addrword_t data)
1047
{
1048
 int message = (int) data;
1049
 int delay;
1050
 
1051
 printf("Beginning execution; thread data is %d\n", message);
1052
 
1053
 cyg_thread_delay(200);
1054
 
1055
 for (;;) {
1056
 delay = 200 + (rand() % 50);
1057
 
1058
 /* note: printf() must be protected by a
1059
 call to cyg_mutex_lock() */
1060
 cyg_mutex_lock(&cliblock); {
1061
 printf("Thread %d: and now a delay of %d clock ticks\n",
1062
        message, delay);
1063
 }
1064
 cyg_mutex_unlock(&cliblock);
1065
 cyg_thread_delay(delay);
1066
 }
1067
}
1068
1069
 
1070
1071
When you run the program (by typing continue at
1072
the (gdb) prompt) the output should look like
1073
this:
1074
 
1075
1076
Starting program: BASE_DIR/examples/twothreads.exe
1077
Entering twothreads' cyg_user_start()
1078
function
1079
Beginning execution; thread data is 0
1080
Beginning execution; thread data is 1
1081
Thread 0: and now a delay of 240 clock ticks
1082
Thread 1: and now a delay of 225 clock ticks
1083
Thread 1: and now a delay of 234 clock ticks
1084
Thread 0: and now a delay of 231 clock ticks
1085
Thread 1: and now a delay of 224 clock ticks
1086
Thread 0: and now a delay of 249 clock ticks
1087
Thread 1: and now a delay of 202 clock ticks
1088
Thread 0: and now a delay of 235 clock ticks
1089
1090
 
1091
1092
When running in a simulator the 
1093
delays might be quite long. On a hardware board (where the clock
1094
speed is 100 ticks/second) the delays should average to
1095
about 2.25 seconds. In simulation, the delay will depend on the
1096
speed of the host processor and will almost always be much slower than
1097
the actual board. You might want to reduce the delay parameter when running
1098
in simulation.
1099
1100
1101
 
1102
1103
 shows how this
1104
multitasking program executes.  Note that apart from the thread
1105
creation system calls, this program also creates and uses a
1106
mutex for synchronization
1107
between the printf() calls in the two
1108
threads. This is because the C library standard I/O (by default) is
1109
configured not to be thread-safe, which means that if more than one
1110
thread is using standard I/O they might corrupt each other. This is
1111
fixed by a mutual exclusion (or mutex) lockout
1112
mechanism: the threads do not call printf() until
1113
cyg_mutex_lock() has returned, which only happens
1114
when the other thread calls
1115
cyg_mutex_unlock().
1116
 
1117
You could avoid using the mutex by configuring the C library to
1118
be thread-safe (by selecting the component
1119
CYGSEM_LIBC_STDIO_THREAD_SAFE_STREAMS).
1120
 
1121
1122
ID="FIGURE-TWOTHREADS-WITH-SIMPLE-PRINTS"> Two</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>1123</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>threads with simple print statements after random delays
1124
1125
1126
 
1127
1128
 
1129
1130
 
1131
1132
 
1133
1134
 
1135
1136
More Features — <!-- <index></index> -->Clocks and Alarm</code></pre></td>
      </tr>
      <tr valign="middle">
         <td>1137</td>
         <td></td>
         <td></td>
         <td class="code"><pre><code>Handlers
1138
 
1139
If a program wanted to execute a task at a given time, or
1140
periodically, it could do it in an inefficient way by sitting in a
1141
loop and checking the real-time clock to see if the proper amount of
1142
time has elapsed. But operating systems usually provide system calls
1143
which allow the program to be informed at the desired time.
1144
 
1145
eCos provides a rich timekeeping formalism, involving
1146
counters, clocks,
1147
alarms, and timers.  The
1148
precise definition, relationship, and motivation of these features is
1149
beyond the scope of this tutorial, but these examples illustrate how
1150
to set up basic periodic tasks.
1151
 
1152
Alarms are events that happen at
1153
a given time, either once or periodically. A thread associates an
1154
alarm handling function with the alarm, so that the function will
1155
be invoked every time the alarm “goes off”.
1156
 
1157
1158
 
1159
1160
A Sample Program with Alarms
1161
 
1162
simple-alarm.c (in
1163
the examples directory) is a short program that creates a thread that
1164
creates an alarm. The alarm is handled by the function
1165
test_alarm_func(), which sets a global
1166
variable. When the main thread of execution sees that the variable has
1167
changed, it prints a message.
1168
 
1169
1170
A sample <!-- <index></index> -->program that creates an alarm
1171
 
1172
1173
/* this is a very simple program meant to demonstrate
1174
 a basic use of time, alarms and alarm-handling functions  in eCos */
1175
 
1176
#include <cyg/kernel/kapi.h>
1177
 
1178
#include <stdio.h>
1179
 
1180
#define NTHREADS 1
1181
#define STACKSIZE 4096
1182
 
1183
static cyg_handle_t thread[NTHREADS];
1184
 
1185
static cyg_thread thread_obj[NTHREADS];
1186
static char stack[NTHREADS][STACKSIZE];
1187
 
1188
static void alarm_prog( cyg_addrword_t data );
1189
 
1190
/* we install our own startup routine which sets up
1191
  threads and starts the scheduler */
1192
void cyg_user_start(void)
1193
{
1194
 cyg_thread_create(4, alarm_prog, (cyg_addrword_t) 0,
1195
        "alarm_thread", (void *) stack[0],
1196
        STACKSIZE, &thread[0], &thread_obj[0]);
1197
 cyg_thread_resume(thread[0]);
1198
}
1199
 
1200
/* we need to declare the alarm handling function (which is
1201
 defined below), so that we can pass it to  cyg_alarm_initialize() */
1202
cyg_alarm_t test_alarm_func;
1203
 
1204
/* alarm_prog() is a thread which sets up an alarm which is then
1205
 handled by test_alarm_func() */
1206
static void alarm_prog(cyg_addrword_t data)
1207
{
1208
 cyg_handle_t test_counterH, system_clockH, test_alarmH;
1209
 cyg_tick_count_t ticks;
1210
 cyg_alarm test_alarm;
1211
 unsigned how_many_alarms = 0, prev_alarms = 0, tmp_how_many;
1212
 
1213
 system_clockH = cyg_real_time_clock();
1214
 cyg_clock_to_counter(system_clockH, &test_counterH);
1215
 cyg_alarm_create(test_counterH, test_alarm_func,
1216
        (cyg_addrword_t) &how_many_alarms,
1217
        &test_alarmH, &test_alarm);
1218
 cyg_alarm_initialize(test_alarmH, cyg_current_time()+200, 200);
1219
 
1220
 /* get in a loop in which we read the current time and
1221
    print it out, just to have something scrolling by */
1222
 for (;;) {
1223
   ticks = cyg_current_time();
1224
   printf("Time is %llu\n", ticks);
1225
   /* note that we must lock access to how_many_alarms, since the
1226
   alarm handler might change it. this involves using the
1227
   annoying temporary variable tmp_how_many so that I can keep the
1228
   critical region short */
1229
   cyg_scheduler_lock();
1230
   tmp_how_many = how_many_alarms;
1231
   cyg_scheduler_unlock();
1232
   if (prev_alarms != tmp_how_many) {
1233
     printf(" --- alarm calls so far: %u\n", tmp_how_many);
1234
     prev_alarms = tmp_how_many;
1235
   }
1236
   cyg_thread_delay(30);
1237
 }
1238
}
1239
 
1240
/* test_alarm_func() is invoked as an alarm handler, so
1241
   it should be quick and simple. in this case it increments
1242
   the data that is passed to it. */
1243
void test_alarm_func(cyg_handle_t alarmH, cyg_addrword_t data)
1244
{
1245
 ++*((unsigned *) data);
1246
}
1247
1248
1249
 
1250
When you run this program (by typing continue at
1251
the (gdb) prompt) the output should look like
1252
this:
1253
1254
Starting program: BASE_DIR/examples/simple-alarm.exe
1255
Time is 0
1256
Time is 30
1257
Time is 60
1258
Time is 90
1259
Time is 120
1260
Time is 150
1261
Time is 180
1262
Time is 210
1263
  --- alarm calls so far: 1
1264
Time is 240
1265
Time is 270
1266
Time is 300
1267
Time is 330
1268
Time is 360
1269
Time is 390
1270
Time is 420
1271
  --- alarm calls so far: 2
1272
Time is 450
1273
Time is 480
1274
1275
 
1276
1277
When running in a simulator the  delays
1278
might be quite long. On a hardware board (where the clock speed is 100
1279
ticks/second) the delays should average to about 0.3 seconds (and 2
1280
seconds between alarms). In simulation, the delay will depend on the
1281
speed of the host processor and will almost always be much slower than
1282
the actual board. You might want to reduce the delay parameter when
1283
running in simulation.
1284
1285
 
1286
Here are a few things you might notice about this program:
1287
 
1288
1289
1290
It used the cyg_real_time_clock() function;
1291
this always returns a handle to the default system real-time  clock. 
1293
1294
 
1295
1296
Clocks are based on  counters, so the function cyg_alarm_create()
1298
uses a counter handle. The program used the function
1299
cyg_clock_to_counter() to strip the clock handle
1300
to the underlying counter handle. 
1301
1302
 
1303
1304
Once the alarm is created it is 
1305
initialized with cyg_alarm_initialize(), which
1306
sets the time at which the alarm should go off, as well as the period
1307
for repeating alarms. It is set to go off at the current time and
1308
then to repeat every 200 ticks. 
1309
1310
 
1311
1312
The alarm handler function
1313
test_alarm_func() conforms to the guidelines for
1314
writing alarm handlers and other  delayed service routines: it does not invoke any
1316
functions which might lock the scheduler.  This is discussed in detail
1317
in the eCos Reference Manual, in the chapter
1318
The eCos Kernel.
1319
1320
 
1321
1322
There is a critical region in this program:
1323
the variable how_many_alarms is accessed in the
1324
main thread of control and is also modified in the alarm handler. To
1325
prevent a possible (though unlikely) race condition on this variable,
1326
access to how_many_alarms in the principal thread
1327
is protected by calls to cyg_scheduler_lock() and
1328
cyg_scheduler_unlock(). When the scheduler is
1329
locked, the alarm handler will not be invoked, so the problem is
1330
averted. 
1331
1332
1333
1334
1335
 
1336
1337
 

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.