Skip to content

Commit

Permalink
Merge pull request #30 from clavisound/setPower
Browse files Browse the repository at this point in the history
SetPower function
  • Loading branch information
brentru authored Oct 8, 2019
2 parents aa28928 + 0bb5bb7 commit b04a3b3
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 10 deletions.
67 changes: 64 additions & 3 deletions TinyLoRa.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ const unsigned char PROGMEM TinyLoRa::LoRa_Frequency[8][3] = {
const unsigned char PROGMEM TinyLoRa::LoRa_Frequency[8][3] = {
{ 0xE1, 0xF9, 0xC0 }, //Channel 0 903.900 MHz / 61.035 Hz = 14809536 = 0xE1F9C0
{ 0xE2, 0x06, 0x8C }, //Channel 1 904.100 MHz / 61.035 Hz = 14812812 = 0xE2068C
{ 0xE2, 0x13, 0x59}, //Channel 2 904.300 MHz / 61.035 Hz = 14816089 = 0xE21359
{ 0xE2, 0x13, 0x59 }, //Channel 2 904.300 MHz / 61.035 Hz = 14816089 = 0xE21359
{ 0xE2, 0x20, 0x26 }, //Channel 3 904.500 MHz / 61.035 Hz = 14819366 = 0xE22026
{ 0xE2, 0x2C, 0xF3 }, //Channel 4 904.700 MHz / 61.035 Hz = 14822643 = 0xE22CF3
{ 0xE2, 0x39, 0xC0 }, //Channel 5 904.900 MHz / 61.035 Hz = 14825920 = 0xE239C0
Expand Down Expand Up @@ -292,7 +292,7 @@ TinyLoRa::TinyLoRa(int8_t rfm_irq, int8_t rfm_nss, int8_t rfm_rst) {
@return True if the RFM has been initialized
*/
/**************************************************************************/
bool TinyLoRa::begin()
bool TinyLoRa::begin()
{

// start and configure SPI
Expand Down Expand Up @@ -330,7 +330,7 @@ bool TinyLoRa::begin()
//Set RFM in LoRa mode
RFM_Write(0x01,MODE_LORA);

//PA pin (maximal power)
//PA pin (maximal power, 17dBm)
RFM_Write(0x09,0xFF);

//Rx Timeout set to 37 symbols
Expand Down Expand Up @@ -365,6 +365,67 @@ bool TinyLoRa::begin()
return 1;
}

/**************************************************************************/
/*!
@brief Sets the TX power
@param Tx_Power How much TX power in dBm
*/
/**************************************************************************/
// Valid values in dBm are: -80, +1 to +17 and +20.
//
// 18-19dBm are undefined in doc but maybe possible. Here are ignored.
// Chip works with three modes. This function offer granularity of 1dBm
// but the chips is capable of more.
//
// -4.2 to 0 is in reality -84 to -80dBm

void TinyLoRa::setPower(int8_t Tx_Power) {

// values to be packed in one byte
bool PaBoost;
int8_t OutputPower; // 0-15
int8_t MaxPower; // 0-7

// this value goes to the register (packed bytes)
uint8_t DataPower;

// 1st possibility -80
if ( Tx_Power == -80 ) { // force -80dBm (lower power)
PaBoost = 0;
MaxPower = 0;
OutputPower = 0;
// 2nd possibility: range 1 to 17dBm
} else if ( Tx_Power >= 0 && Tx_Power < 2 ) { // assume 1 db is given.
PaBoost = 1;
MaxPower = 7;
OutputPower = 1;
} else if ( Tx_Power >= 2 && Tx_Power <=17 ) {
PaBoost = 1;
MaxPower = 7;
// formula to find the OutputPower.
OutputPower = Tx_Power - 2;
}

// 3rd possibility. 20dBm. Special case
// Max Antenna VSWR 3:1, Duty Cycle <1% or destroyed(?) chip
if ( Tx_Power == 20 ) {
PaBoost = 1;
OutputPower = 15;
MaxPower = 7;
RFM_Write(REG_PA_DAC, 0x87); // only for +20dBm probably with 0x86,0x85 = 19,18dBm
} else {
// Setting for non +20dBm power
RFM_Write(REG_PA_DAC, 0x84);
}

// Pack the above data to one byte and send it to HOPE RFM9x
DataPower = (PaBoost << 7) + (MaxPower << 4) + OutputPower;

//PA pin. Default value is 0x4F (DEC 79, 3dBm) from HOPE, 0xFF (DEC 255 / 17dBm) from adafruit.
RFM_Write(REG_PA_CONFIG,DataPower);
}


/**************************************************************************/
/*!
@brief Sends a package with the RFM module.
Expand Down
16 changes: 9 additions & 7 deletions TinyLoRa.h
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ typedef enum rfm_datarates

/* RFM Registers */
#define REG_PA_CONFIG 0x09 ///<PA selection and Output Power control
#define REG_PA_DAC 0x4D ///<PA Higher Power Settings
#define REG_PREAMBLE_MSB 0x20 ///<Preamble Length, MSB
#define REG_PREAMBLE_LSB 0x21 ///<Preamble Length, LSB
#define REG_FRF_MSB 0x06 ///<RF Carrier Frequency MSB
Expand All @@ -110,22 +111,23 @@ class TinyLoRa
public:
uint8_t txrandomNum; ///<random number for AES
uint16_t frameCounter; ///<frame counter
void setChannel(rfm_channels_t channel);
void setDatarate(rfm_datarates_t datarate);
TinyLoRa(int8_t rfm_dio0, int8_t rfm_nss, int8_t rfm_rst);
void setChannel(rfm_channels_t channel);
void setDatarate(rfm_datarates_t datarate);
void setPower(int8_t Tx_Power = 17);
TinyLoRa(int8_t rfm_dio0, int8_t rfm_nss, int8_t rfm_rst);
bool begin(void);
void sendData(unsigned char *Data, unsigned char Data_Length, unsigned int Frame_Counter_Tx, uint8_t Frame_Port = 1);

private:
uint8_t randomNum;
int8_t _cs, _irq, _rst;
bool _isMultiChan;
unsigned char _rfmMSB, _rfmMID, _rfmLSB, _sf, _bw, _modemcfg;
static const unsigned char LoRa_Frequency[8][3];
bool _isMultiChan;
unsigned char _rfmMSB, _rfmMID, _rfmLSB, _sf, _bw, _modemcfg;
static const unsigned char LoRa_Frequency[8][3];
static const unsigned char S_Table[16][16];
void RFM_Send_Package(unsigned char *RFM_Tx_Package, unsigned char Package_Length);
void RFM_Write(unsigned char RFM_Address, unsigned char RFM_Data);
uint8_t RFM_Read(uint8_t RFM_Address);
uint8_t RFM_Read(uint8_t RFM_Address);
void Encrypt_Payload(unsigned char *Data, unsigned char Data_Length, unsigned int Frame_Counter, unsigned char Direction);
void Calculate_MIC(unsigned char *Data, unsigned char *Final_MIC, unsigned char Data_Length, unsigned int Frame_Counter, unsigned char Direction);
void Generate_Keys(unsigned char *K1, unsigned char *K2);
Expand Down
8 changes: 8 additions & 0 deletions examples/hello_LoRa/hello_LoRa.ino
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,14 @@ void setup()
Serial.println("Check your radio");
while(true);
}

// Optional set transmit power. If not set default is +17 dBm.
// Valid options are: -80, 1 to 17, 20 (dBm).
// For safe operation in 20dBm: your antenna must be 3:1 VWSR or better
// and respect the 1% duty cycle.

// lora.setPower(17);

Serial.println("OK");
}

Expand Down
1 change: 1 addition & 0 deletions library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
name=TinyLoRa
version=1.2.2
version=1.3.0
author=Adafruit
maintainer=Adafruit<info@adafruit.com>
sentence=Tiny LoRa Library for TTN
Expand Down

0 comments on commit b04a3b3

Please sign in to comment.