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

📄 zigbee demo.c

📁 zigbee 飞思卡尔 音频传输 基于ucos的所有源码
💻 C
📖 第 1 页 / 共 3 页
字号:
					{
						out_buf_flag = 1;
						encode_bite_n = 0;
						out_buf_count = 0;
						out_buf_full_flag = 2;//set full flag
					}
					break;
			
				default:
					printf("ADC ISR out buf flag error\n");
					break;
			}
			
			//decode
			if(start_decode == 1)
			{
				decode_bite_n++;
				if( decode_bite_n == BIT_N_NUM )
				{
					decode_bite_n = 0;
					switch(in_buf_flag)
					{
						case 1:
							decode_temp = (vuint8)in_data_buf1[in_buf_count];//(vuint8)tt;
							
							in_data_buf1[in_buf_count] = 0x00;//clear the data buf byte after output to pwm
							in_buf_count++;
										
							if(in_buf_count == UIF_MAX_IN)
							{
								in_buf_flag = 2;
								in_buf_count = 0;
							}
							break;
						
						case 2:
							decode_temp = (vuint8)in_data_buf2[in_buf_count];//(vuint8)tt;
							in_data_buf2[in_buf_count] = 0x00;
							in_buf_count++;
							if(in_buf_count == UIF_MAX_IN)
							{
								in_buf_flag = 1;
								in_buf_count = 0;
							}
							break;
						
						default:
							printf("AD isr in buf error\n");
							break;
					
					}
					
				}
				//right
				//tt = ( decode_temp & (0x03<<(decode_bite_n<<1) ) >> (decode_bite_n<<1) );
				//another method
				tt =  (decode_temp >> (decode_bite_n<<ENCODE_BIT)) & BIT_MASK ;
			//	printf("%x\n",tt);
				decoder_data = g726_32decoder( (int)tt, zigbee_p322 );
				
			//	decoder_data = (vuint32)decoder_data ;
			//	decoder_data = decoder_data>>4;
			
				decode_tmp = (vuint32)decoder_data ;
			//	decode_tmp = decode_tmp>>4;
				decode_tmp = decode_tmp + 1933;
								
				(*(vuint16 *)(&__IPSBAR[0x1B001C])) = (vuint16)(decode_tmp)>>2;
				
			//	printf("%d\n",decoder_data);
				
			
			//	decoder_data = decoder_data + 1933;
				
			//	decoder_data = decoder_data & 0x0000ffff;
					
				//(*(vuint16 *)(&__IPSBAR[0x1B001C])) = (vuint16)decoder_data>>4;
			//	(*(vuint16 *)(&__IPSBAR[0x1B001C])) = (vuint16)(decoder_data)>>2;
			}
			break;
	
 	
 		default:	
 			//for new cpt
 			
 			zigbee_cptout = cpt();
 			zigbee_cptout = zigbee_cptout + 32768;
 			(*(vuint16 *)(&__IPSBAR[0x1B001C])) = (vuint16)zigbee_cptout>>6;
 			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)
{	
	OSVectSet(120, (void (*)(void))Zigbee_ADC_PIT_1Interrupt); /* Setup the context switch exception    */
}


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




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


void MCPSDataIndication(tRxPacket *rx_pkt)
{
	uint8 i,length;
	uint8 command;
	
	
	//Received TIMEOUT
	if (rx_pkt->u8Status == TIMEOUT) 
	{	
		printf("command send Fail\r\n");
		app_status = RECEIVER_ALWAYS_ON;
		zigbee_flag = 0;
		index_cpt = 2;
		return;	
	}		
	
	if (rx_pkt->u8Status != SUCCESS)
	{
		printf("Status != success\n");
		index_cpt = 2;
		return;
	}


	if( (rx_pkt->pu8Data[0] != NetID))
	{
		printf("error network id\n");
		return;
	}
	if( (rx_pkt->pu8Data[1] != GatewayID) )
	{
		printf("error gateway id\n");
		return;
	}

	des_nodeid = rx_pkt->pu8Data[2];//set the destination node id
	command = rx_pkt->pu8Data[4];
	
	
	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_CALL:
			app_status = SEND_CALL_ACK;
			index_cpt = 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:
			//receive data from zigbee
			
			//方案三
			start_decode = 1;
			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];
					break;
					
				case 2:
					for(i=0;i<(rx_pkt->u8DataLength-5);i++)
						in_data_buf1[i]=rx_pkt->pu8Data[i+5];
					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)
{


	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("\nSW2 toggles between channels");
    printf("\nSW3 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;
		}
		
		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);
			
				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
			
				index_cpt = 1;
			
				//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
				
				printf("send call \n");
				
				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);
				MCPSDataRequest(&tx_pkt);
				
				app_status = WAIT_FOR_CALL_ACK; 
				MLMERXEnableRequest(&rx_pkt, TIMEOUT_PERIOD);
				
				break;
			
			case WAIT_FOR_CALL_ACK:
				printf("wait for call ack\n");

				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:
				printf("send call ack\n");
				
				zigbee_flag = 1;
				trans_flag = 0;
				zigbee_enter_726 = 0;
				host_client = 0;//set client demo
				
				//for new cpt
				index_cpt = 0;
				
				
				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);
				MCPSDataRequest(&tx_pkt);
				
				app_status = REC_CALL; 
				MLMERXEnableRequest(&rx_pkt, 0);
			
				break;
			
			case REC_CALL:
				

⌨️ 快捷键说明

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