Skip to content

Commit

Permalink
Merge pull request #45 from BeelanMX/Tx_pwr_ctrl
Browse files Browse the repository at this point in the history
Change Tx power control
  • Loading branch information
sabas1080 authored Aug 18, 2020
2 parents 4783bb1 + ef4484f commit e7631f7
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 10 deletions.
61 changes: 56 additions & 5 deletions src/arduino-rfm/RFM95.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -487,11 +487,9 @@ bool RFM_Init()
RFM_Switch_Mode(RFM_MODE_STANDBY);
//Set channel to channel 0
RFM_Change_Channel(CH0);
//PA pin (minimal power)
//RFM_Write(0x09,0xF0);
//set to 17dbm
RFM_Write(RFM_REG_PA_CONFIG,0xF0);

//Set default power to maximun on US915 TODO AS/AU/EU config
//Set the default output pin as PA_BOOST
RFM_Set_Tx_Power(20, PA_BOOST_PIN);

//Switch LNA boost on
RFM_Write(RFM_REG_LNA,0x23);
Expand Down Expand Up @@ -519,6 +517,59 @@ bool RFM_Init()
return 1;
}


void RFM_Set_Tx_Power(int level, int outputPin)
{
if (RFO_PIN == outputPin) {
// RFO
if (level < 0) {
level = 0;
} else if (level > 14) {
level = 14;
}

RFM_Write(RFM_REG_PA_CONFIG, 0x70 | level);
} else {
// PA BOOST
if (level > 17) {
if (level > 20) {
level = 20;
}

// subtract 3 from level, so 18 - 20 maps to 15 - 17
level -= 3;

// High Power +20 dBm Operation (Semtech SX1276/77/78/79 5.4.3.)
RFM_Write(RFM_REG_PA_DAC, 0x87);
RFM_Set_OCP(140);
} else {
if (level < 2) {
level = 2;
}
//Default value PA_HF/LF or +17dBm
RFM_Write(RFM_REG_PA_DAC, 0x84);
RFM_Set_OCP(100);
}

RFM_Write(RFM_REG_PA_CONFIG, 0x80 | (level - 2)); //PA Boost mask
}
}


void RFM_Set_OCP(uint8_t mA)
{
uint8_t ocpTrim = 27;

if (mA <= 120) {
ocpTrim = (mA - 45) / 5;
} else if (mA <=240) {
ocpTrim = (mA + 30) / 10;
}

RFM_Write(RFM_REG_OCP, 0x20 | (0x1F & ocpTrim));
}


/*
*****************************************************************************************
* Description : Function for sending a package with the RFM
Expand Down
10 changes: 9 additions & 1 deletion src/arduino-rfm/RFM95.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,9 @@

typedef enum {NO_MESSAGE,NEW_MESSAGE,CRC_OK,MIC_OK,ADDRESS_OK,MESSAGE_DONE,TIMEOUT,WRONG_MESSAGE} message_t;

#define PA_OUTPUT_RFO_PIN 0
#define PA_OUTPUT_PA_BOOST_PIN 1

/*
*****************************************************************************************
* REGISTER DEFINITIONS
Expand All @@ -57,6 +60,7 @@ typedef enum {
RFM_REG_FR_MSB = 0x06,
RFM_REG_FR_MID = 0x07,
RFM_REG_FR_LSB = 0x08,
RFM_REG_OCP = 0x0b,
RFM_REG_PA_CONFIG = 0x09,
RFM_REG_LNA = 0x0C,
RFM_REG_FIFO_ADDR_PTR = 0x0D,
Expand All @@ -72,7 +76,8 @@ typedef enum {
RFM_REG_INVERT_IQ2 = 0x3b,
RFM_REG_SYNC_WORD = 0x39,
RFM_REG_DIO_MAPPING1 = 0x40,
RFM_REG_DIO_MAPPING2 = 0x41
RFM_REG_DIO_MAPPING2 = 0x41,
RFM_REG_PA_DAC = 0x4d

} rfm_register_t;

Expand Down Expand Up @@ -102,6 +107,9 @@ void RFM_Continuous_Receive(sSettings *LoRa_Settings);
message_t RFM_Get_Package(sBuffer *RFM_Rx_Package);
void RFM_Write(unsigned char RFM_Address, unsigned char RFM_Data);
void RFM_Switch_Mode(unsigned char Mode);
void RFM_Set_Tx_Power(int level, int outputPin);
void RFM_Set_OCP(uint8_t mA);


#endif

6 changes: 6 additions & 0 deletions src/arduino-rfm/Struct.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,12 @@ typedef enum {
MULTI = 20
} channel_t;


typedef enum {
RFO_PIN = 0,
PA_BOOST_PIN = 1
} txPin_t;

typedef enum {
#if defined(US_915)
SF10BW125 = 0x00,
Expand Down
7 changes: 6 additions & 1 deletion src/arduino-rfm/lorawan-arduino-rfm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,11 @@ void LoRaWANClass::setDevAddr(const char *devAddr_in)
RFM_Command_Status = NO_RFM_COMMAND;
}

void LoRaWANClass::setTxPower(int level,txPin_t pinTx)
{
RFM_Set_Tx_Power(level, pinTx);
}

void LoRaWANClass::setDeviceClass(devclass_t dev_class)
{
LoRa_Settings.Mote_Class = (dev_class == CLASS_A)? CLASS_A : CLASS_C;
Expand Down Expand Up @@ -332,7 +337,7 @@ unsigned char LoRaWANClass::getChannel()
unsigned char LoRaWANClass::getDataRate() {
return LoRa_Settings.Datarate_Tx;
}
void LoRaWANClass::setTxPower(unsigned char power_idx)
void LoRaWANClass::setTxPower1(unsigned char power_idx)
{
unsigned char RFM_Data;
LoRa_Settings.Transmit_Power = (power_idx > 0x0F) ? 0x0F : power_idx;
Expand Down
3 changes: 2 additions & 1 deletion src/arduino-rfm/lorawan-arduino-rfm.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,8 @@ class LoRaWANClass
void setChannel(unsigned char channel);
unsigned char getChannel();
unsigned char getDataRate();
void setTxPower(unsigned char power_idx);
void setTxPower1(unsigned char power_idx);
void setTxPower(int level,txPin_t pinTx);
int readData(char *outBuff);
bool readAck(void);
void update(void);
Expand Down
2 changes: 1 addition & 1 deletion test/testFullfrequency/testFullfrequency.ino
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ void setup() {
// Set LoRaWAN Class
lora.setDeviceClass(CLASS_A);

lora.setTxPower(15);
lora.setTxPower(15,PA_BOOST_PIN);

counter.val = 0;
_channel = 0; // 0 - 7
Expand Down
2 changes: 1 addition & 1 deletion test/testOTAA/testOTAA.ino
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void setup() {
return;
}
lora.setDeviceClass(CLASS_A);
lora.setTxPower(15);
lora.setTxPower(15,PA_BOOST_PIN);
lora.setChannel(CH0);
lora.setDataRate(SF9BW125);

Expand Down

0 comments on commit e7631f7

Please sign in to comment.