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

📄 protocol.c

📁 光电鼠标IC资料
💻 C
📖 第 1 页 / 共 3 页
字号:
/*============================================================================
PROJECT...............: Wireless Headset
FILE..................: protocol.c
INITIAL AUTHOR........: Nordic Semiconductor
INITIAL DATE..........: 15.04.2005
COMPILER..............: avr-gcc (GCC) 3.4.3

Description : Functions handling the RF protocol (Master and Slave).


Modification history:

DATE      		VERSION		COMMENT
-----------------------------------------------------------------------------
03.11.2006		1.0			Initial version
21.11.2006		1.1			Audio buffer synchronisation flags
06.12.2006		1.2			Test SW includes Modulated Transmitter				  
============================================================================*/

/***************************************************************************
* Library Includes
***************************************************************************/
#include <avr/io.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>

/***************************************************************************
* Local Includes
***************************************************************************/
#include "protocol.h"
#include "radio.h"
#include "main.h"
#include "mcu.h"

/***************************************************************************
* Constants
***************************************************************************/
const unsigned int Alaw_expand[128] PROGMEM=
{
0,16,32,48,64,80,96,112,
128,144,160,176,192,208,224,240,
256,272,288,304,320,336,352,368,
384,400,432,448,464,480,496,528,
544,576,592,624,640,672,704,736,
768,800,832,864,912,944,992,1024,
1072,1120,1168,1216,1280,1328,1392,1456,
1520,1584,1648,1712,1792,1872,1952,2032,
2128,2224,2320,2416,2528,2624,2752,2864,
2992,3120,3248,3392,3552,3696,3856,4032,
4208,4384,4576,4784,4992,5200,5440,5664,
5920,6176,6448,6720,7024,7328,7648,7984,
8320,8688,9072,9472,9872,10304,10768,11232,
11728,12224,12768,13328,13904,14512,15152,15808,
16496,17216,17968,18752,19568,20432,21312,22256,
23216,24240,25296,26400,27552,28752,30016,31328,
};

const char Alaw_compress[2048] PROGMEM=
{
0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,
16,17,18,19,20,21,22,23,24,25,25,26,27,28,29,30,
30,31,32,32,33,34,34,35,36,36,37,37,38,38,39,39,
40,40,41,41,42,42,43,43,43,44,44,45,45,45,46,46,
47,47,47,48,48,48,49,49,49,50,50,50,51,51,51,51,
52,52,52,53,53,53,53,54,54,54,54,55,55,55,55,56,
56,56,56,57,57,57,57,58,58,58,58,59,59,59,59,59,
60,60,60,60,60,61,61,61,61,61,62,62,62,62,62,63,
63,63,63,63,63,64,64,64,64,64,64,65,65,65,65,65,
65,66,66,66,66,66,66,67,67,67,67,67,67,67,68,68,
68,68,68,68,69,69,69,69,69,69,69,69,70,70,70,70,
70,70,70,71,71,71,71,71,71,71,71,72,72,72,72,72,
72,72,72,73,73,73,73,73,73,73,73,74,74,74,74,74,
74,74,74,74,75,75,75,75,75,75,75,75,75,75,76,76,
76,76,76,76,76,76,76,77,77,77,77,77,77,77,77,77,
77,78,78,78,78,78,78,78,78,78,78,78,79,79,79,79,
79,79,79,79,79,79,79,80,80,80,80,80,80,80,80,80,
80,80,81,81,81,81,81,81,81,81,81,81,81,81,82,82,
82,82,82,82,82,82,82,82,82,82,82,83,83,83,83,83,
83,83,83,83,83,83,83,83,84,84,84,84,84,84,84,84,
84,84,84,84,84,85,85,85,85,85,85,85,85,85,85,85,
85,85,85,85,86,86,86,86,86,86,86,86,86,86,86,86,
86,86,87,87,87,87,87,87,87,87,87,87,87,87,87,87,
87,87,88,88,88,88,88,88,88,88,88,88,88,88,88,88,
88,88,89,89,89,89,89,89,89,89,89,89,89,89,89,89,
89,89,89,90,90,90,90,90,90,90,90,90,90,90,90,90,
90,90,90,90,91,91,91,91,91,91,91,91,91,91,91,91,
91,91,91,91,91,91,91,92,92,92,92,92,92,92,92,92,
92,92,92,92,92,92,92,92,92,92,93,93,93,93,93,93,
93,93,93,93,93,93,93,93,93,93,93,93,93,93,94,94,
94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,94,
94,94,94,95,95,95,95,95,95,95,95,95,95,95,95,95,
95,95,95,95,95,95,95,95,96,96,96,96,96,96,96,96,
96,96,96,96,96,96,96,96,96,96,96,96,96,96,96,97,
97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,97,
97,97,97,97,97,97,97,98,98,98,98,98,98,98,98,98,
98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,98,
99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,99,
99,99,99,99,99,99,99,99,99,100,100,100,100,100,100,100,
100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,100,
100,100,100,100,101,101,101,101,101,101,101,101,101,101,101,101,
101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,101,
101,102,102,102,102,102,102,102,102,102,102,102,102,102,102,102,
102,102,102,102,102,102,102,102,102,102,102,102,102,102,103,103,
103,103,103,103,103,103,103,103,103,103,103,103,103,103,103,103,
103,103,103,103,103,103,103,103,103,103,103,103,103,104,104,104,
104,104,104,104,104,104,104,104,104,104,104,104,104,104,104,104,
104,104,104,104,104,104,104,104,104,104,104,104,105,105,105,105,
105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,105,
105,105,105,105,105,105,105,105,105,105,105,105,105,105,106,106,
106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,
106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,106,
106,107,107,107,107,107,107,107,107,107,107,107,107,107,107,107,
107,107,107,107,107,107,107,107,107,107,107,107,107,107,107,107,
107,107,107,107,107,108,108,108,108,108,108,108,108,108,108,108,
108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,108,
108,108,108,108,108,108,108,108,108,108,108,109,109,109,109,109,
109,109,109,109,109,109,109,109,109,109,109,109,109,109,109,109,
109,109,109,109,109,109,109,109,109,109,109,109,109,109,109,109,
109,109,109,110,110,110,110,110,110,110,110,110,110,110,110,110,
110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,110,
110,110,110,110,110,110,110,110,110,110,110,110,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,111,
111,111,111,111,111,111,111,112,112,112,112,112,112,112,112,112,
112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,
112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,112,
112,112,112,112,113,113,113,113,113,113,113,113,113,113,113,113,
113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,
113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,113,
113,113,113,114,114,114,114,114,114,114,114,114,114,114,114,114,
114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,
114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,114,
114,114,114,114,115,115,115,115,115,115,115,115,115,115,115,115,
115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,
115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,115,
115,115,115,115,115,115,115,116,116,116,116,116,116,116,116,116,
116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,116,
116,116,116,116,116,116,116,116,116,116,116,116,116,117,117,117,
117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,
117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,
117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,117,
117,117,117,117,118,118,118,118,118,118,118,118,118,118,118,118,
118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,
118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,
118,118,118,118,118,118,118,118,118,118,118,118,118,118,118,119,
119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,
119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,
119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,119,
119,119,119,119,119,119,119,119,119,119,119,120,120,120,120,120,
120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,
120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,
120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,120,
120,120,120,120,120,120,120,120,120,120,120,121,121,121,121,121,
121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,
121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,
121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,121,
121,121,121,121,121,121,121,121,121,121,121,121,121,122,122,122,
122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,
122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,
122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,
122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,122,
122,122,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,123,
123,123,123,123,123,123,123,123,123,123,124,124,124,124,124,124,
124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,
124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,
124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,
124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,124,
124,124,124,124,124,125,125,125,125,125,125,125,125,125,125,125,
125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,
125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,
125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,
125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,125,
125,125,125,125,126,126,126,126,126,126,126,126,126,126,126,126,
126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,
126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,
126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,
126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,126,
126,126,126,126,126,126,127,127,127,127,127,127,127,127,127,127,
127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,
127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,
127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,
127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,
127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,127,
};

/***************************************************************************
* Types 
***************************************************************************/

/***************************************************************************
* Variables 
***************************************************************************/
char protocol_flags = 0;
static unsigned int event_times[6];
static unsigned int master_sync_times[2];
unsigned int *event_time;
//static int packet_loss[2];

/***************************************************************************
* Macros 
***************************************************************************/

/***************************************************************************
* Prototypes
***************************************************************************/
void put_audio_packet(void);
void get_audio_packet(void);
void stuff_packet(void);
char compress_audio(int audio_linear16);
int expand_audio(char audio_compressed8);

/***************************************************************************
Declaration : char call_setup(char *freq_list,char n_freqs,unsigned int n_rep)

Description : A 'Call_Setup' packet is transmitted at the given frequencies. 
			  After each transmission it waits for a 'Call_Setup_Ack' packet
			  to be received. If an acknowledge is received it returns the
			  frequency used or else it returns CALL_SETUP_FAILURE when
			  there is no longer call activity.
***************************************************************************/
char call_setup(char *freq_list,char n_freqs)
{
char i;
char *freq_ptr;
char status;
char payload[3];
	
	// Set Payload = 'Call_Setup' packet
	init_rf_signalling();
	payload[0] = SIGNAL_CALL_SETUP;
	payload[1] = freq[0];
	payload[2] = freq[1];
	write_rf_data(W_TX_PAYLOAD,&payload[0],3);
	
	// Re-use payload for all following transmissions
	reuse_rf_payload();

	// Repeat frequency sequence n_rep times
	call_status = CALL_ACTIVITY;
	while(call_status == CALL_ACTIVITY)
	{
		freq_ptr = freq_list;
		
		// Try to set up call at each frequency
		for(i=0; i<n_freqs; i++)
		{
			// Send 'Call_Setup' packet
			set_rf_tx(*freq_ptr);
			clear_rf_interrupt();
			rf_enable_pulse();
			status = wait_rf_irq(TIMEOUT_CALL_SETUP_PACKET,DIV8);
			if(status == STATUS_TIMEOUT)
				return(CALL_SETUP_FAILURE);
			
			// Wait for 'Call_Setup_Ack' to be received
			set_rf_rx(*freq_ptr);
			clear_rf_interrupt();
			RF_ENABLE;
			status = wait_rf_irq(TIMEOUT_CALL_SETUP_PACKET,DIV8);
			RF_DISABLE;

			// If 'Call_Setup_Ack' is received return with RF channel
			if (status != STATUS_TIMEOUT)
			{
				// check if valid Call Setup Ack Packet
				ENABLE_RF_SPI;
				read_rf_byte(R_RX_PAYLOAD);
				payload[0] = read_rf_byte(R_RX_PAYLOAD);
				payload[1] = read_rf_byte(R_RX_PAYLOAD);
				DISABLE_RF_SPI;
				if((payload[0] == SIGNAL_CALL_SETUP_ACK0) && (payload[1] == SIGNAL_CALL_SETUP_ACK1))
					return(*freq_ptr);
				else
					freq_ptr++;
			}		
			// else try next frequency
			else
				freq_ptr++;
		}
		
		#ifdef USB
		if(!CALL_ACTIVITY_PIN)
		{
			call_activity_timer += 1;
			if(call_activity_timer >= TIMEOUT_CALL_ACTIVITY)
				call_status = CALL_NO_ACTIVITY;
		}
		else
			call_activity_timer = 0;
		
		// Abort if USB Suspend Mode
		if(USB_SUSPEND)
			return(CALL_SETUP_FAILURE);
		
		#else
		if(!CALL_CLEAR_KEY)
			call_status = CALL_NO_ACTIVITY;
		#endif
	}

	// No 'Call_Setup_Ack' is received
	return(CALL_SETUP_FAILURE);
}

/***************************************************************************
Declaration : char call_detect(char *freq_list,char n_freqs,unsigned int n_rep)

Description : A 'Call_Setup'is searched for at the given frequencies. 
			  If a 'Call_Setup' is received, a 'Call_Setup_Ack' is transmitted
			  and the frequency used is returned.
			  If no 'Call_Setup'packet is received during the repetitions
			  given by n_rep, it returns CALL_SETUP_FAILURE.
***************************************************************************/
char call_detect(char *freq_list,char n_freqs,unsigned int n_rep)
{
unsigned int i,j;
char *freq_ptr;
char status;
char payload[3];

	// Set Payload = 'Call_Setup_Ack' packet
	init_rf_signalling();
	payload[0] = SIGNAL_CALL_SETUP_ACK0;
	payload[1] = SIGNAL_CALL_SETUP_ACK1;
	payload[2] = 0;
	write_rf_data(W_TX_PAYLOAD,&payload[0],3);
	reuse_rf_payload();
	
	// Repeat frequency sequence n_rep times
	for(i=0; i<n_rep; i++)
	{
		freq_ptr = freq_list;
		
		// Try to set up call at each frequency
		for(j=0; j<n_freqs; j++)
		{
			// Wait for 'Call_Setup' to be received
			set_rf_rx(*freq_ptr);
			clear_rf_interrupt();
			RF_ENABLE;
			status = wait_rf_irq(TIMEOUT_CALL_SETUP_PACKETS,DIV64);
			RF_DISABLE;
			
			// If 'Call_Setup' is received return with RF channel
			if (status != STATUS_TIMEOUT)
			{
				// Send 'Call_Setup_Ack' packet and return RF Channel
				set_rf_tx(*freq_ptr);
				clear_rf_interrupt();
				rf_enable_pulse();
				status = wait_rf_irq(TIMEOUT_CALL_SETUP_PACKET,DIV8);
				if(status != STATUS_TIMEOUT)
				{

⌨️ 快捷键说明

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