-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathfmtx.c
2126 lines (1814 loc) · 66 KB
/
fmtx.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
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
/*
* Low-Level API for FmStick transmitter
* $Id$
* (part of FmStick).
*
* (C) 2011 Serge <piratfm@gmail.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
#define FMTX
#include <string.h>
#ifdef WIN32
#include <malloc.h>
#else
#include <stdlib.h>
#endif
#include "hidapi.h"
#include "fmtx.h"
#include "bitstream.h"
#include "logging.h"
#include "commanddefs.h"
#include "propertydefs.h"
#include <stdio.h>
#include <stdarg.h>
#include <string.h>
#include <time.h>
#include <sys/time.h>
#define FALSE 0
#define TRUE 1
typedef int BOOL;
//HID\VID_0451&PID_2100&REV_0101&MI_02
const unsigned short _usVID=0x0451; /*!< USB vendor ID. */
const unsigned short _usPID=0x2100; /*!< USB product ID. */
/* static vars */
EXTERN FMTX_MODE_ENUM fmtxCmdStatus;
EXTERN FMTX_SPECIAL_ENUM fmtxCmdSpecial;
eeprom_data fmtxCmdEEPROM;
static const char *si471x_requests[] = {
"NONE",
"CPU ID",
"Frontend Reset",
"Frontend Access",
"Get Property",
"Set Property",
"Frontend Power Status",
"Frontend Power Up",
"Frontend Power Down",
"Frontend Enable Audio",
"Frontend Disable Audio",
"EEPROM Read",
"EEPROM Write",
"unknown"
};
#define Si471xRequestStr(x) (x < RequestUnknown ? si471x_requests[x] : si471x_requests[RequestUnknown])
static const char *si471x_statuses[] = {
"OK",
"I2C Timeout",
"I2C Communication Error",
"Bad Argument",
"No CTS!",
"unknown"
};
#define Si471xStatusStr(x) (x < SI4711_ERROR_UNKNOWN ? si471x_statuses[x] : si471x_statuses[SI4711_ERROR_UNKNOWN])
static int16_t getDefaultProp(int16_t i16Prop);
/** \brief Open FMTX device.
* \return Pointer to the FMTX HID device or NULL if none found
*
* This function looks for FMTX devices connected to the computer and
* opens the first one found.
*/
static hid_device *fmtxOpen(void)
{
struct hid_device_info *phdi=NULL;
hid_device *phd=NULL;
char *pszPath=NULL;
#if 0
struct hid_device_info *devs, *cur_dev;
devs = hid_enumerate(0x0, 0x0);
printf("Devices %p\n", devs);
cur_dev = devs;
while (cur_dev) {
printf("Device Found\n type: %04hx %04hx\n path: %s\n serial_number: %ls", cur_dev->vendor_id, cur_dev->product_id, cur_dev->path, cur_dev->serial_number);
printf("\n");
printf(" Manufacturer: %ls\n", cur_dev->manufacturer_string);
printf(" Product: %ls\n", cur_dev->product_string);
printf(" Release: %hx\n", cur_dev->release_number);
printf(" Interface: %d\n", cur_dev->interface_number);
printf("\n");
cur_dev = cur_dev->next;
}
hid_free_enumeration(devs);
#endif
phdi=hid_enumerate(_usVID,_usPID);
if (phdi==NULL)
return NULL;
pszPath=strdup(phdi->path);
if (pszPath==NULL)
return NULL;
hid_free_enumeration(phdi);
phdi=NULL;
if ((phd=hid_open_path(pszPath)) == NULL)
{
free(pszPath);
pszPath=NULL;
return NULL;
}
free(pszPath);
pszPath=NULL;
return phd;
}
/** \brief Close FMTX HID device. */
static void fmtxClose(hid_device *phd)
{
hid_close(phd);
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppDeviceFound(void)
{
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
hid_device *phd=NULL;
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
fmtxClose(phd);
return FMTX_MODE_OK;
}
/** \brief Get FMTX mode.
* \return The current FMTX mode.
* \sa FMTX_MODE_ENUM
*/
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppFeGetMode(int *transmitting)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestSi4711PowerStatus;
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
/* first check status bytes then check which mode */
if (aucBufIn[0]==(PCTransfer | PCRequestError))
return FMTX_MODE_NONE;
/* bad command */
if (aucBufIn[0]!=PCTransfer)
return FMTX_MODE_NONE;
/* check if request done */
if (aucBufIn[1]==(RequestSi4711PowerStatus|RequestDone)) {
logwrite(LOG_DEBUG, "FMTX: %s FePoweredUp: %d, TxPoweredUp: %d", __FUNCTION__,
aucBufIn[2], aucBufIn[3]);
if(aucBufIn[2]) {
*transmitting = aucBufIn[3];
return FMTX_MODE_POWER_UP;
} else
return FMTX_MODE_POWER_DOWN;
}
return FMTX_MODE_NONE;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppFeUp(void)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestSi4711PowerUp;
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
/* first check status bytes then check which mode */
if (aucBufIn[0]==(PCTransfer | PCRequestError))
return FMTX_MODE_TRANSMITTING;
/* bad command */
if (aucBufIn[0]!=PCTransfer)
return FMTX_MODE_NONE;
/* check if request done */
if (aucBufIn[1]==(RequestSi4711PowerUp|RequestDone))
return FMTX_MODE_POWER_UP;
return FMTX_MODE_NONE;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppFeDown(void)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestSi4711PowerDown;
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
/* first check status bytes then check which mode */
if (aucBufIn[0]==(PCTransfer | PCRequestError))
return FMTX_MODE_NONE;
/* bad command */
if (aucBufIn[0]!=PCTransfer)
return FMTX_MODE_NONE;
/* check if request done */
if (aucBufIn[1]==(RequestSi4711PowerDown|RequestDone))
return FMTX_MODE_POWER_DOWN;
return FMTX_MODE_NONE;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppFeReset(void)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestSi4711Reset;
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
/* first check status bytes then check which mode */
if (aucBufIn[0]==(PCTransfer | PCRequestError))
return FMTX_MODE_NONE;
/* bad command */
if (aucBufIn[0]!=PCTransfer)
return FMTX_MODE_NONE;
/* check if request done */
if (aucBufIn[1]==(RequestSi4711Reset|RequestDone))
return FMTX_MODE_POWER_DOWN;
return FMTX_MODE_NONE;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppIdString(char *cpuid, char *rev)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
memset(cpuid, 0x00, 32);
memset(rev, 0x00, 32);
uint16_t rev_major = 0;
char *str = NULL;
int str_len = 0;
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestCpuId;
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
/* first check status bytes then check which mode */
if (aucBufIn[0]==(PCTransfer | PCRequestError))
return FMTX_MODE_NONE;
/* bad command */
if (aucBufIn[0]!=PCTransfer)
return FMTX_MODE_NONE;
/* check if request done */
if (aucBufIn[1]==(RequestCpuId|RequestDone)) {
//bReportOut:
// +-----------+-----------+--------------+---------+-------------+---------+---------------+
// | PCCommand | PCRequest | REV_MAJOR[2] | STR_LEN | REV_MINOR[] | STR_LEN | DEVICE_NAME[] |
// +-----------+-----------+--------------+---------+-------------+---------+---------------+
//print_bytes(LOG_ERROR, (char *) aucBufIn, 32);
rev_major = aucBufIn[2] << 8 | aucBufIn[3];
str_len = aucBufIn[4];
str = (char *) &aucBufIn[5];
print_bytes(LOG_DUMP, (char *) str, str_len);
if(str_len > 9) {
if(!strncmp(&str[str_len-3], " $", 2))
str[str_len-3] = '\0';
if(!strncmp(str, "$Rev: ", 6))
str+=6;
}
snprintf(rev, 32, "%d.%s", rev_major, str_len < 24 ? str : "");
str = (char *) &aucBufIn[4+str_len+1+1];
str_len = aucBufIn[4+str_len+1];
print_bytes(LOG_DUMP, (char *) str, str_len);
snprintf(cpuid, 32, "%s", str_len < 24 ? str : "");
return FMTX_MODE_OK;
}
return FMTX_MODE_NONE;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppEnableAudio(void)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestSi4711AudioEnable;
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
/* first check status bytes then check which mode */
if (aucBufIn[0]==(PCTransfer | PCRequestError))
return FMTX_MODE_NONE;
/* bad command */
if (aucBufIn[0]!=PCTransfer)
return FMTX_MODE_NONE;
/* check if request done */
if (aucBufIn[1]==(RequestSi4711AudioEnable|RequestDone)) {
//errcode 1=sr, 2=fmt
if(aucBufIn[2]!=SI4711_OK){
logwrite(LOG_ERROR, "USB: Error in %s", (aucBufIn[2]==1 ? "SR" : (aucBufIn[2]==3 ? "FMT" : "UNK")));
return FMTX_MODE_NONE;
}
return FMTX_MODE_OK;
}
return FMTX_MODE_NONE;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppDisableAudio(void)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestSi4711AudioDisable;
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
/* first check status bytes then check which mode */
if (aucBufIn[0]==(PCTransfer | PCRequestError))
return FMTX_MODE_NONE;
/* bad command */
if (aucBufIn[0]!=PCTransfer)
return FMTX_MODE_NONE;
/* check if request done */
if (aucBufIn[1]==(RequestSi4711AudioDisable|RequestDone)) {
//errcode 1=sr, 2=fmt
if(aucBufIn[2]!=SI4711_OK){
logwrite(LOG_ERROR, "USB: Error in %s", (aucBufIn[2]==1 ? "SR" : (aucBufIn[2]==3 ? "FMT" : "UNK")));
return FMTX_MODE_NONE;
}
return FMTX_MODE_OK;
}
return FMTX_MODE_NONE;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppAsqStatus(uint8_t *flags, int8_t *level)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
*flags = 0x00;
*level = -70;
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestSi4711AsqStatus;
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
/* first check status bytes then check which mode */
if (aucBufIn[0]==(PCTransfer | PCRequestError))
return FMTX_MODE_POWER_DOWN;
/* bad command */
if (aucBufIn[0]!=PCTransfer)
return FMTX_MODE_NONE;
/* check if request done */
if (aucBufIn[1]==(RequestSi4711AsqStatus|RequestDone)) {
//errcode 1=sr, 2=fmt
if(aucBufIn[2]!=SI4711_OK){
logwrite(LOG_ERROR, "USB: Error in %s", (aucBufIn[2]==1 ? "SR" : (aucBufIn[2]==3 ? "FMT" : "UNK")));
return FMTX_MODE_NONE;
}
if(aucBufIn[3])
*flags |= TX_ASQ_STATUS_OUT_OVERMOD;
if(aucBufIn[4])
*flags |= TX_ASQ_STATUS_OUT_IALL;
if(aucBufIn[5])
*flags |= TX_ASQ_STATUS_OUT_IALH;
*level = aucBufIn[6];
return FMTX_MODE_OK;
}
return FMTX_MODE_NONE;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppEEPROMTuneStatus(uint16_t *freq, uint8_t *power, uint8_t *rfcap)
{
int i;
uint8_t *data =NULL;
/* getting from eeprom image */
for (i=0; i < fmtxCmdEEPROM.numCmds; i++){
data = &fmtxCmdEEPROM.commandData[i*FMTX_EEPROM_CMDSIZE];
if(data[0]==TX_TUNE_FREQ){
*freq =data[2] << 8 | data[3];
}
if(data[0]==TX_TUNE_POWER){
*power =data[3];
*rfcap =data[4];
}
}
return FMTX_MODE_OK;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppGetTuneStatus(uint16_t *freq, uint8_t *power, uint8_t *rfcap)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
*freq = 0;
*power = 0;
*rfcap = 0;
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
if(fmtxCmdSpecial == FMTX_SPECIAL_EEPROM) return fmtxIoAppEEPROMTuneStatus(freq, power, rfcap);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestSi4711TuneStatus;
logwrite(LOG_XTREME, "USB: Sending %d bytes:", 2);
print_bytes(LOG_DUMP, (char *) &aucBufOut[1], 2);
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
logwrite(LOG_XTREME, "USB: Received:");
print_bytes(LOG_DUMP, (char *) aucBufIn, 32);
/* first check status bytes then check which mode */
if (aucBufIn[0]==(PCTransfer | PCRequestError))
return FMTX_MODE_NONE;
/* bad command */
if (aucBufIn[0]!=PCTransfer)
return FMTX_MODE_NONE;
/* check if request done */
if (aucBufIn[1]==(RequestSi4711TuneStatus|RequestDone)) {
//errcode 1=sr, 2=fmt
if(aucBufIn[2]!=SI4711_OK){
logwrite(LOG_ERROR, "USB: Error in %s", (aucBufIn[2]==1 ? "SR" : (aucBufIn[2]==3 ? "FMT" : "UNK")));
return FMTX_MODE_NONE;
}
*freq = aucBufIn[3] << 8 | aucBufIn[4];
*power = aucBufIn[5];
*rfcap = aucBufIn[6];
return FMTX_MODE_OK;
}
return FMTX_MODE_NONE;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxTransmitterGetTuneStatus(double *freq, uint8_t *power, double *rfcap)
{
uint8_t rfcap0;
uint16_t freq0;
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
if(fmtxIoAppGetTuneStatus(&freq0, power, &rfcap0) != FMTX_MODE_OK)
return FMTX_MODE_NONE;
*freq = freq0/100.0;
*rfcap = rfcap0 * 0.25;
// if(freq0!=0 && *power != 0)
// return FMTX_MODE_TRANSMITTING;
return FMTX_MODE_OK;
}
static int si471x_ShowStatus(uint8_t u8Status)
{
logwrite(LOG_DEBUG, "STATUS: %s|%s|%s|%s|%s",
(u8Status & CTS)?"CTS":"cts",
(u8Status & ERR)?"ERR":"err",
(u8Status & RDSINT)?"RDSINT":"rdsint",
(u8Status & ASQINT)?"ASQINT":"asqint",
(u8Status & STCINT)?"STCINT":"stcint");
return (u8Status & CTS)?1:0;
}
static int eeprom_usbAccess(uint8_t *data, uint8_t DataSize, uint16_t offset, int write)
{
hid_device *phd=NULL;
uint8_t aucBufIn[43];
uint8_t aucBufOut[43];
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
if(DataSize>16)
return FMTX_MODE_NONE;
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
//bReportOut:
// +-----------+-----------+-----------+-----------+-------+
// | PCCommand | PCRequest | Offset[0] | Offset[1] | bytes |
// +-----------+-----------+-----------+-----------+-------+
//02 09 00 00 7f 00 00 5c 00 00 00 00 00 00 00 00
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = write ? RequestEepromSectionWrite : RequestEepromSectionRead;
aucBufOut[3] = 0xff & (offset >> 8);
aucBufOut[4] = 0xff & offset;
aucBufOut[5] = DataSize;
if(write)
memcpy(&aucBufOut[6], data, DataSize);
logwrite(LOG_XTREME, "USB: Sending %d bytes:", DataSize+4);
print_bytes(LOG_DUMP, (char *) &aucBufOut[1], 32);
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
logwrite(LOG_XTREME, "USB: Received:");
print_bytes(LOG_DUMP, (char *) aucBufIn, 32);
// +-----------+-----------+--------+---------+------+---------+
// | PCCommand | PCRequest | Status | Data[0] | ... | Data[n] |
// +-----------+-----------+--------+---------+------+---------+
if(aucBufIn[2]!=0x01) {
logwrite(LOG_ERROR, "USB: I2C_TRANSFER failed");
//sometimes it timeoutes, but the CTS=0, so we must re-read this byte again.
return FMTX_MODE_NONE;
}
if(!write)
memcpy(data, &aucBufIn[3], DataSize);
return FMTX_MODE_OK;
}
static int si471x_usbAccess(uint8_t *request, int ReqSize, uint8_t *reply, int *ReplySize)
{
hid_device *phd=NULL;
unsigned char aucBufIn[43];
unsigned char aucBufOut[43];
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
if(ReqSize>8)
return FMTX_MODE_NONE;
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
//bReportOut:
// +-----------+-----------+---------+---------+--------+- ... -+--------+
// | PCCommand | PCRequest | ArgsLen | Command | Arg[1] | ... | Arg[7] |
// +-----------+-----------+---------+---------+--------+- ... -+--------+
aucBufOut[0] = 0; // Report ID, ignored
aucBufOut[1] = PCTransfer;
aucBufOut[2] = RequestSi4711Access;
aucBufOut[3] = ReqSize;
memcpy(&aucBufOut[4], request, ReqSize);
logwrite(LOG_XTREME, "USB: Sending %d bytes:", ReqSize+3);
print_bytes(LOG_DUMP, (char *) &aucBufOut[1], 32);
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
logwrite(LOG_XTREME, "USB: Received:");
print_bytes(LOG_DUMP, (char *) aucBufIn, 32);
// +-----------+-----------+-------------+------------+---------+---------+---------+ ... -+----------+
// | PCCommand | PCRequest | WriteStatus | ReadStatus | RespLen | Status | Resp[1] | ... | Resp[15] |
// +-----------+-----------+-------------+------------+---------+---------+---------+ ... -+----------+
if(aucBufIn[2]!=1) {
logwrite(LOG_ERROR, "USB: command failed (%d): %s, returned (FALSE)", aucBufIn[2], Si471xStatusStr(aucBufIn[2]));
//sometimes it timeoutes, but the CTS=0, so we must re-read this byte again.
//return 1;
}
if(aucBufIn[3]!=SI4711_OK) {
logwrite(LOG_ERROR, "USB: I2C_READ failed (%d): %s", aucBufIn[3], Si471xStatusStr(aucBufIn[3]));
return FMTX_MODE_NONE;
}
if(aucBufIn[4] > 16) {
logwrite(LOG_ERROR, "USB: I2C_READ failed, too much bytes received: %d", aucBufIn[4]);
return FMTX_MODE_NONE;
}
logwrite(LOG_XTREME, "USB: Volume: %d.%d (deviation: %d)", (char ) aucBufIn[21], (char ) aucBufIn[22], (uint16_t ) (aucBufIn[23] << 8 | aucBufIn[24]));
memcpy(reply, &aucBufIn[5], aucBufIn[4]);
*ReplySize=aucBufIn[4];
return FMTX_MODE_OK;
}
static int si471x_cmdEEPROMAccess(uint8_t *request, int ReqSize, uint8_t *reply, int *ReplySize)
{
int i;
uint8_t *data=NULL;
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
if(ReqSize < 1 || ReqSize > 8)
return FMTX_MODE_NONE;
logwrite(LOG_DEBUG, "EEPROM: Emulate command %02x", request[0]);
for(i=0;i<16;i++)
reply[i] = 0x00;
switch(request[0]){
/* forbidden commands */
case POWER_UP:
case POWER_DOWN:
case GET_PROPERTY:
case SET_PROPERTY:
case TX_TUNE_STATUS:
logwrite(LOG_ERROR, "EEPROM: %s: mad cmd %02x not allowed!", __FUNCTION__, request[0]);
return FMTX_MODE_NONE;
break;
/* setting to eeprom image */
case TX_RDS_BUFF:
reply[2] = (FMTX_MAX_EEPROM_CMDS - fmtxCmdEEPROM.numCmds)*3;
case TX_RDS_PS:
case TX_TUNE_FREQ:
case TX_TUNE_POWER:
if(fmtxCmdEEPROM.numCmds > FMTX_MAX_EEPROM_CMDS) {
logwrite(LOG_ERROR, "EEPROM: %s: error setting cmd %02x not enough space!", __FUNCTION__, request[0]);
return FMTX_MODE_NONE;
}
data = &fmtxCmdEEPROM.commandData[fmtxCmdEEPROM.numCmds*FMTX_EEPROM_CMDSIZE];
for(i=0;i<ReqSize;i++)
data[i] = request[i];
for(;i<8;i++)
data[i] = 0x00;
fmtxCmdEEPROM.numCmds++;
break;
default:
*ReplySize=0;
return FMTX_MODE_NONE;
}
reply[0] = 0x80;
*ReplySize=16;
return FMTX_MODE_OK;
}
/*
* i2c transfer to device (write command and read status and response)
*/
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppCommand(uint8_t u8Cmd, uint8_t *pu8Resp, uint8_t u8Amount, ...)
{
uint8_t buf_in[8];
uint8_t buf_out[16];
uint8_t i;
int bytes;
va_list vl;
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
if(u8Amount>7)
return FMTX_MODE_NONE;
memset(buf_in, 0x00, 8);
memset(buf_out, 0x00, 16);
buf_in[0] = u8Cmd;
/* variable args handling */
va_start(vl,u8Amount);
for (i=0; i<u8Amount; i++) {
buf_in[1+i] = (uint8_t ) va_arg(vl,int);
}
va_end(vl);
logwrite(LOG_DEBUG, "USB: Sending device Cmd: %02x %d-args...", u8Cmd, u8Amount);
print_bytes(LOG_DUMP, (char *) &buf_in[1], 7);
bytes=0;
/* cmd for eeprom ? */
if(fmtxCmdSpecial == FMTX_SPECIAL_EEPROM) {
if(si471x_cmdEEPROMAccess(buf_in, u8Amount+1, buf_out, &bytes) != FMTX_MODE_OK)
return FMTX_MODE_NONE;
} else {
if(si471x_usbAccess(buf_in, u8Amount+1, buf_out, &bytes) != FMTX_MODE_OK)
return FMTX_MODE_NONE;
}
if(bytes==0) {
logwrite(LOG_ERROR, "I2C: Status not received!");
return FMTX_MODE_NONE;
}
if (buf_out[0] & ERR) {
logwrite(LOG_ERROR, "I2C: Status not have got ERR flag!");
}
if(!(buf_out[0] & CTS)) {
logwrite(LOG_ERROR, "I2C: Status not have CTS!");
}
memcpy(pu8Resp, buf_out, bytes);
logwrite(LOG_DEBUG, "USB: Cmd %02x answered: %02x... ", u8Cmd, buf_out[0]);
print_bytes(LOG_DUMP, (char *) &buf_out[0], 16);
si471x_ShowStatus(pu8Resp[0]);
return FMTX_MODE_OK;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppSetEEPROMProperty(int16_t i16Prop, int16_t i16Val)
{
uint8_t *data;
if(fmtxCmdEEPROM.numCmds > FMTX_MAX_EEPROM_CMDS) {
logwrite(LOG_ERROR, "EEPROM: %s: error setting prop %04x => %04x not enough space!", __FUNCTION__, i16Prop, i16Val);
return FMTX_MODE_NONE;
}
if(getDefaultProp(i16Prop) == i16Val){
logwrite(LOG_ERROR, "EEPROM: %s: prop %04x have default value %04x saving space!", __FUNCTION__, i16Prop, i16Val);
return FMTX_MODE_OK;
}
data = &fmtxCmdEEPROM.commandData[fmtxCmdEEPROM.numCmds*FMTX_EEPROM_CMDSIZE];
data[0] = SET_PROPERTY;
data[1] = 0x00;
data[2] = 0xff & (i16Prop>>8);
data[3] = 0xff & i16Prop;
data[4] = 0xff & (i16Val>>8);
data[5] = 0xff & i16Val;
data[6] = 0x00;
data[7] = 0x00;
fmtxCmdEEPROM.numCmds++;
return FMTX_MODE_OK;
}
EXTERN FMTX_API_EXPORT FMTX_API_CALL FMTX_MODE_ENUM fmtxIoAppSetProperty(int16_t i16Prop, int16_t i16Val)
{
hid_device *phd=NULL;
uint8_t aucBufIn[43];
uint8_t aucBufOut[43];
logwrite(LOG_DUMP, "FMTX: %s", __FUNCTION__);
if(i16Prop == GPO_IEN ||
i16Prop == DIGITAL_INPUT_FORMAT ||
i16Prop == DIGITAL_INPUT_SAMPLE_RATE ||
i16Prop == REFCLK_FREQ ||
i16Prop == REFCLK_PRESCALE) {
logwrite(LOG_ERROR, "FMTX: %s: Setting porperty %04x - is not allowed!", __FUNCTION__, i16Prop);
return FMTX_MODE_NONE;
}
if(fmtxCmdSpecial == FMTX_SPECIAL_EEPROM) return fmtxIoAppSetEEPROMProperty(i16Prop, i16Val);
memset(aucBufOut, 0x00, 43);
aucBufOut[0] = 0x00; //report number, would be unused!
aucBufOut[1] = PCTransfer; //
aucBufOut[2] = RequestSi4711SetProp;
aucBufOut[3] = i16Prop >> 8;
aucBufOut[4] = i16Prop;
aucBufOut[5] = i16Val >> 8;
aucBufOut[6] = i16Val;
logwrite(LOG_DEBUG, "USB: Sending device \"%s\" %04x => %04x request...", Si471xRequestStr(aucBufOut[2]), i16Prop, i16Val);
print_bytes(LOG_DUMP, (char *) &aucBufOut[1], 6);
phd = fmtxOpen();
if (phd == NULL)
return FMTX_MODE_NONE;
/* Send a BL Query Command */
//bReportOut:
// +-----------+-----------+-------+-------+------+------+
// | PCCommand | PCRequest | AddrH | AddrL | ValH | ValL |
// +-----------+-----------+-------+-------+------+------+
hid_write(phd, aucBufOut, 43);
memset(aucBufIn, 0xCC, 42); // Clear out the response buffer
hid_read(phd, aucBufIn, 42);
fmtxClose(phd);
phd = NULL;
logwrite(LOG_XTREME, "USB: Received:");
print_bytes(LOG_DUMP, (char *) aucBufIn, 32);
// +-----------+-----------+-------------+------------+----------+-----------+
// | PCCommand | PCRequest | WriteStatus | ReadStatus | NumBytes | CMDStatus |
// +-----------+-----------+-------------+------------+----------+-----------+
if(aucBufIn[0] & PCRequestError) {
logwrite(LOG_ERROR, "USB: request error");
goto set_prop_err;
}
if(!(aucBufIn[1] & RequestDone)) {
logwrite(LOG_ERROR, "USB: request is not done!");
goto set_prop_err;
}
if(aucBufIn[8]!=SI4711_OK) {
logwrite(LOG_ERROR, "USB: Device request \"%s\" failed (%d): %s", Si471xRequestStr(RequestSi4711SetProp), aucBufIn[2], Si471xStatusStr(aucBufIn[2]));
goto set_prop_err;
}
if ( aucBufIn[7] & ERR) {
logwrite(LOG_ERROR, "SI471X: Answers: Error setting property 0x%04x to \"0x%04x\".", i16Prop, i16Val);
goto set_prop_err;
}
if ( !(aucBufIn[7] & CTS)) {
logwrite(LOG_ERROR, "SI471X: Answers: NO CTS! When setting property 0x%04x.", i16Prop);
goto set_prop_err;
}
if(aucBufIn[6]!=1) {
logwrite(LOG_ERROR, "USB: Device request \"%s\" failed (%02x): %s (false!)", Si471xRequestStr(RequestSi4711SetProp), aucBufIn[6]);
goto set_prop_err;
}
return FMTX_MODE_OK;
set_prop_err:
return FMTX_MODE_NONE;
}
static int16_t getDefaultProp(int16_t i16Prop)
{
switch (i16Prop){
/* hardcoded values */
case DIGITAL_INPUT_SAMPLE_RATE:
return 44100;
break;
case REFCLK_FREQ:
return 31250;
break;
case REFCLK_PRESCALE:
return 192;
break;
case GPO_IEN:
case DIGITAL_INPUT_FORMAT:
return 0;
break;
/* user-defined values */
case TX_LINE_INPUT_LEVEL:
return 0x327C;
break;
case TX_LINE_INPUT_MUTE:
return 0x00;
break;
case TX_COMPONENT_ENABLE:
return 0x03;
break;
case TX_AUDIO_DEVIATION:
return 6825;
break;
case TX_PILOT_DEVIATION:
return 675;
break;
case TX_RDS_DEVIATION:
return 200;