drivers/timer: Add an upper-half, oneshot timer character driver.

This commit is contained in:
Gregory Nutt
2016-08-12 10:40:07 -06:00
parent 8dd17ad5d7
commit 89135c55e4
8 changed files with 500 additions and 24 deletions
-1
View File
@@ -1367,7 +1367,6 @@ endif # DEBUG_SPI
config DEBUG_TIMER
bool "Timer Debug Features"
default n
depends on TIMER
---help---
Enable timer debug features.
+1 -1
View File
@@ -69,7 +69,7 @@ struct stm32_oneshot_lowerhalf_s
/* Private lower half data follows */
struct stm32_oneshot_s oneshot; /* STM32-specific oneshot state */
oneshot_callback_t callback; /* internal handler that receives callback */
oneshot_callback_t callback; /* Internal handler that receives callback */
FAR void *arg; /* Argument that is passed to the handler */
};
+14 -16
View File
@@ -33,10 +33,6 @@
*
****************************************************************************/
/****************************************************************************
* Compilation Switches
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
@@ -64,14 +60,13 @@
#ifdef CONFIG_ZEROCROSS
#ifdef CONFIG_DISABLE_SIGNALS
#error "This driver needs SIGNAL support, remove CONFIG_DISABLE_SIGNALS"
#endif
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Debug ********************************************************************/
#ifdef CONFIG_DISABLE_SIGNALS
# error "This driver needs SIGNAL support, remove CONFIG_DISABLE_SIGNALS"
#endif
/****************************************************************************
* Private Type Definitions
@@ -132,15 +127,18 @@ static void zerocross_interrupt(FAR const struct zc_lowerhalf_s *lower,
static const struct file_operations g_zcops =
{
zc_open, /* open */
zc_close, /* close */
zc_read, /* read */
zc_write, /* write */
zc_open, /* open */
zc_close, /* close */
zc_read, /* read */
zc_write, /* write */
0, /* seek */
zc_ioctl /* ioctl */
zc_ioctl /* ioctl */
#ifndef CONFIG_DISABLE_POLL
, 0 /* poll */
#endif
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, 0 /* unlink */
#endif
};
volatile int sample = 0;
@@ -467,7 +465,7 @@ static int zc_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
default:
{
sninfo("Forwarding unrecognized cmd: %d arg: %ld\n", cmd, arg);
snerr("ERROR: Unrecognized cmd: %d arg: %ld\n", cmd, arg);
ret = -ENOTTY;
}
break;
@@ -485,7 +483,7 @@ static int zc_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
* Name: zc_register
*
* Description:
* Register the Zero Cross lower half device as 'devpath'
* Register the Zero Cross character device as 'devpath'
*
* Input Parameters:
* devpath - The full path to the driver to register. E.g., "/dev/zc0"
+9 -1
View File
@@ -5,7 +5,7 @@
menu "Timer Driver Support"
menuconfig TIMER
config TIMER
bool "Timer Support"
default n
---help---
@@ -13,6 +13,14 @@ menuconfig TIMER
driver. See include/nuttx/timers/timer.h for further timer driver
information.
config ONESHOT
bool "Oneshot timer driver"
default n
---help---
This selection enables building of the "upper-half" oneshot timer
driver. See include/nuttx/timers/oneshot.h for further oneshot timer
driver information.
menuconfig RTC
bool "RTC Driver Support"
default n
+6
View File
@@ -51,6 +51,12 @@ ifeq ($(CONFIG_TIMER),y)
TMRVPATH = :timers
endif
ifeq ($(CONFIG_ONESHOT),y)
CSRCS += oneshot.c
TMRDEPPATH = --dep-path timers
TMRVPATH = :timers
endif
ifeq ($(CONFIG_RTC_DSXXXX),y)
CSRCS += ds3231.c
TMRDEPPATH = --dep-path timers
+393
View File
@@ -0,0 +1,393 @@
/****************************************************************************
* drivers/timers/oneshot.c
*
* 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.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <sys/types.h>
#include <stdint.h>
#include <stdbool.h>
#include <fcntl.h>
#include <semaphore.h>
#include <signal.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/kmalloc.h>
#include <nuttx/fs/fs.h>
#include <nuttx/timers/oneshot.h>
#ifdef CONFIG_ONESHOT
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifdef CONFIG_DISABLE_SIGNALS
# error "This driver needs SIGNAL support, remove CONFIG_DISABLE_SIGNALS"
#endif
/****************************************************************************
* Private Type Definitions
****************************************************************************/
/* This structure describes the state of the upper half driver */
struct oneshot_dev_s
{
FAR struct oneshot_lowerhalf_s *od_lower; /* Lower-half driver state */
sem_t od_exclsem; /* Supports mutual exclusion */
/* Oneshot timer expiration notification information */
uint8_t od_signo; /* Signal number for notification */
pid_t od_pid; /* PID to be notified */
FAR void *od_arg; /* Signal value argument */
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int oneshot_open(FAR struct file *filep);
static int oneshot_close(FAR struct file *filep);
static ssize_t oneshot_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t oneshot_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int oneshot_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
FAR void *arg);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations g_oneshot_ops =
{
oneshot_open, /* open */
oneshot_close, /* close */
oneshot_read, /* read */
oneshot_write, /* write */
0, /* seek */
oneshot_ioctl /* ioctl */
#ifndef CONFIG_DISABLE_POLL
, 0 /* poll */
#endif
#ifndef CONFIG_DISABLE_PSEUDOFS_OPERATIONS
, 0 /* unlink */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: oneshot_callback
****************************************************************************/
static void oneshot_callback(FAR struct oneshot_lowerhalf_s *lower,
FAR void *arg)
{
FAR struct oneshot_dev_s *priv = (FAR struct oneshot_dev_s *)arg;
#ifdef CONFIG_CAN_PASS_STRUCTS
union sigval value;
#endif
DEBUGASSERT(priv != NULL);
/* Signal the waiter.. if there is one */
#ifdef CONFIG_CAN_PASS_STRUCTS
value.sival_ptr = priv->od_arg;
(void)sigqueue(priv->od_pid, priv->od_signo, value);
#else
(void)sigqueue(priv->od_pid, priv->od_signo, priv->od_arg);
#endif
}
/************************************************************************************
* Name: oneshot_open
*
* Description:
* This function is called whenever the PWM device is opened.
*
************************************************************************************/
static int oneshot_open(FAR struct file *filep)
{
tmrinfo("Opening...\n");
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
return OK;
}
/************************************************************************************
* Name: oneshot_close
*
* Description:
* This function is called when the PWM device is closed.
*
************************************************************************************/
static int oneshot_close(FAR struct file *filep)
{
tmrinfo("Closing...\n");
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
return OK;
}
/************************************************************************************
* Name: oneshot_read
*
* Description:O
* A dummy read method. This is provided only to satsify the VFS layer.
*
************************************************************************************/
static ssize_t oneshot_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
{
/* Return zero -- usually meaning end-of-file */
tmrinfo("buflen=%ld\n", (unsigned long)buflen);
return 0;
}
/************************************************************************************
* Name: oneshot_write
*
* Description:
* A dummy write method. This is provided only to satsify the VFS layer.
*
************************************************************************************/
static ssize_t oneshot_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen)
{
/* Return a failure */
tmrinfo("buflen=%ld\n", (unsigned long)buflen);
return -EPERM;
}
/************************************************************************************
* Name: oneshot_ioctl
*
* Description:
* The standard ioctl method. This is where ALL of the PWM work is done.
*
************************************************************************************/
static int oneshot_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode;
FAR struct oneshot_dev_s *priv;
int ret;
tmrinfo("cmd=%d arg=%08lx\n", cmd, (unsigned long)arg);
DEBUGASSERT(filep != NULL && filep->f_inode != NULL);
inode = filep->f_inode;
priv = (FAR struct oneshot_dev_s *)inode->i_private;
DEBUGASSERT(priv != NULL);
/* Get exclusive access to the device structures */
ret = sem_wait(&priv->od_exclsem);
if (ret < 0)
{
return ret;
}
/* Handle built-in ioctl commands */
ret = -EINVAL;
switch (cmd)
{
/* OSIOC_MAXDELAY - Return the maximum delay that can be supported
* by this timer.
* Argument: A referenct to a struct timespec in
* which the maximum time will be returned.
*/
case OSIOC_MAXDELAY:
{
FAR struct timespec *ts;
uint64_t usecs;
ts = (FAR struct timespec *)((uintptr_t)arg);
DEBUGASSERT(ts != NULL);
ret = ONESHOT_MAX_DELAY(priv->od_lower, &usecs);
if (ret >= 0)
{
uint64_t sec = usecs / 1000000;
usecs -= 1000000 * sec;
ts->tv_sec = (time_t)sec;
ts->tv_nsec = (long)(usecs * 1000);
}
}
break;
/* OSIOC_START - Start the oneshot timer
* Argument: A reference to struct oneshot_start_s
*/
case OSIOC_START:
{
FAR struct oneshot_start_s *start;
pid_t pid;
start = (FAR struct oneshot_start_s *)((uintptr_t)arg);
DEBUGASSERT(start != NULL);
/* Save signalling information */
priv->od_signo = start->signo;
priv->od_arg = start->arg;
pid = start->pid;
if (pid == 0)
{
pid = getpid();
}
priv->od_pid = pid;
/* Start the oneshot timer */
ret = ONESHOT_START(priv->od_lower, oneshot_callback, start->arg,
&start->ts);
}
break;
/* OSIOC_CANCEL - Stop the timer
* Argument: A reference to a struct timespec in
* which the time remaining will be returned.
*/
case OSIOC_CANCEL:
{
FAR struct timespec *ts = (FAR struct timespec *)((uintptr_t)arg);
/* Cancel the oneshot timer */
ret = ONESHOT_CANCEL(priv->od_lower, ts);
}
break;
default:
{
tmrerr("ERROR: Unrecognized cmd: %d arg: %ld\n", cmd, arg);
ret = -ENOTTY;
}
break;
}
sem_post(&priv->od_exclsem);
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: oneshot_register
*
* Description:
* Register the oneshot device as 'devpath'
*
* Input Parameters:
* devpath - The full path to the driver to register. E.g., "/dev/oneshot0"
* lower - An instance of the lower half interface
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure. The following
* possible error values may be returned (most are returned by
* register_driver()):
*
* EINVAL - 'path' is invalid for this operation
* EEXIST - An inode already exists at 'path'
* ENOMEM - Failed to allocate in-memory resources for the operation
*
****************************************************************************/
int oneshot_register(FAR const char *devname,
FAR struct oneshot_lowerhalf_s *lower)
{
FAR struct oneshot_dev_s *priv;
int ret;
sninfo("devname=%s lower=%p\n", devname, lower);
DEBUGASSERT(devname != NULL && lower != NULL);
/* Allocate a new oneshot timer driver instance */
priv = (FAR struct oneshot_dev_s *)
kmm_zalloc(sizeof(struct oneshot_dev_s));
if (!priv)
{
snerr("ERROR: Failed to allocate device structure\n");
return -ENOMEM;
}
/* Initialize the new oneshot timer driver instance */
priv->od_lower = lower;
sem_init(&priv->od_exclsem, 0, 1);
/* And register the oneshot timer driver */
ret = register_driver(devname, &g_oneshot_ops, 0666, priv);
if (ret < 0)
{
snerr("ERROR: register_driver failed: %d\n", ret);
sem_destroy(&priv->od_exclsem);
kmm_free(priv);
}
return ret;
}
#endif /* CONFIG_ONESHOT */
+67
View File
@@ -45,9 +45,38 @@
#include <stdint.h>
#include <time.h>
#include <nuttx/fs/ioctl.h>
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* IOCTL commands ***********************************************************/
/* These commands are used by applications to access the oneshot lower-half
* logic via the oneshot character driver IOCTL command. Since the oneshot
* driver is a device control interface and not a data transfer interface,
* the majority of the functionality is implemented in driver IOCTL calls.
* The oneshot IOCTL commands are listed below:
*
* These are detected and handled by the "upper half" timer driver.
*
* OSIOC_MAXDELAY - Return the maximum delay that can be supported by
* this timer.
* Argument: A referenct to a struct timespec in which
* the maximum time will be returned.
* OSIOC_START - Start the oneshot timer
* Argument: A reference to struct oneshot_start_s
* OSIOC_CANCEL - Stop the timer
* Argument: A reference to a struct timespec in which
* the time remaining will be returned.
*
* NOTE: _TCIOC(0x0020) througn _TCIOC(0x003f) are reserved for use by the
* oneshot driver to assure that the values are unique. Other timer drivers
* must not use IOCTL commands in this numeric range.
*/
#define OSIOC_MAXDELAY _TCIOC(0x0020)
#define OSIOC_START _TCIOC(0x0021)
#define OSIOC_CANCEL _TCIOC(0x0022)
/* Method access helper macros **********************************************/
@@ -162,6 +191,18 @@ struct oneshot_lowerhalf_s
/* Private lower half data may follow */
};
#ifdef CONFIG_ONESHOT
/* Argument to OSIOC_START IOCTL command */
struct oneshot_start_s
{
pid_t pid; /* PID of task to be signalled (0 means calling task) */
int signo; /* Signal number to use */
FAR void *arg; /* Signal value argument */
struct timespec ts; /* Delay until time expiration */
};
#endif
/****************************************************************************
* Public Data
****************************************************************************/
@@ -201,6 +242,32 @@ extern "C"
FAR struct oneshot_lowerhalf_s *oneshot_initialize(int chan,
uint16_t resolution);
/****************************************************************************
* Name: oneshot_register
*
* Description:
* Register the oneshot device as 'devpath'
*
* Input Parameters:
* devpath - The full path to the driver to register. E.g., "/dev/oneshot0"
* lower - An instance of the lower half interface
*
* Returned Value:
* Zero (OK) on success; a negated errno value on failure. The following
* possible error values may be returned (most are returned by
* register_driver()):
*
* EINVAL - 'path' is invalid for this operation
* EEXIST - An inode already exists at 'path'
* ENOMEM - Failed to allocate in-memory resources for the operation
*
****************************************************************************/
#ifdef CONFIG_ONESHOT
int oneshot_register(FAR const char *devname,
FAR struct oneshot_lowerhalf_s *lower);
#endif
#undef EXTERN
#ifdef __cplusplus
}
+10 -5
View File
@@ -77,13 +77,18 @@
* NOTE: The TCIOC_SETHANDLER ioctl cannot be supported in the kernel build
* mode. In that case direct callbacks from kernel space into user space is
* forbidden.
*
* NOTE: _TCIOC(0x0001) througn _TCIOC(0x001f) are reserved for use by the
* timer driver to assure that the values are unique. Other timer drivers,
* such as the oneshot timer, must not use IOCTL commands in this numeric
* range.
*/
#define TCIOC_START _TCIOC(0x001)
#define TCIOC_STOP _TCIOC(0x002)
#define TCIOC_GETSTATUS _TCIOC(0x003)
#define TCIOC_SETTIMEOUT _TCIOC(0x004)
#define TCIOC_SETHANDLER _TCIOC(0x005)
#define TCIOC_START _TCIOC(0x0001)
#define TCIOC_STOP _TCIOC(0x0002)
#define TCIOC_GETSTATUS _TCIOC(0x0003)
#define TCIOC_SETTIMEOUT _TCIOC(0x0004)
#define TCIOC_SETHANDLER _TCIOC(0x0005)
/* Bit Settings *************************************************************/
/* Bit settings for the struct timer_status_s flags field */