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

📄 func_connect.c

📁 2.4G无线音箱的接收程序,采用ATMEGA8单片机和STS公司的无线模块完成
💻 C
字号:
/******************************************************************************
*
* Purpose:      Func_Connect.c
*
* Creator:      Paul v/d Linden
*
* Version:		$Revision: 13 $
*
* File Name:	$Workfile: Func_Connect.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"


/*
** Message definitions 
*/
/*
 * Unique Key Message
 * Byte 1  : 90
 * Byte 2  : Unique Key LSB
 * Byte 3  : Unique Key MSB
 * Byte 16 : Checksum.
 */
#define MSG_TYPE_UNIQUE_KEY	90 /* 0x5B */

/*
 * Key Acknowledge Message
 * Byte 1  : 91
 * Byte 16 : Checksum.
 */
#define MSG_TYPE_KEY_ACK 91 /* 0x5C */

#define TIME_CONNECT_TIMEOUT 60

unsigned char Connect_Timer   = 0xFF;
unsigned int  Connection_Code = 0xFFFF;
unsigned char prev_CONNECT_SW;



/******************************************************************************
*
* Function Name :   Prog_ConnectCode
*
* Purpose       :   Programs Connect code into DARR79 and EEPROM
*
* Arguments     :   Connection_Code
*
* Return value  :   none
*
******************************************************************************/
void Prog_ConnectCode(unsigned int Connection_Code)
{
	unsigned char tmp1,tmp2;

	tmp1 = Connection_Code & 0x0FF;
	tmp2 = (Connection_Code >> 8) & 0xFF;
	
	I2C_Write_Byte(COCO_ADDR, tmp1);
	I2C_Write_Byte(COCO_ADDR + 1, tmp2);
	_CLI();
	_EEPUT(0x10, tmp1);
	_EEPUT(0x11, tmp2);
	_SEI();
}


/******************************************************************************
*
* Function Name :   Get_ConnectCode
*
* Purpose       :   Get connect code.
*
* Arguments     :   none
*
* Return value  :   Connection_Code
*
******************************************************************************/
int Get_ConnectCode(void)
{
	int Con_Code;
	unsigned char tmp;

	_CLI();
	_EEGET(tmp, 0x10);
	Con_Code = tmp;
	_EEGET(tmp, 0x11);
	Con_Code += tmp << 8;
	_SEI();
	
	return (Con_Code);
}


/******************************************************************************
*
* Function Name :   Handle_Connect_Init
*
* Purpose       :   Initialize the Connect Procedure.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
void Handle_Connect_Init(void)
{
	/* Setup Switch Data */
	prev_CONNECT_SW    = CONNECT_SW; /* prevent initial difference */
	
	/* Prepare Connect Code */
	if  (ERASE_CON_CODE_SW == 0)
	{
		/* Reset connection code (no encryption used when connection code is 0xFFFF */
		Prog_ConnectCode(0xFFFF); 
		putstring("Connection_Code = FFFF (RESET)\r");
	}

	/* read connect code from Eeprom */
	Connection_Code = Get_ConnectCode();
	/* write COCO register in DARR79 */
	I2C_Write_Byte(COCO_ADDR, Connection_Code & 0x0FF);
	I2C_Write_Byte(COCO_ADDR + 1, (Connection_Code >> 8) & 0xFF);
	
	#ifdef DEBUG
		putstring("Connection_Code = ");
		puthex((Connection_Code >> 8) & 0xFF);
		putstring(" ");
		puthex( Connection_Code       & 0xFF);
		putstring("\r");
	#endif

	/*
	 * programmed do not use Connect after first time program or reset
	 * using the Volume buttons 
	 */
	/* always startup with CU connect code active */
	if  (Use_ModuleMode == CU) 
	{
		#ifdef DEBUG
			putstring("Connect_Bit SET\r");
		#endif
		Connect_Bit = BIT_CONNECT; /* 0x80 */
	}
	else 
	{
		Connect_Bit = 0x00;
	}
		
	/* Enable Unit & update connect status */
	Write_GEC_ADDR(BIT_ENABLE + Connect_Bit);
}

/******************************************************************************
*
* Function Name :   Handle_Hot_Disconnect
*
* Purpose       :   Handle Hot Disconnect Procedure.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
#ifdef PCB_PNP80_UNIVERSAL_TRX
void Handle_Hot_Disconnect_MainLoop(void)		
{
	/* Reset connection code (no encryption used when connection code is 0xFFFF */
	Prog_ConnectCode(0xFFFF); 

    /* read connect code from Eeprom */
    Connection_Code = Get_ConnectCode();
    /* write COCO register in DARR80 */
    I2C_Write_Byte(COCO_ADDR, Connection_Code & 0x0FF);
    I2C_Write_Byte(COCO_ADDR + 1, (Connection_Code >> 8) & 0xFF);
	
	Connect_Bit = BIT_CONNECT; /* 0x80 */
   
    /* Enable Unit & update connect status */
    Write_GEC_ADDR(BIT_ENABLE + Connect_Bit);
            
}
#endif /*PCB_PNP80_UNIVERSAL_TRX*/
/******************************************************************************
*
* Function Name :   Handle_Connect_MainLoop
*
* Purpose       :   Process the Connection Procedure.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
void Handle_Connect_MainLoop(void)
{
#ifndef PCB_PNP80_UNIVERSAL_TRX
	if (Sync_Led_Status == LED_OFF)
	{
		/* prevent starting Connect Procedure twice */
		/* release the button first before starting again */
		if ((prev_CONNECT_SW != CONNECT_SW))
		{
			prev_CONNECT_SW = CONNECT_SW;
			/* start connect procedure only once */
			if (CONNECT_SW == 0x00)	/* only when key is pressed down */
			{
				DATA_LED_ON;
				if (Use_ModuleMode == CU)
				{
					/*###### CU ######*/
					Connect_Bit = 0;
					/* Turn off Connect only on CU */
					Write_GEC_ADDR( BIT_ENABLE); 
				}
				/* start Connect timeout timer */
				Connect_Timer = 0x00;
				/* change state */
				Sync_Led_Status = LED_CONNECTING;
				DATA_LED_OFF
			}
		}
	}
#else
    if (Sync_Led_Status == LED_SYNC_STATUS)
	{       
		/* prevent starting Connect Procedure twice */
		/* release the button first before starting again */
        if ( prev_CONNECT_SW != CONNECT_SW )
        {                 
           /* Update CONNECT_SW */
           prev_CONNECT_SW = CONNECT_SW;

           if ( !CONNECT_SW )  
           {
                /* 3 seconds delay */
                Wait_For_mSec(100);             
                
                unsigned char i ;
                for(i=50;i>0;i--)          
                {
                	if ( !CONNECT_SW )
                	{
                			Wait_For_mSec(100); 
                	}	
                	else
                	{
                			Connect_Bit = 0x00;
                            /* Turn off Connect only on CU */
                            Write_GEC_ADDR(BIT_ENABLE);
                            /* start Connect timeout timer */
                            Connect_Timer = 0x00;
                            /* change state */
                            Sync_Led_Status = LED_CONNECTING;
                            break ;
                	}	
                }
                if(i==0)
                {	
                		Handle_Hot_Disconnect_MainLoop();
                            /* Set to default link status */
                        Sync_Led_Status = LED_SYNC_STATUS; 
                }	
                /* After 3 seconds check button pressed again */
//                if ( !CONNECT_SW ) 
//                {
//                    Connection_Code = Get_ConnectCode();
//                    /* Check if not connected */
//                    if ( Connection_Code == 0xFFFF )
//                    {   
//                        
//                        Connect_Bit = 0x00;
//                        /* Turn off Connect only on CU */
//                        Write_GEC_ADDR(BIT_ENABLE);
//                      
//                        /* start Connect timeout timer */
//                        Connect_Timer = 0x00;
//                        /* change state */
//                        Sync_Led_Status = LED_CONNECTING;
//                                      
//                    }
//                    /* Already connected */
//                    else
//                    {
//
//                      /* 5 seconds delay */
//                         Wait_For_mSec(5000);                       
//                         if(!CONNECT_SW)
//                         {
//                            /* Reset Connect Code to default */
//                            Handle_Hot_Disconnect_MainLoop();
//                            /* Set to default link status */
////                        Sync_Led_Status = LED_SYNC_STATUS;    
//                         }
//                    }             
//                }
            }
		}
	}
#endif /*PCB_PNP80_UNIVERSAL_TRX*/
	else
	if (Sync_Led_Status == LED_CONNECTING)
	{
		/********************************************/
		/*** 1 Second Timed on CU Process Connect ***/
		/********************************************/
		/* Sec_Event flag is set by MCU timer */
		if (Sec_Event == 1)
		{
			Sec_Event = 0; /*Cleared in end of mailloop*/
			Connect_Timer++;

			if (Use_ModuleMode == CU)	
			{
				DATA_LED_ON;
				TX_Data_send_array[0] = MSG_TYPE_UNIQUE_KEY;
				/*
				** Generate Unique Connection Code
				** (Key press generates a random function in time)
				** Only generate one time at power-up (should be stored in EEPROM)
				*/
				if (Connection_Code == 0xFFFF)
				{
					Connection_Code = RND_Value;
					if (Connection_Code == 0) Connection_Code = 1; /* Connect code 0 is not allowed */
				}
				/* Send Connection_Code */
				TX_Data_send_array[1] = Connection_Code & 0xFF;
				TX_Data_send_array[2] = (Connection_Code >> 8) & 0xFF;
				
				#ifdef DEBUG
					putstring("CU send con_code ");
					puthex(TX_Data_send_array[0]);
					putchar(' ');
					puthex(TX_Data_send_array[1]);
					putchar(' ');
					puthex(TX_Data_send_array[2]);
					putchar('\r');
				#endif

				if (Use_Speaker_Enable_Bits & 0x01) Send_Message(STR_A, 1);
				if (Use_Speaker_Enable_Bits & 0x02) Send_Message(STR_B, 1);
				if (Use_Speaker_Enable_Bits & 0x04) Send_Message(STR_C, 1);
				if (Use_Speaker_Enable_Bits & 0x08) Send_Message(STR_D, 1);
				
				DATA_LED_OFF;
			}
		
			/********************************/
			/*** Check Connection Timeout ***/
			/********************************/
			if (Connect_Timer > TIME_CONNECT_TIMEOUT)
			{
				DATA_LED_ON;
								
				#ifdef NOT_THIS_TIME
				/* Connect_Bit is set only on a CU */
				/*
				** Use this code only in multiple MU applications
				** Do not leave the LED_CONNECTING state on receiving 
				** the first Key Ack msg.
				** Process them and switch to LED_OFF when all MUs
				** had send there Key Ack msg.
				** Only in that case this code section is needed
				** When no response is coming from any MU the
				** Connect_Bit stays 0, audio insecure.
				*/
				if (Use_ModuleMode == CU)	
				{
					/* Do not use connection when a time-out occurred and */
					/* no other units have been connected yet */
					if (Get_ConnectCode() != 0xFFFF)
					{
						#ifdef DEBUG
							putstring("Timeout, Connect_Bit = 0x80\r");
						#endif
						Connect_Bit = BIT_CONNECT; /* 0x80 */
						Write_GEC_ADDR( BIT_ENABLE + Connect_Bit);
					}
				}
				#endif 
#ifndef PCB_PNP80_UNIVERSAL_TRX
				/* Update the RF leds */
				Update_LED(Read_RFC());
				/* leave this the Connection Procedure Unsuccessful */
				Sync_Led_Status = LED_OFF;
#else
                Sync_Led_Status = LED_SYNC_STATUS;
#endif
				DATA_LED_OFF;
			}
		}
	}
}


/******************************************************************************
*
* Function Name :   Handle_Connect_RXMsg
*
* Purpose       :   Handle the RX Messages for the Connection Procedure.
*
* Arguments     :   none
*
* Return value  :   none
*
******************************************************************************/
void Handle_Connect_RXMsg(void)
{
	if (Sync_Led_Status == LED_CONNECTING)
	{
		if (Use_ModuleMode == MU)
		{
			/*###### MU ######*/
			if (RX_Data_recv_array[0] == MSG_TYPE_UNIQUE_KEY)
			{
				Prog_ConnectCode(RX_Data_recv_array[1] + (RX_Data_recv_array[2] << 8));

				#ifdef DEBUG
					putstring("MU send MSG_TYPE_KEY_ACK\r");
				#endif

				TX_Data_send_array[0] = MSG_TYPE_KEY_ACK;
				if (Data_Free_Up)
					Send_Message(STR_FREE, 1); /* use free slots in nack applic */
				else
					Send_Message(RX_Stream_Nr, 1); /* reply on received stream number */
#ifndef PCB_PNP80_UNIVERSAL_TRX

				Update_LED(Read_RFC());
				Sync_Led_Status = LED_OFF;
#else
                Sync_Led_Status = LED_SYNC_STATUS;
#endif
			}
		}
		else
		{
			/*###### CU ######*/
			if (RX_Data_recv_array[0] == MSG_TYPE_KEY_ACK)
			{
				/* MU has Acknowledged Connect key there for the encription can be turned on */
				#ifdef DEBUG
					putstring("CU MSG_TYPE_KEY_ACK\n");
				#endif

				Prog_ConnectCode(Connection_Code);

				#ifdef DEBUG
					putstring("CU SET CONNECT_BIT\n");
				#endif

				Connect_Bit = BIT_CONNECT; /* 0x80 */
				Write_GEC_ADDR( BIT_ENABLE + Connect_Bit);
				
#ifndef PCB_PNP80_UNIVERSAL_TRX
				Update_LED(Read_RFC());
				Sync_Led_Status = LED_OFF;
#else
                Sync_Led_Status = LED_SYNC_STATUS;
#endif
			}
		}
	}
}


⌨️ 快捷键说明

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