|
@@ -0,0 +1,104 @@
|
|
1
|
+/*
|
|
2
|
+ * lora.cpp
|
|
3
|
+ *
|
|
4
|
+ * ESP8266 / ESP32 Environmental Sensor
|
|
5
|
+ *
|
|
6
|
+ * https://github.com/beegee-tokyo/SX126x-Arduino/blob/master/examples/PingPongPio/src/main.cpp
|
|
7
|
+ *
|
|
8
|
+ * ----------------------------------------------------------------------------
|
|
9
|
+ * "THE BEER-WARE LICENSE" (Revision 42):
|
|
10
|
+ * <xythobuz@xythobuz.de> wrote this file. As long as you retain this notice
|
|
11
|
+ * you can do whatever you want with this stuff. If we meet some day, and you
|
|
12
|
+ * think this stuff is worth it, you can buy me a beer in return. Thomas Buck
|
|
13
|
+ * ----------------------------------------------------------------------------
|
|
14
|
+ */
|
|
15
|
+
|
|
16
|
+#ifdef FEATURE_LORA
|
|
17
|
+
|
|
18
|
+#include <Arduino.h>
|
|
19
|
+#include <SX126x-Arduino.h>
|
|
20
|
+
|
|
21
|
+#include "config.h"
|
|
22
|
+#include "DebugLog.h"
|
|
23
|
+#include "lora.h"
|
|
24
|
+
|
|
25
|
+// Define LoRa parameters
|
|
26
|
+#define RF_FREQUENCY 868000000 // Hz
|
|
27
|
+#define TX_OUTPUT_POWER 22 // dBm
|
|
28
|
+#define LORA_BANDWIDTH 0 // [0: 125 kHz, 1: 250 kHz, 2: 500 kHz, 3: Reserved]
|
|
29
|
+#define LORA_SPREADING_FACTOR 7 // [SF7..SF12]
|
|
30
|
+#define LORA_CODINGRATE 1 // [1: 4/5, 2: 4/6, 3: 4/7, 4: 4/8]
|
|
31
|
+#define LORA_PREAMBLE_LENGTH 8 // Same for Tx and Rx
|
|
32
|
+#define LORA_SYMBOL_TIMEOUT 0 // Symbols
|
|
33
|
+#define LORA_FIX_LENGTH_PAYLOAD_ON false
|
|
34
|
+#define LORA_IQ_INVERSION_ON false
|
|
35
|
+#define RX_TIMEOUT_VALUE 3000
|
|
36
|
+#define TX_TIMEOUT_VALUE 3000
|
|
37
|
+
|
|
38
|
+#define BUFFER_SIZE 64 // Define the payload size here
|
|
39
|
+
|
|
40
|
+static hw_config hwConfig;
|
|
41
|
+static RadioEvents_t RadioEvents;
|
|
42
|
+
|
|
43
|
+void lora_init(void) {
|
|
44
|
+ // Define the HW configuration between MCU and SX126x
|
|
45
|
+ hwConfig.CHIP_TYPE = SX1262_CHIP;
|
|
46
|
+
|
|
47
|
+ // TODO pin config!
|
|
48
|
+ hwConfig.PIN_LORA_RESET = 4;
|
|
49
|
+ hwConfig.PIN_LORA_NSS = 5;
|
|
50
|
+ hwConfig.PIN_LORA_SCLK = 18;
|
|
51
|
+ hwConfig.PIN_LORA_MISO = 19;
|
|
52
|
+ hwConfig.PIN_LORA_DIO_1 = 21;
|
|
53
|
+ hwConfig.PIN_LORA_BUSY = 22;
|
|
54
|
+ hwConfig.PIN_LORA_MOSI = 23;
|
|
55
|
+ hwConfig.RADIO_TXEN = 26;
|
|
56
|
+ hwConfig.RADIO_RXEN = 27;
|
|
57
|
+
|
|
58
|
+ hwConfig.USE_DIO2_ANT_SWITCH = true;
|
|
59
|
+ hwConfig.USE_DIO3_TCXO = true;
|
|
60
|
+ hwConfig.USE_DIO3_ANT_SWITCH = false;
|
|
61
|
+
|
|
62
|
+ // Initialize the LoRa chip
|
|
63
|
+ debug.println("Starting lora_hardware_init");
|
|
64
|
+ lora_hardware_init(hwConfig);
|
|
65
|
+
|
|
66
|
+ // Initialize the Radio callbacks
|
|
67
|
+ RadioEvents.TxDone = OnTxDone;
|
|
68
|
+ RadioEvents.RxDone = OnRxDone;
|
|
69
|
+ RadioEvents.TxTimeout = OnTxTimeout;
|
|
70
|
+ RadioEvents.RxTimeout = OnRxTimeout;
|
|
71
|
+ RadioEvents.RxError = OnRxError;
|
|
72
|
+ RadioEvents.CadDone = OnCadDone;
|
|
73
|
+
|
|
74
|
+ // Initialize the Radio
|
|
75
|
+ Radio.Init(&RadioEvents);
|
|
76
|
+
|
|
77
|
+ // Set Radio channel
|
|
78
|
+ Radio.SetChannel(RF_FREQUENCY);
|
|
79
|
+
|
|
80
|
+ // Set Radio TX configuration
|
|
81
|
+ Radio.SetTxConfig(MODEM_LORA, TX_OUTPUT_POWER, 0, LORA_BANDWIDTH,
|
|
82
|
+ LORA_SPREADING_FACTOR, LORA_CODINGRATE,
|
|
83
|
+ LORA_PREAMBLE_LENGTH, LORA_FIX_LENGTH_PAYLOAD_ON,
|
|
84
|
+ true, 0, 0, LORA_IQ_INVERSION_ON, TX_TIMEOUT_VALUE);
|
|
85
|
+
|
|
86
|
+ // Set Radio RX configuration
|
|
87
|
+ Radio.SetRxConfig(MODEM_LORA, LORA_BANDWIDTH, LORA_SPREADING_FACTOR,
|
|
88
|
+ LORA_CODINGRATE, 0, LORA_PREAMBLE_LENGTH,
|
|
89
|
+ LORA_SYMBOL_TIMEOUT, LORA_FIX_LENGTH_PAYLOAD_ON,
|
|
90
|
+ 0, true, 0, 0, LORA_IQ_INVERSION_ON, true);
|
|
91
|
+
|
|
92
|
+ // Start LoRa
|
|
93
|
+ debug.println("Starting Radio.Rx");
|
|
94
|
+ Radio.Rx(RX_TIMEOUT_VALUE);
|
|
95
|
+}
|
|
96
|
+
|
|
97
|
+void lora_run(void) {
|
|
98
|
+#ifdef ARDUINO_ARCH_ESP8266
|
|
99
|
+ // Handle Radio events
|
|
100
|
+ Radio.IrqProcess();
|
|
101
|
+#endif // ARDUINO_ARCH_ESP8266
|
|
102
|
+}
|
|
103
|
+
|
|
104
|
+#endif // FEATURE_LORA
|