summaryrefslogtreecommitdiff
path: root/ecos/examples
diff options
context:
space:
mode:
authorMichael Gielda <mgielda@antmicro.com>2014-04-03 14:53:04 +0200
committerMichael Gielda <mgielda@antmicro.com>2014-04-03 14:53:04 +0200
commitae1e4e08a1005a0c487f03ba189d7536e7fdcba6 (patch)
treef1c296f8a966a9a39876b0e98e16d9c5da1776dd /ecos/examples
parentf157da5337118d3c5cd464266796de4262ac9dbd (diff)
Added the OS files
Diffstat (limited to 'ecos/examples')
-rw-r--r--ecos/examples/ChangeLog8
-rw-r--r--ecos/examples/Makefile55
-rwxr-xr-xecos/examples/build_Make.params74
-rwxr-xr-xecos/examples/build_Makefile39
-rw-r--r--ecos/examples/hello.c8
-rw-r--r--ecos/examples/instrument-test.c50
-rw-r--r--ecos/examples/serial.c83
-rw-r--r--ecos/examples/simple-alarm.c76
-rw-r--r--ecos/examples/twothreads.c62
9 files changed, 455 insertions, 0 deletions
diff --git a/ecos/examples/ChangeLog b/ecos/examples/ChangeLog
new file mode 100644
index 0000000..4c62ab2
--- /dev/null
+++ b/ecos/examples/ChangeLog
@@ -0,0 +1,8 @@
+2003-03-03 John Dallaway <jld@ecoscentric.com>
+
+ Makefile: Rename ECOS_INSTALL -> INSTALL_DIR to match
+ documentation.
+
+2003-01-20 John Dallaway <jld@ecoscentric.com>
+
+ Makefile: Simplify by reference to ecos.mak
diff --git a/ecos/examples/Makefile b/ecos/examples/Makefile
new file mode 100644
index 0000000..77ea85c
--- /dev/null
+++ b/ecos/examples/Makefile
@@ -0,0 +1,55 @@
+# Mostly written by Jonathan Larmour, Red Hat, Inc.
+# Reference to ecos.mak added by John Dallaway, eCosCentric Limited, 2003-01-20
+# This file is in the public domain and may be used for any purpose
+
+# Usage: make INSTALL_DIR=/path/to/ecos/install
+
+INSTALL_DIR=$$(INSTALL_DIR) # override on make command line
+
+include $(INSTALL_DIR)/include/pkgconf/ecos.mak
+
+XCC = $(ECOS_COMMAND_PREFIX)gcc
+XCXX = $(XCC)
+XLD = $(XCC)
+
+CFLAGS = -I$(INSTALL_DIR)/include
+CXXFLAGS = $(CFLAGS)
+LDFLAGS = -nostartfiles -L$(INSTALL_DIR)/lib -Ttarget.ld
+
+# RULES
+
+.PHONY: all clean
+
+all: hello twothreads simple-alarm serial
+
+clean:
+ -rm -f hello hello.o twothreads twothreads.o
+ -rm -f simple-alarm simple-alarm.o serial serial.o
+ -rm -f instrument-test instrument-test.o
+
+%.o: %.c
+ $(XCC) -c -o $*.o $(CFLAGS) $(ECOS_GLOBAL_CFLAGS) $<
+
+%.o: %.cxx
+ $(XCXX) -c -o $*.o $(CXXFLAGS) $(ECOS_GLOBAL_CFLAGS) $<
+
+%.o: %.C
+ $(XCXX) -c -o $*.o $(CXXFLAGS) $(ECOS_GLOBAL_CFLAGS) $<
+
+%.o: %.cc
+ $(XCXX) -c -o $*.o $(CXXFLAGS) $(ECOS_GLOBAL_CFLAGS) $<
+
+hello: hello.o
+ $(XLD) $(LDFLAGS) $(ECOS_GLOBAL_LDFLAGS) -o $@ $@.o
+
+twothreads: twothreads.o
+ $(XLD) $(LDFLAGS) $(ECOS_GLOBAL_LDFLAGS) -o $@ $@.o
+
+simple-alarm: simple-alarm.o
+ $(XLD) $(LDFLAGS) $(ECOS_GLOBAL_LDFLAGS) -o $@ $@.o
+
+serial: serial.o
+ $(XLD) $(LDFLAGS) $(ECOS_GLOBAL_LDFLAGS) -o $@ $@.o
+
+instrument-test: instrument-test.o
+ $(XLD) $(LDFLAGS) $(ECOS_GLOBAL_LDFLAGS) -o $@ $@.o
diff --git a/ecos/examples/build_Make.params b/ecos/examples/build_Make.params
new file mode 100755
index 0000000..7e868b7
--- /dev/null
+++ b/ecos/examples/build_Make.params
@@ -0,0 +1,74 @@
+#! /bin/sh
+
+# This script will set up a Makefile fragment with
+# platform specifics. This fragement can be used by
+# the automatically generated Makefile (via the script
+# 'build_Makefile')
+
+# Copied from 'makefile' in the "install" tree
+
+ECOS_BUILD_DIR=${1-`pwd`}
+ECOS_INSTALL_DIR=`echo ${ECOS_BUILD_DIR}/install`
+if [ ! -d ${ECOS_BUILD_DIR}/install ]; then
+ if [ ! -d ${ECOS_BUILD_DIR}/*_build ]; then
+ echo "Not an eCos install tree"
+ echo "usage: <eCos_repository>/build_Make.params [<eCos_install_dir>]"
+ echo "... where <eCos_install_dir> is the directory which contains"
+ echo " the *.ecc configuration file used to build the kernel"
+ exit
+ else
+ ECOS_INSTALL_DIR=`echo ${ECOS_BUILD_DIR}/*_install`
+ ECOS_BUILD_DIR=`echo ${ECOS_BUILD_DIR}/*_build`
+ fi
+fi
+
+cat <<EOF >Make.params
+
+# Copied from 'makefile' in the "install" tree
+
+EOF
+
+grep export ${ECOS_BUILD_DIR}/makefile >>Make.params
+
+cat <<EOF >>Make.params
+
+#
+# Target specific flags, etc.
+#
+
+EOF
+
+cat ${ECOS_INSTALL_DIR}/include/pkgconf/ecos.mak >>Make.params
+
+#
+# Add CFLAGS manipulation - this should match <ecos>/packages/pkgconf/rules.mak
+#
+
+cat <<EOF >>Make.params
+
+#
+# Modify CFLAGS to match newer compilers
+#
+ACTUAL_CFLAGS = \$(ECOS_GLOBAL_CFLAGS)
+ACTUAL_CXXFLAGS = \$(ECOS_GLOBAL_CFLAGS)
+ACTUAL_LDFLAGS = \$(ECOS_GLOBAL_LDFLAGS)
+
+# GCC since 2.95 does -finit-priority by default so remove it from old HALs
+ACTUAL_CFLAGS := \$(subst -finit-priority,,\$(ACTUAL_CFLAGS))
+
+# -fvtable-gc is known to be broken in all recent GCC.
+ACTUAL_CFLAGS := \$(subst -fvtable-gc,,\$(ACTUAL_CFLAGS))
+
+# Expand inline functions
+ACTUAL_CFLAGS := -finline-limit=7000 \$(ACTUAL_CFLAGS)
+
+# Separate C++ flags out from C flags.
+ACTUAL_CFLAGS := \$(subst -fno-rtti,,\$(ACTUAL_CFLAGS))
+ACTUAL_CFLAGS := \$(subst -frtti,,\$(ACTUAL_CFLAGS))
+ACTUAL_CFLAGS := \$(subst -Woverloaded-virtual,,\$(ACTUAL_CFLAGS))
+ACTUAL_CFLAGS := \$(subst -fvtable-gc,,\$(ACTUAL_CFLAGS))
+
+ACTUAL_CXXFLAGS := \$(subst -Wstrict-prototypes,,\$(ACTUAL_CXXFLAGS))
+
+EOF
+
diff --git a/ecos/examples/build_Makefile b/ecos/examples/build_Makefile
new file mode 100755
index 0000000..46d8c9a
--- /dev/null
+++ b/ecos/examples/build_Makefile
@@ -0,0 +1,39 @@
+#! /bin/sh
+
+# This script will create a generic Makefile template
+# suitable for use with an installed eCos configuration.
+
+# usage: <eCos_repository>/build_Makefile [<eCos_install_dir>]
+# env: ${SRCS} - the default list of source files
+# ${DST} - the default target
+ECOS_DIR=${1-`pwd`}
+
+cat <<EOF >Makefile
+#
+# Makefile for eCos tests
+#
+
+# Platform specific setups
+include Make.params
+
+# Simple build rules
+
+.c.o:
+ \$(CC) -c \$(ACTUAL_CFLAGS) -I\$(PREFIX)/include \$*.c
+
+.o:
+ \$(CC) \$(ACTUAL_LDFLAGS) -L\$(PREFIX)/lib -Ttarget.ld \$*.o -o \$@
+
+SRCS=${SRCS-source_file.c}
+OBJS=\${SRCS:%.c=%.o}
+DST=${DST-result_prog}
+
+\${DST}: \${OBJS}
+
+EOF
+
+# Create actual parameters
+
+`dirname $0`/build_Make.params ${ECOS_DIR}
+
+
diff --git a/ecos/examples/hello.c b/ecos/examples/hello.c
new file mode 100644
index 0000000..94709d9
--- /dev/null
+++ b/ecos/examples/hello.c
@@ -0,0 +1,8 @@
+/* this is a simple hello world program */
+#include <stdio.h>
+
+int main(void)
+{
+ printf("Hello, eCos world!\n");
+ return 0;
+}
diff --git a/ecos/examples/instrument-test.c b/ecos/examples/instrument-test.c
new file mode 100644
index 0000000..21444d5
--- /dev/null
+++ b/ecos/examples/instrument-test.c
@@ -0,0 +1,50 @@
+/* this is a program which uses eCos instrumentation buffers; it needs
+ to be linked with a kernel which was compiled with support for
+ instrumentation */
+
+#include <stdio.h>
+#include <pkgconf/kernel.h>
+#include <cyg/kernel/instrmnt.h>
+#include <cyg/kernel/kapi.h>
+
+#ifndef CYGVAR_KERNEL_INSTRUMENT_EXTERNAL_BUFFER
+# error You must configure eCos with CYGVAR_KERNEL_INSTRUMENT_EXTERNAL_BUFFER
+#endif
+
+struct Instrument_Record
+{
+ CYG_WORD16 type; // record type
+ CYG_WORD16 thread; // current thread id
+ CYG_WORD timestamp; // 32 bit timestamp
+ CYG_WORD arg1; // first arg
+ CYG_WORD arg2; // second arg
+};
+
+struct Instrument_Record instrument_buffer[20];
+cyg_uint32 instrument_buffer_size = 20;
+
+int main(void)
+{
+ int i;
+
+ cyg_instrument_enable(CYG_INSTRUMENT_CLASS_CLOCK, 0);
+ cyg_instrument_enable(CYG_INSTRUMENT_CLASS_THREAD, 0);
+ cyg_instrument_enable(CYG_INSTRUMENT_CLASS_ALARM, 0);
+
+ printf("Program to play with instrumentation buffer\n");
+
+ cyg_thread_delay(2);
+
+ cyg_instrument_disable(CYG_INSTRUMENT_CLASS_CLOCK, 0);
+ cyg_instrument_disable(CYG_INSTRUMENT_CLASS_THREAD, 0);
+ cyg_instrument_disable(CYG_INSTRUMENT_CLASS_ALARM, 0);
+
+ for (i = 0; i < instrument_buffer_size; ++i) {
+ printf("Record %02d: type 0x%04x, thread %d, ",
+ i, instrument_buffer[i].type, instrument_buffer[i].thread);
+ printf("time %5d, arg1 0x%08x, arg2 0x%08x\n",
+ instrument_buffer[i].timestamp, instrument_buffer[i].arg1,
+ instrument_buffer[i].arg2);
+ }
+ return 0;
+}
diff --git a/ecos/examples/serial.c b/ecos/examples/serial.c
new file mode 100644
index 0000000..a35155f
--- /dev/null
+++ b/ecos/examples/serial.c
@@ -0,0 +1,83 @@
+/*
+ * Written 1999-03-19 by Jonathan Larmour, Cygnus Solutions
+ * This file is in the public domain and may be used for any purpose
+ */
+
+/* CONFIGURATION CHECKS */
+
+#include <pkgconf/system.h> /* which packages are enabled/disabled */
+#ifdef CYGPKG_KERNEL
+# include <pkgconf/kernel.h>
+#endif
+#ifdef CYGPKG_LIBC
+# include <pkgconf/libc.h>
+#endif
+#ifdef CYGPKG_IO_SERIAL
+# include <pkgconf/io_serial.h>
+#endif
+
+#ifndef CYGFUN_KERNEL_API_C
+# error Kernel API must be enabled to build this example
+#endif
+
+#ifndef CYGPKG_LIBC_STDIO
+# error C library standard I/O must be enabled to build this example
+#endif
+
+#ifndef CYGPKG_IO_SERIAL_HALDIAG
+# error I/O HALDIAG pseudo-device driver must be enabled to build this example
+#endif
+
+/* INCLUDES */
+
+#include <stdio.h> /* printf */
+#include <string.h> /* strlen */
+#include <cyg/kernel/kapi.h> /* All the kernel specific stuff */
+#include <cyg/io/io.h> /* I/O functions */
+#include <cyg/hal/hal_arch.h> /* CYGNUM_HAL_STACK_SIZE_TYPICAL */
+
+
+
+/* DEFINES */
+
+#define NTHREADS 1
+#define STACKSIZE ( CYGNUM_HAL_STACK_SIZE_TYPICAL + 4096 )
+
+/* STATICS */
+
+static cyg_handle_t thread[NTHREADS];
+static cyg_thread thread_obj[NTHREADS];
+static char stack[NTHREADS][STACKSIZE];
+
+/* FUNCTIONS */
+
+static void simple_prog(CYG_ADDRESS data)
+{
+ cyg_io_handle_t handle;
+ Cyg_ErrNo err;
+ const char test_string[] = "serial example is working correctly!\n";
+ cyg_uint32 len = strlen(test_string);
+
+ printf("Starting serial example\n");
+
+ err = cyg_io_lookup( "/dev/haldiag", &handle );
+
+ if (ENOERR == err) {
+ printf("Found /dev/haldiag. Writing string....\n");
+ err = cyg_io_write( handle, test_string, &len );
+ }
+
+ if (ENOERR == err) {
+ printf("I think I wrote the string. Did you see it?\n");
+ }
+
+ printf("Serial example finished\n");
+
+}
+
+void cyg_user_start(void)
+{
+ cyg_thread_create(4, simple_prog, (cyg_addrword_t) 0, "serial",
+ (void *)stack[0], STACKSIZE, &thread[0], &thread_obj[0]);
+ cyg_thread_resume(thread[0]);
+}
diff --git a/ecos/examples/simple-alarm.c b/ecos/examples/simple-alarm.c
new file mode 100644
index 0000000..e99ae5d
--- /dev/null
+++ b/ecos/examples/simple-alarm.c
@@ -0,0 +1,76 @@
+/* this is a very simple program meant to demonstrate
+ a basic use of time, alarms and alarm-handling functions
+ in eCos */
+
+#include <cyg/kernel/kapi.h>
+
+#include <stdio.h>
+
+#define NTHREADS 1
+#define STACKSIZE 4096
+
+static cyg_handle_t thread[NTHREADS];
+
+static cyg_thread thread_obj[NTHREADS];
+static char stack[NTHREADS][STACKSIZE];
+
+static void alarm_prog( cyg_addrword_t data );
+
+/* we install our own startup routine which sets up
+ threads and starts the scheduler */
+void cyg_user_start(void)
+{
+ cyg_thread_create(4, alarm_prog, (cyg_addrword_t) 0,
+ "alarm_thread", (void *) stack[0],
+ STACKSIZE, &thread[0], &thread_obj[0]);
+ cyg_thread_resume(thread[0]);
+}
+
+/* we need to declare the alarm handling function (which is
+ defined below), so that we can pass it to
+ cyg_alarm_initialize() */
+cyg_alarm_t test_alarm_func;
+
+/* alarm_prog() is a thread which sets up an alarm which is then
+ handled by test_alarm_func() */
+static void alarm_prog(cyg_addrword_t data)
+{
+ cyg_handle_t test_counterH, system_clockH, test_alarmH;
+ cyg_tick_count_t ticks;
+ cyg_alarm test_alarm;
+ unsigned how_many_alarms = 0, prev_alarms = 0, tmp_how_many;
+
+ system_clockH = cyg_real_time_clock();
+ cyg_clock_to_counter(system_clockH, &test_counterH);
+ cyg_alarm_create(test_counterH, test_alarm_func,
+ (cyg_addrword_t) &how_many_alarms,
+ &test_alarmH, &test_alarm);
+ cyg_alarm_initialize(test_alarmH, cyg_current_time()+200, 200);
+
+ /* get in a loop in which we read the current time and
+ print it out, just to have something scrolling by */
+ for (;;) {
+ ticks = cyg_current_time();
+ printf("Time is %llu\n", ticks);
+ /* note that we must lock access to how_many_alarms, since the
+ alarm handler might change it. this involves using the
+ annoying temporary variable tmp_how_many so that I can keep the
+ critical region short */
+ cyg_scheduler_lock();
+ tmp_how_many = how_many_alarms;
+ cyg_scheduler_unlock();
+ if (prev_alarms != tmp_how_many) {
+ printf(" ---> alarm calls so far: %u\n", tmp_how_many);
+ prev_alarms = tmp_how_many;
+ }
+ cyg_thread_delay(30);
+ }
+}
+
+/* test_alarm_func() is invoked as an alarm handler, so
+ it should be quick and simple. in this case it increments
+ the data that is passed to it. */
+void test_alarm_func(cyg_handle_t alarmH, cyg_addrword_t data)
+{
+ ++*((unsigned *) data);
+}
diff --git a/ecos/examples/twothreads.c b/ecos/examples/twothreads.c
new file mode 100644
index 0000000..bbc7347
--- /dev/null
+++ b/ecos/examples/twothreads.c
@@ -0,0 +1,62 @@
+#include <cyg/kernel/kapi.h>
+
+#include <stdio.h>
+#include <math.h>
+#include <stdlib.h>
+
+/* now declare (and allocate space for) some kernel objects,
+ like the two threads we will use */
+cyg_thread thread_s[2]; /* space for two thread objects */
+
+char stack[2][4096]; /* space for two 4K stacks */
+
+/* now the handles for the threads */
+cyg_handle_t simple_threadA, simple_threadB;
+
+/* and now variables for the procedure which is the thread */
+cyg_thread_entry_t simple_program;
+
+/* and now a mutex to protect calls to the C library */
+cyg_mutex_t cliblock;
+
+/* we install our own startup routine which sets up threads */
+void cyg_user_start(void)
+{
+ printf("Entering twothreads' cyg_user_start() function\n");
+
+ cyg_mutex_init(&cliblock);
+
+ cyg_thread_create(4, simple_program, (cyg_addrword_t) 0,
+ "Thread A", (void *) stack[0], 4096,
+ &simple_threadA, &thread_s[0]);
+ cyg_thread_create(4, simple_program, (cyg_addrword_t) 1,
+ "Thread B", (void *) stack[1], 4096,
+ &simple_threadB, &thread_s[1]);
+
+ cyg_thread_resume(simple_threadA);
+ cyg_thread_resume(simple_threadB);
+}
+
+/* this is a simple program which runs in a thread */
+void simple_program(cyg_addrword_t data)
+{
+ int message = (int) data;
+ int delay;
+
+ printf("Beginning execution; thread data is %d\n", message);
+
+ cyg_thread_delay(200);
+
+ for (;;) {
+ delay = 200 + (rand() % 50);
+
+ /* note: printf() must be protected by a
+ call to cyg_mutex_lock() */
+ cyg_mutex_lock(&cliblock); {
+ printf("Thread %d: and now a delay of %d clock ticks\n",
+ message, delay);
+ }
+ cyg_mutex_unlock(&cliblock);
+ cyg_thread_delay(delay);
+ }
+}