2020-05-01 00:43:29 +00:00
|
|
|
#include "RF95Interface.h"
|
|
|
|
#include "MeshRadio.h" // kinda yucky, but we need to know which region we are in
|
2020-05-01 04:11:03 +00:00
|
|
|
#include "RadioLibRF95.h"
|
2020-05-01 00:43:29 +00:00
|
|
|
#include <configuration.h>
|
|
|
|
|
2020-05-03 02:52:37 +00:00
|
|
|
#define MAX_POWER 17
|
|
|
|
// if we use 20 we are limited to 1% duty cycle or hw might overheat. For continuous operation set a limit of 17
|
|
|
|
|
2020-05-01 00:43:29 +00:00
|
|
|
RF95Interface::RF95Interface(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, SPIClass &spi)
|
|
|
|
: RadioLibInterface(cs, irq, rst, 0, spi)
|
|
|
|
{
|
|
|
|
// FIXME - we assume devices never get destroyed
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialise the Driver transport hardware and software.
|
|
|
|
/// Make sure the Driver is properly configured before calling init().
|
|
|
|
/// \return true if initialisation succeeded.
|
|
|
|
bool RF95Interface::init()
|
|
|
|
{
|
2020-05-01 19:11:04 +00:00
|
|
|
RadioLibInterface::init();
|
2020-05-03 02:52:37 +00:00
|
|
|
|
2020-05-01 00:43:29 +00:00
|
|
|
applyModemConfig();
|
2020-05-03 02:52:37 +00:00
|
|
|
if (power > MAX_POWER) // This chip has lower power limits than some
|
|
|
|
power = MAX_POWER;
|
2020-05-01 00:43:29 +00:00
|
|
|
|
2020-05-01 04:29:51 +00:00
|
|
|
iface = lora = new RadioLibRF95(&module);
|
2020-07-10 21:37:01 +00:00
|
|
|
|
|
|
|
#ifdef RF95_TCXO
|
|
|
|
pinMode(RF95_TCXO, OUTPUT);
|
|
|
|
digitalWrite(RF95_TCXO, 1);
|
|
|
|
#endif
|
|
|
|
|
2020-05-01 04:29:51 +00:00
|
|
|
int res = lora->begin(freq, bw, sf, cr, syncWord, power, currentLimit, preambleLength);
|
2020-07-10 21:37:01 +00:00
|
|
|
DEBUG_MSG("RF95 init result %d\n", res);
|
2020-05-01 00:43:29 +00:00
|
|
|
|
|
|
|
if (res == ERR_NONE)
|
|
|
|
res = lora->setCRC(SX126X_LORA_CRC_ON);
|
|
|
|
|
2020-05-03 02:52:37 +00:00
|
|
|
if (res == ERR_NONE)
|
2020-05-01 00:43:29 +00:00
|
|
|
startReceive(); // start receiving
|
|
|
|
|
|
|
|
return res == ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2020-05-01 00:56:30 +00:00
|
|
|
void INTERRUPT_ATTR RF95Interface::disableInterrupt()
|
|
|
|
{
|
|
|
|
lora->clearDio0Action();
|
|
|
|
}
|
|
|
|
|
2020-05-01 00:43:29 +00:00
|
|
|
bool RF95Interface::reconfigure()
|
|
|
|
{
|
|
|
|
applyModemConfig();
|
|
|
|
|
|
|
|
// set mode to standby
|
|
|
|
setStandby();
|
|
|
|
|
|
|
|
// configure publicly accessible settings
|
|
|
|
int err = lora->setSpreadingFactor(sf);
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
|
|
|
err = lora->setBandwidth(bw);
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
|
|
|
err = lora->setCodingRate(cr);
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
|
|
|
err = lora->setSyncWord(syncWord);
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
|
|
|
err = lora->setCurrentLimit(currentLimit);
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
|
|
|
err = lora->setPreambleLength(preambleLength);
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
|
|
|
err = lora->setFrequency(freq);
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
2020-05-03 02:52:37 +00:00
|
|
|
if (power > MAX_POWER) // This chip has lower power limits than some
|
|
|
|
power = MAX_POWER;
|
2020-05-01 00:43:29 +00:00
|
|
|
err = lora->setOutputPower(power);
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
|
|
|
startReceive(); // restart receiving
|
|
|
|
|
|
|
|
return ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2020-05-01 02:58:10 +00:00
|
|
|
/**
|
|
|
|
* Add SNR data to received messages
|
|
|
|
*/
|
|
|
|
void RF95Interface::addReceiveMetadata(MeshPacket *mp)
|
|
|
|
{
|
|
|
|
mp->rx_snr = lora->getSNR();
|
|
|
|
}
|
|
|
|
|
2020-05-01 00:43:29 +00:00
|
|
|
void RF95Interface::setStandby()
|
|
|
|
{
|
|
|
|
int err = lora->standby();
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
|
|
|
isReceiving = false; // If we were receiving, not any more
|
|
|
|
disableInterrupt();
|
2020-05-01 05:53:21 +00:00
|
|
|
completeSending(); // If we were sending, not anymore
|
2020-05-01 00:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RF95Interface::startReceive()
|
|
|
|
{
|
|
|
|
setStandby();
|
|
|
|
int err = lora->startReceive();
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
|
|
|
isReceiving = true;
|
|
|
|
|
|
|
|
// Must be done AFTER, starting transmit, because startTransmit clears (possibly stale) interrupt pending register bits
|
|
|
|
enableInterrupt(isrRxLevel0);
|
|
|
|
}
|
|
|
|
|
|
|
|
/** Could we send right now (i.e. either not actively receving or transmitting)? */
|
2020-05-01 15:32:16 +00:00
|
|
|
bool RF95Interface::isActivelyReceiving()
|
2020-05-01 00:43:29 +00:00
|
|
|
{
|
2020-05-01 15:32:16 +00:00
|
|
|
return lora->isReceiving();
|
2020-05-01 00:43:29 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool RF95Interface::sleep()
|
|
|
|
{
|
|
|
|
// put chipset into sleep mode
|
|
|
|
disableInterrupt();
|
|
|
|
lora->sleep();
|
|
|
|
|
|
|
|
return true;
|
2020-05-03 02:52:37 +00:00
|
|
|
}
|