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

📄 mac01.c

📁 zigbee程序
💻 C
📖 第 1 页 / 共 5 页
字号:
/*****************************************************/ 

async event result_t TimerAsync.before_bi_fired()
{
	
	printfUART("bbi %i\n",call TimerAsync.get_current_ticks());
	
	//call Leds.redOn();
	
	if (mac_PIB.macBeaconOrder != mac_PIB.macSuperframeOrder )
	{
		if ( Beacon_enabled_PAN == 1 )
		{
			//
			//post set_trx();
			trx_status = PHY_TX_ON;
			call PLME_SET_TRX_STATE.request(PHY_TX_ON);
		}
		else
		{
			//
			//post set_trx();
			trx_status = PHY_RX_ON;
			call PLME_SET_TRX_STATE.request(PHY_RX_ON);
		}
	}
	
	//I_AM_IN_CAP = 1;
	findabeacon = 1;
	
	return SUCCESS;
}

/*******************Timer BEACON INTERVAL******************/
async event result_t TimerAsync.bi_fired()
{
	////////////printfUART("BI F %d\n",call Time.getLow32()); 
	printfUART("bi %i\n",call TimerAsync.get_current_ticks());
	call Leds.yellowOn();
	
	//call Leds.redOff();
	
	I_AM_IN_CAP = 1;
	I_AM_IN_IP = 0;
	
	////printfUART("bi %i\n",I_AM_IN_CAP); 
	
	//if the device is a pan coordinator the beacon must be sent in the beginning of the superframe
	if ( Beacon_enabled_PAN == 1 )
	{
		call Leds.yellowOn();
		//the beacon is send directly without CSMA/CA
		call PD_DATA.request(send_beacon_length,send_beacon_frame_ptr);
		
		//call PLME_SET_TRX_STATE.request(PHY_RX_ON);
	}
	
	number_backoff =0;
	number_time_slot=0;
	
	//(SYNC) checking if the node is trying to syncronize
	//if the device is trying to synchronize
	/*
	if(findabeacon == 1 ) 
	{
		missed_beacons++;
		on_sync =0;
		
		if(missed_beacons == aMaxLostBeacons)
		{		
				beacon_loss_reason = MAC_BEACON_LOSS;
				post signal_loss();
				findabeacon=0;
				missed_beacons =0;
				//return SUCCESS;
		}
	}
	*/
	//CHECK there is the need to wait a small amount of time before checking if the beacon as been processed or not
	//possible solition, if it receives a packet stand by for confirmation it its a beacon
	//The device must always receive the beacon
	if (TrackBeacon == 1) 
	{
		if (beacon_processed==1) 
		{
			beacon_processed=0;
		}
		else
		{
			//dealocate all GTS
			//beacon loss
			////////////printfUART("beacon loss track %i\n", missed_beacons);
			on_sync =0;
			beacon_loss_reason = MAC_BEACON_LOSS;
			post signal_loss();
			//return SUCCESS;
		}
	}
	//CHECK 
	//csma_backoff_counter_inst=0;
	//send data
	
	////printfUART("bi %i\n",send_buffer_count); 
	
	post send_frame_csma();

	return SUCCESS;
}

/*******************Timer SUPERFRAME DURATION******************/
async event result_t TimerAsync.sd_fired()
{
	call Leds.yellowOff();
	//call Leds.greenOff();

	I_AM_IN_CFP = 0;
	I_AM_IN_IP = 1;
	
	//printfUART("SD %i\n", call TimerAsync.get_current_ticks()); 
	
	number_backoff=0;
	number_time_slot=0;
	
	
	if (PANCoordinator == 0 && TYPE_DEVICE == ROUTER)
	{
		trx_status = PHY_RX_ON;
		call PLME_SET_TRX_STATE.request(PHY_RX_ON);
	}
	else
	{
		trx_status = PHY_RX_ON;
		call PLME_SET_TRX_STATE.request(PHY_RX_ON);
		//call PLME_SET_TRX_STATE.request(PHY_TRX_OFF);
	}
	
	if (mac_PIB.macShortAddress==0xffff && TYPE_DEVICE == END_DEVICE)
	{
		trx_status = PHY_RX_ON;
		call PLME_SET_TRX_STATE.request(PHY_RX_ON);
	}
	
	//trx_status = PHY_RX_ON;
	//post set_trx();
	
	/*
	
	//turn the transceiver off
	if (mac_PIB.macBeaconOrder != mac_PIB.macSuperframeOrder )
	{
		if ( mac_PIB.macRxOnWhenIdle == 0 && findabeacon == 0) 
		{
			trx_status = PHY_TRX_OFF;
			post set_trx();
		}
		else
		{
			trx_status = PHY_RX_ON;
			post set_trx();
		}
	}

	//if the node is trying to synchronize
	if (on_sync == 0 || mac_PIB.macPromiscuousMode == 1)
	{
		atomic{
			trx_status = PHY_RX_ON;
			post set_trx();
		}
	}
	*/
	/*
	
	if (on_sync == 0)
	{
		findabeacon=1;
	}
	else
	{
		on_sync=0;
	}
	*/
	
	
	if (PANCoordinator == 1)
	{
		//increment the gts_null descriptors
		atomic{
			
				//if (GTS_null_descriptor_count > 0) post increment_gts_null();
			
				//if (GTS_descriptor_count >0 ) post check_gts_expiration();
			
				//if (indirect_trans_count > 0) increment_indirect_trans();
				
				//creation of the beacon
				post create_beacon();
		}
		//trx_status = PHY_TRX_OFF;
		//post set_trx();
	}

	//trx_status = PHY_TRX_OFF;
	//call PLME_SET_TRX_STATE.request(PHY_TRX_OFF);

	return SUCCESS;
}

/*******************Timer BEFORE TIME SLOT FIRED******************/
async event result_t TimerAsync.before_time_slot_fired()
{
	on_s_GTS=0;
	on_r_GTS=0;
	if (next_on_s_GTS == 1)
	{	
		on_s_GTS=1;
		next_on_s_GTS =0;
		trx_status = PHY_TX_ON;
		call PLME_SET_TRX_STATE.request(PHY_TX_ON);
		
		
		//post set_trx();
	}
	
	if(next_on_r_GTS == 1)
	{
		on_r_GTS=1;
		next_on_r_GTS=0;
		trx_status = PHY_RX_ON;
		call PLME_SET_TRX_STATE.request(PHY_RX_ON);
		//post set_trx();
	}

return SUCCESS;
}
/*******************Timer TIME SLOT FIRED******************/
async event result_t TimerAsync.time_slot_fired()
{
	
	//reset the backoff counter and increment the slot boundary
	number_backoff=0;
	number_time_slot++;
		
	//verify is there is data to send in the GTS, and try to send it
	if(PANCoordinator == 1 && GTS_db[15-number_time_slot].direction == 1 && GTS_db[15-number_time_slot].gts_id != 0)
	{
		//COORDINATOR SEND DATA
		//////////printfUART("bbck%i:%i:%i\n", (15-number_time_slot),GTS_db[15-number_time_slot].direction,gts_slot_list[15-number_time_slot].element_count);
		
		post start_coordinator_gts_send();
				
	}
	else
	{
		//DEVICE SEND DATA
		if (number_time_slot == s_GTSss && gts_send_buffer_count > 0 && on_sync == 1)//(send_s_GTSss-send_s_GTS_len) 
		{
				//current_time = call TimerAsync.get_total_tick_counter();
				post start_gts_send();
		}		
	}
	
	next_on_r_GTS =0;
	next_on_s_GTS=0;
	
	//verification if the time slot is entering the CAP
	//GTS FIELDS PROCESSING
	
	if ((number_time_slot + 1) >= final_CAP_slot && (number_time_slot + 1) < 16)
	{
		I_AM_IN_CAP = 0;
		I_AM_IN_CFP = 1;
		//////printfUART("bts %i\n",I_AM_IN_CAP); 
	atomic{
		
		//verification of the next time slot
		if(PANCoordinator == 1 && number_time_slot < 15)
		{
		//COORDINATOR verification of the next time slot
			if(GTS_db[14-number_time_slot].gts_id != 0x00 && GTS_db[14-number_time_slot].DevAddressType != 0x0000)
			{	
				if(GTS_db[14-number_time_slot].direction == 1 ) // device wants to receive
				{
					next_on_s_GTS =1; //PAN coord mode
				}
				else
				{
					next_on_r_GTS=1; //PAN coord mode
				}
			}	
		}
		else
		{
		//device verification of the next time slot
			if( (number_time_slot +1) == s_GTSss || (number_time_slot +1) == r_GTSss )
			{
				////////////printfUART("s_GTSss: %i r_GTSss: %i\n", s_GTSss,r_GTSss);
				if((number_time_slot + 1) == s_GTSss)
				{	
					////////////printfUART("MY SEND SLOT \n", "");
					next_on_s_GTS =1;
					s_GTS_length --;
					if (s_GTS_length != 0 )
					{
						s_GTSss++;
					}
				}
				else			
				{
					////////////printfUART("MY RECEIVE SLOT \n", "");
					next_on_r_GTS =1;
					r_GTS_length --;
					if (r_GTS_length != 0 )
					{
						r_GTSss++;
					}
				}				
			}
			else
			{
				//idle
				next_on_s_GTS=0;
				next_on_r_GTS=0;
			}
		}
	}
	}
return SUCCESS;
}

task void start_coordinator_gts_send()
{
atomic{

	coordinator_gts_send_pending_data =0;
	
	if(gts_slot_list[15-number_time_slot].element_count > 0)
	{
		if (check_gts_send_conditions(gts_send_buffer[gts_slot_list[15-number_time_slot].gts_send_frame_index[gts_slot_list[15-number_time_slot].element_out]].length) == 1 )
		{
			
			gts_send_buffer[gts_slot_list[15-number_time_slot].gts_send_frame_index[gts_slot_list[15-number_time_slot].element_out]].length = gts_send_buffer[gts_slot_list[15-number_time_slot].gts_send_frame_index[gts_slot_list[15-number_time_slot].element_out]].length -2;
			
			call PD_DATA.request(gts_send_buffer[gts_slot_list[15-number_time_slot].gts_send_frame_index[gts_slot_list[15-number_time_slot].element_out]].length,(uint8_t *)&gts_send_buffer[gts_slot_list[15-number_time_slot].gts_send_frame_index[gts_slot_list[15-number_time_slot].element_out]]);
	
			available_gts_index_count++;
			available_gts_index[available_gts_index_count] = gts_slot_list[15-number_time_slot].gts_send_frame_index[gts_slot_list[15-number_time_slot].element_out];
			
			gts_slot_list[15-number_time_slot].element_count--;
			gts_slot_list[15-number_time_slot].element_out++;
			
			if (gts_slot_list[15-number_time_slot].element_out == GTS_SEND_BUFFER_SIZE)
				gts_slot_list[15-number_time_slot].element_out=0;

			if(gts_slot_list[15-number_time_slot].element_count > 0 )
			{
				coordinator_gts_send_pending_data =1;
				coordinator_gts_send_time_slot = number_time_slot;
			}
		}
	}
}
return;
}

task void start_gts_send()
{

	atomic{
	gts_send_pending_data = 0;
	
	if(gts_send_buffer_count > 0)
	{
		if (check_gts_send_conditions(gts_send_buffer[gts_send_buffer_msg_out].length) == 1 )
		{

			gts_send_buffer[gts_send_buffer_msg_out].length = gts_send_buffer[gts_send_buffer_msg_out].length -2;

			call PD_DATA.request(gts_send_buffer[gts_send_buffer_msg_out].length,(uint8_t *)&gts_send_buffer[gts_send_buffer_msg_out]);
	
			gts_send_buffer_count --;
			gts_send_buffer_msg_out++;

			if (gts_send_buffer_msg_out == GTS_SEND_BUFFER_SIZE)
				gts_send_buffer_msg_out=0;

			////////////printfUART("after send %i %i %i \n",gts_send_buffer_count,gts_send_buffer_msg_in,gts_send_buffer_msg_out);
			
			if (gts_send_buffe

⌨️ 快捷键说明

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