2020-04-29 23:28:11 +00:00
|
|
|
#include "SX1262Interface.h"
|
|
|
|
#include <configuration.h>
|
|
|
|
|
|
|
|
SX1262Interface::SX1262Interface(RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst, RADIOLIB_PIN_TYPE busy,
|
|
|
|
SPIClass &spi)
|
|
|
|
: RadioLibInterface(cs, irq, rst, busy, spi, &lora), lora(&module)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Initialise the Driver transport hardware and software.
|
|
|
|
/// Make sure the Driver is properly configured before calling init().
|
|
|
|
/// \return true if initialisation succeeded.
|
|
|
|
bool SX1262Interface::init()
|
|
|
|
{
|
2020-05-01 19:11:04 +00:00
|
|
|
RadioLibInterface::init();
|
|
|
|
|
2020-06-15 21:38:09 +00:00
|
|
|
#ifdef SX1262_RXEN // set not rx or tx mode
|
|
|
|
pinMode(SX1262_RXEN, OUTPUT);
|
2020-06-16 13:26:21 +00:00
|
|
|
#endif
|
|
|
|
#ifdef SX1262_TXEN
|
2020-06-15 21:38:09 +00:00
|
|
|
pinMode(SX1262_TXEN, OUTPUT);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef SX1262_E22
|
|
|
|
float tcxoVoltage = 0; // None - we use an XTAL
|
|
|
|
#else
|
|
|
|
float tcxoVoltage =
|
2020-06-16 19:02:13 +00:00
|
|
|
1.8; // E22 uses DIO3 to power tcxo per https://github.com/jgromes/RadioLib/issues/12#issuecomment-520695575
|
2020-06-15 21:38:09 +00:00
|
|
|
#endif
|
2020-04-29 23:28:11 +00:00
|
|
|
bool useRegulatorLDO = false; // Seems to depend on the connection to pin 9/DCC_SW - if an inductor DCDC?
|
|
|
|
|
2020-04-30 01:46:32 +00:00
|
|
|
applyModemConfig();
|
2020-04-30 02:04:59 +00:00
|
|
|
if (power > 22) // This chip has lower power limits than some
|
|
|
|
power = 22;
|
2020-04-29 23:28:11 +00:00
|
|
|
int res = lora.begin(freq, bw, sf, cr, syncWord, power, currentLimit, preambleLength, tcxoVoltage, useRegulatorLDO);
|
|
|
|
DEBUG_MSG("LORA init result %d\n", res);
|
|
|
|
|
2020-06-16 13:38:08 +00:00
|
|
|
#ifdef SX1262_TXEN
|
2020-06-16 13:26:21 +00:00
|
|
|
// lora.begin sets Dio2 as RF switch control, which is not true if we are manually controlling RX and TX
|
2020-06-15 21:38:09 +00:00
|
|
|
if (res == ERR_NONE)
|
|
|
|
res = lora.setDio2AsRfSwitch(false);
|
|
|
|
#endif
|
|
|
|
|
2020-04-30 17:00:40 +00:00
|
|
|
if (res == ERR_NONE)
|
2020-04-30 16:44:16 +00:00
|
|
|
res = lora.setCRC(SX126X_LORA_CRC_ON);
|
|
|
|
|
2020-04-30 19:37:58 +00:00
|
|
|
if (res == ERR_NONE)
|
|
|
|
startReceive(); // start receiving
|
|
|
|
|
2020-04-30 20:50:40 +00:00
|
|
|
return res == ERR_NONE;
|
2020-04-29 23:28:11 +00:00
|
|
|
}
|
2020-04-30 01:46:32 +00:00
|
|
|
|
|
|
|
bool SX1262Interface::reconfigure()
|
|
|
|
{
|
|
|
|
applyModemConfig();
|
|
|
|
|
|
|
|
// set mode to standby
|
2020-04-30 20:50:40 +00:00
|
|
|
setStandby();
|
2020-04-30 01:46:32 +00:00
|
|
|
|
|
|
|
// configure publicly accessible settings
|
2020-04-30 20:50:40 +00:00
|
|
|
int err = lora.setSpreadingFactor(sf);
|
2020-04-30 01:46:32 +00:00
|
|
|
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-04-30 02:04:59 +00:00
|
|
|
if (power > 22) // This chip has lower power limits than some
|
|
|
|
power = 22;
|
2020-04-30 01:46:32 +00:00
|
|
|
err = lora.setOutputPower(power);
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
2020-04-30 19:37:58 +00:00
|
|
|
startReceive(); // restart receiving
|
|
|
|
|
|
|
|
return ERR_NONE;
|
|
|
|
}
|
|
|
|
|
2020-05-27 22:47:59 +00:00
|
|
|
void INTERRUPT_ATTR SX1262Interface::disableInterrupt()
|
|
|
|
{
|
|
|
|
lora.clearDio1Action();
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:50:40 +00:00
|
|
|
void SX1262Interface::setStandby()
|
|
|
|
{
|
|
|
|
int err = lora.standby();
|
|
|
|
assert(err == ERR_NONE);
|
|
|
|
|
2020-06-15 21:38:09 +00:00
|
|
|
#ifdef SX1262_RXEN // we have RXEN/TXEN control - turn off RX and TX power
|
|
|
|
digitalWrite(SX1262_RXEN, LOW);
|
2020-06-16 19:02:13 +00:00
|
|
|
#endif
|
2020-06-16 13:26:21 +00:00
|
|
|
#ifdef SX1262_TXEN
|
2020-06-15 21:38:09 +00:00
|
|
|
digitalWrite(SX1262_TXEN, LOW);
|
|
|
|
#endif
|
|
|
|
|
2020-04-30 20:50:40 +00:00
|
|
|
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-04-30 20:50:40 +00:00
|
|
|
}
|
|
|
|
|
2020-05-01 02:58:10 +00:00
|
|
|
/**
|
|
|
|
* Add SNR data to received messages
|
|
|
|
*/
|
2020-05-01 05:53:21 +00:00
|
|
|
void SX1262Interface::addReceiveMetadata(MeshPacket *mp)
|
|
|
|
{
|
2020-05-01 02:58:10 +00:00
|
|
|
mp->rx_snr = lora.getSNR();
|
|
|
|
}
|
|
|
|
|
2020-06-15 21:38:09 +00:00
|
|
|
/** start an immediate transmit
|
|
|
|
* We override to turn on transmitter power as needed.
|
|
|
|
*/
|
2020-06-16 13:26:21 +00:00
|
|
|
void SX1262Interface::configHardwareForSend()
|
2020-06-15 21:38:09 +00:00
|
|
|
{
|
2020-06-16 13:26:21 +00:00
|
|
|
#ifdef SX1262_TXEN // we have RXEN/TXEN control - turn on TX power / off RX power
|
2020-06-15 21:38:09 +00:00
|
|
|
digitalWrite(SX1262_TXEN, HIGH);
|
|
|
|
#endif
|
|
|
|
|
2020-06-16 13:26:21 +00:00
|
|
|
RadioLibInterface::configHardwareForSend();
|
2020-06-15 21:38:09 +00:00
|
|
|
}
|
|
|
|
|
2020-05-25 02:23:50 +00:00
|
|
|
// For power draw measurements, helpful to force radio to stay sleeping
|
|
|
|
// #define SLEEP_ONLY
|
|
|
|
|
2020-04-30 19:37:58 +00:00
|
|
|
void SX1262Interface::startReceive()
|
|
|
|
{
|
2020-05-25 02:23:50 +00:00
|
|
|
#ifdef SLEEP_ONLY
|
|
|
|
sleep();
|
|
|
|
#else
|
2020-06-15 21:38:09 +00:00
|
|
|
|
2020-06-16 13:26:21 +00:00
|
|
|
setStandby();
|
|
|
|
|
2020-06-15 21:38:09 +00:00
|
|
|
#ifdef SX1262_RXEN // we have RXEN/TXEN control - turn on RX power / off TX power
|
|
|
|
digitalWrite(SX1262_RXEN, HIGH);
|
|
|
|
#endif
|
|
|
|
|
2020-05-25 02:23:50 +00:00
|
|
|
// int err = lora.startReceive();
|
|
|
|
int err = lora.startReceiveDutyCycleAuto(); // We use a 32 bit preamble so this should save some power by letting radio sit in
|
|
|
|
// standby mostly.
|
2020-04-30 19:37:58 +00:00
|
|
|
assert(err == ERR_NONE);
|
2020-04-30 01:46:32 +00:00
|
|
|
|
2020-04-30 20:50:40 +00:00
|
|
|
isReceiving = true;
|
|
|
|
|
2020-04-30 19:37:58 +00:00
|
|
|
// Must be done AFTER, starting transmit, because startTransmit clears (possibly stale) interrupt pending register bits
|
|
|
|
enableInterrupt(isrRxLevel0);
|
2020-05-25 02:23:50 +00:00
|
|
|
#endif
|
2020-04-30 01:46:32 +00:00
|
|
|
}
|
2020-04-30 16:44:16 +00:00
|
|
|
|
|
|
|
/** Could we send right now (i.e. either not actively receving or transmitting)? */
|
2020-05-01 15:32:16 +00:00
|
|
|
bool SX1262Interface::isActivelyReceiving()
|
2020-04-30 16:44:16 +00:00
|
|
|
{
|
2020-05-25 14:48:36 +00:00
|
|
|
// return false; // FIXME
|
|
|
|
// FIXME this is not correct? - often always true - need to add an extra conditional
|
|
|
|
return lora.getPacketLength() > 0;
|
2020-04-30 22:50:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool SX1262Interface::sleep()
|
|
|
|
{
|
2020-05-01 00:43:29 +00:00
|
|
|
// put chipset into sleep mode
|
|
|
|
disableInterrupt();
|
|
|
|
lora.sleep();
|
2020-04-30 22:50:07 +00:00
|
|
|
|
2020-05-01 00:43:29 +00:00
|
|
|
return true;
|
2020-04-30 16:44:16 +00:00
|
|
|
}
|