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

📄 ping.c

📁 ST7540 Demo 下位机C程序
💻 C
📖 第 1 页 / 共 2 页
字号:
/*********************************************************************************
*                 ST 7538 DEMOBOARD SOFTWARE 	  ping.c                            *
*                                                                                 *
*                 ping  functions                                    V1.2         *
***********************************************************************************/

#include "global.h"

extern unsigned char		vUSB_Configuration;

#pragma CODE_SEG APPLI_CODE



//local functions
/**********************************************************************************
* Create_frame function: generates frame to transmit and put it to the SPI buffer
**********************************************************************************/
void Create_frame(void);
/**********************************************************************************
* SetRS232Frame function: generates Master statistic informations and put the data
* on RS-232 buffer
**********************************************************************************/
void SetRS232Frame(unsigned char);
/**********************************************************************************
* Check_frame function: evaluate if frame received is correct
**********************************************************************************/
char Check_Ping_frame(void);
/**********************************************************************************
* PING_Reset function: reset statistical information
**********************************************************************************/
void PING_Reset(void);
/**********************************************************************************
* SPI_xor_Buffer function: perform an XOR of packet with "AA" every 2 bytes
**********************************************************************************/
void SPI_xor_Buffer(BYTE);
/**********************************************************************************
* HandleSlaveFrame function: gets informations from master packet
**********************************************************************************/
void HandleSlaveFrame(void);
/**********************************************************************************
* bus_free function: return 0 if bus is busy (CD/PD or BU actives), 1 otherwise
**********************************************************************************/
unsigned char bus_free(void);
/**********************************************************************************
* CDMA_acces function: return 0 if medium access is not avilable, 1 otherwise
**********************************************************************************/
unsigned char CDMA_acces(void); 




/**********************************************************************************
* PING_Init function: initializes data
**********************************************************************************/
void PING_Init(void)
{
	PING_Status = PS_STOP;
	PING_Flags = 0;
  PING_Status = 0;
  repetition = 0;
  CDMA_Mode = 0;
  msg_num.whole = 0;
  old_msg_num.whole = 0;
  max_msg_num.whole = 0;
  ok_msg.whole = 0;
  wrong_msg.whole = 0;
  corr_msg.whole = 0;
  more_msg.whole = 0;
  Slave_Add = 1;
  Slave_Num = 0;  
  timWait = 0;
  rep_count = 0;
  max_slots = 0;
  max_time = 0;
  Wait_time = 0;
  Mast_Add = 0;
  CDMA_flags=1;
}

/**********************************************************************************
* PING_Start function: enables ping function and activate timeouts
**********************************************************************************/
void PING_Start(void)
{
	// Aggiungere la parte di abilitazione del registro di controllo
	// per il ST7540/38E
	
	TIMEOUT_Close(timWait);		//close previous timers
	TIMEOUT_Close(timIntA);

  timWait  = TIMEOUT_Open(Wait_time);   //master wait time 
  timIntA  = TIMEOUT_Open(max_time);    //ping CDMA max wait time
	  
	PING_Reset();													//reset counters
	PING_Status = PS_START;								//first step
}

/**********************************************************************************
* PING_Stop function: disables ping function 
**********************************************************************************/
void PING_Stop(void)
{
	PING_Status = PS_STOP;
	SPI_Disable(); 
}

/**********************************************************************************
* PING_Analyze function: manages ping 
**********************************************************************************/
void PING_Analyze(void)
{


	if(PING_Status==PS_STOP)
		return;
	
	if(PING_Flags&PF_MASTER) {	// MASTER PING
		switch (PING_Status) {
			case PS_START:					 //TRANSMIT MESSAGE
			  if(SPI_OnRun()) //wait rs-232 communication end 
					  break;
			PING_Status = PS_SEND;				
				if (_200mS_Delay_Elapsed())
				{
				  if (CDMA_acces())				//if medium is free transmit the data, otherwise  wait
				  {
				    DI;
		        Create_frame();
		        SPI_xor_Buffer(7);
		        EI; 
				    Tx_Data(1,22,SPI_Buffer);
				    //PING_Status = PS_SEND;
				    CDMA_flags=1;
				    refresh_200mS_Delay();
				  } else (PING_Status = PS_START);
				  
				} else	(PING_Status = PS_START);
				break;	
			case PS_SEND:					  //RECEIVE MESAGE
			  if(SPI_OnRun())
					  break;
				SPI_Disable();
				PING_Flags &= ~(PF_RECV);		//set receive flag to 0
				PING_Status = PS_WAIT;			//go to wait state
				DI;
				 Rx_Data(RX_SYN,0xE958,0xFFFF,16);
				EI;
			  TIMEOUT_Refresh(timWait);	  //REFRESH WAIT TIME
				break;
			case PS_WAIT:						//WAIT MESSAGE
			  if (PING_Flags&PF_RECV) //if received a packet 
					 PING_Status = PS_RXOK;														//go to RXOK
				else if(TIMEOUT_Expired(timWait))
				   PING_Status = PS_NORX;														//else after timWait msec go to NORX
				break; 
			case PS_NORX:								//NO REPONSE BY SLAVE
				SPI_Disable();
					
			  if((repetition)) //if repetition enable and not first message
			  {
			      rep_count++;
			      if (rep_count<3){
			          PING_Status = PS_START;			 //re-transmit same message
			          break;
			      }
			        
			  }
			  
			  if((msg_num.whole!=0))	{			// messagge 0 not acknownleged
					    SetRS232Frame(PR_NOACK);
				}
				
				if(Slave_Add<Slave_Num) {						//update slave address
    				Slave_Add++;
				    rep_count=0; 
    		}
    		else {
    					Slave_Add = 1;
    					msg_num.whole++;							//update message number
    					rep_count=0; 
    		}	
    		
    		if(msg_num.whole>max_msg_num.whole){		//if sequence completed stop
    					PING_Status = PS_STOP;
    		}
    				else
    		{
      				PING_Status = PS_START;	
    		}
				break;	
			case PS_RXOK:										 //MESSAGE ARRIVED
				SPI_Disable();
				PING_Flags &= ~(PF_FEC);
				DI;
				SPI_xor_Buffer(1);
				if(Check_Ping_frame()) {	
				    EI;		//message OK
				  	if(SPI_Buffer[0]!=Mast_Add) {		//if master address is not correct
						  PING_Flags &= ~(PF_RECV);
				      PING_Status = PS_WAIT;				//wait another message
				      DI;
				      Rx_Data(RX_SYN,0xE958,0xFFFF,16);
				      EI;
						  break;
						}
					if(PING_Flags&PF_FEC) {		//corrected trough FEC
						SetRS232Frame(PR_FEC);
					}
					else {
						SetRS232Frame(PR_OK);		//message OK
					}
				}
				else {
				  EI;
					SetRS232Frame(PR_FCS);		//message wrong
					}	
				if(Slave_Add<Slave_Num) {		//update slave address
						Slave_Add++;
				    rep_count=0; 
					}
				else {
						Slave_Add = 1;
						msg_num.whole++;				//update message number
						rep_count=0; 
					}
				if(msg_num.whole>max_msg_num.whole) {	 //chek end of sequence
						PING_Status = PS_STOP;
					}
				else
				{
				      PING_Status = PS_START;	
				}      
				break;
			default:
			PING_Status = PS_STOP;
			
		}
	}
	else {						// SLAVE PING
		switch(PING_Status) {
			case PS_START:										//RECEIVE MESSAGE
			  if(SPI_OnRun()) //wait rs-232 communication end 
					break;
				PING_Flags &= ~(PF_RECV);
				PING_Status = PS_WAIT;
				DI;
				  Rx_Data(RX_SYN,0x9B58,0xFFFF,16);		//receive message
				EI;
				break;
			case PS_WAIT:												//WAIT MESSAGE
				if (PING_Flags&PF_RECV)								//if received message go to RX_OK
						PING_Status=PS_RXOK;
				break;
			case PS_RXOK: 											//MESSAGE ARRIVED
				SPI_Disable();
				PING_Flags &= ~(PF_FEC);
				DI;
				SPI_xor_Buffer(1);
				if(Check_Ping_frame()) 				 //if message is OK (or corrected successful)
				{
  					EI;
  					if(Slave_Add!=SPI_Buffer[0]) 	 //if another slave address ignore message
  					{
  						PING_Status = PS_START;
  						return;
  					}
  					else 
  					{
  					  HandleSlaveFrame();		 //Get message informations
  					}  		
  					if(msg_num.whole==0)  //if message 0 reset counters
  					{
  						PING_Reset();
  					  PING_Status = PS_START;
  					  return;
  					}										 
  					else 											 //else process statistics
  					{	
  					  if ( msg_num.whole == old_msg_num.whole )// New mesage ?
  					     	rep_count++;						// No
  						else 									// Yes
  						{
  					    	if (rep_count)						// there are more then once repetition of message
  					          more_msg.whole++;
  						    if(PING_Flags&PF_FEC)
  							      corr_msg.whole++;
  						
  						    ok_msg.whole++;
  							  old_msg_num.whole = msg_num.whole; 
  						  	rep_count = 0;
  					  }    
  					}
  					PING_Status = PS_SEND;  
  				}
  				else 									//message wrong 
  				{
  				  EI;
  				  wrong_msg.whole++;
  					PING_Status = PS_START;
				  }
				break;
			case PS_SEND:										//RE-TRANSMIT MESSAGE
			   if(SPI_OnRun()) //wait rs-232 communication end 
					break;	
			
				if (CDMA_acces())				//if medium is free transmit the data, else wait
				{

⌨️ 快捷键说明

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