/*! \brief  to initialiaze hw timer
 */
uint8_t tmr_init(void)
{
    uint8_t tmr_mul;
    /* Configure clock service. */
#if SAM4L
    sysclk_enable_peripheral_clock(TIMER);
#else
    sysclk_enable_peripheral_clock(ID_TC);
#endif

    /* Get system clock. */
    tmr_mul = sysclk_get_peripheral_bus_hz(TIMER) / DEF_1MHZ;
    tmr_mul = tmr_mul >> 1;
#if SAM4L
    tc_init(TIMER, TIMER_CHANNEL_ID,
            TC_CMR_TCCLKS_TIMER_CLOCK2 | TC_CMR_WAVE |
            TC_CMR_WAVSEL_UP_NO_AUTO);
#elif SAM4E
    tc_init(TIMER, TIMER_CHANNEL_ID,
            TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_WAVE |
            TC_CMR_WAVSEL_UP_RC);
#else
    tc_init(TIMER, TIMER_CHANNEL_ID,
            TC_CMR_TCCLKS_TIMER_CLOCK1 | TC_CMR_WAVE |
            TC_CMR_WAVSEL_UP);
#endif

    /* Configure and enable interrupt on RC compare. */
    configure_NVIC(TIMER, TIMER_CHANNEL_ID);
#if SAM4E
    tc_get_status(TIMER, TIMER_CHANNEL_ID);
    tc_enable_interrupt(TIMER, TIMER_CHANNEL_ID, TC_IER_CPCS);
    tc_write_rc(TIMER, TIMER_CHANNEL_ID, UINT16_MAX);
#else
    tc_get_status(TIMER, TIMER_CHANNEL_ID);
    tc_enable_interrupt(TIMER, TIMER_CHANNEL_ID, TC_IER_COVFS);
#endif
    tmr_disable_cc_interrupt();
    tc_start(TIMER, TIMER_CHANNEL_ID);
    return tmr_mul;
}
Exemple #2
0
/*! \brief  to initialiaze hw timer
 */
uint8_t tmr_init(void)
{
	uint8_t timer_multiplier;
	/* Options for waveform generation. */
	tc_waveform_opt_t waveform_opt = {
		.channel  = TIMER_CHANNEL_ID,    /* Channel selection. */

		.bswtrg   = TC_EVT_EFFECT_NOOP,       /* Software trigger effect
		                                       * on TIOB. */
		.beevt    = TC_EVT_EFFECT_NOOP,       /* External event effect
		                                       * on TIOB. */
		.bcpc     = TC_EVT_EFFECT_NOOP,       /* RC compare effect on
		                                       * TIOB. */
		.bcpb     = TC_EVT_EFFECT_NOOP,       /* RB compare effect on
		                                       * TIOB. */

		.aswtrg   = TC_EVT_EFFECT_NOOP,       /* Software trigger effect
		                                       * on TIOA. */
		.aeevt    = TC_EVT_EFFECT_NOOP,       /* External event effect
		                                       * on TIOA. */
		.acpc     = TC_EVT_EFFECT_NOOP,       /* RC compare effect on
		                                       * TIOA */
		.acpa     = TC_EVT_EFFECT_NOOP,       /* RA compare effect on
		                                       * TIOA */

		.wavsel   = TC_WAVEFORM_SEL_UP_MODE,  /* Waveform selection: Up
		                                       * mode without automatic
		                                       * trigger on RC compare.
		                                       **/
		.enetrg   = false,                    /* External event trigger
		                                       * enable. */
		.eevt     = TC_EXT_EVENT_SEL_TIOB_INPUT, /* External event
		                                          * selection. */
		.eevtedg  = TC_SEL_NO_EDGE,           /* External event edge
		                                       * selection. */
		.cpcdis   = false,                    /* Counter disable when RC
		                                       * compare. */
		.cpcstop  = false,                    /* Counter clock stopped
		                                       * with RC compare. */

		.burst    = TC_BURST_NOT_GATED,       /* Burst signal selection.
		                                      **/
		.clki     = TC_CLOCK_RISING_EDGE,     /* Clock inversion. */
		.tcclks   = TC_CLOCK_SOURCE_TC2       /* Internal source clock
		                                       * 3, connected to fPBA /
		                                       * 8. */
	};

	sysclk_enable_peripheral_clock(TIMER);
	/* Initialize the timer/counter. */
	tc_init_waveform(TIMER, &waveform_opt);

	/* calculate how faster the timer with current clk freq compared to
	 * timer with 1Mhz */
	timer_multiplier = sysclk_get_peripheral_bus_hz(TIMER) / DEF_1MHZ;
	/* */
	timer_multiplier = timer_multiplier >> 1;

	configure_irq_handler();

	tmr_enable_ovf_interrupt();

	tmr_disable_cc_interrupt();
	tc_start(TIMER, TIMER_CHANNEL_ID);
	return timer_multiplier;
}

/*! \brief  to disable compare interrupt
 */
void tmr_disable_cc_interrupt(void)
{
	tc_interrupt.cpcs = 0;
	tc_configure_interrupts(TIMER, TIMER_CHANNEL_ID, &tc_interrupt);
}