arch/arm/src/sam34/sam4cm_oneshot.c
/****************************************************************************
 * Name: sam_oneshot_initialize
 *
 * Description:
 *   Initialize the oneshot timer wrapper
 *
 * Input Parameters:
 *   oneshot    Caller allocated instance of the oneshot state structure
 *   chan       Timer counter channel to be used.  See the TC_CHAN*
 *              definitions in arch/arm/src/sam34/sam_tc.h.
 *   resolution The required resolution of the timer in units of
 *              microseconds.  NOTE that the range is restricted to the
 *              range of uint16_t (excluding zero).
 *
 * Returned Value:
 *   Zero (OK) is returned on success; a negated errno value is returned
 *   on failure.
 *
 ****************************************************************************/
sam_oneshot_initialize
Description:
Initialize the oneshot timer wrapper

Input Parameters:
oneshot    Caller allocated instance of the oneshot state structure
chan       Timer counter channel to be used.

Returned Value:
Zero (OK) is returned on success; a negated errno value is returned
on failure.
sam_oneshot_initialize Description : Initialize the oneshot timer wrapper Input Parameters : oneshot Caller allocated instance of the oneshot state structure chan Timer counter channel to be used . Returned Value : Zero ( OK ) is returned on success ; a negated errno value is returned on failure .
int sam_oneshot_initialize(struct sam_oneshot_s *oneshot, int chan,
                           uint16_t resolution)
{
  uint32_t frequency;
  uint32_t divisor;
  uint32_t cmr;
  int ret;
  tcvdbg("chan=%d resolution=%d usec\n", chan, resolution);
  DEBUGASSERT(oneshot && resolution > 0);
  frequency = USEC_PER_SEC / (uint32_t)resolution;
  ret = sam_tc_divisor(frequency, &divisor, &cmr);
  if (ret < 0)
    {
      tcdbg("ERROR: sam_tc_divisor failed: %d\n", ret);
      return ret;
    }
  tcvdbg("frequency=%lu, divisor=%lu, cmr=%08lx\n",
         (unsigned long)frequency, (unsigned long)divisor,
         (unsigned long)cmr);
  cmr |= (TC_CMR_BURST_NOTGATED | TC_CMR_CPCSTOP       | TC_CMR_EEVTEDG_NONE |
          TC_CMR_EEVT_TIOB      | TC_CMR_WAVSEL_UPAUTO | TC_CMR_WAVE         |
          TC_CMR_ACPA_NONE      | TC_CMR_ACPC_NONE     | TC_CMR_AEEVT_NONE   |
          TC_CMR_ASWTRG_NONE    | TC_CMR_BCPB_NONE     | TC_CMR_BCPC_NONE    |
          TC_CMR_BEEVT_NONE     | TC_CMR_BSWTRG_NONE);
  oneshot->tch = sam_tc_allocate(chan, cmr);
  if (!oneshot->tch)
    {
      tcdbg("ERROR: Failed to allocate timer channel %d\n", chan);
      return -EBUSY;
    }
  oneshot->chan       = chan;
  oneshot->running    = false;
  oneshot->handler    = NULL;
  oneshot->arg        = NULL;
  return OK;
}
int sam_oneshot_initialize ( struct sam_oneshot_s * oneshot , int chan , uint16_t resolution ) { uint32_t frequency ; uint32_t divisor ; uint32_t cmr ; int ret ; tcvdbg ( " \n " , chan , resolution ) ; DEBUGASSERT ( oneshot && resolution > 0 ) ; frequency = USEC_PER_SEC / ( uint32_t ) resolution ; ret = sam_tc_divisor ( frequency , & divisor , & cmr ) ; if ( ret < 0 ) { tcdbg ( " \n " , ret ) ; return ret ; } tcvdbg ( " \n " , ( unsigned long ) frequency , ( unsigned long ) divisor , ( unsigned long ) cmr ) ; cmr |= ( TC_CMR_BURST_NOTGATED | TC_CMR_CPCSTOP | TC_CMR_EEVTEDG_NONE | TC_CMR_EEVT_TIOB | TC_CMR_WAVSEL_UPAUTO | TC_CMR_WAVE | TC_CMR_ACPA_NONE | TC_CMR_ACPC_NONE | TC_CMR_AEEVT_NONE | TC_CMR_ASWTRG_NONE | TC_CMR_BCPB_NONE | TC_CMR_BCPC_NONE | TC_CMR_BEEVT_NONE | TC_CMR_BSWTRG_NONE ) ; oneshot -> tch = sam_tc_allocate ( chan , cmr ) ; if ( ! oneshot -> tch ) { tcdbg ( " \n " , chan ) ; return - EBUSY ; } oneshot -> chan = chan ; oneshot -> running = false ; oneshot -> handler = NULL ; oneshot -> arg = NULL ; return OK ; }
Name: sam_oneshot_initialize
 Description:
   Initialize the oneshot timer wrapper
Name : sam_oneshot_initialize Description : Initialize the oneshot timer wrapper
/* Get the TC frequency the corresponds to the requested resolution */ /* The pre-calculate values to use when we start the timer */ /* Allocate the timer/counter and select its mode of operation
   *
   *   CMR_TCCLKS          - Returned by sam_tc_divisor
   *   TC_CMR_CLKI=0       - Not inverted
   *   TC_CMR_BURST_NONE   - Not gated by an external signal
   *   TC_CMR_CPCSTOP=1    - Stop the clock on an RC compare event
   *   TC_CMR_CPCDIS=0     - Don't disable the clock on an RC compare event
   *   TC_CMR_EEVTEDG_NONE - No external events (and, hence, no edges
   *   TC_CMR_EEVT_TIOB    - ???? REVISIT
   *   TC_CMR_ENET=0       - External event trigger disabled
   *   TC_CMR_WAVSEL_UPRC  - TC_CV is incremented from 0 to the value of RC,
   *                         then automatically reset on a RC Compare
   *   TC_CMR_WAVE         - Waveform mode
   *   TC_CMR_ACPA_NONE    - RA compare has no effect on TIOA
   *   TC_CMR_ACPC_NONE    - RC compare has no effect on TIOA
   *   TC_CMR_AEEVT_NONE   - No external event effect on TIOA
   *   TC_CMR_ASWTRG_NONE  - No software trigger effect on TIOA
   *   TC_CMR_BCPB_NONE    - RB compare has no effect on TIOB
   *   TC_CMR_BCPC_NONE    - RC compare has no effect on TIOB
   *   TC_CMR_BEEVT_NONE   - No external event effect on TIOB
   *   TC_CMR_BSWTRG_NONE  - No software trigger effect on TIOB
   */ /* Initialize the remaining fields in the state structure and return
   * success.
   */