Skip to content

Commit cf99eb0

Browse files
committed
Avoid switching to rx on each packet
1 parent b89a136 commit cf99eb0

File tree

3 files changed

+58
-25
lines changed

3 files changed

+58
-25
lines changed

include/loraprs_service.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,14 @@ class Service : virtual public Kiss::Processor
5252

5353
void onRigTaskRxPacket();
5454
void onRigTaskTxPacket();
55+
void onRigTaskStartRx();
56+
void onRigTaskStartTx();
5557
static void rigTask(void *self);
5658
static ICACHE_RAM_ATTR void onRigIsrRxPacket();
5759

60+
void startRx();
61+
static bool startRxTimer(void *param);
62+
5863
void onAprsisDataAvailable();
5964

6065
void sendSignalReportEvent(int rssi, float snr);
@@ -150,7 +155,9 @@ class Service : virtual public Kiss::Processor
150155
// radio task commands
151156
enum RadioTaskBits {
152157
Receive = 0x01,
153-
Transmit = 0x02
158+
Transmit = 0x02,
159+
StartReceive = 0x04,
160+
StartTransmit = 0x10
154161
};
155162

156163
private:
@@ -169,6 +176,7 @@ class Service : virtual public Kiss::Processor
169176

170177
// peripherals, radio
171178
static TaskHandle_t rigTaskHandle_;
179+
Timer<1> startRxTimer_;
172180
static volatile bool rigIsRxActive_;
173181
static volatile bool rigIsRxIsrEnabled_;
174182
bool rigIsImplicitMode_;

src/loraprs_service.cpp

Lines changed: 48 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -367,6 +367,7 @@ void Service::loop()
367367
if (config_.TlmEnable) {
368368
telemetryTimer_.tick();
369369
}
370+
startRxTimer_.tick();
370371
}
371372

372373
ICACHE_RAM_ATTR void Service::onRigIsrRxPacket() {
@@ -388,9 +389,30 @@ void Service::rigTask(void *self) {
388389
else if (commandBits & RadioTaskBits::Transmit) {
389390
static_cast<Service*>(self)->onRigTaskTxPacket();
390391
}
392+
if (commandBits & RadioTaskBits::StartReceive) {
393+
static_cast<Service*>(self)->onRigTaskStartRx();
394+
}
395+
else if (commandBits & RadioTaskBits::StartTransmit) {
396+
static_cast<Service*>(self)->onRigTaskStartTx();
397+
}
391398
}
392399
}
393400

401+
void Service::onRigTaskStartRx() {
402+
LOG_TRACE("onRigTaskStartRx");
403+
if (config_.PttEnable) {
404+
digitalWrite(config_.PttPin, LOW);
405+
}
406+
if (isHalfDuplex()) {
407+
setFreq(config_.LoraFreqRx);
408+
}
409+
int state = rig_->startReceive();
410+
if (state != RADIOLIB_ERR_NONE) {
411+
LOG_ERROR("Start receive error: ", state);
412+
}
413+
rigIsRxIsrEnabled_ = true;
414+
}
415+
394416
void Service::onRigTaskRxPacket() {
395417
int packetSize = rig_->getPacketLength();
396418
LOG_TRACE("onRigTaskRxPacket", packetSize);
@@ -411,15 +433,21 @@ void Service::onRigTaskRxPacket() {
411433
rigIsRxActive_ = false;
412434
}
413435

414-
void Service::onRigTaskTxPacket() {
415-
rigIsRxIsrEnabled_ = false;
416-
if (isHalfDuplex()) {
417-
setFreq(config_.LoraFreqTx);
418-
}
419-
if (config_.PttEnable) {
420-
digitalWrite(config_.PttPin, HIGH);
436+
void Service::onRigTaskStartTx() {
437+
LOG_TRACE("onRigTaskStartTx");
438+
if (rigIsRxIsrEnabled_) {
439+
rigIsRxIsrEnabled_ = false;
440+
if (isHalfDuplex()) {
441+
setFreq(config_.LoraFreqTx);
442+
}
443+
if (config_.PttEnable) {
444+
digitalWrite(config_.PttPin, HIGH);
445+
}
421446
delay(config_.PttTxDelayMs);
422447
}
448+
}
449+
450+
void Service::onRigTaskTxPacket() {
423451
while (rigTxQueueIndex_.size() > 0) {
424452
int txPacketSize = rigTxQueueIndex_.shift();
425453
LOG_TRACE("onRigTaskTxPacket", txPacketSize);
@@ -435,18 +463,7 @@ void Service::onRigTaskTxPacket() {
435463
}
436464
vTaskDelay(1);
437465
}
438-
if (config_.PttEnable) {
439-
delay(config_.PttTxTailMs);
440-
digitalWrite(config_.PttPin, LOW);
441-
}
442-
if (isHalfDuplex()) {
443-
setFreq(config_.LoraFreqRx);
444-
}
445-
int state = rig_->startReceive();
446-
if (state != RADIOLIB_ERR_NONE) {
447-
LOG_ERROR("Start receive error: ", state);
448-
}
449-
rigIsRxIsrEnabled_ = true;
466+
startRxTimer_.in(config_.PttTxTailMs, &startRxTimer);
450467
}
451468

452469
void Service::sendPeriodicBeacon()
@@ -528,6 +545,16 @@ bool Service::sendModemTelemetryTimer(void *param)
528545
return true;
529546
}
530547

548+
bool Service::startRxTimer(void *param)
549+
{
550+
static_cast<Service*>(param)->startRx();
551+
return true;
552+
}
553+
554+
void Service::startRx() {
555+
xTaskNotify(rigTaskHandle_, RadioTaskBits::StartReceive, eSetBits);
556+
}
557+
531558
void Service::sendModemTelemetry()
532559
{
533560
float batVoltage = 2 * analogRead(config_.TlmBatMonPin) * (3.3 / 4096.0) + config_.TlmBatMonCal;
@@ -591,10 +618,6 @@ void Service::performFrequencyCorrection() {
591618

592619
void Service::setFreq(long loraFreq) const {
593620
rig_->setFrequency((float)loraFreq / 1e6);
594-
int state = rig_->startReceive();
595-
if (state != RADIOLIB_ERR_NONE) {
596-
LOG_ERROR("Start receive error:", state);
597-
}
598621
}
599622

600623
void Service::processIncomingRawPacketAsServer(const byte *packet, int packetLength) {
@@ -650,6 +673,8 @@ void Service::processIncomingRawPacketAsServer(const byte *packet, int packetLen
650673
bool Service::onRigTxBegin()
651674
{
652675
LOG_TRACE("onRigTxBegin");
676+
startRxTimer_.cancel();
677+
xTaskNotify(rigTaskHandle_, RadioTaskBits::StartTransmit, eSetBits);
653678
rigCurrentTxPacketSize_ = 0;
654679
return true;
655680
}

src/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
#include "loraprs_service.h"
1919

20-
const int CfgPollDelayMs = 20; // main loop delay
20+
const int CfgPollDelayMs = 10; // main loop delay
2121

2222
/*
2323
* Initialize config from config.h options.

0 commit comments

Comments
 (0)