-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathrpi-interface.c
More file actions
2073 lines (1752 loc) · 56.5 KB
/
Copy pathrpi-interface.c
File metadata and controls
2073 lines (1752 loc) · 56.5 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
/*
* rpi-interface.c
*
* Edited on: Dec 22, 2025
* Author: Wojciech Kaczmarski, SP5WWP
* M17 Foundation
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#include <math.h>
#include <stdarg.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h>
#include <netinet/tcp.h>
#include <netinet/ip.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <fcntl.h>
#include <termios.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <time.h>
#include <signal.h>
#include <zmq.h>
#include <gpiod.h>
//rpi-interface commands
#include "interface_cmds.h"
//libm17
#include <m17.h>
#include "term.h" //colored terminal font
#define DEBUG_HALT while(1)
#define MAX_UDP_LEN 65535
#define ZMQ_RX_BUFF_SIZE 1024 //how many RX baseband samples do we want to publish over ZMQ at once?
#define RX_SYMBOL_SCALING_COEFF (1.0f/(0.8f/(40.0e3f/2097152*0xAD)*130.0f)) //CC1200 User's Guide, p. 24
//0xAD is `DEVIATION_M`, 2097152=2^21
//+1.0 is the symbol for +0.8kHz
//40.0e3 is F_TCXO in kHz
//129 is `CFM_RX_DATA_OUT` register value at max. F_DEV (130 is 1 off but offers a better symbol map)
//datasheet might have this wrong (it says 64)
#define TX_SYMBOL_SCALING_COEFF (0.8f/((40.0e3f/2097152)*0xAD)*64.0f) //0xAD is `DEVIATION_M`, 2097152=2^21
//+0.8kHz is the deviation for symbol +1
//40.0e3 is F_TCXO in kHz
//64 is `CFM_TX_DATA_IN` register value for max. F_DEV
//internet
struct sockaddr_in source, dest;
int sockt;
struct iphdr *iph;
struct sockaddr_in saddr;
struct sockaddr_in daddr;
struct sockaddr_in serv_addr;
uint32_t saddr_size=sizeof(saddr);
uint8_t tx_buff[512]={0};
uint8_t rx_buff[65536]={0};
int tx_len=0, rx_len=0;
//config stuff
struct config_t
{
char log_path[128];
char uart[64];
uint32_t uart_rate;
char node[10];
char refl_addr[20];
uint16_t refl_port;
char reflector[8];
char module;
uint8_t enc_node[6];
int16_t freq_corr;
float tx_pwr;
uint32_t rx_freq;
uint32_t tx_freq;
uint8_t afc;
uint16_t zmq_port;
//GPIO Pins
uint16_t pa_en;
uint16_t boot0;
uint16_t nrst;
//GPIO resources (handles)
struct gpiod_chip *gpio_chip;
struct gpiod_line *pa_en_line;
struct gpiod_line *boot0_line;
struct gpiod_line *nrst_line;
} config;
//device stuff
uint8_t cmd[8];
//M17
struct m17stream_t
{
uint16_t sid;
lsf_t lsf;
uint16_t fn;
uint8_t pld[16];
} m17stream;
enum rx_state_t
{
RX_IDLE,
RX_SYNCD
};
enum tx_state_t
{
TX_IDLE,
TX_ACTIVE
};
enum err_t
{
ERR_OK, //all good
ERR_TRX_PLL, //TRX PLL lock error
ERR_TRX_SPI, //TRX SPI comms error
ERR_RANGE, //value out of range
ERR_CMD_MALFORM, //malformed command
ERR_BUSY, //busy!
ERR_BUFF_FULL, //buffer full
ERR_NOP, //nothing to do
ERR_OTHER
};
int8_t flt_buff[8*5+1]; //length of this has to match RRC filter's length
float f_flt_buff[8*5+2*(8*5+4800/25*5)+2]; //8 preamble symbols, 8 for the syncword, and 960 for the payload.
//floor(sps/2)=2 extra samples for timing error correction
uint8_t rx_samp_buff[1024];
int8_t raw_bsb_rx[960];
uint16_t rx_buff_cnt;
uint8_t uart_rx_sync;
uint8_t uart_rx_data_valid;
volatile uint8_t uart_lock;
enum rx_state_t rx_state=RX_IDLE;
enum tx_state_t tx_state=TX_IDLE;
int8_t lsf_sync_ext[16]; //extended LSF syncword
lsf_t lsf; //recovered LSF
uint16_t sample_cnt=0; //sample counter (for RX sync timeout)
uint16_t fn, last_fn=0xFFFFU; //current and last received FN (stream mode)
uint8_t pkt_fn, last_pkt_fn=0xFF; //current and last received FN (packet mode)
uint8_t lsf_b[30]; //raw decoded LSF
uint8_t first_frame=1; //first decoded frame after SYNC?
uint8_t lich_parts=0; //LICH chunks received (bit flags)
uint8_t got_lsf=0; //got LSF? either from LSF or reconstructed from LICH
const int8_t eot_symbols[8] = { +3, +3, +3, +3, +3, +3, -3, +3 };
//timer for timeouts
uint32_t tx_timer=0;
//log traffic to file
FILE* logfile = NULL;
//ZMQ PUB for the baseband
char zmq_addr[64];
void *zmq_ctx;
void *bsb_downlink;
int8_t zmq_samp_buff[ZMQ_RX_BUFF_SIZE];
uint16_t zmq_samples=0;
time_t last_refl_ping;
//debug printf
void dbg_print(const char* color_code, const char* fmt, ...)
{
char str[1000]; //1k chars is probably an overkill, but - oh well :)
va_list ap;
va_start(ap, fmt);
vsprintf(str, fmt, ap);
va_end(ap);
if(color_code!=NULL)
{
fputs(color_code, stdout);
fputs(str, stdout);
fputs(TERM_DEFAULT, stdout);
}
else
{
fputs(str, stdout);
}
}
void move_cursor(uint8_t x, uint8_t y)
{
printf("\033[%d;%dH", y, x);
}
uint32_t get_ms(void)
{
struct timespec spec;
clock_gettime(CLOCK_REALTIME, &spec);
time_t s = spec.tv_sec;
uint32_t ms = roundf(spec.tv_nsec/1.0e6); //convert nanoseconds to milliseconds
if(ms>999)
{
s++;
ms=0;
}
return s*1000 + ms;
}
//UART magic
int fd; //UART handle
int get_baud(uint32_t baud)
{
switch(baud)
{
case 9600:
return B9600;
case 19200:
return B19200;
case 38400:
return B38400;
case 57600:
return B57600;
case 115200:
return B115200;
case 230400:
return B230400;
case 460800:
return B460800;
case 500000:
return B500000;
case 576000:
return B576000;
case 921600:
return B921600;
case 1000000:
return B1000000;
case 1152000:
return B1152000;
case 1500000:
return B1500000;
case 2000000:
return B2000000;
case 2500000:
return B2500000;
case 3000000:
return B3000000;
case 3500000:
return B3500000;
case 4000000:
return B4000000;
default:
return -1;
}
}
int set_interface_attribs(int fd, uint32_t speed, int parity)
{
struct termios tty;
if(tcgetattr(fd, &tty) != 0)
{
dbg_print(TERM_YELLOW, " Error from tcgetattr\n");
exit(1);
}
cfsetospeed(&tty, get_baud(speed));
cfsetispeed(&tty, get_baud(speed));
tty.c_cflag = (tty.c_cflag & ~CSIZE) | CS8; //8-bit chars
//disable IGNBRK for mismatched speed tests; otherwise receive break
//as \000 chars
tty.c_iflag &= ~IGNBRK; //disable break processing
tty.c_lflag = 0; //no signaling chars, no echo,
//no canonical processing
tty.c_oflag = 0; //no remapping, no delays
tty.c_cc[VMIN] = 1; //read returns when 1 byte available
tty.c_cc[VTIME] = 5; //5*0.5=0.5 seconds read timeout
tty.c_iflag &= ~(IXON | IXOFF | IXANY); // shut off xon/xoff ctrl
tty.c_cflag |= (CLOCAL | CREAD); //ignore modem controls,
//enable reading
tty.c_cflag &= ~(PARENB | PARODD); //shut off parity
tty.c_cflag |= parity;
tty.c_cflag &= ~CSTOPB;
tty.c_cflag &= ~CRTSCTS;
if(tcsetattr(fd, TCSANOW, &tty)!=0)
{
dbg_print(TERM_RED, " Error from tcsetattr\n");
exit(1);
}
return 0;
}
/**
* @brief Replaces the first character with ASCII code under 0x20 with 0x00 (null termination).
* rtrim() scans the input string left to right.
*
* @param inp Pointer to a string with text to trim.
*/
void rtrim(uint8_t* inp)
{
for(uint8_t i=0; i<strlen((char*)inp); i++)
{
if(inp[i]<' ')
{
inp[i]=0;
break;
}
}
}
int8_t load_config(struct config_t *cfg, char *path)
{
FILE* cfg_fp=fopen(path, "r");
char line[128];
//load defaults
cfg->log_path[0]=0; //empty string - disabled
sprintf(cfg->uart, "/dev/ttyAMA0");
cfg->uart_rate=460800;
sprintf(cfg->node, "N0CALL H");
sprintf(cfg->refl_addr, "152.70.192.70");
cfg->refl_port=17000;
sprintf(cfg->reflector, "M17-M17");
cfg->module='A';
cfg->rx_freq=433475000U;
cfg->tx_freq=433475000U;
cfg->freq_corr=0;
cfg->tx_pwr=10.0f;
cfg->afc=0;
cfg->zmq_port=0; //0 - disabled
cfg->nrst=21;
cfg->pa_en=18;
cfg->boot0=20;
//overwrite settings
if(cfg_fp!=NULL)
{
//mindlessly load all the values, we will perform sanity checks later
while(fgets((char*)line, sizeof(line), cfg_fp)>(char*)0)
{
uint8_t len;
if(strstr(line, "log_path")==line)
{
len=strstr(strstr(line, "\"")+1, "\"")-strstr(line, "\"")-1;
memcpy(cfg->log_path, strstr(line, "\"")+1, len);
cfg->log_path[len]=0;
}
else if(strstr(line, "device")==line)
{
len=strstr(strstr(line, "\"")+1, "\"")-strstr(line, "\"")-1;
memcpy(cfg->uart, strstr(line, "\"")+1, len);
cfg->uart[len]=0;
}
else if(strstr(line, "speed")==line)
{
cfg->uart_rate=atoi(strstr(line, "=")+1);
}
else if(strstr(line, "node")==line)
{
len=strstr(strstr(line, "\"")+1, "\"")-strstr(line, "\"")-1;
memcpy(cfg->node, strstr(line, "\"")+1, len);
cfg->node[len]=0;
}
else if(strstr(line, "ipv4")==line)
{
len=strstr(strstr(line, "\"")+1, "\"")-strstr(line, "\"")-1;
memcpy(cfg->refl_addr, strstr(line, "\"")+1, len);
cfg->refl_addr[len]=0;
}
else if(strstr(line, "port")==line)
{
cfg->refl_port=atoi(strstr(line, "=")+1);
}
else if(strstr(line, "reflector")==line)
{
len=strstr(strstr(line, "\"")+1, "\"")-strstr(line, "\"")-1;
memcpy(cfg->reflector, strstr(line, "\"")+1, len);
cfg->reflector[len]=0;
}
else if(strstr(line, "module")==line)
{
cfg->module=*(strstr(line, "\"")+1);
}
else if(strstr(line, "nrst")==line)
{
cfg->nrst=atoi(strstr(line, "=")+1);
}
else if(strstr(line, "pa_en")==line)
{
cfg->pa_en=atoi(strstr(line, "=")+1);
}
else if(strstr(line, "boot0")==line)
{
cfg->boot0=atoi(strstr(line, "=")+1);
}
else if(strstr(line, "tx_freq")==line)
{
cfg->tx_freq=atoi(strstr(line, "=")+1);
}
else if(strstr(line, "rx_freq")==line)
{
cfg->rx_freq=atoi(strstr(line, "=")+1);
}
else if(strstr(line, "freq_corr")==line)
{
cfg->freq_corr=atoi(strstr(line, "=")+1);
}
else if(strstr(line, "tx_pwr")==line)
{
cfg->tx_pwr=atof(strstr(line, "=")+1);
}
else if(strstr(line, "afc")==line)
{
if(*(strstr(line, "=")+1)=='1')
cfg->afc=1;
else
cfg->afc=0;
}
else if(strstr(line, "zmq_port")==line)
{
cfg->zmq_port=atoi(strstr(line, "=")+1);
}
}
fclose(cfg_fp);
return 0; //file read OK
}
else
{
return -1; //error reading file
}
}
// Release GPIO resources
void gpio_cleanup(void)
{
// Release all GPIO lines
if (config.pa_en_line) {
gpiod_line_release(config.pa_en_line);
config.pa_en_line = NULL;
}
if (config.boot0_line) {
gpiod_line_release(config.boot0_line);
config.boot0_line = NULL;
}
if (config.nrst_line) {
gpiod_line_release(config.nrst_line);
config.nrst_line = NULL;
}
// Close the chip
if (config.gpio_chip) {
gpiod_chip_close(config.gpio_chip);
config.gpio_chip = NULL;
}
dbg_print(TERM_GREEN, "GPIO resources released\n");
}
void gpio_init(const char *program_name)
{
int ret;
// Initialize to NULL for safety
config.gpio_chip = NULL;
config.pa_en_line = NULL;
config.boot0_line = NULL;
config.nrst_line = NULL;
// Open the GPIO chip
config.gpio_chip = gpiod_chip_open_by_name("gpiochip0"); // Assuming gpiochip0, might need to be configurable
if (!config.gpio_chip) {
dbg_print(TERM_RED, "\nError opening GPIO chip\n");
// No need to call gpio_cleanup as nothing was allocated yet
exit(1);
}
// Get the lines
config.pa_en_line = gpiod_chip_get_line(config.gpio_chip, config.pa_en);
if (!config.pa_en_line) {
dbg_print(TERM_RED, "\nError getting PA_EN line (GPIO%d)\n", config.pa_en);
gpio_cleanup();
exit(1);
}
config.boot0_line = gpiod_chip_get_line(config.gpio_chip, config.boot0);
if (!config.boot0_line) {
dbg_print(TERM_RED, "\nError getting BOOT0 line (GPIO%d)\n", config.boot0);
gpio_cleanup();
exit(1);
}
config.nrst_line = gpiod_chip_get_line(config.gpio_chip, config.nrst);
if (!config.nrst_line) {
dbg_print(TERM_RED, "\nError getting nRST line (GPIO%d)\n", config.nrst);
gpio_cleanup();
exit(1);
}
// Request lines as outputs, initially low
ret = gpiod_line_request_output(config.pa_en_line, program_name, 0);
if (ret < 0) {
dbg_print(TERM_RED, "\nError requesting PA_EN line %d as output\n", config.pa_en);
gpio_cleanup();
exit(1);
}
ret = gpiod_line_request_output(config.boot0_line, program_name, 0);
if (ret < 0) {
dbg_print(TERM_RED, "\nError requesting BOOT0 line %d as output\n", config.boot0);
gpio_cleanup();
exit(1);
}
ret = gpiod_line_request_output(config.nrst_line, program_name, 0);
if (ret < 0) {
dbg_print(TERM_RED, "\nError requesting nRST line %d as output\n", config.nrst);
gpio_cleanup();
exit(1);
}
// Lines are now requested and set to low
// The handles are stored in the config structure for efficient reuse
}
uint8_t gpio_set(uint16_t gpio, uint8_t state)
{
struct gpiod_line *line = NULL;
int ret;
// Determine which line to use based on the GPIO number
if (gpio == config.pa_en) {
line = config.pa_en_line;
} else if (gpio == config.boot0) {
line = config.boot0_line;
} else if (gpio == config.nrst) {
line = config.nrst_line;
}
// Verify we have a valid line
if (!line) {
dbg_print(TERM_RED, "Error: Invalid GPIO number %d or GPIO not initialized\n", gpio);
return 1;
}
// Set the value using the stored line handle
ret = gpiod_line_set_value(line, state ? 1 : 0);
// dbg_print(0, "Attempted to set GPIO line %d to %d, gpiod_line_set_value returned %d\n", gpio, state, ret);
if (ret < 0) {
dbg_print(TERM_RED, "Error setting GPIO line %d value to %d (errno: %d)\n", gpio, state, errno);
return 1;
}
return 0;
}
//M17 stuff
void refl_send(const uint8_t* msg, uint16_t len)
{
if(sendto(sockt, msg, len, 0, (const struct sockaddr*)&serv_addr, sizeof(serv_addr))<0)
{
dbg_print(TERM_RED, "\nError while sending data to reflector.\nExiting.\n");
exit(EXIT_FAILURE);
}
}
//device config funcs
int8_t dev_ping(void)
{
uint8_t cid = CMD_PING;
uint8_t cmd[3] = {cid, 3, 0};
uint8_t resp[7] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 3);
int rd = 0;
while (rd < 7)
{
int r = read(fd, resp + rd, 7 - rd);
if (r <= 0)
{
uart_lock = 0;
dbg_print(TERM_RED, "PING: Timeout waiting for device response\n");
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 7, 0, 0, 0, 0, 0}, 7) == 0)
{
dbg_print(TERM_GREEN, "PONG OK\n"); //OK
return 0;
}
uint32_t dev_err;
memcpy((uint8_t*)&dev_err, &resp[3], sizeof(uint32_t));
dbg_print(TERM_YELLOW, "PONG error code: 0x%04X\n", dev_err);
return -1;
}
int8_t dev_set_rx_freq(uint32_t freq)
{
uint8_t cid = CMD_SET_RX_FREQ;
uint8_t cmd[3+4] = {cid, 7, 0};
memcpy(&cmd[3], (uint8_t*)&freq, sizeof(freq));
uint8_t resp[4] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 7);
int rd = 0;
while (rd < 4)
{
int r = read(fd, resp + rd, 4 - rd);
if (r <= 0)
{
uart_lock = 0;
//dbg_print(TERM_RED, "%s(): Timeout waiting for device response\n", __func__);
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_OK}, 4) == 0)
{
dbg_print(0, "RX frequency: ");
dbg_print(TERM_GREEN, "%lu Hz\n", freq); //OK
return 0;
}
dbg_print(TERM_YELLOW, "Error %d setting RX frequency: %lu Hz\n", resp[3], freq); //error
return -1;
}
int8_t dev_set_tx_freq(uint32_t freq)
{
uint8_t cid = CMD_SET_TX_FREQ;
uint8_t cmd[3+4] = {cid, 7, 0};
memcpy(&cmd[3], (uint8_t*)&freq, sizeof(freq));
uint8_t resp[4] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 7);
int rd = 0;
while (rd < 4)
{
int r = read(fd, resp + rd, 4 - rd);
if (r <= 0)
{
uart_lock = 0;
//dbg_print(TERM_RED, "%s(): Timeout waiting for device response\n", __func__);
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_OK}, 4) == 0)
{
dbg_print(0, "TX frequency: ");
dbg_print(TERM_GREEN, "%lu Hz\n", freq); //OK
return 0;
}
dbg_print(TERM_YELLOW, "Error %d setting TX frequency: %lu Hz\n", resp[3], freq); //error
return -1;
}
int8_t dev_set_freq_corr(int16_t corr)
{
uint8_t cid = CMD_SET_FREQ_CORR;
uint8_t cmd[3+2] = {cid, 5, 0, corr&0xFF, (corr>>8)&0xFF};
uint8_t resp[4] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 5);
int rd = 0;
while (rd < 4)
{
int r = read(fd, resp + rd, 4 - rd);
if (r <= 0)
{
uart_lock = 0;
//dbg_print(TERM_RED, "%s(): Timeout\n", __func__);
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_OK}, 4) == 0)
{
dbg_print(0, "Frequency correction: ");
dbg_print(TERM_GREEN, "%d\n", corr); //OK
return 0;
}
dbg_print(TERM_YELLOW, "Error %d setting frequency correction: %d\n", resp[3], corr); //error
return -1;
}
int8_t dev_set_afc(uint8_t en)
{
uint8_t cid = CMD_SET_AFC;
uint8_t cmd[3+1] = {cid, 4, 0, en==0?0:1};
uint8_t resp[4] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 4);
int rd = 0;
while (rd < 4)
{
int r = read(fd, resp + rd, 4 - rd);
if (r <= 0)
{
uart_lock = 0;
//dbg_print(TERM_RED, "%s(): Timeout\n", __func__);
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_OK}, 4) == 0)
{
dbg_print(0, "AFC: ");
dbg_print(TERM_GREEN, "%s\n", en==0?"disabled":"enabled"); //OK
return 0;
}
dbg_print(TERM_YELLOW, "Error setting AFC\n"); //error
return -1;
}
int8_t dev_set_tx_power(float power) //powr in dBm
{
uint8_t cid = CMD_SET_TX_POWER;
uint8_t cmd[3+1] = {cid, 4, 0, roundf(power*4.0f)};
uint8_t resp[4] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 4);
int rd = 0;
while (rd < 4)
{
int r = read(fd, resp + rd, 4 - rd);
if (r <= 0)
{
uart_lock = 0;
//dbg_print(TERM_RED, "%s(): Timeout\n", __func__);
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_OK}, 4) == 0)
{
dbg_print(0, "TX power: ");
dbg_print(TERM_GREEN, "%2.2f dBm\n", power); //OK
return 0;
}
dbg_print(TERM_YELLOW, "Error %d setting TX power: %2.2f dBm\n", resp[3], power); //error
return -1;
}
int8_t dev_start_tx(void)
{
uint8_t cid = CMD_TX_START;
uint8_t cmd[4] = {cid, 4, 0, 1};
uint8_t resp[4] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 4);
int rd = 0;
while (rd < 4)
{
int r = read(fd, resp + rd, 4 - rd);
if (r <= 0)
{
uart_lock = 0;
//dbg_print(TERM_RED, "%s(): Timeout\n", __func__);
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_OK}, 4) == 0 ||
memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_NOP}, 4) == 0)
{
//dbg_print(TERM_GREEN, "%s(): OK\n", __func__);
return 0;
}
//dbg_print(TERM_RED, "%s(): Bad resp = %02X %02X %02X %02X\n", __func__, resp[0], resp[1], resp[2], resp[3]);
return -1;
}
int8_t dev_stop_tx(void)
{
uint8_t cid = CMD_TX_START;
uint8_t cmd[4] = {cid, 4, 0, 0};
uint8_t resp[4] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 4);
int rd = 0;
while (rd < 4)
{
int r = read(fd, resp + rd, 4 - rd);
if (r <= 0)
{
uart_lock = 0;
//dbg_print(TERM_RED, "%s(): Timeout\n", __func__);
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_OK}, 4) == 0 ||
memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_NOP}, 4) == 0)
{
//dbg_print(TERM_GREEN, "%s(): OK\n", __func__);
return 0;
}
//dbg_print(TERM_RED, "%s(): Bad resp = %02X %02X %02X %02X\n", __func__, resp[0], resp[1], resp[2], resp[3]);
return -1;
}
int8_t dev_start_rx(void) //start reception
{
uint8_t cid = CMD_RX_START;
uint8_t cmd[4] = {cid, 4, 0, 1};
uint8_t resp[4] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 4);
int rd = 0;
while (rd < 4)
{
int r = read(fd, resp + rd, 4 - rd);
if (r <= 0)
{
uart_lock = 0;
//dbg_print(TERM_RED, "%s(): Timeout\n", __func__);
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_OK}, 4) == 0 ||
memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_NOP}, 4) == 0)
{
//dbg_print(TERM_GREEN, "%s(): OK\n", __func__);
return 0;
}
//dbg_print(TERM_RED, "%s(): Bad resp = %02X %02X %02X %02X\n", __func__, resp[0], resp[1], resp[2], resp[3]);
return -1;
}
int8_t dev_stop_rx(void) //stop reception
{
uint8_t cid = CMD_RX_START;
uint8_t cmd[4] = {cid, 4, 0, 0};
uint8_t resp[4] = {0};
uart_lock = 1; //prevent main loop from reading
tcflush(fd, TCIFLUSH); //clear leftover bytes
write(fd, cmd, 4);
int rd = 0;
while (rd < 4)
{
int r = read(fd, resp + rd, 4 - rd);
if (r <= 0)
{
uart_lock = 0;
//dbg_print(TERM_RED, "%s(): Timeout\n", __func__);
return -1;
}
rd += r;
}
uart_lock = 0;
if (memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_OK}, 4) == 0 ||
memcmp(resp, (uint8_t[]){cid, 4, 0, ERR_NOP}, 4) == 0)
{
//dbg_print(TERM_GREEN, "%s(): OK\n", __func__);
return 0;
}
//dbg_print(TERM_RED, "%s(): Bad resp = %02X %02X %02X %02X\n", __func__, resp[0], resp[1], resp[2], resp[3]);
return -1;
}
void sigint_handler(int val)
{
(void)val; //get rid of unused variable warning
dbg_print(TERM_YELLOW, "\nSIGINT caught, disconnecting\n");
sprintf((char*)tx_buff, "DISCxxxxxx"); //that "xxxxxx" is just a placeholder
memcpy(&tx_buff[4], config.enc_node, sizeof(config.enc_node));
refl_send(tx_buff, 4+6); //DISC
// Clean up GPIO resources
gpio_cleanup();
//close log file if necessary
if(logfile!=NULL)
{
fclose(logfile);
}
dbg_print(TERM_YELLOW, "Exiting\n");
exit(EXIT_SUCCESS);
}
//samples per symbol (sps) = 5
//old code - deprecated
/*void filter_symbols(int8_t *out, const int8_t *in, const float* flt, uint8_t phase_inv)
{
#define FLT_LEN 41
static int8_t last[FLT_LEN]; //memory for last symbols
if(out!=NULL)
{
for(uint8_t i=0; i<SYM_PER_FRA; i++)
{
for(uint8_t j=0; j<5; j++)
{
for(uint8_t k=0; k<FLT_LEN-1; k++)
last[k]=last[k+1];
if(j==0)