⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 timer.c

📁 COLDFIRE 5282SC的文件
💻 C
字号:
/*
 * File:		timer.c
 * Purpose:		routines for accessing integrated timer modules
 *
 * Notes:
 *
 * Modifications:
 *
 */

#include "src/init/m5282evb.h"
#include "src/ethernet/tftp/timer.h"

/********************************************************************/

/* The MCF5282 has four general purpose timer modules */
mcf5282_timer timer[4] = \
	{
		{0, 0, 0, 0, 0, 0, 0, 0, 0}, 
		{0, 0, 0, 0, 0, 0, 0, 0, 0}, 
		{0, 0, 0, 0, 0, 0, 0, 0, 0}, 
		{0, 0, 0, 0, 0, 0, 0, 0, 0}
	};

const uint8 TIMER_BENCHMARK = 0;
const uint8 TIMER_NETWORK = 1;

/********************************************************************/
static uint32
timer_read(uint8 channel)
{
	uint32 now;

	now = MCF5282_TIMER_DTCN(channel);
	timer[channel].then = timer[channel].now;
	timer[channel].now = now;
	return now;
}

/********************************************************************/
static void
timer_start(uint8 channel)
{
	/* Reset Timer module */
	MCF5282_TIMER_DTMR(channel) = 0;

	/* Clear any pending Timer events */
	MCF5282_TIMER_DTER(channel) = (0
		| MCF5282_TIMER_DTER_REF
		| MCF5282_TIMER_DTER_CAP);
	
	/* Set Timer Interrupt Control Register */
	MCF5282_INTC0_ICR20 = (uint8)(0
		| MCF5282_INTC_ICR_IL(timer[channel].level));
		
	/* Allow Timer 1 Interrupts */	
	MCF5282_INTC0_IMRL &= 	~(MCF5282_INTC_IMRL_INT20 
							| MCF5282_INTC_IMRL_MASKALL);

	/* Reset the counter */
	MCF5282_TIMER_DTCN(channel) = 0;

	/* Write the TRR register */
	MCF5282_TIMER_DTRR(channel) = timer[channel].trr;

	/* Write the TMR register and start the free-running timer */
	MCF5282_TIMER_DTMR(channel) = timer[channel].tmr;
}

/********************************************************************/
static void
timer_stop(uint8 channel)
{
	/* Get the latest time */
	timer_read(channel);

	/* Disable timer (reset) */
	MCF5282_TIMER_DTMR(channel) = 0;
}

/********************************************************************/
uint32
timer_set_secs(uint8 channel, uint32 secs)
{
	uint32 timeout;

	/* Get the timeout in seconds */
	timeout = (uint32)(((timer[channel].period * 0x10000)/1000000000) + 0.5);

	if (timeout == 0)
	{
		timer[channel].reference = 1;
		return FALSE;
	}

	/* Save the reference reached counter value */
	timer[channel].reference = secs/timeout;

	/* Reset the timeout counter */
	timer[channel].timeouts = 0;

	/* Start the timer */
	timer_start(channel);

	return TRUE;
}

/********************************************************************/
uint32
timer_get_reference(uint8 channel)
{	
	return timer[channel].reference;
}

/********************************************************************/
static uint32
timer_get_time(uint8 channel)
{
	/* NOT COMPLETE!  FIX */
	return (timer[channel].now - timer[channel].then);
}

/********************************************************************/
uint32
timer_default_isr(mcf5282_timer *t)
{
	/* Clear the pending event */
	MCF5282_TIMER_DTER(t->channel) = (0
		| MCF5282_TIMER_DTER_REF
		| MCF5282_TIMER_DTER_CAP);

	t->timeouts++;
	if (t->reference)
		if (--t->reference == 0)
			timer_stop(t->channel);

	return TRUE;
}

/********************************************************************/
uint32
timer_init(uint8 channel, float period, float sysclock, uint32 level)
{
	uint16 prescale;

	if ((channel > 3) || (level < 1) || (level > 7))
		return FALSE;

	/* Set the Reference Reached value to the maximum value */
	timer[channel].trr = 0xFFFF;

	/* Store TMR value */
	timer[channel].tmr = (MCF5282_TIMER_DTMR_ORRI
						| MCF5282_TIMER_DTMR_CE_NONE
						| MCF5282_TIMER_DTMR_OM
						| MCF5282_TIMER_DTMR_FRR
						| MCF5282_TIMER_DTMR_RST);

	if (period < (1000/sysclock)) 
	{
		period = 1000/sysclock;
		timer[channel].tmr |= (MCF5282_TIMER_DTMR_CLK_DIV1 
							 | MCF5282_TIMER_DTMR_PS(0));
	}
	else if (period > (1000 * 256 * 16)/sysclock)
	{
		period = (1000 * 256 * 16)/sysclock;
		timer[channel].tmr |= (MCF5282_TIMER_DTMR_CLK_DIV16 
							 | MCF5282_TIMER_DTMR_PS(256));
	}
	else if (period <= (1000 * 256)/sysclock)
	{
		prescale = (uint16)((period * sysclock)/1000);
		timer[channel].tmr |= (MCF5282_TIMER_DTMR_CLK_DIV1 
							 | MCF5282_TIMER_DTMR_PS(prescale));
	}
	else /* period <= (1000 * 256 * 16)/sysclock */
	{
		prescale = (uint16)((period * sysclock)/(1000 * 16));
		timer[channel].tmr |= (MCF5282_TIMER_DTMR_CLK_DIV16 
							 | MCF5282_TIMER_DTMR_PS(prescale));
	}

	/* Save the Timer settings */
	timer[channel].period = period;
	timer[channel].channel = channel;
	timer[channel].level = level;

	return TRUE;
}

/********************************************************************/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -