diff --git a/include/loraprs_service.h b/include/loraprs_service.h index 259040e..53b7033 100644 --- a/include/loraprs_service.h +++ b/include/loraprs_service.h @@ -52,9 +52,14 @@ private: void onRigTaskRxPacket(); void onRigTaskTxPacket(); + void onRigTaskStartRx(); + void onRigTaskStartTx(); static void rigTask(void *self); static ICACHE_RAM_ATTR void onRigIsrRxPacket(); + void startRx(); + static bool startRxTimer(void *param); + void onAprsisDataAvailable(); void sendSignalReportEvent(int rssi, float snr); @@ -150,7 +155,9 @@ private: // radio task commands enum RadioTaskBits { Receive = 0x01, - Transmit = 0x02 + Transmit = 0x02, + StartReceive = 0x04, + StartTransmit = 0x10 }; private: @@ -169,6 +176,7 @@ private: // peripherals, radio static TaskHandle_t rigTaskHandle_; + Timer<1> startRxTimer_; static volatile bool rigIsRxActive_; static volatile bool rigIsRxIsrEnabled_; bool rigIsImplicitMode_; diff --git a/src/loraprs_service.cpp b/src/loraprs_service.cpp index ac5ca68..aaa1bba 100644 --- a/src/loraprs_service.cpp +++ b/src/loraprs_service.cpp @@ -367,6 +367,7 @@ void Service::loop() if (config_.TlmEnable) { telemetryTimer_.tick(); } + startRxTimer_.tick(); } ICACHE_RAM_ATTR void Service::onRigIsrRxPacket() { @@ -388,9 +389,30 @@ void Service::rigTask(void *self) { else if (commandBits & RadioTaskBits::Transmit) { static_cast(self)->onRigTaskTxPacket(); } + if (commandBits & RadioTaskBits::StartReceive) { + static_cast(self)->onRigTaskStartRx(); + } + else if (commandBits & RadioTaskBits::StartTransmit) { + static_cast(self)->onRigTaskStartTx(); + } } } +void Service::onRigTaskStartRx() { + LOG_TRACE("onRigTaskStartRx"); + if (config_.PttEnable) { + digitalWrite(config_.PttPin, LOW); + } + if (isHalfDuplex()) { + setFreq(config_.LoraFreqRx); + } + int state = rig_->startReceive(); + if (state != RADIOLIB_ERR_NONE) { + LOG_ERROR("Start receive error: ", state); + } + rigIsRxIsrEnabled_ = true; +} + void Service::onRigTaskRxPacket() { int packetSize = rig_->getPacketLength(); LOG_TRACE("onRigTaskRxPacket", packetSize); @@ -411,15 +433,21 @@ void Service::onRigTaskRxPacket() { rigIsRxActive_ = false; } -void Service::onRigTaskTxPacket() { - rigIsRxIsrEnabled_ = false; - if (isHalfDuplex()) { - setFreq(config_.LoraFreqTx); - } - if (config_.PttEnable) { - digitalWrite(config_.PttPin, HIGH); +void Service::onRigTaskStartTx() { + LOG_TRACE("onRigTaskStartTx"); + if (rigIsRxIsrEnabled_) { + rigIsRxIsrEnabled_ = false; + if (isHalfDuplex()) { + setFreq(config_.LoraFreqTx); + } + if (config_.PttEnable) { + digitalWrite(config_.PttPin, HIGH); + } delay(config_.PttTxDelayMs); } +} + +void Service::onRigTaskTxPacket() { while (rigTxQueueIndex_.size() > 0) { int txPacketSize = rigTxQueueIndex_.shift(); LOG_TRACE("onRigTaskTxPacket", txPacketSize); @@ -435,18 +463,7 @@ void Service::onRigTaskTxPacket() { } vTaskDelay(1); } - if (config_.PttEnable) { - delay(config_.PttTxTailMs); - digitalWrite(config_.PttPin, LOW); - } - if (isHalfDuplex()) { - setFreq(config_.LoraFreqRx); - } - int state = rig_->startReceive(); - if (state != RADIOLIB_ERR_NONE) { - LOG_ERROR("Start receive error: ", state); - } - rigIsRxIsrEnabled_ = true; + startRxTimer_.in(config_.PttTxTailMs, &startRxTimer); } void Service::sendPeriodicBeacon() @@ -528,6 +545,16 @@ bool Service::sendModemTelemetryTimer(void *param) return true; } +bool Service::startRxTimer(void *param) +{ + static_cast(param)->startRx(); + return true; +} + +void Service::startRx() { + xTaskNotify(rigTaskHandle_, RadioTaskBits::StartReceive, eSetBits); +} + void Service::sendModemTelemetry() { float batVoltage = 2 * analogRead(config_.TlmBatMonPin) * (3.3 / 4096.0) + config_.TlmBatMonCal; @@ -591,10 +618,6 @@ void Service::performFrequencyCorrection() { void Service::setFreq(long loraFreq) const { rig_->setFrequency((float)loraFreq / 1e6); - int state = rig_->startReceive(); - if (state != RADIOLIB_ERR_NONE) { - LOG_ERROR("Start receive error:", state); - } } void Service::processIncomingRawPacketAsServer(const byte *packet, int packetLength) { @@ -650,6 +673,8 @@ void Service::processIncomingRawPacketAsServer(const byte *packet, int packetLen bool Service::onRigTxBegin() { LOG_TRACE("onRigTxBegin"); + startRxTimer_.cancel(); + xTaskNotify(rigTaskHandle_, RadioTaskBits::StartTransmit, eSetBits); rigCurrentTxPacketSize_ = 0; return true; } diff --git a/src/main.cpp b/src/main.cpp index 1cb4df1..8adde30 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,7 +17,7 @@ #include "loraprs_service.h" -const int CfgPollDelayMs = 20; // main loop delay +const int CfgPollDelayMs = 10; // main loop delay /* * Initialize config from config.h options.