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

📄 timerasyncm.nc.svn-base

📁 802.15.4协议的实现
💻 SVN-BASE
字号:
/*
 * @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
 * @author Andre Cunha
 *
 */

//CPUCLK = 7.3728MHz
#define TCLK_CPU_DIV1 1	 	
//CPUCLK/8 = 921,6 KHz (~1,085 us intervals)
#define TCLK_CPU_DIV8 2	
//CPUCLK/64 = 115,2 KHz (~8,681 us intervals)
#define TCLK_CPU_DIV64 3	
//CPUCLK/256 = 28,8 KHz (~34,722 us intervals)
#define TCLK_CPU_DIV256 4
//CPUCLK/1024 = 7,2 KHz (~138,889 us intervals)
#define TCLK_CPU_DIV1024 5	



#define  SCALE TCLK_CPU_DIV256
#define  INTERVAL 1

#define BEFORE_BI_INTERVAL 12
#define BEFORE_BB_INTERVAL 12


includes PrintfUART;

module TimerAsyncM
{
	provides interface TimerAsync;
	
	uses {
		interface Clock as Timer;
	 	interface Leds;			
	}
			
}

implementation
{

uint32_t ticks_counter;


uint32_t bi_ticks;
uint32_t sd_ticks;


uint32_t b_ticks;
uint32_t bb_ticks;

uint32_t before_bi_ticks;

uint32_t before_bb_ticks;



bool backoffs=0;

bool enable_backoffs=0;

bool sd_fired;

uint32_t b_tick_next_fire;
uint32_t bb_tick_next_fire;

/*****************************INIT START STOP FUNCTIONS*******************************/
async command result_t TimerAsync.start()
{
	
	call Timer.setIntervalAndScale(INTERVAL, SCALE);  //sets timer, starts and enables interrupt
	
	return SUCCESS;
}

command result_t TimerAsync.init()
{
	
	return SUCCESS;
}	

async command result_t TimerAsync.stop()
{
	call Timer.intDisable();
	return SUCCESS;
}

/*RESET the tick counter, */
async command result_t TimerAsync.reset()
{
	atomic ticks_counter = 0;
	
	return SUCCESS;
}

/*RESET the tick counter, to the start ticks */
async command result_t TimerAsync.reset_start(uint32_t start_ticks)
{
	atomic ticks_counter = start_ticks;
	return SUCCESS;
}

async event result_t Timer.fire() {

	atomic{
		ticks_counter++;

		
		if (ticks_counter == sd_ticks)	
		{
			signal TimerAsync.sd_fired();
			backoffs=0;
			
			//printfUART("SD:%i\n", sd_ticks) ;
		}
		
		if (ticks_counter == before_bi_ticks)
		{
			signal TimerAsync.before_bi_fired();
		} 
		
		
		if (ticks_counter == bi_ticks)
		{
			ticks_counter=0;
			b_tick_next_fire=b_ticks;
			bb_tick_next_fire=bb_ticks;
			backoffs=1;
			signal TimerAsync.bi_fired();
			
			//printfUART("ticks_counter: %i\n", ticks_counter) ;
			//printfUART("BI:%i\n", bi_ticks) ;
		}
		
		if (enable_backoffs == 1 && backoffs == 1)
		{	
			//backoff
			if(ticks_counter == b_tick_next_fire)
			{
				b_tick_next_fire = ticks_counter + b_ticks;
				
				signal TimerAsync.b_fired();
			}
			
			//before  backoff boundary 
			if(ticks_counter == before_bb_ticks)
			{
				signal TimerAsync.before_bb_fired();
			}
			
			//boundary backoffs
			if (ticks_counter == bb_tick_next_fire)
			{
				bb_tick_next_fire = ticks_counter + bb_ticks;
				before_bb_ticks = bb_tick_next_fire - BEFORE_BB_INTERVAL;
				
				signal TimerAsync.bb_fired();
			}
		}
		
		
		
	}
	return SUCCESS;
}

/***********************************SET COMMANDS******************************/

/*set the number of ticks for the sd_fired event*/ 
async command result_t TimerAsync.set_sd_symbols(uint32_t symbols)
{
	atomic sd_ticks = symbols / 2;
	return SUCCESS;
}	
 
async command result_t TimerAsync.set_bi_symbols(uint32_t symbols)
{
	atomic{
		bi_ticks = symbols / 2;
		before_bi_ticks = bi_ticks - BEFORE_BI_INTERVAL;
	}
	return SUCCESS;
}	

async command result_t TimerAsync.set_bb_symbols(uint32_t symbols)
{
	atomic{
		bb_ticks = symbols / 2;
		before_bb_ticks = bb_ticks - BEFORE_BB_INTERVAL;
	}
	return SUCCESS;
}
  
async command result_t TimerAsync.set_b_symbols(uint32_t symbols)
{
	atomic b_ticks = symbols / 2;
	return SUCCESS;
} 

async command result_t TimerAsync.set_enable_backoffs(bool enable)
{
	atomic enable_backoffs = enable;
	return SUCCESS;
}

/***********************************GET COMMANDS******************************/
/*get current clock ticks*/
async command uint32_t TimerAsync.get_current_ticks()
{
	return ticks_counter;
}
/*get current sd ticks*/  
async command uint32_t TimerAsync.get_sd_ticks()
{
	return sd_ticks;
}	
/*get current clock ticks*/
async command uint32_t TimerAsync.get_bi_ticks()
{
	return bi_ticks;
}	
/*get current backoff ticks*/
async command uint32_t TimerAsync.get_b_ticks()
{
return b_ticks;
}	
/*get current backoff boundaries ticks*/
async command uint32_t TimerAsync.get_bb_ticks()
{
	return bb_ticks;
}	


}



⌨️ 快捷键说明

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