/
sx1262_test_main.c
351 lines (288 loc) · 11.2 KB
/
sx1262_test_main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
/****************************************************************************
* apps/examples/sx1262_test/sx1262_test_main.c
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership. The
* ASF licenses this file to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the
* License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*
****************************************************************************/
// Demo Program for LoRa SX1262 on NuttX
#include <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <unistd.h>
#include "../libs/libsx1262/include/radio.h"
#include "../libs/libsx1262/include/sx126x-board.h"
/// TODO: We are using LoRa Frequency 923 MHz for Singapore. Change this for your region.
#define USE_BAND_923
#if defined(USE_BAND_433)
#define RF_FREQUENCY 434000000 /* Hz */
#elif defined(USE_BAND_780)
#define RF_FREQUENCY 780000000 /* Hz */
#elif defined(USE_BAND_868)
#define RF_FREQUENCY 868000000 /* Hz */
#elif defined(USE_BAND_915)
#define RF_FREQUENCY 915000000 /* Hz */
#elif defined(USE_BAND_923)
#define RF_FREQUENCY 923000000 /* Hz */
#else
#error "Please define a frequency band in the compiler options."
#endif
/// LoRa Parameters
#define LORAPING_TX_OUTPUT_POWER 14 /* dBm */
#define LORAPING_BANDWIDTH 0 /* [0: 125 kHz, */
/* 1: 250 kHz, */
/* 2: 500 kHz, */
/* 3: Reserved] */
#define LORAPING_SPREADING_FACTOR 7 /* [SF7..SF12] */
#define LORAPING_CODINGRATE 1 /* [1: 4/5, */
/* 2: 4/6, */
/* 3: 4/7, */
/* 4: 4/8] */
#define LORAPING_PREAMBLE_LENGTH 8 /* Same for Tx and Rx */
#define LORAPING_SYMBOL_TIMEOUT 5 /* Symbols */
#define LORAPING_FIX_LENGTH_PAYLOAD_ON false
#define LORAPING_IQ_INVERSION_ON false
#define LORAPING_TX_TIMEOUT_MS 3000 /* ms */
#define LORAPING_RX_TIMEOUT_MS 10000 /* ms */
#define LORAPING_BUFFER_SIZE 64 /* LoRa message size */
const uint8_t loraping_ping_msg[] = "PING"; // We send a "PING" message
const uint8_t loraping_pong_msg[] = "PONG"; // We expect a "PONG" response
static uint8_t loraping_buffer[LORAPING_BUFFER_SIZE]; // 64-byte buffer for our LoRa messages
static int loraping_rx_size;
/// LoRa Statistics
struct {
int rx_timeout;
int rx_ping;
int rx_pong;
int rx_other;
int rx_error;
int tx_timeout;
int tx_success;
} loraping_stats;
///////////////////////////////////////////////////////////////////////////////
// Main Function
static void read_registers(void);
static void init_driver(void);
static void send_message(void);
static void receive_message(void);
int main(int argc, FAR char *argv[]) {
// Uncomment to read SX1262 registers
// #define READ_REGISTERS
#ifdef READ_REGISTERS
// Read SX1262 registers 0x00 to 0x0F
read_registers();
#endif // READ_REGISTERS
// Init SX1262 driver
init_driver();
// TODO: Do we need to wait?
sleep(1);
// Uncomment to send a LoRa message
#define SEND_MESSAGE
#ifdef SEND_MESSAGE
// Send a LoRa message
send_message();
#endif // SEND_MESSAGE
// Uncomment to receive a LoRa message
// #define RECEIVE_MESSAGE
#ifdef RECEIVE_MESSAGE
// Handle LoRa events for the next 10 seconds
for (int i = 0; i < 10; i++) {
// Prepare to receive a LoRa message
receive_message();
// Process the received LoRa message, if any
RadioOnDioIrq(NULL);
// Sleep for 1 second
usleep(1000 * 1000);
}
#endif // RECEIVE_MESSAGE
// TODO: Close the SPI Bus
// close(spi);
puts("Done!");
return 0;
UNUSED(read_registers);
UNUSED(send_message);
UNUSED(receive_message);
}
///////////////////////////////////////////////////////////////////////////////
// LoRa Commands
static void send_once(int is_ping);
static void on_tx_done(void);
static void on_rx_done(uint8_t *payload, uint16_t size, int16_t rssi, int8_t snr);
static void on_tx_timeout(void);
static void on_rx_timeout(void);
static void on_rx_error(void);
/// Read SX1262 registers
static void read_registers(void) {
puts("read_registers");
// Init the SPI port
SX126xIoInit();
// Read and print the first 16 registers: 0 to 15
for (uint16_t addr = 0; addr < 0x10; addr++) {
// Read the register
uint8_t val = SX126xReadRegister(addr); // For SX1262
// uint8_t val = SX1276Read(addr); // For SX1276
// Print the register value
printf("Register 0x%02x = 0x%02x\n", addr, val);
}
}
/// Initialise the SX1262 driver.
/// Assume that create_task has been called to init the Event Queue.
static void init_driver(void) {
puts("init_driver");
// Set the LoRa Callback Functions
RadioEvents_t radio_events;
memset(&radio_events, 0, sizeof(radio_events)); // Must init radio_events to null, because radio_events lives on stack!
radio_events.TxDone = on_tx_done;
radio_events.RxDone = on_rx_done;
radio_events.TxTimeout = on_tx_timeout;
radio_events.RxTimeout = on_rx_timeout;
radio_events.RxError = on_rx_error;
// Init the SPI Port and the LoRa Transceiver
Radio.Init(&radio_events);
// Set the LoRa Frequency
Radio.SetChannel(RF_FREQUENCY);
// Configure the LoRa Transceiver for transmitting messages
Radio.SetTxConfig(
MODEM_LORA,
LORAPING_TX_OUTPUT_POWER,
0, // Frequency deviation: Unused with LoRa
LORAPING_BANDWIDTH,
LORAPING_SPREADING_FACTOR,
LORAPING_CODINGRATE,
LORAPING_PREAMBLE_LENGTH,
LORAPING_FIX_LENGTH_PAYLOAD_ON,
true, // CRC enabled
0, // Frequency hopping disabled
0, // Hop period: N/A
LORAPING_IQ_INVERSION_ON,
LORAPING_TX_TIMEOUT_MS
);
// Configure the LoRa Transceiver for receiving messages
Radio.SetRxConfig(
MODEM_LORA,
LORAPING_BANDWIDTH,
LORAPING_SPREADING_FACTOR,
LORAPING_CODINGRATE,
0, // AFC bandwidth: Unused with LoRa
LORAPING_PREAMBLE_LENGTH,
LORAPING_SYMBOL_TIMEOUT,
LORAPING_FIX_LENGTH_PAYLOAD_ON,
0, // Fixed payload length: N/A
true, // CRC enabled
0, // Frequency hopping disabled
0, // Hop period: N/A
LORAPING_IQ_INVERSION_ON,
true // Continuous receive mode
);
}
/// Send a LoRa message. Assume that SX1262 driver has been initialised.
static void send_message(void) {
puts("send_message");
// Send the "PING" message
send_once(1);
}
/// Send a LoRa message. If is_ping is 0, send "PONG". Otherwise send "PING".
static void send_once(int is_ping) {
// Copy the "PING" or "PONG" message to the transmit buffer
if (is_ping) {
memcpy(loraping_buffer, loraping_ping_msg, 4);
} else {
memcpy(loraping_buffer, loraping_pong_msg, 4);
}
// Fill up the remaining space in the transmit buffer (64 bytes) with values 0, 1, 2, ...
for (int i = 4; i < sizeof loraping_buffer; i++) {
loraping_buffer[i] = i - 4;
}
// We send the transmit buffer (64 bytes)
Radio.Send(loraping_buffer, sizeof loraping_buffer);
}
/// Receive a LoRa message. Assume that SX1262 driver has been initialised.
/// Assume that create_task has been called to init the Event Queue.
static void receive_message(void) {
puts("receive_message");
// Receive a LoRa message within the timeout period
Radio.Rx(LORAPING_RX_TIMEOUT_MS);
}
///////////////////////////////////////////////////////////////////////////////
// LoRa Callback Functions
/// Callback Function that is called when our LoRa message has been transmitted
static void on_tx_done(void)
{
puts("Tx done");
// Log the success status
loraping_stats.tx_success++;
// Switch the LoRa Transceiver to low power, sleep mode
Radio.Sleep();
// TODO: Receive a "PING" or "PONG" LoRa message
// os_eventq_put(os_eventq_dflt_get(), &loraping_ev_rx);
}
/// Callback Function that is called when a LoRa message has been received
static void on_rx_done(
uint8_t *payload, // Buffer containing received LoRa message
uint16_t size, // Size of the LoRa message
int16_t rssi, // Signal strength
int8_t snr) // Signal To Noise ratio
{
puts("Rx done:");
// Switch the LoRa Transceiver to low power, sleep mode
Radio.Sleep();
// Copy the received packet
if (size > sizeof loraping_buffer) {
size = sizeof loraping_buffer;
}
loraping_rx_size = size;
memcpy(loraping_buffer, payload, size);
// Log the signal strength, signal to noise ratio
// TODO: loraping_rxinfo_rxed(rssi, snr);
// Dump the contents of the received packet
for (int i = 0; i < loraping_rx_size; i++) {
printf("%02x ", loraping_buffer[i]);
}
puts("");
// TODO: Send a "PING" or "PONG" LoRa message
// os_eventq_put(os_eventq_dflt_get(), &loraping_ev_tx);
}
/// Callback Function that is called when our LoRa message couldn't be transmitted due to timeout
static void on_tx_timeout(void) {
puts("Tx timeout");
// Switch the LoRa Transceiver to low power, sleep mode
Radio.Sleep();
// Log the timeout
loraping_stats.tx_timeout++;
// TODO: Receive a "PING" or "PONG" LoRa message
// os_eventq_put(os_eventq_dflt_get(), &loraping_ev_rx);
}
/// Callback Function that is called when no LoRa messages could be received due to timeout
static void on_rx_timeout(void) {
puts("Rx timeout");
// Switch the LoRa Transceiver to low power, sleep mode
Radio.Sleep();
// Log the timeout
loraping_stats.rx_timeout++;
// TODO: loraping_rxinfo_timeout();
// TODO: Send a "PING" or "PONG" LoRa message
// os_eventq_put(os_eventq_dflt_get(), &loraping_ev_tx);
}
/// Callback Function that is called when we couldn't receive a LoRa message due to error
static void on_rx_error(void) {
puts("Rx error");
// Log the error
loraping_stats.rx_error++;
// Switch the LoRa Transceiver to low power, sleep mode
Radio.Sleep();
// TODO: Send a "PING" or "PONG" LoRa message
// os_eventq_put(os_eventq_dflt_get(), &loraping_ev_tx);
}