diff --git a/examples/README.txt b/examples/README.txt index aafd7ac47..5002b77c4 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -1441,6 +1441,11 @@ examples/null This is the do nothing application. It is only used for bringing up new NuttX architectures in the most minimal of environments. +examples/oneshot +^^^^^^^^^^^^^^^^ + + Simple test of a oneshot driver. + examples/ostest ^^^^^^^^^^^^^^^ diff --git a/examples/oneshot/.gitignore b/examples/oneshot/.gitignore new file mode 100644 index 000000000..fa1ec7579 --- /dev/null +++ b/examples/oneshot/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.depend +/.built +/*.asm +/*.obj +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/oneshot/Kconfig b/examples/oneshot/Kconfig new file mode 100644 index 000000000..d1be8a500 --- /dev/null +++ b/examples/oneshot/Kconfig @@ -0,0 +1,65 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_ONESHOT + bool "Oneshot timer example" + default n + depends on ONESHOT + ---help--- + Enable the oneshot timer driver example + +if EXAMPLES_ONESHOT + +config EXAMPLE_ONESHOT_DEVNAME + string "Oneshot time device name" + default "/dev/oneshot" + ---help--- + This is the default name of the timer device that will be tested if + one is not specified on the command line. + +config EXAMPLE_ONESHOT_DELAY + int "Sample delay (microseconds)" + default 100000 + ---help--- + This is the default delay samples in microseconds if one is not + specified on the command line + +config EXAMPLE_ONESHOT_SIGNO + int "Signal number" + default 13 + ---help--- + This is the number of the signal that will be used in the oneshot + notification. + +config EXAMPLES_ONESHOT_APPNAME + string "Oneshot timer executable name" + default "oneshot" + depends on NSH_BUILTIN_APPS + ---help--- + This is the name of the built-in application + +config EXAMPLES_ONESHOT_STACKSIZE + int "Oneshot timer stack size" + default 2048 + depends on NSH_BUILTIN_APPS + ---help--- + This is the stack size allocated when the oneshot timer task runs + +config EXAMPLES_ONESHOT_PRIORITY + int "Oneshot timer task priority" + default 100 + depends on NSH_BUILTIN_APPS + ---help--- + This is the priority of the oneshot timer task + +config EXAMPLES_ONESHOT_PROGNAME + string "Oneshot timer program name" + default "oneshot" + depends on BUILD_KERNEL + ---help--- + This is the name of the program that will be use when the NSH ELF + program is installed. + +endif diff --git a/examples/oneshot/Make.defs b/examples/oneshot/Make.defs new file mode 100644 index 000000000..728d63a05 --- /dev/null +++ b/examples/oneshot/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/oneshot/Make.defs +# Adds selected applications to apps/ build +# +# Copyright (C) 2016 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +ifeq ($(CONFIG_EXAMPLES_ONESHOT),y) +CONFIGURED_APPS += examples/oneshot +endif diff --git a/examples/oneshot/Makefile b/examples/oneshot/Makefile new file mode 100644 index 000000000..c99b34370 --- /dev/null +++ b/examples/oneshot/Makefile @@ -0,0 +1,56 @@ +############################################################################ +# apps/examples/oneshot/Makefile +# +# Copyright (C) 2016 Gregory Nutt. All rights reserved. +# Author: Gregory Nutt +# +# Redistribution and use in source and binary forms, with or without +# modification, are permitted provided that the following conditions +# are met: +# +# 1. Redistributions of source code must retain the above copyright +# notice, this list of conditions and the following disclaimer. +# 2. Redistributions in binary form must reproduce the above copyright +# notice, this list of conditions and the following disclaimer in +# the documentation and/or other materials provided with the +# distribution. +# 3. Neither the name NuttX nor the names of its contributors may be +# used to endorse or promote products derived from this software +# without specific prior written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS +# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT +# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS +# FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE +# COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, +# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, +# BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS +# OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED +# AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT +# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN +# ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +# POSSIBILITY OF SUCH DAMAGE. +# +############################################################################ + +-include $(TOPDIR)/Make.defs + +# Oneshot timer built-in application info + +CONFIG_EXAMPLES_ONESHOT_APPNAME ?= "oneshot" +CONFIG_EXAMPLES_ONESHOT_STACKSIZE ?= 2048 +CONFIG_EXAMPLES_ONESHOT_PRIORITY ?= 100 +CONFIG_EXAMPLES_ONESHOT_PROGNAME ?= "oneshot" + +APPNAME = $(CONFIG_EXAMPLES_ONESHOT_APPNAME) +PRIORITY = $(CONFIG_EXAMPLES_ONESHOT_PRIORITY) +STACKSIZE = $(CONFIG_EXAMPLES_ONESHOT_STACKSIZE) +PROGNAME = $(CONFIG_EXAMPLES_ONESHOT_PROGNAME) + +# Oneshot timer example + +ASRCS = +CSRCS = +MAINSRC = oneshot_main.c + +include $(APPDIR)/Application.mk diff --git a/examples/oneshot/oneshot_main.c b/examples/oneshot/oneshot_main.c new file mode 100644 index 000000000..ece6a6a52 --- /dev/null +++ b/examples/oneshot/oneshot_main.c @@ -0,0 +1,204 @@ +/**************************************************************************** + * examples/oneshot/oneshot_main.c + * + * Copyright (C) 2015 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include +#include +#include +#include + +#include + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ + +#ifndef CONFIG_EXAMPLE_ONESHOT_DEVNAME +# define CONFIG_EXAMPLE_ONESHOT_DEVNAME "/dev/oneshot" +#endif + +#ifndef CONFIG_EXAMPLE_ONESHOT_DELAY +# define CONFIG_EXAMPLE_ONESHOT_DELAY 100000 +#endif + +#ifndef CONFIG_EXAMPLE_ONESHOT_SIGNO +# define CONFIG_EXAMPLE_ONESHOT_SIGNO 13 +#endif + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: oneshot_main + ****************************************************************************/ + +static void show_usage(FAR const char *progname) +{ + fprintf(stderr, "USAGE: %s [-d ] []\n", progname); + fprintf(stderr, "Where:\n"); + fprintf(stderr, "\t-d :\n"); + fprintf(stderr, "\tSpecifies the oneshot delay in microseconds. Default %ld\n", + (unsigned long)CONFIG_EXAMPLE_ONESHOT_DELAY); + fprintf(stderr, "\t:\n"); + fprintf(stderr, "\tSpecifies the path to the oneshot driver. Default %s\n", + CONFIG_EXAMPLE_ONESHOT_DEVNAME); + exit(EXIT_FAILURE); +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: oneshot_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int oneshot_main(int argc, char *argv[]) +#endif +{ + FAR const char *devname = CONFIG_EXAMPLE_ONESHOT_DEVNAME; + unsigned long usecs = CONFIG_EXAMPLE_ONESHOT_DELAY; + unsigned long secs; + struct oneshot_start_s start; + struct siginfo info; + sigset_t set; + int ret; + int fd; + + /* USAGE: nsh> oneshot [-d ] [] */ + + if (argc > 1) + { + if (argc == 2) + { + /* FORM: nsh> oneshot [] */ + + devname = argv[1]; + } + else if (argc < 5) + { + /* FORM: nsh> oneshot [-d ] */ + + if (strcmp(argv[1], "-d") != 0) + { + fprintf(stderr, "ERROR: Unrecognized option: %s\n", argv[1]); + show_usage(argv[0]); + } + + usecs = strtoul(argv[2], NULL, 10); + + if (argc == 4) + { + /* FORM: nsh> oneshot [-d ] [] */ + + devname = argv[3]; + } + } + else + { + fprintf(stderr, "ERROR: Unsupported number of arguments: %d\n", argc); + show_usage(argv[0]); + } + } + + /* Open the oneshot device */ + + printf("Opening %s\n", devname); + + fd = open(devname, O_RDONLY); + if (fd < 0) + { + fprintf(stderr, "ERROR: Failed to open %s: %d\n", + devname, errno); + return EXIT_FAILURE; + } + + /* Start the oneshot timer */ + + sigemptyset(&set); + sigaddset(&set, CONFIG_EXAMPLE_ONESHOT_SIGNO); + + printf("Starting oneshot timer with delay %lu microseconds\n", usecs); + + start.pid = 0; + start.signo = CONFIG_EXAMPLE_ONESHOT_SIGNO; + start.arg = NULL; + + secs = usecs / 1000000; + usecs -= 1000000 * secs; + + start.ts.tv_sec = secs; + start.ts.tv_nsec = usecs * 1000; + + ret = ioctl(fd, OSIOC_START, (unsigned long)((uintptr_t)&start)); + if (ret < 0) + { + fprintf(stderr, "ERROR: Failed to start the oneshot interval: %d\n", + errno); + close(fd); + return EXIT_FAILURE; + } + + /* Wait for the oneshot to fire */ + + printf("Waiting...\n"); + ret = sigwaitinfo(&set, &info); + if (ret < 0) + { + fprintf(stderr, "ERROR: sigwaitinfo failed: %d\n", + errno); + close(fd); + return EXIT_FAILURE; + } + + /* Close the oneshot driver */ + + printf("Finished\n"); + close(fd); + return EXIT_SUCCESS; +} diff --git a/examples/timer/Kconfig b/examples/timer/Kconfig index 3df21dd0f..43d672268 100644 --- a/examples/timer/Kconfig +++ b/examples/timer/Kconfig @@ -8,7 +8,7 @@ config EXAMPLES_TIMER default n depends on TIMER && BUILD_FLAT ---help--- - Enable the \"Timer, World!\" example + Enable the timer example if EXAMPLES_TIMER diff --git a/examples/timer/Makefile b/examples/timer/Makefile index 02390b68d..fec6936f8 100644 --- a/examples/timer/Makefile +++ b/examples/timer/Makefile @@ -47,7 +47,7 @@ PRIORITY = $(CONFIG_EXAMPLES_TIMER_PRIORITY) STACKSIZE = $(CONFIG_EXAMPLES_TIMER_STACKSIZE) PROGNAME = $(CONFIG_EXAMPLES_TIMER_PROGNAME) -# Timer, World! Example +# Timer example ASRCS = CSRCS = diff --git a/examples/timer/timer_main.c b/examples/timer/timer_main.c index 5ec7aebff..ada283895 100644 --- a/examples/timer/timer_main.c +++ b/examples/timer/timer_main.c @@ -68,7 +68,7 @@ #endif /**************************************************************************** - * Private Data + * Private Functions ****************************************************************************/ /****************************************************************************