int sam_freerun_initialize(struct sam_freerun_s *freerun, int chan, uint16_t resolution) { uint32_t frequency; uint32_t divisor; uint32_t cmr; int ret; tmrinfo("chan=%d resolution=%d usec\n", chan, resolution); DEBUGASSERT(freerun && resolution > 0); /* Get the TC frequency the corresponds to the requested resolution */ frequency = USEC_PER_SEC / (uint32_t)resolution; /* The pre-calculate values to use when we start the timer */ ret = sam_tc_divisor(frequency, &divisor, &cmr); if (ret < 0) { tmrerr("ERROR: sam_tc_divisor failed: %d\n", ret); return ret; } tmrinfo("frequency=%lu, divisor=%u, cmr=%08lx\n", (unsigned long)frequency, (unsigned long)divisor, (unsigned long)cmr); /* 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=0 - Don't 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_UP - TC_CV is incremented from 0 to 0xffffffff * 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 */ cmr |= (TC_CMR_BURST_NOTGATED | TC_CMR_EEVTEDG_NONE | TC_CMR_EEVT_TIOB | TC_CMR_WAVSEL_UP | 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); freerun->tch = sam_tc_allocate(chan, cmr); if (!freerun->tch) { tmrerr("ERROR: Failed to allocate timer channel %d\n", chan); return -EBUSY; } /* Initialize the remaining fields in the state structure and return * success. */ freerun->chan = chan; freerun->running = false; freerun->overflow = 0; /* Set up to receive the callback when the counter overflow occurs */ (void)sam_tc_attach(freerun->tch, sam_freerun_handler, freerun, TC_INT_COVFS); /* Start the counter */ sam_tc_start(freerun->tch); return OK; }
int sam_oneshot_initialize(struct sam_oneshot_s *oneshot, int chan, uint16_t resolution) { uint32_t frequency; uint32_t actual; uint32_t cmr; int ret; tmrinfo("chan=%d resolution=%d usec\n", chan, resolution); DEBUGASSERT(oneshot && resolution > 0); /* Get the TC frequency the corresponds to the requested resolution */ frequency = USEC_PER_SEC / (uint32_t)resolution; /* The pre-calculate values to use when we start the timer */ ret = sam_tc_clockselect(frequency, &cmr, &actual); if (ret < 0) { tmrerr("ERROR: sam_tc_clockselect failed: %d\n", ret); return ret; } tmrinfo("frequency=%lu, actual=%lu, cmr=%08lx\n", (unsigned long)frequency, (unsigned long)actual, (unsigned long)cmr); /* Allocate the timer/counter and select its mode of operation * * TC_CMR_TCCLKS - Returned by sam_tc_clockselect * 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 */ cmr |= (TC_CMR_BURST_NONE | TC_CMR_CPCSTOP | TC_CMR_EEVTEDG_NONE | TC_CMR_EEVT_TIOB | TC_CMR_WAVSEL_UPRC | 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) { tmrerr("ERROR: Failed to allocate timer channel %d\n", chan); return -EBUSY; } /* Initialize the remaining fields in the state structure and return * success. */ oneshot->chan = chan; oneshot->running = false; oneshot->handler = NULL; oneshot->arg = NULL; oneshot->start_count = 0; return OK; }