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

📄 func_own_wlan_detection.c

📁 2.4G无线音箱的接收程序,采用ATMEGA8单片机和STS公司的无线模块完成
💻 C
字号:
/******************************************************************************
*
* Purpose:      Own WLAN Detection Functions
*
* Creator:      Paul v/d Linden
*
* Version:		$Revision: 23 $
*
* File Name:	$Workfile: Func_Own_WLAN_Detection.c $
*
* Author:		Paul v/d Linden
*
* Check in:		$Author: Chong.cheeleong $
*
* The information is provided 揳s is?without any express or implied warranty  
* of any kind, * including warranties of merchantability, non infringement of   
* intellectual property, or fitness for any particular purpose. In no event shall
* Wireless Sound Solutions and/or its affiliate companies, or its suppliers be 
* liable for any damages whatsoever arising out of the use of or inability to  
* use the information or the materials. Wireless Sound Solutions, its affiliate
* companies, and its suppliers further do not warrant the accuracy or          
* completeness of information, text, graphics, or other items contained within 
* materials. Wireless Sound Solutions, Inc., may make changes to materials, or 
* the products described within, at any time, without notice.                  
* ?007 Wireless Sound Solutions. All rights reserved. Wireless Sound Solutions
* STS and STS-wireless are trademarks of Wireless Sound Solutions.        
******************************************************************************/
/*
** Include files
** -------------
*/
#include <stdio.h>
#include <ina90.h>
#include "defines.h"
#include <string.h>



/* Define detection Params */


#define OWNWLANDETLEVEL 25  /* Level above Background WLANs */
#define OWNWLANDETLEVEL_HIGH 25
#define OWNWLANDETLEVEL_LOW	10  /* Minimum Level WLAN tracking */
#define OWNWLANDETRFTIME	30  /* Detection time */

#define MIN 3*60
#define SEC 3

#define OWNWLANDETOFFTIME	(15 * MIN)      /* time that is will take that a Mask is removed when no Wlan is detected.*/          
#define REDUCE_OWNWLAN_TIME (30 * SEC)		/* interval that the Own Wlan is reduced when nowlan is detected */
//#define DISABLE_MASK_TIME 	(5  * MIN)		/* Time is will take when the "disable Mask" is removed again. */
//#define REDUCE_ASTH_TIMOUT  (40 * SEC)		/* interval that the DARR sniffer will be made more sensitive. */


#define MASK_REMOVE 5
#define NO_WLAN_DETECTED 6


/* Global Public Flag */
volatile unsigned char Process_New_Frame_Own_WLAN_Det = 0;

/* Global Private Data Declarations */
unsigned char MaxPeak[5]    = {0, 0, 0, 0, 0};
unsigned char MaxPeakAvg[5] = {0, 0, 0, 0, 0};
unsigned char last_max_i    = 6; /* 6 = Masked Own WLAN None */

unsigned char Cnt[5]={0, 0, 0, 0, 0};
unsigned int  CNT_TO = 0;
unsigned char CNT_RTL_TO =0;    
unsigned char CNT_ASTH_TO=0;
unsigned char OwnWLANMask = 0; /* no channel masked state value */
unsigned char OwnWLANDetLevel = OWNWLANDETLEVEL_HIGH; /* min tracking level */
unsigned char RFC_Reg;
unsigned char FMSK_Reg=0;
unsigned char Peak[5];
unsigned char Ratio[5];
unsigned char RF_Used=0;
unsigned char max_I_PeakAvg=0;
unsigned char Disable_Mask=0;
unsigned char QUIN_Reg=255;
volatile unsigned char BadLink_Cnt=0;
volatile unsigned char ASTH_Reg=55;
unsigned char Backup_ASTH_Reg = 55;
volatile unsigned char RFMR_Cnt=0;
unsigned char DARR_Sniffer_Enabled = 1;

unsigned char Freq_Cnt_Reduc_Time = 80;
unsigned char REDUCE_ASTH_TIMOUT = 40;
unsigned char DISABLE_MASK_TIME = 5;

/******************************************************************************
*
* Function Name :   Handle_Own_WLAN_Detection_Init
*
* Purpose       :   Initialize the Own WLAN Detection Procedure.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
void Handle_Own_WLAN_Detection_Init(void)
{
	/* to the normal INTM setting add New Frame Interrupts */

	I2C_Write_Byte(INTM_ADDR,        0x40 + 0x30 + 0x0E + 0x01); 
	
    OwnWLANMask = 0; /* no channel masked state value */

    OwnWLANDetLevel = OWNWLANDETLEVEL_HIGH; /* min traking level */
    Disable_Mask=0;
  
    BadLink_Cnt=0;
    QUIN_Reg=255;
    for (unsigned char i = 0; i < 5; i++) Cnt[i] = 0;
	
}


/******************************************************************************
*
* Function Name :   Handle_Own_WLAN_Detection_MainLoop
*
* Purpose       :   Process the Own WLAN Detection Procedure.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/

void Handle_Own_WLAN_Detection_MainLoop(void)
{
     
     
    /* Demask if to many Badsync or Frequency switch.*/
    if (Use_ModuleMode == MU) 
    {
      Disable_Mask = 0;
    }
#ifdef Target_CU
    else
    {     
        
		if ((Process_New_Frame_Own_WLAN_Det == 1) )
		{
		   
	        Process_New_Frame = 1;
		 if((InSync_Detected)&&(BadLink_Cnt == 50))		
		 {
		 	Handle_Own_WLAN_Detection_Init();
		 }	
	        Handle_Disable_Mask_DisableSniffer();  
	        
	#ifndef GUI
			/* Set Process Flag Low */
			Process_New_Frame_Own_WLAN_Det = 0;
	
			/*************************************/
	        /*** Read Sniffer information 	   ***/
	        /*************************************/
			I2C_Read_Buf(SPD1_ADDR, 25);
	
			for (unsigned char i = 0; i < 5; i++)
			{
				Peak[i] = 85 - RX_Data_recv_array[(5 * i)]; /* SPDx */
			}
	#endif

			/* Process Avg */
			for (unsigned char i = 0; i < 5; i++)
			{
				/* Take Max Peak Value */
				if (Peak[i] > MaxPeak[i]) MaxPeak[i] = Peak[i];
				/* Make an Average of Max Peak */
				MaxPeakAvg[i] += ((MaxPeak[i] - MaxPeakAvg[i]) >> 3);
				
				/* Decrease Max Peak by 1, Short high energy pulses are stretched */
				if (MaxPeak[i] >= 5) MaxPeak[i] -= 5;
				else if (MaxPeak[i] >= 1) MaxPeak[i]--;
			}
	
					
	    	/* define some local data */
	
	        unsigned char max_i = NO_WLAN_DETECTED; /* none */
	        max_I_PeakAvg=0;
		 unsigned char new_FMSK_Reg = 0 ;
	        /*************************************/
	        /*** define new mask 	  		   ***/
	        /*************************************/
	    	/* Test 5 RF channels 10 sec active and stable to apply mask. */
	        max_I_PeakAvg =0;
	    	for (unsigned char i = 0; i < 5; i++)
	    	{
	    		if (MaxPeakAvg[i] > OwnWLANDetLevel)
	        	{
	        		Cnt[i]++;
	                if ((Disable_Mask == 0))
	                	CNT_TO = 0; /* reset timeout */
	               
	        	}
	        	else Cnt[i] = 0;
	        	if (Cnt[i] > OWNWLANDETRFTIME)
	        	{
	        		Cnt[i] = OWNWLANDETRFTIME;
	              
	                if (MaxPeakAvg[i] > max_I_PeakAvg)
	                {
	        			max_i = i; /* 1 of 5 channels selected (0--4) */
	                  	max_I_PeakAvg = MaxPeakAvg[i];
	                }
	               	if (i == 0) new_FMSK_Reg =  new_FMSK_Reg | 0x01; /* mask RF1 */
		           	if (i == 1) new_FMSK_Reg =  new_FMSK_Reg | 0x02; /* mask RF2 */
		           	if (i == 2) new_FMSK_Reg =  new_FMSK_Reg | 0x04; /* mask RF3 */
		           	if (i == 3) new_FMSK_Reg =  new_FMSK_Reg | 0x03; /* mask RF1 & RF2 */
		          	if (i == 4) new_FMSK_Reg =  new_FMSK_Reg | 0x06; /* mask RF2 & RF3 */
	        	}
			}
	        if ((FMSK_Reg |new_FMSK_Reg) == 0x07)
	        {
	             if (max_i == 0) new_FMSK_Reg =  0x01; /* mask RF1 */
		         if (max_i == 1) new_FMSK_Reg =  0x02; /* mask RF2 */
		         if (max_i == 2) new_FMSK_Reg =  0x04; /* mask RF3 */
		         if (max_i == 3) new_FMSK_Reg =  0x03; /* mask RF1 & RF2 */
		         if (max_i == 4) new_FMSK_Reg =  0x06; /* mask RF2 & RF3 */
	        }
	        if (((FMSK_Reg & 0x0F) |(new_FMSK_Reg & 0x0F)) != 0x07)
	        {
	             /* Add New MASK */
	             FMSK_Reg = (FMSK_Reg |new_FMSK_Reg) & 0x0F;
	        }
	        else
	        {
	             /* All RF masked so use current Detection as new mask.*/	
	             FMSK_Reg = new_FMSK_Reg & 0x0F;
	        }
	       	/* There is a Stable Mask */
		
	      	/* Test count timeout mask remove */
	    	if ((max_i == NO_WLAN_DETECTED)|| (Disable_Mask != 0)) CNT_TO += 1;
	    	else CNT_TO = 0;
	        
	        /*************************************/
	        /*** Remove Move Masking	       ***/
	        /*************************************/
	    	if (CNT_TO > OWNWLANDETOFFTIME)
	    	{
	    	    /* reduce Own Wlan Threshold when no wlan is detected */
	            CNT_TO = 0;
	            CNT_RTL_TO = REDUCE_OWNWLAN_TIME;   /* Force Threshold reduction */
	            FMSK_Reg = 0; /*Remove Mask */
	            max_i = MASK_REMOVE;
	    	}
			/************************************/
	        /*** Reduce Own Wlan Threshold    ***/
	        /************************************/
	        if (CNT_RTL_TO > REDUCE_OWNWLAN_TIME)		
	        {
	            CNT_RTL_TO = 0;
	           
	            if (OwnWLANDetLevel > OWNWLANDETLEVEL_LOW) OwnWLANDetLevel -= 1;
	        }
		//	printf ("Cnt2 = %d %d %d %d %d %d %d\r", Cnt[0], Cnt[1], Cnt[2], Cnt[3], Cnt[4], CNT_TO,CNT_RTL_TO,CNT_ASTH_TO);
	     
		    if (max_i != NO_WLAN_DETECTED)
		    {
	                CNT_RTL_TO = 0;
		            if (max_i != MASK_REMOVE)  OwnWLANDetLevel = max_I_PeakAvg;
	                if (    OwnWLANDetLevel> OWNWLANDETLEVEL_HIGH) OwnWLANDetLevel=OWNWLANDETLEVEL_HIGH ;
		    }
	        else  CNT_RTL_TO++;
	        
	        /************************************/
	        /*** Increase DARR Sniffer        ***/
	        /************************************/
	        if (CNT_ASTH_TO > REDUCE_ASTH_TIMOUT*3)
		    {
	        	CNT_ASTH_TO = 0;
		        if ((ASTH_Reg<70) && (DARR_Sniffer_Enabled)) ASTH_Reg++;
		    }
	        else  CNT_ASTH_TO++;
	       
		    if((ASTH_Reg != Backup_ASTH_Reg)&&(REDUCE_ASTH_TIMOUT == 0xFF))
		    {
			    Backup_ASTH_Reg = ASTH_Reg;
			    I2C_Write_Byte(ASTH_ADDR, ASTH_Reg);
		    }
	   		/************************************/  
			/*** Update MAsk and RFC Register ***/  
			/************************************/  

	        if ((Disable_Mask == 0))
	        {	          
	        	if ((OwnWLANMask != FMSK_Reg))
	        	{
	        	
	            	OwnWLANMask = FMSK_Reg;
	            	for (unsigned char i = 0; i < 5; i++) Cnt[i] = 0;
	    			/* Send Out the Mask Data to the DARR register */
	                #ifndef GUI
	                I2C_Write_Byte(FMSK_ADDR, FMSK_Reg);
	                #endif	              
	        	}
			
	
	
			    /* Try to correct RF channel selected */
			   
		#ifndef GUI    
		            RF_Used = I2C_Read_Byte(RFC_ADDR)&0x03;
		#endif
		            RFC_Reg = 0;
			    if ((FMSK_Reg == 0x03))        /* Double mask RF1 RF2*/
			    {
		               if (RF_Used != 2)
			  	    	RFC_Reg = 0xBA; /* Force to RF3 */
		
			    }
			    else
			    if ((FMSK_Reg == 0x06) )        /* Double mask RF2 RF3*/
			    {
		                if (RF_Used != 0)
			    	  RFC_Reg = 0xB8; /* Force to RF1 */
		
			    }else
		        if ((FMSK_Reg == 0x05) )        /* Double mask RF1 RF3*/
			    {
		                if  (RF_Used != 1)
			    	    RFC_Reg = 0xB9; /* Force to RF2 */
		
			    }
		        else
		        if (((FMSK_Reg-1) == RF_Used)|| ((FMSK_Reg == 4)&& (RF_Used == 2)))  /* Single MASK */
			    {
			        unsigned char min_i = 6; /* none */
			        unsigned char min_level = 0xFF;
			
			        /* Test 3 RF channels for minimum AGC */
			        for (unsigned char i = 0; i < 3; i++)
			        {
			        	if (MaxPeakAvg[i] < min_level)
			            {
			            	min_level = MaxPeakAvg[i];
			                min_i = i;
			            }
			        }
			        if (min_i == 0) RFC_Reg = 0xB8; /* Force to RF1 */
			        if (min_i == 1) RFC_Reg = 0xB9; /* Force to RF2 */
			        if (min_i == 2) RFC_Reg = 0xBA; /* Force to RF3 */         
			    }
		        if (RFC_Reg!=0) 
		        {
		        	#ifndef GUI
		        	I2C_Write_Byte(RFC_ADDR, RFC_Reg);		                   
		          	#endif 
		        }
	        }

		}

    }
#endif /*Target_CU*/    



}

/******************************************************************************
*
* Function Name :   Handle_Disable_Mask_DisableSniffer
*
* Purpose       :   This handle the disabling of masking and turning of the DARR sniffer when the RF
                    enviroment is too severe to have robust radio.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
unsigned char Quin_NACK_Appl[10] = {80,130,150,180,120,160,80,130,150,180};

void Handle_Disable_Mask_DisableSniffer(void)
{
#ifdef Target_CU
    unsigned char QUIN_40Per;
    QUIN_Reg = QUIN_Reg - (QUIN_Reg>>2) + (I2C_Read_Byte(QUIN_ADDR) >> 2);
               
      
    QUIN_40Per = Quin_NACK_Appl[Use_Application];
    /************************************/
	/*** Disable Masking		      ***/
	/************************************/
    /* Remove mask for when PER is to high  */
    if (QUIN_Reg < QUIN_40Per)
    {
        
        if ( Disable_Mask == 0) 
        { 
            DARR_Sniffer_Enabled = 0;
            I2C_Write_Byte(ASID_ADDR, 0); /* Disable DARR Sniffer */
            I2C_Write_Byte(ASC_ADDR, 0x60); /* FAA uses timer_Controll to make jump */
              
    		Disable_Mask = 128;	  /* Mask Disabled */
            I2C_Write_Byte(FMSK_ADDR, 0);
            
            OwnWLANMask = 0;
            
        }
     
       CNT_TO = 0;   /* Keep counter low to keep mask disabled */
    }
    else 
    {
      if (CNT_TO > (DISABLE_MASK_TIME*3*60) )		
      {
        Disable_Mask = 0;
        FMSK_Reg = 0;
           for (unsigned char i = 0; i < 5; i++) Cnt[i] = 0;          
      }
    }
    
    /************************************/
	/*** Disable DARR Sniffer         ***/
	/************************************/
    if ((BadLink_Cnt > 4) )
    {        
        if ( DARR_Sniffer_Enabled == 1) 
        { 
            DARR_Sniffer_Enabled = 0;
            I2C_Write_Byte(ASID_ADDR, 0); /* Disable DARR Sniffer */    		          
            I2C_Write_Byte(ASC_ADDR, 0x60); /* FAA uses timer_Controll to make jump */
          
        }       
    }
   
	if (RFMR_Cnt>=Freq_Cnt_Reduc_Time) /* Check every*/
	{
        if ((BadLink_Cnt==0) && (DARR_Sniffer_Enabled == 0)&&
            Disable_Mask == 0)
        { 
          DARR_Sniffer_Enabled = 1;
          I2C_Write_Byte(ASID_ADDR, 0x85); /* Enable DARR Sniffer */    
          I2C_Write_Byte(ASC_ADDR, 0xE0); /* FAA uses timer_Controll to make jump */
          
        }    

        if ((BadLink_Cnt>10)&&(BadLink_Cnt !=50)) BadLink_Cnt=10;      		        
        if (BadLink_Cnt>0) BadLink_Cnt--;              
		RFMR_Cnt = 0;      
       
    }   
#ifndef	NO_GUI
	Show_RF_Status();	
#endif	/*NO_GUI*/
#endif /*Target_CU*/
}





⌨️ 快捷键说明

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