📄 protocol.c
字号:
}
/***************************************************************************
Declaration : void get_audio_packet(void)
Description :
***************************************************************************/
void get_audio_packet(void)
{
int audio_sample;
volatile char status;
char audio_byte;
int i;
ENABLE_RF_SPI;
read_rf_byte(R_RX_PAYLOAD);
signal_in[0] = read_rf_byte(R_RX_PAYLOAD);
signal_in[1] = read_rf_byte(R_RX_PAYLOAD);
WRITE_SPI(R_RX_PAYLOAD);
WAIT_SPI_READY;
for(i=0; i< AUDIO_SAMPLES; i++)
{
// Get audio byte in nRF24L01 FIFO
READ_SPI(audio_byte);
WRITE_SPI(R_RX_PAYLOAD);
// Expand audio to 16-bit
audio_sample = expand_audio(audio_byte);
*output_write_ptr++ = (char)(audio_sample >> 8);
*output_write_ptr++ = (char)(audio_sample);
#ifndef USB
*output_write_ptr++ = 0;
*output_write_ptr++ = 0;
#endif
if(output_write_ptr >= &output[AUDIO_BUFFER_LENGTH])
output_write_ptr = &output[0];
}
if(!(protocol_flags & FLAG_BUFFER_SYNC2))
{
output_write_ptr = (char *)((unsigned int)output_read_ptr + 0xFFFC);
output_write_ptr -= 40;
if(output_write_ptr < &output[0])
output_write_ptr += AUDIO_BUFFER_LENGTH;
protocol_flags |= FLAG_BUFFER_SYNC2;
}
#ifdef HEADSET
// Handle slip due to clock differences
i = (int)output_read_ptr - (int)output_write_ptr;
if(i < 0)
i += AUDIO_BUFFER_LENGTH;
if(i < 20)
{
#ifdef USB
output_write_ptr -= 2;
#else
output_write_ptr -= 4;
#endif
if(output_write_ptr < &output[0])
output_write_ptr = &output[AUDIO_BUFFER_LENGTH-1];
}
if(i > 60)
{
#ifdef USB
output_write_ptr -= 2;
#else
output_write_ptr -= 4;
#endif
if(output_write_ptr >= &output[AUDIO_BUFFER_LENGTH])
output_write_ptr = &output[0];
}
#endif
READ_SPI_STATUS(status);
READ_SPI(audio_byte);
DISABLE_RF_SPI;
}
/***************************************************************************
Declaration : void stuff_packet(void)
Description :
***************************************************************************/
void stuff_packet(void)
{
int i;
// Zero fill audio packet
for(i=0; i< AUDIO_SAMPLES; i++)
{
*output_write_ptr++ = 0;
*output_write_ptr++ = 0;
#ifndef USB
output_write_ptr += 2;
#endif
if(output_write_ptr >= &output[AUDIO_BUFFER_LENGTH])
output_write_ptr = &output[0];
}
}
/***************************************************************************
Declaration : char compress_audio(int audio_linear16)
Description : Compresses 16-bit linear to 8-bit using binary search in a
128 word A-law compression table.
***************************************************************************/
char compress_audio(int audio_linear16)
{
unsigned int abs_linear16;
unsigned int table_val;
unsigned int index = 64;
// Get the magnitude
if(audio_linear16 < 0)
abs_linear16 = (unsigned int)(-audio_linear16);
else
abs_linear16 = (unsigned int)audio_linear16;
// Find compressed value as the index in the compression table using binary search.
table_val = pgm_read_word(&Alaw_expand[64]);
if(abs_linear16 < table_val)
index -= 32;
else
index += 32;
table_val = pgm_read_word(&Alaw_expand[index]);
if(abs_linear16 < table_val)
index -= 16;
else
index += 16;
table_val = pgm_read_word(&Alaw_expand[index]);
if(abs_linear16 < table_val)
index -= 8;
else
index += 8;
table_val = pgm_read_word(&Alaw_expand[index]);
if(abs_linear16 < table_val)
index -= 4;
else
index += 4;
table_val = pgm_read_word(&Alaw_expand[index]);
if(abs_linear16 < table_val)
index -= 2;
else
index += 2;
table_val = pgm_read_word(&Alaw_expand[index]);
if(abs_linear16 < table_val)
index -= 1;
else
index += 1;
table_val = pgm_read_word(&Alaw_expand[index]);
if(abs_linear16 < table_val)
index -= 1;
// Set correct sign
if(audio_linear16 < 0)
index |= 0x80;
return(index);
}
/***************************************************************************
Declaration : int expand_audio(char audio_compressed8)
Description : Expands 8-bit A-law to 16-bit linear using a 128 word
A-law compression table.
***************************************************************************/
int expand_audio(char audio_compressed8)
{
int linear16;
// Read linear value in 128 word compression table
linear16 = pgm_read_word(&Alaw_expand[(audio_compressed8 & 0x7F)]);
// Check and correct if negative
if(audio_compressed8 & 0x80)
return(-linear16);
else
return(linear16);
}
/***************************************************************************
Declaration : void test_rf_transmitter(char rf_channel)
Description : Test RF transmitter using CW signal
***************************************************************************/
#ifdef TEST_TX_CW
void test_rf_transmitter(char rf_channel)
{
int i;
char payload[32];
// Initialize nRF24L01 for CW Test
init_rf_test();
for(i=0; i< 32; i++)
payload[i] = 0;
write_rf_data(W_TX_PAYLOAD,&payload[0],32);
reuse_rf_payload();
// Transmit continuously at given frequency
set_rf_tx(rf_channel);
RF_ENABLE;
while(1)
;
}
#endif
/***************************************************************************
Declaration : void test_rf_modulator(char rf_channel)
Description : Test RF transmitter using modulated signal (1010)
***************************************************************************/
#ifdef TEST_TX_MOD
void test_rf_modulator(char rf_channel)
{
int i;
char payload[32];
// Initialize nRF24L01 for CW Test
init_rf_test();
for(i=0; i< 32; i++)
payload[i] = 0xAA;
write_rf_data(W_TX_PAYLOAD,&payload[0],32);
reuse_rf_payload();
// Transmit continuously at given frequency
set_rf_tx(rf_channel);
while(1)
{
clear_rf_interrupt();
rf_enable_pulse();
wait_rf_irq(TIMEOUT_AUDIO_PACKET,DIV8);
}
}
#endif
/***************************************************************************
Declaration : void test_rf_receiver(void)
Description : Test RF receiver
***************************************************************************/
#ifdef TEST_RX
void test_rf_receiver(char rf_channel)
{
// Initialize nRF24L01 for Rx Test
init_rf_test();
// Receive continuously at given frequency
set_rf_rx(rf_channel);
RF_ENABLE;
while(1)
;
}
#endif
/***************************************************************************
Declaration : void init_protocol(void)
Description : Initializes Protocol Parameters
***************************************************************************/
void init_protocol(void)
{
#ifdef DONGLE
#ifdef USB
event_times[0] = MASTER_START_TX0/32;
event_times[1] = MASTER_END_RX0/32;
event_times[2] = MASTER_START_TX1/32;
event_times[3] = MASTER_END_RX1/32;
#else
event_times[0] = MASTER_START_TX0;
event_times[1] = MASTER_END_RX0;
event_times[2] = MASTER_START_TX1;
event_times[3] = MASTER_END_RX1;
#endif
protocol_flags = 0;
call_status = 0;
call_timer = 0;
call_activity_timer = 0;
signal_in[0] = 0;
signal_in[1] = 0;
signal_out[0] = 0;
signal_out[1] = 0;
packet_loss[0] = 0;
packet_loss[1] = 0;
frame_loss = 0;
#endif
#ifdef HEADSET
event_times[0] = SLAVE_START_RX0;
event_times[1] = SLAVE_END_RX0;
event_times[2] = SLAVE_START_TX0;
event_times[3] = SLAVE_START_RX1;
event_times[4] = SLAVE_END_RX1;
event_times[5] = SLAVE_START_TX1;
master_sync_times[0] = SLAVE_RX0_IRQ + TX_RX_DELAY;
master_sync_times[1] = SLAVE_RX1_IRQ + TX_RX_DELAY;
protocol_flags = 0;
call_status = 0;
call_timer = 0;
call_activity_timer = 0;
signal_in[0] = 0;
signal_in[1] = 0;
signal_out[0] = 0;
signal_out[1] = 0;
packet_loss[0] = 0;
packet_loss[1] = 0;
frame_loss = 0;
#endif
}
/***************************************************************************
Declaration : void init_freq(void)
Description : Set frequencies to be used in Call setup and Call connection
***************************************************************************/
void init_freq(void)
{
setup_freq[0] = FREQ_SETUP0;
setup_freq[1] = FREQ_SETUP1;
setup_freq[2] = FREQ_SETUP2;
setup_freq[3] = FREQ_SETUP3;
#ifdef DONGLE
freq[0] = eeprom_read(EEPROM_ADR_FREQ0);
freq[1] = eeprom_read(EEPROM_ADR_FREQ1);
if((freq[0] <= FREQ0_MIN) || (freq[0] > FREQ0_MAX))
freq[0] = FREQ0_MIN;
if((freq[1] <= FREQ1_MIN) || (freq[1] > FREQ1_MAX))
freq[1] = FREQ1_MIN;
#else
freq[0] = FREQ0_MIN;
freq[1] = FREQ1_MIN;
#endif
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -