-
Notifications
You must be signed in to change notification settings - Fork 332
Expand file tree
/
Copy pathloraspi.c
More file actions
1079 lines (942 loc) · 37.8 KB
/
loraspi.c
File metadata and controls
1079 lines (942 loc) · 37.8 KB
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* loraspi.c — Native SPI LoRa driver for Dire Wolf.
*
* Supports SX1276/SX1278 (RFM95W) and SX1262/SX1268 LoRa modules wired
* directly to a Raspberry Pi via SPI and GPIO.
*
* Hardware is selected by the LORAHW directive in direwolf.conf, which
* maps to a built-in profile table (mirrors hardware_profiles.yaml).
*
* RF parameters (LORAFREQ, LORASF, LORABW, LORACR, LORASW, LORATXPOWER)
* are read from the per-channel config populated by config.c.
*
* LoRa APRS air format:
* 0x3C 0xFF 0x01 <TNC2 text>
* The preamble is stripped on receive and prepended on transmit.
*
* Architecture:
* loraspi_init() — called once from direwolf.c at startup
* for each MEDIUM_LORA channel:
* open spidev, export/configure GPIO pins
* initialise LoRa chip (reset, configure modem params)
* start rx_thread — polls for received packets
* start tx_thread — drains the tx_queue
*
* loraspi_send_packet() — called from tq.c
* converts AX.25 packet → TNC2 text
* pushes to per-channel tx_queue
*
* GPIO is accessed via Linux sysfs (/sys/class/gpio/) — no extra
* library dependency beyond a standard Linux kernel.
*/
#include "direwolf.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <stdbool.h>
#include <unistd.h>
#include <fcntl.h>
#include <errno.h>
#include <time.h>
#include <pthread.h>
#include <sys/ioctl.h>
#include <linux/spi/spidev.h>
#include <math.h>
#include "textcolor.h"
#include "audio.h"
#include "ax25_pad.h"
#include "dlq.h"
#include "loraspi.h"
/* =========================================================================
* Compile guard — Linux SPI/GPIO only
* ========================================================================= */
#ifndef __linux__
void loraspi_init (struct audio_s *pa) { (void)pa; }
void loraspi_send_packet (int chan, packet_t pp) { (void)chan; (void)pp; }
int loraspi_apply_profile (int chan, const char *name, struct audio_s *pa) { (void)chan; (void)name; (void)pa; return -1; }
#else
/* =========================================================================
* TX queue
* ========================================================================= */
#define TX_QUEUE_DEPTH 8
typedef struct {
uint8_t data[256];
int len;
} tx_item_t;
typedef struct {
tx_item_t items[TX_QUEUE_DEPTH];
int head, tail, count;
pthread_mutex_t lock;
pthread_cond_t cond;
} tx_queue_t;
static void txq_init (tx_queue_t *q) {
memset(q, 0, sizeof(*q));
pthread_mutex_init(&q->lock, NULL);
pthread_cond_init (&q->cond, NULL);
}
static bool txq_push (tx_queue_t *q, const uint8_t *data, int len) {
pthread_mutex_lock(&q->lock);
if (q->count >= TX_QUEUE_DEPTH) {
pthread_mutex_unlock(&q->lock);
return false;
}
tx_item_t *it = &q->items[q->tail];
memcpy(it->data, data, len);
it->len = len;
q->tail = (q->tail + 1) % TX_QUEUE_DEPTH;
q->count++;
pthread_cond_signal(&q->cond);
pthread_mutex_unlock(&q->lock);
return true;
}
static bool txq_pop (tx_queue_t *q, uint8_t *data, int *len) {
pthread_mutex_lock(&q->lock);
while (q->count == 0)
pthread_cond_wait(&q->cond, &q->lock);
tx_item_t *it = &q->items[q->head];
memcpy(data, it->data, it->len);
*len = it->len;
q->head = (q->head + 1) % TX_QUEUE_DEPTH;
q->count--;
pthread_mutex_unlock(&q->lock);
return true;
}
/* =========================================================================
* Per-channel state
* ========================================================================= */
typedef struct {
int chan; /* Dire Wolf channel number */
int chip; /* LORA_CHIP_SX1276 or LORA_CHIP_SX1262 */
int spi_fd; /* /dev/spidev<bus>.<dev> */
/* GPIO pin numbers (-1 = not used) */
int pin_cs;
int pin_reset;
int pin_irq; /* DIO0 (SX1276) or DIO1 (SX1262) */
int pin_busy; /* SX1262 only */
int pin_tx_en;
int pin_rx_en;
/* RF config */
float freq_mhz;
int sf;
int bw_khz;
int cr; /* coding rate denominator: 5..8 */
int sw; /* sync word */
int txpower; /* dBm */
bool pa_boost; /* SX1276: PA_BOOST vs RFO */
bool tcxo;
float tcxo_voltage;
/* Mutex protecting SPI bus — shared by rx_thread and tx_thread */
pthread_mutex_t spi_lock;
/* Threads */
pthread_t rx_thread;
pthread_t tx_thread;
tx_queue_t txq;
volatile bool running;
} lora_chan_t;
#define MAX_LORA_CHANS 4
static lora_chan_t s_lora[MAX_LORA_CHANS];
static int s_lora_count = 0;
/* =========================================================================
* Hardware profile table (mirrors hardware_profiles.yaml)
* ========================================================================= */
typedef struct {
const char *name;
int chip; /* LORA_CHIP_* */
int spi_bus;
int spi_dev;
int pin_cs;
int pin_reset;
int pin_irq;
int pin_busy;
int pin_tx_en;
int pin_rx_en;
bool pa_boost;
bool tcxo;
float tcxo_voltage;
} hw_profile_t;
static const hw_profile_t s_profiles[] = {
/* name chip bus dev cs rst irq busy txen rxen pa_boost tcxo tcxo_v */
{ "meshadv", LORA_CHIP_SX1262, 0, 0, 21, 18, 16, 20, 13, 12, false, true, 1.8f },
{ "e22_900m30s", LORA_CHIP_SX1262, 0, 0, 8, 25, 24, 23, -1, 22, false, true, 1.8f },
{ "e22_400m30s", LORA_CHIP_SX1262, 0, 0, 8, 25, 24, 23, 17, 27, false, true, 1.8f },
{ "ebyte_e22", LORA_CHIP_SX1262, 0, 0, 8, 25, 24, 23, -1, -1, false, true, 1.8f },
{ "lorapi_rfm95w", LORA_CHIP_SX1276, 0, 1, 7, 22, -1, -1, -1, -1, true, false, 0.0f },
{ "lorapi_rfm98w", LORA_CHIP_SX1276, 0, 1, 7, 22, -1, -1, -1, -1, true, false, 0.0f },
{ "generic_sx1276", LORA_CHIP_SX1276, 0, 0, 8, 25, 24, -1, -1, -1, true, false, 0.0f },
{ NULL, 0, 0, 0, 0, 0, 0, 0, 0, 0, false, false, 0.0f }
};
static const hw_profile_t *find_profile (const char *name) {
for (const hw_profile_t *p = s_profiles; p->name; p++)
if (strcasecmp(p->name, name) == 0)
return p;
return NULL;
}
/* =========================================================================
* GPIO via sysfs (/sys/class/gpio/)
* ========================================================================= */
static void gpio_export (int pin) {
char path[64];
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d", pin);
if (access(path, F_OK) == 0) return; /* already exported */
int fd = open("/sys/class/gpio/export", O_WRONLY);
if (fd < 0) return;
char buf[8];
snprintf(buf, sizeof(buf), "%d", pin);
(void)write(fd, buf, strlen(buf));
close(fd);
usleep(50000); /* wait for udev to set permissions */
}
static void gpio_direction (int pin, const char *dir) {
char path[80];
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/direction", pin);
int fd = open(path, O_WRONLY);
if (fd < 0) return;
(void)write(fd, dir, strlen(dir));
close(fd);
}
static void gpio_write (int pin, int val) {
char path[80];
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
int fd = open(path, O_WRONLY);
if (fd < 0) return;
(void)write(fd, val ? "1" : "0", 1);
close(fd);
}
static int gpio_read (int pin) {
char path[80];
snprintf(path, sizeof(path), "/sys/class/gpio/gpio%d/value", pin);
int fd = open(path, O_RDONLY);
if (fd < 0) return 0;
char c = '0';
(void)read(fd, &c, 1);
close(fd);
return c == '1';
}
static void gpio_setup_out (int pin, int initial) {
if (pin < 0) return;
gpio_export(pin);
gpio_direction(pin, "out");
gpio_write(pin, initial);
}
static void gpio_setup_in (int pin) {
if (pin < 0) return;
gpio_export(pin);
gpio_direction(pin, "in");
}
/* =========================================================================
* SPI via spidev
* ========================================================================= */
static int spi_open (int bus, int dev, uint32_t speed_hz) {
char path[32];
snprintf(path, sizeof(path), "/dev/spidev%d.%d", bus, dev);
int fd = open(path, O_RDWR);
if (fd < 0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("loraspi: cannot open %s: %s\n", path, strerror(errno));
return -1;
}
uint8_t mode = SPI_MODE_0;
uint8_t bits = 8;
ioctl(fd, SPI_IOC_WR_MODE, &mode);
ioctl(fd, SPI_IOC_WR_BITS_PER_WORD, &bits);
ioctl(fd, SPI_IOC_WR_MAX_SPEED_HZ, &speed_hz);
return fd;
}
static void spi_transfer (int fd, int cs_pin, const uint8_t *tx, uint8_t *rx, int len) {
if (cs_pin >= 0) gpio_write(cs_pin, 0);
struct spi_ioc_transfer t = {
.tx_buf = (unsigned long)tx,
.rx_buf = (unsigned long)rx,
.len = (uint32_t)len,
.speed_hz = 0,
.delay_usecs = 0,
.bits_per_word = 8,
};
ioctl(fd, SPI_IOC_MESSAGE(1), &t);
if (cs_pin >= 0) gpio_write(cs_pin, 1);
}
/* =========================================================================
* SX1276 register-level driver
* ========================================================================= */
/* Register addresses */
#define SX1276_REG_FIFO 0x00
#define SX1276_REG_OP_MODE 0x01
#define SX1276_REG_FRF_MSB 0x06
#define SX1276_REG_FRF_MID 0x07
#define SX1276_REG_FRF_LSB 0x08
#define SX1276_REG_PA_CONFIG 0x09
#define SX1276_REG_PA_DAC 0x4D
#define SX1276_REG_LNA 0x0C
#define SX1276_REG_FIFO_ADDR_PTR 0x0D
#define SX1276_REG_FIFO_TX_BASE 0x0E
#define SX1276_REG_FIFO_RX_BASE 0x0F
#define SX1276_REG_FIFO_RX_CURRENT 0x10
#define SX1276_REG_IRQ_FLAGS_MASK 0x11
#define SX1276_REG_IRQ_FLAGS 0x12
#define SX1276_REG_RX_NB_BYTES 0x13
#define SX1276_REG_MODEM_CONFIG1 0x1D
#define SX1276_REG_MODEM_CONFIG2 0x1E
#define SX1276_REG_SYMB_TIMEOUT_LSB 0x1F
#define SX1276_REG_PREAMBLE_MSB 0x20
#define SX1276_REG_PREAMBLE_LSB 0x21
#define SX1276_REG_PAYLOAD_LENGTH 0x22
#define SX1276_REG_MAX_PAYLOAD 0x23
#define SX1276_REG_MODEM_CONFIG3 0x26
#define SX1276_REG_RSSI_VALUE 0x1A
#define SX1276_REG_PKT_SNR 0x19
#define SX1276_REG_PKT_RSSI 0x1A
#define SX1276_REG_DETECT_OPTIMIZE 0x31
#define SX1276_REG_INVERT_IQ 0x33
#define SX1276_REG_DETECT_THRESH 0x37
#define SX1276_REG_SYNC_WORD 0x39
#define SX1276_REG_DIO_MAPPING1 0x40
#define SX1276_REG_VERSION 0x42
/* OP_MODE values (LoRa mode bit 7 set) */
#define SX1276_MODE_SLEEP 0x80
#define SX1276_MODE_STDBY 0x81
#define SX1276_MODE_TX 0x83
#define SX1276_MODE_RX_CONT 0x85
#define SX1276_MODE_RX_SINGLE 0x86
/* IRQ flags */
#define SX1276_IRQ_RX_DONE 0x40
#define SX1276_IRQ_TX_DONE 0x08
#define SX1276_IRQ_CRC_ERR 0x20
#define SX1276_IRQ_VALID_HDR 0x10
static uint8_t sx1276_read_reg (lora_chan_t *lc, uint8_t reg) {
uint8_t tx[2] = { reg & 0x7F, 0x00 };
uint8_t rx[2] = { 0, 0 };
spi_transfer(lc->spi_fd, lc->pin_cs, tx, rx, 2);
return rx[1];
}
static void sx1276_write_reg (lora_chan_t *lc, uint8_t reg, uint8_t val) {
uint8_t tx[2] = { reg | 0x80, val };
uint8_t rx[2];
spi_transfer(lc->spi_fd, lc->pin_cs, tx, rx, 2);
}
static void sx1276_reset (lora_chan_t *lc) {
if (lc->pin_reset < 0) return;
gpio_write(lc->pin_reset, 0);
usleep(10000);
gpio_write(lc->pin_reset, 1);
usleep(10000);
}
static bool sx1276_init (lora_chan_t *lc) {
sx1276_reset(lc);
uint8_t ver = sx1276_read_reg(lc, SX1276_REG_VERSION);
if (ver != 0x12) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("loraspi: SX1276 not found on channel %d (version=0x%02X, expected 0x12)\n",
lc->chan, ver);
return false;
}
/* Switch to LoRa sleep mode */
sx1276_write_reg(lc, SX1276_REG_OP_MODE, SX1276_MODE_SLEEP);
usleep(10000);
/* Set frequency */
uint64_t frf = (uint64_t)((double)lc->freq_mhz * 1e6 / 32e6 * (double)(1ULL << 19));
sx1276_write_reg(lc, SX1276_REG_FRF_MSB, (frf >> 16) & 0xFF);
sx1276_write_reg(lc, SX1276_REG_FRF_MID, (frf >> 8) & 0xFF);
sx1276_write_reg(lc, SX1276_REG_FRF_LSB, frf & 0xFF);
/* BW: 7.8=0 10.4=1 15.6=2 20.8=3 31.25=4 41.7=5 62.5=6 125=7 250=8 500=9 */
int bw_code;
if (lc->bw_khz <= 8) bw_code = 0;
else if (lc->bw_khz <= 11) bw_code = 1;
else if (lc->bw_khz <= 16) bw_code = 2;
else if (lc->bw_khz <= 21) bw_code = 3;
else if (lc->bw_khz <= 32) bw_code = 4;
else if (lc->bw_khz <= 42) bw_code = 5;
else if (lc->bw_khz <= 63) bw_code = 6;
else if (lc->bw_khz <= 125) bw_code = 7;
else if (lc->bw_khz <= 250) bw_code = 8;
else bw_code = 9;
int cr_code = lc->cr - 4; /* 4/5=1 4/6=2 4/7=3 4/8=4 */
sx1276_write_reg(lc, SX1276_REG_MODEM_CONFIG1,
(bw_code << 4) | (cr_code << 1));
/* SF, CRC on, RX timeout MSB */
sx1276_write_reg(lc, SX1276_REG_MODEM_CONFIG2,
(lc->sf << 4) | 0x04); /* 0x04 = RX payload CRC on */
/* Low data rate optimise for SF11/SF12 at BW125 */
bool ldro = (lc->sf >= 11 && lc->bw_khz <= 125);
sx1276_write_reg(lc, SX1276_REG_MODEM_CONFIG3, ldro ? 0x08 : 0x00);
/* Preamble length = 8 */
sx1276_write_reg(lc, SX1276_REG_PREAMBLE_MSB, 0x00);
sx1276_write_reg(lc, SX1276_REG_PREAMBLE_LSB, 0x08);
/* Max payload */
sx1276_write_reg(lc, SX1276_REG_MAX_PAYLOAD, 0xFF);
/* Sync word */
sx1276_write_reg(lc, SX1276_REG_SYNC_WORD, (uint8_t)lc->sw);
/* LoRa detection optimisation */
sx1276_write_reg(lc, SX1276_REG_DETECT_OPTIMIZE, 0xC3);
sx1276_write_reg(lc, SX1276_REG_DETECT_THRESH, 0x0A);
/* Boosted LNA */
sx1276_write_reg(lc, SX1276_REG_LNA, 0x23);
/* TX power */
if (lc->pa_boost) {
if (lc->txpower > 17) {
sx1276_write_reg(lc, SX1276_REG_PA_DAC, 0x87);
sx1276_write_reg(lc, SX1276_REG_PA_CONFIG, 0xFF);
} else {
sx1276_write_reg(lc, SX1276_REG_PA_DAC, 0x84);
sx1276_write_reg(lc, SX1276_REG_PA_CONFIG,
0x80 | (uint8_t)(lc->txpower - 2));
}
} else {
/* RFO path: max 14 dBm */
int pwr = lc->txpower < 0 ? 0 : (lc->txpower > 14 ? 14 : lc->txpower);
sx1276_write_reg(lc, SX1276_REG_PA_CONFIG, (uint8_t)(0x70 | pwr));
}
/* FIFO base addresses */
sx1276_write_reg(lc, SX1276_REG_FIFO_TX_BASE, 0x00);
sx1276_write_reg(lc, SX1276_REG_FIFO_RX_BASE, 0x00);
/* Standby */
sx1276_write_reg(lc, SX1276_REG_OP_MODE, SX1276_MODE_STDBY);
usleep(10000);
/* Read back actual frequency from registers as a sanity check */
uint32_t frf_check = ((uint32_t)sx1276_read_reg(lc, SX1276_REG_FRF_MSB) << 16) |
((uint32_t)sx1276_read_reg(lc, SX1276_REG_FRF_MID) << 8) |
(uint32_t)sx1276_read_reg(lc, SX1276_REG_FRF_LSB);
double freq_actual = (double)frf_check * 32e6 / (double)(1ULL << 19);
if (fabs(freq_actual - (double)lc->freq_mhz * 1e6) > 1000.0) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("loraspi ch%d: WARNING frequency mismatch — config %.3f MHz, chip %.3f MHz\n",
lc->chan, lc->freq_mhz, freq_actual / 1e6);
}
return true;
}
static int sx1276_receive (lora_chan_t *lc, uint8_t *buf, int maxlen,
int *rssi_out, float *snr_out) {
uint8_t irq = sx1276_read_reg(lc, SX1276_REG_IRQ_FLAGS);
sx1276_write_reg(lc, SX1276_REG_IRQ_FLAGS, 0xFF); /* clear all */
if (!(irq & SX1276_IRQ_RX_DONE)) return 0;
if (irq & SX1276_IRQ_CRC_ERR) return -1;
int nb = sx1276_read_reg(lc, SX1276_REG_RX_NB_BYTES);
int ptr = sx1276_read_reg(lc, SX1276_REG_FIFO_RX_CURRENT);
if (nb <= 0 || nb > maxlen) return -1;
sx1276_write_reg(lc, SX1276_REG_FIFO_ADDR_PTR, (uint8_t)ptr);
/* Burst-read FIFO */
uint8_t tx_buf[257];
uint8_t rx_buf[257];
tx_buf[0] = SX1276_REG_FIFO & 0x7F;
memset(tx_buf + 1, 0, nb);
spi_transfer(lc->spi_fd, lc->pin_cs, tx_buf, rx_buf, nb + 1);
memcpy(buf, rx_buf + 1, nb);
/* SNR (signed byte, units = 0.25 dB) */
int8_t snr_raw = (int8_t)sx1276_read_reg(lc, SX1276_REG_PKT_SNR);
*snr_out = snr_raw / 4.0f;
/* RSSI (HF port 868/915 MHz uses -157, LF port 433 MHz uses -164) */
int rssi_offset = (lc->freq_mhz < 600.0f) ? -164 : -157;
*rssi_out = rssi_offset + sx1276_read_reg(lc, SX1276_REG_PKT_RSSI);
return nb;
}
static bool sx1276_transmit (lora_chan_t *lc, const uint8_t *data, int len) {
sx1276_write_reg(lc, SX1276_REG_OP_MODE, SX1276_MODE_STDBY);
usleep(1000);
sx1276_write_reg(lc, SX1276_REG_FIFO_ADDR_PTR, 0x00);
sx1276_write_reg(lc, SX1276_REG_PAYLOAD_LENGTH, (uint8_t)len);
/* Burst-write FIFO */
uint8_t tx_buf[257];
uint8_t rx_buf[257];
tx_buf[0] = SX1276_REG_FIFO | 0x80;
memcpy(tx_buf + 1, data, len);
spi_transfer(lc->spi_fd, lc->pin_cs, tx_buf, rx_buf, len + 1);
sx1276_write_reg(lc, SX1276_REG_OP_MODE, SX1276_MODE_TX);
/* Poll TX_DONE (timeout 10 s) */
for (int i = 0; i < 10000; i++) {
usleep(1000);
if (sx1276_read_reg(lc, SX1276_REG_IRQ_FLAGS) & SX1276_IRQ_TX_DONE) {
sx1276_write_reg(lc, SX1276_REG_IRQ_FLAGS, 0xFF);
return true;
}
}
sx1276_write_reg(lc, SX1276_REG_IRQ_FLAGS, 0xFF);
return false;
}
static void sx1276_start_rx (lora_chan_t *lc) {
sx1276_write_reg(lc, SX1276_REG_FIFO_RX_BASE, 0x00);
sx1276_write_reg(lc, SX1276_REG_FIFO_ADDR_PTR, 0x00);
sx1276_write_reg(lc, SX1276_REG_IRQ_FLAGS, 0xFF);
sx1276_write_reg(lc, SX1276_REG_OP_MODE, SX1276_MODE_RX_CONT);
}
/* =========================================================================
* SX1262 command-level driver
* ========================================================================= */
/* SX1262 command opcodes */
#define SX1262_CMD_SET_SLEEP 0x84
#define SX1262_CMD_SET_STANDBY 0x80
#define SX1262_CMD_SET_TX 0x83
#define SX1262_CMD_SET_RX 0x82
#define SX1262_CMD_SET_PACKET_TYPE 0x8A
#define SX1262_CMD_SET_RF_FREQUENCY 0x86
#define SX1262_CMD_SET_TX_PARAMS 0x8E
#define SX1262_CMD_SET_PA_CONFIG 0x95
#define SX1262_CMD_SET_MODULATION_PARAMS 0x8B
#define SX1262_CMD_SET_PACKET_PARAMS 0x8C
#define SX1262_CMD_SET_BUFFER_BASE_ADDR 0x8F
#define SX1262_CMD_GET_RX_BUFFER_STATUS 0x13
#define SX1262_CMD_GET_PACKET_STATUS 0x14
#define SX1262_CMD_READ_BUFFER 0x1E
#define SX1262_CMD_WRITE_BUFFER 0x0E
#define SX1262_CMD_WRITE_REGISTER 0x0D
#define SX1262_CMD_READ_REGISTER 0x1D
#define SX1262_CMD_SET_DIO2_RF_SWITCH 0x9D
#define SX1262_CMD_SET_DIO3_TCXO 0x97
#define SX1262_CMD_CALIBRATE_IMAGE 0x98
#define SX1262_CMD_CLR_IRQ_STATUS 0x02
#define SX1262_CMD_SET_IRQ_PARAMS 0x08
#define SX1262_CMD_GET_IRQ_STATUS 0x12
#define SX1262_IRQ_RX_DONE 0x0002
#define SX1262_IRQ_TX_DONE 0x0001
#define SX1262_IRQ_CRC_ERR 0x0040
static void sx1262_wait_busy (lora_chan_t *lc) {
if (lc->pin_busy < 0) return;
for (int i = 0; i < 10000; i++) {
if (!gpio_read(lc->pin_busy)) return;
usleep(100);
}
text_color_set(DW_COLOR_ERROR);
dw_printf ("loraspi: SX1262 BUSY timeout on channel %d\n", lc->chan);
}
static void sx1262_cmd (lora_chan_t *lc, const uint8_t *tx, uint8_t *rx, int len) {
sx1262_wait_busy(lc);
spi_transfer(lc->spi_fd, lc->pin_cs, tx, rx, len);
}
static void sx1262_write_reg (lora_chan_t *lc, uint16_t addr, uint8_t val) {
uint8_t tx[4] = { SX1262_CMD_WRITE_REGISTER,
(addr >> 8) & 0xFF, addr & 0xFF, val };
uint8_t rx[4];
sx1262_cmd(lc, tx, rx, 4);
}
static void sx1262_reset (lora_chan_t *lc) {
if (lc->pin_reset < 0) return;
gpio_write(lc->pin_reset, 0);
usleep(200);
gpio_write(lc->pin_reset, 1);
usleep(10000);
}
static bool sx1262_init (lora_chan_t *lc) {
sx1262_reset(lc);
/* Standby XOSC */
uint8_t cmd[2] = { SX1262_CMD_SET_STANDBY, 0x01 };
uint8_t rx[2];
sx1262_cmd(lc, cmd, rx, 2);
usleep(10000);
/* TCXO control on DIO3 */
if (lc->tcxo) {
/* Voltage codes: 1.6=0 1.7=1 1.8=2 2.2=3 2.4=4 2.7=5 3.0=6 3.3=7 */
uint8_t vcode = 2; /* default 1.8 V */
if (lc->tcxo_voltage < 1.65f) vcode = 0;
else if (lc->tcxo_voltage < 1.75f) vcode = 1;
else if (lc->tcxo_voltage < 2.0f) vcode = 2;
else if (lc->tcxo_voltage < 2.3f) vcode = 3;
else if (lc->tcxo_voltage < 2.55f) vcode = 4;
else if (lc->tcxo_voltage < 2.85f) vcode = 5;
else if (lc->tcxo_voltage < 3.15f) vcode = 6;
else vcode = 7;
uint8_t tcxo_cmd[5] = { SX1262_CMD_SET_DIO3_TCXO, vcode, 0x00, 0x00, 0x64 }; /* 100 * 15.625 us ≈ 1.5 ms */
uint8_t tcxo_rx[5];
sx1262_cmd(lc, tcxo_cmd, tcxo_rx, 5);
usleep(5000);
}
/* DIO2 as RF switch (handles TXEN/RXEN internally on most modules) */
uint8_t dio2_cmd[2] = { SX1262_CMD_SET_DIO2_RF_SWITCH, 0x01 };
uint8_t dio2_rx[2];
sx1262_cmd(lc, dio2_cmd, dio2_rx, 2);
/* Image calibration for the frequency band */
uint8_t cal_cmd[3] = { SX1262_CMD_CALIBRATE_IMAGE, 0, 0 };
if (lc->freq_mhz < 450.0f) { cal_cmd[1] = 0x6B; cal_cmd[2] = 0x6F; }
else if (lc->freq_mhz < 900.0f) { cal_cmd[1] = 0xC1; cal_cmd[2] = 0xC5; }
else { cal_cmd[1] = 0xD7; cal_cmd[2] = 0xDB; }
uint8_t cal_rx[3];
sx1262_cmd(lc, cal_cmd, cal_rx, 3);
usleep(5000);
/* LoRa packet type */
uint8_t pt[2] = { SX1262_CMD_SET_PACKET_TYPE, 0x01 };
uint8_t pt_rx[2];
sx1262_cmd(lc, pt, pt_rx, 2);
/* RF frequency */
uint32_t frf = (uint32_t)((double)lc->freq_mhz * 1e6 / 0.95367431640625);
uint8_t freq_cmd[5] = { SX1262_CMD_SET_RF_FREQUENCY,
(frf >> 24) & 0xFF, (frf >> 16) & 0xFF,
(frf >> 8) & 0xFF, frf & 0xFF };
uint8_t freq_rx[5];
sx1262_cmd(lc, freq_cmd, freq_rx, 5);
/* PA config for SX1262 (22 dBm max) */
uint8_t pa_cmd[5] = { SX1262_CMD_SET_PA_CONFIG, 0x04, 0x07, 0x00, 0x01 };
uint8_t pa_rx[5];
sx1262_cmd(lc, pa_cmd, pa_rx, 5);
/* TX params: power, ramp time (40 us = 0x04) */
int8_t pwr = (int8_t)(lc->txpower > 22 ? 22 : lc->txpower);
uint8_t tx_params[3] = { SX1262_CMD_SET_TX_PARAMS, (uint8_t)pwr, 0x04 };
uint8_t tx_rx[3];
sx1262_cmd(lc, tx_params, tx_rx, 3);
/* Modulation params: SF, BW, CR, LDRO */
/* BW codes: 7.81=0x00 10.42=0x08 15.63=0x01 20.83=0x09 31.25=0x02
41.67=0x0A 62.5=0x03 125=0x04 250=0x05 500=0x06 */
uint8_t bw_code;
if (lc->bw_khz <= 8) bw_code = 0x00;
else if (lc->bw_khz <= 11) bw_code = 0x08;
else if (lc->bw_khz <= 16) bw_code = 0x01;
else if (lc->bw_khz <= 21) bw_code = 0x09;
else if (lc->bw_khz <= 32) bw_code = 0x02;
else if (lc->bw_khz <= 42) bw_code = 0x0A;
else if (lc->bw_khz <= 63) bw_code = 0x03;
else if (lc->bw_khz <= 125) bw_code = 0x04;
else if (lc->bw_khz <= 250) bw_code = 0x05;
else bw_code = 0x06;
uint8_t cr_code = (uint8_t)(lc->cr - 4); /* 4/5=1 4/6=2 4/7=3 4/8=4 */
bool ldro = (lc->sf >= 11 && lc->bw_khz <= 125);
uint8_t mod[5] = { SX1262_CMD_SET_MODULATION_PARAMS,
(uint8_t)lc->sf, bw_code, cr_code, ldro ? 0x01 : 0x00 };
uint8_t mod_rx[5];
sx1262_cmd(lc, mod, mod_rx, 5);
/* Packet params: preamble=8, explicit header, max payload=255, CRC off, IQ normal */
uint8_t pkt[7] = { SX1262_CMD_SET_PACKET_PARAMS,
0x00, 0x08, /* preamble = 8 */
0x00, /* explicit header */
0xFF, /* max payload */
0x00, /* CRC off (LoRa APRS uses AX.25 FCS) */
0x00 }; /* standard IQ */
uint8_t pkt_rx[7];
sx1262_cmd(lc, pkt, pkt_rx, 7);
/* Sync word: SX1262 uses 2 bytes at 0x0740/0x0741.
* The SX1276-style single byte (e.g. 0x12) maps to SX1262 format by
* expanding each nibble: 0x12 -> 0x1424, 0x34 -> 0x3444.
* Formula: high = (sw >> 4 & 0x0F) << 4 | 0x04
* low = (sw & 0x0F) << 4 | 0x04 */
uint8_t sw_hi = (uint8_t)(((lc->sw >> 4) & 0x0F) << 4 | 0x04);
uint8_t sw_lo = (uint8_t)(( lc->sw & 0x0F) << 4 | 0x04);
sx1262_write_reg(lc, 0x0740, sw_hi);
sx1262_write_reg(lc, 0x0741, sw_lo);
/* Buffer base addresses: TX=0x00, RX=0x00 */
uint8_t buf_base[3] = { SX1262_CMD_SET_BUFFER_BASE_ADDR, 0x00, 0x00 };
uint8_t buf_rx[3];
sx1262_cmd(lc, buf_base, buf_rx, 3);
/* Enable RX_DONE and TX_DONE IRQs on DIO1 */
uint8_t irq_cmd[9] = { SX1262_CMD_SET_IRQ_PARAMS,
0x00, 0x43, /* IRQ mask: RX_DONE | TX_DONE | CRC_ERR */
0x00, 0x43, /* DIO1 mask */
0x00, 0x00, /* DIO2 mask */
0x00, 0x00 }; /* DIO3 mask */
uint8_t irq_rx[9];
sx1262_cmd(lc, irq_cmd, irq_rx, 9);
return true;
}
static int sx1262_receive (lora_chan_t *lc, uint8_t *buf, int maxlen,
int *rssi_out, float *snr_out) {
/* Read and clear IRQ status */
uint8_t get_irq[4] = { SX1262_CMD_GET_IRQ_STATUS, 0, 0, 0 };
uint8_t irq_rx[4];
sx1262_cmd(lc, get_irq, irq_rx, 4);
uint16_t irq = ((uint16_t)irq_rx[2] << 8) | irq_rx[3];
uint8_t clr[3] = { SX1262_CMD_CLR_IRQ_STATUS, 0xFF, 0xFF };
uint8_t clr_rx[3];
sx1262_cmd(lc, clr, clr_rx, 3);
if (!(irq & SX1262_IRQ_RX_DONE)) return 0;
if (irq & SX1262_IRQ_CRC_ERR) return -1;
/* Get payload length and buffer offset */
uint8_t get_buf[4] = { SX1262_CMD_GET_RX_BUFFER_STATUS, 0, 0, 0 };
uint8_t buf_rx[4];
sx1262_cmd(lc, get_buf, buf_rx, 4);
int nb = buf_rx[2];
int offset = buf_rx[3];
if (nb <= 0 || nb > maxlen) return -1;
/* Read buffer (fixed max 256 bytes payload) */
uint8_t rd_cmd[256 + 3];
uint8_t rd_rx[256 + 3];
rd_cmd[0] = SX1262_CMD_READ_BUFFER;
rd_cmd[1] = (uint8_t)offset;
rd_cmd[2] = 0x00; /* NOP status byte */
memset(rd_cmd + 3, 0, nb);
sx1262_cmd(lc, rd_cmd, rd_rx, nb + 3);
memcpy(buf, rd_rx + 3, nb);
/* Packet status: RSSI, SNR */
uint8_t ps_cmd[4] = { SX1262_CMD_GET_PACKET_STATUS, 0, 0, 0 };
uint8_t ps_rx[4];
sx1262_cmd(lc, ps_cmd, ps_rx, 4);
*rssi_out = -(ps_rx[1] / 2);
*snr_out = (int8_t)ps_rx[2] / 4.0f;
return nb;
}
static bool sx1262_transmit (lora_chan_t *lc, const uint8_t *data, int len) {
/* Write buffer (fixed max 256 bytes payload) */
uint8_t wr_cmd[256 + 2];
uint8_t wr_rx[256 + 2];
if (len > 256) return false;
wr_cmd[0] = SX1262_CMD_WRITE_BUFFER;
wr_cmd[1] = 0x00; /* offset */
memcpy(wr_cmd + 2, data, len);
sx1262_cmd(lc, wr_cmd, wr_rx, len + 2);
/* Packet params: set actual payload length */
uint8_t pkt[7] = { SX1262_CMD_SET_PACKET_PARAMS,
0x00, 0x08, 0x00, (uint8_t)len, 0x00, 0x00 };
uint8_t pkt_rx[7];
sx1262_cmd(lc, pkt, pkt_rx, 7);
/* SetTx: timeout=0 (no timeout) */
uint8_t tx_cmd[4] = { SX1262_CMD_SET_TX, 0x00, 0x00, 0x00 };
uint8_t tx_rx[4];
sx1262_cmd(lc, tx_cmd, tx_rx, 4);
/* Poll TX_DONE via IRQ (timeout 10 s) */
for (int i = 0; i < 10000; i++) {
usleep(1000);
uint8_t gi[4] = { SX1262_CMD_GET_IRQ_STATUS, 0, 0, 0 };
uint8_t gr[4];
sx1262_cmd(lc, gi, gr, 4);
uint16_t irq = ((uint16_t)gr[2] << 8) | gr[3];
if (irq & SX1262_IRQ_TX_DONE) {
uint8_t clr[3] = { SX1262_CMD_CLR_IRQ_STATUS, 0xFF, 0xFF };
uint8_t clr_rx[3];
sx1262_cmd(lc, clr, clr_rx, 3);
return true;
}
}
return false;
}
static void sx1262_start_rx (lora_chan_t *lc) {
uint8_t clr[3] = { SX1262_CMD_CLR_IRQ_STATUS, 0xFF, 0xFF };
uint8_t clr_rx[3];
sx1262_cmd(lc, clr, clr_rx, 3);
/* SetRx: timeout=0xFFFFFF = continuous */
uint8_t rx_cmd[4] = { SX1262_CMD_SET_RX, 0xFF, 0xFF, 0xFF };
uint8_t rx_rx[4];
sx1262_cmd(lc, rx_cmd, rx_rx, 4);
}
/* =========================================================================
* Chip-agnostic wrappers
* ========================================================================= */
static bool chip_init (lora_chan_t *lc) {
if (lc->chip == LORA_CHIP_SX1276) return sx1276_init(lc);
if (lc->chip == LORA_CHIP_SX1262) return sx1262_init(lc);
return false;
}
static void chip_start_rx (lora_chan_t *lc) {
if (lc->chip == LORA_CHIP_SX1276) sx1276_start_rx(lc);
if (lc->chip == LORA_CHIP_SX1262) sx1262_start_rx(lc);
}
static int chip_receive (lora_chan_t *lc, uint8_t *buf, int maxlen,
int *rssi, float *snr) {
if (lc->chip == LORA_CHIP_SX1276)
return sx1276_receive(lc, buf, maxlen, rssi, snr);
if (lc->chip == LORA_CHIP_SX1262)
return sx1262_receive(lc, buf, maxlen, rssi, snr);
return 0;
}
static bool chip_transmit (lora_chan_t *lc, const uint8_t *data, int len) {
bool ok;
if (lc->chip == LORA_CHIP_SX1276) ok = sx1276_transmit(lc, data, len);
else if (lc->chip == LORA_CHIP_SX1262) ok = sx1262_transmit(lc, data, len);
else ok = false;
/* Return to continuous RX after TX */
chip_start_rx(lc);
return ok;
}
/* =========================================================================
* TNC2 <-> AX.25 helpers
* =========================================================================
*
* LoRa APRS uses TNC2 ASCII text on the air. Dire Wolf works with binary
* AX.25 packet objects internally. These helpers bridge the two.
*
* On receive: strip 3-byte preamble, pass TNC2 text to ax25_from_text().
* On transmit: ax25_to_text() → prepend preamble → send over LoRa.
*/
#define LORA_PREAMBLE_LEN 3
static const uint8_t LORA_PREAMBLE[LORA_PREAMBLE_LEN] = { 0x3C, 0xFF, 0x01 };
/* =========================================================================
* RX thread
* ========================================================================= */
static void *rx_thread (void *arg) {
lora_chan_t *lc = (lora_chan_t *)arg;
uint8_t buf[256];
pthread_mutex_lock(&lc->spi_lock);
chip_start_rx(lc);
pthread_mutex_unlock(&lc->spi_lock);
while (lc->running) {
int rssi = 0;
float snr = 0.0f;
pthread_mutex_lock(&lc->spi_lock);
int nb = chip_receive(lc, buf, sizeof(buf), &rssi, &snr);
pthread_mutex_unlock(&lc->spi_lock);
if (nb > 0) {
/* Strip LoRa APRS preamble (0x3C 0xFF 0x01) if present */
uint8_t *payload = buf;
int plen = nb;
if (plen >= LORA_PREAMBLE_LEN &&
memcmp(payload, LORA_PREAMBLE, LORA_PREAMBLE_LEN) == 0) {
payload += LORA_PREAMBLE_LEN;
plen -= LORA_PREAMBLE_LEN;
}
if (plen <= 0) goto next;
/* Null-terminate and convert to TNC2 string */
char tnc2[256];
if (plen >= (int)sizeof(tnc2)) plen = (int)sizeof(tnc2) - 1;
memcpy(tnc2, payload, plen);
tnc2[plen] = '\0';
/* Strip trailing whitespace */
for (int i = plen - 1; i >= 0 && (tnc2[i] == '\r' || tnc2[i] == '\n' || tnc2[i] == ' '); i--)
tnc2[i] = '\0';
/* Parse TNC2 into AX.25 packet object */
packet_t pp = ax25_from_text(tnc2, 1);
if (pp == NULL) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("loraspi: failed to parse: %s\n", tnc2);
goto next;
}
/* Build signal level struct for display */
alevel_t alevel;
memset(&alevel, 0, sizeof(alevel));
alevel.rec = rssi;
/* Build spectrum string: "LoRa SNR=X.XdB" */
char spectrum[32];
snprintf(spectrum, sizeof(spectrum), "LoRa SNR=%.1fdB", snr);
/* Inject into Dire Wolf frame queue */
dlq_rec_frame(lc->chan, -3, 0, pp, alevel, fec_type_none, RETRY_NONE, spectrum);
text_color_set(DW_COLOR_REC);
dw_printf ("%s RSSI=%d dBm SNR=%.1f dB\n", tnc2, rssi, snr);
}
next:
/* 10 ms poll interval — low CPU, ~100 Hz check rate */
usleep(10000);
}
return NULL;
}
/* =========================================================================
* TX thread
* ========================================================================= */
static void *tx_thread (void *arg) {
lora_chan_t *lc = (lora_chan_t *)arg;
uint8_t data[256];
int len;
while (lc->running) {
txq_pop(&lc->txq, data, &len);
if (!lc->running) break;
pthread_mutex_lock(&lc->spi_lock);
bool ok = chip_transmit(lc, data, len);
/* SX1276/SX1262 drops to STDBY after TX_DONE — re-arm RX immediately */
chip_start_rx(lc);
pthread_mutex_unlock(&lc->spi_lock);
text_color_set(ok ? DW_COLOR_XMIT : DW_COLOR_ERROR);
dw_printf ("loraspi: TX %s (%d bytes)\n", ok ? "OK" : "FAILED", len);
}
return NULL;
}
/* =========================================================================
* Public API
* ========================================================================= */
void loraspi_init (struct audio_s *pa) {
for (int chan = 0; chan < MAX_TOTAL_CHANS; chan++) {
if (pa->chan_medium[chan] != MEDIUM_LORA) continue;
if (s_lora_count >= MAX_LORA_CHANS) {
text_color_set(DW_COLOR_ERROR);
dw_printf ("loraspi: too many LORA channels (max %d)\n", MAX_LORA_CHANS);
continue;
}
lora_chan_t *lc = &s_lora[s_lora_count++];
memset(lc, 0, sizeof(*lc));
lc->chan = chan;
lc->chip = pa->lora_chip[chan];
/* RF parameters */
lc->freq_mhz = pa->lora_freq_mhz[chan];
lc->sf = pa->lora_sf[chan];
lc->bw_khz = pa->lora_bw_khz[chan];
lc->cr = pa->lora_cr[chan];
lc->sw = pa->lora_sw[chan];
lc->txpower = pa->lora_txpower[chan];
/* Hardware pins */
lc->pin_cs = pa->lora_pin_cs[chan];
lc->pin_reset = pa->lora_pin_reset[chan];
lc->pin_irq = pa->lora_pin_irq[chan];
lc->pin_busy = pa->lora_pin_busy[chan];
lc->pin_tx_en = pa->lora_pin_tx_en[chan];
lc->pin_rx_en = pa->lora_pin_rx_en[chan];
lc->pa_boost = pa->lora_pa_boost[chan];
lc->tcxo = pa->lora_tcxo[chan];
lc->tcxo_voltage = pa->lora_tcxo_voltage[chan];
/* Set up GPIO */
gpio_setup_out(lc->pin_cs, 1);
gpio_setup_out(lc->pin_reset, 1);
gpio_setup_in (lc->pin_irq);
gpio_setup_in (lc->pin_busy);
gpio_setup_out(lc->pin_tx_en, 0);
gpio_setup_out(lc->pin_rx_en, 0);
/* Open SPI */
lc->spi_fd = spi_open(pa->lora_spi_bus[chan], pa->lora_spi_dev[chan],
pa->lora_spi_speed[chan] > 0 ?
(uint32_t)pa->lora_spi_speed[chan] : 2000000U);
if (lc->spi_fd < 0) {
s_lora_count--;
continue;
}
/* Initialise chip */
if (!chip_init(lc)) {