2020-04-29 14:54:03 -07:00
|
|
|
#pragma once
|
|
|
|
|
|
2021-02-11 19:00:17 +08:00
|
|
|
#include "MeshPacketQueue.h"
|
2023-01-21 14:34:29 +01:00
|
|
|
#include "RadioInterface.h"
|
|
|
|
|
#include "concurrency/NotifiedWorkerThread.h"
|
2020-04-29 14:54:03 -07:00
|
|
|
|
|
|
|
|
#include <RadioLib.h>
|
|
|
|
|
|
2020-04-30 09:44:16 -07:00
|
|
|
// ESP32 has special rules about ISR code
|
|
|
|
|
#ifdef ARDUINO_ARCH_ESP32
|
|
|
|
|
#define INTERRUPT_ATTR IRAM_ATTR
|
|
|
|
|
#else
|
|
|
|
|
#define INTERRUPT_ATTR
|
|
|
|
|
#endif
|
|
|
|
|
|
2023-05-08 13:18:28 +02:00
|
|
|
#define RADIOLIB_PIN_TYPE uint32_t
|
|
|
|
|
|
2020-08-28 17:38:23 -07:00
|
|
|
/**
|
2023-05-08 13:18:28 +02:00
|
|
|
* We need to override the RadioLib ArduinoHal class to add mutex protection for SPI bus access
|
2020-08-28 17:38:23 -07:00
|
|
|
*/
|
2023-05-08 13:18:28 +02:00
|
|
|
class LockingArduinoHal : public ArduinoHal
|
2020-08-28 17:38:23 -07:00
|
|
|
{
|
|
|
|
|
public:
|
2024-05-10 04:36:20 -05:00
|
|
|
LockingArduinoHal(SPIClass &spi, SPISettings spiSettings, RADIOLIB_PIN_TYPE _busy = RADIOLIB_NC)
|
|
|
|
|
: ArduinoHal(spi, spiSettings)
|
|
|
|
|
{
|
|
|
|
|
#if ARCH_PORTDUINO
|
|
|
|
|
busy = _busy;
|
|
|
|
|
#endif
|
|
|
|
|
};
|
2023-01-21 14:34:29 +01:00
|
|
|
|
2023-05-08 13:18:28 +02:00
|
|
|
void spiBeginTransaction() override;
|
|
|
|
|
void spiEndTransaction() override;
|
2024-04-01 18:31:36 -05:00
|
|
|
#if ARCH_PORTDUINO
|
2024-05-10 04:36:20 -05:00
|
|
|
RADIOLIB_PIN_TYPE busy;
|
2024-04-01 18:31:36 -05:00
|
|
|
void spiTransfer(uint8_t *out, size_t len, uint8_t *in) override;
|
2024-05-10 04:36:20 -05:00
|
|
|
|
2024-04-01 18:31:36 -05:00
|
|
|
#endif
|
2020-08-28 17:38:23 -07:00
|
|
|
};
|
|
|
|
|
|
2023-06-01 07:14:55 -05:00
|
|
|
#if defined(USE_STM32WLx)
|
|
|
|
|
/**
|
|
|
|
|
* A wrapper for the RadioLib STM32WLx_Module class, that doesn't connect any pins as they are virtual
|
|
|
|
|
*/
|
|
|
|
|
class STM32WLx_ModuleWrapper : public STM32WLx_Module
|
|
|
|
|
{
|
|
|
|
|
public:
|
|
|
|
|
STM32WLx_ModuleWrapper(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst,
|
|
|
|
|
RADIOLIB_PIN_TYPE busy)
|
|
|
|
|
: STM32WLx_Module(){};
|
|
|
|
|
};
|
|
|
|
|
#endif
|
|
|
|
|
|
2020-10-11 09:18:47 +08:00
|
|
|
class RadioLibInterface : public RadioInterface, protected concurrency::NotifiedWorkerThread
|
2020-04-29 14:54:03 -07:00
|
|
|
{
|
2020-05-01 12:11:04 -07:00
|
|
|
/// Used as our notification from the ISR
|
2020-05-02 08:29:51 -07:00
|
|
|
enum PendingISR { ISR_NONE = 0, ISR_RX, ISR_TX, TRANSMIT_DELAY_COMPLETED };
|
2020-04-30 09:44:16 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Raw ISR handler that just calls our polymorphic method
|
|
|
|
|
*/
|
2020-05-02 08:29:51 -07:00
|
|
|
static void isrTxLevel0(), isrLevel0Common(PendingISR code);
|
2020-04-30 09:44:16 -07:00
|
|
|
|
2021-02-11 19:00:17 +08:00
|
|
|
MeshPacketQueue txQueue = MeshPacketQueue(MAX_TX_QUEUE);
|
2020-06-09 15:47:05 -07:00
|
|
|
|
2020-04-29 16:28:11 -07:00
|
|
|
protected:
|
2020-04-29 18:46:32 -07:00
|
|
|
/**
|
2023-01-21 14:34:29 +01:00
|
|
|
* We use a meshtastic sync word, but hashed with the Channel name. For releases before 1.2 we used 0x12 (or for very old
|
|
|
|
|
* loads 0x14) Note: do not use 0x34 - that is reserved for lorawan
|
|
|
|
|
*
|
|
|
|
|
* We now use 0x2b (so that someday we can possibly use NOT 2b - because that would be funny pun). We will be staying with
|
|
|
|
|
* this code for a long time.
|
2020-04-29 18:46:32 -07:00
|
|
|
*/
|
2021-03-03 11:46:09 +08:00
|
|
|
const uint8_t syncWord = 0x2b;
|
2023-01-21 14:34:29 +01:00
|
|
|
|
|
|
|
|
float currentLimit = 100; // 100mA OCP - Should be acceptable for RFM95/SX127x chipset.
|
2020-04-29 16:28:11 -07:00
|
|
|
|
2023-06-01 07:14:55 -05:00
|
|
|
#if !defined(USE_STM32WLx)
|
2023-05-08 13:18:28 +02:00
|
|
|
Module module; // The HW interface to the radio
|
2023-06-01 07:14:55 -05:00
|
|
|
#else
|
|
|
|
|
STM32WLx_ModuleWrapper module;
|
|
|
|
|
#endif
|
2020-04-29 16:28:11 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* provides lowest common denominator RadioLib API
|
|
|
|
|
*/
|
2020-04-30 17:43:29 -07:00
|
|
|
PhysicalLayer *iface;
|
2020-04-29 14:54:03 -07:00
|
|
|
|
2020-04-30 13:50:40 -07:00
|
|
|
/// are _trying_ to receive a packet currently (note - we might just be waiting for one)
|
2022-01-24 07:00:14 +00:00
|
|
|
bool isReceiving = false;
|
2020-04-30 13:50:40 -07:00
|
|
|
|
2020-05-14 14:20:05 -07:00
|
|
|
public:
|
|
|
|
|
/** Our ISR code currently needs this to find our active instance
|
|
|
|
|
*/
|
|
|
|
|
static RadioLibInterface *instance;
|
|
|
|
|
|
2020-04-30 09:44:16 -07:00
|
|
|
/**
|
|
|
|
|
* Glue functions called from ISR land
|
|
|
|
|
*/
|
|
|
|
|
virtual void disableInterrupt() = 0;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Enable a particular ISR callback glue function
|
|
|
|
|
*/
|
|
|
|
|
virtual void enableInterrupt(void (*)()) = 0;
|
|
|
|
|
|
2024-08-16 17:15:51 -05:00
|
|
|
/**
|
|
|
|
|
* Debugging counts
|
|
|
|
|
*/
|
|
|
|
|
uint32_t rxBad = 0, rxGood = 0, txGood = 0;
|
|
|
|
|
|
2020-04-29 14:54:03 -07:00
|
|
|
public:
|
2023-05-08 13:18:28 +02:00
|
|
|
RadioLibInterface(LockingArduinoHal *hal, RADIOLIB_PIN_TYPE cs, RADIOLIB_PIN_TYPE irq, RADIOLIB_PIN_TYPE rst,
|
|
|
|
|
RADIOLIB_PIN_TYPE busy, PhysicalLayer *iface = NULL);
|
2020-04-29 14:54:03 -07:00
|
|
|
|
2023-01-21 18:22:19 +01:00
|
|
|
virtual ErrorCode send(meshtastic_MeshPacket *p) override;
|
2020-04-29 14:54:03 -07:00
|
|
|
|
2020-04-30 15:50:07 -07:00
|
|
|
/**
|
|
|
|
|
* Return true if we think the board can go to sleep (i.e. our tx queue is empty, we are not sending or receiving)
|
|
|
|
|
*
|
|
|
|
|
* This method must be used before putting the CPU into deep or light sleep.
|
|
|
|
|
*/
|
2022-01-24 17:24:40 +00:00
|
|
|
virtual bool canSleep() override;
|
2020-04-30 15:50:07 -07:00
|
|
|
|
2020-05-14 14:20:05 -07:00
|
|
|
/**
|
|
|
|
|
* Start waiting to receive a message
|
|
|
|
|
*
|
|
|
|
|
* External functions can call this method to wake the device from sleep.
|
2024-07-03 16:02:20 -07:00
|
|
|
* Subclasses must override and call this base method
|
2020-05-14 14:20:05 -07:00
|
|
|
*/
|
2024-07-03 16:02:20 -07:00
|
|
|
virtual void startReceive();
|
2020-05-14 14:20:05 -07:00
|
|
|
|
2022-04-20 20:09:12 +02:00
|
|
|
/** can we detect a LoRa preamble on the current channel? */
|
|
|
|
|
virtual bool isChannelActive() = 0;
|
|
|
|
|
|
2020-10-08 06:23:05 +08:00
|
|
|
/** are we actively receiving a packet (only called during receiving state)
|
|
|
|
|
* This method is only public to facilitate debugging. Do not call.
|
|
|
|
|
*/
|
|
|
|
|
virtual bool isActivelyReceiving() = 0;
|
|
|
|
|
|
2021-02-11 17:39:53 +08:00
|
|
|
/** Attempt to cancel a previously sent packet. Returns true if a packet was found we could cancel */
|
2022-01-24 17:24:40 +00:00
|
|
|
virtual bool cancelSending(NodeNum from, PacketId id) override;
|
2021-02-11 19:00:17 +08:00
|
|
|
|
2020-04-30 12:37:58 -07:00
|
|
|
private:
|
2023-01-21 14:34:29 +01:00
|
|
|
/** if we have something waiting to send, start a short (random) timer so we can come check for collision before actually
|
|
|
|
|
* doing the transmit */
|
2022-04-23 18:57:45 +02:00
|
|
|
void setTransmitDelay();
|
2022-04-20 20:09:12 +02:00
|
|
|
|
|
|
|
|
/** random timer with certain min. and max. settings */
|
2020-05-02 08:29:51 -07:00
|
|
|
void startTransmitTimer(bool withDelay = true);
|
2020-04-30 12:37:58 -07:00
|
|
|
|
2022-04-20 20:09:12 +02:00
|
|
|
/** timer scaled to SNR of to be flooded packet */
|
2022-03-14 17:47:01 -07:00
|
|
|
void startTransmitTimerSNR(float snr);
|
|
|
|
|
|
2020-04-30 12:37:58 -07:00
|
|
|
void handleTransmitInterrupt();
|
|
|
|
|
void handleReceiveInterrupt();
|
|
|
|
|
|
2020-05-02 08:29:51 -07:00
|
|
|
static void timerCallback(void *p1, uint32_t p2);
|
|
|
|
|
|
2022-01-24 17:24:40 +00:00
|
|
|
virtual void onNotify(uint32_t notification) override;
|
2020-05-25 07:48:36 -07:00
|
|
|
|
2020-06-16 06:26:21 -07:00
|
|
|
/** start an immediate transmit
|
|
|
|
|
* This method is virtual so subclasses can hook as needed, subclasses should not call directly
|
|
|
|
|
*/
|
2023-01-21 18:22:19 +01:00
|
|
|
virtual void startSend(meshtastic_MeshPacket *txp);
|
2020-06-16 06:26:21 -07:00
|
|
|
|
2023-01-21 18:22:19 +01:00
|
|
|
meshtastic_QueueStatus getQueueStatus();
|
2023-01-04 14:56:52 +02:00
|
|
|
|
2020-04-29 18:46:32 -07:00
|
|
|
protected:
|
2024-09-25 16:19:18 -05:00
|
|
|
uint32_t activeReceiveStart = 0;
|
|
|
|
|
|
|
|
|
|
bool receiveDetected(uint16_t irq, ulong syncWordHeaderValidFlag, ulong preambleDetectedFlag);
|
|
|
|
|
|
2024-07-03 16:02:20 -07:00
|
|
|
/** Do any hardware setup needed on entry into send configuration for the radio.
|
|
|
|
|
* Subclasses can customize, but must also call this base method */
|
|
|
|
|
virtual void configHardwareForSend();
|
2020-06-15 14:38:09 -07:00
|
|
|
|
2020-04-30 09:44:16 -07:00
|
|
|
/** Could we send right now (i.e. either not actively receiving or transmitting)? */
|
2020-05-02 19:51:25 -07:00
|
|
|
virtual bool canSendImmediately();
|
2020-05-01 08:32:16 -07:00
|
|
|
|
2020-04-30 12:37:58 -07:00
|
|
|
/**
|
|
|
|
|
* Raw ISR handler that just calls our polymorphic method
|
|
|
|
|
*/
|
|
|
|
|
static void isrRxLevel0();
|
2020-04-30 13:50:40 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* If a send was in progress finish it and return the buffer to the pool */
|
|
|
|
|
void completeSending();
|
2020-04-30 19:58:10 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Add SNR data to received messages
|
|
|
|
|
*/
|
2023-01-21 18:22:19 +01:00
|
|
|
virtual void addReceiveMetadata(meshtastic_MeshPacket *mp) = 0;
|
2020-05-01 12:11:04 -07:00
|
|
|
|
2024-07-03 16:02:20 -07:00
|
|
|
/**
|
|
|
|
|
* Subclasses must override, implement and then call into this base class implementation
|
|
|
|
|
*/
|
|
|
|
|
virtual void setStandby();
|
2024-05-10 04:36:20 -05:00
|
|
|
};
|