zigbee demo.c

来自「zigbee 飞思卡尔 音频传输 基于ucos的所有源码」· C语言 代码 · 共 1,677 行 · 第 1/3 页

C
1,677
字号
			tt =  (decode_temp >> (decode_bite_n<<1)) & 0x03 ;
		//	decoder_data = g726_16_decoder( (int)tt, zigbee_p2 );
			decoder_data = (vuint32)decoder_data;
			decoder_data = decoder_data & 0x000000fff;
			//for debug
			//if(host_client == 0)
			//	printf("decoder_data=%d   ",decoder_data);
			//for debug
			//printf("decoder=%d",decoder_data);
			
			//for debug
			//printf("decoder_data= %d\n",decoder_data);
			//(*(vuint16 *)(&__IPSBAR[0x1B001C])) = (vuint16)decoder_data>>4;
			(*(vuint16 *)(&__IPSBAR[0x1B001C])) = (vuint16)(decoder_data)>>2;
			break;
	
 	
 		default:
 			if(cpt_i<80)
 			{
 				(*(vuint16 *)(&__IPSBAR[0x1B001C])) = (vuint16)( (dataout[cpt_i] & 0xffff)>>6);
 				cpt_i++;
 			}
 			else
 			{
 				cpt_i = 0;
 				CPT_Generator(index, &burst_duration, &silent_duration, dataout);
	
				if((burst_duration==0)&&(silent_duration==0))	
					initial(index, &burst_duration, &silent_duration);	
 			}
 			break;
	
 	}
	/*********************************************************************/

	MCF_PIT1_PCSR |= MCF_PIT_PCSR_PIE;		// Enable Interrupt
   	
  	OSIntExit();

}




/******************************************************
Function: Set the PIT1 Interrupt Vector
*******************************************************/
void Zigbee_ADC_PIT1_Interrupt_Vector_Set(void)
{	
	//printf("Set zigbee audio transrec the Interrupt Vector\n");
	OSVectSet(120, (void (*)(void))Zigbee_ADC_PIT_1Interrupt); /* Setup the context switch exception    */
	//printf("End of Setting the Interrupt Vector\n");
}


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








/*************************************************************
 *
 * Call back function
 *
 *************************************************************/


void MCPSDataIndication(tRxPacket *rx_pkt)
{
	uint8 i,length;
	uint8 command;
	
	#ifdef zigbee_debug
	//printf("enter the call back \n");
	#endif
	
	
	//Received TIMEOUT
	if (rx_pkt->u8Status == TIMEOUT) 
	{	
		printf("command send Fail\r\n");
		app_status = RECEIVER_ALWAYS_ON;
		zigbee_flag = 0;
		index = 2;
		return;	
	}		
	
	if (rx_pkt->u8Status != SUCCESS)
	{
		printf("Status != success\n");
		index = 2;
		return;
	}
	/*
	// ACK command
	if (rx_pkt->pu8Data[0] == 'A' && rx_pkt->pu8Data[1] == 'C' && rx_pkt->pu8Data[2] == 'K') 
	{
		if (app_status == WAITING_FOR_ACK) 
		{	
			app_status = RECEIVER_ALWAYS_ON; 	//go back to rx_mode.
			printf("Data send Success\r\n");
			printf("\nInput key please >");
			return;
		}
	}
	*/
	/*
	// error network ID
	if((rx_pkt->pu8Data[0] != NetID) && (rx_pkt->pu8Data[3] != CMD_JOIN))
		return;
	*/
	/*
	//for debug
	if((rx_pkt->pu8Data[0] != NetID) && (rx_pkt->pu8Data[3] != CMD_JOIN))
	{
		printf("error network ID or isnot CMD_JOIN!\n");
		return;
	}
		
	command=rx_pkt->pu8Data[3];
	*/
	if( (rx_pkt->pu8Data[0] != NetID))
	{
		printf("error network id\n");
		return;
	}
	if( (rx_pkt->pu8Data[1] != GatewayID) )
	{
		printf("error gateway id\n");
		return;
	}
	/*
	if( rx_pkt->pu8Data[3] != src_nodeid)
	{
		#ifdef zigbee_debug
		printf("error destination nodeid\n");
		printf("the des_nodeid of rec_package is %d\n",rx_pkt->pu8Data[3]);
		#endif
		return;
	}
	*/
	des_nodeid = rx_pkt->pu8Data[2];//set the destination node id
	command = rx_pkt->pu8Data[4];
	#ifdef zigbee_debug
	//printf("command = %d\n",command);
	#endif
	
	switch(command)
	{
		case CMD_JOIN:								// device request to join
			tx_data_buffer[0]=rx_pkt->pu8Data[0];	// device NetID
			tx_data_buffer[1]=GatewayID;			// src ID
			tx_data_buffer[2]=rx_pkt->pu8Data[1];	// current Device ID
			tx_data_buffer[3]=CMD_JOIN;				// command ID
			tx_data_buffer[4]=NetID;				// NetID
			tx_data_buffer[5]=NodeID++;				// assign device ID 
			//tx_data_buffer[5]=NodeID;
			tx_pkt.u8DataLength=6;					// data length
			app_status = TRANSMIT_CMD;				
			printf("\n\r\nOne device request to join\r\n");
			printf("Assign node ID = ");
			printf("%d\r\n",NodeID-1);
			
			if(NodeID==6)							
			{
				printf("Maximum 5 device joined\r\n");
				NodeID=1;	
			}
			
			//printf("\nInput key please >");	
			break;
			
		case CMD_STATUS:							// device response to join comfirm
			printf("\nDevice ");
			printf("%d Joined",rx_pkt->pu8Data[1]);
			printf("\r\nLink Quality= -");			// send back link quality
			printf("%d",(rx_pkt->pu8Data[4]/2));
			printf("dBm.\r\n");
			app_status=RECEIVER_ALWAYS_ON;
			//printf("\nInput key please >");
			break;
			
		case CMD_DATA:								// receive data from device to me
			//if(rx_pkt->pu8Data[2] == GatewayID)
			if(rx_pkt->pu8Data[1] == GatewayID)//change by michael chen
			{
			  	printf("\r\nReceive data from device ");
				printf("%d >",rx_pkt->pu8Data[1]);
				for(i=4;i<rx_pkt->u8DataLength;i++)
				{
					printf("%c",rx_pkt->pu8Data[i]);
				}
				app_status = TRANSMIT_ACK;	
			}
			else
			{
			
				//app_status = RECEIVER_ALWAYS_ON;	
				tx_data_buffer[0]=NetID;			
				tx_data_buffer[1]=rx_pkt->pu8Data[1];
				tx_data_buffer[2]=rx_pkt->pu8Data[2];
				tx_data_buffer[3]=CMD_FDATA;
				for(i=4;i<rx_pkt->u8DataLength;i++)
					tx_data_buffer[i]=rx_pkt->pu8Data[i];
				tx_pkt.u8DataLength=rx_pkt->u8DataLength;	
				app_status = TRANSMIT_CMD;	
				//for debug
				printf("CMD_FDATA\n");
			}
			
			break;
		/*	
		case CMD_FDATA:								// forward data from device to other device
			if(rx_pkt->pu8Data[2] != GatewayID)
			{
				tx_data_buffer[0]=NetID;			
				tx_data_buffer[1]=rx_pkt->pu8Data[1];
				tx_data_buffer[2]=rx_pkt->pu8Data[2];
				tx_data_buffer[3]=CMD_FDATA;
				for(i=4;i<rx_pkt->u8DataLength-4;i++)
					tx_data_buffer[i]=rx_pkt->pu8Data[i];
				tx_pkt.u8DataLength=rx_pkt->u8DataLength;	
				app_status = TRANSMIT_CMD;	
				
			}
			else
			{
			
				app_status = RECEIVER_ALWAYS_ON;
			}
			
			break;
		*/	
		
		case CMD_CALL:
			app_status = SEND_CALL_ACK;
			index = 0;
			#ifdef zigbee_debug
			printf("cmd_call\n");
			#endif
			break;
			
		case CMD_ACCEPT:
			app_status = SEND_ACCEPT_ACK;
			#ifdef zigbee_debug
			printf("cmd_accept\n");
			#endif
			break;
			
		case CMD_CALL_ACK:
			#ifdef zigbee_debug
			printf("cmd_call_ack\n");
			#endif
			app_status = WAIT_ACCEPT;
			break;
			
		case CMD_ACCEPT_ACK:
			#ifdef zigbee_debug
			printf("cmd_accept_ack\n");
			#endif
			app_status = REC_ACCEPT_ACK;
			break;
			
		case CMD_AUDIO_DATA:
			//app_status = AUDIO_DATA;
			#ifdef zigbee_debug
			//printf("rec data\n");
			//printf("1");
			#endif
			//receive data from zigbee
			
			//方案三
			
			switch(in_buf_flag)
			{	
				case 1:
					//在receive package的前5个数据是有关数据包的数据,有效数据是从第6个开始的数据
					for(i=0;i<(rx_pkt->u8DataLength-5);i++)
						in_data_buf2[i]=rx_pkt->pu8Data[i+5];
					/*
					//for debug
					for(i=0;i<rx_pkt->u8DataLength;i++)
						printf("indata1	%d",in_data_buf2[i]);
					*/
					//in_buf_flag = 2;
					#ifdef zigbee_debug
					//printf("put %d bytes into in_data_buf3\n",rx_pkt->u8DataLength);
					#endif
					break;
					
				case 2:
					for(i=0;i<(rx_pkt->u8DataLength-5);i++)
						in_data_buf1[i]=rx_pkt->pu8Data[i+5];
					/*
					//for debug
					for(i=0;i<rx_pkt->u8DataLength;i++)
						printf("indata2   %d",in_data_buf1[i]);
					*/
					//in_buf_flag = 1;
					#ifdef zigbee_debug
					//printf("put %d bytes into in_data_buf1\n",rx_pkt->u8DataLength);
					#endif
					break;
					
				default:
					printf("ZIGBEE isr in buf flag error\n");
					break;
			}
			trans_flag = 1;//can transmit a package of audio data
			break;
			
			
		default:
			printf("cmd error\n");
			break;	
			
	}
	
}



/*********************************************************************/
void zigbee_main (void)
{

	//char tx_msg[UIF_MAX_LINE];
	//uint8 i,length;
	//char temp_key;
	uint32 i;
	
	/*initialize the LEDs for outputs*/
	MCF_GPIO_PTCPAR = 0x00;   
    MCF_GPIO_DDRTC = 0x0F;
	MCF_GPIO_PORTTC = 0x0;
	
	
	/*Initialize QSPI*/
	PortInit();
	
	/*initialize MC13192*/
	MC13192Init();  

	rx_pkt.pu8Data = &rx_data_buffer[0];
	tx_pkt.pu8Data = &tx_data_buffer[0];

	/*Set the txcvr to initial state*/
	PLMESetTrxStateRequest(IDLE_MODE);
	PLMESetChannelRequest(channel);
  	MLMEMC13192PAOutputAdjust(power_level);	//Set MIN power setting
	
    /* Enable interrupts to the core */
	mcf5xxx_irq_enable();
		
	
	printf("\n\n**********Welcome to MCF5213 SMAC Demo*************");
	printf("\n***************************************************");
	printf("\nSW1 toggles between channels");
    printf("\nSW2 toggles between Minimum, Nominal, and Max PA power");
   	printf("\nSW4 cancel button ");
	printf("\nSW5 send the call command or accept the call command\n");
	
	app_status=RECEIVER_ALWAYS_ON;
	zigbee_flag = 0;
	
	for(;;) 
	{	
		
		// data input
		if ( (zigbee_flag == 0) && ( 	button_value_current == 5 )) 
		{
			app_status = SEND_CALL;
			button_value_current = 0;


		}
		//for debug
		//printf("app_status=%d\n",app_status);
		switch (app_status) 
		{
		
			case IDLE_STATE:			//Do nothing.
				break;
				
			case RECEIVER_ALWAYS_ON:
			
				MLMERXEnableRequest(&rx_pkt,0);
				app_status=IDLE_STATE;
				break;
				
			case WAITING_FOR_ACK:		//Waiting for ack	
				break;
			
				
			case TRANSMIT_DATA:

				if (MLMERXDisableRequest() != SUCCESS)  //Turn off the RX mode.
				{
					app_status = TRANSMIT_DATA;
					break;
				}
				
				tx_pkt.pu8Data = &tx_data_buffer[0];;
				MCPSDataRequest(&tx_pkt);
				//for debug
				//printf("MCPDataRequest has over!\n");
				app_status = WAITING_FOR_ACK; 
				MLMERXEnableRequest(&rx_pkt, TIMEOUT_PERIOD);
				
				break;
				
			case TRANSMIT_CMD:
				for(i=0;i<=0x2000;i++);
   					nop();
				MLMERXDisableRequest();
				tx_pkt.pu8Data=&tx_data_buffer[0];
				MCPSDataRequest(&tx_pkt);
				app_status = RECEIVER_ALWAYS_ON;
				break;
				
			case TRANSMIT_ACK:
				tx_data_buffer[0] = 'A';
				tx_data_buffer[1] = 'C';
				tx_data_buffer[2] = 'K';
				tx_data_buffer[3] = '\0';
				tx_pkt.pu8Data = (uint8*)tx_data_buffer;
				tx_pkt.u8DataLength = 4;
				MCPSDataRequest(&tx_pkt);
				app_status = RECEIVER_ALWAYS_ON;				
				break;
			//add by michael 
			//	#define SEND_CALL			7
			//	#define	SEND_ACCEPT			8
			//	#define	REC_CALL			9
			//	#define	REC_ACCEPT			10
			//	#define	AUDIO_DATA			11
			//	#define	WAIT_FOR_CALL_ACK	12
			//	#define	WAIT_FOR_ACCEPT_ACK	13
			//	#define	WAIT_ACCEPT			14
			//	#define	SEND_CALL_ACK		15
			//	#define SEND_ACCEPT_ACK		16
			//	#define CANCEL				17
			//	#define REC_ACCEPT_ACK		18


			case SEND_CALL:
				zigbee_flag = 1;
				trans_flag = 1;
				zigbee_enter_726 = 0;
				host_client = 1;//host demo
				//cpt
				index = 1;
				cpt_i = 0;
				initial(index, &burst_duration, &silent_duration);
				CPT_Generator(index, &burst_duration, &silent_duration, dataout);
				
				//enable the ADC and the cpt
				Zigbee_ADC_InitPwmRegister();				// Initialize the Pwm Register
				Zigbee_ADC_PIT1_Interrupt_Vector_Set();	// Set the PIT1 interrupt Register
				Zigbee_ADC_InitPIT1Register();				// Initialize the PIT1 Register
				
				//#ifdef zigbee_debug
				printf("send call \n");
				//#endif
				if (MLMERXDisableRequest() != SUCCESS)  //Turn off the RX mode.
				{
					app_status = SEND_CALL;
					printf("can't disable the rx\n");
					break;
				}
				//void make_trans_package(unsigned char src_id,unsigned char des_id,unsigned char cmd,unsigned char *data,unsigned char data_buf_length)
				make_trans_package(src_nodeid,des_nodeid,CMD_CALL,cmd_para_buf,0);
				//tx_pkt.pu8Data = &tx_data_buffer[0];
				MCPSDataRequest(&tx_pkt);
				//for debug
				//printf("MCPDataRequest has over!\n");
				app_status = WAIT_FOR_CALL_ACK; 
				MLMERXEnableRequest(&rx_pkt, TIMEOUT_PERIOD);
				
				break;
			
			case WAIT_FOR_CALL_ACK:
				//#ifdef zigbee_debug
				printf("wait for call ack\n");
				//#endif
				if( button_value_current == 4 )//press the SW4 cancel button
				{
					button_value_current = 0;
					printf("cancel this call\n");
					app_status = CANCEL;
					break;
				}
				break;
				
			case SEND_CALL_ACK:
				//#ifdef zigbee_debug
				printf("send call ack\n");
				//#endif
				
				zigbee_flag = 1;
				trans_flag = 0;
				zigbee_enter_726 = 0;
				host_client = 0;//set client demo
				//cpt
				index = 0;
				cpt_i = 0;
				initial(index, &burst_duration, &silent_duration);
				CPT_Generator(index, &burst_duration, &silent_duration, dataout);
				
				Zigbee_ADC_InitPwmRegister();				// Initialize the Pwm Register
				Zigbee_ADC_PIT1_Interrupt_Vector_Set();	// Set the PIT1 interrupt Register
				Zigbee_ADC_InitPIT1Register();				// Initialize the PIT1 Register
				
				if (MLMERXDisableRequest() != SUCCESS)  //Turn off the RX mode.
				{
					app_status = SEND_ACCEPT;
					printf("can't disable the rx\n");
					break;
				}
				//void make_trans_package(unsigned char src_id,unsigned char des_id,unsigned char cmd,unsigned char *data,unsigned char data_buf_length)
				make_trans_package(src_nodeid,des_nodeid,CMD_CALL_ACK,cmd_para_buf,0);
				//tx_pkt.pu8Data = &tx_data_buffer[0];
				MCPSDataRequest(&tx_pkt);
				//for debug
				//printf("MCPDataRequest has over!\n");
				app_status = REC_CALL; 
				MLMERXEnableRequest(&rx_pkt, 0);
				break;
			
			case REC_CALL:
				#ifdef zigbee_debug
				//printf("receive call \n");
				#endif
				
				if( button_value_current == 4 )//press the SW4 cancel button
				{
					button_value_current = 0;
					printf("cancel this call\n");
					app_status = CANCEL;
					break;
				}
				if( button_value_current ==5 )//press the SW5 confirm button
				{
					button_value_current = 0;
					printf("accept this call\n send the accept command\n");
					app_status = SEND_ACCEPT;
					break;
				}
				break;
				
			case SEND_ACCEPT:
			
				//#ifdef zigbee_debug
				printf("send accept \n");

⌨️ 快捷键说明

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