mirror of
https://bitbucket.org/librepilot/librepilot.git
synced 2025-01-17 02:52:12 +01:00
Updated spectrum scanning code.
git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2859 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
parent
e9d7ab09be
commit
d3ea16de99
@ -106,7 +106,6 @@ SRC += $(HOME_DIR)/aes.c
|
||||
SRC += $(HOME_DIR)/rfm22b.c
|
||||
SRC += $(HOME_DIR)/packet_handler.c
|
||||
SRC += $(HOME_DIR)/stream.c
|
||||
SRC += $(HOME_DIR)/scan_spectrum.c
|
||||
SRC += $(HOME_DIR)/ppm.c
|
||||
SRC += $(HOME_DIR)/transparent_comms.c
|
||||
#SRC += $(HOME_DIR)/api_comms.c
|
||||
|
@ -49,7 +49,8 @@ enum {
|
||||
PIPX_PACKET_TYPE_REQ_SETTINGS,
|
||||
PIPX_PACKET_TYPE_SETTINGS,
|
||||
PIPX_PACKET_TYPE_REQ_STATE,
|
||||
PIPX_PACKET_TYPE_STATE
|
||||
PIPX_PACKET_TYPE_STATE,
|
||||
PIPX_PACKET_TYPE_SPECTRUM
|
||||
};
|
||||
|
||||
typedef struct
|
||||
@ -102,19 +103,20 @@ typedef struct
|
||||
typedef struct
|
||||
{
|
||||
uint32_t start_frequency;
|
||||
float frequency_step_size;
|
||||
uint16_t frequency_step_size;
|
||||
uint16_t magnitudes;
|
||||
// int8_t magnitude[0];
|
||||
} __attribute__((__packed__)) t_pipx_config_data_spectrum;
|
||||
} __attribute__((__packed__)) t_pipx_config_spectrum;
|
||||
|
||||
// *****************************************************************************
|
||||
// local variables
|
||||
|
||||
uint32_t apiconfig_previous_com_port = 0;
|
||||
uint32_t apiconfig_previous_com_port = 0;
|
||||
|
||||
volatile uint16_t apiconfig_rx_config_timer = 0;
|
||||
volatile uint16_t apiconfig_rx_timer = 0;
|
||||
volatile uint16_t apiconfig_tx_timer = 0;
|
||||
volatile uint16_t apiconfig_ss_timer = 0;
|
||||
|
||||
uint8_t apiconfig_rx_buffer[256] __attribute__ ((aligned(4)));
|
||||
uint16_t apiconfig_rx_buffer_wr;
|
||||
@ -125,6 +127,19 @@ uint16_t apiconfig_tx_buffer_wr;
|
||||
uint8_t apiconfig_tx_config_buffer[128] __attribute__ ((aligned(4)));
|
||||
uint16_t apiconfig_tx_config_buffer_wr;
|
||||
|
||||
// for scanning the spectrum
|
||||
int8_t apiconfig_spec_buffer[64] __attribute__ ((aligned(4)));
|
||||
uint16_t apiconfig_spec_buffer_wr;
|
||||
uint32_t apiconfig_spec_start_freq;
|
||||
uint32_t apiconfig_spec_freq;
|
||||
|
||||
bool apiconfig_usb_comms;
|
||||
uint32_t apiconfig_comm_port;
|
||||
|
||||
uint16_t apiconfig_connection_index; // the RF connection we are using
|
||||
|
||||
uint8_t led_counter;
|
||||
|
||||
// *****************************************************************************
|
||||
|
||||
int apiconfig_sendDetailsPacket(void)
|
||||
@ -240,6 +255,44 @@ int apiconfig_sendSettingsPacket(void)
|
||||
return total_packet_size;
|
||||
}
|
||||
|
||||
int apiconfig_sendSpectrumPacket(uint32_t start_frequency, uint16_t freq_step_size, void *spectrum_data, uint32_t spectrum_data_size)
|
||||
{
|
||||
if (sizeof(apiconfig_tx_config_buffer) - apiconfig_tx_config_buffer_wr < sizeof(t_pipx_config_header) + sizeof(t_pipx_config_spectrum) + spectrum_data_size)
|
||||
return -1; // not enough room in the tx buffer for the packet we were going to send
|
||||
|
||||
if (!spectrum_data || spectrum_data_size <= 0)
|
||||
return -2; // nothing to send
|
||||
|
||||
t_pipx_config_header *header = (t_pipx_config_header *)(apiconfig_tx_config_buffer + apiconfig_tx_config_buffer_wr);
|
||||
t_pipx_config_spectrum *spectrum = (t_pipx_config_spectrum *)((uint8_t *)header + sizeof(t_pipx_config_header));
|
||||
uint8_t *data = (uint8_t *)spectrum + sizeof(t_pipx_config_spectrum);
|
||||
|
||||
header->marker = PIPX_HEADER_MARKER;
|
||||
header->serial_number = serial_number_crc32;
|
||||
header->type = PIPX_PACKET_TYPE_SPECTRUM;
|
||||
header->spare = 0;
|
||||
header->data_size = sizeof(t_pipx_config_spectrum) + spectrum_data_size;
|
||||
|
||||
spectrum->start_frequency = start_frequency;
|
||||
spectrum->frequency_step_size = freq_step_size;
|
||||
spectrum->magnitudes = spectrum_data_size;
|
||||
memmove(data, spectrum_data, spectrum_data_size);
|
||||
|
||||
header->data_crc = updateCRC32Data(0xffffffff, spectrum, header->data_size);
|
||||
header->header_crc = 0;
|
||||
header->header_crc = updateCRC32Data(0xffffffff, header, sizeof(t_pipx_config_header));
|
||||
|
||||
int total_packet_size = sizeof(t_pipx_config_header) + header->data_size;
|
||||
|
||||
apiconfig_tx_config_buffer_wr += total_packet_size;
|
||||
|
||||
#if defined(APICONFIG_DEBUG)
|
||||
DEBUG_PRINTF("TX api config: spectrum\r\n");
|
||||
#endif
|
||||
|
||||
return total_packet_size;
|
||||
}
|
||||
|
||||
void apiconfig_processInputPacket(void *buf, uint16_t len)
|
||||
{
|
||||
if (len <= 0)
|
||||
@ -291,7 +344,12 @@ void apiconfig_processInputPacket(void *buf, uint16_t len)
|
||||
if (header->serial_number == serial_number_crc32)
|
||||
{ // the packet is meant for us
|
||||
|
||||
t_pipx_config_settings *settings = (t_pipx_config_settings *)data;
|
||||
ph_set_remote_serial_number(0, 0); // stop the packet handler trying to communicate
|
||||
|
||||
// ******
|
||||
// save the new settings
|
||||
|
||||
t_pipx_config_settings *settings = (t_pipx_config_settings *)data;
|
||||
|
||||
saved_settings.mode = settings->mode;
|
||||
saved_settings.destination_id = settings->destination_id;
|
||||
@ -304,25 +362,47 @@ void apiconfig_processInputPacket(void *buf, uint16_t len)
|
||||
memcpy((char *)saved_settings.aes_key, (char *)settings->aes_key, sizeof(saved_settings.aes_key));
|
||||
saved_settings.rts_time = settings->rts_time;
|
||||
|
||||
saved_settings_save(); // save the new settings
|
||||
saved_settings_save(); // save them into flash
|
||||
|
||||
// ******
|
||||
|
||||
ph_set_remote_serial_number(0, 0); // stop the packet handler trying to communicate
|
||||
switch (saved_settings.mode)
|
||||
{
|
||||
case MODE_NORMAL: // normal 2-way packet mode
|
||||
case MODE_STREAM_TX: // 1-way continuous tx packet mode
|
||||
case MODE_STREAM_RX: // 1-way continuous rx packet mode
|
||||
case MODE_PPM_TX: // PPM tx mode
|
||||
case MODE_PPM_RX: // PPM rx mode
|
||||
case MODE_SCAN_SPECTRUM: // scan the receiver over the whole band
|
||||
case MODE_TX_BLANK_CARRIER_TEST: // blank carrier Tx mode (for calibrating the carrier frequency say)
|
||||
case MODE_TX_SPECTRUM_TEST: // pseudo random Tx data mode (for checking the Tx carrier spectrum)
|
||||
break;
|
||||
default: // unknown mode
|
||||
saved_settings.mode = MODE_NORMAL;
|
||||
break;
|
||||
}
|
||||
|
||||
PIOS_COM_ChangeBaud(PIOS_COM_SERIAL, saved_settings.serial_baudrate);
|
||||
|
||||
// rfm22_setTxNormal();
|
||||
|
||||
rfm22_init(rfm22_minFrequency(), rfm22_maxFrequency(), rfm22_freqHopSize());
|
||||
|
||||
rfm22_setFreqCalibration(saved_settings.rf_xtal_cap);
|
||||
ph_setNominalCarrierFrequency(saved_settings.frequency_Hz);
|
||||
ph_setDatarate(saved_settings.max_rf_bandwidth);
|
||||
ph_setTxPower(saved_settings.max_tx_power);
|
||||
|
||||
ph_set_remote_encryption(0, saved_settings.aes_enable, (const void *)saved_settings.aes_key);
|
||||
ph_set_remote_serial_number(0, saved_settings.destination_id);
|
||||
if (saved_settings.mode != MODE_SCAN_SPECTRUM)
|
||||
{
|
||||
rfm22_init_normal(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz, rfm22_freqHopSize());
|
||||
rfm22_setFreqCalibration(saved_settings.rf_xtal_cap);
|
||||
ph_setNominalCarrierFrequency(saved_settings.frequency_Hz);
|
||||
ph_setDatarate(saved_settings.max_rf_bandwidth);
|
||||
ph_setTxPower(saved_settings.max_tx_power);
|
||||
ph_set_remote_encryption(0, saved_settings.aes_enable, (const void *)saved_settings.aes_key);
|
||||
ph_set_remote_serial_number(0, saved_settings.destination_id);
|
||||
}
|
||||
else
|
||||
{
|
||||
rfm22_init_scan_spectrum(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz);
|
||||
rfm22_setFreqCalibration(saved_settings.rf_xtal_cap);
|
||||
apiconfig_spec_start_freq = saved_settings.min_frequency_Hz;
|
||||
apiconfig_spec_freq = apiconfig_spec_start_freq;
|
||||
apiconfig_spec_buffer_wr = 0;
|
||||
apiconfig_ss_timer = 0;
|
||||
}
|
||||
|
||||
switch (saved_settings.mode)
|
||||
{
|
||||
@ -344,9 +424,6 @@ void apiconfig_processInputPacket(void *buf, uint16_t len)
|
||||
case MODE_TX_SPECTRUM_TEST: // pseudo random Tx data mode (for checking the Tx carrier spectrum)
|
||||
rfm22_setTxPNMode();
|
||||
break;
|
||||
default: // unknown mode
|
||||
saved_settings.mode = MODE_NORMAL;
|
||||
break;
|
||||
}
|
||||
|
||||
// ******
|
||||
@ -449,121 +526,59 @@ uint16_t apiconfig_scanForConfigPacket(void *buf, uint16_t *len, bool rf_packet)
|
||||
}
|
||||
|
||||
// *****************************************************************************
|
||||
// can be called from an interrupt if you wish
|
||||
|
||||
void apiconfig_1ms_tick(void)
|
||||
{ // call this once every 1ms
|
||||
|
||||
if (apiconfig_rx_config_timer < 0xffff) apiconfig_rx_config_timer++;
|
||||
if (apiconfig_rx_timer < 0xffff) apiconfig_rx_timer++;
|
||||
if (apiconfig_tx_timer < 0xffff) apiconfig_tx_timer++;
|
||||
}
|
||||
|
||||
// *****************************************************************************
|
||||
// call this as often as possible - not from an interrupt
|
||||
|
||||
void apiconfig_process(void)
|
||||
{ // copy data from comm-port RX buffer to RF packet handler TX buffer, and from RF packet handler RX buffer to comm-port TX buffer
|
||||
|
||||
// ********************
|
||||
// decide which comm-port we are using (usart or usb)
|
||||
|
||||
bool usb_comms = false; // TRUE if we are using the usb port for comms.
|
||||
uint32_t comm_port = PIOS_COM_SERIAL; // default to using the usart comm-port
|
||||
|
||||
#if defined(PIOS_INCLUDE_USB_HID)
|
||||
if (PIOS_USB_HID_CheckAvailable(0))
|
||||
{ // USB comms is up, use the USB comm-port instead of the USART comm-port
|
||||
usb_comms = true;
|
||||
comm_port = PIOS_COM_TELEM_USB;
|
||||
}
|
||||
#endif
|
||||
|
||||
// ********************
|
||||
// check to see if the local communication port has changed (usart/usb)
|
||||
|
||||
if (apiconfig_previous_com_port == 0 && apiconfig_previous_com_port != comm_port)
|
||||
{ // the local communications port has changed .. remove any data in the buffers
|
||||
apiconfig_rx_buffer_wr = 0;
|
||||
apiconfig_tx_buffer_wr = 0;
|
||||
apiconfig_tx_config_buffer_wr = 0;
|
||||
}
|
||||
else
|
||||
if (usb_comms)
|
||||
{ // we're using the USB for comms - keep the USART rx buffer empty
|
||||
int32_t bytes = PIOS_COM_ReceiveBufferUsed(PIOS_COM_SERIAL);
|
||||
while (bytes > 0)
|
||||
{
|
||||
PIOS_COM_ReceiveBuffer(PIOS_COM_SERIAL);
|
||||
bytes--;
|
||||
}
|
||||
}
|
||||
|
||||
apiconfig_previous_com_port = comm_port; // remember the current comm-port we are using
|
||||
|
||||
// ********************
|
||||
|
||||
uint16_t connection_index = 0; // the RF connection we are using
|
||||
|
||||
// ********************
|
||||
// send the data received from the local comm-port to the RF packet handler TX buffer
|
||||
// send the data received from the local comm-port to the RF packet handler TX buffer
|
||||
|
||||
void apiconfig_processTx(void)
|
||||
{
|
||||
while (TRUE)
|
||||
{
|
||||
// free space size in the RF packet handler tx buffer
|
||||
uint16_t ph_num = ph_putData_free(connection_index);
|
||||
|
||||
// get the number of data bytes received down the comm-port
|
||||
int32_t com_num = PIOS_COM_ReceiveBufferUsed(comm_port);
|
||||
|
||||
// set the USART RTS handshaking line
|
||||
if (!usb_comms && ph_connected(connection_index))
|
||||
{
|
||||
if (ph_num < 32)
|
||||
SERIAL_RTS_CLEAR; // lower the USART RTS line - we don't have space in the buffer for anymore bytes
|
||||
else
|
||||
SERIAL_RTS_SET; // release the USART RTS line - we have space in the buffer for now bytes
|
||||
}
|
||||
else
|
||||
SERIAL_RTS_SET; // release the USART RTS line
|
||||
|
||||
// limit number of bytes we will get to how much space we have in our RX buffer
|
||||
if (com_num > sizeof(apiconfig_rx_buffer) - apiconfig_rx_buffer_wr)
|
||||
com_num = sizeof(apiconfig_rx_buffer) - apiconfig_rx_buffer_wr;
|
||||
|
||||
while (com_num > 0)
|
||||
{ // fetch a byte from the comm-port RX buffer and save it into our RX buffer
|
||||
apiconfig_rx_buffer[apiconfig_rx_buffer_wr++] = PIOS_COM_ReceiveBuffer(comm_port);
|
||||
com_num--;
|
||||
}
|
||||
|
||||
// scan for a configuration packet in the received data
|
||||
// scan for a configuration packet in the rx buffer
|
||||
uint16_t data_size = apiconfig_scanForConfigPacket(apiconfig_rx_buffer, &apiconfig_rx_buffer_wr, false);
|
||||
|
||||
uint16_t time_out = 5; // ms
|
||||
if (!usb_comms) time_out = (15000 * sizeof(t_pipx_config_header)) / saved_settings.serial_baudrate; // allow enough time to rx a config header
|
||||
if (!apiconfig_usb_comms) time_out = (15000 * sizeof(t_pipx_config_header)) / saved_settings.serial_baudrate; // allow enough time to rx a config header
|
||||
if (time_out < 5) time_out = 5;
|
||||
|
||||
if (data_size == 0 && apiconfig_rx_timer >= time_out)
|
||||
{ // no config packet found in the buffer within the timeout period, treat any data in the buffer as data to be sent over the air
|
||||
// no config packet found in the buffer within the timeout period, treat any data in the buffer as data to be sent over the air
|
||||
data_size = apiconfig_rx_buffer_wr;
|
||||
}
|
||||
|
||||
if (data_size == 0)
|
||||
break; // no data to send over the air
|
||||
|
||||
#if defined(APICONFIG_DEBUG)
|
||||
DEBUG_PRINTF("RX api config: data size %u\r\n", data_size);
|
||||
#endif
|
||||
if (saved_settings.mode == MODE_NORMAL || saved_settings.mode == MODE_STREAM_TX)
|
||||
{
|
||||
// free space size in the RF packet handler tx buffer
|
||||
uint16_t ph_num = ph_putData_free(apiconfig_connection_index);
|
||||
|
||||
if (ph_connected(connection_index))
|
||||
{ // we have an RF link to a remote modem
|
||||
// set the USART RTS handshaking line
|
||||
if (!apiconfig_usb_comms && ph_connected(apiconfig_connection_index))
|
||||
{
|
||||
if (ph_num < 32)
|
||||
SERIAL_RTS_CLEAR; // lower the USART RTS line - we don't have space in the buffer for anymore bytes
|
||||
else
|
||||
SERIAL_RTS_SET; // release the USART RTS line - we have space in the buffer for now bytes
|
||||
}
|
||||
else
|
||||
SERIAL_RTS_SET; // release the USART RTS line
|
||||
|
||||
if (ph_num < data_size)
|
||||
break; // not enough room in the RF packet handler TX buffer for the data
|
||||
#if defined(APICONFIG_DEBUG)
|
||||
DEBUG_PRINTF("RX api config: data size %u\r\n", data_size);
|
||||
#endif
|
||||
|
||||
// copy the data into the RF packet handler TX buffer for sending over the RF link
|
||||
data_size = ph_putData(connection_index, apiconfig_rx_buffer, data_size);
|
||||
if (ph_connected(apiconfig_connection_index))
|
||||
{ // we have an RF link to a remote modem
|
||||
|
||||
if (ph_num < data_size)
|
||||
break; // not enough room in the RF packet handler TX buffer for the data
|
||||
|
||||
// copy the data into the RF packet handler TX buffer for sending over the RF link
|
||||
data_size = ph_putData(apiconfig_connection_index, apiconfig_rx_buffer, data_size);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
SERIAL_RTS_SET; // release the USART RTS line - we have space in the buffer for now bytes
|
||||
}
|
||||
|
||||
// remove the data from our RX buffer
|
||||
@ -571,10 +586,13 @@ void apiconfig_process(void)
|
||||
if (apiconfig_rx_buffer_wr > 0)
|
||||
memmove(apiconfig_rx_buffer, apiconfig_rx_buffer + data_size, apiconfig_rx_buffer_wr);
|
||||
}
|
||||
}
|
||||
|
||||
// ********************
|
||||
// send data down the local comm port
|
||||
// *****************************************************************************
|
||||
// send data down the local comm port
|
||||
|
||||
void apiconfig_processRx(void)
|
||||
{
|
||||
if (apiconfig_tx_config_buffer_wr > 0)
|
||||
{ // send any config packets in the config buffer
|
||||
|
||||
@ -585,15 +603,11 @@ void apiconfig_process(void)
|
||||
// if (data_size > 32)
|
||||
// data_size = 32;
|
||||
|
||||
if (!usb_comms && !GPIO_IN(SERIAL_CTS_PIN))
|
||||
if (!apiconfig_usb_comms && !GPIO_IN(SERIAL_CTS_PIN))
|
||||
break; // we can't yet send data down the comm-port
|
||||
|
||||
// send the data out the comm-port
|
||||
int32_t res;
|
||||
// if (usb_comms)
|
||||
// res = PIOS_COM_SendBuffer(comm_port, apiconfig_tx_config_buffer, data_size);
|
||||
// else
|
||||
res = PIOS_COM_SendBufferNonBlocking(comm_port, apiconfig_tx_config_buffer, data_size);
|
||||
int32_t res = PIOS_COM_SendBufferNonBlocking(apiconfig_comm_port, apiconfig_tx_config_buffer, data_size);
|
||||
if (res < 0)
|
||||
{ // failed to send the data out the comm-port
|
||||
|
||||
@ -623,14 +637,17 @@ void apiconfig_process(void)
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // send the data received via the RF link out the comm-port
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (saved_settings.mode == MODE_NORMAL || saved_settings.mode == MODE_STREAM_RX)
|
||||
{
|
||||
// send the data received via the RF link out the comm-port
|
||||
while (TRUE)
|
||||
{
|
||||
// get number of data bytes received via the RF link
|
||||
uint16_t ph_num = ph_getData_used(connection_index);
|
||||
uint16_t ph_num = ph_getData_used(apiconfig_connection_index);
|
||||
|
||||
// limit to how much space we have in the temp TX buffer
|
||||
if (ph_num > sizeof(apiconfig_tx_buffer) - apiconfig_tx_buffer_wr)
|
||||
@ -638,7 +655,7 @@ void apiconfig_process(void)
|
||||
|
||||
if (ph_num > 0)
|
||||
{ // fetch the data bytes received via the RF link and save into our temp buffer
|
||||
ph_num = ph_getData(connection_index, apiconfig_tx_buffer + apiconfig_tx_buffer_wr, ph_num);
|
||||
ph_num = ph_getData(apiconfig_connection_index, apiconfig_tx_buffer + apiconfig_tx_buffer_wr, ph_num);
|
||||
apiconfig_tx_buffer_wr += ph_num;
|
||||
}
|
||||
|
||||
@ -646,30 +663,22 @@ void apiconfig_process(void)
|
||||
if (data_size == 0)
|
||||
break; // no data to send
|
||||
|
||||
// uint16_t data_size = apiconfig_scanForConfigPacket(apiconfig_tx_buffer, &apiconfig_tx_buffer_wr, true);
|
||||
// if (data_size == 0)
|
||||
// break; // no data to send
|
||||
|
||||
// we have data to send down the comm-port
|
||||
/*
|
||||
#if (defined(PIOS_COM_DEBUG) && (PIOS_COM_DEBUG == PIOS_COM_SERIAL))
|
||||
if (!usb_comms)
|
||||
{ // the serial-port is being used for debugging - don't send data down it
|
||||
apiconfig_tx_buffer_wr = 0;
|
||||
apiconfig_tx_timer = 0;
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
if (!apiconfig_usb_comms)
|
||||
{ // the serial-port is being used for debugging - don't send data down it
|
||||
apiconfig_tx_buffer_wr = 0;
|
||||
apiconfig_tx_timer = 0;
|
||||
continue;
|
||||
}
|
||||
#endif
|
||||
*/
|
||||
if (!usb_comms && !GPIO_IN(SERIAL_CTS_PIN))
|
||||
if (!apiconfig_usb_comms && !GPIO_IN(SERIAL_CTS_PIN))
|
||||
break; // we can't yet send data down the comm-port
|
||||
|
||||
// send the data out the comm-port
|
||||
int32_t res;
|
||||
// if (usb_comms)
|
||||
// res = PIOS_COM_SendBuffer(comm_port, apiconfig_tx_buffer, data_size);
|
||||
// else
|
||||
res = PIOS_COM_SendBufferNonBlocking(comm_port, apiconfig_tx_buffer, data_size);
|
||||
int32_t res = PIOS_COM_SendBufferNonBlocking(apiconfig_comm_port, apiconfig_tx_buffer, data_size);
|
||||
if (res < 0)
|
||||
{ // failed to send the data out the comm-port
|
||||
|
||||
@ -694,13 +703,128 @@ void apiconfig_process(void)
|
||||
apiconfig_tx_timer = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// *************************************************************
|
||||
|
||||
void apiconfig_process_scan_spectrum(void)
|
||||
{
|
||||
if (saved_settings.mode != MODE_SCAN_SPECTRUM)
|
||||
return;
|
||||
|
||||
if (apiconfig_ss_timer < 1) return; // 1ms
|
||||
apiconfig_ss_timer = 0;
|
||||
|
||||
if (++led_counter >= 30)
|
||||
{
|
||||
led_counter = 0;
|
||||
// RX_LED_TOGGLE;
|
||||
LINK_LED_TOGGLE;
|
||||
}
|
||||
|
||||
// uint16_t freq_step_size = (uint16_t)(rfm22_getFrequencyStepSize() * 4 + 0.5f);
|
||||
uint16_t freq_step_size = (uint16_t)(rfm22_getFrequencyStepSize() * 20 + 0.5f);
|
||||
|
||||
// fetch the current rssi level
|
||||
int16_t rssi_dbm = rfm22_getRSSI();
|
||||
if (rssi_dbm < -128) rssi_dbm = -128;
|
||||
else
|
||||
if (rssi_dbm > 0) rssi_dbm = 0;
|
||||
|
||||
// onto next frequency
|
||||
apiconfig_spec_freq += freq_step_size;
|
||||
if (apiconfig_spec_freq >= rfm22_maxFrequency()) apiconfig_spec_freq = rfm22_minFrequency();
|
||||
rfm22_setNominalCarrierFrequency(apiconfig_spec_freq);
|
||||
// rfm22_setRxMode(RX_SCAN_SPECTRUM, true);
|
||||
|
||||
// add the RSSI value to the buffer
|
||||
apiconfig_spec_buffer[apiconfig_spec_buffer_wr] = rssi_dbm;
|
||||
if (++apiconfig_spec_buffer_wr >= sizeof(apiconfig_spec_buffer))
|
||||
{ // send the buffer
|
||||
apiconfig_sendSpectrumPacket(apiconfig_spec_start_freq, freq_step_size, apiconfig_spec_buffer, apiconfig_spec_buffer_wr);
|
||||
|
||||
apiconfig_spec_buffer_wr = 0;
|
||||
apiconfig_spec_start_freq = apiconfig_spec_freq;
|
||||
}
|
||||
}
|
||||
|
||||
// *****************************************************************************
|
||||
// call this as often as possible - not from an interrupt
|
||||
|
||||
void apiconfig_process(void)
|
||||
{ // copy data from comm-port RX buffer to RF packet handler TX buffer, and from RF packet handler RX buffer to comm-port TX buffer
|
||||
|
||||
apiconfig_process_scan_spectrum();
|
||||
|
||||
// ********************
|
||||
|
||||
// decide which comm-port we are using (usart or usb)
|
||||
apiconfig_usb_comms = false; // TRUE if we are using the usb port for comms.
|
||||
apiconfig_comm_port = PIOS_COM_SERIAL; // default to using the usart comm-port
|
||||
#if defined(PIOS_INCLUDE_USB_HID)
|
||||
if (PIOS_USB_HID_CheckAvailable(0))
|
||||
{ // USB comms is up, use the USB comm-port instead of the USART comm-port
|
||||
apiconfig_usb_comms = true;
|
||||
apiconfig_comm_port = PIOS_COM_TELEM_USB;
|
||||
}
|
||||
#endif
|
||||
|
||||
// check to see if the local communication port has changed (usart/usb)
|
||||
if (apiconfig_previous_com_port == 0 && apiconfig_previous_com_port != apiconfig_comm_port)
|
||||
{ // the local communications port has changed .. remove any data in the buffers
|
||||
apiconfig_rx_buffer_wr = 0;
|
||||
apiconfig_tx_buffer_wr = 0;
|
||||
apiconfig_tx_config_buffer_wr = 0;
|
||||
}
|
||||
else
|
||||
if (apiconfig_usb_comms)
|
||||
{ // we're using the USB for comms - keep the USART rx buffer empty
|
||||
int32_t bytes = PIOS_COM_ReceiveBufferUsed(PIOS_COM_SERIAL);
|
||||
while (bytes > 0)
|
||||
{
|
||||
PIOS_COM_ReceiveBuffer(PIOS_COM_SERIAL);
|
||||
bytes--;
|
||||
}
|
||||
}
|
||||
apiconfig_previous_com_port = apiconfig_comm_port; // remember the current comm-port we are using
|
||||
|
||||
apiconfig_connection_index = 0; // the RF connection we are using
|
||||
|
||||
// *******************
|
||||
// fetch the data received via the comm-port
|
||||
|
||||
// get the number of data bytes received down the comm-port
|
||||
int32_t com_num = PIOS_COM_ReceiveBufferUsed(apiconfig_comm_port);
|
||||
|
||||
// limit number of bytes we will get to how much space we have in our RX buffer
|
||||
if (com_num > sizeof(apiconfig_rx_buffer) - apiconfig_rx_buffer_wr)
|
||||
com_num = sizeof(apiconfig_rx_buffer) - apiconfig_rx_buffer_wr;
|
||||
|
||||
while (com_num > 0)
|
||||
{ // fetch a byte from the comm-port RX buffer and save it into our RX buffer
|
||||
apiconfig_rx_buffer[apiconfig_rx_buffer_wr++] = PIOS_COM_ReceiveBuffer(apiconfig_comm_port);
|
||||
com_num--;
|
||||
}
|
||||
|
||||
apiconfig_processTx();
|
||||
apiconfig_processRx();
|
||||
|
||||
// speed the pinging speed up if the GCS is connected and monitoring the signal level etc
|
||||
ph_setFastPing(apiconfig_rx_config_timer < 2000);
|
||||
}
|
||||
|
||||
// *****************************************************************************
|
||||
// can be called from an interrupt if you wish
|
||||
|
||||
void apiconfig_1ms_tick(void)
|
||||
{ // call this once every 1ms
|
||||
|
||||
if (apiconfig_rx_config_timer < 0xffff) apiconfig_rx_config_timer++;
|
||||
if (apiconfig_rx_timer < 0xffff) apiconfig_rx_timer++;
|
||||
if (apiconfig_tx_timer < 0xffff) apiconfig_tx_timer++;
|
||||
if (apiconfig_ss_timer < 0xffff) apiconfig_ss_timer++;
|
||||
}
|
||||
|
||||
// *****************************************************************************
|
||||
|
||||
void apiconfig_init(void)
|
||||
@ -716,6 +840,12 @@ void apiconfig_init(void)
|
||||
apiconfig_rx_config_timer = 0xffff;
|
||||
apiconfig_rx_timer = 0;
|
||||
apiconfig_tx_timer = 0;
|
||||
apiconfig_ss_timer = 0;
|
||||
|
||||
apiconfig_spec_buffer_wr = 0;
|
||||
apiconfig_spec_start_freq = 0;
|
||||
|
||||
led_counter = 0;
|
||||
}
|
||||
|
||||
// *****************************************************************************
|
||||
|
@ -33,7 +33,7 @@
|
||||
|
||||
// firmware version
|
||||
#define VERSION_MAJOR 0 // 0 to 255
|
||||
#define VERSION_MINOR 5 // 0 to 255
|
||||
#define VERSION_MINOR 6 // 0 to 255
|
||||
|
||||
// macro's for reading internal flash memory
|
||||
#define mem8(addr) (*((volatile uint8_t *)(addr)))
|
||||
|
@ -41,7 +41,8 @@
|
||||
|
||||
// ************************************
|
||||
|
||||
enum { RX_WAIT_PREAMBLE_MODE = 0,
|
||||
enum { RX_SCAN_SPECTRUM = 0,
|
||||
RX_WAIT_PREAMBLE_MODE,
|
||||
RX_WAIT_SYNC_MODE,
|
||||
RX_DATA_MODE,
|
||||
TX_DATA_MODE,
|
||||
@ -391,17 +392,18 @@ enum { RX_WAIT_PREAMBLE_MODE = 0,
|
||||
#define RFM22_header_cntl1_hdch_01 0x03 // Received Header check for bytes 0 & 1.
|
||||
|
||||
#define RFM22_header_control2 0x33 // R/W
|
||||
#define RFM22_header_cntl2_prealen 0x01 // MSB of Preamble Length. See register Preamble Length.
|
||||
#define RFM22_header_cntl2_synclen_3 0x00 // Synchronization Word 3
|
||||
#define RFM22_header_cntl2_synclen_32 0x02 // Synchronization Word 3 followed by 2
|
||||
#define RFM22_header_cntl2_synclen_321 0x04 // Synchronization Word 3 followed by 2 followed by 1
|
||||
#define RFM22_header_cntl2_synclen_3210 0x06 // Synchronization Word 3 followed by 2 followed by 1 followed by 0
|
||||
#define RFM22_header_cntl2_fixpklen 0x08 // Fix Packet Length. When fixpklen = 1 the packet length (pklen[7:0]) is not included in the header. When fixpklen = 0 the packet length is included in the header.
|
||||
#define RFM22_header_cntl2_hdlen_none 0x00 // no header
|
||||
#define RFM22_header_cntl2_hdlen_3 0x10 // header 3
|
||||
#define RFM22_header_cntl2_hdlen_32 0x20 // header 3 and 2
|
||||
#define RFM22_header_cntl2_hdlen_321 0x30 // header 3 and 2 and 1
|
||||
#define RFM22_header_cntl2_hdlen_3210 0x40 // header 3 and 2 and 1 and 0
|
||||
#define RFM22_header_cntl2_fixpklen 0x08 // Fix Packet Length. When fixpklen = 1 the packet length (pklen[7:0]) is not included in the header. When fixpklen = 0 the packet length is included in the header.
|
||||
#define RFM22_header_cntl2_synclen_3 0x00 // Synchronization Word 3
|
||||
#define RFM22_header_cntl2_synclen_32 0x02 // Synchronization Word 3 followed by 2
|
||||
#define RFM22_header_cntl2_synclen_321 0x04 // Synchronization Word 3 followed by 2 followed by 1
|
||||
#define RFM22_header_cntl2_synclen_3210 0x06 // Synchronization Word 3 followed by 2 followed by 1 followed by 0
|
||||
#define RFM22_header_cntl2_prealen 0x01 // MSB of Preamble Length. See register Preamble Length.
|
||||
#define RFM22_header_cntl2_skipsyn 0x80 // If high, the system will ignore the syncword search timeout reset. The chip will not return to searching for Preamble, but instead will remain searching for Sync word.
|
||||
|
||||
#define RFM22_preamble_length 0x34 // R/W
|
||||
|
||||
@ -588,6 +590,10 @@ uint32_t rfm22_freqHopSize(void);
|
||||
void rfm22_setDatarate(uint32_t datarate_bps);
|
||||
uint32_t rfm22_getDatarate(void);
|
||||
|
||||
void rfm22_setRxMode(uint8_t mode, bool multi_packet_mode);
|
||||
|
||||
int16_t rfm22_getRSSI(void);
|
||||
|
||||
int16_t rfm22_receivedRSSI(void);
|
||||
int32_t rfm22_receivedAFCHz(void);
|
||||
uint16_t rfm22_receivedLength(void);
|
||||
@ -616,7 +622,8 @@ bool rfm22_txReady(void);
|
||||
void rfm22_1ms_tick(void);
|
||||
void rfm22_process(void);
|
||||
|
||||
int rfm22_init(uint32_t min_frequency_hz, uint32_t max_frequency_hz, uint32_t freq_hop_step_size);
|
||||
int rfm22_init_scan_spectrum(uint32_t min_frequency_hz, uint32_t max_frequency_hz);
|
||||
int rfm22_init_normal(uint32_t min_frequency_hz, uint32_t max_frequency_hz, uint32_t freq_hop_step_size);
|
||||
|
||||
// ************************************
|
||||
|
||||
|
@ -1,39 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file scan_spectrum.h
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
||||
* @brief Puts module into Rx mode and scan across the RF spectrum monitoring the RSSI
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
#ifndef SCAN_SPECTRUM_H_
|
||||
#define SCAN_SPECTRUM_H_
|
||||
|
||||
#include "stdint.h"
|
||||
|
||||
// *************************************************************
|
||||
|
||||
void ss_1ms_tick(void);
|
||||
void ss_process(void);
|
||||
void ss_init(void);
|
||||
|
||||
// *************************************************************
|
||||
|
||||
#endif
|
@ -38,7 +38,6 @@
|
||||
#include "rfm22b.h"
|
||||
#include "packet_handler.h"
|
||||
#include "stream.h"
|
||||
#include "scan_spectrum.h"
|
||||
#include "ppm.h"
|
||||
#include "transparent_comms.h"
|
||||
//#include "api_comms.h"
|
||||
@ -206,9 +205,6 @@ void TIMER_INT_FUNC(void)
|
||||
if (mode == MODE_STREAM_TX || mode == MODE_STREAM_RX)
|
||||
stream_1ms_tick(); // continuous data stream tick
|
||||
|
||||
if (mode == MODE_SCAN_SPECTRUM)
|
||||
ss_1ms_tick(); // spectrum scan tick
|
||||
|
||||
if (mode == MODE_PPM_TX || mode == MODE_PPM_RX)
|
||||
ppm_1ms_tick(); // ppm tick
|
||||
|
||||
@ -322,7 +318,7 @@ void init_RF_module(void)
|
||||
case FREQBAND_434MHz:
|
||||
case FREQBAND_868MHz:
|
||||
case FREQBAND_915MHz:
|
||||
i = rfm22_init(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz, 50000);
|
||||
i = rfm22_init_normal(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz, 50000);
|
||||
break;
|
||||
|
||||
default:
|
||||
@ -720,8 +716,6 @@ int main()
|
||||
|
||||
ppm_init(); // initialise the ppm module
|
||||
|
||||
ss_init(); // initialise the spectrum scanning module
|
||||
|
||||
saved_settings_save();
|
||||
|
||||
booting = FALSE;
|
||||
@ -811,9 +805,6 @@ int main()
|
||||
if (mode == MODE_STREAM_TX || mode == MODE_STREAM_RX)
|
||||
stream_process(); // continuous data stream processing
|
||||
|
||||
if (mode == MODE_SCAN_SPECTRUM)
|
||||
ss_process(); // spectrum scan processing
|
||||
|
||||
if (mode == MODE_PPM_TX || mode == MODE_PPM_RX)
|
||||
ppm_process(); // ppm processing
|
||||
|
||||
|
@ -119,45 +119,77 @@
|
||||
//#define RFM22_DEFAULT_RF_DATARATE 256000 // 256k bits per sec .. NOT YET WORKING
|
||||
|
||||
// ************************************
|
||||
|
||||
#define RFM22_DEFAULT_SS_RF_DATARATE 125 // 128bps
|
||||
|
||||
// ************************************
|
||||
// Normal data streaming
|
||||
// GFSK modulation
|
||||
// no manchester encoding
|
||||
// data whitening
|
||||
// FIFO mode
|
||||
// 5-nibble rx preamble length detection
|
||||
// 10-nibble tx preamble length
|
||||
// AFC enabled
|
||||
|
||||
#define LOOKUP_SIZE 14
|
||||
|
||||
/*
|
||||
// xtal 20 ppm
|
||||
*/
|
||||
|
||||
// xtal 10 ppm, 434MHz
|
||||
uint32_t data_rate[LOOKUP_SIZE] = { 500, 1000, 2000, 4000, 8000, 9600, 16000, 19200, 24000, 32000, 64000, 128000, 192000, 256000};
|
||||
uint8_t modulation_index[LOOKUP_SIZE] = { 16, 8, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
|
||||
uint32_t freq_deviation[LOOKUP_SIZE] = { 4000, 4000, 4000, 4000, 4000, 4800, 8000, 9600, 12000, 16000, 32000, 64000, 96000, 128000};
|
||||
uint32_t rx_bandwidth[LOOKUP_SIZE] = { 17500, 17500, 17500, 17500, 17500, 19400, 32200, 38600, 51200, 64100, 137900, 269300, 420200, 518800};
|
||||
int8_t est_rx_sens_dBm[LOOKUP_SIZE] = { -118, -118, -117, -116, -115, -115, -112, -112, -110, -109, -106, -103, -101, -100}; // estimated receiver sensitivity for BER = 1E-3
|
||||
const uint32_t data_rate[LOOKUP_SIZE] = { 500, 1000, 2000, 4000, 8000, 9600, 16000, 19200, 24000, 32000, 64000, 128000, 192000, 256000};
|
||||
const uint8_t modulation_index[LOOKUP_SIZE] = { 16, 8, 4, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
|
||||
const uint32_t freq_deviation[LOOKUP_SIZE] = { 4000, 4000, 4000, 4000, 4000, 4800, 8000, 9600, 12000, 16000, 32000, 64000, 96000, 128000};
|
||||
const uint32_t rx_bandwidth[LOOKUP_SIZE] = { 17500, 17500, 17500, 17500, 17500, 19400, 32200, 38600, 51200, 64100, 137900, 269300, 420200, 518800};
|
||||
const int8_t est_rx_sens_dBm[LOOKUP_SIZE] = { -118, -118, -117, -116, -115, -115, -112, -112, -110, -109, -106, -103, -101, -100}; // estimated receiver sensitivity for BER = 1E-3
|
||||
|
||||
uint8_t reg_1C[LOOKUP_SIZE] = { 0x3A, 0x3A, 0x3A, 0x3A, 0x3A, 0x3B, 0x26, 0x2B, 0x2E, 0x16, 0x07, 0x83, 0x8A, 0x8C}; // rfm22_if_filter_bandwidth
|
||||
uint8_t reg_1D[LOOKUP_SIZE] = { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40}; // rfm22_afc_loop_gearshift_override
|
||||
const uint8_t reg_1C[LOOKUP_SIZE] = { 0x3A, 0x3A, 0x3A, 0x3A, 0x3A, 0x3B, 0x26, 0x2B, 0x2E, 0x16, 0x07, 0x83, 0x8A, 0x8C}; // rfm22_if_filter_bandwidth
|
||||
const uint8_t reg_1D[LOOKUP_SIZE] = { 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40, 0x40}; // rfm22_afc_loop_gearshift_override
|
||||
|
||||
uint8_t reg_20[LOOKUP_SIZE] = { 0xE8, 0xF4, 0xFA, 0x7D, 0x3F, 0x34, 0x3F, 0x34, 0x2A, 0x3F, 0x3F, 0x5E, 0x3F, 0x2F}; // rfm22_clk_recovery_oversampling_ratio
|
||||
uint8_t reg_21[LOOKUP_SIZE] = { 0x60, 0x20, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x01, 0x02, 0x02}; // rfm22_clk_recovery_offset2
|
||||
uint8_t reg_22[LOOKUP_SIZE] = { 0x20, 0x41, 0x83, 0x06, 0x0C, 0x75, 0x0C, 0x75, 0x12, 0x0C, 0x0C, 0x5D, 0x0C, 0xBB}; // rfm22_clk_recovery_offset1
|
||||
uint8_t reg_23[LOOKUP_SIZE] = { 0xC5, 0x89, 0x12, 0x25, 0x4A, 0x25, 0x4A, 0x25, 0x6F, 0x4A, 0x4A, 0x86, 0x4A, 0x0D}; // rfm22_clk_recovery_offset0
|
||||
uint8_t reg_24[LOOKUP_SIZE] = { 0x00, 0x00, 0x00, 0x02, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x02, 0x04, 0x05}; // rfm22_clk_recovery_timing_loop_gain1
|
||||
uint8_t reg_25[LOOKUP_SIZE] = { 0x0A, 0x23, 0x85, 0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBB, 0x12, 0x74}; // rfm22_clk_recovery_timing_loop_gain0
|
||||
const uint8_t reg_20[LOOKUP_SIZE] = { 0xE8, 0xF4, 0xFA, 0x7D, 0x3F, 0x34, 0x3F, 0x34, 0x2A, 0x3F, 0x3F, 0x5E, 0x3F, 0x2F}; // rfm22_clk_recovery_oversampling_ratio
|
||||
const uint8_t reg_21[LOOKUP_SIZE] = { 0x60, 0x20, 0x00, 0x01, 0x02, 0x02, 0x02, 0x02, 0x03, 0x02, 0x02, 0x01, 0x02, 0x02}; // rfm22_clk_recovery_offset2
|
||||
const uint8_t reg_22[LOOKUP_SIZE] = { 0x20, 0x41, 0x83, 0x06, 0x0C, 0x75, 0x0C, 0x75, 0x12, 0x0C, 0x0C, 0x5D, 0x0C, 0xBB}; // rfm22_clk_recovery_offset1
|
||||
const uint8_t reg_23[LOOKUP_SIZE] = { 0xC5, 0x89, 0x12, 0x25, 0x4A, 0x25, 0x4A, 0x25, 0x6F, 0x4A, 0x4A, 0x86, 0x4A, 0x0D}; // rfm22_clk_recovery_offset0
|
||||
const uint8_t reg_24[LOOKUP_SIZE] = { 0x00, 0x00, 0x00, 0x02, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x07, 0x02, 0x04, 0x05}; // rfm22_clk_recovery_timing_loop_gain1
|
||||
const uint8_t reg_25[LOOKUP_SIZE] = { 0x0A, 0x23, 0x85, 0x0E, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xBB, 0x12, 0x74}; // rfm22_clk_recovery_timing_loop_gain0
|
||||
|
||||
uint8_t reg_2A[LOOKUP_SIZE] = { 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0D, 0x0D, 0x0E, 0x12, 0x17, 0x31, 0x50, 0x50, 0x50}; // rfm22_afc_limiter .. AFC_pull_in_range = ±AFCLimiter[7:0] x (hbsel+1) x 625 Hz
|
||||
const uint8_t reg_2A[LOOKUP_SIZE] = { 0x0E, 0x0E, 0x0E, 0x0E, 0x0E, 0x0D, 0x0D, 0x0E, 0x12, 0x17, 0x31, 0x50, 0x50, 0x50}; // rfm22_afc_limiter .. AFC_pull_in_range = ±AFCLimiter[7:0] x (hbsel+1) x 625 Hz
|
||||
|
||||
uint8_t reg_6E[LOOKUP_SIZE] = { 0x04, 0x08, 0x10, 0x20, 0x41, 0x4E, 0x83, 0x9D, 0xC4, 0x08, 0x10, 0x20, 0x31, 0x41}; // rfm22_tx_data_rate1
|
||||
uint8_t reg_6F[LOOKUP_SIZE] = { 0x19, 0x31, 0x62, 0xC5, 0x89, 0xA5, 0x12, 0x49, 0x9C, 0x31, 0x62, 0xC5, 0x27, 0x89}; // rfm22_tx_data_rate0
|
||||
const uint8_t reg_6E[LOOKUP_SIZE] = { 0x04, 0x08, 0x10, 0x20, 0x41, 0x4E, 0x83, 0x9D, 0xC4, 0x08, 0x10, 0x20, 0x31, 0x41}; // rfm22_tx_data_rate1
|
||||
const uint8_t reg_6F[LOOKUP_SIZE] = { 0x19, 0x31, 0x62, 0xC5, 0x89, 0xA5, 0x12, 0x49, 0x9C, 0x31, 0x62, 0xC5, 0x27, 0x89}; // rfm22_tx_data_rate0
|
||||
|
||||
uint8_t reg_70[LOOKUP_SIZE] = { 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D}; // rfm22_modulation_mode_control1
|
||||
uint8_t reg_71[LOOKUP_SIZE] = { 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23}; // rfm22_modulation_mode_control2
|
||||
const uint8_t reg_70[LOOKUP_SIZE] = { 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x2D, 0x0D, 0x0D, 0x0D, 0x0D, 0x0D}; // rfm22_modulation_mode_control1
|
||||
const uint8_t reg_71[LOOKUP_SIZE] = { 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23, 0x23}; // rfm22_modulation_mode_control2
|
||||
|
||||
uint8_t reg_72[LOOKUP_SIZE] = { 0x06, 0x06, 0x06, 0x06, 0x06, 0x08, 0x0D, 0x0F, 0x13, 0x1A, 0x33, 0x66, 0x9A, 0xCD}; // rfm22_frequency_deviation
|
||||
const uint8_t reg_72[LOOKUP_SIZE] = { 0x06, 0x06, 0x06, 0x06, 0x06, 0x08, 0x0D, 0x0F, 0x13, 0x1A, 0x33, 0x66, 0x9A, 0xCD}; // rfm22_frequency_deviation
|
||||
|
||||
// ************************************
|
||||
// Scan Spectrum settings
|
||||
// GFSK modulation
|
||||
// no manchester encoding
|
||||
// data whitening
|
||||
// FIFO mode
|
||||
// 5-nibble rx preamble length detection
|
||||
// 10-nibble tx preamble length
|
||||
// AFC disabled
|
||||
|
||||
#define SS_LOOKUP_SIZE 2
|
||||
|
||||
// xtal 1 ppm, 434MHz
|
||||
const uint32_t ss_rx_bandwidth[SS_LOOKUP_SIZE] = { 2600, 10600};
|
||||
|
||||
const uint8_t ss_reg_1C[SS_LOOKUP_SIZE] = { 0x51, 0x32}; // rfm22_if_filter_bandwidth
|
||||
const uint8_t ss_reg_1D[SS_LOOKUP_SIZE] = { 0x00, 0x00}; // rfm22_afc_loop_gearshift_override
|
||||
|
||||
const uint8_t ss_reg_20[SS_LOOKUP_SIZE] = { 0xE8, 0x38}; // rfm22_clk_recovery_oversampling_ratio
|
||||
const uint8_t ss_reg_21[SS_LOOKUP_SIZE] = { 0x60, 0x02}; // rfm22_clk_recovery_offset2
|
||||
const uint8_t ss_reg_22[SS_LOOKUP_SIZE] = { 0x20, 0x4D}; // rfm22_clk_recovery_offset1
|
||||
const uint8_t ss_reg_23[SS_LOOKUP_SIZE] = { 0xC5, 0xD3}; // rfm22_clk_recovery_offset0
|
||||
const uint8_t ss_reg_24[SS_LOOKUP_SIZE] = { 0x00, 0x07}; // rfm22_clk_recovery_timing_loop_gain1
|
||||
const uint8_t ss_reg_25[SS_LOOKUP_SIZE] = { 0x0F, 0xFF}; // rfm22_clk_recovery_timing_loop_gain0
|
||||
|
||||
const uint8_t ss_reg_2A[SS_LOOKUP_SIZE] = { 0xFF, 0xFF}; // rfm22_afc_limiter .. AFC_pull_in_range = ±AFCLimiter[7:0] x (hbsel+1) x 625 Hz
|
||||
|
||||
const uint8_t ss_reg_70[SS_LOOKUP_SIZE] = { 0x24, 0x2D}; // rfm22_modulation_mode_control1
|
||||
const uint8_t ss_reg_71[SS_LOOKUP_SIZE] = { 0x2B, 0x23}; // rfm22_modulation_mode_control2
|
||||
|
||||
// ************************************
|
||||
|
||||
@ -180,6 +212,7 @@ uint32_t carrier_frequency_hz; // the current RF frequency we are on
|
||||
uint32_t carrier_datarate_bps; // the RF data rate we are using
|
||||
|
||||
uint32_t rf_bandwidth_used; // the RF bandwidth currently used
|
||||
uint32_t ss_rf_bandwidth_used; // the RF bandwidth currently used
|
||||
|
||||
uint8_t hbsel; // holds the hbsel (1 or 2)
|
||||
float frequency_step_size; // holds the minimum frequency step size
|
||||
@ -233,6 +266,7 @@ volatile uint16_t tx_data_rd; // the tx data read index
|
||||
volatile uint16_t tx_data_wr; // the tx data write index
|
||||
|
||||
int lookup_index;
|
||||
int ss_lookup_index;
|
||||
|
||||
volatile bool power_on_reset; // set if the RF module has reset itself
|
||||
|
||||
@ -462,7 +496,7 @@ void rfm22_setNominalCarrierFrequency(uint32_t frequency_hz)
|
||||
|
||||
frequency_step_size = 156.25f * hbsel;
|
||||
|
||||
rfm22_write(RFM22_frequency_hopping_channel_select, frequency_hop_channel); // frequency hoppping channel (0-255)
|
||||
rfm22_write(RFM22_frequency_hopping_channel_select, frequency_hop_channel); // frequency hopping channel (0-255)
|
||||
|
||||
rfm22_write(RFM22_frequency_offset1, 0); // no frequency offset
|
||||
rfm22_write(RFM22_frequency_offset2, 0); // no frequency offset
|
||||
@ -481,7 +515,6 @@ void rfm22_setNominalCarrierFrequency(uint32_t frequency_hz)
|
||||
#if defined(RFM22_EXT_INT_USE)
|
||||
exec_using_spi = FALSE;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
uint32_t rfm22_getNominalCarrierFrequency(void)
|
||||
@ -638,7 +671,6 @@ void rfm22_setDatarate(uint32_t datarate_bps)
|
||||
#if defined(RFM22_EXT_INT_USE)
|
||||
exec_using_spi = FALSE;
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
uint32_t rfm22_getDatarate(void)
|
||||
@ -648,40 +680,56 @@ uint32_t rfm22_getDatarate(void)
|
||||
|
||||
// ************************************
|
||||
|
||||
void rfm22_reenableRx(void)
|
||||
void rfm22_setSSBandwidth(uint32_t bandwidth_index)
|
||||
{
|
||||
|
||||
#if defined(RFM22_EXT_INT_USE)
|
||||
exec_using_spi = TRUE;
|
||||
#endif
|
||||
// *******
|
||||
|
||||
RX_LED_OFF;
|
||||
ss_lookup_index = bandwidth_index;
|
||||
|
||||
// disable the receiver
|
||||
// rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_xton); // READY mode
|
||||
rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_pllon); // TUNE mode
|
||||
ss_rf_bandwidth_used = ss_rx_bandwidth[lookup_index];
|
||||
|
||||
// clear FIFOs
|
||||
rfm22_write(RFM22_op_and_func_ctrl2, RFM22_opfc2_ffclrrx | RFM22_opfc2_ffclrtx);
|
||||
rfm22_write(RFM22_op_and_func_ctrl2, 0x00);
|
||||
// ********************************
|
||||
|
||||
rx_buffer_wr = 0; // empty the rx buffer
|
||||
rfm22_write(0x1C, ss_reg_1C[ss_lookup_index]); // rfm22_if_filter_bandwidth
|
||||
rfm22_write(0x1D, ss_reg_1D[ss_lookup_index]); // rfm22_afc_loop_gearshift_override
|
||||
|
||||
rfm22_int_timer = 0; // reset the timer
|
||||
rfm22_write(0x20, ss_reg_20[ss_lookup_index]); // rfm22_clk_recovery_oversampling_ratio
|
||||
rfm22_write(0x21, ss_reg_21[ss_lookup_index]); // rfm22_clk_recovery_offset2
|
||||
rfm22_write(0x22, ss_reg_22[ss_lookup_index]); // rfm22_clk_recovery_offset1
|
||||
rfm22_write(0x23, ss_reg_23[ss_lookup_index]); // rfm22_clk_recovery_offset0
|
||||
rfm22_write(0x24, ss_reg_24[ss_lookup_index]); // rfm22_clk_recovery_timing_loop_gain1
|
||||
rfm22_write(0x25, ss_reg_25[ss_lookup_index]); // rfm22_clk_recovery_timing_loop_gain0
|
||||
|
||||
STOPWATCH_reset(); // reset clear channel detect timer
|
||||
rfm22_write(0x2A, ss_reg_2A[ss_lookup_index]); // rfm22_afc_limiter
|
||||
|
||||
rf_mode = RX_WAIT_PREAMBLE_MODE;
|
||||
rfm22_write(0x58, 0x80); // rfm22_chargepump_current_trimming_override
|
||||
|
||||
// enable the receiver
|
||||
// rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_xton | RFM22_opfc1_rxon);
|
||||
rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_pllon | RFM22_opfc1_rxon);
|
||||
rfm22_write(0x70, ss_reg_70[ss_lookup_index]); // rfm22_modulation_mode_control1
|
||||
rfm22_write(0x71, ss_reg_71[ss_lookup_index]); // rfm22_modulation_mode_control2
|
||||
|
||||
rfm22_write(RFM22_ook_counter_value1, 0x00);
|
||||
rfm22_write(RFM22_ook_counter_value2, 0x00);
|
||||
|
||||
// ********************************
|
||||
|
||||
#if defined(RFM22_DEBUG)
|
||||
DEBUG_PRINTF("ss_rf_rx_bandwidth[%u]: %u\r\n", ss_lookup_index, ss_rx_bandwidth[ss_lookup_index]);
|
||||
#endif
|
||||
|
||||
// *******
|
||||
|
||||
#if defined(RFM22_EXT_INT_USE)
|
||||
exec_using_spi = FALSE;
|
||||
#endif
|
||||
}
|
||||
|
||||
void rfm22_setRxMode(void)
|
||||
// ************************************
|
||||
|
||||
void rfm22_setRxMode(uint8_t mode, bool multi_packet_mode)
|
||||
{
|
||||
#if defined(RFM22_EXT_INT_USE)
|
||||
exec_using_spi = TRUE;
|
||||
@ -710,21 +758,32 @@ void rfm22_setRxMode(void)
|
||||
|
||||
rfm22_int_timer = 0; // reset the timer
|
||||
|
||||
STOPWATCH_reset(); // reset clear channel detect timer
|
||||
rf_mode = mode;
|
||||
|
||||
rf_mode = RX_WAIT_PREAMBLE_MODE;
|
||||
if (mode != RX_SCAN_SPECTRUM)
|
||||
{
|
||||
STOPWATCH_reset(); // reset clear channel detect timer
|
||||
|
||||
// enable RX interrupts
|
||||
rfm22_write(RFM22_interrupt_enable1, RFM22_ie1_encrcerror | RFM22_ie1_enpkvalid | RFM22_ie1_enrxffafull | RFM22_ie1_enfferr);
|
||||
rfm22_write(RFM22_interrupt_enable2, RFM22_ie2_enpreainval | RFM22_ie2_enpreaval | RFM22_ie2_enswdet);
|
||||
// enable RX interrupts
|
||||
rfm22_write(RFM22_interrupt_enable1, RFM22_ie1_encrcerror | RFM22_ie1_enpkvalid | RFM22_ie1_enrxffafull | RFM22_ie1_enfferr);
|
||||
rfm22_write(RFM22_interrupt_enable2, RFM22_ie2_enpreainval | RFM22_ie2_enpreaval | RFM22_ie2_enswdet);
|
||||
}
|
||||
|
||||
// read interrupt status - clear interrupts
|
||||
rfm22_read(RFM22_interrupt_status1);
|
||||
rfm22_read(RFM22_interrupt_status2);
|
||||
|
||||
// clear FIFOs
|
||||
rfm22_write(RFM22_op_and_func_ctrl2, RFM22_opfc2_ffclrrx | RFM22_opfc2_ffclrtx);
|
||||
rfm22_write(RFM22_op_and_func_ctrl2, 0x00);
|
||||
if (!multi_packet_mode)
|
||||
{
|
||||
rfm22_write(RFM22_op_and_func_ctrl2, RFM22_opfc2_ffclrrx | RFM22_opfc2_ffclrtx);
|
||||
rfm22_write(RFM22_op_and_func_ctrl2, 0x00);
|
||||
}
|
||||
else
|
||||
{
|
||||
rfm22_write(RFM22_op_and_func_ctrl2, RFM22_opfc2_rxmpk | RFM22_opfc2_ffclrrx | RFM22_opfc2_ffclrtx);
|
||||
rfm22_write(RFM22_op_and_func_ctrl2, RFM22_opfc2_rxmpk);
|
||||
}
|
||||
|
||||
// enable the receiver
|
||||
// rfm22_write(RFM22_op_and_func_ctrl1, RFM22_opfc1_xton | RFM22_opfc1_rxon);
|
||||
@ -891,8 +950,7 @@ void rfm22_processRxInt(void)
|
||||
debug_outputted = true;
|
||||
#endif
|
||||
|
||||
// rfm22_reenableRx(); // re-enable the receiver
|
||||
rfm22_setRxMode();
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false);
|
||||
return;
|
||||
}
|
||||
else
|
||||
@ -946,8 +1004,7 @@ void rfm22_processRxInt(void)
|
||||
debug_outputted = true;
|
||||
#endif
|
||||
|
||||
// rfm22_reenableRx(); // re-enable the receiver
|
||||
rfm22_setRxMode();
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -958,8 +1015,7 @@ void rfm22_processRxInt(void)
|
||||
debug_outputted = true;
|
||||
#endif
|
||||
|
||||
// rfm22_reenableRx(); // re-enable the receiver
|
||||
rfm22_setRxMode();
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false);
|
||||
return;
|
||||
}
|
||||
|
||||
@ -1001,8 +1057,7 @@ void rfm22_processRxInt(void)
|
||||
debug_outputted = true;
|
||||
#endif
|
||||
|
||||
// rfm22_reenableRx(); // re-enable the receiver
|
||||
rfm22_setRxMode(); // reset the receiver
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the receiver
|
||||
return;
|
||||
}
|
||||
}
|
||||
@ -1048,8 +1103,7 @@ void rfm22_processRxInt(void)
|
||||
rx_buffer_wr = wr;
|
||||
}
|
||||
|
||||
// rfm22_reenableRx(); // re-enable the receiver
|
||||
rfm22_setRxMode(); // reset the receiver
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the receiver
|
||||
|
||||
if (wr != len)
|
||||
{ // we have a packet length error .. discard the packet
|
||||
@ -1082,8 +1136,7 @@ void rfm22_processRxInt(void)
|
||||
}
|
||||
else
|
||||
{
|
||||
//// rfm22_reenableRx(); // re-enable the receiver
|
||||
// rfm22_setRxMode(); // reset the receiver
|
||||
// rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the receiver
|
||||
// return;
|
||||
}
|
||||
}
|
||||
@ -1097,7 +1150,7 @@ void rfm22_processTxInt(void)
|
||||
/*
|
||||
if (int_stat1 & RFM22_is1_ifferr)
|
||||
{ // FIFO underflow/overflow error
|
||||
rfm22_setRxMode();
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false);
|
||||
tx_data_addr = NULL;
|
||||
tx_data_rd = tx_data_wr = 0;
|
||||
return;
|
||||
@ -1152,7 +1205,7 @@ void rfm22_processTxInt(void)
|
||||
|
||||
if (rf_mode == TX_DATA_MODE)
|
||||
{
|
||||
rfm22_setRxMode(); // back to receive mode
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // back to receive mode
|
||||
|
||||
tx_data_addr = NULL;
|
||||
tx_data_rd = tx_data_wr = 0;
|
||||
@ -1261,6 +1314,9 @@ void rfm22_processInt(void)
|
||||
|
||||
switch (rf_mode)
|
||||
{
|
||||
case RX_SCAN_SPECTRUM:
|
||||
break;
|
||||
|
||||
case RX_WAIT_PREAMBLE_MODE:
|
||||
case RX_WAIT_SYNC_MODE:
|
||||
case RX_DATA_MODE:
|
||||
@ -1273,7 +1329,7 @@ void rfm22_processInt(void)
|
||||
debug_outputted = true;
|
||||
#endif
|
||||
|
||||
rfm22_setRxMode(); // reset the receiver
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the receiver
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1287,7 +1343,7 @@ void rfm22_processInt(void)
|
||||
debug_outputted = true;
|
||||
#endif
|
||||
|
||||
rfm22_setRxMode(); // reset the receiver
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the receiver
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1295,7 +1351,7 @@ void rfm22_processInt(void)
|
||||
if (rf_mode == RX_DATA_MODE && timer_ms >= timeout_data_ms)
|
||||
{ // missing interrupts
|
||||
rfm22_int_time_outs++;
|
||||
rfm22_setRxMode(); // reset the receiver
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the receiver
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1311,7 +1367,7 @@ void rfm22_processInt(void)
|
||||
debug_outputted = true;
|
||||
#endif
|
||||
|
||||
rfm22_setRxMode(); // reset the receiver
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the receiver
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1330,7 +1386,7 @@ void rfm22_processInt(void)
|
||||
debug_outputted = true;
|
||||
#endif
|
||||
|
||||
rfm22_setRxMode(); // back to rx mode
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // back to rx mode
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1338,7 +1394,7 @@ void rfm22_processInt(void)
|
||||
if (timer_ms >= timeout_data_ms)
|
||||
{
|
||||
rfm22_int_time_outs++;
|
||||
rfm22_setRxMode(); // back to rx mode
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // back to rx mode
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1354,7 +1410,7 @@ void rfm22_processInt(void)
|
||||
debug_outputted = true;
|
||||
#endif
|
||||
|
||||
rfm22_setRxMode(); // back to rx mode
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // back to rx mode
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1368,7 +1424,7 @@ void rfm22_processInt(void)
|
||||
|
||||
// if (timer_ms >= TX_TEST_MODE_TIMELIMIT_MS) // 'nn'ms limit
|
||||
// {
|
||||
// rfm22_setRxMode(); // back to rx mode
|
||||
// rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // back to rx mode
|
||||
// tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
// break;
|
||||
// }
|
||||
@ -1376,7 +1432,7 @@ void rfm22_processInt(void)
|
||||
break;
|
||||
|
||||
default: // unknown mode - this should NEVER happen, maybe we should do a complete CPU reset here
|
||||
rfm22_setRxMode(); // to rx mode
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // to rx mode
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1388,6 +1444,9 @@ void rfm22_processInt(void)
|
||||
{
|
||||
switch (rf_mode)
|
||||
{
|
||||
case RX_SCAN_SPECTRUM:
|
||||
DEBUG_PRINTF(" R_SCAN_SPECTRUM\r\n");
|
||||
break;
|
||||
case RX_WAIT_PREAMBLE_MODE:
|
||||
DEBUG_PRINTF(" R_WAIT_PREAMBLE\r\n");
|
||||
break;
|
||||
@ -1418,6 +1477,22 @@ void rfm22_processInt(void)
|
||||
|
||||
// ************************************
|
||||
|
||||
int16_t rfm22_getRSSI(void)
|
||||
{
|
||||
#if defined(RFM22_EXT_INT_USE)
|
||||
exec_using_spi = TRUE;
|
||||
#endif
|
||||
|
||||
rssi = rfm22_read(RFM22_rssi); // read rx signal strength .. 45 = -100dBm, 205 = -20dBm
|
||||
rssi_dBm = ((int16_t)rssi / 2) - 122; // convert to dBm
|
||||
|
||||
#if defined(RFM22_EXT_INT_USE)
|
||||
exec_using_spi = FALSE;
|
||||
#endif
|
||||
|
||||
return rssi_dBm;
|
||||
}
|
||||
|
||||
int16_t rfm22_receivedRSSI(void)
|
||||
{ // return the packets signal strength
|
||||
if (!initialized)
|
||||
@ -1468,7 +1543,7 @@ int32_t rfm22_sendData(void *data, uint16_t length, bool send_immediately)
|
||||
if (tx_data_wr > 0)
|
||||
return -4; // already have data to be sent
|
||||
|
||||
if (rf_mode == TX_DATA_MODE || rf_mode == TX_CARRIER_MODE || rf_mode == TX_PN_MODE)
|
||||
if (rf_mode == TX_DATA_MODE || rf_mode == TX_CARRIER_MODE || rf_mode == TX_PN_MODE || rf_mode == RX_SCAN_SPECTRUM)
|
||||
return -5; // we are currently transmitting
|
||||
|
||||
tx_data_addr = data;
|
||||
@ -1493,8 +1568,9 @@ void rfm22_setTxNormal(void)
|
||||
return;
|
||||
|
||||
// if (rf_mode == TX_CARRIER_MODE || rf_mode == TX_PN_MODE)
|
||||
if (rf_mode != RX_SCAN_SPECTRUM)
|
||||
{
|
||||
rfm22_setRxMode();
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false);
|
||||
tx_data_rd = tx_data_wr = 0;
|
||||
|
||||
rx_packet_wr = 0;
|
||||
@ -1511,7 +1587,7 @@ void rfm22_setTxCarrierMode(void)
|
||||
if (!initialized)
|
||||
return;
|
||||
|
||||
if (rf_mode != TX_CARRIER_MODE)
|
||||
if (rf_mode != TX_CARRIER_MODE && rf_mode != RX_SCAN_SPECTRUM)
|
||||
rfm22_setTxMode(TX_CARRIER_MODE);
|
||||
}
|
||||
|
||||
@ -1521,7 +1597,7 @@ void rfm22_setTxPNMode(void)
|
||||
if (!initialized)
|
||||
return;
|
||||
|
||||
if (rf_mode != TX_PN_MODE)
|
||||
if (rf_mode != TX_PN_MODE && rf_mode != RX_SCAN_SPECTRUM)
|
||||
rfm22_setTxMode(TX_PN_MODE);
|
||||
}
|
||||
|
||||
@ -1546,7 +1622,7 @@ bool rfm22_channelIsClear(void)
|
||||
return FALSE; // we haven't yet been initialized
|
||||
|
||||
if (rf_mode != RX_WAIT_PREAMBLE_MODE && rf_mode != RX_WAIT_SYNC_MODE)
|
||||
return FALSE; // we are receiving something or we are transmitting
|
||||
return FALSE; // we are receiving something or we are transmitting or we are scanning the spectrum
|
||||
|
||||
return TRUE;
|
||||
// return (STOPWATCH_get_count() > clear_channel_count);
|
||||
@ -1558,7 +1634,7 @@ bool rfm22_txReady(void)
|
||||
if (!initialized)
|
||||
return FALSE; // we haven't yet been initialized
|
||||
|
||||
return (tx_data_rd == 0 && tx_data_wr == 0 && rf_mode != TX_DATA_MODE && rf_mode != TX_CARRIER_MODE && rf_mode != TX_PN_MODE);
|
||||
return (tx_data_rd == 0 && tx_data_wr == 0 && rf_mode != TX_DATA_MODE && rf_mode != TX_CARRIER_MODE && rf_mode != TX_PN_MODE && rf_mode != RX_SCAN_SPECTRUM);
|
||||
}
|
||||
|
||||
// ************************************
|
||||
@ -1575,7 +1651,7 @@ void rfm22_setFreqCalibration(uint8_t value)
|
||||
|
||||
if (rf_mode == TX_CARRIER_MODE || rf_mode == TX_PN_MODE)
|
||||
{
|
||||
rfm22_setRxMode();
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false);
|
||||
tx_data_rd = tx_data_wr = 0;
|
||||
}
|
||||
|
||||
@ -1607,8 +1683,10 @@ void rfm22_1ms_tick(void)
|
||||
if (!initialized)
|
||||
return; // we haven't yet been initialized
|
||||
|
||||
if (rfm22_int_timer < 0xffff)
|
||||
rfm22_int_timer++;
|
||||
if (rf_mode != RX_SCAN_SPECTRUM)
|
||||
{
|
||||
if (rfm22_int_timer < 0xffff) rfm22_int_timer++;
|
||||
}
|
||||
}
|
||||
|
||||
// *****************************************************************************
|
||||
@ -1620,25 +1698,54 @@ void rfm22_process(void)
|
||||
return; // we haven't yet been initialized
|
||||
|
||||
#if !defined(RFM22_EXT_INT_USE)
|
||||
rfm22_processInt(); // manually poll the interrupt line routine
|
||||
if (rf_mode != RX_SCAN_SPECTRUM)
|
||||
rfm22_processInt(); // manually poll the interrupt line routine
|
||||
#endif
|
||||
|
||||
if (power_on_reset)
|
||||
{ // we need to re-initialize the RF module - it told us it's reset itself
|
||||
uint32_t current_freq = carrier_frequency_hz; // fetch current rf nominal frequency
|
||||
rfm22_init(lower_carrier_frequency_limit_Hz, upper_carrier_frequency_limit_Hz, rfm22_freqHopSize());
|
||||
rfm22_setNominalCarrierFrequency(current_freq); // restore the nominal carrier frequency
|
||||
if (rf_mode != RX_SCAN_SPECTRUM)
|
||||
{ // normal data mode
|
||||
uint32_t current_freq = carrier_frequency_hz; // fetch current rf nominal frequency
|
||||
rfm22_init_normal(lower_carrier_frequency_limit_Hz, upper_carrier_frequency_limit_Hz, rfm22_freqHopSize());
|
||||
rfm22_setNominalCarrierFrequency(current_freq); // restore the nominal carrier frequency
|
||||
}
|
||||
else
|
||||
{ // we are scanning the spectrum
|
||||
rfm22_init_scan_spectrum(lower_carrier_frequency_limit_Hz, upper_carrier_frequency_limit_Hz);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
switch (rf_mode)
|
||||
{
|
||||
case RX_SCAN_SPECTRUM: // we are scanning the spectrum
|
||||
|
||||
// read device status register
|
||||
device_status = rfm22_read(RFM22_device_status);
|
||||
|
||||
// read ezmac status register
|
||||
ezmac_status = rfm22_read(RFM22_ezmac_status);
|
||||
|
||||
// read interrupt status registers - clears the interrupt line
|
||||
int_status1 = rfm22_read(RFM22_interrupt_status1);
|
||||
int_status2 = rfm22_read(RFM22_interrupt_status2);
|
||||
|
||||
if (int_status2 & RFM22_is2_ipor)
|
||||
{ // the RF module has gone and done a reset - we need to re-initialize the rf module
|
||||
initialized = FALSE;
|
||||
power_on_reset = TRUE;
|
||||
return;
|
||||
}
|
||||
|
||||
break;
|
||||
|
||||
case RX_WAIT_PREAMBLE_MODE:
|
||||
|
||||
if (rfm22_int_timer >= timeout_ms)
|
||||
{ // assume somethings locked up
|
||||
rfm22_int_time_outs++;
|
||||
rfm22_setRxMode(); // reset the RF module to rx mode
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the RF module to rx mode
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1657,7 +1764,7 @@ void rfm22_process(void)
|
||||
if (rfm22_int_timer >= timeout_sync_ms)
|
||||
{ // assume somethings locked up
|
||||
rfm22_int_time_outs++;
|
||||
rfm22_setRxMode(); // reset the RF module to rx mode
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the RF module to rx mode
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1677,7 +1784,7 @@ void rfm22_process(void)
|
||||
if (rfm22_int_timer >= timeout_data_ms)
|
||||
{ // assume somethings locked up
|
||||
rfm22_int_time_outs++;
|
||||
rfm22_setRxMode(); // reset the RF module to rx mode
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // reset the RF module to rx mode
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1689,7 +1796,7 @@ void rfm22_process(void)
|
||||
|
||||
// if (rfm22_int_timer >= TX_TEST_MODE_TIMELIMIT_MS)
|
||||
// {
|
||||
// rfm22_setRxMode(); // back to rx mode
|
||||
// rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // back to rx mode
|
||||
// tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
// break;
|
||||
// }
|
||||
@ -1698,7 +1805,7 @@ void rfm22_process(void)
|
||||
|
||||
default:
|
||||
// unknown mode - this should never happen, maybe we should do a complete CPU reset here?
|
||||
rfm22_setRxMode(); // to rx mode
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false); // to rx mode
|
||||
tx_data_rd = tx_data_wr = 0; // wipe TX buffer
|
||||
break;
|
||||
}
|
||||
@ -1715,7 +1822,7 @@ void rfm22_process(void)
|
||||
// ************************************
|
||||
// reset the RF module
|
||||
|
||||
int rfm22_resetModule(void)
|
||||
int rfm22_resetModule(uint8_t mode)
|
||||
{
|
||||
initialized = false;
|
||||
|
||||
@ -1781,7 +1888,7 @@ int rfm22_resetModule(void)
|
||||
inside_ext_int = FALSE;
|
||||
#endif
|
||||
|
||||
rf_mode = RX_WAIT_PREAMBLE_MODE;
|
||||
rf_mode = mode;
|
||||
|
||||
device_status = int_status1 = int_status2 = ezmac_status = 0;
|
||||
|
||||
@ -1798,8 +1905,10 @@ int rfm22_resetModule(void)
|
||||
tx_data_rd = tx_data_wr = 0;
|
||||
|
||||
lookup_index = 0;
|
||||
ss_lookup_index = 0;
|
||||
|
||||
rf_bandwidth_used = 0;
|
||||
ss_rf_bandwidth_used = 0;
|
||||
|
||||
rfm22_int_timer = 0;
|
||||
rfm22_int_time_outs = 0;
|
||||
@ -1861,15 +1970,105 @@ int rfm22_resetModule(void)
|
||||
}
|
||||
|
||||
// ************************************
|
||||
// Initialise this hardware layer module and the rf module
|
||||
|
||||
int rfm22_init(uint32_t min_frequency_hz, uint32_t max_frequency_hz, uint32_t freq_hop_step_size)
|
||||
int rfm22_init_scan_spectrum(uint32_t min_frequency_hz, uint32_t max_frequency_hz)
|
||||
{
|
||||
#if defined(RFM22_DEBUG)
|
||||
DEBUG_PRINTF("\r\nRF init\r\n");
|
||||
DEBUG_PRINTF("\r\nRF init scan spectrum\r\n");
|
||||
#endif
|
||||
|
||||
int res = rfm22_resetModule();
|
||||
int res = rfm22_resetModule(RX_SCAN_SPECTRUM);
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
||||
// ****************
|
||||
// set the minimum and maximum carrier frequency allowed
|
||||
|
||||
if (min_frequency_hz < RFM22_MIN_CARRIER_FREQUENCY_HZ) min_frequency_hz = RFM22_MIN_CARRIER_FREQUENCY_HZ;
|
||||
else
|
||||
if (min_frequency_hz > RFM22_MAX_CARRIER_FREQUENCY_HZ) min_frequency_hz = RFM22_MAX_CARRIER_FREQUENCY_HZ;
|
||||
|
||||
if (max_frequency_hz < RFM22_MIN_CARRIER_FREQUENCY_HZ) max_frequency_hz = RFM22_MIN_CARRIER_FREQUENCY_HZ;
|
||||
else
|
||||
if (max_frequency_hz > RFM22_MAX_CARRIER_FREQUENCY_HZ) max_frequency_hz = RFM22_MAX_CARRIER_FREQUENCY_HZ;
|
||||
|
||||
if (min_frequency_hz > max_frequency_hz)
|
||||
{ // swap them over
|
||||
uint32_t tmp = min_frequency_hz;
|
||||
min_frequency_hz = max_frequency_hz;
|
||||
max_frequency_hz = tmp;
|
||||
}
|
||||
|
||||
lower_carrier_frequency_limit_Hz = min_frequency_hz;
|
||||
upper_carrier_frequency_limit_Hz = max_frequency_hz;
|
||||
|
||||
// calibrate our RF module to be exactly on frequency .. different for every module
|
||||
osc_load_cap = OSC_LOAD_CAP; // default
|
||||
rfm22_write(RFM22_xtal_osc_load_cap, osc_load_cap);
|
||||
|
||||
// rfm22_setSSBandwidth(0);
|
||||
rfm22_setSSBandwidth(1);
|
||||
|
||||
// FIFO mode, GFSK modulation
|
||||
uint8_t fd_bit = rfm22_read(RFM22_modulation_mode_control2) & RFM22_mmc2_fd;
|
||||
rfm22_write(RFM22_modulation_mode_control2, RFM22_mmc2_trclk_clk_none | RFM22_mmc2_dtmod_fifo | fd_bit | RFM22_mmc2_modtyp_gfsk);
|
||||
|
||||
rfm22_write(RFM22_cpu_output_clk, RFM22_coc_1MHz); // 1MHz clock output
|
||||
|
||||
rfm22_write(RFM22_rssi_threshold_clear_chan_indicator, 0);
|
||||
|
||||
rfm22_write(RFM22_preamble_detection_ctrl1, 31 << 3); // 31-nibbles rx preamble detection
|
||||
|
||||
// avoid packet detection
|
||||
rfm22_write(RFM22_data_access_control, RFM22_dac_enpacrx | RFM22_dac_encrc);
|
||||
rfm22_write(RFM22_header_control1, 0x0f);
|
||||
rfm22_write(RFM22_header_control2, 0x77);
|
||||
|
||||
rfm22_write(RFM22_sync_word3, SYNC_BYTE_1);
|
||||
rfm22_write(RFM22_sync_word2, SYNC_BYTE_2);
|
||||
rfm22_write(RFM22_sync_word1, SYNC_BYTE_3 ^ 0xff);
|
||||
rfm22_write(RFM22_sync_word0, SYNC_BYTE_4 ^ 0xff);
|
||||
|
||||
// all the bits to be checked
|
||||
rfm22_write(RFM22_header_enable3, 0xff);
|
||||
rfm22_write(RFM22_header_enable2, 0xff);
|
||||
rfm22_write(RFM22_header_enable1, 0xff);
|
||||
rfm22_write(RFM22_header_enable0, 0xff);
|
||||
|
||||
// rfm22_write(RFM22_frequency_hopping_step_size, 0); // set frequency hopping channel step size (multiples of 10kHz)
|
||||
|
||||
rfm22_setNominalCarrierFrequency(min_frequency_hz); // set our nominal carrier frequency
|
||||
|
||||
rfm22_write(RFM22_tx_power, RFM22_tx_pwr_lna_sw | 0); // set minimum tx power
|
||||
|
||||
rfm22_write(RFM22_agc_override1, RFM22_agc_ovr1_sgi | RFM22_agc_ovr1_agcen);
|
||||
|
||||
// rfm22_write(RFM22_vco_current_trimming, 0x7f);
|
||||
// rfm22_write(RFM22_vco_calibration_override, 0x40);
|
||||
// rfm22_write(RFM22_chargepump_current_trimming_override, 0x80);
|
||||
|
||||
#if defined(RFM22_EXT_INT_USE)
|
||||
// Enable RF module external interrupt
|
||||
rfm22_enableExtInt();
|
||||
#endif
|
||||
|
||||
rfm22_setRxMode(RX_SCAN_SPECTRUM, true);
|
||||
|
||||
initialized = true;
|
||||
|
||||
return 0; // OK
|
||||
}
|
||||
|
||||
// ************************************
|
||||
// Initialise this hardware layer module and the rf module
|
||||
|
||||
int rfm22_init_normal(uint32_t min_frequency_hz, uint32_t max_frequency_hz, uint32_t freq_hop_step_size)
|
||||
{
|
||||
#if defined(RFM22_DEBUG)
|
||||
DEBUG_PRINTF("\r\nRF init normal\r\n");
|
||||
#endif
|
||||
|
||||
int res = rfm22_resetModule(RX_WAIT_PREAMBLE_MODE);
|
||||
if (res < 0)
|
||||
return res;
|
||||
|
||||
@ -1915,6 +2114,7 @@ int rfm22_init(uint32_t min_frequency_hz, uint32_t max_frequency_hz, uint32_t fr
|
||||
*/ rfm22_write(RFM22_xtal_osc_load_cap, osc_load_cap);
|
||||
|
||||
// ****************
|
||||
|
||||
// set the RF datarate
|
||||
rfm22_setDatarate(RFM22_DEFAULT_RF_DATARATE);
|
||||
|
||||
@ -2001,7 +2201,7 @@ int rfm22_init(uint32_t min_frequency_hz, uint32_t max_frequency_hz, uint32_t fr
|
||||
rfm22_enableExtInt();
|
||||
#endif
|
||||
|
||||
rfm22_setRxMode();
|
||||
rfm22_setRxMode(RX_WAIT_PREAMBLE_MODE, false);
|
||||
|
||||
initialized = true;
|
||||
|
||||
|
@ -1,68 +0,0 @@
|
||||
/**
|
||||
******************************************************************************
|
||||
*
|
||||
* @file scan_spectrum.c
|
||||
* @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
|
||||
* @brief Puts module into Rx mode and scan across the RF spectrum monitoring the RSSI
|
||||
* @see The GNU Public License (GPL) Version 3
|
||||
*
|
||||
*****************************************************************************/
|
||||
/*
|
||||
* This program is free software; you can redistribute it and/or modify
|
||||
* it under the terms of the GNU General Public License as published by
|
||||
* the Free Software Foundation; either version 3 of the License, or
|
||||
* (at your option) any later version.
|
||||
*
|
||||
* This program is distributed in the hope that it will be useful, but
|
||||
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
|
||||
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
||||
* for more details.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License along
|
||||
* with this program; if not, write to the Free Software Foundation, Inc.,
|
||||
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
||||
*/
|
||||
|
||||
//#include <string.h> // memmove
|
||||
|
||||
#include "main.h"
|
||||
#include "rfm22b.h"
|
||||
#include "saved_settings.h"
|
||||
#include "scan_spectrum.h"
|
||||
|
||||
#if defined(PIOS_COM_DEBUG)
|
||||
#define SS_DEBUG
|
||||
#endif
|
||||
|
||||
// *************************************************************
|
||||
// can be called from an interrupt if you wish.
|
||||
// call this once every ms
|
||||
|
||||
void ss_1ms_tick(void)
|
||||
{
|
||||
if (saved_settings.mode == MODE_SCAN_SPECTRUM)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// *************************************************************
|
||||
// call this from the main loop (not interrupt) as often as possible
|
||||
|
||||
void ss_process(void)
|
||||
{
|
||||
if (saved_settings.mode == MODE_SCAN_SPECTRUM)
|
||||
{
|
||||
}
|
||||
}
|
||||
|
||||
// *************************************************************
|
||||
|
||||
void ss_init(void)
|
||||
{
|
||||
#if defined(SS_DEBUG)
|
||||
DEBUG_PRINTF("\r\nSS init\r\n");
|
||||
#endif
|
||||
|
||||
}
|
||||
|
||||
// *************************************************************
|
@ -29,6 +29,7 @@
|
||||
#include "gpio_in.h"
|
||||
#include "transparent_comms.h"
|
||||
#include "packet_handler.h"
|
||||
#include "saved_settings.h"
|
||||
#include "main.h"
|
||||
|
||||
#if defined(PIOS_COM_DEBUG)
|
||||
@ -104,99 +105,112 @@ void trans_process(void)
|
||||
// ********************
|
||||
// send the data received down the comm-port to the RF packet handler TX buffer
|
||||
|
||||
// free space size in the RF packet handler tx buffer
|
||||
uint16_t ph_num = ph_putData_free(connection_index);
|
||||
|
||||
// get the number of data bytes received down the comm-port
|
||||
int32_t com_num = PIOS_COM_ReceiveBufferUsed(comm_port);
|
||||
|
||||
// set the USART RTS handshaking line
|
||||
if (!usb_comms)
|
||||
if (saved_settings.mode == MODE_NORMAL || saved_settings.mode == MODE_STREAM_TX)
|
||||
{
|
||||
if (ph_num < 32 || !ph_connected(connection_index))
|
||||
SERIAL_RTS_CLEAR; // lower the USART RTS line - we don't have space in the buffer for anymore bytes
|
||||
// free space size in the RF packet handler tx buffer
|
||||
uint16_t ph_num = ph_putData_free(connection_index);
|
||||
|
||||
// get the number of data bytes received down the comm-port
|
||||
int32_t com_num = PIOS_COM_ReceiveBufferUsed(comm_port);
|
||||
|
||||
// set the USART RTS handshaking line
|
||||
if (!usb_comms)
|
||||
{
|
||||
if (ph_num < 32 || !ph_connected(connection_index))
|
||||
SERIAL_RTS_CLEAR; // lower the USART RTS line - we don't have space in the buffer for anymore bytes
|
||||
else
|
||||
SERIAL_RTS_SET; // release the USART RTS line - we have space in the buffer for now bytes
|
||||
}
|
||||
else
|
||||
SERIAL_RTS_SET; // release the USART RTS line - we have space in the buffer for now bytes
|
||||
SERIAL_RTS_SET; // release the USART RTS line
|
||||
|
||||
// limit number of bytes we will get to the size of the temp buffer
|
||||
if (com_num > sizeof(trans_temp_buffer1))
|
||||
com_num = sizeof(trans_temp_buffer1);
|
||||
|
||||
// limit number of bytes we will get to the size of the free space in the RF packet handler TX buffer
|
||||
if (com_num > ph_num)
|
||||
com_num = ph_num;
|
||||
|
||||
// copy data received down the comm-port into our temp buffer
|
||||
register uint16_t bytes_saved = 0;
|
||||
while (bytes_saved < com_num)
|
||||
trans_temp_buffer1[bytes_saved++] = PIOS_COM_ReceiveBuffer(comm_port);
|
||||
|
||||
// put the received comm-port data bytes into the RF packet handler TX buffer
|
||||
if (bytes_saved > 0)
|
||||
{
|
||||
trans_rx_timer = 0;
|
||||
ph_putData(connection_index, trans_temp_buffer1, bytes_saved);
|
||||
}
|
||||
}
|
||||
else
|
||||
SERIAL_RTS_SET; // release the USART RTS line
|
||||
|
||||
// limit number of bytes we will get to the size of the temp buffer
|
||||
if (com_num > sizeof(trans_temp_buffer1))
|
||||
com_num = sizeof(trans_temp_buffer1);
|
||||
|
||||
// limit number of bytes we will get to the size of the free space in the RF packet handler TX buffer
|
||||
if (com_num > ph_num)
|
||||
com_num = ph_num;
|
||||
|
||||
// copy data received down the comm-port into our temp buffer
|
||||
register uint16_t bytes_saved = 0;
|
||||
while (bytes_saved < com_num)
|
||||
trans_temp_buffer1[bytes_saved++] = PIOS_COM_ReceiveBuffer(comm_port);
|
||||
|
||||
// put the received comm-port data bytes into the RF packet handler TX buffer
|
||||
if (bytes_saved > 0)
|
||||
{
|
||||
trans_rx_timer = 0;
|
||||
ph_putData(connection_index, trans_temp_buffer1, bytes_saved);
|
||||
{ // empty the comm-ports rx buffer
|
||||
int32_t com_num = PIOS_COM_ReceiveBufferUsed(comm_port);
|
||||
while (com_num > 0)
|
||||
PIOS_COM_ReceiveBuffer(comm_port);
|
||||
}
|
||||
|
||||
// ********************
|
||||
// send the data received via the RF link out the comm-port
|
||||
|
||||
if (trans_temp_buffer2_wr < sizeof(trans_temp_buffer2))
|
||||
if (saved_settings.mode == MODE_NORMAL || saved_settings.mode == MODE_STREAM_RX)
|
||||
{
|
||||
// get number of data bytes received via the RF link
|
||||
ph_num = ph_getData_used(connection_index);
|
||||
if (trans_temp_buffer2_wr < sizeof(trans_temp_buffer2))
|
||||
{
|
||||
// get number of data bytes received via the RF link
|
||||
uint16_t ph_num = ph_getData_used(connection_index);
|
||||
|
||||
// limit to how much space we have in the temp buffer
|
||||
if (ph_num > sizeof(trans_temp_buffer2) - trans_temp_buffer2_wr)
|
||||
ph_num = sizeof(trans_temp_buffer2) - trans_temp_buffer2_wr;
|
||||
// limit to how much space we have in the temp buffer
|
||||
if (ph_num > sizeof(trans_temp_buffer2) - trans_temp_buffer2_wr)
|
||||
ph_num = sizeof(trans_temp_buffer2) - trans_temp_buffer2_wr;
|
||||
|
||||
if (ph_num > 0)
|
||||
{ // fetch the data bytes received via the RF link and save into our temp buffer
|
||||
ph_num = ph_getData(connection_index, trans_temp_buffer2 + trans_temp_buffer2_wr, ph_num);
|
||||
trans_temp_buffer2_wr += ph_num;
|
||||
if (ph_num > 0)
|
||||
{ // fetch the data bytes received via the RF link and save into our temp buffer
|
||||
ph_num = ph_getData(connection_index, trans_temp_buffer2 + trans_temp_buffer2_wr, ph_num);
|
||||
trans_temp_buffer2_wr += ph_num;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#if (defined(PIOS_COM_DEBUG) && (PIOS_COM_DEBUG == PIOS_COM_SERIAL))
|
||||
if (!usb_comms)
|
||||
{ // the serial-port is being used for debugging - don't send data down it
|
||||
trans_temp_buffer2_wr = 0;
|
||||
trans_tx_timer = 0;
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
if (trans_temp_buffer2_wr > 0)
|
||||
{ // we have data in our temp buffer that needs sending out the comm-port
|
||||
|
||||
if (usb_comms || (!usb_comms && GPIO_IN(SERIAL_CTS_PIN)))
|
||||
{ // we are OK to send the data out the comm-port
|
||||
|
||||
// send the data out the comm-port
|
||||
int32_t res;
|
||||
// if (usb_comms)
|
||||
// res = PIOS_COM_SendBuffer(comm_port, trans_temp_buffer2, trans_temp_buffer2_wr);
|
||||
// else
|
||||
res = PIOS_COM_SendBufferNonBlocking(comm_port, trans_temp_buffer2, trans_temp_buffer2_wr); // this one doesn't work properly with USB :(
|
||||
if (res >= 0)
|
||||
{ // data was sent out the comm-port OK .. remove the sent data from the temp buffer
|
||||
#if (defined(PIOS_COM_DEBUG) && (PIOS_COM_DEBUG == PIOS_COM_SERIAL))
|
||||
if (!usb_comms)
|
||||
{ // the serial-port is being used for debugging - don't send data down it
|
||||
trans_temp_buffer2_wr = 0;
|
||||
trans_tx_timer = 0;
|
||||
return;
|
||||
}
|
||||
else
|
||||
{ // failed to send the data out the comm-port
|
||||
#if defined(TRANS_DEBUG)
|
||||
DEBUG_PRINTF("PIOS_COM_SendBuffer %d %d\r\n", trans_temp_buffer2_wr, res);
|
||||
#endif
|
||||
#endif
|
||||
|
||||
if (trans_tx_timer >= 5000)
|
||||
trans_temp_buffer2_wr = 0; // seems we can't send our data for at least the last 5 seconds - delete it
|
||||
if (trans_temp_buffer2_wr > 0)
|
||||
{ // we have data in our temp buffer that needs sending out the comm-port
|
||||
|
||||
if (usb_comms || (!usb_comms && GPIO_IN(SERIAL_CTS_PIN)))
|
||||
{ // we are OK to send the data out the comm-port
|
||||
|
||||
// send the data out the comm-port
|
||||
int32_t res = PIOS_COM_SendBufferNonBlocking(comm_port, trans_temp_buffer2, trans_temp_buffer2_wr); // this one doesn't work properly with USB :(
|
||||
if (res >= 0)
|
||||
{ // data was sent out the comm-port OK .. remove the sent data from the temp buffer
|
||||
trans_temp_buffer2_wr = 0;
|
||||
trans_tx_timer = 0;
|
||||
}
|
||||
else
|
||||
{ // failed to send the data out the comm-port
|
||||
#if defined(TRANS_DEBUG)
|
||||
DEBUG_PRINTF("PIOS_COM_SendBuffer %d %d\r\n", trans_temp_buffer2_wr, res);
|
||||
#endif
|
||||
|
||||
if (trans_tx_timer >= 5000)
|
||||
trans_temp_buffer2_wr = 0; // seems we can't send our data for at least the last 5 seconds - delete it
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
else
|
||||
{ // empty the buffer
|
||||
trans_temp_buffer2_wr = 0;
|
||||
trans_tx_timer = 0;
|
||||
}
|
||||
|
||||
// ********************
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user