1
0
mirror of https://bitbucket.org/librepilot/librepilot.git synced 2024-12-01 09:24:10 +01:00

Slight reorganisation to allow easier continuous data transmission (DATA streaming and PPM streaming) and allow easier total configuration changes on the fly.

git-svn-id: svn://svn.openpilot.org/OpenPilot/trunk@2961 ebee16cc-31ac-478f-84a7-5cbb03baadba
This commit is contained in:
pip 2011-03-04 08:34:44 +00:00 committed by pip
parent 32c7596636
commit 7317891d39
12 changed files with 252 additions and 102 deletions

View File

@ -129,6 +129,7 @@ SRC += $(PIOSSTM32F10X)/pios_irq.c
SRC += $(PIOSSTM32F10X)/pios_adc.c
SRC += $(PIOSSTM32F10X)/pios_gpio.c
SRC += $(PIOSSTM32F10X)/pios_spi.c
SRC += $(PIOSSTM32F10X)/pios_ppm.c
# PIOS USB related files (seperated to make code maintenance more easy)
ifeq ($(USE_USB), YES)

View File

@ -30,6 +30,8 @@
#include "api_config.h"
#include "rfm22b.h"
#include "packet_handler.h"
#include "stream.h"
#include "ppm.h"
#include "saved_settings.h"
#include "crc.h"
#include "main.h"
@ -344,6 +346,8 @@ void apiconfig_processInputPacket(void *buf, uint16_t len)
if (header->serial_number == serial_number_crc32)
{ // the packet is meant for us
booting = true;
ph_set_remote_serial_number(0, 0); // stop the packet handler trying to communicate
// ******
@ -384,59 +388,53 @@ void apiconfig_processInputPacket(void *buf, uint16_t len)
break;
}
if (saved_settings.mode != MODE_SCAN_SPECTRUM)
{
if (saved_settings.mode == MODE_STREAM_TX || saved_settings.mode == MODE_PPM_TX)
rfm22_init_tx_stream(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz);
else
if (saved_settings.mode == MODE_STREAM_RX || saved_settings.mode == MODE_PPM_RX)
rfm22_init_rx_stream(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz);
else
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)
{
case MODE_NORMAL: // normal 2-way packet mode
case MODE_NORMAL: // normal 2-way packet mode
ph_init(serial_number_crc32); // initialise the packet handler
break;
case MODE_STREAM_TX: // 1-way continuous tx packet mode
rfm22_setTxStream(); // TEST ONLY
case MODE_STREAM_TX: // 1-way continuous tx packet mode
case MODE_STREAM_RX: // 1-way continuous rx packet mode
stream_init(serial_number_crc32); // initialise the continuous packet stream module
break;
case MODE_STREAM_RX: // 1-way continuous rx packet mode
break;
case MODE_PPM_TX: // PPM tx mode
rfm22_setTxStream(); // TEST ONLY
break;
case MODE_PPM_RX: // PPM rx mode
case MODE_PPM_TX: // PPM tx mode
case MODE_PPM_RX: // PPM rx mode
ppm_init(serial_number_crc32); // initialise the PPM module
break;
case MODE_SCAN_SPECTRUM: // scan the receiver over the whole band
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;
break;
case MODE_TX_BLANK_CARRIER_TEST: // blank carrier Tx mode (for calibrating the carrier frequency say)
rfm22_init_normal(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz, rfm22_freqHopSize());
rfm22_setFreqCalibration(saved_settings.rf_xtal_cap);
rfm22_setNominalCarrierFrequency(saved_settings.frequency_Hz);
rfm22_setDatarate(saved_settings.max_rf_bandwidth, TRUE);
rfm22_setTxPower(saved_settings.max_tx_power);
rfm22_setTxCarrierMode();
break;
case MODE_TX_SPECTRUM_TEST: // pseudo random Tx data mode (for checking the Tx carrier spectrum)
rfm22_init_normal(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz, rfm22_freqHopSize());
rfm22_setFreqCalibration(saved_settings.rf_xtal_cap);
rfm22_setNominalCarrierFrequency(saved_settings.frequency_Hz);
rfm22_setDatarate(saved_settings.max_rf_bandwidth, TRUE);
rfm22_setTxPower(saved_settings.max_tx_power);
rfm22_setTxPNMode();
break;
}
// ******
booting = false;
}
break;

View File

@ -68,7 +68,7 @@ void ph_set_AES128_key(const void *key);
int ph_set_remote_serial_number(int connection_index, uint32_t sn);
void ph_set_remote_encryption(int connection_index, bool enabled, const void *key);
void ph_init(uint32_t our_sn, uint32_t datarate_bps, uint8_t tx_power);
void ph_init(uint32_t our_sn);
// *****************************************************************************

View File

@ -40,5 +40,6 @@
#define PIOS_INCLUDE_GPIO
#define PIOS_INCLUDE_EXTI
#define PIOS_INCLUDE_USB_HID
//#define PIOS_INCLUDE_PPM
#endif /* PIOS_CONFIG_H */

View File

@ -32,7 +32,7 @@
void ppm_1ms_tick(void);
void ppm_process(void);
void ppm_init(void);
void ppm_init(uint32_t our_sn);
// *************************************************************

View File

@ -575,7 +575,8 @@ enum { RX_SCAN_SPECTRUM = 0,
// ************************************
typedef void ( *t_rfm22_TxDataCallback ) (uint8_t *data, uint16_t len);
typedef int16_t ( *t_rfm22_TxDataByteCallback ) (void);
typedef bool ( *t_rfm22_RxDataByteCallback ) (uint8_t b);
// ************************************
@ -629,7 +630,8 @@ bool rfm22_txReady(void);
void rfm22_1ms_tick(void);
void rfm22_process(void);
void rfm22_TxData_SetCallback(t_rfm22_TxDataCallback new_function);
void rfm22_TxDataByte_SetCallback(t_rfm22_TxDataByteCallback new_function);
void rfm22_RxDataByte_SetCallback(t_rfm22_RxDataByteCallback new_function);
int rfm22_init_scan_spectrum(uint32_t min_frequency_hz, uint32_t max_frequency_hz);
int rfm22_init_tx_stream(uint32_t min_frequency_hz, uint32_t max_frequency_hz);

View File

@ -32,7 +32,7 @@
void stream_1ms_tick(void);
void stream_process(void);
void stream_init(void);
void stream_init(uint32_t our_sn);
// *************************************************************

View File

@ -1,3 +1,4 @@
/**
******************************************************************************
*
@ -391,9 +392,9 @@ void init_RF_module(void)
}
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);
rfm22_setNominalCarrierFrequency(saved_settings.frequency_Hz);
rfm22_setDatarate(saved_settings.max_rf_bandwidth, TRUE);
rfm22_setTxPower(saved_settings.max_tx_power);
}
// *****************************************************************************
@ -553,12 +554,10 @@ int main()
// *************
random32 ^= serial_number_crc32; // try to randomize the random number
// random32 ^= serial_number_crc32 ^ CRC_IDR; // try to randomize the random number
random32 ^= serial_number_crc32; // try to randomise the random number
// random32 ^= serial_number_crc32 ^ CRC_IDR; // try to randomise the random number
ph_init(serial_number_crc32, 128000, 0); // initialise the packet handler
trans_init(); // initialise the tranparent communications module
trans_init(); // initialise the transparent communications module
// api_init(); // initialise the API communications module
apiconfig_init(); // initialise the API communications module
@ -728,41 +727,57 @@ int main()
init_RF_module(); // initialise the RF module
stream_init(); // initialise the continuous packet stream module
// *************
ppm_init(); // initialise the ppm module
#if defined(PIOS_COM_DEBUG)
DEBUG_PRINTF("\r\n");
DEBUG_PRINTF("RF datarate: %dbps\r\n", ph_getDatarate());
DEBUG_PRINTF("RF frequency: %dHz\r\n", rfm22_getNominalCarrierFrequency());
DEBUG_PRINTF("RF TX power: %d\r\n", ph_getTxPower());
saved_settings_save();
DEBUG_PRINTF("\r\nUnit mode: ");
switch (saved_settings.mode)
{
case MODE_NORMAL: DEBUG_PRINTF("NORMAL\r\n"); break;
case MODE_STREAM_TX: DEBUG_PRINTF("STREAM-TX\r\n"); break;
case MODE_STREAM_RX: DEBUG_PRINTF("STREAM-RX\r\n"); break;
case MODE_PPM_TX: DEBUG_PRINTF("PPM-TX\r\n"); break;
case MODE_PPM_RX: DEBUG_PRINTF("PPM-RX\r\n"); break;
case MODE_SCAN_SPECTRUM: DEBUG_PRINTF("SCAN-SPECTRUM\r\n"); break;
case MODE_TX_BLANK_CARRIER_TEST: DEBUG_PRINTF("TX-BLANK-CARRIER\r\n"); break;
case MODE_TX_SPECTRUM_TEST: DEBUG_PRINTF("TX_SPECTRUM\r\n"); break;
default: DEBUG_PRINTF("UNKNOWN [%u]\r\n", saved_settings.mode); break;
}
#endif
booting = FALSE;
switch (saved_settings.mode)
{
case MODE_NORMAL:
ph_init(serial_number_crc32); // initialise the packet handler
break;
case MODE_STREAM_TX:
case MODE_STREAM_RX:
stream_init(serial_number_crc32); // initialise the continuous packet stream module
break;
case MODE_PPM_TX:
case MODE_PPM_RX:
ppm_init(serial_number_crc32); // initialise the ppm module
break;
case MODE_SCAN_SPECTRUM:
case MODE_TX_BLANK_CARRIER_TEST:
case MODE_TX_SPECTRUM_TEST:
break;
}
// *************
// Main executive loop
#if defined(PIOS_COM_DEBUG)
DEBUG_PRINTF("\r\n");
DEBUG_PRINTF("RF datarate: %dbps\r\n", ph_getDatarate());
DEBUG_PRINTF("RF frequency: %dHz\r\n", rfm22_getNominalCarrierFrequency());
DEBUG_PRINTF("RF TX power: %d\r\n", ph_getTxPower());
saved_settings_save();
DEBUG_PRINTF("\r\nUnit mode: ");
switch (saved_settings.mode)
{
case MODE_NORMAL: DEBUG_PRINTF("NORMAL\r\n"); break;
case MODE_STREAM_TX: DEBUG_PRINTF("STREAM-TX\r\n"); break;
case MODE_STREAM_RX: DEBUG_PRINTF("STREAM-RX\r\n"); break;
case MODE_PPM_TX: DEBUG_PRINTF("PPM-TX\r\n"); break;
case MODE_PPM_RX: DEBUG_PRINTF("PPM-RX\r\n"); break;
case MODE_SCAN_SPECTRUM: DEBUG_PRINTF("SCAN-SPECTRUM\r\n"); break;
case MODE_TX_BLANK_CARRIER_TEST: DEBUG_PRINTF("TX-BLANK-CARRIER\r\n"); break;
case MODE_TX_SPECTRUM_TEST: DEBUG_PRINTF("TX_SPECTRUM\r\n"); break;
default: DEBUG_PRINTF("UNKNOWN [%u]\r\n", saved_settings.mode); break;
}
#endif
// start a remote connection going to another modem
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);
booting = FALSE;
for (;;)
{

View File

@ -348,10 +348,23 @@ int ph_startConnect(int connection_index, uint32_t sn)
}
// *****************************************************************************
// return a byte for the tx packet transmission.
//
// return value < 0 if no more bytes available, otherwise return byte to be sent
void rfm22_TxDataCallback(uint8_t *data, uint16_t len)
int16_t ph_TxDataByteCallback(void)
{
return -1;
}
// *************************************************************
// we are being given a received byte
//
// return TRUE to continue current packet receive, otherwise return FALSe to halt current packet reception
bool ph_RxDataByteCallback(uint8_t b)
{
return true;
}
// *****************************************************************************
@ -1518,8 +1531,6 @@ int ph_set_remote_serial_number(int connection_index, uint32_t sn)
fifoBuf_init(&conn->tx_fifo_buffer, conn->tx_buffer, PH_FIFO_BUFFER_SIZE);
fifoBuf_init(&conn->rx_fifo_buffer, conn->rx_buffer, PH_FIFO_BUFFER_SIZE);
rfm22_TxData_SetCallback(rfm22_TxDataCallback);
return connection_index;
}
@ -1542,6 +1553,8 @@ void ph_set_remote_encryption(int connection_index, bool enabled, const void *ke
void ph_1ms_tick(void)
{
if (booting) return;
if (saved_settings.mode == MODE_NORMAL)
{
// help randomize the encryptor cbc bytes
@ -1597,6 +1610,8 @@ void ph_1ms_tick(void)
void ph_process(void)
{
if (booting) return;
if (saved_settings.mode == MODE_NORMAL)
{
ph_processRxPacket();
@ -1612,7 +1627,7 @@ void ph_process(void)
// *****************************************************************************
void ph_init(uint32_t our_sn, uint32_t datarate_bps, uint8_t tx_power)
void ph_init(uint32_t our_sn)
{
our_serial_number = our_sn; // remember our own serial number
@ -1665,20 +1680,33 @@ void ph_init(uint32_t our_sn, uint32_t datarate_bps, uint8_t tx_power)
conn->rx_afc_Hz = 0;
}
ph_setDatarate(datarate_bps);
ph_setTxPower(tx_power);
// set the AES encryption key using the default AES key
ph_set_AES128_key(default_aes_key);
// try too randomize the tx AES CBC bytes
// try too randomise the tx AES CBC bytes
for (uint32_t j = 0, k = 0; j < 123 + (random32 & 1023); j++)
{
random32 = updateCRC32(random32, 0xff);
enc_cbc[k] ^= random32 >> 3;
if (++k >= sizeof(enc_cbc)) k = 0;
}
// ******
rfm22_init_normal(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz, rfm22_freqHopSize());
rfm22_TxDataByte_SetCallback(ph_TxDataByteCallback);
rfm22_RxDataByte_SetCallback(ph_RxDataByteCallback);
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);
// ******
}
// *****************************************************************************

View File

@ -40,6 +40,8 @@
void ppm_1ms_tick(void)
{
if (booting) return;
if (saved_settings.mode == MODE_PPM_TX)
{
}
@ -49,11 +51,33 @@ void ppm_1ms_tick(void)
}
}
// *************************************************************
// return a byte for the tx packet transmission.
//
// return value < 0 if no more bytes available, otherwise return byte to be sent
int16_t ppm_TxDataByteCallback(void)
{
return -1;
}
// *************************************************************
// we are being given a received byte
//
// return TRUE to continue current packet receive, otherwise return FALSe to halt current packet reception
bool ppm_RxDataByteCallback(uint8_t b)
{
return true;
}
// *************************************************************
// call this from the main loop (not interrupt) as often as possible
void ppm_process(void)
{
if (booting) return;
if (saved_settings.mode == MODE_PPM_TX)
{
}
@ -65,12 +89,27 @@ void ppm_process(void)
// *************************************************************
void ppm_init(void)
void ppm_init(uint32_t our_sn)
{
#if defined(PPM_DEBUG)
DEBUG_PRINTF("\r\nPPM init\r\n");
#endif
if (saved_settings.mode == MODE_PPM_TX)
rfm22_init_tx_stream(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz);
else
if (saved_settings.mode == MODE_PPM_RX)
rfm22_init_rx_stream(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz);
rfm22_TxDataByte_SetCallback(ppm_TxDataByteCallback);
rfm22_RxDataByte_SetCallback(ppm_RxDataByteCallback);
rfm22_setFreqCalibration(saved_settings.rf_xtal_cap);
rfm22_setNominalCarrierFrequency(saved_settings.frequency_Hz);
rfm22_setDatarate(saved_settings.max_rf_bandwidth, FALSE);
rfm22_setTxPower(saved_settings.max_tx_power);
rfm22_setTxStream(); // TEST ONLY
}
// *************************************************************

View File

@ -287,7 +287,8 @@ uint16_t timeout_ms = 20000; //
uint16_t timeout_sync_ms = 3; //
uint16_t timeout_data_ms = 20; //
t_rfm22_TxDataCallback tx_data_callback_function;
t_rfm22_TxDataByteCallback tx_data_byte_callback_function;
t_rfm22_RxDataByteCallback rx_data_byte_callback_function;
// ************************************
// SPI read/write
@ -922,6 +923,10 @@ void rfm22_setTxMode(uint8_t mode)
// add some data
for (uint16_t j = wr - rd; j > 0; j--)
{
// int16_t b = -1;
// if (tx_data_byte_callback_function)
// b = tx_data_byte_callback_function();
rfm22_burstWrite(tx_data_addr[rd++]);
if (++i >= max_bytes) break;
}
@ -1082,6 +1087,12 @@ void rfm22_processRxInt(void)
for (register uint16_t i = RX_FIFO_HI_WATERMARK; i > 0; i--)
{
register uint8_t b = rfm22_burstRead(); // read a byte from the rf modules RX FIFO buffer
if (rx_data_byte_callback_function)
{
rx_data_byte_callback_function(b);
}
if (wr < sizeof(rx_buffer))
rx_buffer[wr++] = b; // save the byte into our rx buffer
}
@ -1151,11 +1162,18 @@ void rfm22_processRxInt(void)
{ // their must still be data in the RX FIFO we need to get
rfm22_startBurstRead(RFM22_fifo_access);
while (wr < len)
{
if (wr >= sizeof(rx_buffer)) break;
rx_buffer[wr++] = rfm22_burstRead();
}
while (wr < len)
{
register uint8_t b = rfm22_burstRead();
if (rx_data_byte_callback_function)
{
rx_data_byte_callback_function(b);
}
if (wr >= sizeof(rx_buffer)) break;
rx_buffer[wr++] = b;
}
rfm22_endBurstRead();
rx_buffer_wr = wr;
@ -1220,6 +1238,10 @@ uint8_t rfm22_topUpRFTxFIFO(void)
// add some data
for (uint16_t j = wr - rd; j > 0; j--)
{
// int16_t b = -1;
// if (tx_data_byte_callback_function)
// b = tx_data_byte_callback_function();
rfm22_burstWrite(tx_data_addr[rd++]);
if (++i >= max_bytes) break;
}
@ -1794,9 +1816,8 @@ uint8_t rfm22_getFreqCalibration(void)
void rfm22_1ms_tick(void)
{ // call this once every ms
if (!initialized)
return; // we haven't yet been initialized
if (booting) return;
if (!initialized) return; // we haven't yet been initialized
if (rf_mode != RX_SCAN_SPECTRUM)
{
@ -1809,8 +1830,8 @@ void rfm22_1ms_tick(void)
void rfm22_process(void)
{
if (!initialized)
return; // we haven't yet been initialized
if (booting) return;
if (!initialized) return; // we haven't yet been initialized
#if !defined(RFM22_EXT_INT_USE)
if (rf_mode != RX_SCAN_SPECTRUM)
@ -1942,9 +1963,14 @@ void rfm22_process(void)
// ************************************
void rfm22_TxData_SetCallback(t_rfm22_TxDataCallback new_function)
void rfm22_TxDataByte_SetCallback(t_rfm22_TxDataByteCallback new_function)
{
tx_data_callback_function = new_function;
tx_data_byte_callback_function = new_function;
}
void rfm22_RxDataByte_SetCallback(t_rfm22_RxDataByteCallback new_function)
{
rx_data_byte_callback_function = new_function;
}
// ************************************
@ -2028,7 +2054,8 @@ int rfm22_resetModule(uint8_t mode, uint32_t min_frequency_hz, uint32_t max_freq
rssi = 0;
rssi_dBm = -200;
tx_data_callback_function = NULL;
tx_data_byte_callback_function = NULL;
rx_data_byte_callback_function = NULL;
rx_buffer_current = 0;
rx_buffer_wr = 0;

View File

@ -43,6 +43,8 @@
void stream_1ms_tick(void)
{
if (booting) return;
if (saved_settings.mode == MODE_STREAM_TX)
{
}
@ -52,11 +54,33 @@ void stream_1ms_tick(void)
}
}
// *************************************************************
// return a byte for the tx packet transmission.
//
// return value < 0 if no more bytes available, otherwise return byte to be sent
int16_t stream_TxDataByteCallback(void)
{
return -1;
}
// *************************************************************
// we are being given a received byte
//
// return TRUE to continue current packet receive, otherwise return FALSe to halt current packet reception
bool stream_RxDataByteCallback(uint8_t b)
{
return true;
}
// *************************************************************
// call this from the main loop (not interrupt) as often as possible
void stream_process(void)
{
if (booting) return;
if (saved_settings.mode == MODE_STREAM_TX)
{
}
@ -68,12 +92,27 @@ void stream_process(void)
// *************************************************************
void stream_init(void)
void stream_init(uint32_t our_sn)
{
#if defined(STREAM_DEBUG)
DEBUG_PRINTF("\r\nSTREAM init\r\n");
#endif
if (saved_settings.mode == MODE_STREAM_TX)
rfm22_init_tx_stream(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz);
else
if (saved_settings.mode == MODE_STREAM_RX)
rfm22_init_rx_stream(saved_settings.min_frequency_Hz, saved_settings.max_frequency_Hz);
rfm22_TxDataByte_SetCallback(stream_TxDataByteCallback);
rfm22_RxDataByte_SetCallback(stream_RxDataByteCallback);
rfm22_setFreqCalibration(saved_settings.rf_xtal_cap);
rfm22_setNominalCarrierFrequency(saved_settings.frequency_Hz);
rfm22_setDatarate(saved_settings.max_rf_bandwidth, FALSE);
rfm22_setTxPower(saved_settings.max_tx_power);
rfm22_setTxStream(); // TEST ONLY
}
// *************************************************************