Skip to content

Commit

Permalink
RF: support for dynamic channel selection being assigned for a time slot
Browse files Browse the repository at this point in the history
  • Loading branch information
lyusupov committed Oct 6, 2021
1 parent ffc7d9a commit 19d8075
Show file tree
Hide file tree
Showing 11 changed files with 175 additions and 43 deletions.
4 changes: 2 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: c
env:
global:
- ARDUINO_IDE_VERSION=1.8.6
- ARDUINO_IDE_VERSION=1.8.13
- ENERGIA_IDE_VERSION=1.8.10E23
matrix:
- BOARD=esp8266:esp8266:nodemcuv2:xtal=80,eesz=4M3M
Expand Down Expand Up @@ -120,7 +120,7 @@ install:
fi
- if [[ "$BOARD" =~ "adafruit:nrf52:" ]]; then
arduino --pref "boardsmanager.additional.urls=https://www.adafruit.com/package_adafruit_index.json" --save-prefs ;
arduino --install-boards adafruit:nrf52:1.0.0 ;
arduino --install-boards adafruit:nrf52:1.1.0 ;
arduino --board $BOARD --save-prefs ;
arduino --pref "custom_debug=pca10056_l0" --save-prefs ;
arduino --pref "custom_softdevice=pca10056_s140v6" --save-prefs ;
Expand Down
132 changes: 98 additions & 34 deletions software/firmware/source/SoftRF/src/driver/RF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ FreqPlan RF_FreqPlan;
static bool RF_ready = false;

static size_t RF_tx_size = 0;
static long TxRandomValue = 0;

const rfchip_ops_t *rf_chip = NULL;
bool RF_SX12XX_RST_is_connected = true;
Expand All @@ -62,7 +61,12 @@ const char *Protocol_ID[] = {
};

size_t (*protocol_encode)(void *, ufo_t *);
bool (*protocol_decode)(void *, ufo_t *, ufo_t *);
bool (*protocol_decode)(void *, ufo_t *, ufo_t *);

static Slots_descr_t Time_Slots, *ts;
static uint8_t RF_timing = RF_TIMING_INTERVAL;

extern const gnss_chip_ops_t *gnss_chip;

static bool nrf905_probe(void);
static void nrf905_setup(void);
Expand Down Expand Up @@ -272,6 +276,34 @@ byte RF_setup(void)

if (rf_chip) {
rf_chip->setup();

const rf_proto_desc_t *p;

switch (settings->rf_protocol)
{
case RF_PROTOCOL_OGNTP: p = &ogntp_proto_desc; break;
case RF_PROTOCOL_P3I: p = &p3i_proto_desc; break;
case RF_PROTOCOL_FANET: p = &fanet_proto_desc; break;
case RF_PROTOCOL_ADSB_UAT: p = &uat978_proto_desc; break;
case RF_PROTOCOL_LEGACY:
default: p = &legacy_proto_desc; break;
}

RF_timing = p->tm_type;

ts = &Time_Slots;
ts->air_time = p->air_time;
ts->interval_min = p->tx_interval_min;
ts->interval_max = p->tx_interval_max;
ts->interval_mid = (p->tx_interval_max + p->tx_interval_min) / 2;
ts->s0.begin = p->slot0.begin;
ts->s1.begin = p->slot1.begin;
ts->s0.duration = p->slot0.end - p->slot0.begin;
ts->s1.duration = p->slot1.end - p->slot1.begin;

uint16_t duration = ts->s0.duration + ts->s1.duration;
ts->adj = duration > ts->interval_mid ? 0 : (ts->interval_mid - duration) / 2;

return rf_chip->type;
} else {
return RF_IC_NONE;
Expand All @@ -280,59 +312,81 @@ byte RF_setup(void)

void RF_SetChannel(void)
{
tmElements_t tm;
time_t Time;
tmElements_t tm;
time_t Time;
uint8_t Slot;
unsigned long pps_btime_ms, ref_time_ms;

switch (settings->mode)
{
case SOFTRF_MODE_TXRX_TEST:
Time = now();
RF_timing = RF_timing == RF_TIMING_2SLOTS_PPS_SYNC ?
RF_TIMING_INTERVAL : RF_timing;
break;
#if !defined(EXCLUDE_MAVLINK)
case SOFTRF_MODE_UAV:
Time = the_aircraft.location.gps_time_stamp / 1000000;
RF_timing = RF_timing == RF_TIMING_2SLOTS_PPS_SYNC ?
RF_TIMING_INTERVAL : RF_timing;
break;
#endif /* EXCLUDE_MAVLINK */
case SOFTRF_MODE_NORMAL:
default:
unsigned long pps_btime_ms = SoC->get_PPS_TimeMarker();
unsigned long time_corr_pos = 0;
unsigned long time_corr_neg = 0;
pps_btime_ms = SoC->get_PPS_TimeMarker();
unsigned long time_corr_neg;

if (pps_btime_ms) {
unsigned long lastCommitTime = millis() - gnss.time.age();
if (pps_btime_ms <= lastCommitTime) {
time_corr_neg = (lastCommitTime - pps_btime_ms) % 1000;
unsigned long last_Commit_Time = millis() - gnss.time.age();
if (pps_btime_ms <= last_Commit_Time) {
time_corr_neg = (last_Commit_Time - pps_btime_ms) % 1000;
} else {
time_corr_neg = 1000 - ((pps_btime_ms - lastCommitTime) % 1000);
time_corr_neg = 1000 - ((pps_btime_ms - last_Commit_Time) % 1000);
}
time_corr_pos = 400; /* 400 ms after PPS for V6, 350 ms - for OGNTP */
ref_time_ms = pps_btime_ms;
} else {
unsigned long last_RMC_Commit = millis() - gnss.date.age();
time_corr_neg = gnss_chip ? gnss_chip->rmc_ms : 100;
ref_time_ms = last_RMC_Commit - time_corr_neg;
}

int yr = gnss.date.year();
int yr = gnss.date.year();
if( yr > 99)
yr = yr - 1970;
yr = yr - 1970;
else
yr += 30;
tm.Year = yr;
tm.Month = gnss.date.month();
tm.Day = gnss.date.day();
tm.Hour = gnss.time.hour();
yr += 30;
tm.Year = yr;
tm.Month = gnss.date.month();
tm.Day = gnss.date.day();
tm.Hour = gnss.time.hour();
tm.Minute = gnss.time.minute();
tm.Second = gnss.time.second();

Time = makeTime(tm) + (gnss.time.age() - time_corr_neg + time_corr_pos)/ 1000;
Time = makeTime(tm) + (gnss.time.age() - time_corr_neg)/ 1000;
break;
}

uint8_t Slot = 0; /* only #0 "400ms" timeslot is currently in use */
uint8_t OGN = (settings->rf_protocol == RF_PROTOCOL_OGNTP ? 1 : 0);

/* FANET uses 868.2 MHz. Bandwidth is 250kHz */
if (settings->rf_protocol == RF_PROTOCOL_FANET) {
switch (RF_timing)
{
case RF_TIMING_2SLOTS_PPS_SYNC:
if ((millis() - ts->s0.tmarker) >= ts->interval_mid) {
ts->s0.tmarker = ref_time_ms + ts->s0.begin - ts->adj;
ts->current = 0;
}
if ((millis() - ts->s1.tmarker) >= ts->interval_mid) {
ts->s1.tmarker = ref_time_ms + ts->s1.begin;
ts->current = 1;
}
Slot = ts->current;
break;
case RF_TIMING_INTERVAL:
default:
Slot = 0;
break;
}

uint8_t OGN = (settings->rf_protocol == RF_PROTOCOL_OGNTP ? 1 : 0);

uint8_t chan = RF_FreqPlan.getChannel(Time, Slot, OGN);

#if DEBUG
Expand Down Expand Up @@ -375,7 +429,7 @@ size_t RF_Encode(ufo_t *fop)
return size;
}

if ((millis() - TxTimeMarker) > TxRandomValue) {
if (millis() > TxTimeMarker) {
size = (*protocol_encode)((void *) &TxBuffer[0], fop);
}
}
Expand All @@ -391,7 +445,7 @@ bool RF_Transmit(size_t size, bool wait)
return true;
}

if (!wait || (millis() - TxTimeMarker) > TxRandomValue) {
if (!wait || millis() > TxTimeMarker) {

time_t timestamp = now();

Expand All @@ -407,14 +461,24 @@ bool RF_Transmit(size_t size, bool wait)
tx_packets_counter++;
RF_tx_size = 0;

TxRandomValue = (
#if !defined(EXCLUDE_SX12XX)
LMIC.protocol ?
SoC->random(LMIC.protocol->tx_interval_min, LMIC.protocol->tx_interval_max) :
#endif
SoC->random(LEGACY_TX_INTERVAL_MIN, LEGACY_TX_INTERVAL_MAX));
Slot_descr_t *next;
unsigned long adj;

TxTimeMarker = millis();
switch (RF_timing)
{
case RF_TIMING_2SLOTS_PPS_SYNC:
next = RF_FreqPlan.Channels == 1 ? &(ts->s0) :
ts->current == 1 ? &(ts->s0) : &(ts->s1);
adj = ts->current ? ts->adj : 0;
TxTimeMarker = next->tmarker +
ts->interval_mid +
SoC->random(adj, next->duration - ts->air_time);
break;
case RF_TIMING_INTERVAL:
default:
TxTimeMarker = millis() + SoC->random(ts->interval_min, ts->interval_max) - ts->air_time;
break;
}

return true;
}
Expand Down
17 changes: 17 additions & 0 deletions software/firmware/source/SoftRF/src/driver/RF.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,23 @@ typedef struct rfchip_ops_struct {
void (*shutdown)();
} rfchip_ops_t;

typedef struct Slot_descr_struct {
uint16_t begin;
uint16_t duration;
unsigned long tmarker;
} Slot_descr_t;

typedef struct Slots_descr_struct {
uint16_t interval_min;
uint16_t interval_max;
uint16_t interval_mid;
uint16_t adj;
uint16_t air_time;
Slot_descr_t s0;
Slot_descr_t s1;
uint8_t current;
} Slots_descr_t;

String Bin2Hex(byte *, size_t);
uint8_t parity(uint32_t);

Expand Down
3 changes: 2 additions & 1 deletion software/firmware/source/SoftRF/src/platform/PSoC4.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,8 @@ struct rst_info {

#if defined(CubeCell_GPS)
#define SOC_GPIO_PIN_LED RGB // P0_6
#define SOC_GPIO_PIN_GNSS_PPS GPIO8
#define SOC_GPIO_PIN_GNSS_PPS GPIO8 /* V1.0 */
//#define SOC_GPIO_PIN_GNSS_PPS GPIO12 /* V1.1 */
#define SOC_GPIO_PIN_BATTERY ADC1 // P2_0

#define SOC_GPIO_PIN_GNSS_PWR GPIO14 // P0_7
Expand Down
5 changes: 4 additions & 1 deletion software/firmware/source/SoftRF/src/protocol/radio/FANET.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,11 @@ const rf_proto_desc_t fanet_proto_desc = {

.air_time = FANET_AIR_TIME,

.tm_type = RF_TIMING_INTERVAL,
.tx_interval_min = FANET_TX_INTERVAL_MIN,
.tx_interval_max = FANET_TX_INTERVAL_MAX
.tx_interval_max = FANET_TX_INTERVAL_MAX,
.slot0 = {0, 0},
.slot1 = {0, 0}
};

const uint8_t aircraft_type_to_fanet[] PROGMEM = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,15 @@ const rf_proto_desc_t legacy_proto_desc = {

.air_time = LEGACY_AIR_TIME,

#if defined(USE_TIME_SLOTS)
.tm_type = RF_TIMING_2SLOTS_PPS_SYNC,
#else
.tm_type = RF_TIMING_INTERVAL,
#endif
.tx_interval_min = LEGACY_TX_INTERVAL_MIN,
.tx_interval_max = LEGACY_TX_INTERVAL_MAX
.tx_interval_max = LEGACY_TX_INTERVAL_MAX,
.slot0 = {400, 800},
.slot1 = {800, 1200}
};

/* http://en.wikipedia.org/wiki/XXTEA */
Expand Down
9 changes: 8 additions & 1 deletion software/firmware/source/SoftRF/src/protocol/radio/OGNTP.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,15 @@ const rf_proto_desc_t ogntp_proto_desc = {

.air_time = OGNTP_AIR_TIME,

#if defined(USE_TIME_SLOTS)
.tm_type = RF_TIMING_2SLOTS_PPS_SYNC,
#else
.tm_type = RF_TIMING_INTERVAL,
#endif
.tx_interval_min = OGNTP_TX_INTERVAL_MIN,
.tx_interval_max = OGNTP_TX_INTERVAL_MAX
.tx_interval_max = OGNTP_TX_INTERVAL_MAX,
.slot0 = {400, 800},
.slot1 = {800, 1200}
};

static GPS_Position pos;
Expand Down
6 changes: 4 additions & 2 deletions software/firmware/source/SoftRF/src/protocol/radio/P3I.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,11 +47,13 @@ const rf_proto_desc_t p3i_proto_desc = {

.air_time = P3I_AIR_TIME,

.tm_type = RF_TIMING_INTERVAL,
.tx_interval_min = P3I_TX_INTERVAL_MIN,
.tx_interval_max = P3I_TX_INTERVAL_MAX
.tx_interval_max = P3I_TX_INTERVAL_MAX,
.slot0 = {0, 0},
.slot1 = {0, 0}
};


const uint8_t whitening_pattern[] PROGMEM = { 0x05, 0xb4, 0x05, 0xae, 0x14, 0xda,
0xbf, 0x83, 0xc4, 0x04, 0xb2, 0x04, 0xd6, 0x4d, 0x87, 0xe2, 0x01, 0xa3, 0x26,
0xac, 0xbb, 0x63, 0xf1, 0x01, 0xca, 0x07, 0xbd, 0xaf, 0x60, 0xc8, 0x12, 0xed,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,11 @@ const rf_proto_desc_t uat978_proto_desc = {

.air_time = UAT978_AIR_TIME,

.tm_type = RF_TIMING_INTERVAL,
.tx_interval_min = UAT978_TX_INTERVAL_MIN,
.tx_interval_max = UAT978_TX_INTERVAL_MAX
.tx_interval_max = UAT978_TX_INTERVAL_MAX,
.slot0 = {0, 0},
.slot1 = {0, 0}
};

static struct uat_adsb_mdb mdb;
Expand Down
14 changes: 14 additions & 0 deletions software/firmware/source/libraries/arduino-basicmac/src/protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,8 +96,19 @@ enum
RF_RX_BANDWIDTH_SS_1567KHZ
};

enum
{
RF_TIMING_INTERVAL,
RF_TIMING_2SLOTS_PPS_SYNC
};

#define RF_MAX_SYNC_WORD_SIZE 8

typedef struct tslot_struct {
uint16_t begin;
uint16_t end;
} tslot_t;

typedef struct RF_PROTOCOL {
const char name[10];
uint8_t type;
Expand All @@ -120,8 +131,11 @@ typedef struct RF_PROTOCOL {

uint16_t air_time;

uint8_t tm_type;
uint16_t tx_interval_min;
uint16_t tx_interval_max;
tslot_t slot0;
tslot_t slot1;
} rf_proto_desc_t;

#endif /* PROTOCOL_H */
Loading

0 comments on commit 19d8075

Please sign in to comment.