examples/serialrx added by Bob Doison

This commit is contained in:
Gregory Nutt 2014-04-22 08:40:48 -06:00
parent e5e044faa1
commit df33d9bcc8
4 changed files with 1549 additions and 0 deletions

616
arch/arm/src/sam34/sam_tc.c Normal file
View File

@ -0,0 +1,616 @@
/****************************************************************************
* arch/arm/src/sam34/sam_tc.c
*
* Copyright (C) 2014 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 <nuttx/arch.h>
#include <sys/types.h>
#include <stdint.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/timer.h>
#include <arch/board/board.h>
#include "up_arch.h"
#include "sam_tc.h"
#include "sam_periphclks.h"
//#define CONFIG_SAM34_TC_REGDEBUG
#if defined(CONFIG_TIMER) && (defined(CONFIG_SAM34_TC0) || defined(CONFIG_SAM34_TC1) || defined(CONFIG_SAM34_TC2) || defined(CONFIG_SAM34_TC3) || defined(CONFIG_SAM34_TC4) || defined(CONFIG_SAM34_TC5))
/****************************************************************************
* Pre-Processor Definitions
****************************************************************************/
/* Clocking *****************************************************************/
#warning "TODO: allow selection of any of the input clocks"
#define TC_FCLK (BOARD_SLCK_FREQUENCY)
#define TC_MAXTIMEOUT ((1000000ULL * (1ULL + TC_RVALUE_MASK)) / TC_FCLK)
/* Configuration ************************************************************/
/* Debug ********************************************************************/
/* Non-standard debug that may be enabled just for testing the timer
* driver. NOTE: that only lldbg types are used so that the output is
* immediately available.
*/
#ifdef CONFIG_DEBUG_TIMER
# define tcdbg lldbg
# define tcvdbg llvdbg
#else
# define tcdbg(x...)
# define tcvdbg(x...)
#endif
/****************************************************************************
* Private Types
****************************************************************************/
/* This structure provides the private representation of the "lower-half"
* driver state structure. This structure must be cast-compatible with the
* timer_lowerhalf_s structure.
*/
struct sam34_lowerhalf_s
{
FAR const struct timer_ops_s *ops; /* Lower half operations */
xcpt_t handler; /* Current user interrupt handler */
uint32_t timeout; /* The actual timeout value (us) */
bool started; /* The timer has been started */
uint16_t reload; /* The 12-bit reload field reset value (WDV) */
uint16_t debug;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
/* Register operations ******************************************************/
#if defined(CONFIG_SAM34_TC_REGDEBUG) && defined(CONFIG_DEBUG)
static uint32_t sam34_getreg(uint32_t addr);
static void sam34_putreg(uint32_t val, uint32_t addr);
#else
# define sam34_getreg(addr) getreg32(addr)
# define sam34_putreg(val,addr) putreg32(val,addr)
#endif
/* Interrupt hanlding *******************************************************/
static int sam34_interrupt(int irq, FAR void *context);
/* "Lower half" driver methods **********************************************/
static int sam34_start(FAR struct timer_lowerhalf_s *lower);
static int sam34_stop(FAR struct timer_lowerhalf_s *lower);
static int sam34_getstatus(FAR struct timer_lowerhalf_s *lower,
FAR struct timer_status_s *status);
static int sam34_settimeout(FAR struct timer_lowerhalf_s *lower,
uint32_t timeout);
static xcpt_t sam34_capture(FAR struct timer_lowerhalf_s *lower,
xcpt_t handler);
static int sam34_ioctl(FAR struct timer_lowerhalf_s *lower, int cmd,
unsigned long arg);
/****************************************************************************
* Private Data
****************************************************************************/
/* "Lower half" driver methods */
static const struct timer_ops_s g_tcops =
{
.start = sam34_start,
.stop = sam34_stop,
.getstatus = sam34_getstatus,
.settimeout = sam34_settimeout,
.capture = sam34_capture,
.ioctl = sam34_ioctl,
};
/* "Lower half" driver state */
static struct sam34_lowerhalf_s g_tcdev;
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Name: sam34_getreg
*
* Description:
* Get the contents of a register
*
****************************************************************************/
#if defined(CONFIG_SAM34_TC_REGDEBUG) && defined(CONFIG_DEBUG)
static uint32_t sam34_getreg(uint32_t addr)
{
static uint32_t prevaddr = 0;
static uint32_t count = 0;
static uint32_t preval = 0;
/* Read the value from the register */
uint32_t val = getreg32(addr);
/* Is this the same value that we read from the same registe last time? Are
* we polling the register? If so, suppress some of the output.
*/
if (addr == prevaddr && val == preval)
{
if (count == 0xffffffff || ++count > 3)
{
if (count == 4)
{
lldbg("...\n");
}
return val;
}
}
/* No this is a new address or value */
else
{
/* Did we print "..." for the previous value? */
if (count > 3)
{
/* Yes.. then show how many times the value repeated */
lldbg("[repeats %d more times]\n", count-3);
}
/* Save the new address, value, and count */
prevaddr = addr;
preval = val;
count = 1;
}
/* Show the register value read */
lldbg("%08lx->%08lx\n", addr, val);
return val;
}
#endif
/****************************************************************************
* Name: sam34_putreg
*
* Description:
* Set the contents of an SAM34 register to a value
*
****************************************************************************/
#if defined(CONFIG_SAM34_TC_REGDEBUG) && defined(CONFIG_DEBUG)
static void sam34_putreg(uint32_t val, uint32_t addr)
{
/* Show the register value being written */
lldbg("%08lx<-%08lx\n", addr, val);
/* Write the value */
putreg32(val, addr);
}
#endif
/****************************************************************************
* Name: sam34_interrupt
*
* Description:
* TC interrupt
*
* Input Parameters:
* Usual interrupt handler arguments.
*
* Returned Values:
* Always returns OK.
*
****************************************************************************/
static int sam34_interrupt(int irq, FAR void *context)
{
FAR struct sam34_lowerhalf_s *priv = &g_tcdev;
uint16_t regval;
tcvdbg("Entry\n");
/* Check if the interrupt is really pending */
regval = sam34_getreg(SAM_TC0_SR);
if ((regval & TC_INT_CPCS) != 0)
{
/* Is there a registered handler? */
if (priv->handler)
{
priv->handler(irq, context);
}
/* TC_INT_CPCS is cleared by reading SAM_TC0_SR */
}
return OK;
}
/****************************************************************************
* Name: sam34_start
*
* Description:
* Start the timer, resetting the time to the current timeout,
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the "lower-half"
* driver state structure.
*
* Returned Values:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_start(FAR struct timer_lowerhalf_s *lower)
{
FAR struct sam34_lowerhalf_s *priv = (FAR struct sam34_lowerhalf_s *)lower;
uint32_t mr_val = 0;
tcvdbg("Entry\n");
DEBUGASSERT(priv);
sam_tc0_enableclk();
sam34_putreg(TC_CCR_CLKDIS, SAM_TC0_CCR); // disable counter
// TC_CMR_WAVE - waveform mode
// TC_CMR_WAVSEL_UPAUTO - reset on RC compare (interval timer)
// TC_CMR_TCCLKS_TIMERCLOCK5 = SCLK
mr_val |= (TC_CMR_WAVE + TC_CMR_WAVSEL_UPAUTO + TC_CMR_TCCLKS_TIMERCLOCK5);
sam34_putreg(mr_val, SAM_TC0_CMR);
sam34_putreg(priv->reload, SAM_TC0_RC); // set interval
#warning "isr active without user handle for now..."
// if(priv->handler)
// {
// clear status
sam34_getreg(SAM_TC0_SR);
sam34_putreg(TC_INT_CPCS, SAM_TC0_IMR);
sam34_putreg(TC_INT_CPCS, SAM_TC0_IER);
// }
sam34_putreg(TC_CCR_SWTRG + TC_CCR_CLKEN, SAM_TC0_CCR); // start counter
priv->started = true;
return OK;
}
/****************************************************************************
* Name: sam34_stop
*
* Description:
* Stop the timer
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the "lower-half"
* driver state structure.
*
* Returned Values:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_stop(FAR struct timer_lowerhalf_s *lower)
{
tcvdbg("Entry\n");
sam34_putreg(TC_CCR_CLKDIS, SAM_TC0_CCR); // disable counter
sam34_putreg(TC_INT_ALL, SAM_TC0_IDR); // disable all ints
sam_tc0_disableclk();
return OK;
}
/****************************************************************************
* Name: sam34_getstatus
*
* Description:
* Get the current timer status
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the "lower-half"
* driver state structure.
* stawtus - The location to return the status information.
*
* Returned Values:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_getstatus(FAR struct timer_lowerhalf_s *lower,
FAR struct timer_status_s *status)
{
FAR struct sam34_lowerhalf_s *priv = (FAR struct sam34_lowerhalf_s *)lower;
uint32_t elapsed;
tcvdbg("Entry\n");
DEBUGASSERT(priv);
/* Return the status bit */
status->flags = 0;
if (priv->started)
{
status->flags |= TCFLAGS_ACTIVE;
}
if (priv->handler)
{
status->flags |= TCFLAGS_CAPTURE;
}
/* Return the actual timeout is milliseconds */
status->timeout = priv->timeout;
/* Get the time remaining until the timer expires (in microseconds) */
elapsed = sam34_getreg(SAM_TC0_CV);
status->timeleft = (priv->timeout * elapsed) / (priv->reload + 1);
tcvdbg(" flags : %08x\n", status->flags);
tcvdbg(" timeout : %d\n", status->timeout);
tcvdbg(" timeleft : %d\n", status->timeleft);
return OK;
}
/****************************************************************************
* Name: sam34_settimeout
*
* Description:
* Set a new timeout value (and reset the timer)
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the "lower-half"
* driver state structure.
* timeout - The new timeout value in millisecnds.
*
* Returned Values:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_settimeout(FAR struct timer_lowerhalf_s *lower,
uint32_t timeout)
{
FAR struct sam34_lowerhalf_s *priv = (FAR struct sam34_lowerhalf_s *)lower;
uint32_t reload;
DEBUGASSERT(priv);
tcvdbg("Entry: timeout=%d\n", timeout);
/* Can this timeout be represented? */
if (timeout < 1 || timeout > TC_MAXTIMEOUT)
{
tcdbg("Cannot represent timeout=%lu > %lu\n",
timeout, TC_MAXTIMEOUT);
return -ERANGE;
}
#warning "-1 or no?"
reload = (((uint64_t)timeout * TC_FCLK) / 1000000) - 1;
/* Make sure that the final reload value is within range */
#warning "+1 or no?"
if (reload > TC_CV_MASK)
{
reload = TC_CV_MASK;
}
/* Calculate and save the actual timeout value in milliseconds:
*
* timeout = 1000 * (reload + 1) / Fwdt
*/
priv->timeout = 1000 * (reload + 1) / TC_FCLK;
/* Remember the selected values */
priv->reload = reload;
tcvdbg("fwdt=%d reload=%d timout=%d\n",
TC_FCLK, reload, priv->timeout);
// Don't commit to MR register until started!
return OK;
}
/****************************************************************************
* Name: sam34_capture
*
* Description:
* Don't reset on timer timeout; instead, call this user provider
* timeout handler. NOTE: Providing handler==NULL will restore the reset
* behavior.
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the "lower-half"
* driver state structure.
* newhandler - The new timer expiration function pointer. If this
* function pointer is NULL, then the reset-on-expiration
* behavior is restored,
*
* Returned Values:
* The previous timer expiration function pointer or NULL is there was
* no previous function pointer, i.e., if the previous behavior was
* reset-on-expiration (NULL is also returned if an error occurs).
*
****************************************************************************/
static xcpt_t sam34_capture(FAR struct timer_lowerhalf_s *lower,
xcpt_t handler)
{
#if 0 // TODO
FAR struct sam34_lowerhalf_s *priv = (FAR struct sam34_lowerhalf_s *)lower;
irqstate_t flags;
xcpt_t oldhandler;
uint16_t regval;
DEBUGASSERT(priv);
tcvdbg("Entry: handler=%p\n", handler);
/* Get the old handler return value */
flags = irqsave();
oldhandler = priv->handler;
/* Save the new handler */
priv->handler = handler;
/* Are we attaching or detaching the handler? */
regval = sam34_getreg(SAM_TC_CFR);
if (handler)
{
/* Attaching... Enable the EWI interrupt */
regval |= WWDG_CFR_EWI;
sam34_putreg(regval, SAM_TC_CFR);
up_enable_irq(STM32_IRQ_WWDG);
}
else
{
/* Detaching... Disable the EWI interrupt */
regval &= ~WWDG_CFR_EWI;
sam34_putreg(regval, SAM_TC_CFR);
up_disable_irq(STM32_IRQ_WWDG);
}
irqrestore(flags);
return oldhandler;
#endif
ASSERT(0);
return NULL;
}
/****************************************************************************
* Name: sam34_ioctl
*
* Description:
* Any ioctl commands that are not recognized by the "upper-half" driver
* are forwarded to the lower half driver through this method.
*
* Input Parameters:
* lower - A pointer the publicly visible representation of the "lower-half"
* driver state structure.
* cmd - The ioctol command value
* arg - The optional argument that accompanies the 'cmd'. The
* interpretation of this argument depends on the particular
* command.
*
* Returned Values:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
static int sam34_ioctl(FAR struct timer_lowerhalf_s *lower, int cmd,
unsigned long arg)
{
FAR struct sam34_lowerhalf_s *priv = (FAR struct sam34_lowerhalf_s *)lower;
int ret = -ENOTTY;
DEBUGASSERT(priv);
tcvdbg("Entry: cmd=%d arg=%ld\n", cmd, arg);
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sam_tcinitialize
*
* Description:
* Initialize the timer. The timer is initialized and
* registers as 'devpath'.
*
* Input Parameters:
* devpath - The full path to the timer. This should be of the form
* /dev/tc0
*
* Returned Values:
* None
*
****************************************************************************/
void sam_tcinitialize(FAR const char *devpath, int irq)
{
FAR struct sam34_lowerhalf_s *priv = &g_tcdev;
tcvdbg("Entry: devpath=%s\n", devpath);
/* NOTE we assume that clocking to the IWDG has already been provided by
* the RCC initialization logic.
*/
/* Initialize the driver state structure. Here we assume: (1) the state
* structure lies in .bss and was zeroed at reset time. (2) This function
* is only called once so it is never necessary to re-zero the structure.
*/
priv->ops = &g_tcops;
#warning "add irq + switch in sam34_interrupt or something (also need register base address..."
(void)irq_attach(irq, sam34_interrupt);
// enable interrupt.
#warning "may want to enable/disable in start/stop..."
up_enable_irq(irq);
/* Register the timer driver as /dev/timerX */
(void)timer_register(devpath, (FAR struct timer_lowerhalf_s *)priv);
}
#endif /* CONFIG_TIMER && CONFIG_SAM34_TCx */

View File

@ -0,0 +1,96 @@
/****************************************************************************
* arch/arm/src/sam34/sam_tc.h
*
* Copyright (C) 2012 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.
*
****************************************************************************/
#ifndef __ARCH_ARM_SRC_SAM34_TC_H
#define __ARCH_ARM_SRC_SAM34_TC_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include "chip.h"
#include "chip/sam_tc.h"
#ifdef CONFIG_TIMER
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
#ifndef __ASSEMBLY__
#undef EXTERN
#if defined(__cplusplus)
#define EXTERN extern "C"
extern "C" {
#else
#define EXTERN extern
#endif
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: sam_tcinitialize
*
* Description:
* Initialize the timer. The timer is initialized and
* registers as 'devpath. The initial state of the timer is
* disabled.
*
* Input Parameters:
* devpath - The full path to the timer. This should be of the form
* /dev/tc0
* irq - irq associated with the timer
* Returned Values:
* None
*
****************************************************************************/
#if defined(CONFIG_SAM34_TC0) || defined(CONFIG_SAM34_TC1) || defined(CONFIG_SAM34_TC2) || defined(CONFIG_SAM34_TC3) || defined(CONFIG_SAM34_TC4) || defined(CONFIG_SAM34_TC5)
EXTERN void sam_tcinitialize(FAR const char *devpath, int irq);
#endif
#undef EXTERN
#if defined(__cplusplus)
}
#endif
#endif /* __ASSEMBLY__ */
#endif /* CONFIG_TIMER */
#endif /* __ARCH_ARM_SRC_SAM34_TC_H */

567
drivers/timer.c Normal file
View File

@ -0,0 +1,567 @@
/****************************************************************************
* drivers/timer.c
*
* Copyright (C) 2012 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 <string.h>
#include <semaphore.h>
#include <fcntl.h>
#include <assert.h>
#include <errno.h>
#include <debug.h>
#include <nuttx/fs/fs.h>
#include <nuttx/irq.h>
#include <nuttx/kmalloc.h>
#include <nuttx/timer.h>
#ifdef CONFIG_TIMER
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* Debug ********************************************************************/
/* Non-standard debug that may be enabled just for testing the timer driver */
#ifdef CONFIG_DEBUG_TIMER
# define tmrdbg dbg
# define tmrvdbg vdbg
# define tmrlldbg lldbg
# define tmrllvdbg llvdbg
#else
# define tmrdbg(x...)
# define tmrvdbg(x...)
# define tmrlldbg(x...)
# define tmrllvdbg(x...)
#endif
/****************************************************************************
* Private Type Definitions
****************************************************************************/
/* This structure describes the state of the upper half driver */
struct timer_upperhalf_s
{
uint8_t crefs; /* The number of times the device has been opened */
// sem_t exclsem; /* Supports mutual exclusion */
FAR char *path; /* Registration path */
/* The contained lower-half driver */
FAR struct timer_lowerhalf_s *lower;
};
/****************************************************************************
* Private Function Prototypes
****************************************************************************/
static int timer_open(FAR struct file *filep);
static int timer_close(FAR struct file *filep);
static ssize_t timer_read(FAR struct file *filep, FAR char *buffer,
size_t buflen);
static ssize_t timer_write(FAR struct file *filep, FAR const char *buffer,
size_t buflen);
static int timer_ioctl(FAR struct file *filep, int cmd,
unsigned long arg);
/****************************************************************************
* Private Data
****************************************************************************/
static const struct file_operations g_timerops =
{
timer_open, /* open */
timer_close, /* close */
timer_read, /* read */
timer_write, /* write */
0, /* seek */
timer_ioctl /* ioctl */
#ifndef CONFIG_DISABLE_POLL
, 0 /* poll */
#endif
};
/****************************************************************************
* Private Functions
****************************************************************************/
/************************************************************************************
* Name: timer_open
*
* Description:
* This function is called whenever the timer device is opened.
*
************************************************************************************/
static int timer_open(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct timer_upperhalf_s *upper = inode->i_private;
uint8_t tmp;
int ret;
tmrvdbg("crefs: %d\n", upper->crefs);
/* Get exclusive access to the device structures */
ret = 1; //sem_wait(&upper->exclsem);
if (ret < 0)
{
ret = -errno;
goto errout;
}
/* Increment the count of references to the device. If this the first
* time that the driver has been opened for this device, then initialize
* the device.
*/
#warning "anythin init to do on first open?"
tmp = upper->crefs + 1;
if (tmp == 0)
{
/* More than 255 opens; uint8_t overflows to zero */
ret = -EMFILE;
goto errout_with_sem;
}
/* Save the new open count */
upper->crefs = tmp;
ret = OK;
errout_with_sem:
// sem_post(&upper->exclsem);
errout:
return ret;
}
/************************************************************************************
* Name: timer_close
*
* Description:
* This function is called when the timer device is closed.
*
************************************************************************************/
static int timer_close(FAR struct file *filep)
{
FAR struct inode *inode = filep->f_inode;
FAR struct timer_upperhalf_s *upper = inode->i_private;
int ret;
tmrvdbg("crefs: %d\n", upper->crefs);
/* Get exclusive access to the device structures */
ret = 1; //sem_wait(&upper->exclsem);
if (ret < 0)
{
ret = -errno;
goto errout;
}
/* Decrement the references to the driver. If the reference count will
* decrement to 0, then uninitialize the driver.
*/
if (upper->crefs > 0)
{
upper->crefs--;
}
#warning "anythin uninit to do on last close?"
//sem_post(&upper->exclsem);
ret = OK;
errout:
return ret;
}
/************************************************************************************
* Name: timer_read
*
* Description:
* A dummy read method. This is provided only to satisfy the VFS layer.
*
************************************************************************************/
static ssize_t timer_read(FAR struct file *filep, FAR char *buffer, size_t buflen)
{
/* Return zero -- usually meaning end-of-file */
#warning "return counter value?"
return 0;
}
/************************************************************************************
* Name: timer_write
*
* Description:
* A dummy write method. This is provided only to satisfy the VFS layer.
*
************************************************************************************/
static ssize_t timer_write(FAR struct file *filep, FAR const char *buffer, size_t buflen)
{
return 0;
}
/************************************************************************************
* Name: timer_ioctl
*
* Description:
* The standard ioctl method. This is where ALL of the timer work is
* done.
*
************************************************************************************/
static int timer_ioctl(FAR struct file *filep, int cmd, unsigned long arg)
{
FAR struct inode *inode = filep->f_inode;
FAR struct timer_upperhalf_s *upper = inode->i_private;
FAR struct timer_lowerhalf_s *lower = upper->lower;
int ret;
tmrvdbg("cmd: %d arg: %ld\n", cmd, arg);
DEBUGASSERT(upper && lower);
/* Get exclusive access to the device structures */
ret = 1; //sem_wait(&upper->exclsem);
if (ret < 0)
{
return ret;
}
/* Handle built-in ioctl commands */
switch (cmd)
{
/* cmd: TCIOC_START
* Description: Start the timer
* Argument: Ignored
*/
case TCIOC_START:
{
/* Start the timer, resetting the time to the current timeout */
//DEBUGASSERT(lower->ops->start); /* Required */
if(lower->ops->start)
{
ret = lower->ops->start(lower);
}
else
{
ret = -ENOSYS;
}
}
break;
/* cmd: TCIOC_STOP
* Description: Stop the timer
* Argument: Ignored
*/
case TCIOC_STOP:
{
/* Stop the timer */
//DEBUGASSERT(lower->ops->stop); /* Required */
if(lower->ops->start)
{
ret = lower->ops->stop(lower);
}
else
{
ret = -ENOSYS;
}
}
break;
/* cmd: TCIOC_GETSTATUS
* Description: Get the status of the timer.
* Argument: A writeable pointer to struct timer_status_s.
*/
case TCIOC_GETSTATUS:
{
FAR struct timer_status_s *status;
/* Get the current timer status */
if (lower->ops->getstatus) /* Optional */
{
status = (FAR struct timer_status_s *)((uintptr_t)arg);
if (status)
{
ret = lower->ops->getstatus(lower, status);
}
else
{
ret = -EINVAL;
}
}
else
{
ret = -ENOSYS;
}
}
break;
/* cmd: TCIOC_SETTIMEOUT
* Description: Reset the timeout to this value
* Argument: A 32-bit timeout value in microseconds.
*/
#warning "TODO - pass pointer to uint64 ns? Need to determine if these timers are 16 or 32 bit..."
case TCIOC_SETTIMEOUT:
{
/* Set a new timeout value (and reset the timer) */
if (lower->ops->settimeout) /* Optional */
{
ret = lower->ops->settimeout(lower, (uint32_t)arg);
}
else
{
ret = -ENOSYS;
}
}
break;
/* cmd: TCIOC_CAPTURE
* Description: Called this handler on timeout
* Argument: A pointer to struct timer_capture_s.
*/
case TCIOC_CAPTURE:
{
FAR struct timer_capture_s *capture;
/* Don't reset on timer timeout; instead, call this user
* provider timeout handler. NOTE: Providing handler==NULL will
* restore the reset behavior.
*/
if (lower->ops->capture) /* Optional */
{
capture = (FAR struct timer_capture_s *)((uintptr_t)arg);
if (capture)
{
capture->oldhandler =
lower->ops->capture(lower, capture->newhandler);
ret = OK;
}
else
{
ret = -EINVAL;
}
}
else
{
ret = -ENOSYS;
}
}
break;
/* Any unrecognized IOCTL commands might be platform-specific ioctl commands */
default:
{
tmrvdbg("Forwarding unrecognized cmd: %d arg: %ld\n", cmd, arg);
/* An ioctl commands that are not recognized by the "upper-half"
* driver are forwarded to the lower half driver through this
* method.
*/
if (lower->ops->ioctl) /* Optional */
{
ret = lower->ops->ioctl(lower, cmd, arg);
}
else
{
ret = -ENOSYS;
}
}
break;
}
//sem_post(&upper->exclsem);
return ret;
}
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: timer_register
*
* Description:
* This function binds an instance of a "lower half" timer driver with the
* "upper half" timer device and registers that device so that can be used
* by application code.
*
* When this function is called, the "lower half" driver should be in the
* disabled state (as if the stop() method had already been called).
*
* Input parameters:
* dev path - The full path to the driver to be registers in the NuttX
* pseudo-filesystem. The recommended convention is to name all timer
* drivers as "/dev/tc0", "/dev/tc1", etc. where the driver
* path differs only in the "minor" number at the end of the device name.
* lower - A pointer to an instance of lower half timer driver. This
* instance is bound to the timer driver and must persists as long as
* the driver persists.
*
* Returned Value:
* On success, a non-NULL handle is returned to the caller. In the event
* of any failure, a NULL value is returned.
*
****************************************************************************/
FAR void *timer_register(FAR const char *path,
FAR struct timer_lowerhalf_s *lower)
{
FAR struct timer_upperhalf_s *upper;
int ret;
DEBUGASSERT(path && lower);
tmrvdbg("Entry: path=%s\n", path);
/* Allocate the upper-half data structure */
upper = (FAR struct timer_upperhalf_s *)
kzalloc(sizeof(struct timer_upperhalf_s));
if (!upper)
{
tmrdbg("Upper half allocation failed\n");
goto errout;
}
/* Initialize the timer device structure (it was already zeroed
* by kzalloc()).
*/
//sem_init(&upper->exclsem, 0, 1);
upper->lower = lower;
/* Copy the registration path */
upper->path = strdup(path);
if (!upper->path)
{
tmrdbg("Path allocation failed\n");
goto errout_with_upper;
}
/* Register the timer device */
ret = register_driver(path, &g_timerops, 0666, upper);
if (ret < 0)
{
tmrdbg("register_driver failed: %d\n", ret);
goto errout_with_path;
}
return (FAR void *)upper;
errout_with_path:
kfree(upper->path);
errout_with_upper:
//sem_destroy(&upper->exclsem);
kfree(upper);
errout:
return NULL;
}
/****************************************************************************
* Name: timer_unregister
*
* Description:
* This function can be called to disable and unregister the timer
* device driver.
*
* Input parameters:
* handle - This is the handle that was returned by timer_register()
*
* Returned Value:
* None
*
****************************************************************************/
void timer_unregister(FAR void *handle)
{
FAR struct timer_upperhalf_s *upper;
FAR struct timer_lowerhalf_s *lower;
/* Recover the pointer to the upper-half driver state */
upper = (FAR struct timer_upperhalf_s *)handle;
lower = upper->lower;
DEBUGASSERT(upper && lower);
tmrvdbg("Unregistering: %s\n", upper->path);
/* Disable the timer */
DEBUGASSERT(lower->ops->stop); /* Required */
(void)lower->ops->stop(lower);
/* Unregister the timer device */
(void)unregister_driver(upper->path);
/* Then free all of the driver resources */
kfree(upper->path);
//sem_destroy(&upper->exclsem);
kfree(upper);
}
#endif /* CONFIG_TIMER */

270
include/nuttx/timer.h Normal file
View File

@ -0,0 +1,270 @@
/****************************************************************************
* include/nuttx/timer.h
*
* Copyright (C) 2012 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.
*
****************************************************************************/
#ifndef __INCLUDE_NUTTX_TIMER_H
#define __INCLUDE_NUTTX_TIMER_H
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/compiler.h>
#include <nuttx/irq.h>
#include <nuttx/fs/ioctl.h>
#ifdef CONFIG_TIMER
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/* IOCTL Commands ***********************************************************/
/* The timer driver uses a standard character driver framework. However,
* since the timer driver is a device control interface and not a data
* transfer interface, the majority of the functionality is implemented in
* driver ioctl calls. The timer ioctl commands are lised below:
*
* These are detected and handled by the "upper half" timer driver.
*
* TCIOC_START - Start the timer
* Argument: Ignored
* TCIOC_STOP - Stop the timer
* Argument: Ignored
* TCIOC_GETSTATUS - Get the status of the timer.
* Argument: A writeable pointer to struct timer_status_s.
* TCIOC_SETTIMEOUT - Reset the timer timeout to this value
* Argument: A 32-bit timeout value in microseconds.
* TCIOC_CAPTURE - Do not reset. Instead, called this handler.
* Argument: A pointer to struct timer_capture_s.
*/
#warning "may change TCIOC_SETTIMEOUT to pass pointer to 64bit nanoseconds or timespec structure"
#define TCIOC_START _TCIOC(0x001)
#define TCIOC_STOP _TCIOC(0x002)
#define TCIOC_GETSTATUS _TCIOC(0x003)
#define TCIOC_SETTIMEOUT _TCIOC(0x004)
#define TCIOC_CAPTURE _TCIOC(0x005)
/* Bit Settings *************************************************************/
/* Bit settings for the struct timer_status_s flags field */
#define TCFLAGS_ACTIVE (1 << 0) /* 1=The timer is running */
#define TCFLAGS_CAPTURE (1 << 1) /* 1=Call the user function when the
* timer expires */
/****************************************************************************
* Public Types
****************************************************************************/
/* This is the type of the argument passed to the TCIOC_CAPTURE ioctl */
struct timer_capture_s
{
CODE xcpt_t newhandler; /* The new timer capture handler */
CODE xcpt_t oldhandler; /* The previous timer capture handler (if any) */
};
/* This is the type of the argument passed to the TCIOC_GETSTATUS ioctl and
* and returned by the "lower half" getstatus() method.
*/
struct timer_status_s
{
uint32_t flags; /* See TCFLAGS_* definitions above */
uint32_t timeout; /* The current timeout setting (in milliseconds) */
uint32_t timeleft; /* Time left until the timer expiration
* (in milliseconds) */
};
/* This structure provides the "lower-half" driver operations available to
* the "upper-half" driver.
*/
struct timer_lowerhalf_s;
struct timer_ops_s
{
/* Required methods ********************************************************/
/* Start the timer, resetting the time to the current timeout */
CODE int (*start)(FAR struct timer_lowerhalf_s *lower);
/* Stop the timer */
CODE int (*stop)(FAR struct timer_lowerhalf_s *lower);
/* Get the current timer status */
CODE int (*getstatus)(FAR struct timer_lowerhalf_s *lower,
FAR struct timer_status_s *status);
/* Set a new timeout value (and reset the timer) */
CODE int (*settimeout)(FAR struct timer_lowerhalf_s *lower,
uint32_t timeout);
/* Call this user provider timeout handler on timeout.
* NOTE: Providing handler==NULL disable.
*/
CODE xcpt_t (*capture)(FAR struct timer_lowerhalf_s *lower,
CODE xcpt_t handler);
/* Any ioctl commands that are not recognized by the "upper-half" driver
* are forwarded to the lower half driver through this method.
*/
CODE int (*ioctl)(FAR struct timer_lowerhalf_s *lower, int cmd,
unsigned long arg);
};
/* This structure provides the publicly visible representation of the
* "lower-half" driver state structure. "lower half" drivers will have an
* internal structure definition that will be cast-compatible with this
* structure definitions.
*/
struct timer_lowerhalf_s
{
/* Publicly visible portion of the "lower-half" driver state structure. */
FAR const struct timer_ops_s *ops; /* Lower half operations */
/* The remainder of the structure is used by the "lower-half" driver
* for whatever state storage that it may need.
*/
};
/****************************************************************************
* Public Data
****************************************************************************/
/****************************************************************************
* Public Function Prototypes
****************************************************************************/
#ifdef __cplusplus
#define EXTERN extern "C"
extern "C" {
#else
#define EXTERN extern
#endif
/****************************************************************************
* "Upper-Half" Timer Driver Interfaces
****************************************************************************/
/****************************************************************************
* Name: timer_register
*
* Description:
* This function binds an instance of a "lower half" timer driver with the
* "upper half" timer device and registers that device so that can be used
* by application code.
*
* When this function is called, the "lower half" driver should be in the
* disabled state (as if the stop() method had already been called).
*
* NOTE: Normally, this function would not be called by application code.
* Rather it is called indirectly through the architecture-specific
* interface up_timerinitialize() described below.
*
* Input parameters:
* dev path - The full path to the driver to be registers in the NuttX
* pseudo-filesystem. The recommended convention is to name all timer
* drivers as "/dev/timer0", "/dev/timer1", etc. where the driver
* path differs only in the "minor" number at the end of the device name.
* lower - A pointer to an instance of lower half timer driver. This
* instance is bound to the timer driver and must persists as long as
* the driver persists.
*
* Returned Value:
* On success, a non-NULL handle is returned to the caller. In the event
* of any failure, a NULL value is returned.
*
****************************************************************************/
EXTERN FAR void *timer_register(FAR const char *path,
FAR struct timer_lowerhalf_s *lower);
/****************************************************************************
* Name: timer_unregister
*
* Description:
* This function can be called to disable and unregister the timer
* device driver.
*
* Input parameters:
* handle - This is the handle that was returned by timer_register()
*
* Returned Value:
* None
*
****************************************************************************/
EXTERN void timer_unregister(FAR void *handle);
/****************************************************************************
* Platform-Independent "Lower-Half" Timer Driver Interfaces
****************************************************************************/
/****************************************************************************
* Architecture-specific Application Interfaces
****************************************************************************/
/****************************************************************************
* Name: up_timerinitialize()
*
* Description:
* Perform architecture-specific initialization of the timer hardware.
* This interface should be provided by all configurations using
* to avoid exposed platform-dependent logic.
*
* At a minimum, this function should call timer_register() which is
* described above.
*
* Input parameters:
* None
*
* Returned Value:
* Zero on success; a negated errno value on failure.
*
****************************************************************************/
EXTERN int up_timerinitialize(void);
#undef EXTERN
#ifdef __cplusplus
}
#endif
#endif /* CONFIG_TIMER */
#endif /* __INCLUDE_NUTTX_TIMER_H */