diff --git a/arch/arm/src/sam34/Make.defs b/arch/arm/src/sam34/Make.defs index efd7e23267..c09eeab733 100644 --- a/arch/arm/src/sam34/Make.defs +++ b/arch/arm/src/sam34/Make.defs @@ -134,6 +134,10 @@ ifeq ($(CONFIG_SAM34_RTC),y) CHIP_CSRCS += sam_rtc.c endif +ifeq ($(CONFIG_SAM34_RTT),y) +CHIP_CSRCS += sam_rtt.c +endif + ifeq ($(CONFIG_SAM34_WDT),y) CHIP_CSRCS += sam_wdt.c endif diff --git a/arch/arm/src/sam34/sam_rtc.c b/arch/arm/src/sam34/sam_rtc.c index 0bdb028d20..9b4e8b40fb 100644 --- a/arch/arm/src/sam34/sam_rtc.c +++ b/arch/arm/src/sam34/sam_rtc.c @@ -54,20 +54,25 @@ #include "up_arch.h" #include "sam_rtc.h" +#if defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTT) +# include "sam_rtt.h" +# include "sam_periphclks.h" +#endif + #ifdef CONFIG_RTC /************************************************************************************ * Pre-processor Definitions ************************************************************************************/ /* Configuration ********************************************************************/ -/* This RTC implementation supports only date/time RTC hardware */ - -#ifndef CONFIG_RTC_DATETIME -# error "CONFIG_RTC_DATETIME must be set to use this driver" -#endif #ifdef CONFIG_RTC_HIRES -# error "CONFIG_RTC_HIRES must NOT be set with this driver" +# if !defined(CONFIG_SAM34_RTT) +# error RTT is required to emulate high resolution RTC +# endif +# if (CONFIG_RTC_FREQUENCY > 32768) || ((32768 % CONFIG_RTC_FREQUENCY) != 0) +# error CONFIG_RTC_FREQUENCY must be an integer division of 32768 +# endif #endif #if defined(CONFIG_RTC_ALARM) && !defined(CONFIG_SCHED_WORKQUEUE) @@ -119,6 +124,12 @@ struct work_s g_alarmwork; volatile bool g_rtc_enabled = false; +/* g_rtt_offset holds the rtt->rtc count offset */ + +#if defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTT) +uint32_t g_rtt_offset = 0; +#endif + /************************************************************************************ * Private Functions ************************************************************************************/ @@ -302,6 +313,40 @@ static int rtc_interrupt(int irq, void *context) } #endif +/************************************************************************************ + * Name: rtc_sync + * + * Description: + * Waits and returns immediately after 1 sec tick. For best accuracy, + * call with interrupts disabled. + * + * Returns value of the TIMR register + * + ************************************************************************************/ + +static uint32_t rtc_sync(void) +{ + uint32_t r0, r1; + + /* Get start second (stable) */ + + do + { + r0 = getreg32(SAM_RTC_TIMR); + } + while (r0 != getreg32(SAM_RTC_TIMR)); + + /* Now read until it changes */ + + do + { + r1 = getreg32(SAM_RTC_TIMR); + } + while (r1 == r0); + + return r1; +} + /************************************************************************************ * Public Functions ************************************************************************************/ @@ -370,6 +415,32 @@ int up_rtcinitialize(void) #endif +#if defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTT) + /* Using the RTT for subsecond ticks. */ + + sam_rtt_enableclk(); + + /* Disable ints, set prescaler, start counter */ + + putreg32(RTT_MR_RTPRES(32768/CONFIG_RTC_FREQUENCY) | RTT_MR_RTTRST, SAM_RTT_MR); + + /* wait for a second tick to get the RTT offset. + * Interrupts are assumed to still be off at this point. + */ + + rtc_sync(); + + /* Probably safe to read the RTT_VR register now since the clock just ticked, + * but we'll be careful anyway. + */ + + do + { + g_rtt_offset = getreg32(SAM_RTT_VR); + } + while(getreg32(SAM_RTT_VR) != g_rtt_offset); +#endif + rtc_dumpregs("After Initialization"); return OK; } @@ -681,4 +752,55 @@ int up_rtc_setalarm(FAR const struct timespec *tp, alarmcb_t callback) } #endif + +/************************************************************************************ + * Name: up_rtc_gettime + * + * Description: + * Get the current time from the high resolution RTC clock/counter. This interface + * is only supported by the high-resolution RTC/counter hardware implementation. + * It is used to replace the system timer. + * + * Input Parameters: + * tp - The location to return the high resolution time value. + * + * Returned Value: + * Zero (OK) on success; a negated errno on failure + * + ************************************************************************************/ + +#if defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTT) + +int up_rtc_gettime(FAR struct timespec *tp) +{ + /* This is a hack to emulate a high resolution rtc using the rtt */ + uint32_t rtc_cal, rtc_tim, rtt_val; + struct tm t; + + do + { + rtc_cal = getreg32(SAM_RTC_CALR); + rtc_tim = getreg32(SAM_RTC_TIMR); + rtt_val = getreg32(SAM_RTT_VR); + } while((rtc_cal != getreg32(SAM_RTC_CALR)) || + (rtc_tim != getreg32(SAM_RTC_TIMR)) || + (rtt_val != getreg32(SAM_RTT_VR))); + + t.tm_sec = rtc_bcd2bin((rtc_tim & RTC_TIMR_SEC_MASK) >> RTC_TIMR_SEC_SHIFT); + t.tm_min = rtc_bcd2bin((rtc_tim & RTC_TIMR_MIN_MASK) >> RTC_TIMR_MIN_SHIFT); + t.tm_hour = rtc_bcd2bin((rtc_tim & RTC_TIMR_HOUR_MASK) >> RTC_TIMR_HOUR_SHIFT); + t.tm_mday = rtc_bcd2bin((rtc_cal & RTC_CALR_DATE_MASK) >> RTC_CALR_DATE_SHIFT); + t.tm_mon = rtc_bcd2bin((rtc_cal & RTC_CALR_MONTH_MASK) >> RTC_CALR_MONTH_SHIFT); + t.tm_year = (rtc_bcd2bin((rtc_cal & RTC_CALR_CENT_MASK) >> RTC_CALR_CENT_SHIFT) * 100) + + rtc_bcd2bin((rtc_cal & RTC_CALR_YEAR_MASK) >> RTC_CALR_YEAR_SHIFT) + - 1900; + + tp->tv_sec = mktime(&t); + tp->tv_nsec = (((rtt_val-g_rtt_offset) & (CONFIG_RTC_FREQUENCY-1)) * 1000000000ULL) / + CONFIG_RTC_FREQUENCY; + + return OK; +} +#endif + #endif /* CONFIG_RTC */ diff --git a/arch/arm/src/sam34/sam_rtt.c b/arch/arm/src/sam34/sam_rtt.c new file mode 100644 index 0000000000..fc9ddd1706 --- /dev/null +++ b/arch/arm/src/sam34/sam_rtt.c @@ -0,0 +1,668 @@ +/**************************************************************************** + * arch/arm/src/sam34/sam_rtt.c + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Authors: Gregory Nutt + * Bob Dioron + * + * 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 +#include + +#include + +#include +#include +#include +#include +#include + +#include "up_arch.h" +#include "sam_rtt.h" +#include "sam_periphclks.h" + +#if defined(CONFIG_TIMER) && (defined(CONFIG_SAM34_RTT)) + +/**************************************************************************** + * Pre-Processor Definitions + ****************************************************************************/ +/* Clocking *****************************************************************/ + +#if defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTC) +# define RTT_PRES (32768/CONFIG_RTC_FREQUENCY) +#else +/* TODO: Allow prescaler selection. */ +# define RTT_PRES 1 +#endif + +#define RTT_FCLK (BOARD_SLCK_FREQUENCY/RTT_PRES) +#define RTT_MAXTIMEOUT ((1000000ULL * (0x100000000ULL)) / RTT_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_RTT +# define rttdbg lldbg +# define rttvdbg llvdbg +#else +# define rttdbg(x...) +# define rttvdbg(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 */ + + /* Private data */ + + tccb_t handler; /* Current user interrupt handler */ + uint32_t timeout; /* The current timeout value (us) */ + uint32_t clkticks; /* actual clock ticks for current interval */ + uint32_t val; /* rtt value of current timeout */ + uint32_t adjustment; /* time lost due to clock resolution truncation (us) */ + bool started; /* The timer has been started */ +}; + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ +/* Register operations ******************************************************/ + +#if defined(CONFIG_SAM34_RTT_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 handling *******************************************************/ + +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 tccb_t sam34_sethandler(FAR struct timer_lowerhalf_s *lower, + tccb_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, + .sethandler = sam34_sethandler, + .ioctl = sam34_ioctl, +}; + +/* "Lower half" driver state */ + +static struct sam34_lowerhalf_s g_tcdev; + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: sam34_readvr + * + * Description: + * Get the contents of the value register. + * + ****************************************************************************/ +static inline uint32_t sam34_readvr(void) +{ + register uint32_t v; + + /* Async counter, read until two consecutive reads match */ + + do + { + v = getreg32(SAM_RTT_VR); + } + while(v != getreg32(SAM_RTT_VR)); + + return v; +} + +/**************************************************************************** + * Name: sam34_getreg + * + * Description: + * Get the contents of a register + * + ****************************************************************************/ + +#if defined(CONFIG_SAM34_RTT_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_RTT_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; + + rttvdbg("Entry\n"); + DEBUGASSERT(irq == SAM_IRQ_RTT); + + /* Check if the interrupt is really pending */ + + if ((sam34_getreg(SAM_RTT_SR) & RTT_SR_ALMS) != 0) + { + uint32_t timeout; + uint32_t mrval; + uint32_t vr; + uint32_t lateticks; + + /* Is there a registered handler? */ + + if (priv->handler && priv->handler(&priv->timeout)) + { + /* Disable int before writing new alarm */ + + mrval = sam34_getreg(SAM_RTT_MR); + sam34_putreg(mrval & ~RTT_MR_ALMIEN, SAM_RTT_MR); + + /* Calculate new ticks / dither adjustment */ + + vr = sam34_readvr(); + priv->clkticks = + ((uint64_t)(priv->adjustment + priv->timeout)) * RTT_FCLK / 1000000; + + /* Subtract off how late we are, but only up to half an interval. + * TODO calculate lost ticks? + */ + + lateticks = vr - priv->val; + if (lateticks <= (priv->clkticks>>1)) + { + priv->clkticks -= lateticks; + } + + /* Set next interval interval. */ + + priv->val = vr + priv->clkticks; + sam34_putreg(priv->val-1, SAM_RTT_AR); + + /* Re-enable alarm */ + + sam34_putreg(mrval, SAM_RTT_MR); + + /* Truncated timeout */ + + timeout = (1000000ULL * priv->clkticks) / RTT_FCLK; + + /* Truncated time to be added to next interval (dither) */ + + priv->adjustment = (priv->adjustment + priv->timeout) - timeout; + } + else /* stop */ + { + sam34_stop((FAR struct timer_lowerhalf_s *)priv); + } + + /* RTT_SR_ALMS is cleared by reading SAM_RTT_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; + uint32_t vr; + + rttvdbg("Entry\n"); + DEBUGASSERT(priv); + + if (priv->started) + { + return -EINVAL; + } + +#if defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTC) + /* RTT is started with the RTC and always on */ + + mr = sam34_getreg(SAM_RTT_MR) & ~RTT_MR_ALMIEN; + sam34_putreg(mr, SAM_RTT_MR); + vr = sam34_readvr(); + +#else + sam_rtt_enableclk(); /* Enable peripheral clock */ + mr = RTT_MR_RTPRES(RTT_PRES); + sam34_putreg(mr, SAM_RTT_MR); /* Set prescaler, disable ints */ + vr = 0; /* we're going to reset the counter */ +#endif + + priv->val = vr + priv->clkticks; /* value at end of interval */ + sam34_putreg(priv->val-1, SAM_RTT_AR); /* Set interval */ + + if (priv->handler) + { + /* Clear status and enable interrupt */ + + sam34_getreg(SAM_RTT_SR); + mr |= RTT_MR_ALMIEN; + sam34_putreg(mr, SAM_RTT_MR); + } + +#if !(defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTC)) + sam34_putreg(mr | RTT_MR_RTTRST, SAM_RTT_MR); /* Start counter */ +#endif /* !(CONFIG_RTC_HIRES && CONFIG_SAM34_RTC) */ + + 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) +{ + FAR struct sam34_lowerhalf_s *priv = (FAR struct sam34_lowerhalf_s *)lower; + rttvdbg("Entry\n"); + DEBUGASSERT(priv); + + if(!priv->started) + { + return -EINVAL; + } + +#if !(defined(CONFIG_RTC_HIRES) && defined (CONFIG_SAM34_RTC)) + sam34_putreg(RTT_MR_RTTDIS, SAM_RTT_MR); /* Disable RTT */ + sam_rtt_disableclk(); /* Disable peripheral clock */ +#endif + + priv->started = false; + + 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. + * status - 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; + + rttvdbg("Entry\n"); + DEBUGASSERT(priv); + + /* Return the status bit */ + + status->flags = 0; + if (priv->started) + { + status->flags |= TCFLAGS_ACTIVE; + } + + if (priv->handler) + { + status->flags |= TCFLAGS_HANDLER; + } + + /* Return the actual timeout is milliseconds */ + + status->timeout = priv->timeout; + + /* Get the time remaining until the timer expires (in microseconds) */ + + status->timeleft = 1000000ULL*(sam34_getreg(SAM_RTT_AR) - sam34_readvr())/RTT_FCLK; + + rttvdbg(" flags : %08x\n", status->flags); + rttvdbg(" timeout : %d\n", status->timeout); + rttvdbg(" 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 milliseconds. + * + * 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; + + DEBUGASSERT(priv); + rttvdbg("Entry: timeout=%d\n", timeout); + + if(priv->started) return -EPERM; + + /* Can this timeout be represented? */ + + if (timeout < 1 || timeout > RTT_MAXTIMEOUT) + { + rttdbg("Cannot represent timeout=%lu > %lu\n", + timeout, RTT_MAXTIMEOUT); + return -ERANGE; + } + + priv->timeout = timeout; /* Intended timeout */ + priv->clkticks = (((uint64_t)timeout * RTT_FCLK) / 1000000); /* Actual clock ticks */ + timeout = (1000000ULL * priv->clkticks) / RTT_FCLK; /* Truncated timeout */ + priv->adjustment = priv->timeout - timeout; /* Truncated time to be added to next interval (dither) */ + + rttvdbg("fclk=%d clkticks=%d timout=%d, adjustment=%d\n", + RTT_FCLK, priv->clkticks, priv->timeout, priv->adjustment); + + return OK; +} + +/**************************************************************************** + * Name: sam34_sethandler + * + * Description: + * Call this user provided timeout handler. + * + * 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. + * + ****************************************************************************/ + +static tccb_t sam34_sethandler(FAR struct timer_lowerhalf_s *lower, + tccb_t handler) +{ + FAR struct sam34_lowerhalf_s *priv = (FAR struct sam34_lowerhalf_s *)lower; + irqstate_t flags; + tccb_t oldhandler; + + flags = irqsave(); + + DEBUGASSERT(priv); + rttvdbg("Entry: handler=%p\n", handler); + + /* Get the old handler return value */ + + oldhandler = priv->handler; + + /* Save the new handler */ + + priv->handler = handler; + + irqrestore(flags); + return oldhandler; +} + +/**************************************************************************** + * 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 ioctl 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); + rttvdbg("Entry: cmd=%d arg=%ld\n", cmd, arg); + + return ret; +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: sam_rttinitialize + * + * 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/rtt0 + * + * Returned Values: + * None + * + ****************************************************************************/ + +void sam_rttinitialize(FAR const char *devpath) +{ + FAR struct sam34_lowerhalf_s *priv = &g_tcdev; + + rttvdbg("Entry: devpath=%s\n", devpath); + + /* 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; + + (void)irq_attach(SAM_IRQ_RTT, sam34_interrupt); + + /* Enable NVIC interrupt. */ + + up_enable_irq(SAM_IRQ_RTT); + + /* Register the timer driver as /dev/timerX */ + + (void)timer_register(devpath, (FAR struct timer_lowerhalf_s *)priv); +} + +#endif /* CONFIG_TIMER && CONFIG_SAM34_TCx */ diff --git a/arch/arm/src/sam34/sam_rtt.h b/arch/arm/src/sam34/sam_rtt.h new file mode 100644 index 0000000000..f300747efb --- /dev/null +++ b/arch/arm/src/sam34/sam_rtt.h @@ -0,0 +1,100 @@ +/**************************************************************************** + * arch/arm/src/sam34/sam_rtt.h + * + * Copyright (C) 2014 Gregory Nutt. All rights reserved. + * Authors: Gregory Nutt + * Bob Doiron + * + * 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_RTT_H +#define __ARCH_ARM_SRC_SAM34_RTT_H + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include "chip.h" +#include "chip/sam_rtt.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 + ****************************************************************************/ + +#if defined(CONFIG_SAM34_RTT) + +/**************************************************************************** + * Name: sam_rttinitialize + * + * 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/rtt0 + * + * Returned Values: + * None + * + ****************************************************************************/ + +void sam_rttinitialize(FAR const char *devpath); + +#endif // CONFIG_SAM34_RTT + + +#undef EXTERN +#if defined(__cplusplus) +} +#endif + +#endif /* __ASSEMBLY__ */ +#endif /* CONFIG_TIMER */ +#endif /* __ARCH_ARM_SRC_SAM34_RTT_H */ diff --git a/arch/arm/src/sam34/sam_tc.c b/arch/arm/src/sam34/sam_tc.c index 27dbd16420..b955d6bb03 100644 --- a/arch/arm/src/sam34/sam_tc.c +++ b/arch/arm/src/sam34/sam_tc.c @@ -106,7 +106,6 @@ struct sam34_lowerhalf_s uint32_t clkticks; /* actual clock ticks for current interval */ bool started; /* The timer has been started */ uint16_t periphid; /* peripheral id */ - uint16_t debug; }; /**************************************************************************** @@ -134,7 +133,7 @@ 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 tccb_t sam34_capture(FAR struct timer_lowerhalf_s *lower, +static tccb_t sam34_sethandler(FAR struct timer_lowerhalf_s *lower, tccb_t handler); static int sam34_ioctl(FAR struct timer_lowerhalf_s *lower, int cmd, unsigned long arg); @@ -150,7 +149,7 @@ static const struct timer_ops_s g_tcops = .stop = sam34_stop, .getstatus = sam34_getstatus, .settimeout = sam34_settimeout, - .capture = sam34_capture, + .sethandler = sam34_sethandler, .ioctl = sam34_ioctl, }; @@ -267,15 +266,13 @@ static void sam34_putreg(uint32_t val, uint32_t addr) static int sam34_interrupt(int irq, FAR void *context) { FAR struct sam34_lowerhalf_s *priv = &g_tcdevs[irq-SAM_IRQ_TC0]; - uint16_t regval; tcvdbg("Entry\n"); DEBUGASSERT((irq >= SAM_IRQ_TC0) && (irq <= SAM_IRQ_TC5)); /* Check if the interrupt is really pending */ - regval = sam34_getreg(priv->base + SAM_TC_SR_OFFSET); - if ((regval & TC_INT_CPCS) != 0) + if ((sam34_getreg(priv->base + SAM_TC_SR_OFFSET) & TC_INT_CPCS) != 0) { uint32_t timeout; @@ -283,7 +280,7 @@ static int sam34_interrupt(int irq, FAR void *context) if (priv->handler && priv->handler(&priv->timeout)) { - /* Calculate new ticks */ + /* Calculate new ticks / dither adjustment */ priv->clkticks = ((uint64_t)(priv->adjustment + priv->timeout))*TC_FCLK / 1000000; @@ -323,7 +320,7 @@ static int sam34_interrupt(int irq, FAR void *context) 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; + uint32_t mr_val; tcvdbg("Entry\n"); DEBUGASSERT(priv); @@ -335,13 +332,14 @@ static int sam34_start(FAR struct timer_lowerhalf_s *lower) sam_enableperiph0(priv->periphid); /* Enable peripheral clock */ sam34_putreg(TC_CCR_CLKDIS, priv->base + SAM_TC_CCR_OFFSET); /* Disable counter */ + sam34_putreg(0, priv->base + SAM_TC_CV_OFFSET); /* clear 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); + mr_val = (TC_CMR_WAVE + TC_CMR_WAVSEL_UPAUTO + TC_CMR_TCCLKS_TIMERCLOCK5); sam34_putreg(mr_val, priv->base + SAM_TC_CMR_OFFSET); sam34_putreg(priv->clkticks, priv->base + SAM_TC_RC_OFFSET); /* Set interval */ @@ -381,7 +379,7 @@ static int sam34_stop(FAR struct timer_lowerhalf_s *lower) tcvdbg("Entry\n"); DEBUGASSERT(priv); - if(!priv->started) + if (!priv->started) { return -EINVAL; } @@ -430,7 +428,7 @@ static int sam34_getstatus(FAR struct timer_lowerhalf_s *lower, if (priv->handler) { - status->flags |= TCFLAGS_CAPTURE; + status->flags |= TCFLAGS_HANDLER; } /* Return the actual timeout is milliseconds */ @@ -440,7 +438,7 @@ static int sam34_getstatus(FAR struct timer_lowerhalf_s *lower, /* Get the time remaining until the timer expires (in microseconds) */ elapsed = sam34_getreg(priv->base + SAM_TC_CV_OFFSET); - status->timeleft = (priv->timeout * elapsed) / (priv->clkticks + 1); /* TODO - check on this +1 */ + status->timeleft = ((uint64_t)priv->timeout * elapsed) / (priv->clkticks + 1); /* TODO - check on this +1 */ tcvdbg(" flags : %08x\n", status->flags); tcvdbg(" timeout : %d\n", status->timeout); @@ -470,6 +468,12 @@ static int sam34_settimeout(FAR struct timer_lowerhalf_s *lower, FAR struct sam34_lowerhalf_s *priv = (FAR struct sam34_lowerhalf_s *)lower; DEBUGASSERT(priv); + + if (priv->started) + { + return -EPERM; + } + tcvdbg("Entry: timeout=%d\n", timeout); /* Can this timeout be represented? */ @@ -486,21 +490,17 @@ static int sam34_settimeout(FAR struct timer_lowerhalf_s *lower, timeout = (1000000ULL * priv->clkticks) / TC_FCLK; /* Truncated timeout */ priv->adjustment = priv->timeout - timeout; /* Truncated time to be added to next interval (dither) */ - tcvdbg("fwdt=%d reload=%d timout=%d, adjustment=%d\n", - TC_FCLK, reload, priv->timeout, priv->adjustment); - - /* Don't commit to MR register until started! */ + tcvdbg("fclk=%d clkticks=%d timout=%d, adjustment=%d\n", + TC_FCLK, priv->clkticks, priv->timeout, priv->adjustment); return OK; } /**************************************************************************** - * Name: sam34_capture + * Name: sam34_sethandler * * Description: - * Don't reset on timer timeout; instead, call this user provider - * timeout handler. NOTE: Providing handler==NULL will restore the reset - * behavior. + * Call this user provided timeout handler. * * Input Parameters: * lower - A pointer the publicly visible representation of the "lower-half" @@ -511,13 +511,12 @@ static int sam34_settimeout(FAR struct timer_lowerhalf_s *lower, * * 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). + * no previous function pointer. * ****************************************************************************/ -static tccb_t sam34_capture(FAR struct timer_lowerhalf_s *lower, - tccb_t handler) +static tccb_t sam34_sethandler(FAR struct timer_lowerhalf_s *lower, + tccb_t handler) { FAR struct sam34_lowerhalf_s *priv = (FAR struct sam34_lowerhalf_s *)lower; irqstate_t flags;