diff --git a/ChangeLog.txt b/ChangeLog.txt index 13b918279..386ed441f 100644 --- a/ChangeLog.txt +++ b/ChangeLog.txt @@ -1441,4 +1441,5 @@ essentially the same as examples/smart, but has all of the SmartFS specific logic ripped out. This was created for testing the new tmpfs (2015-10-10). - + * apps/examples/zerocross: Add a Zero Cross application example. + From Alan Carvalho de Assis (2015-10-13). diff --git a/examples/Kconfig b/examples/Kconfig index f1e393de5..04a2a39bc 100644 --- a/examples/Kconfig +++ b/examples/Kconfig @@ -89,5 +89,6 @@ source "$APPSDIR/examples/watchdog/Kconfig" source "$APPSDIR/examples/wget/Kconfig" source "$APPSDIR/examples/wgetjson/Kconfig" source "$APPSDIR/examples/xmlrpc/Kconfig" +source "$APPSDIR/examples/zerocross/Kconfig" endmenu # Examples diff --git a/examples/README.txt b/examples/README.txt index e139821ad..562000fc0 100644 --- a/examples/README.txt +++ b/examples/README.txt @@ -2250,3 +2250,8 @@ examples/xmlrpc Default 0x0a000001. Ignored if CONFIG_NSH_BUILTIN_APPS is selected. CONFIG_EXAMPLES_XMLRPC_NETMASK - Network Mask. Default 0xffffff00 Ignored if CONFIG_NSH_BUILTIN_APPS is selected. + +examples/zerocross +^^^^^^^^^^^^^^^^^^ + + A simple test of the Zero Crossing device driver. diff --git a/examples/zerocross/.gitignore b/examples/zerocross/.gitignore new file mode 100644 index 000000000..105259c49 --- /dev/null +++ b/examples/zerocross/.gitignore @@ -0,0 +1,11 @@ +/Make.dep +/.context +/.depend +/.built +/*.asm +/*.rel +/*.lst +/*.sym +/*.adb +/*.lib +/*.src diff --git a/examples/zerocross/Kconfig b/examples/zerocross/Kconfig new file mode 100644 index 000000000..541da9fa8 --- /dev/null +++ b/examples/zerocross/Kconfig @@ -0,0 +1,23 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config EXAMPLES_ZEROCROSS + bool "Zero Cross Detection example" + default n + depends on ZEROCROSS && !DISABLE_SIGNALS + ---help--- + Enable the zero cross detection example + +if EXAMPLES_ZEROCROSS + +config EXAMPLES_ZEROCROSS_DEVNAME + string "Zero Cross device name" + default "/dev/zc0" + +config EXAMPLES_ZEROCROSS_SIGNO + int "Zero Cross signal" + default 13 + +endif diff --git a/examples/zerocross/Make.defs b/examples/zerocross/Make.defs new file mode 100644 index 000000000..e3f15f592 --- /dev/null +++ b/examples/zerocross/Make.defs @@ -0,0 +1,39 @@ +############################################################################ +# apps/examples/zerocross/Make.defs +# Adds selected applications to apps/ build +# +# 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. +# +############################################################################ + +ifeq ($(CONFIG_EXAMPLES_ZEROCROSS),y) +CONFIGURED_APPS += examples/zerocross +endif diff --git a/examples/zerocross/Makefile b/examples/zerocross/Makefile new file mode 100644 index 000000000..6d79faa05 --- /dev/null +++ b/examples/zerocross/Makefile @@ -0,0 +1,53 @@ +############################################################################ +# apps/examples/zerocross/Makefile +# +# 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. +# +############################################################################ + +-include $(TOPDIR)/Make.defs + +# Zero Cross Example + +ASRCS = +CSRCS = +MAINSRC = zerocross_main.c + +CONFIG_ZEROCROSS_PROGNAME ?= zerocross$(EXEEXT) +PROGNAME = $(CONFIG_ZEROCROSS_PROGNAME) + +# Buttons built-in application info + +APPNAME = zerocross +PRIORITY = SCHED_PRIORITY_DEFAULT +STACKSIZE = 2048 + +include $(APPDIR)/Application.mk diff --git a/examples/zerocross/zerocross_main.c b/examples/zerocross/zerocross_main.c new file mode 100644 index 000000000..89d66aee6 --- /dev/null +++ b/examples/zerocross/zerocross_main.c @@ -0,0 +1,148 @@ +/**************************************************************************** + * examplex/zerocross/zerocross_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 + +/**************************************************************************** + * Pre-processor Definitions + ****************************************************************************/ +/* Configuration ************************************************************/ + +#ifndef CONFIG_ZEROCROSS +# error "CONFIG_ZEROCROSS is not defined in the configuration" +#endif + +#ifndef CONFIG_EXAMPLES_ZEROCROSS_DEVNAME +# define CONFIG_EXAMPLES_ZEROCROSS_DEVNAME "/dev/zc0" +#endif + +#ifndef CONFIG_EXAMPLES_ZEROCROSS_SIGNO +# define CONFIG_EXAMPLES_ZEROCROSS_SIGNO 13 +#endif + +/* Helpers ******************************************************************/ + +#ifndef MIN +# define MIN(a,b) (a < b ? a : b) +#endif +#ifndef MAX +# define MAX(a,b) (a > b ? a : b) +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * zerocross_main + ****************************************************************************/ + +#ifdef CONFIG_BUILD_KERNEL +int main(int argc, FAR char *argv[]) +#else +int zerocross_main(int argc, char *argv[]) +#endif +{ + struct zc_notify_s notify; + int fd; + int tmp; + int ret; + int err = EXIT_FAILURE; + + /* Open the zerocross device */ + + fd = open(CONFIG_EXAMPLES_ZEROCROSS_DEVNAME, O_RDONLY); + if (fd < 0) + { + fprintf(stderr, "ERROR: Failed to open %s: %d\n", + CONFIG_EXAMPLES_ZEROCROSS_DEVNAME, errno); + return EXIT_FAILURE; + } + + /* Register to receive a signal on every zero cross event */ + + notify.zc_signo = CONFIG_EXAMPLES_ZEROCROSS_SIGNO; + + ret = ioctl(fd, ZCIOC_REGISTER, (unsigned long)((uintptr_t)¬ify)); + if (ret < 0) + { + fprintf(stderr, "ERROR: ioctl(ZCIOC_REGISTER) failed: %d\n", errno); + goto errout_with_fd; + } + + /* Then loop, receiving signals indicating zero cross events. */ + + for (; ; ) + { + struct siginfo value; + sigset_t set; + ssize_t nread; + + /* Wait for a signal */ + + (void)sigemptyset(&set); + (void)sigaddset(&set, CONFIG_EXAMPLES_ZEROCROSS_SIGNO); + ret = sigwaitinfo(&set, &value); + if (ret < 0) + { + fprintf(stderr, "ERROR: sigwaitinfo() failed: %d\n", errno); + goto errout_with_fd; + } + + /* Show the value accompanying the signal */ + + printf("Signal received!\n"); + printf("Sample = %d\n", value.si_value.sival_int); + } + + err = EXIT_SUCCESS; + +errout_with_fd: + close(fd); + return err; +}