mirror of
https://github.com/meshtastic/firmware.git
synced 2025-12-16 07:42:37 +00:00
Hi, I think the problem you were having building for ESP32 was due to a funny thing. Notice the #define for INTERRUPT_ATTR. That macro expands to IRAM_ATTR - which is a special flag the ESP32 requires for _any_ code that is going to be called from an ISR. So that the code is guaranteed to be in RAM (the ESP32 uses a clever scheme where the FLASH is actually high speed serial flash and all reads/writes are actually only happening to a small number of pages in RAM and they have a driver that is constantly copying blocks they need into that ram. This essentially how VM works for desktop computers, but in their case they are paging to FLASH. But for code that runs in an interrupt handler must _always_ be in RAM because if you took a 'page fault' for that code being missing in RAM they can't nicely do their clever VM scheme. So that's all good. The problem was - apparently GCC for the ESP32 has a a bug when that attribute is applied in the class declaration. So I moved it out into the cpp file and all seems well now.
133 lines
3.5 KiB
C++
133 lines
3.5 KiB
C++
#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()
|
|
{
|
|
RadioLibInterface::init();
|
|
|
|
float tcxoVoltage = 0; // None - we use an XTAL
|
|
bool useRegulatorLDO = false; // Seems to depend on the connection to pin 9/DCC_SW - if an inductor DCDC?
|
|
|
|
applyModemConfig();
|
|
if (power > 22) // This chip has lower power limits than some
|
|
power = 22;
|
|
int res = lora.begin(freq, bw, sf, cr, syncWord, power, currentLimit, preambleLength, tcxoVoltage, useRegulatorLDO);
|
|
DEBUG_MSG("LORA init result %d\n", res);
|
|
|
|
if (res == ERR_NONE)
|
|
res = lora.setCRC(SX126X_LORA_CRC_ON);
|
|
|
|
if (res == ERR_NONE)
|
|
startReceive(); // start receiving
|
|
|
|
return res == ERR_NONE;
|
|
}
|
|
|
|
bool SX1262Interface::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);
|
|
|
|
if (power > 22) // This chip has lower power limits than some
|
|
power = 22;
|
|
err = lora.setOutputPower(power);
|
|
assert(err == ERR_NONE);
|
|
|
|
startReceive(); // restart receiving
|
|
|
|
return ERR_NONE;
|
|
}
|
|
|
|
void INTERRUPT_ATTR SX1262Interface::disableInterrupt()
|
|
{
|
|
lora.clearDio1Action();
|
|
}
|
|
|
|
void SX1262Interface::setStandby()
|
|
{
|
|
int err = lora.standby();
|
|
assert(err == ERR_NONE);
|
|
|
|
isReceiving = false; // If we were receiving, not any more
|
|
disableInterrupt();
|
|
completeSending(); // If we were sending, not anymore
|
|
}
|
|
|
|
/**
|
|
* Add SNR data to received messages
|
|
*/
|
|
void SX1262Interface::addReceiveMetadata(MeshPacket *mp)
|
|
{
|
|
mp->rx_snr = lora.getSNR();
|
|
}
|
|
|
|
// For power draw measurements, helpful to force radio to stay sleeping
|
|
// #define SLEEP_ONLY
|
|
|
|
void SX1262Interface::startReceive()
|
|
{
|
|
#ifdef SLEEP_ONLY
|
|
sleep();
|
|
#else
|
|
setStandby();
|
|
// 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.
|
|
assert(err == ERR_NONE);
|
|
|
|
isReceiving = true;
|
|
|
|
// Must be done AFTER, starting transmit, because startTransmit clears (possibly stale) interrupt pending register bits
|
|
enableInterrupt(isrRxLevel0);
|
|
#endif
|
|
}
|
|
|
|
/** Could we send right now (i.e. either not actively receving or transmitting)? */
|
|
bool SX1262Interface::isActivelyReceiving()
|
|
{
|
|
// return false; // FIXME
|
|
// FIXME this is not correct? - often always true - need to add an extra conditional
|
|
return lora.getPacketLength() > 0;
|
|
}
|
|
|
|
bool SX1262Interface::sleep()
|
|
{
|
|
// put chipset into sleep mode
|
|
disableInterrupt();
|
|
lora.sleep();
|
|
|
|
return true;
|
|
} |