arch/arm/src/sam34/sam4cm_oneshot.c
/****************************************************************************
 * Name: sam_oneshot_start
 *
 * Description:
 *   Start the oneshot timer
 *
 * Input Parameters:
 *   oneshot Caller allocated instance of the oneshot state structure.  This
 *           structure must have been previously initialized via a call to
 *           sam_oneshot_initialize();
 *   handler The function to call when when the oneshot timer expires.
 *   arg     An opaque argument that will accompany the callback.
 *   ts      Provides the duration of the one shot timer.
 *
 * Returned Value:
 *   Zero (OK) is returned on success; a negated errno value is returned
 *   on failure.
 *
 ****************************************************************************/
sam_oneshot_start
Description:
Start the oneshot timer

Input Parameters:
oneshot Caller allocated instance of the oneshot state structure.  This
structure must have been previously initialized via a call to
sam_oneshot_initialize();
handler The function to call when when the oneshot timer expires.
arg     An opaque argument that will accompany the callback.
ts      Provides the duration of the one shot timer.

Returned Value:
Zero (OK) is returned on success; a negated errno value is returned
on failure.
sam_oneshot_start Description : Start the oneshot timer Input Parameters : oneshot Caller allocated instance of the oneshot state structure . This structure must have been previously initialized via a call to sam_oneshot_initialize () ; handler The function to call when when the oneshot timer expires . arg An opaque argument that will accompany the callback . ts Provides the duration of the one shot timer . Returned Value : Zero ( OK ) is returned on success ; a negated errno value is returned on failure .
int sam_oneshot_start(struct sam_oneshot_s *oneshot, oneshot_handler_t handler,
                      void *arg, const struct timespec *ts)
{
  uint64_t usec;
  uint64_t regval;
  irqstate_t flags;
  tcvdbg("handler=%p arg=%p, ts=(%lu, %lu)\n",
         handler, arg, (unsigned long)ts->tv_sec, (unsigned long)ts->tv_nsec);
  DEBUGASSERT(oneshot && handler && ts);
  flags = irqsave();
  if (oneshot->running)
    {
      tcvdbg("Already running... cancelling\n");
      (void)sam_oneshot_cancel(oneshot, NULL);
    }
  oneshot->handler = handler;
  oneshot->arg     = arg;
  usec = (uint64_t)ts->tv_sec * USEC_PER_SEC + (uint64_t)(ts->tv_nsec / NSEC_PER_USEC);
  regval = (usec * (uint64_t)sam_tc_divfreq(oneshot->tch)) / USEC_PER_SEC;
  tcvdbg("usec=%llu regval=%08llx\n", usec, regval);
  DEBUGASSERT(regval <= UINT16_MAX);
  (void)sam_tc_attach(oneshot->tch, sam_oneshot_handler, oneshot,
                      TC_INT_CPCS);
  sam_tc_setregister(oneshot->tch, TC_REGC, (uint32_t)regval);
  sam_tc_start(oneshot->tch);
  oneshot->running = true;
  irqrestore(flags);
  return OK;
}
int sam_oneshot_start ( struct sam_oneshot_s * oneshot , oneshot_handler_t handler , void * arg , const struct timespec * ts ) { uint64_t usec ; uint64_t regval ; irqstate_t flags ; tcvdbg ( " \n " , handler , arg , ( unsigned long ) ts -> tv_sec , ( unsigned long ) ts -> tv_nsec ) ; DEBUGASSERT ( oneshot && handler && ts ) ; flags = irqsave ( ) ; if ( oneshot -> running ) { tcvdbg ( " \n " ) ; ( void ) sam_oneshot_cancel ( oneshot , NULL ) ; } oneshot -> handler = handler ; oneshot -> arg = arg ; usec = ( uint64_t ) ts -> tv_sec * USEC_PER_SEC + ( uint64_t ) ( ts -> tv_nsec / NSEC_PER_USEC ) ; regval = ( usec * ( uint64_t ) sam_tc_divfreq ( oneshot -> tch ) ) / USEC_PER_SEC ; tcvdbg ( " \n " , usec , regval ) ; DEBUGASSERT ( regval <= UINT16_MAX ) ; ( void ) sam_tc_attach ( oneshot -> tch , sam_oneshot_handler , oneshot , TC_INT_CPCS ) ; sam_tc_setregister ( oneshot -> tch , TC_REGC , ( uint32_t ) regval ) ; sam_tc_start ( oneshot -> tch ) ; oneshot -> running = true ; irqrestore ( flags ) ; return OK ; }
Name: sam_oneshot_start
 Description:
   Start the oneshot timer
Name : sam_oneshot_start Description : Start the oneshot timer
/* Was the oneshot already running? */ /* Yes.. then cancel it */ /* Save the new handler and its argument */ /* Express the delay in microseconds */ /* Get the timer counter frequency and determine the number of counts need to achieve the requested delay.
   *
   *   frequency = ticks / second
   *   ticks     = seconds * frequency
   *             = (usecs * frequency) / USEC_PER_SEC;
   */ /* Set up to receive the callback when the interrupt occurs */ /* Set RC so that an event will be triggered when TC_CV register counts
   * up to RC.
   */ /* Start the counter */ /* Enable interrupts.  We should get the callback when the interrupt
   * occurs.
   */