apps/examples: Add Oneshot timer example

This commit is contained in:
Gregory Nutt 2016-08-12 14:20:52 -06:00
parent add52b0503
commit d8c7a8fff8
9 changed files with 383 additions and 3 deletions

View File

@ -1441,6 +1441,11 @@ examples/null
This is the do nothing application. It is only used for bringing This is the do nothing application. It is only used for bringing
up new NuttX architectures in the most minimal of environments. up new NuttX architectures in the most minimal of environments.
examples/oneshot
^^^^^^^^^^^^^^^^
Simple test of a oneshot driver.
examples/ostest examples/ostest
^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^

11
examples/oneshot/.gitignore vendored Normal file
View File

@ -0,0 +1,11 @@
/Make.dep
/.depend
/.built
/*.asm
/*.obj
/*.rel
/*.lst
/*.sym
/*.adb
/*.lib
/*.src

65
examples/oneshot/Kconfig Normal file
View File

@ -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

View File

@ -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 <gnutt@nuttx.org>
#
# 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

56
examples/oneshot/Makefile Normal file
View File

@ -0,0 +1,56 @@
############################################################################
# apps/examples/oneshot/Makefile
#
# Copyright (C) 2016 Gregory Nutt. All rights reserved.
# Author: Gregory Nutt <gnutt@nuttx.org>
#
# 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

View File

@ -0,0 +1,204 @@
/****************************************************************************
* examples/oneshot/oneshot_main.c
*
* Copyright (C) 2015 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* 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 <nuttx/config.h>
#include <sys/ioctl.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <fcntl.h>
#include <time.h>
#include <errno.h>
#include <nuttx/timers/oneshot.h>
/****************************************************************************
* 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 <usecs>] [<devname>]\n", progname);
fprintf(stderr, "Where:\n");
fprintf(stderr, "\t-d <usecs>:\n");
fprintf(stderr, "\tSpecifies the oneshot delay in microseconds. Default %ld\n",
(unsigned long)CONFIG_EXAMPLE_ONESHOT_DELAY);
fprintf(stderr, "\t<devname>:\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 <usecs>] [<devname>] */
if (argc > 1)
{
if (argc == 2)
{
/* FORM: nsh> oneshot [<devname>] */
devname = argv[1];
}
else if (argc < 5)
{
/* FORM: nsh> oneshot [-d <usecs>] */
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 <usecs>] [<devname>] */
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;
}

View File

@ -8,7 +8,7 @@ config EXAMPLES_TIMER
default n default n
depends on TIMER && BUILD_FLAT depends on TIMER && BUILD_FLAT
---help--- ---help---
Enable the \"Timer, World!\" example Enable the timer example
if EXAMPLES_TIMER if EXAMPLES_TIMER

View File

@ -47,7 +47,7 @@ PRIORITY = $(CONFIG_EXAMPLES_TIMER_PRIORITY)
STACKSIZE = $(CONFIG_EXAMPLES_TIMER_STACKSIZE) STACKSIZE = $(CONFIG_EXAMPLES_TIMER_STACKSIZE)
PROGNAME = $(CONFIG_EXAMPLES_TIMER_PROGNAME) PROGNAME = $(CONFIG_EXAMPLES_TIMER_PROGNAME)
# Timer, World! Example # Timer example
ASRCS = ASRCS =
CSRCS = CSRCS =

View File

@ -68,7 +68,7 @@
#endif #endif
/**************************************************************************** /****************************************************************************
* Private Data * Private Functions
****************************************************************************/ ****************************************************************************/
/**************************************************************************** /****************************************************************************