forked from SpaceTeddy/CC1101
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cc1100_arduino.cpp
1413 lines (1246 loc) · 62.1 KB
/
cc1100_arduino.cpp
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
/*------------------------------------------------------------------------------
' CC1100 Arduino Library
' ----------------------
'
'
'
'
'
' module contains helper code from other people. Thx for that
'-----------------------------------------------------------------------------*/
#include <cc1100_arduino.h>
//-------------------[global EEPROM default settings 868 Mhz]-------------------
static uint8_t cc1100_GFSK_1_2_kb[] EEMEM = {
0x07, // IOCFG2 GDO2 Output Pin Configuration
0x2E, // IOCFG1 GDO1 Output Pin Configuration
0x80, // IOCFG0 GDO0 Output Pin Configuration
0x07, // FIFOTHR RX FIFO and TX FIFO Thresholds
0x57, // SYNC1 Sync Word, High Byte
0x43, // SYNC0 Sync Word, Low Byte
0x3E, // PKTLEN Packet Length
0x0E, // PKTCTRL1 Packet Automation Control
0x45, // PKTCTRL0 Packet Automation Control
0xFF, // ADDR Device Address
0x00, // CHANNR Channel Number
0x08, // FSCTRL1 Frequency Synthesizer Control
0x00, // FSCTRL0 Frequency Synthesizer Control
0x21, // FREQ2 Frequency Control Word, High Byte
0x65, // FREQ1 Frequency Control Word, Middle Byte
0x6A, // FREQ0 Frequency Control Word, Low Byte
0xF5, // MDMCFG4 Modem Configuration
0x83, // MDMCFG3 Modem Configuration
0x13, // MDMCFG2 Modem Configuration
0xA0, // MDMCFG1 Modem Configuration
0xF8, // MDMCFG0 Modem Configuration
0x15, // DEVIATN Modem Deviation Setting
0x07, // MCSM2 Main Radio Control State Machine Configuration
0x0C, // MCSM1 Main Radio Control State Machine Configuration
0x18, // MCSM0 Main Radio Control State Machine Configuration
0x16, // FOCCFG Frequency Offset Compensation Configuration
0x6C, // BSCFG Bit Synchronization Configuration
0x03, // AGCCTRL2 AGC Control
0x40, // AGCCTRL1 AGC Control
0x91, // AGCCTRL0 AGC Control
0x02, // WOREVT1 High Byte Event0 Timeout
0x26, // WOREVT0 Low Byte Event0 Timeout
0x09, // WORCTRL Wake On Radio Control
0x56, // FREND1 Front End RX Configuration
0x17, // FREND0 Front End TX Configuration
0xA9, // FSCAL3 Frequency Synthesizer Calibration
0x0A, // FSCAL2 Frequency Synthesizer Calibration
0x00, // FSCAL1 Frequency Synthesizer Calibration
0x11, // FSCAL0 Frequency Synthesizer Calibration
0x41, // RCCTRL1 RC Oscillator Configuration
0x00, // RCCTRL0 RC Oscillator Configuration
0x59, // FSTEST Frequency Synthesizer Calibration Control,
0x7F, // PTEST Production Test
0x3F, // AGCTEST AGC Test
0x81, // TEST2 Various Test Settings
0x3F, // TEST1 Various Test Settings
0x0B // TEST0 Various Test Settings
};
static uint8_t cc1100_GFSK_38_4_kb[] EEMEM = {
0x07, // IOCFG2 GDO2 Output Pin Configuration
0x2E, // IOCFG1 GDO1 Output Pin Configuration
0x80, // IOCFG0 GDO0 Output Pin Configuration
0x07, // FIFOTHR RX FIFO and TX FIFO Thresholds
0x57, // SYNC1 Sync Word, High Byte
0x43, // SYNC0 Sync Word, Low Byte
0x3E, // PKTLEN Packet Length
0x0E, // PKTCTRL1 Packet Automation Control
0x45, // PKTCTRL0 Packet Automation Control
0xFF, // ADDR Device Address
0x00, // CHANNR Channel Number
0x06, // FSCTRL1 Frequency Synthesizer Control
0x00, // FSCTRL0 Frequency Synthesizer Control
0x21, // FREQ2 Frequency Control Word, High Byte
0x65, // FREQ1 Frequency Control Word, Middle Byte
0x6A, // FREQ0 Frequency Control Word, Low Byte
0xCA, // MDMCFG4 Modem Configuration
0x83, // MDMCFG3 Modem Configuration
0x13, // MDMCFG2 Modem Configuration
0xA0, // MDMCFG1 Modem Configuration
0xF8, // MDMCFG0 Modem Configuration
0x34, // DEVIATN Modem Deviation Setting
0x07, // MCSM2 Main Radio Control State Machine Configuration
0x0C, // MCSM1 Main Radio Control State Machine Configuration
0x18, // MCSM0 Main Radio Control State Machine Configuration
0x16, // FOCCFG Frequency Offset Compensation Configuration
0x6C, // BSCFG Bit Synchronization Configuration
0x43, // AGCCTRL2 AGC Control
0x40, // AGCCTRL1 AGC Control
0x91, // AGCCTRL0 AGC Control
0x02, // WOREVT1 High Byte Event0 Timeout
0x26, // WOREVT0 Low Byte Event0 Timeout
0x09, // WORCTRL Wake On Radio Control
0x56, // FREND1 Front End RX Configuration
0x17, // FREND0 Front End TX Configuration
0xA9, // FSCAL3 Frequency Synthesizer Calibration
0x0A, // FSCAL2 Frequency Synthesizer Calibration
0x00, // FSCAL1 Frequency Synthesizer Calibration
0x11, // FSCAL0 Frequency Synthesizer Calibration
0x41, // RCCTRL1 RC Oscillator Configuration
0x00, // RCCTRL0 RC Oscillator Configuration
0x59, // FSTEST Frequency Synthesizer Calibration Control,
0x7F, // PTEST Production Test
0x3F, // AGCTEST AGC Test
0x81, // TEST2 Various Test Settings
0x3F, // TEST1 Various Test Settings
0x0B // TEST0 Various Test Settings
};
static uint8_t cc1100_GFSK_100_kb[] EEMEM = {
0x07, // IOCFG2 GDO2 Output Pin Configuration
0x2E, // IOCFG1 GDO1 Output Pin Configuration
0x80, // IOCFG0 GDO0 Output Pin Configuration
0x07, // FIFOTHR RX FIFO and TX FIFO Thresholds
0x57, // SYNC1 Sync Word, High Byte
0x43, // SYNC0 Sync Word, Low Byte
0x3E, // PKTLEN Packet Length
0x0E, // PKTCTRL1 Packet Automation Control
0x45, // PKTCTRL0 Packet Automation Control
0xFF, // ADDR Device Address
0x00, // CHANNR Channel Number
0x08, // FSCTRL1 Frequency Synthesizer Control
0x00, // FSCTRL0 Frequency Synthesizer Control
0x21, // FREQ2 Frequency Control Word, High Byte
0x65, // FREQ1 Frequency Control Word, Middle Byte
0x6A, // FREQ0 Frequency Control Word, Low Byte
0x5B, // MDMCFG4 Modem Configuration
0xF8, // MDMCFG3 Modem Configuration
0x13, // MDMCFG2 Modem Configuration
0xA0, // MDMCFG1 Modem Configuration
0xF8, // MDMCFG0 Modem Configuration
0x47, // DEVIATN Modem Deviation Setting
0x07, // MCSM2 Main Radio Control State Machine Configuration
0x0C, // MCSM1 Main Radio Control State Machine Configuration
0x18, // MCSM0 Main Radio Control State Machine Configuration
0x1D, // FOCCFG Frequency Offset Compensation Configuration
0x1C, // BSCFG Bit Synchronization Configuration
0xC7, // AGCCTRL2 AGC Control
0x00, // AGCCTRL1 AGC Control
0xB2, // AGCCTRL0 AGC Control
0x02, // WOREVT1 High Byte Event0 Timeout
0x26, // WOREVT0 Low Byte Event0 Timeout
0x09, // WORCTRL Wake On Radio Control
0xB6, // FREND1 Front End RX Configuration
0x17, // FREND0 Front End TX Configuration
0xEA, // FSCAL3 Frequency Synthesizer Calibration
0x0A, // FSCAL2 Frequency Synthesizer Calibration
0x00, // FSCAL1 Frequency Synthesizer Calibration
0x11, // FSCAL0 Frequency Synthesizer Calibration
0x41, // RCCTRL1 RC Oscillator Configuration
0x00, // RCCTRL0 RC Oscillator Configuration
0x59, // FSTEST Frequency Synthesizer Calibration Control,
0x7F, // PTEST Production Test
0x3F, // AGCTEST AGC Test
0x81, // TEST2 Various Test Settings
0x3F, // TEST1 Various Test Settings
0x0B // TEST0 Various Test Settings
};
static uint8_t cc1100_MSK_250_kb[] EEMEM = {
0x07, // IOCFG2 GDO2 Output Pin Configuration
0x2E, // IOCFG1 GDO1 Output Pin Configuration
0x80, // IOCFG0 GDO0 Output Pin Configuration
0x07, // FIFOTHR RX FIFO and TX FIFO Thresholds
0x57, // SYNC1 Sync Word, High Byte
0x43, // SYNC0 Sync Word, Low Byte
0x3E, // PKTLEN Packet Length
0x0E, // PKTCTRL1 Packet Automation Control
0x45, // PKTCTRL0 Packet Automation Control
0xFF, // ADDR Device Address
0x00, // CHANNR Channel Number
0x0B, // FSCTRL1 Frequency Synthesizer Control
0x00, // FSCTRL0 Frequency Synthesizer Control
0x21, // FREQ2 Frequency Control Word, High Byte
0x65, // FREQ1 Frequency Control Word, Middle Byte
0x6A, // FREQ0 Frequency Control Word, Low Byte
0x2D, // MDMCFG4 Modem Configuration
0x3B, // MDMCFG3 Modem Configuration
0x73, // MDMCFG2 Modem Configuration
0xA0, // MDMCFG1 Modem Configuration
0xF8, // MDMCFG0 Modem Configuration
0x00, // DEVIATN Modem Deviation Setting
0x07, // MCSM2 Main Radio Control State Machine Configuration
0x0C, // MCSM1 Main Radio Control State Machine Configuration
0x18, // MCSM0 Main Radio Control State Machine Configuration
0x1D, // FOCCFG Frequency Offset Compensation Configuration
0x1C, // BSCFG Bit Synchronization Configuration
0xC7, // AGCCTRL2 AGC Control
0x00, // AGCCTRL1 AGC Control
0xB2, // AGCCTRL0 AGC Control
0x02, // WOREVT1 High Byte Event0 Timeout
0x26, // WOREVT0 Low Byte Event0 Timeout
0x09, // WORCTRL Wake On Radio Control
0xB6, // FREND1 Front End RX Configuration
0x17, // FREND0 Front End TX Configuration
0xEA, // FSCAL3 Frequency Synthesizer Calibration
0x0A, // FSCAL2 Frequency Synthesizer Calibration
0x00, // FSCAL1 Frequency Synthesizer Calibration
0x11, // FSCAL0 Frequency Synthesizer Calibration
0x41, // RCCTRL1 RC Oscillator Configuration
0x00, // RCCTRL0 RC Oscillator Configuration
0x59, // FSTEST Frequency Synthesizer Calibration Control,
0x7F, // PTEST Production Test
0x3F, // AGCTEST AGC Test
0x81, // TEST2 Various Test Settings
0x3F, // TEST1 Various Test Settings
0x0B // TEST0 Various Test Settings
};
static uint8_t cc1100_MSK_500_kb[] EEMEM = {
0x07, // IOCFG2 GDO2 Output Pin Configuration
0x2E, // IOCFG1 GDO1 Output Pin Configuration
0x80, // IOCFG0 GDO0 Output Pin Configuration
0x07, // FIFOTHR RX FIFO and TX FIFO Thresholds
0x57, // SYNC1 Sync Word, High Byte
0x43, // SYNC0 Sync Word, Low Byte
0x3E, // PKTLEN Packet Length
0x0E, // PKTCTRL1 Packet Automation Control
0x45, // PKTCTRL0 Packet Automation Control
0xFF, // ADDR Device Address
0x00, // CHANNR Channel Number
0x0C, // FSCTRL1 Frequency Synthesizer Control
0x00, // FSCTRL0 Frequency Synthesizer Control
0x21, // FREQ2 Frequency Control Word, High Byte
0x65, // FREQ1 Frequency Control Word, Middle Byte
0x6A, // FREQ0 Frequency Control Word, Low Byte
0x0E, // MDMCFG4 Modem Configuration
0x3B, // MDMCFG3 Modem Configuration
0x73, // MDMCFG2 Modem Configuration
0xA0, // MDMCFG1 Modem Configuration
0xF8, // MDMCFG0 Modem Configuration
0x00, // DEVIATN Modem Deviation Setting
0x07, // MCSM2 Main Radio Control State Machine Configuration
0x0C, // MCSM1 Main Radio Control State Machine Configuration
0x18, // MCSM0 Main Radio Control State Machine Configuration
0x1D, // FOCCFG Frequency Offset Compensation Configuration
0x1C, // BSCFG Bit Synchronization Configuration
0xC7, // AGCCTRL2 AGC Control
0x40, // AGCCTRL1 AGC Control
0xB2, // AGCCTRL0 AGC Control
0x02, // WOREVT1 High Byte Event0 Timeout
0x26, // WOREVT0 Low Byte Event0 Timeout
0x09, // WORCTRL Wake On Radio Control
0xB6, // FREND1 Front End RX Configuration
0x17, // FREND0 Front End TX Configuration
0xEA, // FSCAL3 Frequency Synthesizer Calibration
0x0A, // FSCAL2 Frequency Synthesizer Calibration
0x00, // FSCAL1 Frequency Synthesizer Calibration
0x19, // FSCAL0 Frequency Synthesizer Calibration
0x41, // RCCTRL1 RC Oscillator Configuration
0x00, // RCCTRL0 RC Oscillator Configuration
0x59, // FSTEST Frequency Synthesizer Calibration Control,
0x7F, // PTEST Production Test
0x3F, // AGCTEST AGC Test
0x81, // TEST2 Various Test Settings
0x3F, // TEST1 Various Test Settings
0x0B // TEST0 Various Test Settings
};
static uint8_t cc1100_OOK_4_8_kb[] EEMEM = {
0x06, // IOCFG2 GDO2 Output Pin Configuration
0x2E, // IOCFG1 GDO1 Output Pin Configuration
0x06, // IOCFG0 GDO0 Output Pin Configuration
0x47, // FIFOTHR RX FIFO and TX FIFO Thresholds
0x57, // SYNC1 Sync Word, High Byte
0x43, // SYNC0 Sync Word, Low Byte
0xFF, // PKTLEN Packet Length
0x04, // PKTCTRL1 Packet Automation Control
0x05, // PKTCTRL0 Packet Automation Control
0x00, // ADDR Device Address
0x00, // CHANNR Channel Number
0x06, // FSCTRL1 Frequency Synthesizer Control
0x00, // FSCTRL0 Frequency Synthesizer Control
0x21, // FREQ2 Frequency Control Word, High Byte
0x65, // FREQ1 Frequency Control Word, Middle Byte
0x6A, // FREQ0 Frequency Control Word, Low Byte
0x87, // MDMCFG4 Modem Configuration
0x83, // MDMCFG3 Modem Configuration
0x3B, // MDMCFG2 Modem Configuration
0x22, // MDMCFG1 Modem Configuration
0xF8, // MDMCFG0 Modem Configuration
0x15, // DEVIATN Modem Deviation Setting
0x07, // MCSM2 Main Radio Control State Machine Configuration
0x30, // MCSM1 Main Radio Control State Machine Configuration
0x18, // MCSM0 Main Radio Control State Machine Configuration
0x14, // FOCCFG Frequency Offset Compensation Configuration
0x6C, // BSCFG Bit Synchronization Configuration
0x07, // AGCCTRL2 AGC Control
0x00, // AGCCTRL1 AGC Control
0x92, // AGCCTRL0 AGC Control
0x87, // WOREVT1 High Byte Event0 Timeout
0x6B, // WOREVT0 Low Byte Event0 Timeout
0xFB, // WORCTRL Wake On Radio Control
0x56, // FREND1 Front End RX Configuration
0x17, // FREND0 Front End TX Configuration
0xE9, // FSCAL3 Frequency Synthesizer Calibration
0x2A, // FSCAL2 Frequency Synthesizer Calibration
0x00, // FSCAL1 Frequency Synthesizer Calibration
0x1F, // FSCAL0 Frequency Synthesizer Calibration
0x41, // RCCTRL1 RC Oscillator Configuration
0x00, // RCCTRL0 RC Oscillator Configuration
0x59, // FSTEST Frequency Synthesizer Calibration Control
0x7F, // PTEST Production Test
0x3F, // AGCTEST AGC Test
0x81, // TEST2 Various Test Settings
0x35, // TEST1 Various Test Settings
0x09, // TEST0 Various Test Settings
};
//Patable index: -30 -20- -15 -10 0 5 7 10 dBm
static uint8_t patable_power_315[] EEMEM = {0x17,0x1D,0x26,0x69,0x51,0x86,0xCC,0xC3};
static uint8_t patable_power_433[] EEMEM = {0x6C,0x1C,0x06,0x3A,0x51,0x85,0xC8,0xC0};
static uint8_t patable_power_868[] EEMEM = {0x03,0x17,0x1D,0x26,0x50,0x86,0xCD,0xC0};
static uint8_t patable_power_915[] EEMEM = {0x0B,0x1B,0x6D,0x67,0x50,0x85,0xC9,0xC1};
//static uint8_t patable_power_2430[] EEMEM = {0x44,0x84,0x46,0x55,0xC6,0x6E,0x9A,0xFE};
//----------------------------------[END]---------------------------------------
//-------------------------[CC1100 reset function]------------------------------
void CC1100::reset(void) // reset defined in cc1100 datasheet
{
digitalWrite(SS_PIN, LOW);
delayMicroseconds(10);
digitalWrite(SS_PIN, HIGH);
delayMicroseconds(40);
spi_write_strobe(SRES);
delay(1);
}
//-----------------------------[END]--------------------------------------------
//------------------------[set Power Down]--------------------------------------
void CC1100::powerdown(void)
{
sidle();
spi_write_strobe(SPWD); // CC1100 Power Down
}
//-----------------------------[end]--------------------------------------------
//---------------------------[WakeUp]-------------------------------------------
void CC1100::wakeup(void)
{
digitalWrite(SS_PIN, LOW);
delayMicroseconds(10);
digitalWrite(SS_PIN, HIGH);
delayMicroseconds(10);
receive(); // go to RX Mode
}
//-----------------------------[end]--------------------------------------------
//---------------------[CC1100 set debug level]---------------------------------
uint8_t CC1100::set_debug_level(uint8_t set_debug_level = 1) //default ON
{
debug_level = set_debug_level; //set debug level of CC1101 outputs
return debug_level;
}
//-----------------------------[end]--------------------------------------------
//---------------------[CC1100 get debug level]---------------------------------
uint8_t CC1100::get_debug_level(void)
{
return debug_level;
}
//-----------------------------[end]--------------------------------------------
//----------------------[CC1100 init functions]---------------------------------
uint8_t CC1100::begin(volatile uint8_t &My_addr)
{
uint8_t cc1100_freq_select, cc1100_mode_select, cc1100_channel_select;
uint8_t partnum, version;
pinMode(GDO0, INPUT); //setup AVR GPIO ports
pinMode(GDO2, INPUT);
set_debug_level(set_debug_level()); //set debug level of CC1101 outputs
if(debug_level > 0){
Serial.println(F("Init CC1100..."));
}
spi_begin(); //inits SPI Interface
reset(); //CC1100 init reset
spi_write_strobe(SFTX);delayMicroseconds(100);//flush the TX_fifo content
spi_write_strobe(SFRX);delayMicroseconds(100);//flush the RX_fifo content
partnum = spi_read_register(PARTNUM); //reads CC1100 partnumber
version = spi_read_register(VERSION); //reads CC1100 version number
//checks if valid Chip ID is found. Usualy 0x03 or 0x14. if not -> abort
if(version == 0x00 || version == 0xFF){
if(debug_level > 0){
Serial.print(F("no CC11xx found!"));
Serial.println();
}
return FALSE;
}
if(debug_level > 0){
Serial.print(F("Partnumber:"));
uart_puthex_byte(partnum);
Serial.println();
Serial.print(F("Version:"));
uart_puthex_byte(version);
Serial.println();
}
//reads setting parameters from eeprom
My_addr = eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_MY_ADDR);
cc1100_freq_select = eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_FREQUENCY);
cc1100_mode_select = eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_MODE);
cc1100_channel_select = eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_CHANNEL);
//if no valid settings are available, CC100 Powerdown and SPI disable
if( My_addr == 0xFF ||
cc1100_freq_select == 0xFF ||
cc1100_mode_select == 0xFF ||
cc1100_channel_select == 0xFF)
{
if(debug_level > 0){
Serial.print(F("no EEPROM settings..."));
Serial.println();
}
end(); //CC1100 Powerdown and disable SPI bus
return FALSE;
}
//set modulation mode
set_mode(cc1100_mode_select);
//set ISM band
set_ISM(cc1100_freq_select);
//set channel
set_channel(cc1100_channel_select);
//set output power amplifier
set_output_power_level(0); //set PA to 0dBm as default
//set my receiver address
set_myaddr(My_addr); //My_Addr from EEPROM to global variable
if(debug_level > 0){
Serial.println(F("...done"));
}
receive(); //set CC1100 in receive mode
return TRUE;
}
//-------------------------------[end]------------------------------------------
//-----------------[finish's the CC1100 operation]------------------------------
void CC1100::end(void)
{
powerdown(); //power down CC1100
spi_end(); //disable SPI Interface
}
//-------------------------------[end]------------------------------------------
//-----------------------[show all CC1100 registers]----------------------------
void CC1100::show_register_settings(void)
{
if(debug_level > 0){
uint8_t config_reg_verify[CFG_REGISTER],Patable_verify[CFG_REGISTER];
spi_read_burst(READ_BURST,config_reg_verify,CFG_REGISTER); //reads all 47 config register
spi_read_burst(PATABLE_BURST,Patable_verify,8); //reads output power settings
//show_main_settings();
Serial.println(F("Cfg_reg:"));
for(uint8_t i = 0 ; i < CFG_REGISTER; i++) //showes rx_buffer for debug
{
uart_puthex_byte(config_reg_verify[i]);Serial.print(F(" "));
if(i==9 || i==19 || i==29 || i==39) //just for beautiful output style
{
Serial.println();
}
}
Serial.println();
Serial.println(F("PaTable:"));
for(uint8_t i = 0 ; i < 8; i++) //showes rx_buffer for debug
{
uart_puthex_byte(Patable_verify[i]);Serial.print(F(" "));
}
Serial.println();
}
}
//-------------------------------[end]------------------------------------------
//--------------------------[show settings]-------------------------------------
void CC1100::show_main_settings(void)
{
if(debug_level > 0){
Serial.print(F("Mode:"));
Serial.print(eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_MODE));
Serial.println();
Serial.print(F("Frequency:"));
Serial.print(eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_FREQUENCY));
Serial.println();
Serial.print(F("Channel:"));
Serial.print(eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_CHANNEL));
Serial.println();
Serial.print(F("My_Addr:"));
Serial.print(eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_MY_ADDR));
Serial.println();
}
}
//-------------------------------[end]------------------------------------------
//----------------------------[idle mode]---------------------------------------
uint8_t CC1100::sidle(void)
{
uint8_t marcstate;
spi_write_strobe(SIDLE); //sets to idle first. must be in
marcstate = 0xFF; //set unknown/dummy state value
while(marcstate != 0x01) //0x01 = sidle
{
marcstate = (spi_read_register(MARCSTATE) & 0x1F); //read out state of cc1100 to be sure in RX
//uart_puthex_byte(marcstate);
}
//Serial.println();
delayMicroseconds(100);
return TRUE;
}
//-------------------------------[end]------------------------------------------
//---------------------------[transmit mode]------------------------------------
uint8_t CC1100::transmit(void)
{
uint8_t marcstate;
sidle(); //sets to idle first.
spi_write_strobe(STX); //sends the data over air
marcstate = 0xFF; //set unknown/dummy state value
while(marcstate != 0x01) //0x01 = ILDE after sending data
{
marcstate = (spi_read_register(MARCSTATE) & 0x1F); //read out state of cc1100 to be sure in IDLE and TX is finished
//uart_puthex_byte(marcstate);
}
//Serial.println();
delayMicroseconds(100);
return TRUE;
}
//-------------------------------[end]------------------------------------------
//---------------------------[receive mode]-------------------------------------
uint8_t CC1100::receive(void)
{
uint8_t marcstate;
sidle(); //sets to idle first.
spi_write_strobe(SRX); //writes receive strobe (receive mode)
marcstate = 0xFF; //set unknown/dummy state value
while(marcstate != 0x0D) //0x0D = RX
{
marcstate = (spi_read_register(MARCSTATE) & 0x1F); //read out state of cc1100 to be sure in RX
//uart_puthex_byte(marcstate);
}
//Serial.println();
delayMicroseconds(100);
return TRUE;
}
//-------------------------------[end]------------------------------------------
//------------[enables WOR Mode EVENT0 ~1890ms; rx_timeout ~235ms]--------------------
void CC1100::wor_enable()
{
/*
EVENT1 = WORCTRL[6:4] -> Datasheet page 88
EVENT0 = (750/Xtal)*(WOREVT1<<8+WOREVT0)*2^(5*WOR_RES) = (750/26Meg)*65407*2^(5*0) = 1.89s
(WOR_RES=0;RX_TIME=0) -> Datasheet page 80
i.E RX_TIMEOUT = EVENT0* (3.6038) *26/26Meg = 235.8ms
(WOR_RES=0;RX_TIME=1) -> Datasheet page 80
i.E.RX_TIMEOUT = EVENT0* (1.8029) *26/26Meg = 117.9ms
*/
sidle();
spi_write_register(MCSM0, 0x18); //FS Autocalibration
spi_write_register(MCSM2, 0x01); //MCSM2.RX_TIME = 1b
// configure EVENT0 time
spi_write_register(WOREVT1, 0xFF); //High byte Event0 timeout
spi_write_register(WOREVT0, 0x7F); //Low byte Event0 timeout
// configure EVENT1 time
spi_write_register(WORCTRL, 0x78); //WOR_RES=0b; tEVENT1=0111b=48d -> 48*(750/26MHz)= 1.385ms
spi_write_strobe(SFRX); //flush RX buffer
spi_write_strobe(SWORRST); //resets the WOR timer to the programmed Event 1
spi_write_strobe(SWOR); //put the radio in WOR mode when CSn is released
delayMicroseconds(100);
}
//-------------------------------[end]------------------------------------------
//------------------------[disable WOR Mode]-------------------------------------
void CC1100::wor_disable()
{
sidle(); //exit WOR Mode
spi_write_register(MCSM2, 0x07); //stay in RX. No RX timeout
}
//-------------------------------[end]------------------------------------------
//------------------------[resets WOR Timer]------------------------------------
void CC1100::wor_reset()
{
sidle(); //go to IDLE
spi_write_register(MCSM2, 0x01); //MCSM2.RX_TIME = 1b
spi_write_strobe(SFRX); //flush RX buffer
spi_write_strobe(SWORRST); //resets the WOR timer to the programmed Event 1
spi_write_strobe(SWOR); //put the radio in WOR mode when CSn is released
delayMicroseconds(100);
}
//-------------------------------[end]------------------------------------------
//-------------------------[tx_payload_burst]-----------------------------------
uint8_t CC1100::tx_payload_burst(uint8_t my_addr, uint8_t rx_addr,
uint8_t *txbuffer, uint8_t length)
{
txbuffer[0] = length-1;
txbuffer[1] = rx_addr;
txbuffer[2] = my_addr;
spi_write_burst(TXFIFO_BURST,txbuffer,length); //writes TX_Buffer +1 because of pktlen must be also transfered
if(debug_level > 0){
Serial.print(F("TX_FIFO:"));
for(uint8_t i = 0 ; i < length; i++) //TX_fifo debug out
{
uart_puthex_byte(txbuffer[i]);
}
Serial.println();
}
return TRUE;
}
//-------------------------------[end]------------------------------------------
//------------------[rx_payload_burst - package received]-----------------------
uint8_t CC1100::rx_payload_burst(uint8_t rxbuffer[], uint8_t &pktlen)
{
uint8_t bytes_in_RXFIFO = 0;
uint8_t res = 0;
bytes_in_RXFIFO = spi_read_register(RXBYTES); //reads the number of bytes in RXFIFO
if((bytes_in_RXFIFO & 0x7F) && !(bytes_in_RXFIFO & 0x80)) //if bytes in buffer and no RX Overflow
{
spi_read_burst(RXFIFO_BURST, rxbuffer, bytes_in_RXFIFO);
pktlen = rxbuffer[0];
res = TRUE;
}
else
{
if(debug_level > 0){
Serial.print(F("no bytes in RX buffer or RX Overflow!: "));Serial.println(bytes_in_RXFIFO);
}
sidle(); //set to IDLE
spi_write_strobe(SFRX);delayMicroseconds(100); //flush RX Buffer
receive(); //set to receive mode
res = FALSE;
}
return res;
}
//-------------------------------[end]------------------------------------------
//---------------------------[sent packet]--------------------------------------
uint8_t CC1100::sent_packet(uint8_t my_addr, uint8_t rx_addr, uint8_t *txbuffer,
uint8_t pktlen, uint8_t tx_retries)
{
uint8_t pktlen_ack; //default package len for ACK
uint8_t rxbuffer[FIFOBUFFER];
uint8_t tx_retries_count = 0;
uint8_t from_sender;
uint16_t ackWaitCounter = 0;
if(pktlen > (FIFOBUFFER - 1)) //FIFO overflow check
{
printf("ERROR: package size overflow\r\n");
return FALSE;
}
do //sent package out with retries
{
tx_payload_burst(my_addr, rx_addr, txbuffer, pktlen); //loads the data in cc1100 buffer
transmit(); //sents data over air
receive(); //receive mode
if(rx_addr == BROADCAST_ADDRESS){ //no wait acknowledge if sent to broadcast address or tx_retries = 0
return TRUE; //successful sent to BROADCAST_ADDRESS
}
while (ackWaitCounter < ACK_TIMEOUT ) //wait for an acknowledge
{
if (packet_available() == TRUE) //if RF package received check package acknowge
{
from_sender = rx_addr; //the original message sender address
rx_fifo_erase(rxbuffer); //erase RX software buffer
rx_payload_burst(rxbuffer, pktlen_ack); //reads package in buffer
check_acknowledge(rxbuffer, pktlen_ack, from_sender, my_addr); //check if received message is an acknowledge from client
return TRUE; //package successfully sent
}
else{
ackWaitCounter++; //increment ACK wait counter
delay(1); //delay to give receiver time
}
}
ackWaitCounter = 0; //resets the ACK_Timeout
tx_retries_count++; //increase tx retry counter
if(debug_level > 0){ //debug output messages
Serial.print(F(" #:"));
uart_puthex_byte(tx_retries_count-1);
Serial.println();
}
}while(tx_retries_count <= tx_retries); //while count of retries is reaches
return FALSE; //sent failed. too many retries
}
//-------------------------------[end]------------------------------------------
//--------------------------[sent ACKNOWLEDGE]------------------------------------
void CC1100::sent_acknowledge(uint8_t my_addr, uint8_t tx_addr)
{
uint8_t pktlen = 0x06; //complete Pktlen for ACK packet
uint8_t tx_buffer[0x06]; //tx buffer array init
tx_buffer[3] = 'A'; tx_buffer[4] = 'c'; tx_buffer[5] = 'k'; //fill buffer with ACK Payload
tx_payload_burst(my_addr, tx_addr, tx_buffer, pktlen); //load payload to CC1100
transmit(); //sent package over the air
receive(); //set CC1100 in receive mode
if(debug_level > 0){ //debut output
Serial.println(F("Ack_sent!"));
}
}
//-------------------------------[end]------------------------------------------
//----------------------[check if Packet is received]---------------------------
uint8_t CC1100::packet_available()
{
if(digitalRead(GDO2) == TRUE) //if RF package received
{
if(spi_read_register(IOCFG2) == 0x06) //if sync word detect mode is used
{
while(digitalRead(GDO2) == TRUE){ //wait till sync word is fully received
Serial.println(F("!"));
}
}
if(debug_level > 0){
//Serial.println("Pkt->:");
}
return TRUE;
}
return FALSE;
}
//-------------------------------[end]------------------------------------------
//------------------[check Payload for ACK or Data]-----------------------------
uint8_t CC1100::get_payload(uint8_t rxbuffer[], uint8_t &pktlen, uint8_t &my_addr,
uint8_t &sender, int8_t &rssi_dbm, uint8_t &lqi)
{
uint8_t crc;
rx_fifo_erase(rxbuffer); //delete rx_fifo bufffer
if(rx_payload_burst(rxbuffer, pktlen) == FALSE) //read package in buffer
{
rx_fifo_erase(rxbuffer); //delete rx_fifo bufffer
return FALSE; //exit
}
else
{
my_addr = rxbuffer[1]; //set receiver address to my_addr
sender = rxbuffer[2];
if(check_acknowledge(rxbuffer, pktlen, sender, my_addr) == TRUE) //acknowlage received?
{
rx_fifo_erase(rxbuffer); //delete rx_fifo bufffer
return FALSE; //Ack received -> finished
}
else //real data, and sent acknowladge
{
rssi_dbm = rssi_convert(rxbuffer[pktlen + 1]); //converts receiver strength to dBm
lqi = lqi_convert(rxbuffer[pktlen + 2]); //get rf quialtiy indicator
crc = check_crc(lqi); //get packet CRC
if(debug_level > 0){ //debug output messages
if(rxbuffer[1] == BROADCAST_ADDRESS) //if my receiver address is BROADCAST_ADDRESS
{
Serial.println(F("BROADCAST message"));
}
Serial.print(F("RX_FIFO:"));
for(uint8_t i = 0 ; i < pktlen + 1; i++) //showes rx_buffer for debug
{
uart_puthex_byte(rxbuffer[i]);
}
Serial.print(" |");
uart_puthex_byte(rxbuffer[pktlen+1]);
uart_puthex_byte(rxbuffer[pktlen+2]);
Serial.print("|");
Serial.println();
Serial.print(F("RSSI:"));uart_puti(rssi_dbm);Serial.print(F(" "));
Serial.print(F("LQI:"));uart_puthex_byte(lqi);Serial.print(F(" "));
Serial.print(F("CRC:"));uart_puthex_byte(crc);
Serial.println();
}
my_addr = rxbuffer[1]; //set receiver address to my_addr
sender = rxbuffer[2]; //set from_sender address
if(my_addr != BROADCAST_ADDRESS) //send only ack if no BROADCAST_ADDRESS
{
sent_acknowledge(my_addr, sender); //sending acknowledge to sender!
}
return TRUE;
}
return FALSE;
}
}
//-------------------------------[end]------------------------------------------
//-------------------------[check ACKNOWLEDGE]------------------------------------
uint8_t CC1100::check_acknowledge(uint8_t *rxbuffer, uint8_t pktlen, uint8_t sender, uint8_t my_addr)
{
int8_t rssi_dbm;
uint8_t crc, lqi;
if((pktlen == 0x05 && \
(rxbuffer[1] == my_addr || rxbuffer[1] == BROADCAST_ADDRESS)) && \
rxbuffer[2] == sender && \
rxbuffer[3] == 'A' && rxbuffer[4] == 'c' && rxbuffer[5] == 'k') //acknowledge received!
{
if(rxbuffer[1] == BROADCAST_ADDRESS){ //if receiver address BROADCAST_ADDRESS skip acknowledge
if(debug_level > 0){
Serial.println(F("BROADCAST ACK"));
}
return FALSE;
}
rssi_dbm = rssi_convert(rxbuffer[pktlen + 1]);
lqi = lqi_convert(rxbuffer[pktlen + 2]);
crc = check_crc(lqi);
if(debug_level > 0){
//Serial.println();
Serial.print(F("ACK! "));
Serial.print(F("RSSI:"));uart_puti(rssi_dbm);Serial.print(F(" "));
Serial.print(F("LQI:"));uart_puthex_byte(lqi);Serial.print(F(" "));
Serial.print(F("CRC:"));uart_puthex_byte(crc);
Serial.println();
}
return TRUE;
}
return FALSE;
}
//-------------------------------[end]------------------------------------------
//------------[check if Packet is received within defined time in ms]-----------
uint8_t CC1100::wait_for_packet(uint16_t milliseconds)
{
for(uint16_t i = 0; i < milliseconds; i++)
{
delay(1); //delay till system has data available
if (packet_available())
{
return TRUE;
}
}
//Serial.println(F("no packet received!"));
return FALSE;
}
//-------------------------------[end]------------------------------------------
//--------------------------[tx_fifo_erase]-------------------------------------
void CC1100::tx_fifo_erase(uint8_t *txbuffer)
{
memset(txbuffer, 0, sizeof(FIFOBUFFER)); //erased the TX_fifo array content to "0"
}
//-------------------------------[end]------------------------------------------
//--------------------------[rx_fifo_erase]-------------------------------------
void CC1100::rx_fifo_erase(uint8_t *rxbuffer)
{
memset(rxbuffer, 0, sizeof(FIFOBUFFER)); //erased the RX_fifo array content to "0"
}
//-------------------------------[end]------------------------------------------
//------------------------[set CC1100 address]----------------------------------
void CC1100::set_myaddr(uint8_t addr)
{
spi_write_register(ADDR,addr); //stores MyAddr in the CC1100
if(eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_MY_ADDR) != addr)
{
eeprom_write_byte((uint8_t*)EEPROM_ADDRESS_CC1100_MY_ADDR, addr); //writes byte to eeprom
}
}
//-------------------------------[end]------------------------------------------
//---------------------------[set channel]--------------------------------------
void CC1100::set_channel(uint8_t channel)
{
spi_write_register(CHANNR,channel); //stores the new channel # in the CC1100
if(eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_CHANNEL) != channel)
{
eeprom_write_byte((uint8_t*)EEPROM_ADDRESS_CC1100_CHANNEL, channel); //writes byte to eeprom
}
}
//-------------------------------[end]------------------------------------------
//-[set modulation mode 1 = GFSK_1_2_kb; 2 = GFSK_38_4_kb; 3 = GFSK_100_kb; 4 = MSK_250_kb; 5 = MSK_500_kb; 6 = OOK_4_8_kb]-
void CC1100::set_mode(uint8_t mode)
{
uint8_t Cfg_reg[CFG_REGISTER];
switch (mode)
{
case 0x01:
eeprom_read_block(Cfg_reg,cc1100_GFSK_1_2_kb,CFG_REGISTER); //sets up settings for GFSK 1,2 kbit mode/speed
break;
case 0x02:
eeprom_read_block(Cfg_reg,cc1100_GFSK_38_4_kb,CFG_REGISTER); //sets up settings for GFSK 38,4 kbit mode/speed
break;
case 0x03:
eeprom_read_block(Cfg_reg,cc1100_GFSK_100_kb,CFG_REGISTER); //sets up settings for GFSK 100 kbit mode/speed
break;
case 0x04:
eeprom_read_block(Cfg_reg,cc1100_MSK_250_kb,CFG_REGISTER); //sets up settings for GFSK 38,4 kbit mode/speed
break;
case 0x05:
eeprom_read_block(Cfg_reg,cc1100_MSK_500_kb,CFG_REGISTER); //sets up settings for GFSK 38,4 kbit mode/speed
break;
case 0x06:
eeprom_read_block(Cfg_reg,cc1100_OOK_4_8_kb,CFG_REGISTER); //sets up settings for GFSK 38,4 kbit mode/speed
break;
default:
eeprom_read_block(Cfg_reg,cc1100_GFSK_38_4_kb,CFG_REGISTER); //sets up settings for GFSK 38,4 kbit mode/speed
mode = 0x02;
break;
}
spi_write_burst(WRITE_BURST,Cfg_reg,CFG_REGISTER); //writes all 47 config register
if(eeprom_read_byte((const uint8_t*)EEPROM_ADDRESS_CC1100_MODE) != mode)
{
eeprom_write_byte((uint8_t*)EEPROM_ADDRESS_CC1100_MODE, mode); //sets the cc1100 frequency
}
}
//-------------------------------[end]------------------------------------------
//---------[set ISM Band 1=315MHz; 2=433MHz; 3=868MHz; 4=915MHz]----------------
void CC1100::set_ISM(uint8_t ism_freq)
{
uint8_t freq2, freq1, freq0;
uint8_t Patable[8];
switch (ism_freq) //loads the RF freq which is defined in cc1100_freq_select
{
case 0x01: //315MHz
freq2=0x0C;
freq1=0x1D;
freq0=0x89;
eeprom_read_block(Patable,patable_power_315,8);
break;
case 0x02: //433.92MHz
freq2=0x10;
freq1=0xB0;
freq0=0x71;
eeprom_read_block(Patable,patable_power_433,8);
break;
case 0x03: //868.3MHz