summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGregory Nutt <gnutt@nuttx.org>2015-01-13 15:55:54 -0600
committerGregory Nutt <gnutt@nuttx.org>2015-01-13 15:55:54 -0600
commitff41392ee5f6d7c3606ce23fa03d1a80a0edd653 (patch)
tree1d9f0466163c80176d92dbaf5f9c0f418d33ed84
parent88c0a658b7fd624af3dacf652c5ed16c02199650 (diff)
downloadpx4-nuttx-ff41392ee5f6d7c3606ce23fa03d1a80a0edd653.tar.gz
px4-nuttx-ff41392ee5f6d7c3606ce23fa03d1a80a0edd653.tar.bz2
px4-nuttx-ff41392ee5f6d7c3606ce23fa03d1a80a0edd653.zip
Tiva Timer: Timer test must attach a timer handler or the timer is stopped at the first interrupt
-rw-r--r--apps/examples/timer/timer_main.c38
-rw-r--r--nuttx/arch/arm/src/sam34/sam_tc.c9
-rw-r--r--nuttx/arch/arm/src/tiva/tiva_timerlow32.c36
3 files changed, 60 insertions, 23 deletions
diff --git a/apps/examples/timer/timer_main.c b/apps/examples/timer/timer_main.c
index ed1219447..4546e5b9d 100644
--- a/apps/examples/timer/timer_main.c
+++ b/apps/examples/timer/timer_main.c
@@ -72,6 +72,21 @@
****************************************************************************/
/****************************************************************************
+ * timer_handler
+ ****************************************************************************/
+
+static bool timer_handler(FAR uint32_t *next_interval_us)
+{
+ /* This handler may:
+ *
+ * (1) Modify the timeout value to change the frequency dynamically, or
+ * (2) Return false to stop the timer.
+ */
+
+ return true;
+}
+
+/****************************************************************************
* timer_status
****************************************************************************/
@@ -111,6 +126,7 @@ int main(int argc, FAR char *argv[])
int timer_main(int argc, char *argv[])
#endif
{
+ struct timer_sethandler_s handler;
int ret;
int fd;
int i;
@@ -144,6 +160,28 @@ int timer_main(int argc, char *argv[])
return EXIT_FAILURE;
}
+ /* Show the timer status before attaching the timer handler */
+
+ timer_status(fd);
+
+ /* Attach the timer handler
+ *
+ * NOTE: If no handler is attached, the timer stop at the first interrupt.
+ */
+
+ printf("Attach timer handler\n");
+
+ handler.newhandler = timer_handler;
+ handler.oldhandler = NULL;
+
+ ret = ioctl(fd, TCIOC_SETHANDLER, (unsigned long)((uintptr_t)&handler));
+ if (ret < 0)
+ {
+ fprintf(stderr, "ERROR: Failed to set the timer handler: %d\n", errno);
+ close(fd);
+ return EXIT_FAILURE;
+ }
+
/* Show the timer status before starting */
timer_status(fd);
diff --git a/nuttx/arch/arm/src/sam34/sam_tc.c b/nuttx/arch/arm/src/sam34/sam_tc.c
index 1f866726a..0f2ae3c37 100644
--- a/nuttx/arch/arm/src/sam34/sam_tc.c
+++ b/nuttx/arch/arm/src/sam34/sam_tc.c
@@ -276,7 +276,9 @@ static int sam34_interrupt(int irq, FAR void *context)
{
uint32_t timeout;
- /* Is there a registered handler? */
+ /* Is there a registered handler? If the handler has been nullified,
+ * the timer will be stopped.
+ */
if (priv->handler && priv->handler(&priv->timeout))
{
@@ -291,9 +293,12 @@ static int sam34_interrupt(int irq, FAR void *context)
timeout = (1000000ULL * priv->clkticks) / TC_FCLK; /* trucated timeout */
priv->adjustment = (priv->adjustment + priv->timeout) - timeout; /* truncated time to be added to next interval (dither) */
}
- else /* stop */
+ else
{
+ /* No handler or the handler returned false.. stop the timer */
+
sam34_stop((FAR struct timer_lowerhalf_s *)priv);
+ tcvdbg("Stopped\n");
}
/* TC_INT_CPCS is cleared by reading SAM_TCx_SR */
diff --git a/nuttx/arch/arm/src/tiva/tiva_timerlow32.c b/nuttx/arch/arm/src/tiva/tiva_timerlow32.c
index ab6053db2..20bf600a8 100644
--- a/nuttx/arch/arm/src/tiva/tiva_timerlow32.c
+++ b/nuttx/arch/arm/src/tiva/tiva_timerlow32.c
@@ -87,7 +87,7 @@ struct tiva_lowerhalf_s
static uint32_t tiva_usec2ticks(struct tiva_lowerhalf_s *priv, uint32_t usecs);
static uint32_t tiva_ticks2usec(struct tiva_lowerhalf_s *priv, uint32_t ticks);
-static int tiva_timeout(struct tiva_lowerhalf_s *priv, uint32_t timeout);
+static void tiva_timeout(struct tiva_lowerhalf_s *priv, uint32_t timeout);
/* Interrupt handling *******************************************************/
@@ -192,11 +192,11 @@ static uint32_t tiva_ticks2usec(struct tiva_lowerhalf_s *priv, uint32_t ticks)
* timeout - The new timeout value in microseconds.
*
* Returned Values:
- * Zero on success; a negated errno value on failure.
+ * None
*
****************************************************************************/
-static int tiva_timeout(struct tiva_lowerhalf_s *priv, uint32_t timeout)
+static void tiva_timeout(struct tiva_lowerhalf_s *priv, uint32_t timeout)
{
timvdbg("Entry: timeout=%d\n", timeout);
@@ -215,8 +215,6 @@ static int tiva_timeout(struct tiva_lowerhalf_s *priv, uint32_t timeout)
timvdbg("clkin=%d clkticks=%d timeout=%d, adjustment=%d\n",
priv->clkin, priv->clkticks, priv->timeout, priv->adjustment);
-
- return OK;
}
/****************************************************************************
@@ -246,7 +244,9 @@ static void tiva_handler(TIMER_HANDLE handle, void *arg, uint32_t status)
{
uint32_t timeout;
- /* Is there a registered handler? */
+ /* Is there a registered handler? If the handler has been nullified,
+ * the timer will be stopped.
+ */
if (priv->handler && priv->handler(&priv->timeout))
{
@@ -269,9 +269,12 @@ static void tiva_handler(TIMER_HANDLE handle, void *arg, uint32_t status)
timeout = tiva_ticks2usec(priv, priv->clkticks);
priv->adjustment = (priv->adjustment + priv->timeout) - timeout;
}
- else /* stop */
+ else
{
+ /* No handler or the handler returned false.. stop the timer */
+
tiva_timer32_stop(priv->handle);
+ timvdbg("Stopped\n");
}
}
}
@@ -422,7 +425,6 @@ static int tiva_getstatus(struct timer_lowerhalf_s *lower,
static int tiva_settimeout(struct timer_lowerhalf_s *lower, uint32_t timeout)
{
struct tiva_lowerhalf_s *priv = (struct tiva_lowerhalf_s *)lower;
- int ret;
DEBUGASSERT(priv);
@@ -435,15 +437,12 @@ static int tiva_settimeout(struct timer_lowerhalf_s *lower, uint32_t timeout)
/* Calculate the the new time settings */
- ret = tiva_timeout(priv, timeout);
- if (ret == OK)
- {
- /* Reset the timer interval */
+ tiva_timeout(priv, timeout);
- tiva_timer32_setinterval(priv->handle, priv->clkticks);
- }
+ /* Reset the timer interval */
- return ret;
+ tiva_timer32_setinterval(priv->handle, priv->clkticks);
+ return OK;
}
/****************************************************************************
@@ -584,12 +583,7 @@ int tiva_timer_register(const char *devpath, int gptm, uint32_t timeout,
/* Set the initial timer interval */
- ret = tiva_timeout(priv, timeout);
- if (ret < 0)
- {
- timdbg("ERROR: Failed to set initial timeout\n");
- goto errout_with_alloc;
- }
+ tiva_timeout(priv, timeout);
/* Create the timer handle */