Skip to content

Commit 07bfead

Browse files
ricaunsandeepmistry
authored andcommitted
Add function to invert IQ signal register (sandeepmistry#179)
Add function to change register (REG_INVERTIQ) to support communication node and gateway The register does not exist in datasheet but use on but used in Semtech code. Reference : https://github.com/intel-iot-devkit/upm/blob/master/src/sx1276/sx1276.cxx * Add LoRa Simple Gateway/Node Exemple Example how to use InvertIQ function to create a simple Gateway/Node logic.
1 parent 4037386 commit 07bfead

File tree

6 files changed

+255
-0
lines changed

6 files changed

+255
-0
lines changed

API.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,16 @@ LoRa.enableCrc();
329329
LoRa.disableCrc();
330330
```
331331

332+
### Invert IQ Signals
333+
334+
Enable or disable Invert the LoRa I and Q signals, by default a invertIQ is not used.
335+
336+
```arduino
337+
LoRa.enableInvertIQ();
338+
339+
LoRa.disableInvertIQ();
340+
```
341+
332342
## Other functions
333343

334344
### Random
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
LoRa Simple Gateway/Node Exemple
3+
4+
This code uses InvertIQ function to create a simple Gateway/Node logic.
5+
6+
Gateway - Sends messages with enableInvertIQ()
7+
- Receives messages with disableInvertIQ()
8+
9+
Node - Sends messages with disableInvertIQ()
10+
- Receives messages with enableInvertIQ()
11+
12+
With this arrangement a Gateway never receive messages from another Gateway
13+
and a Node never receive message from another Node.
14+
Only Gateway to Node and vice versa.
15+
16+
This code receives messages and sends a message every second.
17+
18+
InvertIQ function basically invert the LoRa I and Q signals.
19+
20+
See the Semtech datasheet, http://www.semtech.com/images/datasheet/sx1276.pdf
21+
for more on InvertIQ register 0x33.
22+
23+
created 05 August 2018
24+
by Luiz H. Cassettari
25+
*/
26+
27+
#include <SPI.h> // include libraries
28+
#include <LoRa.h>
29+
30+
const long frequency = 915E6; // LoRa Frequency
31+
32+
const int csPin = 10; // LoRa radio chip select
33+
const int resetPin = 9; // LoRa radio reset
34+
const int irqPin = 2; // change for your board; must be a hardware interrupt pin
35+
36+
void setup() {
37+
Serial.begin(9600); // initialize serial
38+
while (!Serial);
39+
40+
LoRa.setPins(csPin, resetPin, irqPin);
41+
42+
if (!LoRa.begin(frequency)) {
43+
Serial.println("LoRa init failed. Check your connections.");
44+
while (true); // if failed, do nothing
45+
}
46+
47+
Serial.println("LoRa init succeeded.");
48+
Serial.println();
49+
Serial.println("LoRa Simple Gateway");
50+
Serial.println("Only receive messages from nodes");
51+
Serial.println("Tx: invertIQ enable");
52+
Serial.println("Rx: invertIQ disable");
53+
Serial.println();
54+
55+
LoRa.onReceive(onReceive);
56+
LoRa_rxMode();
57+
}
58+
59+
void loop() {
60+
if (runEvery(5000)) { // repeat every 5000 millis
61+
62+
String message = "HeLoRa World! ";
63+
message += "I'm a Gateway! ";
64+
message += millis();
65+
66+
LoRa_sendMessage(message); // send a message
67+
68+
Serial.println("Send Message!");
69+
}
70+
}
71+
72+
void LoRa_rxMode(){
73+
LoRa.disableInvertIQ(); // normal mode
74+
LoRa.receive(); // set receive mode
75+
}
76+
77+
void LoRa_txMode(){
78+
LoRa.idle(); // set standby mode
79+
LoRa.enableInvertIQ(); // active invert I and Q signals
80+
}
81+
82+
void LoRa_sendMessage(String message) {
83+
LoRa_txMode(); // set tx mode
84+
LoRa.beginPacket(); // start packet
85+
LoRa.print(message); // add payload
86+
LoRa.endPacket(); // finish packet and send it
87+
LoRa_rxMode(); // set rx mode
88+
}
89+
90+
void onReceive(int packetSize) {
91+
String message = "";
92+
93+
while (LoRa.available()) {
94+
message += (char)LoRa.read();
95+
}
96+
97+
Serial.print("Gateway Receive: ");
98+
Serial.println(message);
99+
100+
}
101+
102+
boolean runEvery(unsigned long interval)
103+
{
104+
static unsigned long previousMillis = 0;
105+
unsigned long currentMillis = millis();
106+
if (currentMillis - previousMillis >= interval)
107+
{
108+
previousMillis = currentMillis;
109+
return true;
110+
}
111+
return false;
112+
}
113+
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
/*
2+
LoRa Simple Gateway/Node Exemple
3+
4+
This code uses InvertIQ function to create a simple Gateway/Node logic.
5+
6+
Gateway - Sends messages with enableInvertIQ()
7+
- Receives messages with disableInvertIQ()
8+
9+
Node - Sends messages with disableInvertIQ()
10+
- Receives messages with enableInvertIQ()
11+
12+
With this arrangement a Gateway never receive messages from another Gateway
13+
and a Node never receive message from another Node.
14+
Only Gateway to Node and vice versa.
15+
16+
This code receives messages and sends a message every second.
17+
18+
InvertIQ function basically invert the LoRa I and Q signals.
19+
20+
See the Semtech datasheet, http://www.semtech.com/images/datasheet/sx1276.pdf
21+
for more on InvertIQ register 0x33.
22+
23+
created 05 August 2018
24+
by Luiz H. Cassettari
25+
*/
26+
27+
#include <SPI.h> // include libraries
28+
#include <LoRa.h>
29+
30+
const long frequency = 915E6; // LoRa Frequency
31+
32+
const int csPin = 10; // LoRa radio chip select
33+
const int resetPin = 9; // LoRa radio reset
34+
const int irqPin = 2; // change for your board; must be a hardware interrupt pin
35+
36+
void setup() {
37+
Serial.begin(9600); // initialize serial
38+
while (!Serial);
39+
40+
LoRa.setPins(csPin, resetPin, irqPin);
41+
42+
if (!LoRa.begin(frequency)) {
43+
Serial.println("LoRa init failed. Check your connections.");
44+
while (true); // if failed, do nothing
45+
}
46+
47+
Serial.println("LoRa init succeeded.");
48+
Serial.println();
49+
Serial.println("LoRa Simple Node");
50+
Serial.println("Only receive messages from gateways");
51+
Serial.println("Tx: invertIQ disable");
52+
Serial.println("Rx: invertIQ enable");
53+
Serial.println();
54+
55+
LoRa.onReceive(onReceive);
56+
LoRa_rxMode();
57+
}
58+
59+
void loop() {
60+
if (runEvery(1000)) { // repeat every 1000 millis
61+
62+
String message = "HeLoRa World! ";
63+
message += "I'm a Node! ";
64+
message += millis();
65+
66+
LoRa_sendMessage(message); // send a message
67+
68+
Serial.println("Send Message!");
69+
}
70+
}
71+
72+
void LoRa_rxMode(){
73+
LoRa.enableInvertIQ(); // active invert I and Q signals
74+
LoRa.receive(); // set receive mode
75+
}
76+
77+
void LoRa_txMode(){
78+
LoRa.idle(); // set standby mode
79+
LoRa.disableInvertIQ(); // normal mode
80+
}
81+
82+
void LoRa_sendMessage(String message) {
83+
LoRa_txMode(); // set tx mode
84+
LoRa.beginPacket(); // start packet
85+
LoRa.print(message); // add payload
86+
LoRa.endPacket(); // finish packet and send it
87+
LoRa_rxMode(); // set rx mode
88+
}
89+
90+
void onReceive(int packetSize) {
91+
String message = "";
92+
93+
while (LoRa.available()) {
94+
message += (char)LoRa.read();
95+
}
96+
97+
Serial.print("Node Receive: ");
98+
Serial.println(message);
99+
100+
}
101+
102+
boolean runEvery(unsigned long interval)
103+
{
104+
static unsigned long previousMillis = 0;
105+
unsigned long currentMillis = millis();
106+
if (currentMillis - previousMillis >= interval)
107+
{
108+
previousMillis = currentMillis;
109+
return true;
110+
}
111+
return false;
112+
}
113+

keywords.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ setPreambleLength KEYWORD2
4444
setSyncWord KEYWORD2
4545
enableCrc KEYWORD2
4646
disableCrc KEYWORD2
47+
enableInvertIQ KEYWORD2
48+
disableInvertIQ KEYWORD2
4749

4850
random KEYWORD2
4951
setPins KEYWORD2

src/LoRa.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,10 @@
3131
#define REG_FREQ_ERROR_LSB 0x2a
3232
#define REG_RSSI_WIDEBAND 0x2c
3333
#define REG_DETECTION_OPTIMIZE 0x31
34+
#define REG_INVERTIQ 0x33
3435
#define REG_DETECTION_THRESHOLD 0x37
3536
#define REG_SYNC_WORD 0x39
37+
#define REG_INVERTIQ2 0x3b
3638
#define REG_DIO_MAPPING_1 0x40
3739
#define REG_VERSION 0x42
3840
#define REG_PA_DAC 0x4d
@@ -531,6 +533,18 @@ void LoRaClass::disableCrc()
531533
writeRegister(REG_MODEM_CONFIG_2, readRegister(REG_MODEM_CONFIG_2) & 0xfb);
532534
}
533535

536+
void LoRaClass::enableInvertIQ()
537+
{
538+
writeRegister(REG_INVERTIQ, 0x66);
539+
writeRegister(REG_INVERTIQ2, 0x19);
540+
}
541+
542+
void LoRaClass::disableInvertIQ()
543+
{
544+
writeRegister(REG_INVERTIQ, 0x27);
545+
writeRegister(REG_INVERTIQ2, 0x1d);
546+
}
547+
534548
void LoRaClass::setOCP(uint8_t mA)
535549
{
536550
uint8_t ocpTrim = 27;

src/LoRa.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,9 @@ class LoRaClass : public Stream {
6666
void setSyncWord(int sw);
6767
void enableCrc();
6868
void disableCrc();
69+
void enableInvertIQ();
70+
void disableInvertIQ();
71+
6972
void setOCP(uint8_t mA); // Over Current Protection control
7073

7174
// deprecated

0 commit comments

Comments
 (0)