kopia lustrzana https://github.com/sh123/esp32_loraprs
Add configurable support for reading in ISR vs reading on separate task
rodzic
af096a3548
commit
7b90dcc589
5
config.h
5
config.h
|
@ -24,10 +24,9 @@
|
||||||
#define CFG_LORA_PIN_A LORA_IRQ // (sx127x - dio0, sx126x/sx128x - dio1)
|
#define CFG_LORA_PIN_A LORA_IRQ // (sx127x - dio0, sx126x/sx128x - dio1)
|
||||||
#ifdef USE_RADIOLIB
|
#ifdef USE_RADIOLIB
|
||||||
#define CFG_LORA_PIN_B RADIOLIB_NC // (sx127x - dio1, sx126x/sx128x - busy)
|
#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
|
#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
|
#define CFG_LORA_USE_CAD false // set to true to utilize carrier detection
|
||||||
|
|
||||||
// lora protocol parameters
|
// lora protocol parameters
|
||||||
|
|
|
@ -6,6 +6,7 @@ byte Service::rxBuf_[CfgMaxPacketSize];
|
||||||
|
|
||||||
#ifdef USE_RADIOLIB
|
#ifdef USE_RADIOLIB
|
||||||
#pragma message("Using RadioLib")
|
#pragma message("Using RadioLib")
|
||||||
|
volatile bool Service::loraDataAvailable_ = false;
|
||||||
bool Service::interruptEnabled_ = true;
|
bool Service::interruptEnabled_ = true;
|
||||||
std::shared_ptr<MODULE_NAME> Service::radio_;
|
std::shared_ptr<MODULE_NAME> Service::radio_;
|
||||||
#else
|
#else
|
||||||
|
@ -24,6 +25,7 @@ Service::Service()
|
||||||
{
|
{
|
||||||
#ifdef USE_RADIOLIB
|
#ifdef USE_RADIOLIB
|
||||||
interruptEnabled_ = true;
|
interruptEnabled_ = true;
|
||||||
|
loraDataAvailable_ = false;
|
||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -66,6 +68,13 @@ void Service::setup(const Config &conf)
|
||||||
setupLora(config_.LoraFreq, config_.LoraBw, config_.LoraSf,
|
setupLora(config_.LoraFreq, config_.LoraBw, config_.LoraSf,
|
||||||
config_.LoraCodingRate, config_.LoraPower, config_.LoraSync, config_.LoraEnableCrc);
|
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
|
// peripherls, WiFi
|
||||||
if (needsWifi()) {
|
if (needsWifi()) {
|
||||||
setupWifi(config_.WifiSsid, config_.WifiKey);
|
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)
|
#if (MODULE_NAME == SX1268)
|
||||||
radio_->setRfSwitchPins(4, 5);
|
radio_->setRfSwitchPins(4, 5);
|
||||||
radio_->clearDio1Action();
|
radio_->clearDio1Action();
|
||||||
|
if (config_.LoraUseIsr) {
|
||||||
radio_->setDio1Action(onLoraDataAvailableIsr);
|
radio_->setDio1Action(onLoraDataAvailableIsr);
|
||||||
|
} else {
|
||||||
|
radio_->setDio1Action(onLoraDataAvailableIsrNoRead);
|
||||||
|
}
|
||||||
#else
|
#else
|
||||||
radio_->clearDio0Action();
|
radio_->clearDio0Action();
|
||||||
|
if (config_.LoraUseIsr) {
|
||||||
radio_->setDio0Action(onLoraDataAvailableIsr);
|
radio_->setDio0Action(onLoraDataAvailableIsr);
|
||||||
|
} else {
|
||||||
|
radio_->setDio0Action(onLoraDataAvailableIsrNoRead);
|
||||||
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
state = radio_->startReceive();
|
state = radio_->startReceive();
|
||||||
|
@ -334,6 +351,12 @@ bool Service::isLoraRxBusy() {
|
||||||
|
|
||||||
#ifdef USE_RADIOLIB
|
#ifdef USE_RADIOLIB
|
||||||
|
|
||||||
|
ICACHE_RAM_ATTR void Service::onLoraDataAvailableIsrNoRead() {
|
||||||
|
if (interruptEnabled_) {
|
||||||
|
loraDataAvailable_ = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
ICACHE_RAM_ATTR void Service::onLoraDataAvailableIsr() {
|
ICACHE_RAM_ATTR void Service::onLoraDataAvailableIsr() {
|
||||||
if (interruptEnabled_) {
|
if (interruptEnabled_) {
|
||||||
int packetSize = radio_->getPacketLength();
|
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
|
#else // USE_RADIOLIB
|
||||||
|
|
||||||
ICACHE_RAM_ATTR void Service::onLoraDataAvailableIsr(int packetSize)
|
ICACHE_RAM_ATTR void Service::onLoraDataAvailableIsr(int packetSize)
|
||||||
|
|
|
@ -9,7 +9,6 @@
|
||||||
// When using RadioLib, default module is SX1278, if you are using
|
// When using RadioLib, default module is SX1278, if you are using
|
||||||
// different module then update MODULE_NAME in module_name.h
|
// different module then update MODULE_NAME in module_name.h
|
||||||
#define USE_RADIOLIB
|
#define USE_RADIOLIB
|
||||||
#define HW_DJAPRS 2
|
|
||||||
#ifdef USE_RADIOLIB
|
#ifdef USE_RADIOLIB
|
||||||
#include <RadioLib.h>
|
#include <RadioLib.h>
|
||||||
#include "module_name.h"
|
#include "module_name.h"
|
||||||
|
@ -50,7 +49,9 @@ private:
|
||||||
bool isLoraRxBusy();
|
bool isLoraRxBusy();
|
||||||
#ifdef USE_RADIOLIB
|
#ifdef USE_RADIOLIB
|
||||||
void onLoraDataAvailable();
|
void onLoraDataAvailable();
|
||||||
|
static void processIncomingDataTask(void *param);
|
||||||
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr();
|
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr();
|
||||||
|
static ICACHE_RAM_ATTR void onLoraDataAvailableIsrNoRead();
|
||||||
#else
|
#else
|
||||||
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr(int packetSize);
|
static ICACHE_RAM_ATTR void onLoraDataAvailableIsr(int packetSize);
|
||||||
void loraReceive(int packetSize);
|
void loraReceive(int packetSize);
|
||||||
|
@ -117,7 +118,7 @@ private:
|
||||||
|
|
||||||
// processor config
|
// processor config
|
||||||
const int CfgConnRetryMs = 500; // connection retry delay, e.g. wifi
|
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
|
const int CfgConnRetryMaxTimes = 10; // number of connection retries
|
||||||
static const int CfgMaxPacketSize = 256; // maximum packet size
|
static const int CfgMaxPacketSize = 256; // maximum packet size
|
||||||
|
|
||||||
|
@ -145,6 +146,7 @@ private:
|
||||||
// peripherals
|
// peripherals
|
||||||
static byte rxBuf_[CfgMaxPacketSize];
|
static byte rxBuf_[CfgMaxPacketSize];
|
||||||
#ifdef USE_RADIOLIB
|
#ifdef USE_RADIOLIB
|
||||||
|
static volatile bool loraDataAvailable_;
|
||||||
static bool interruptEnabled_;
|
static bool interruptEnabled_;
|
||||||
CircularBuffer<uint8_t, CfgMaxPacketSize> txQueue_;
|
CircularBuffer<uint8_t, CfgMaxPacketSize> txQueue_;
|
||||||
static std::shared_ptr<MODULE_NAME> radio_;
|
static std::shared_ptr<MODULE_NAME> radio_;
|
||||||
|
|
Ładowanie…
Reference in New Issue