examples: add DAC example
This commit is contained in:
parent
25237eafe0
commit
318350d289
@ -48,11 +48,11 @@ examples/adc
|
||||
CONFIG_EXAMPLES_ADC_GROUPSIZE - The number of samples to read at once.
|
||||
Default: 4
|
||||
|
||||
examples/adjoystick
|
||||
examples/ajoystick
|
||||
^^^^^^^^^^^^^^^^^^
|
||||
|
||||
This is a simple test of the analog joystick driver. See details about
|
||||
this driver in nuttx/include/nuttx/input/adjoystick.h.
|
||||
this driver in nuttx/include/nuttx/input/ajoystick.h.
|
||||
|
||||
Configuration Pre-requisites:
|
||||
|
||||
@ -262,6 +262,11 @@ examples/cxxtest
|
||||
|
||||
Additional uClibc++ settings may be required in your build environment.
|
||||
|
||||
examples/dac
|
||||
^^^^^^^^^^^^
|
||||
|
||||
This is a tool for writing values to DAC device.
|
||||
|
||||
examples/dhcpd
|
||||
^^^^^^^^^^^^^^
|
||||
|
||||
@ -315,7 +320,7 @@ examples/discover
|
||||
CONFIG_EXAMPLES_DISCOVER_DRIPADDR - Router IP address
|
||||
CONFIG_EXAMPLES_DISCOVER_NETMASK - Network Mask
|
||||
|
||||
examples/adjoystick
|
||||
examples/djoystick
|
||||
^^^^^^^^^^^^^^^^^^
|
||||
|
||||
This is a simple test of the discrete joystick driver. See details about
|
||||
|
@ -35,7 +35,7 @@
|
||||
|
||||
-include $(TOPDIR)/Make.defs
|
||||
|
||||
# NuttX NX Graphics Example.
|
||||
# ADC example
|
||||
|
||||
ASRCS =
|
||||
CSRCS =
|
||||
@ -44,7 +44,7 @@ MAINSRC = adc_main.c
|
||||
CONFIG_XYZ_PROGNAME ?= adc$(EXEEXT)
|
||||
PROGNAME = $(CONFIG_XYZ_PROGNAME)
|
||||
|
||||
# Touchscreen built-in application info
|
||||
# application info
|
||||
|
||||
APPNAME = adc
|
||||
PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||
|
11
examples/dac/.gitignore
vendored
Normal file
11
examples/dac/.gitignore
vendored
Normal file
@ -0,0 +1,11 @@
|
||||
/Make.dep
|
||||
/.depend
|
||||
/.built
|
||||
/*.asm
|
||||
/*.obj
|
||||
/*.rel
|
||||
/*.lst
|
||||
/*.sym
|
||||
/*.adb
|
||||
/*.lib
|
||||
/*.src
|
15
examples/dac/Kconfig
Normal file
15
examples/dac/Kconfig
Normal file
@ -0,0 +1,15 @@
|
||||
config EXAMPLES_DAC
|
||||
bool "DAC tool"
|
||||
default n
|
||||
depends on DAC
|
||||
---help---
|
||||
Enable support for the DAC tool.
|
||||
|
||||
if EXAMPLES_DAC
|
||||
|
||||
config EXAMPLES_DAC_DEVPATH
|
||||
string "DAC device path"
|
||||
default "/dev/dac0"
|
||||
---help---
|
||||
The default path to the DAC device. Default: /dev/dac0
|
||||
endif
|
3
examples/dac/Make.defs
Normal file
3
examples/dac/Make.defs
Normal file
@ -0,0 +1,3 @@
|
||||
ifeq ($(CONFIG_EXAMPLES_DAC),y)
|
||||
CONFIGURED_APPS += examples/dac
|
||||
endif
|
53
examples/dac/Makefile
Normal file
53
examples/dac/Makefile
Normal file
@ -0,0 +1,53 @@
|
||||
############################################################################
|
||||
# apps/examples/dac/Makefile
|
||||
#
|
||||
# Copyright (C) 2017 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
|
||||
|
||||
# DAC tool
|
||||
|
||||
ASRCS =
|
||||
CSRCS =
|
||||
MAINSRC = dac_main.c
|
||||
|
||||
CONFIG_XYZ_PROGNAME ?= dac$(EXEEXT)
|
||||
PROGNAME = $(CONFIG_XYZ_PROGNAME)
|
||||
|
||||
# application info
|
||||
|
||||
APPNAME = dac
|
||||
PRIORITY = SCHED_PRIORITY_DEFAULT
|
||||
STACKSIZE = 2048
|
||||
|
||||
include $(APPDIR)/Application.mk
|
443
examples/dac/dac_main.c
Normal file
443
examples/dac/dac_main.c
Normal file
@ -0,0 +1,443 @@
|
||||
/****************************************************************************
|
||||
* examples/dac/dac_main.c
|
||||
*
|
||||
* Copyright (C) 2017 Gregory Nutt. All rights reserved.
|
||||
* Author: Juha Niskanen <juha.niskanen@haltian.com>
|
||||
*
|
||||
* 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 <errno.h>
|
||||
#include <debug.h>
|
||||
#include <fcntl.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <sys/ioctl.h>
|
||||
#include <nuttx/analog/dac.h>
|
||||
#include <nuttx/arch.h>
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
#ifndef CONFIG_DAC
|
||||
# error "DAC device support is not enabled (CONFIG_DAC)"
|
||||
#endif
|
||||
|
||||
#ifndef CONFIG_EXAMPLES_DAC_DEVPATH
|
||||
# define CONFIG_EXAMPLES_DAC_DEVPATH "/dev/dac0"
|
||||
#endif
|
||||
|
||||
#ifndef ARRAY_SIZE
|
||||
# define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
|
||||
#endif
|
||||
|
||||
#define PROGNAME "dac"
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
struct command
|
||||
{
|
||||
const char *name;
|
||||
int (* const cmd)(int argc, const char *argv[]);
|
||||
const char *args;
|
||||
};
|
||||
|
||||
struct dac_state_s
|
||||
{
|
||||
char *devpath;
|
||||
int count;
|
||||
int delay;
|
||||
uint8_t channel;
|
||||
bool initialized;
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Function Prototypes
|
||||
****************************************************************************/
|
||||
|
||||
static int cmd_dac_put(int argc, const char *argv[]);
|
||||
static int cmd_dac_putv(int argc, const char *argv[]);
|
||||
|
||||
/****************************************************************************
|
||||
* Private Data
|
||||
****************************************************************************/
|
||||
|
||||
static const struct command commands[] =
|
||||
{
|
||||
{ "put", cmd_dac_put, "DATA [DELAY]" },
|
||||
{ "putv", cmd_dac_putv, "DATA [DATA...]" },
|
||||
};
|
||||
|
||||
static struct dac_state_s g_dacstate;
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
|
||||
static void dac_devpath(struct dac_state_s *dac,
|
||||
const char *devpath)
|
||||
{
|
||||
if (dac->devpath)
|
||||
{
|
||||
free(dac->devpath);
|
||||
}
|
||||
|
||||
dac->devpath = strdup(devpath);
|
||||
}
|
||||
|
||||
static void print_cmds(const char *header,
|
||||
const struct command *cmds,
|
||||
size_t ncmds,
|
||||
const char *trailer)
|
||||
{
|
||||
printf(header);
|
||||
while (ncmds--)
|
||||
{
|
||||
printf(" %s %s %c", cmds->name, cmds->args,
|
||||
(ncmds > 0) ? '\n' : ' ');
|
||||
cmds++;
|
||||
}
|
||||
printf(trailer);
|
||||
}
|
||||
|
||||
static const struct command *find_cmd(const char *name,
|
||||
const struct command *cmds,
|
||||
size_t ncmds)
|
||||
{
|
||||
while (ncmds--)
|
||||
{
|
||||
if (!strcmp(cmds->name, name))
|
||||
{
|
||||
return cmds;
|
||||
}
|
||||
cmds++;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
static int execute_cmd(int argc,
|
||||
const char *argv[],
|
||||
const struct command *cmds,
|
||||
size_t ncmds)
|
||||
{
|
||||
const struct command *cmd;
|
||||
|
||||
cmd = find_cmd(argv[0], cmds, ncmds);
|
||||
if (!cmd)
|
||||
{
|
||||
fprintf(stderr, "ERROR: unknown command: %s\n", argv[0]);
|
||||
print_cmds("", cmds, ncmds, "\n");
|
||||
return -EINVAL;
|
||||
}
|
||||
|
||||
return cmd->cmd(argc-1, argv+1);
|
||||
}
|
||||
|
||||
static int dac_put(const char *devpath,
|
||||
struct dac_msg_s *msg,
|
||||
size_t nmsgs,
|
||||
int delay)
|
||||
{
|
||||
size_t retries, i;
|
||||
int fd, ret;
|
||||
|
||||
fd = open(devpath, O_WRONLY | O_NONBLOCK);
|
||||
if (fd < 0)
|
||||
{
|
||||
fprintf(stderr, "ERROR: open() failed: %d\n", errno);
|
||||
return -ENODEV;
|
||||
}
|
||||
|
||||
i = retries = 0;
|
||||
while (i < nmsgs && retries < 3)
|
||||
{
|
||||
errno = 0;
|
||||
ret = write(fd, &msg[i], sizeof(*msg));
|
||||
_info("write: ret=%d, errno=%d\n", ret, errno);
|
||||
if (ret != sizeof(*msg))
|
||||
{
|
||||
if (errno == EAGAIN)
|
||||
{
|
||||
retries++;
|
||||
continue;
|
||||
}
|
||||
|
||||
_err("write failed: ret=%d, errno=%d\n", ret, errno);
|
||||
ret = -errno;
|
||||
break;
|
||||
}
|
||||
else
|
||||
{
|
||||
_info("wrote: chan=%d, data=%d\n", msg[i].am_channel, msg[i].am_data);
|
||||
i++;
|
||||
retries = 0;
|
||||
ret = OK;
|
||||
usleep(1000 * delay);
|
||||
}
|
||||
}
|
||||
|
||||
close(fd);
|
||||
return (i > 0) ? i : ret;
|
||||
}
|
||||
|
||||
static int cmd_dac_put(int argc, const char *argv[])
|
||||
{
|
||||
int i, data, delay;
|
||||
struct dac_msg_s msgs[1];
|
||||
int ret = OK;
|
||||
|
||||
/* This command allows overriding the "sticky" delay option. */
|
||||
|
||||
data = (argc > 0) ? atoi(argv[0]) : 100;
|
||||
delay = (argc > 1) ? atoi(argv[1]) : g_dacstate.delay;
|
||||
|
||||
_info("devpath=%s data=%d delay=%d\n",
|
||||
g_dacstate.devpath, data, delay);
|
||||
|
||||
for (i = 0; i < g_dacstate.count; i++)
|
||||
{
|
||||
msgs[0].am_channel = g_dacstate.channel;
|
||||
msgs[0].am_data = data;
|
||||
ret = dac_put(g_dacstate.devpath, msgs, ARRAY_SIZE(msgs), delay);
|
||||
_info("ret=%d\n", ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
static int cmd_dac_putv(int argc, const char *argv[])
|
||||
{
|
||||
int i, nmsgs;
|
||||
struct dac_msg_s msgs[CONFIG_NSH_MAXARGUMENTS];
|
||||
int ret = OK;
|
||||
|
||||
for (nmsgs = 0; nmsgs < CONFIG_NSH_MAXARGUMENTS; nmsgs++)
|
||||
{
|
||||
if (nmsgs >= argc)
|
||||
{
|
||||
break;
|
||||
}
|
||||
msgs[nmsgs].am_channel = g_dacstate.channel;
|
||||
msgs[nmsgs].am_data = atoi(argv[nmsgs]);
|
||||
}
|
||||
|
||||
for (i = 0; i < g_dacstate.count; i++)
|
||||
{
|
||||
ret = dac_put(g_dacstate.devpath, msgs, nmsgs, g_dacstate.delay);
|
||||
_info("ret=%d\n", ret);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifdef CONFIG_NSH_BUILTIN_APPS
|
||||
static void dac_help(void)
|
||||
{
|
||||
printf("Usage: dac [OPTIONS] command [CMD OPTIONS]\n");
|
||||
printf("\nGlobal arguments are \"sticky\". For example, once the DAC device is\n");
|
||||
printf("specified, that device will be re-used until it is changed.\n");
|
||||
printf("\n\"sticky\" OPTIONS include:\n");
|
||||
printf(" [-c channel] selects the DAC channel. "
|
||||
"Default: 0 Current: %d\n", g_dacstate.channel);
|
||||
printf(" [-d delay] pause for DELAY ms between writes. "
|
||||
"Default: 0 Current: %d\n", g_dacstate.delay);
|
||||
printf(" [-n count] repeats device write COUNT times. "
|
||||
"Default: 1 Current: %d\n", g_dacstate.count);
|
||||
printf(" [-p devpath] selects the DAC device. "
|
||||
"Default: %s Current: %s\n",
|
||||
CONFIG_EXAMPLES_DAC_DEVPATH, g_dacstate.devpath ? g_dacstate.devpath : "NONE");
|
||||
print_cmds("\nCommands:\n", commands, ARRAY_SIZE(commands), "\n");
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_NSH_BUILTIN_APPS
|
||||
static int arg_string(const char **arg, const char **value)
|
||||
{
|
||||
const char *ptr = *arg;
|
||||
|
||||
if (ptr[2] == '\0')
|
||||
{
|
||||
*value = arg[1];
|
||||
return 2;
|
||||
}
|
||||
else
|
||||
{
|
||||
*value = &ptr[2];
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_NSH_BUILTIN_APPS
|
||||
static int arg_decimal(const char **arg, long *value)
|
||||
{
|
||||
const char *string;
|
||||
int ret;
|
||||
|
||||
ret = arg_string(arg, &string);
|
||||
*value = strtol(string, NULL, 10);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
#ifdef CONFIG_NSH_BUILTIN_APPS
|
||||
static int parse_args(struct dac_state_s *dac,
|
||||
int argc,
|
||||
const char *argv[])
|
||||
{
|
||||
const char *ptr;
|
||||
const char *str;
|
||||
long value;
|
||||
int i, n, nargs;
|
||||
|
||||
for (i = n = 1; i < argc; )
|
||||
{
|
||||
ptr = argv[i];
|
||||
if (ptr[0] != '-')
|
||||
{
|
||||
i++;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch (ptr[1])
|
||||
{
|
||||
case 'c':
|
||||
nargs = arg_decimal(&argv[i], &value);
|
||||
if (value < 0 || value > 255)
|
||||
{
|
||||
printf("Channel must be in range [0..255]: %ld\n", value);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
dac->channel = (uint8_t)value;
|
||||
i += nargs;
|
||||
n += nargs;
|
||||
break;
|
||||
|
||||
case 'd':
|
||||
nargs = arg_decimal(&argv[i], &value);
|
||||
if (value < 0)
|
||||
{
|
||||
printf("Delay must be non-negative: %ld\n", value);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
dac->delay = (uint32_t)value;
|
||||
i += nargs;
|
||||
n += nargs;
|
||||
break;
|
||||
|
||||
case 'n':
|
||||
nargs = arg_decimal(&argv[i], &value);
|
||||
if (value < 0)
|
||||
{
|
||||
printf("Count must be non-negative: %ld\n", value);
|
||||
exit(1);
|
||||
}
|
||||
|
||||
dac->count = (uint32_t)value;
|
||||
i += nargs;
|
||||
n += nargs;
|
||||
break;
|
||||
|
||||
case 'p':
|
||||
nargs = arg_string(&argv[i], &str);
|
||||
dac_devpath(dac, str);
|
||||
i += nargs;
|
||||
n += nargs;
|
||||
break;
|
||||
|
||||
case '?':
|
||||
default:
|
||||
printf("Unsupported option: %s\n", ptr);
|
||||
dac_help();
|
||||
exit(1);
|
||||
}
|
||||
}
|
||||
return n;
|
||||
}
|
||||
#endif
|
||||
|
||||
/****************************************************************************
|
||||
* Public Functions
|
||||
****************************************************************************/
|
||||
|
||||
int dac_main(int argc, const char *argv[])
|
||||
{
|
||||
int ret;
|
||||
int nargs = 1;
|
||||
|
||||
if (!g_dacstate.initialized)
|
||||
{
|
||||
/* Initialization of the DAC hardware must be performed by board-specific
|
||||
* logic prior to running this test.
|
||||
*/
|
||||
|
||||
/* Set the default values */
|
||||
|
||||
dac_devpath(&g_dacstate, CONFIG_EXAMPLES_DAC_DEVPATH);
|
||||
g_dacstate.count = 1;
|
||||
g_dacstate.channel = 0; /* This seems to be ignored by driver. */
|
||||
g_dacstate.initialized = true;
|
||||
}
|
||||
|
||||
/* Parse the command line */
|
||||
|
||||
#ifdef CONFIG_NSH_BUILTIN_APPS
|
||||
nargs = parse_args(&g_dacstate, argc, argv);
|
||||
#endif
|
||||
|
||||
argc -= nargs;
|
||||
argv += nargs;
|
||||
|
||||
if (argc < 1)
|
||||
{
|
||||
dac_help();
|
||||
return EXIT_FAILURE;
|
||||
}
|
||||
else
|
||||
{
|
||||
ret = execute_cmd(argc, argv, commands, ARRAY_SIZE(commands));
|
||||
}
|
||||
|
||||
return (ret >= 0) ? EXIT_SUCCESS : EXIT_FAILURE;
|
||||
}
|
Loading…
Reference in New Issue
Block a user