#pragma once #include "CustomSX1262.h" #include "RadioLibWrappers.h" #include "SX126xReset.h" #ifndef USE_SX1262 #define USE_SX1262 #endif class CustomSX1262Wrapper : public RadioLibWrapper { public: CustomSX1262Wrapper(CustomSX1262& radio, mesh::MainBoard& board) : RadioLibWrapper(radio, board) { } #ifdef WITH_RX_POWERSAVING // The duty-cycle sleep floor is this board's TCXO delay + 1000 us (RadioLib // subtracts it from the sleep; below it the register underflows). uint32_t rxPowerSavingTransitionUs() const override { return ((CustomSX1262 *)_radio)->getTcxoDelay() + 1000; } bool supportsRxPowerSaving() const override { return true; } #endif void setParams(float freq, float bw, uint8_t sf, uint8_t cr) override { #ifdef WITH_RX_POWERSAVING prepareForRadioConfig(); #endif ((CustomSX1262 *)_radio)->setFrequency(freq); ((CustomSX1262 *)_radio)->setSpreadingFactor(sf); ((CustomSX1262 *)_radio)->setBandwidth(bw); ((CustomSX1262 *)_radio)->setCodingRate(cr); updatePreamble(sf); PacketMillis pm = calcMaxPacketMillis(sf, bw, cr, preambleLengthForSF(sf)); ((CustomSX1262 *)_radio)->setPreambleMillis(pm.preambleMillis); ((CustomSX1262 *)_radio)->setMaxPayloadMillis(pm.payloadMillis); } bool lastRxHadCRC() override { bool crc = true; ((CustomSX1262 *)_radio)->getLoRaRxHeaderInfo(NULL, &crc); return crc; } bool isReceivingPacket() override { #ifdef WITH_RX_POWERSAVING // In the sleep phase (BUSY high) an SPI probe of the IRQ flags would wake the // chip and break the cycle -- and there is nothing to receive. Outside RXPS // this stays a real channel check: Dispatcher::checkSend() relies on it. if (_rx_ps_armed && ((CustomSX1262 *)_radio)->isChipBusy()) return false; #endif return ((CustomSX1262 *)_radio)->isReceiving(); } #ifdef WITH_RX_POWERSAVING bool isChipBusy() override { return ((CustomSX1262 *)_radio)->isChipBusy(); } #endif #if defined(LORA_POLL_IRQ) || defined(PIN_DIAG) // CUSTOM (TeTeHacko): SX126x native status bits. TIMEOUT is in the mask because // an RX window that expires has to end the wait too -- with the DIO1 line dead // nothing else would ever release it. uint32_t irqDoneMask() const override { return RADIOLIB_SX126X_IRQ_TX_DONE | RADIOLIB_SX126X_IRQ_RX_DONE | RADIOLIB_SX126X_IRQ_TIMEOUT; } #endif float getCurrentRSSI() override { return ((CustomSX1262 *)_radio)->getRSSI(false); } #ifndef WITH_RX_POWERSAVING // with it, the base class answers from the values cached at RX float getLastRSSI() const override { return ((CustomSX1262 *)_radio)->getRSSI(); } float getLastSNR() const override { return ((CustomSX1262 *)_radio)->getSNR(); } #endif float packetScore(float snr, int packet_len) override { int sf = ((CustomSX1262 *)_radio)->spreadingFactor; return packetScoreInt(snr, sf, packet_len); } uint8_t getSpreadingFactor() const override { return ((CustomSX1262 *)_radio)->spreadingFactor; } virtual void powerOff() override { #ifdef WITH_RX_POWERSAVING if (_rx_ps_armed) stopReceiveDutyCycle(); #endif ((CustomSX1262 *)_radio)->sleep(false); } bool supportsRxBoostedGain() const override { return true; } bool setRxBoostedGainMode(bool en) override { #ifdef WITH_RX_POWERSAVING prepareForRadioConfig(); #endif return ((CustomSX1262 *)_radio)->setRxBoostedGainMode(en) == RADIOLIB_ERR_NONE; } bool getRxBoostedGainMode() const override { return ((CustomSX1262 *)_radio)->getRxBoostedGainMode(); } void doResetAGC() override { sx126xResetAGC((SX126x *)_radio, getRxBoostedGainMode()); } #ifdef WITH_RX_POWERSAVING protected: int16_t armDutyCycle(RadioLibIrqFlags_t irq_flags, RadioLibIrqFlags_t irq_mask, uint32_t* eff_rx_us, uint32_t* eff_sleep_us) override { // RadioLib programs exactly what is asked (it subtracts the wake-up // transition from the sleep internally). *eff_rx_us = _rx_ps_rx_us; *eff_sleep_us = _rx_ps_sleep_us; return ((CustomSX1262 *)_radio)->startReceiveDutyCycle(_rx_ps_rx_us, _rx_ps_sleep_us, irq_flags, irq_mask); } int16_t stopDutyCycleHardware() override { int16_t standby_err = _radio->standby(); int16_t rtc_err = ((CustomSX1262 *)_radio)->stopRTC(); return standby_err != RADIOLIB_ERR_NONE ? standby_err : rtc_err; } // continuous RX keeps the preamble-detect IRQ, like CustomSX1262::startReceive() int16_t startContinuousReceive() override { return ((CustomSX1262 *)_radio)->startReceive(); } public: #endif #ifdef RADIO_DIAG protected: // CUSTOM (TeTeHacko): `radiostat`, chip side. mode: 2 STBY_RC, 3 STBY_XOSC, // 4 FS, 5 RX, 6 TX; cmd: 2 data available, 3 timeout, 4 processing error, // 5 exec failure. Pin levels are read back, so -1 = not wired. int diagChip(char* out, int sz) override { CustomSX1262* r = (CustomSX1262 *)_radio; uint8_t s = r->diagStatus(); int dio1 = -1, rxen = -1; #ifdef P_LORA_DIO_1 if ((uint32_t) P_LORA_DIO_1 != (uint32_t) RADIOLIB_NC) dio1 = digitalRead(P_LORA_DIO_1); #endif #ifdef SX126X_RXEN if ((uint32_t) SX126X_RXEN != (uint32_t) RADIOLIB_NC) rxen = digitalRead(SX126X_RXEN); #endif return snprintf(out, sz, " mode=%u cmd=%u irq=0x%04lx deverr=0x%04x rssi=%d dio1=%d rxen=%d boost=%d", (unsigned) ((s >> 4) & 7), (unsigned) ((s >> 1) & 7), (unsigned long) r->getIrqFlags(), (unsigned) r->diagDeviceErrors(), (int) r->getRSSI(false), dio1, rxen, (int) r->getRxBoostedGainMode()); } public: #endif };