2016-08-12 22:20:52 +02:00
|
|
|
/****************************************************************************
|
2021-03-23 14:20:04 +01:00
|
|
|
* apps/examples/oneshot/oneshot_main.c
|
2016-08-12 22:20:52 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02: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
|
2016-08-12 22:20:52 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02:00
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
2016-08-12 22:20:52 +02:00
|
|
|
*
|
2021-06-15 09:09:58 +02: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.
|
2016-08-12 22:20:52 +02:00
|
|
|
*
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* 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>
|
2023-02-04 14:56:06 +01:00
|
|
|
#include <unistd.h>
|
2016-08-12 22:20:52 +02:00
|
|
|
|
|
|
|
#include <nuttx/timers/oneshot.h>
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Pre-processor Definitions
|
|
|
|
****************************************************************************/
|
|
|
|
|
2017-08-24 21:37:36 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_ONESHOT_DEVNAME
|
|
|
|
# define CONFIG_EXAMPLES_ONESHOT_DEVNAME "/dev/oneshot"
|
2016-08-12 22:20:52 +02:00
|
|
|
#endif
|
|
|
|
|
2017-08-24 21:37:36 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_ONESHOT_DELAY
|
|
|
|
# define CONFIG_EXAMPLES_ONESHOT_DELAY 100000
|
2016-08-12 22:20:52 +02:00
|
|
|
#endif
|
|
|
|
|
2017-08-24 21:37:36 +02:00
|
|
|
#ifndef CONFIG_EXAMPLES_ONESHOT_SIGNO
|
2023-03-25 00:07:59 +01:00
|
|
|
# define CONFIG_EXAMPLES_ONESHOT_SIGNO 32
|
2016-08-12 22:20:52 +02:00
|
|
|
#endif
|
|
|
|
|
2016-08-15 20:54:05 +02:00
|
|
|
/* For long delays that have to be broken into segments, some loss of
|
2016-08-19 14:25:42 +02:00
|
|
|
* precision is expected due to interrupt and context switching overhead.
|
|
|
|
* This inaccuracy will cause somewhat longer times than the time requested
|
|
|
|
* due to this overhead. It might be possible to reduce this inaccuracy
|
|
|
|
* some by (1) making this task very high priority so that it runs in a more
|
|
|
|
* deterministic way, and (2) by subtracting a "fudge factor" to account for
|
|
|
|
* time lost due to the overhead.
|
2016-08-15 20:54:05 +02:00
|
|
|
*/
|
|
|
|
|
|
|
|
#define FUDGE_FACTOR 10
|
|
|
|
|
2016-08-12 22:20:52 +02:00
|
|
|
/****************************************************************************
|
|
|
|
* 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");
|
2020-11-18 06:26:11 +01:00
|
|
|
fprintf(stderr, "\tSpecifies the oneshot delay in microseconds."
|
|
|
|
" Default %ld\n", (unsigned long)CONFIG_EXAMPLES_ONESHOT_DELAY);
|
2016-08-12 22:20:52 +02:00
|
|
|
fprintf(stderr, "\t<devname>:\n");
|
2020-11-18 06:26:11 +01:00
|
|
|
fprintf(stderr, "\tSpecifies the path to the oneshot driver. Default %s\n",
|
2017-08-24 21:37:36 +02:00
|
|
|
CONFIG_EXAMPLES_ONESHOT_DEVNAME);
|
2016-08-12 22:20:52 +02:00
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Public Functions
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
/****************************************************************************
|
|
|
|
* Name: oneshot_main
|
|
|
|
****************************************************************************/
|
|
|
|
|
|
|
|
int main(int argc, FAR char *argv[])
|
|
|
|
{
|
2017-08-24 21:37:36 +02:00
|
|
|
FAR const char *devname = CONFIG_EXAMPLES_ONESHOT_DEVNAME;
|
|
|
|
unsigned long usecs = CONFIG_EXAMPLES_ONESHOT_DELAY;
|
2016-08-12 22:20:52 +02:00
|
|
|
unsigned long secs;
|
|
|
|
struct oneshot_start_s start;
|
2016-08-15 19:44:52 +02:00
|
|
|
struct timespec ts;
|
|
|
|
uint64_t maxus;
|
2016-08-12 22:20:52 +02:00
|
|
|
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>] */
|
2018-08-13 15:47:26 +02:00
|
|
|
|
2016-08-12 22:20:52 +02:00
|
|
|
devname = argv[3];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2020-11-18 06:26:11 +01:00
|
|
|
fprintf(stderr, "ERROR: Unsupported number of arguments: %d\n",
|
|
|
|
argc);
|
2016-08-12 22:20:52 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2016-08-15 19:44:52 +02:00
|
|
|
/* Get the maximum delay */
|
2016-08-12 22:20:52 +02:00
|
|
|
|
2016-08-15 19:44:52 +02:00
|
|
|
ret = ioctl(fd, OSIOC_MAXDELAY, (unsigned long)((uintptr_t)&ts));
|
2016-08-12 22:20:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
2016-08-15 19:44:52 +02:00
|
|
|
fprintf(stderr, "ERROR: Failed to get the maximum delayl: %d\n",
|
2016-08-12 22:20:52 +02:00
|
|
|
errno);
|
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
2021-03-23 14:20:04 +01:00
|
|
|
maxus = (uint64_t)ts.tv_sec * USEC_PER_SEC +
|
|
|
|
(uint64_t)ts.tv_nsec / NSEC_PER_USEC;
|
2016-08-12 22:20:52 +02:00
|
|
|
|
2021-03-23 14:20:04 +01:00
|
|
|
printf("Maximum delay is %" PRIu64 "\n", maxus);
|
2016-08-15 19:44:52 +02:00
|
|
|
|
2020-11-18 05:28:11 +01:00
|
|
|
/* Ignore the default signal action */
|
|
|
|
|
|
|
|
signal(CONFIG_EXAMPLES_ONESHOT_SIGNO, SIG_IGN);
|
|
|
|
|
2016-08-15 19:44:52 +02:00
|
|
|
/* Loop waiting until the full delay expires */
|
|
|
|
|
|
|
|
while (usecs > 0)
|
2016-08-12 22:20:52 +02:00
|
|
|
{
|
2016-08-15 19:44:52 +02:00
|
|
|
/* Start the oneshot timer */
|
|
|
|
|
|
|
|
sigemptyset(&set);
|
2017-08-24 21:37:36 +02:00
|
|
|
sigaddset(&set, CONFIG_EXAMPLES_ONESHOT_SIGNO);
|
2016-08-15 19:44:52 +02:00
|
|
|
|
|
|
|
if (usecs < maxus)
|
|
|
|
{
|
|
|
|
/* Wait for the remaining time */
|
|
|
|
|
|
|
|
printf("Starting oneshot timer with delay %lu microseconds\n",
|
|
|
|
usecs);
|
|
|
|
|
2021-03-23 14:20:04 +01:00
|
|
|
start.pid = 0;
|
|
|
|
secs = usecs / USEC_PER_SEC;
|
|
|
|
usecs -= USEC_PER_SEC * secs;
|
2016-08-15 19:44:52 +02:00
|
|
|
|
|
|
|
start.ts.tv_sec = secs;
|
2021-03-23 14:20:04 +01:00
|
|
|
start.ts.tv_nsec = usecs * NSEC_PER_USEC;
|
2018-08-13 15:47:26 +02:00
|
|
|
|
2019-01-27 16:48:29 +01:00
|
|
|
start.event.sigev_notify = SIGEV_SIGNAL;
|
|
|
|
start.event.sigev_signo = CONFIG_EXAMPLES_ONESHOT_SIGNO;
|
|
|
|
start.event.sigev_value.sival_ptr = NULL;
|
|
|
|
|
2016-08-15 19:44:52 +02:00
|
|
|
/* Zero usecs to terminate the loop */
|
|
|
|
|
|
|
|
usecs = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
/* Wait for the maximum */
|
|
|
|
|
|
|
|
printf("Starting oneshot timer with delay %llu microseconds\n",
|
|
|
|
maxus);
|
|
|
|
|
|
|
|
start.ts.tv_sec = ts.tv_sec;
|
|
|
|
start.ts.tv_nsec = ts.tv_nsec;
|
|
|
|
|
2021-03-23 14:20:04 +01:00
|
|
|
usecs -= maxus;
|
2016-08-15 20:54:05 +02:00
|
|
|
|
|
|
|
#if FUDGE_FACTOR > 0
|
|
|
|
if (usecs > FUDGE_FACTOR)
|
|
|
|
{
|
2021-03-23 14:20:04 +01:00
|
|
|
usecs -= FUDGE_FACTOR;
|
2016-08-15 20:54:05 +02:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2021-03-23 14:20:04 +01:00
|
|
|
usecs = 0;
|
2016-08-15 20:54:05 +02:00
|
|
|
}
|
|
|
|
#endif
|
2016-08-15 19:44:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ret = ioctl(fd, OSIOC_START, (unsigned long)((uintptr_t)&start));
|
|
|
|
if (ret < 0)
|
|
|
|
{
|
2020-11-18 06:26:11 +01:00
|
|
|
fprintf(stderr, "ERROR: Failed to start the oneshot: %d\n",
|
2016-08-15 19:44:52 +02:00
|
|
|
errno);
|
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Wait for the oneshot to fire */
|
|
|
|
|
|
|
|
printf("Waiting...\n");
|
2017-03-02 05:41:08 +01:00
|
|
|
ret = sigwaitinfo(&set, NULL);
|
2016-08-15 19:44:52 +02:00
|
|
|
if (ret < 0)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "ERROR: sigwaitinfo failed: %d\n",
|
|
|
|
errno);
|
|
|
|
close(fd);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2016-08-12 22:20:52 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Close the oneshot driver */
|
|
|
|
|
|
|
|
printf("Finished\n");
|
|
|
|
close(fd);
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|