forked from erikkaashoek/tinySA
-
Notifications
You must be signed in to change notification settings - Fork 0
/
si4432.c
1400 lines (1261 loc) · 38.8 KB
/
si4432.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
/* Copyright (c) 2020, Erik Kaashoek [email protected]
* All rights reserved.
*
* This 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 3, or (at your option)
* any later version.
*
* The software 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 GNU Radio; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 51 Franklin Street,
* Boston, MA 02110-1301, USA.
*/
#include "ch.h"
#include "hal.h"
#include "nanovna.h"
#include <math.h>
#include "si4432.h"
#define _FAST_SHIFT_
#pragma GCC push_options
#pragma GCC optimize ("O2")
#define SPI2_CLK_HIGH palSetPad(GPIOB, GPIO_SPI2_CLK)
#define SPI2_CLK_LOW palClearPad(GPIOB, GPIO_SPI2_CLK)
#define SPI2_SDI_HIGH palSetPad(GPIOB, GPIO_SPI2_SDI)
#define SPI2_SDI_LOW palClearPad(GPIOB, GPIO_SPI2_SDI)
#define SPI2_RESET palClearPort(GPIOB, (1<<GPIO_SPI2_CLK)|(1<<GPIO_SPI2_SDI))
#define SPI2_SDO ((palReadPort(GPIOB)>>GPIO_SPI2_SDO)&1)
#define SPI2_portSDO (palReadPort(GPIOB)&(1<<GPIO_SPI2_SDO))
#define CS_PE_HIGH palSetPad(GPIOC, GPIO_PE_SEL)
#define CS_PE_LOW palClearPad(GPIOC, GPIO_PE_SEL)
//#define MAXLOG 1024
//unsigned char SI4432_logging[MAXLOG];
//volatile int log_index = 0;
//#define SI4432_log(X) { if (log_index < MAXLOG) SI4432_logging[log_index++] = X; }
#define SI4432_log(X)
static void shiftOut(uint8_t val)
{
// SI4432_log(SI4432_Sel);
// SI4432_log(val);
#ifndef _FAST_SHIFT_
uint16_t i = 8;
do {
if (val & 0x80)
SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
val<<=1;
}while(--i);
#else
if (val & 0x80) SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
if (val & 0x40) SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
if (val & 0x20) SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
if (val & 0x10) SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
if (val & 0x08) SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
if (val & 0x04) SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
if (val & 0x02) SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
if (val & 0x01) SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
#endif
}
static uint8_t shiftIn(void)
{
uint32_t value = 0;
#ifndef _FAST_SHIFT_
uint16_t i = 8;
do {
value<<=1;
SPI2_CLK_HIGH;
value|=SPI2_portSDO;
SPI2_CLK_LOW;
}while(--i);
#else
SPI2_CLK_HIGH;
value =SPI2_portSDO;
SPI2_CLK_LOW;
value<<=1;
SPI2_CLK_HIGH;
value|=SPI2_portSDO;
SPI2_CLK_LOW;
value<<=1;
SPI2_CLK_HIGH;
value|=SPI2_portSDO;
SPI2_CLK_LOW;
value<<=1;
SPI2_CLK_HIGH;
value|=SPI2_portSDO;
SPI2_CLK_LOW;
value<<=1;
SPI2_CLK_HIGH;
value|=SPI2_portSDO;
SPI2_CLK_LOW;
value<<=1;
SPI2_CLK_HIGH;
value|=SPI2_portSDO;
SPI2_CLK_LOW;
value<<=1;
SPI2_CLK_HIGH;
value|=SPI2_portSDO;
SPI2_CLK_LOW;
value<<=1;
SPI2_CLK_HIGH;
value|=SPI2_portSDO;
SPI2_CLK_LOW;
#endif
return value>>GPIO_SPI2_SDO;
}
static inline void shiftInBuf(uint16_t sel, uint8_t addr, deviceRSSI_t *buf, uint16_t size, uint16_t delay) {
int idx = 0;
do{
palClearPad(GPIOC, sel);
uint32_t value = addr;
uint16_t i = 8;
do {
if (value & 0x80) SPI2_SDI_HIGH;
SPI2_CLK_HIGH;
SPI2_RESET;
value<<=1;
}while(--i);
value = 0;
i = 8;
do {
SPI2_CLK_HIGH;
value<<=1;
value|=SPI2_portSDO;
SPI2_CLK_LOW;
}while(--i);
buf[idx]=value>>GPIO_SPI2_SDO;
palSetPad(GPIOC, sel);
if (++idx >=size) return;
if (delay)
my_microsecond_delay(delay);
}while(1);
}
#ifdef __SI4432__
#define CS_SI0_HIGH palSetPad(GPIOC, GPIO_RX_SEL)
#define CS_SI1_HIGH palSetPad(GPIOC, GPIO_LO_SEL)
#define RF_POWER_HIGH palSetPad(GPIOB, GPIO_RF_PWR)
#define CS_SI0_LOW palClearPad(GPIOC, GPIO_RX_SEL)
#define CS_SI1_LOW palClearPad(GPIOC, GPIO_LO_SEL)
const uint16_t SI_nSEL[MAX_SI4432+1] = { GPIO_RX_SEL, GPIO_LO_SEL, 0}; // #3 is dummy!!!!!!
uint16_t SI4432_Sel = GPIO_RX_SEL; // currently selected SI4432
uint8_t SI4432_rbw_selected = 0;
// volatile int SI4432_guard = 0;
#ifdef __SI4432_H__
#define SELECT_DELAY 10
void SI4432_shiftOutDword(uint32_t buf, uint16_t size) {
uint16_t port = SI_nSEL[SI4432_Sel];
palClearPad(GPIOC, port);
do{
shiftOut(buf);
buf>>=8;
}while(--size);
palSetPad(GPIOC, port);
}
#ifndef SI4432_Write_Byte
void SI4432_Write_Byte(uint8_t ADR, uint8_t DATA )
{
uint16_t port = SI_nSEL[SI4432_Sel];
// if (SI4432_guard)
// while(1) ;
// SI4432_guard = 1;
// SPI2_CLK_LOW;
palClearPad(GPIOC, port);
// chThdSleepMicroseconds(SELECT_DELAY);
ADR |= 0x80 ; // RW = 1
shiftOut( ADR );
shiftOut( DATA );
palSetPad(GPIOC, port);
// SI4432_guard = 0;
}
void SI4432_Write_2_Byte(uint8_t ADR, uint8_t DATA1, uint8_t DATA2)
{
uint16_t port = SI_nSEL[SI4432_Sel];
// if (SI4432_guard)
// while(1) ;
// SI4432_guard = 1;
// SPI2_CLK_LOW;
palClearPad(GPIOC, port);
// chThdSleepMicroseconds(SELECT_DELAY);
ADR |= 0x80 ; // RW = 1
shiftOut( ADR );
shiftOut( DATA1 );
shiftOut( DATA2 );
palSetPad(GPIOC, port);
// SI4432_guard = 0;
}
#endif
#ifndef SI4432_Write_3_Byte
void SI4432_Write_3_Byte(uint8_t ADR, uint8_t DATA1, uint8_t DATA2, uint8_t DATA3)
{
uint16_t port = SI_nSEL[SI4432_Sel];
// if (SI4432_guard)
// while(1) ;
// SI4432_guard = 1;
// SPI2_CLK_LOW;
palClearPad(GPIOC, port);
// chThdSleepMicroseconds(SELECT_DELAY);
ADR |= 0x80 ; // RW = 1
shiftOut( ADR );
shiftOut( DATA1 );
shiftOut( DATA2 );
shiftOut( DATA3 );
palSetPad(GPIOC, port);
// SI4432_guard = 0;
}
#endif
uint8_t SI4432_Read_Byte(uint8_t ADR)
{
uint8_t DATA ;
uint16_t port = SI_nSEL[SI4432_Sel];
// if (SI4432_guard)
// while(1) ;
// SI4432_guard = 1;
// SPI2_CLK_LOW;
palClearPad(GPIOC, port);
shiftOut(ADR);
DATA = shiftIn();
palSetPad(GPIOC, port);
// SI4432_guard = 0;
return DATA ;
}
void SI4432_Reset(void)
{
int count = 0;
SI4432_Read_Byte (SI4432_INT_STATUS1); // Clear pending interrupts
SI4432_Read_Byte (SI4432_INT_STATUS2);
// always perform a system reset (don't send 0x87)
SI4432_Write_Byte(SI4432_STATE, 0x80);
chThdSleepMilliseconds(10);
// wait for chiprdy bit
while (count++ < 100 && ( SI4432_Read_Byte (SI4432_INT_STATUS2) & 0x02 ) == 0) {
chThdSleepMilliseconds(10);
}
}
void SI4432_Drive(int d)
{
SI4432_Write_Byte(SI4432_TX_POWER, (uint8_t) (0x18+(d & 7)));
}
void SI4432_Transmit(int d)
{
int count = 0;
SI4432_Drive(d);
if (( SI4432_Read_Byte(SI4432_DEV_STATUS) & 0x03 ) == 2)
return; // Already in transmit mode
chThdSleepMilliseconds(3);
SI4432_Write_Byte(SI4432_STATE, 0x02);
chThdSleepMilliseconds(3);
SI4432_Write_Byte(SI4432_STATE, 0x0b);
chThdSleepMilliseconds(10);
while (count++ < 100 && ( SI4432_Read_Byte(SI4432_DEV_STATUS) & 0x03 ) != 2) {
chThdSleepMilliseconds(10);
}
}
void SI4432_Receive(void)
{
int count = 0;
if (( SI4432_Read_Byte (SI4432_DEV_STATUS) & 0x03 ) == 1)
return; // Already in receive mode
chThdSleepMilliseconds(3);
SI4432_Write_Byte(SI4432_STATE, 0x02);
chThdSleepMilliseconds(3);
SI4432_Write_Byte(SI4432_STATE, 0x07);
chThdSleepMilliseconds(10);
while (count++ < 100 && ( SI4432_Read_Byte(SI4432_DEV_STATUS) & 0x03 ) != 1) {
chThdSleepMilliseconds(5);
}
}
// User asks for an RBW of WISH, go through table finding the last triple
// for which WISH is greater than the first entry, use those values,
// Return the first entry of the following triple for the RBW actually achieved
#define IF_BW(dwn3, ndec, filset) (((dwn3)<<7)|((ndec)<<4)|(filset))
typedef struct {
uint8_t reg; // IF_BW(dwn3, ndec, filset)
int8_t RSSI_correction_x_10; // Correction * 10
uint16_t RBWx10; // RBW * 10 in kHz
}RBW_t; // sizeof(RBW_t) = 4 bytes
static const RBW_t RBW_choices[] =
{
// BW register corr freq
// {IF_BW(0,5,1),0,26},
// {IF_BW(0,5,2),0,28},
#if 0
{IF_BW(0,5,3),3,31},
{IF_BW(0,5,4),-3,32},
{IF_BW(0,5,5),6,37},
{IF_BW(0,5,6),5,42},
{IF_BW(0,5,7),5,45},
{IF_BW(0,4,1),0,49},
{IF_BW(0,4,2),0,54},
{IF_BW(0,4,3),0,59},
{IF_BW(0,4,4),0,61},
{IF_BW(0,4,5),5,72},
{IF_BW(0,4,6),5,82},
{IF_BW(0,4,7),5,88},
{IF_BW(0,3,1),0,95},
{IF_BW(0,3,2),0,106},
{IF_BW(0,3,3),2,115},
{IF_BW(0,3,4),0,121},
{IF_BW(0,3,5),5,142},
{IF_BW(0,3,6),5,162},
{IF_BW(0,3,7),5,175},
{IF_BW(0,2,1),0,189},
{IF_BW(0,2,2),0,210},
{IF_BW(0,2,3),3,227},
{IF_BW(0,2,4),0,240},
{IF_BW(0,2,5),5,282},
{IF_BW(0,2,6),5,322},
{IF_BW(0,2,7),5,347},
{IF_BW(0,1,1),0,377},
{IF_BW(0,1,2),0,417},
{IF_BW(0,1,3),1,452},
{IF_BW(0,1,4),0,479},
{IF_BW(0,1,5),5,562},
{IF_BW(0,1,6),5,641},
{IF_BW(0,1,7),5,692},
{IF_BW(0,0,1),0,752},
{IF_BW(0,0,2),0,832},
{IF_BW(0,0,3),0,900},
{IF_BW(0,0,4),-1,953},
{IF_BW(0,0,5),9,1121},
{IF_BW(0,0,6),2,1279},
{IF_BW(0,0,7),5,1379},
{IF_BW(1,1,4),20,1428},
{IF_BW(1,1,5),26,1678},
{IF_BW(1,1,9),-50,1811},
{IF_BW(1,0,15),-100,1915},
{IF_BW(1,0,1),20,2251},
{IF_BW(1,0,2),22,2488},
{IF_BW(1,0,3),21,2693},
{IF_BW(1,0,4),15,2849},
{IF_BW(1,0,8),-15,3355},
{IF_BW(1,0,9),-53,3618},
{IF_BW(1,0,10),-15,4202},
{IF_BW(1,0,11),-13,4684},
{IF_BW(1,0,12),-20,5188},
{IF_BW(1,0,13),-14,5770},
{IF_BW(1,0,14),-9,6207},
#else
{IF_BW(0,5,3),1,31},
{IF_BW(0,5,4),-4,32},
{IF_BW(0,5,5),1,37},
{IF_BW(0,5,6),-3,42},
{IF_BW(0,5,7),-3,45},
{IF_BW(0,4,1),-4,49},
{IF_BW(0,4,2),-4,54},
{IF_BW(0,4,3),-3,59},
{IF_BW(0,4,4),-4,61},
{IF_BW(0,4,5),1,72},
{IF_BW(0,4,6),1,82},
{IF_BW(0,4,7),1,88},
{IF_BW(0,3,1),-4,95},
{IF_BW(0,3,2),-4,106},
{IF_BW(0,3,3),-3,115},
{IF_BW(0,3,4),-8,121},
{IF_BW(0,3,5),1,142},
{IF_BW(0,3,6),1,162},
{IF_BW(0,3,7),1,175},
{IF_BW(0,2,1),-4,189},
{IF_BW(0,2,2),-4,210},
{IF_BW(0,2,3),1,227},
{IF_BW(0,2,4),-4,240},
{IF_BW(0,2,5),1,282},
{IF_BW(0,2,6),1,322},
{IF_BW(0,2,7),1,347},
{IF_BW(0,1,1),-4,377},
{IF_BW(0,1,2),-4,417},
{IF_BW(0,1,3),-3,452},
{IF_BW(0,1,4),-4,479},
{IF_BW(0,1,5),1,562},
{IF_BW(0,1,6),1,641},
{IF_BW(0,1,7),1,692},
{IF_BW(0,0,1),-4,752},
{IF_BW(0,0,2),-4,832},
{IF_BW(0,0,3),-3,900},
{IF_BW(0,0,4),-4,953},
{IF_BW(0,0,5),6,1121},
{IF_BW(0,0,6),1,1279},
{IF_BW(0,0,7),1,1379},
{IF_BW(1,1,4),16,1428},
{IF_BW(1,1,5),22,1678},
{IF_BW(1,1,9),-53,1811},
{IF_BW(1,0,15),-104,1915},
{IF_BW(1,0,1),16,2251},
{IF_BW(1,0,2),21,2488},
{IF_BW(1,0,3),17,2693},
{IF_BW(1,0,4),11,2849},
{IF_BW(1,0,8),-19,3355},
{IF_BW(1,0,9),-54,3618},
{IF_BW(1,0,10),-14,4202},
{IF_BW(1,0,11),-14,4684},
{IF_BW(1,0,12),-23,5188},
{IF_BW(1,0,13),-14,5770},
{IF_BW(1,0,14),-9,6207},
#endif
};
const uint8_t SI4432_RBW_count = ARRAY_COUNT(RBW_choices);
static pureRSSI_t SI4432_RSSI_correction = float_TO_PURE_RSSI(-120);
uint16_t force_rbw(int i)
{
SI4432_Write_Byte(SI4432_IF_FILTER_BW, RBW_choices[i].reg); // Write RBW settings to Si4432
SI4432_RSSI_correction = float_TO_PURE_RSSI(RBW_choices[i].RSSI_correction_x_10 - 1200)/10; // Set RSSI correction
// SI4432_RSSI_correction = float_TO_PURE_RSSI( - 1200)/10; // Set RSSI correction
SI4432_rbw_selected = i;
return RBW_choices[i].RBWx10; // RBW achieved by Si4432 in kHz * 10
}
uint16_t set_rbw(uint16_t WISH) {
uint16_t i;
for (i=0; i <ARRAY_COUNT(RBW_choices) - 1; i++)
if (WISH <= RBW_choices[i].RBWx10)
break;
return force_rbw(i);
}
uint8_t SI4432_frequency_changed = false;
uint8_t SI4432_offset_changed = false;
// #define __CACHE_BAND__ // Is not reliable!!!!!!
#ifdef __CACHE_BAND__
static uint8_t old_freq_band[2] = {-1,-1};
static uint8_t written[2]= {0,0};
#endif
void SI4432_Set_Frequency ( freq_t Freq ) {
// int mode = SI4432_Read_Byte(0x02) & 0x03; // Disabled as unreliable
// SI4432_Write_Byte(0x07, 0x02); // Switch to tune mode
// Freq = (Freq / 1000 ) * 1000; // force freq to 1000 grid
uint8_t hbsel;
if (0) shell_printf("%d: Freq %q\r\n", SI4432_Sel, Freq);
if (Freq >= 480000000U) {
hbsel = 1<<5;
Freq>>=1;
} else {
hbsel = 0;
}
uint8_t sbsel = 1 << 6;
uint32_t N = (Freq / config.setting_frequency_10mhz - 24)&0x1F;
uint32_t K = Freq % config.setting_frequency_10mhz;
if (N>=24) {
N=23;
K = 9999999;
}
uint32_t Carrier = (K<<2) / 625;
uint8_t Freq_Band = N | hbsel | sbsel;
// int count = 0;
// my_microsecond_delay(200);
// int s;
// while (count++ < 100 && ( (s = SI4432_Read_Byte ( 0x02 )) & 0x03 ) != 0) {
// my_microsecond_delay(100);
// }
#ifdef __CACHE_BAND__
if (old_freq_band[SI4432_Sel] == Freq_Band) {
if (written[SI4432_Sel] < 4) {
SI4432_Write_Byte(SI4432_FREQBAND, Freq_Band );
written[SI4432_Sel]++;
}
SI4432_Write_Byte(SI4432_FREQCARRIER_H, (Carrier>>8) & 0xFF );
SI4432_Write_Byte(SI4432_FREQCARRIER_L, Carrier & 0xFF );
} else {
#endif
#if 0 // Do not use multi byte write
SI4432_Write_Byte(SI4432_FREQBAND, Freq_Band ); // Freq band must be written first !!!!!!!!!!!!
SI4432_Write_Byte(SI4432_FREQCARRIER_H, (Carrier>>8) & 0xFF );
SI4432_Write_Byte(SI4432_FREQCARRIER_L, Carrier & 0xFF );
#else
SI4432_Write_3_Byte (SI4432_FREQBAND, Freq_Band, (Carrier>>8) & 0xFF, Carrier & 0xFF);
#endif
#ifdef __CACHE_BAND__
old_freq_band[SI4432_Sel] = Freq_Band;
written[SI4432_Sel] = 0;
}
#endif
SI4432_frequency_changed = true;
// if (mode == 1) // RX mode Disabled as unreliable
// SI4432_Write_Byte( 0x07, 0x07);
// else
// SI4432_Write_Byte( 0x07, 0x0B);
}
uint32_t SI4432_step_delay = 1500;
//extern int setting.repeat;
#ifdef __FAST_SWEEP__
extern deviceRSSI_t age[POINTS_COUNT];
static uint16_t buf_index = 0;
static uint16_t buf_end = 0;
static bool buf_read = false;
#if 0
int SI4432_is_fast_mode(void)
{
return buf_read;
}
#endif
#ifdef __ULTRA__
void enable_ultra(int s)
{
static int old_ultra = -1;
if (s != old_ultra) {
#ifdef TINYSA4
if (s)
palClearLine(LINE_ULTRA);
else
palSetLine(LINE_ULTRA);
#endif
old_ultra = s;
}
}
#endif
//--------------------------- Trigger -------------------
// ************** trigger mode if need
#if 0
// trigger on measure 4 point
#define T_POINTS 4
#define T_LEVEL_UNDEF (1<<(16-T_POINTS)) // should drop after 4 shifts left
#define T_LEVEL_BELOW 1
#define T_LEVEL_ABOVE 0
// Trigger mask, should have width T_POINTS bit
#define T_DOWN_MASK (0b0011) // 2 from up 2 to bottom
#define T_UP_MASK (0b1100) // 2 from bottom 2 to up
#define T_LEVEL_CLEAN ~(1<<T_POINTS) // cleanup old trigger data
#else
// trigger on measure 2 point
#define T_POINTS 2
#define T_LEVEL_UNDEF (1<<(16-T_POINTS)) // should drop after 2 shifts left
#define T_LEVEL_BELOW 1
#define T_LEVEL_ABOVE 0
// Trigger mask, should have width T_POINTS bit
#define T_DOWN_MASK (0b0001) // 1 from up 1 to bottom
#define T_UP_MASK (0b0010) // 1 from bottom 1 to up
#define T_LEVEL_CLEAN ~(1<<T_POINTS) // cleanup old trigger data
#endif
enum { ST_ARMING, ST_WAITING, ST_FILLING };
void SI4432_trigger_fill(int s, uint8_t trigger_lvl, int up_direction, int trigger_mode)
{
SI4432_Sel = s;
uint8_t rssi;
uint16_t sel = SI_nSEL[SI4432_Sel];
uint32_t t = setting.additional_step_delay_us;
systime_t measure = chVTGetSystemTimeX();
int waiting = ST_ARMING;
// __disable_irq();
int i = 0;
uint16_t t_mode = up_direction ? T_UP_MASK : T_DOWN_MASK;
uint16_t data_level = T_LEVEL_UNDEF;
do {
if (operation_requested) // allow aborting a wait for trigger
return; // abort
palClearPad(GPIOC, sel);
shiftOut(SI4432_REG_RSSI);
age[i++] = rssi = shiftIn();
palSetPad(GPIOC, sel);
if (i >= sweep_points)
i = 0;
switch (waiting) {
case ST_ARMING:
if (i == sweep_points-1) {
waiting = ST_WAITING;
setting.measure_sweep_time_us = sa_ST2US(chVTGetSystemTimeX() - measure);
}
break;
case ST_WAITING:
// Store data level bitfield (remember only last 2 states)
// T_LEVEL_UNDEF mode bit drop after 2 shifts
#if 0
if (rssi < trigger_lvl) {
data_level = ((data_level<<1) | (T_LEVEL_BELOW))&(T_LEVEL_CLEAN);
} else {
data_level = ((data_level<<1) | (T_LEVEL_ABOVE))&(T_LEVEL_CLEAN);
}
#else
data_level = ((data_level<<1) | (rssi < trigger_lvl ? T_LEVEL_BELOW : T_LEVEL_ABOVE))&(T_LEVEL_CLEAN);
#endif
if (data_level == t_mode) { // wait trigger
// if (i == 128) { // wait trigger
waiting = ST_FILLING;
switch (trigger_mode) {
case T_PRE: // Trigger at the begin of the scan
buf_index = i;
goto fill_rest;
break;
case T_POST: // Trigger at the end of the scan
buf_index = i;
goto done;
break;
case T_MID: // Trigger in the middle of the scan
buf_index = i + sweep_points/2;
if (buf_index >= sweep_points)
buf_index -= sweep_points;
break;
}
}
break;
case ST_FILLING:
if (i == buf_index)
goto done;
}
fill_rest:
if (t)
my_microsecond_delay(t);
}while(1);
done:
buf_end = buf_index;
buf_read = true;
}
void SI4432_Fill(int s, int start)
{
SI4432_Sel = s;
uint16_t sel = SI_nSEL[SI4432_Sel];
#if 0
uint32_t t = calc_min_sweep_time_us(); // Time to delay in uS for all sweep
if (t < setting.sweep_time_us){
t = setting.sweep_time_us - t;
t = t / (sweep_points - 1); // Now in uS per point
}
else
t = 0;
#endif
uint32_t t = setting.additional_step_delay_us;
systime_t measure = chVTGetSystemTimeX();
// __disable_irq();
#if 1
int stop = sweep_points - start;
int i = 0;
uint8_t *buf = &age[start];
do {
palClearPad(GPIOC, sel);
shiftOut(SI4432_REG_RSSI);
buf[i]=shiftIn();
palSetPad(GPIOC, sel);
if (++i >=stop) break;
if (t)
my_microsecond_delay(t);
} while(1);
#else
shiftInBuf(sel, SI4432_REG_RSSI, &age[start], sweep_points - start, t);
#endif
// __enable_irq();
setting.measure_sweep_time_us = sa_ST2US(chVTGetSystemTimeX() - measure);
buf_index = start; // Is used to skip 1st entry during level triggering
buf_end = sweep_points - 1;
buf_read = true;
}
#endif
#ifdef __LISTEN__
const uint8_t dBm_to_volt [] =
{
255,
225,
198,
175,
154,
136,
120,
106,
93,
82,
72,
64,
56,
50,
44,
39,
34,
30,
26,
23,
21,
18,
16,
14,
12,
11,
10,
8,
7,
7,
6,
5,
5,
};
void SI4432_Listen(int s)
{
SI4432_Sel = s;
uint16_t sel = SI_nSEL[SI4432_Sel];
uint8_t max = 0;
uint16_t count = 0;
operation_requested = OP_NONE;
do {
uint8_t v;
palClearPad(GPIOC, sel);
shiftOut(SI4432_REG_RSSI);
v = shiftIn();
palSetPad(GPIOC, sel);
if (max < v) // Peak
max = v;
if (count > 1000) { // Decay
max -= 1;
count = 0;
} else
count++;
v = max - v;
DAC->DHR12R1 = dBm_to_volt[v] << 4; // Use DAC: CH1 and put 12 bit right aligned value
} while(operation_requested == OP_NONE);
count = 0;
// dacPutChannelX(&DACD1, 0, 0);
}
#endif
#define MINIMUM_WAIT_FOR_RSSI 280
uint32_t SI4432_offset_delay = 300;
pureRSSI_t getSI4432_RSSI_correction(void){
return SI4432_RSSI_correction;
};
pureRSSI_t SI4432_RSSI(uint32_t i, int s, bool break_on_operation)
{
(void) i;
int32_t RSSI_RAW;
// SEE DATASHEET PAGE 61
#ifdef USE_SI4463 // Not used!!!!!!!
if (SI4432_Sel == 2) {
RSSI_RAW = Si446x_getRSSI();
} else
#endif
//START_PROFILE
#ifdef __FAST_SWEEP__
if (buf_read) {
pureRSSI_t val = DEVICE_TO_PURE_RSSI(age[buf_index++]);
if (buf_index >= sweep_points)
buf_index = 0;
if (buf_index == buf_end)
buf_read = false;
return val;
}
#endif
SI4432_Sel = s;
int stepdelay = SI4432_step_delay;
if (SI4432_frequency_changed) {
if (stepdelay < MINIMUM_WAIT_FOR_RSSI) {
stepdelay = MINIMUM_WAIT_FOR_RSSI;
}
SI4432_frequency_changed = false;
} else if (SI4432_offset_changed) {
// stepdelay = MINIMUM_WAIT_FOR_RSSI + (stepdelay - MINIMUM_WAIT_FOR_RSSI)/8;
stepdelay = SI4432_offset_delay;
SI4432_offset_changed = false;
}
if (stepdelay)
my_microsecond_delay(stepdelay);
// chThdSleepMicroseconds(SI4432_step_delay);
i = setting.repeat;
RSSI_RAW = 0;
do{
RSSI_RAW += DEVICE_TO_PURE_RSSI((deviceRSSI_t)SI4432_Read_Byte(SI4432_REG_RSSI));
if (--i == 0) break;
my_microsecond_delay(100);
if (break_on_operation && operation_requested)
break;
}while(1);
if (setting.repeat > 1){
RSSI_RAW = RSSI_RAW / setting.repeat;
}
// if (MODE_INPUT(setting.mode) && RSSI_RAW == 0)
// SI4432_Init();
#ifdef __SIMULATION__
#error "Fixme!!! add correct simulation in pureRSSI_t type"
RSSI_RAW = Simulated_SI4432_RSSI(i,s);
#endif
//STOP_PROFILE
// Serial.println(dBm,2);
return RSSI_RAW;
}
static const uint8_t SI4432_init_script[] =
{
SI4432_INT_ENABLE1, 0x0,
SI4432_INT_ENABLE2, 0x0,
SI4432_CLOCK_RECOVERY_GEARSHIFT, 0x00,
SI4432_AGC_OVERRIDE, 0x60,
SI4432_AFC_LOOP_GEARSHIFT_OVERRIDE, 0x00,
SI4432_AFC_TIMING_CONTROL, 0x02,
SI4432_CLOCK_RECOVERY_GEARSHIFT, 0x03,
SI4432_CLOCK_RECOVERY_OFFSET2, 0x01,
SI4432_CLOCK_RECOVERY_OFFSET1, 0x11,
SI4432_CLOCK_RECOVERY_OFFSET0, 0x11,
SI4432_CLOCK_RECOVERY_TIMING_GAIN1, 0x01,
SI4432_CLOCK_RECOVERY_TIMING_GAIN0, 0x13,
SI4432_AFC_LIMITER, 0xFF,
SI4432_DATAACCESS_CONTROL, 0x61, // Disable all packet handling
SI4432_AGC_OVERRIDE, 0x60, // AGC, no LNA, fast gain increment
SI4432_GPIO0_CONF, 0x12, // Normal
SI4432_GPIO1_CONF, 0x15,
SI4432_GPIO2_CONF, 0x1F
};
void SI4432_Sub_Init(void)
{
SI4432_Reset();
const uint8_t *p = SI4432_init_script;
while (*p) {
uint8_t r = *p++;
uint8_t v = *p++;
SI4432_Write_Byte (r,v);
}
// IF Filter Bandwidth
// set_rbw(100) ;
// SI4432_Receive();
}
#define V0_XTAL_CAPACITANCE 0x64
#define V1_XTAL_CAPACITANCE 0x64
void SI4432_Init()
{
#if 1
CS_SI0_LOW; // Drop CS so power can be removed
CS_SI1_LOW; // Drop CS so power can be removed
CS_PE_LOW; // low is the default safe state
SPI2_CLK_LOW; // low is the default safe state
SPI2_SDI_LOW; // will be set with any data out
palClearPad(GPIOB, GPIO_RF_PWR); // Drop power
chThdSleepMilliseconds(10); // Wait
palSetPad(GPIOB, GPIO_RF_PWR); // Restore power
CS_SI0_HIGH; // And set chip select lines back to inactive
CS_SI1_HIGH;
chThdSleepMilliseconds(10); // Wait
#endif
SPI2_CLK_LOW;
//DebugLine("IO set");
SI4432_Sel = SI4432_RX;
SI4432_Sub_Init();
SI4432_Sel = SI4432_LO;
SI4432_Sub_Init();
//DebugLine("1 init done");
}
void set_calibration_freq(int freq)
{
SI4432_Sel = SI4432_LO; //Select Lo module
if (freq < 0 || freq > 7 ) {
SI4432_Write_Byte(SI4432_GPIO2_CONF, 0x1F) ; // Set GPIO2 to GND
} else {
SI4432_Write_Byte(SI4432_GPIO2_CONF, 0xC0) ; // Set GPIO2 maximumdrive and clock output
SI4432_Write_Byte(Si4432_UC_OUTPUT_CLOCK, freq & 0x07) ; // Set GPIO2 frequency
}
}
#endif
#endif
//------------PE4302 -----------------------------------------------
#ifdef __PE4302__
// Comment out this define to use parallel mode PE4302
#define PE4302_en 10
void PE4302_init(void) {
CS_PE_LOW;
}
#define PE4302_DELAY 100
#if 0
void PE4302_shiftOut(uint8_t val)
{
uint8_t i;
SI4432_log(SI4432_Sel);
SI4432_log(val);
for (i = 0; i < 8; i++) {
if (val & (1 << (7 - i)))
SPI2_SDI_HIGH;
else
SPI2_SDI_LOW;
// chThdSleepMicroseconds(PE4302_DELAY);
SPI2_CLK_HIGH;
// chThdSleepMicroseconds(PE4302_DELAY);
SPI2_CLK_LOW;
// chThdSleepMicroseconds(PE4302_DELAY);
}
}
#endif
static unsigned char old_attenuation = 255;
bool PE4302_Write_Byte(unsigned char DATA )
{
if (old_attenuation == DATA)
return false;
// chThdSleepMicroseconds(PE4302_DELAY);
// SPI2_CLK_LOW;
// chThdSleepMicroseconds(PE4302_DELAY);
// PE4302_shiftOut(DATA);
shiftOut(DATA);
// chThdSleepMicroseconds(PE4302_DELAY);
CS_PE_HIGH;
// chThdSleepMicroseconds(PE4302_DELAY);
CS_PE_LOW;
// chThdSleepMicroseconds(PE4302_DELAY);
return true;
}
#endif
#if 0
//-----------------SI4432 dummy------------------
void SI4432_Write_Byte(unsigned char ADR, unsigned char DATA ) {}
unsigned char SI4432_Read_Byte(unsigned char ADR) {return ADR;}
float set_rbw(float WISH) {return (WISH > 600.0?600: (WISH<3.0?3:WISH));}
void set_calibration_freq(int p) {}
void SI4432_Set_Frequency(long f) {}
void PE4302_Write_Byte(unsigned char DATA ) {}
void PE4302_init(void) {}
#endif
#ifdef __SIMULATION__
unsigned long seed = 123456789;
extern float actual_rbw;
float myfrand(void)
{
seed = (unsigned int) (1103515245 * seed + 12345) ;