2017-09-01 10:22:39 +02:00
|
|
|
/****************************************************************************
|
2021-06-16 09:22:16 +02:00
|
|
|
* apps/examples/dac/dac_main.c
|
2017-09-01 10:22:39 +02:00
|
|
|
*
|
2022-03-08 10:31:25 +01:00
|
|
|
* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
|
|
* contributor license agreements. See the NOTICE file distributed with
|
|
|
|
* this work for additional information regarding copyright ownership. The
|
|
|
|
* ASF licenses this file to you under the Apache License, Version 2.0 (the
|
|
|
|
* "License"); you may not use this file except in compliance with the
|
|
|
|
* License. You may obtain a copy of the License at
|
2017-09-01 10:22:39 +02:00
|
|
|
*
|
2022-03-08 10:31:25 +01:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2017-09-01 10:22:39 +02:00
|
|
|
*
|
2022-03-08 10:31:25 +01:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
|
|
|
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
|
|
|
* License for the specific language governing permissions and limitations
|
|
|
|
* under the License.
|
2017-09-01 10:22:39 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Included Files
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
#include <nuttx/config.h>
|
|
|
|
|
|
|
|
#include <errno.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
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Types
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
struct command
|
|
|
|
{
|
2019-07-29 01:52:28 +02:00
|
|
|
FAR const char *name;
|
|
|
|
CODE int (* const cmd)(int argc, const char *argv[]);
|
2017-09-01 10:22:39 +02:00
|
|
|
const char *args;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct dac_state_s
|
|
|
|
{
|
2019-07-29 01:52:28 +02:00
|
|
|
FAR char *devpath;
|
2017-09-01 10:22:39 +02:00
|
|
|
int count;
|
|
|
|
int delay;
|
|
|
|
uint8_t channel;
|
|
|
|
bool initialized;
|
|
|
|
};
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Private Function Prototypes
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static int cmd_dac_put(int argc, FAR const char *argv[]);
|
|
|
|
static int cmd_dac_putv(int argc, FAR const char *argv[]);
|
2017-09-01 10:22:39 +02:00
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static void dac_devpath(FAR struct dac_state_s *dac,
|
|
|
|
FAR const char *devpath)
|
2017-09-01 10:22:39 +02:00
|
|
|
{
|
|
|
|
if (dac->devpath)
|
|
|
|
{
|
|
|
|
free(dac->devpath);
|
|
|
|
}
|
|
|
|
|
|
|
|
dac->devpath = strdup(devpath);
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static void print_cmds(FAR const char *header,
|
|
|
|
FAR const struct command *cmds,
|
2017-09-01 10:22:39 +02:00
|
|
|
size_t ncmds,
|
2019-07-29 01:52:28 +02:00
|
|
|
FAR const char *trailer)
|
2017-09-01 10:22:39 +02:00
|
|
|
{
|
2022-11-19 06:17:16 +01:00
|
|
|
printf("%s", header);
|
2017-09-01 10:22:39 +02:00
|
|
|
while (ncmds--)
|
|
|
|
{
|
|
|
|
printf(" %s %s %c", cmds->name, cmds->args,
|
|
|
|
(ncmds > 0) ? '\n' : ' ');
|
|
|
|
cmds++;
|
|
|
|
}
|
2019-07-29 01:52:28 +02:00
|
|
|
|
2022-11-19 06:17:16 +01:00
|
|
|
printf("%s", trailer);
|
2017-09-01 10:22:39 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static const struct command *find_cmd(FAR const char *name,
|
|
|
|
FAR const struct command *cmds,
|
2017-09-01 10:22:39 +02:00
|
|
|
size_t ncmds)
|
|
|
|
{
|
|
|
|
while (ncmds--)
|
|
|
|
{
|
|
|
|
if (!strcmp(cmds->name, name))
|
|
|
|
{
|
|
|
|
return cmds;
|
|
|
|
}
|
2019-07-29 01:52:28 +02:00
|
|
|
|
|
|
|
cmds++;
|
2017-09-01 10:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int execute_cmd(int argc,
|
2019-07-29 01:52:28 +02:00
|
|
|
FAR const char *argv[],
|
|
|
|
FAR const struct command *cmds,
|
2017-09-01 10:22:39 +02:00
|
|
|
size_t ncmds)
|
|
|
|
{
|
2019-07-29 01:52:28 +02:00
|
|
|
FAR const struct command *cmd;
|
2017-09-01 10:22:39 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
return cmd->cmd(argc - 1, argv + 1);
|
2017-09-01 10:22:39 +02:00
|
|
|
}
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static int dac_put(FAR const char *devpath,
|
|
|
|
FAR struct dac_msg_s *msg,
|
2017-09-01 10:22:39 +02:00
|
|
|
size_t nmsgs,
|
|
|
|
int delay)
|
|
|
|
{
|
2019-07-29 01:52:28 +02:00
|
|
|
size_t retries;
|
|
|
|
size_t i;
|
|
|
|
int fd;
|
|
|
|
int ret = OK;
|
2017-09-01 10:22:39 +02:00
|
|
|
|
|
|
|
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));
|
2017-09-01 15:39:39 +02:00
|
|
|
printf("write: ret=%d, errno=%d\n", ret, errno);
|
2017-09-01 10:22:39 +02:00
|
|
|
if (ret != sizeof(*msg))
|
|
|
|
{
|
|
|
|
if (errno == EAGAIN)
|
|
|
|
{
|
|
|
|
retries++;
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2017-09-01 15:39:39 +02:00
|
|
|
fprintf(stderr, "write failed: ret=%d, errno=%d\n", ret, errno);
|
2017-09-01 10:22:39 +02:00
|
|
|
ret = -errno;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-13 09:37:27 +01:00
|
|
|
printf("wrote: chan=%d, data=%" PRId32 "\n", msg[i].am_channel,
|
2020-11-13 09:36:42 +01:00
|
|
|
msg[i].am_data);
|
2017-09-01 10:22:39 +02:00
|
|
|
i++;
|
|
|
|
retries = 0;
|
|
|
|
ret = OK;
|
|
|
|
usleep(1000 * delay);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
close(fd);
|
|
|
|
return (i > 0) ? i : ret;
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static int cmd_dac_put(int argc, FAR const char *argv[])
|
2017-09-01 10:22:39 +02:00
|
|
|
{
|
|
|
|
struct dac_msg_s msgs[1];
|
2019-07-29 01:52:28 +02:00
|
|
|
int data;
|
|
|
|
int delay;
|
|
|
|
int i;
|
2017-09-01 10:22:39 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
/* This command allows overriding the "sticky" delay option. */
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
data = (argc > 0) ? atoi(argv[0]) : 100;
|
2017-09-01 10:22:39 +02:00
|
|
|
delay = (argc > 1) ? atoi(argv[1]) : g_dacstate.delay;
|
|
|
|
|
2017-09-01 15:39:39 +02:00
|
|
|
printf("devpath=%s data=%d delay=%d\n",
|
2017-09-01 10:22:39 +02:00
|
|
|
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);
|
2017-09-01 15:39:39 +02:00
|
|
|
printf("ret=%d\n", ret);
|
2017-09-01 10:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static int cmd_dac_putv(int argc, FAR const char *argv[])
|
2017-09-01 10:22:39 +02:00
|
|
|
{
|
|
|
|
struct dac_msg_s msgs[CONFIG_NSH_MAXARGUMENTS];
|
2019-07-29 01:52:28 +02:00
|
|
|
int nmsgs;
|
|
|
|
int i;
|
2017-09-01 10:22:39 +02:00
|
|
|
int ret = OK;
|
|
|
|
|
|
|
|
for (nmsgs = 0; nmsgs < CONFIG_NSH_MAXARGUMENTS; nmsgs++)
|
|
|
|
{
|
|
|
|
if (nmsgs >= argc)
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
2019-07-29 01:52:28 +02:00
|
|
|
|
2017-09-01 10:22:39 +02:00
|
|
|
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);
|
2017-09-01 15:39:39 +02:00
|
|
|
printf("ret=%d\n", ret);
|
2017-09-01 10:22:39 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void dac_help(void)
|
|
|
|
{
|
|
|
|
printf("Usage: dac [OPTIONS] command [CMD OPTIONS]\n");
|
2020-11-13 09:36:42 +01:00
|
|
|
printf("\nGlobal arguments are \"sticky\". "
|
|
|
|
"For example, once the DAC device is\n");
|
2017-09-01 10:22:39 +02:00
|
|
|
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",
|
2020-11-13 09:36:42 +01:00
|
|
|
CONFIG_EXAMPLES_DAC_DEVPATH,
|
|
|
|
g_dacstate.devpath ? g_dacstate.devpath : "NONE");
|
2017-09-01 10:22:39 +02:00
|
|
|
print_cmds("\nCommands:\n", commands, ARRAY_SIZE(commands), "\n");
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static int arg_string(FAR const char **arg, FAR const char **value)
|
2017-09-01 10:22:39 +02:00
|
|
|
{
|
2019-07-29 01:52:28 +02:00
|
|
|
FAR const char *ptr = *arg;
|
2017-09-01 10:22:39 +02:00
|
|
|
|
|
|
|
if (ptr[2] == '\0')
|
|
|
|
{
|
|
|
|
*value = arg[1];
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
*value = &ptr[2];
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static int arg_decimal(FAR const char **arg, FAR long *value)
|
2017-09-01 10:22:39 +02:00
|
|
|
{
|
2019-07-29 01:52:28 +02:00
|
|
|
FAR const char *string;
|
2017-09-01 10:22:39 +02:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
ret = arg_string(arg, &string);
|
|
|
|
*value = strtol(string, NULL, 10);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2019-07-29 01:52:28 +02:00
|
|
|
static int parse_args(FAR struct dac_state_s *dac,
|
2017-09-01 10:22:39 +02:00
|
|
|
int argc,
|
|
|
|
const char *argv[])
|
|
|
|
{
|
2019-07-29 01:52:28 +02:00
|
|
|
FAR const char *ptr;
|
|
|
|
FAR const char *str;
|
2017-09-01 10:22:39 +02:00
|
|
|
long value;
|
2019-07-29 01:52:28 +02:00
|
|
|
int nargs;
|
|
|
|
int n;
|
|
|
|
int i;
|
2017-09-01 10:22:39 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
}
|
2019-07-29 01:52:28 +02:00
|
|
|
|
2017-09-01 10:22:39 +02:00
|
|
|
return n;
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2019-10-06 14:14:39 +02:00
|
|
|
int main(int argc, FAR const char *argv[])
|
2017-09-01 10:22:39 +02:00
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
int nargs = 1;
|
|
|
|
|
|
|
|
if (!g_dacstate.initialized)
|
|
|
|
{
|
2020-11-13 09:36:42 +01:00
|
|
|
/* Initialization of the DAC hardware must be performed by
|
|
|
|
* board-specific logic prior to running this test.
|
2017-09-01 10:22:39 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
/* Set the default values */
|
|
|
|
|
|
|
|
dac_devpath(&g_dacstate, CONFIG_EXAMPLES_DAC_DEVPATH);
|
2019-07-29 01:52:28 +02:00
|
|
|
g_dacstate.count = 1;
|
|
|
|
g_dacstate.channel = 0; /* This seems to be ignored by driver. */
|
2017-09-01 10:22:39 +02:00
|
|
|
g_dacstate.initialized = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Parse the command line */
|
|
|
|
|
|
|
|
nargs = parse_args(&g_dacstate, argc, argv);
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|