Skip to content

Commit

Permalink
Instrument (radiolib only for now) lora for powermon
Browse files Browse the repository at this point in the history
  • Loading branch information
geeksville committed Jun 20, 2024
1 parent f872cf1 commit 33a929c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/PowerMon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void PowerMon::clearState(_meshtastic_PowerMon_State state, const char *reason)
void PowerMon::emitLog(const char *reason)
{
// The nrf52 printf doesn't understand 64 bit ints, so if we ever reach that point this function will need to change.
LOG_INFO("PowerMon:C,0x%08lx,%s\n", (uint32_t)states, reason);
LOG_INFO("S:PMon:C,0x%08lx,%s\n", (uint32_t)states, reason);
}

PowerMon *powerMon;
Expand Down
3 changes: 2 additions & 1 deletion src/mesh/LR11x0Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ template <typename T> void LR11x0Interface<T>::setStandby()
activeReceiveStart = 0;
disableInterrupt();
completeSending(); // If we were sending, not anymore
RadioLibInterface::setStandby();
}

/**
Expand Down Expand Up @@ -223,7 +224,7 @@ template <typename T> void LR11x0Interface<T>::startReceive()
0); // only RX_DONE IRQ is needed, we'll check for PREAMBLE_DETECTED and HEADER_VALID in isActivelyReceiving
assert(err == RADIOLIB_ERR_NONE);

isReceiving = true;
RadioLibInterface::startReceive();

// Must be done AFTER, starting transmit, because startTransmit clears (possibly stale) interrupt pending register bits
enableInterrupt(isrRxLevel0);
Expand Down
31 changes: 17 additions & 14 deletions src/mesh/RF95Interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ typedef struct {
} DACDB;

// Interpolation function
DACDB interpolate(uint8_t dbm, uint8_t dbm1, uint8_t dbm2, DACDB val1, DACDB val2) {
DACDB interpolate(uint8_t dbm, uint8_t dbm1, uint8_t dbm2, DACDB val1, DACDB val2)
{
DACDB result;
double fraction = (double)(dbm - dbm1) / (dbm2 - dbm1);
result.dac = (uint8_t)(val1.dac + fraction * (val2.dac - val1.dac));
Expand All @@ -34,16 +35,17 @@ DACDB interpolate(uint8_t dbm, uint8_t dbm1, uint8_t dbm2, DACDB val1, DACDB val
}

// Function to find the correct DAC and DB values based on dBm using interpolation
DACDB getDACandDB(uint8_t dbm) {
DACDB getDACandDB(uint8_t dbm)
{
// Predefined values
static const struct {
uint8_t dbm;
DACDB values;
} dbmToDACDB[] = {
{20, {168, 2}}, // 100mW
{24, {148, 6}}, // 250mW
{27, {128, 9}}, // 500mW
{30, {90, 12}} // 1000mW
{20, {168, 2}}, // 100mW
{24, {148, 6}}, // 250mW
{27, {128, 9}}, // 500mW
{30, {90, 12}} // 1000mW
};
const int numValues = sizeof(dbmToDACDB) / sizeof(dbmToDACDB[0]);

Expand Down Expand Up @@ -103,7 +105,7 @@ bool RF95Interface::init()

if (power > RF95_MAX_POWER) // This chip has lower power limits than some
power = RF95_MAX_POWER;

limitPower();

iface = lora = new RadioLibRF95(&module);
Expand All @@ -116,13 +118,13 @@ bool RF95Interface::init()
// enable PA
#ifdef RF95_PA_EN
#if defined(RF95_PA_DAC_EN)
#ifdef RADIOMASTER_900_BANDIT_NANO
// Use calculated DAC value
dacWrite(RF95_PA_EN, powerDAC);
#else
// Use Value set in /*/variant.h
dacWrite(RF95_PA_EN, RF95_PA_LEVEL);
#endif
#ifdef RADIOMASTER_900_BANDIT_NANO
// Use calculated DAC value
dacWrite(RF95_PA_EN, powerDAC);
#else
// Use Value set in /*/variant.h
dacWrite(RF95_PA_EN, RF95_PA_LEVEL);
#endif
#endif
#endif

Expand Down Expand Up @@ -254,6 +256,7 @@ void RF95Interface::setStandby()
isReceiving = false; // If we were receiving, not any more
disableInterrupt();
completeSending(); // If we were sending, not anymore
RadioLibInterface::setStandby();
}

/** We override to turn on transmitter power as needed.
Expand Down
21 changes: 21 additions & 0 deletions src/mesh/RadioLibInterface.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "RadioLibInterface.h"
#include "MeshTypes.h"
#include "NodeDB.h"
#include "PowerMon.h"
#include "SPILock.h"
#include "configuration.h"
#include "error.h"
Expand Down Expand Up @@ -317,6 +318,7 @@ void RadioLibInterface::handleTransmitInterrupt()
// ignore the transmit interrupt
if (sendingPacket)
completeSending();
powerMon->clearState(meshtastic_PowerMon_State_Lora_TXOn); // But our transmitter is deffinitely off now
}

void RadioLibInterface::completeSending()
Expand Down Expand Up @@ -412,6 +414,24 @@ void RadioLibInterface::handleReceiveInterrupt()
}
}

void RadioLibInterface::startReceive()
{
isReceiving = true;
powerMon->setState(meshtastic_PowerMon_State_Lora_RXOn);
}

void RadioLibInterface::configHardwareForSend()
{
powerMon->setState(meshtastic_PowerMon_State_Lora_TXOn);
}

void RadioLibInterface::setStandby()
{
// neither sending nor receiving
powerMon->clearState(meshtastic_PowerMon_State_Lora_RXOn);
powerMon->clearState(meshtastic_PowerMon_State_Lora_TXOn);
}

/** start an immediate transmit */
void RadioLibInterface::startSend(meshtastic_MeshPacket *txp)
{
Expand All @@ -431,6 +451,7 @@ void RadioLibInterface::startSend(meshtastic_MeshPacket *txp)

// This send failed, but make sure to 'complete' it properly
completeSending();
powerMon->clearState(meshtastic_PowerMon_State_Lora_TXOn); // Transmitter off now
startReceive(); // Restart receive mode (because startTransmit failed to put us in xmit mode)
}

Expand Down
13 changes: 9 additions & 4 deletions src/mesh/RadioLibInterface.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,8 +126,9 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified
* Start waiting to receive a message
*
* External functions can call this method to wake the device from sleep.
* Subclasses must override and call this base method
*/
virtual void startReceive() = 0;
virtual void startReceive();

/** can we detect a LoRa preamble on the current channel? */
virtual bool isChannelActive() = 0;
Expand Down Expand Up @@ -166,8 +167,9 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified
meshtastic_QueueStatus getQueueStatus();

protected:
/** Do any hardware setup needed on entry into send configuration for the radio. Subclasses can customize */
virtual void configHardwareForSend() {}
/** 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();

/** Could we send right now (i.e. either not actively receiving or transmitting)? */
virtual bool canSendImmediately();
Expand All @@ -186,5 +188,8 @@ class RadioLibInterface : public RadioInterface, protected concurrency::Notified
*/
virtual void addReceiveMetadata(meshtastic_MeshPacket *mp) = 0;

virtual void setStandby() = 0;
/**
* Subclasses must override, implement and then call into this base class implementation
*/
virtual void setStandby();
};
3 changes: 2 additions & 1 deletion src/mesh/SX126xInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ template <typename T> void SX126xInterface<T>::setStandby()
activeReceiveStart = 0;
disableInterrupt();
completeSending(); // If we were sending, not anymore
RadioLibInterface::setStandby();
}

/**
Expand Down Expand Up @@ -270,7 +271,7 @@ template <typename T> void SX126xInterface<T>::startReceive()
LOG_ERROR("Radiolib error %d when attempting SX126X startReceiveDutyCycleAuto!\n", err);
assert(err == RADIOLIB_ERR_NONE);

isReceiving = true;
RadioLibInterface::startReceive();

// Must be done AFTER, starting transmit, because startTransmit clears (possibly stale) interrupt pending register bits
enableInterrupt(isrRxLevel0);
Expand Down
3 changes: 2 additions & 1 deletion src/mesh/SX128xInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,7 @@ template <typename T> void SX128xInterface<T>::setStandby()
activeReceiveStart = 0;
disableInterrupt();
completeSending(); // If we were sending, not anymore
RadioLibInterface::setStandby();
}

/**
Expand Down Expand Up @@ -263,7 +264,7 @@ template <typename T> void SX128xInterface<T>::startReceive()
LOG_ERROR("Radiolib error %d when attempting SX128X startReceive!\n", err);
assert(err == RADIOLIB_ERR_NONE);

isReceiving = true;
RadioLibInterface::startReceive();

// Must be done AFTER, starting transmit, because startTransmit clears (possibly stale) interrupt pending register bits
enableInterrupt(isrRxLevel0);
Expand Down

0 comments on commit 33a929c

Please sign in to comment.