📄 timerasyncm.nc
字号:
/*
* @author IPP HURRAY http://www.hurray.isep.ipp.pt/art-wise
* @author Andre Cunha
*
*/
//TIMER ASYNC TELOSB
#define BEFORE_BI_INTERVAL 100
#define BEFORE_BB_INTERVAL 5
#define SO_EQUAL_BO_DIFFERENCE 2
//#define SYMBOL_DIVISION 4
//temporary
#define NUMBER_TIME_SLOTS 16
includes PrintfUART;
module TimerAsyncM
{
provides interface StdControl;
provides interface TimerAsync;
uses interface MSP430TimerControl as AlarmControl;
uses interface MSP430Compare as AlarmCompare;
uses interface Leds;
uses interface HPLCC2420Capture as SFD;
}
implementation
{
uint32_t ticks_counter;
//BEACON INTERVAL VARIABLES
uint32_t bi_ticks;
uint32_t bi_backoff_periods;
uint32_t before_bi_ticks;
uint32_t sd_ticks;
//number of backoff periods
uint32_t time_slot_backoff_periods;
//number of ticks in the timeslot
uint32_t time_slot_ticks;
uint32_t before_time_slot_ticks;
uint32_t time_slot_tick_next_fire;
//BACKOFF VARIABLES
uint32_t backoff_symbols;
//number of ticks in the backoff
uint32_t backoff_ticks = 5;
//COUNTER VARIABLES
uint32_t backoff_ticks_counter=0;
//give the current time slot number
uint8_t current_time_slot=0;
//counts the current number of time slots of each time slot
uint32_t current_number_backoff_on_time_slot=0;
//count the total number of backoffs
uint32_t current_number_backoff = 0;
//OTHER
bool backoffs=0;
bool enable_backoffs=0;
uint8_t previous_sfd=0;
uint8_t current_sfd = 0;
uint32_t process_frame_tick_counter=0;
uint32_t total_tick_counter=0;
uint8_t timers_enable=0x01;
/*****************************INIT START STOP FUNCTIONS*******************************/
command result_t StdControl.start()
{
atomic {
call AlarmControl.disableEvents();
}
return SUCCESS;
}
command result_t StdControl.init()
{
call AlarmControl.setControlAsCompare();
call AlarmControl.disableEvents();
return SUCCESS;
}
command result_t StdControl.stop()
{
atomic {
call AlarmControl.disableEvents();
}
return SUCCESS;
}
async command result_t TimerAsync.start()
{
call AlarmCompare.setEventFromNow(10);
call AlarmControl.clearPendingInterrupt();
call AlarmControl.enableEvents();
return SUCCESS;
}
async command result_t TimerAsync.stop()
{
atomic {
call AlarmControl.disableEvents();
}
return SUCCESS;
}
/*RESET the tick counter, */
async command result_t TimerAsync.reset()
{
atomic ticks_counter = 0;
return SUCCESS;
}
async command result_t TimerAsync.set_bi_sd(uint32_t bi_symbols,uint32_t sd_symbols)
{
atomic{
time_slot_backoff_periods = (sd_symbols / NUMBER_TIME_SLOTS) / backoff_symbols;
time_slot_ticks = time_slot_backoff_periods * backoff_ticks;
time_slot_tick_next_fire = time_slot_ticks;
before_time_slot_ticks = time_slot_ticks - BEFORE_BB_INTERVAL;
sd_ticks = time_slot_ticks * NUMBER_TIME_SLOTS;
if (bi_symbols == sd_symbols )
{
//in order not to have the same time for both BI and SI
sd_ticks = sd_ticks - SO_EQUAL_BO_DIFFERENCE;
}
bi_backoff_periods = bi_symbols/ backoff_symbols;
bi_ticks = bi_backoff_periods * backoff_ticks;
before_bi_ticks = bi_ticks - BEFORE_BI_INTERVAL;
}
return SUCCESS;
}
async command result_t TimerAsync.set_backoff_symbols(uint8_t Backoff_Duration_Symbols)
{
atomic
{
backoff_symbols = Backoff_Duration_Symbols;
backoff_ticks = 1;
}
return SUCCESS;
}
async command result_t TimerAsync.set_enable_backoffs(bool enable)
{
atomic enable_backoffs = enable;
return SUCCESS;
}
async event result_t SFD.captured(uint16_t time) {
//call Leds.redToggle();
return SUCCESS;
}
async event void AlarmCompare.fired()
{
atomic{
if(timers_enable==0x01)
{
ticks_counter++;
process_frame_tick_counter++;
total_tick_counter++;
if (ticks_counter == before_bi_ticks)
{
signal TimerAsync.before_bi_fired();
}
if (ticks_counter == bi_ticks)
{
//printfUART("bi%d\n", ticks_counter);
ticks_counter = 0;
current_time_slot=0;
backoff_ticks_counter=0;
time_slot_tick_next_fire=time_slot_ticks;
backoffs=1;
enable_backoffs = 1;
current_number_backoff =0;
signal TimerAsync.bi_fired();
}
if(ticks_counter == sd_ticks)
{
backoffs=0;
signal TimerAsync.sd_fired();
}
if ((enable_backoffs == 1) && (backoffs == 1))
{
backoff_ticks_counter++;
if (backoff_ticks_counter == backoff_ticks)
{
backoff_ticks_counter=0;
current_number_backoff ++;
current_number_backoff_on_time_slot++;
signal TimerAsync.backoff_fired();
}
//before time slot boundary
if(ticks_counter == before_time_slot_ticks)
{
signal TimerAsync.before_time_slot_fired();
}
//time slot fired
if (ticks_counter == time_slot_tick_next_fire)
{
time_slot_tick_next_fire = time_slot_tick_next_fire + time_slot_ticks;
before_time_slot_ticks = time_slot_tick_next_fire - BEFORE_BB_INTERVAL;
backoff_ticks_counter=0;
current_number_backoff_on_time_slot=0;
current_time_slot++;
if ((current_time_slot > 0) && (current_time_slot < 16) )
signal TimerAsync.time_slot_fired();
}
}
}
call AlarmCompare.setEventFromNow( 10 );
}
}
async command result_t TimerAsync.set_timers_enable(uint8_t timer)
{
atomic timers_enable = timer;
//printfUART("te%i\n", timers_enable);
return SUCCESS;
}
async command result_t TimerAsync.reset_process_frame_tick_counter()
{
atomic process_frame_tick_counter=0;
return SUCCESS;
}
/*RESET the tick counter, to the start ticks */
async command uint8_t TimerAsync.reset_start(uint32_t start_ticks)
{
//ticks_counter =0;
//ticks_counter = start_ticks;
current_time_slot = start_ticks / time_slot_ticks;
if (current_time_slot == 0)
{
time_slot_tick_next_fire= time_slot_ticks;
current_number_backoff = start_ticks / backoff_ticks;
current_number_backoff_on_time_slot = current_number_backoff;
}
else
{
time_slot_tick_next_fire= ((current_time_slot+1) * time_slot_ticks);
current_number_backoff = start_ticks / backoff_ticks;
current_number_backoff_on_time_slot = current_number_backoff - (current_time_slot * time_slot_backoff_periods);
}
backoff_ticks_counter=0;
backoffs=1;
//on_sync = 1;
total_tick_counter = total_tick_counter + start_ticks;
ticks_counter = start_ticks;
return current_time_slot;
}
/***********************************SET COMMANDS******************************/
/***********************************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 time_slot_ticks * NUMBER_TIME_SLOTS;
}
/*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_backoff_ticks()
{
return backoff_ticks;
}
/*get current time slot ticks*/
async command uint32_t TimerAsync.get_time_slot_ticks()
{
return time_slot_ticks;
}
/*get current backoff ticks*/
async command uint32_t TimerAsync.get_current_number_backoff()
{
return current_number_backoff;
}
async command uint32_t TimerAsync.get_time_slot_backoff_periods()
{
return time_slot_backoff_periods;
}
async command uint32_t TimerAsync.get_current_time_slot()
{
return current_time_slot;
}
async command uint32_t TimerAsync.get_current_number_backoff_on_time_slot()
{
return current_number_backoff_on_time_slot;
}
async command uint32_t TimerAsync.get_total_tick_counter()
{
return total_tick_counter;
}
async command uint32_t TimerAsync.get_process_frame_tick_counter()
{
//printfUART("%d\n", process_frame_tick_counter);
return process_frame_tick_counter;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -