Add configurable support for reading in ISR vs reading on separate task

pull/42/head
sh123 2021-11-12 20:39:39 +02:00
rodzic af096a3548
commit 7b90dcc589
3 zmienionych plików z 58 dodań i 7 usunięć

Wyświetl plik

@ -24,10 +24,9 @@
#define CFG_LORA_PIN_A LORA_IRQ // (sx127x - dio0, sx126x/sx128x - dio1)
#ifdef USE_RADIOLIB
#define CFG_LORA_PIN_B RADIOLIB_NC // (sx127x - dio1, sx126x/sx128x - busy)
#define CFG_LORA_USE_ISR true // always ON for RadioLib
#else
#define CFG_LORA_USE_ISR false // set to true for ISR usage in arduino-LoRa
#endif
#define CFG_LORA_USE_ISR true // true - read incoming data in ISR, false - do not read in ISR
#define CFG_LORA_USE_CAD false // set to true to utilize carrier detection
// lora protocol parameters

Wyświetl plik

@ -6,6 +6,7 @@ byte Service::rxBuf_[CfgMaxPacketSize];
#ifdef USE_RADIOLIB
#pragma message("Using RadioLib")
volatile bool Service::loraDataAvailable_ = false;
bool Service::interruptEnabled_ = true;
std::shared_ptr<MODULE_NAME> Service::radio_;
#else
@ -24,6 +25,7 @@ Service::Service()
{
#ifdef USE_RADIOLIB
interruptEnabled_ = true;
loraDataAvailable_ = false;
#endif
}
@ -66,6 +68,13 @@ void Service::setup(const Config &conf)
setupLora(config_.LoraFreq, config_.LoraBw, config_.LoraSf,
config_.LoraCodingRate, config_.LoraPower, config_.LoraSync, config_.LoraEnableCrc);
#ifdef USE_RADIOLIB
if (!config_.LoraUseIsr) {
LOG_INFO("Reading data on separate task");
xTaskCreate(processIncomingDataTask, "processIncomingDataTask", 10000, NULL, 1, NULL);
}
#endif
// peripherls, WiFi
if (needsWifi()) {
setupWifi(config_.WifiSsid, config_.WifiKey);
@ -206,10 +215,18 @@ void Service::setupLora(long loraFreq, long bw, int sf, int cr, int pwr, int syn
#if (MODULE_NAME == SX1268)
radio_->setRfSwitchPins(4, 5);
radio_->clearDio1Action();
radio_->setDio1Action(onLoraDataAvailableIsr);
if (config_.LoraUseIsr) {
radio_->setDio1Action(onLoraDataAvailableIsr);
} else {
radio_->setDio1Action(onLoraDataAvailableIsrNoRead);
}
#else
radio_->clearDio0Action();
radio_->setDio0Action(onLoraDataAvailableIsr);
if (config_.LoraUseIsr) {
radio_->setDio0Action(onLoraDataAvailableIsr);
} else {
radio_->setDio0Action(onLoraDataAvailableIsrNoRead);
}
#endif
state = radio_->startReceive();
@ -334,6 +351,12 @@ bool Service::isLoraRxBusy() {
#ifdef USE_RADIOLIB
ICACHE_RAM_ATTR void Service::onLoraDataAvailableIsrNoRead() {
if (interruptEnabled_) {
loraDataAvailable_ = true;
}
}
ICACHE_RAM_ATTR void Service::onLoraDataAvailableIsr() {
if (interruptEnabled_) {
int packetSize = radio_->getPacketLength();
@ -355,6 +378,33 @@ ICACHE_RAM_ATTR void Service::onLoraDataAvailableIsr() {
}
}
void Service::processIncomingDataTask(void *param) {
LOG_INFO("Incoming data process task started");
while (true) {
if (loraDataAvailable_) {
int packetSize = radio_->getPacketLength();
if (packetSize > 0) {
int state = radio_->readData(rxBuf_, packetSize);
if (state == ERR_NONE) {
queueRigToSerialIsr(Cmd::Data, rxBuf_, packetSize);
} else {
LOG_ERROR("Read data error: ", state);
}
state = radio_->startReceive();
if (state != ERR_NONE) {
LOG_ERROR("Start receive error: ", state);
}
}
loraDataAvailable_ = false;
}
delay(CfgPollDelayMs);
}
}
#else // USE_RADIOLIB
ICACHE_RAM_ATTR void Service::onLoraDataAvailableIsr(int packetSize)

Wyświetl plik

@ -9,7 +9,6 @@
// When using RadioLib, default module is SX1278, if you are using
// different module then update MODULE_NAME in module_name.h
#define USE_RADIOLIB
#define HW_DJAPRS 2
#ifdef USE_RADIOLIB
#include <RadioLib.h>
#include "module_name.h"
@ -50,7 +49,9 @@ private:
bool isLoraRxBusy();
#ifdef USE_RADIOLIB
void onLoraDataAvailable();
static void processIncomingDataTask(void *param);
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr();
static ICACHE_RAM_ATTR void onLoraDataAvailableIsrNoRead();
#else
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr(int packetSize);
void loraReceive(int packetSize);
@ -117,7 +118,7 @@ private:
// processor config
const int CfgConnRetryMs = 500; // connection retry delay, e.g. wifi
const int CfgPollDelayMs = 5; // main loop delay
static const int CfgPollDelayMs = 5; // main loop delay
const int CfgConnRetryMaxTimes = 10; // number of connection retries
static const int CfgMaxPacketSize = 256; // maximum packet size
@ -145,6 +146,7 @@ private:
// peripherals
static byte rxBuf_[CfgMaxPacketSize];
#ifdef USE_RADIOLIB
static volatile bool loraDataAvailable_;
static bool interruptEnabled_;
CircularBuffer<uint8_t, CfgMaxPacketSize> txQueue_;
static std::shared_ptr<MODULE_NAME> radio_;