forked from notpike/The-Fonz
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchipcon_nic.py
2275 lines (1884 loc) · 79.9 KB
/
chipcon_nic.py
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
#!/usr/bin/env ipython
import re
import sys
import usb
import code
import time
import struct
import pickle
import threading
#from chipcondefs import *
from chipcon_usb import *
# band limits in Hz
FREQ_MIN_300 = 281000000
FREQ_MAX_300 = 361000000
FREQ_MIN_400 = 378000000
FREQ_MAX_400 = 481000000
FREQ_MIN_900 = 749000000
FREQ_MAX_900 = 962000000
# band transition points in Hz
FREQ_EDGE_400 = 369000000
FREQ_EDGE_900 = 615000000
# VCO transition points in Hz
FREQ_MID_300 = 318000000
FREQ_MID_400 = 424000000
FREQ_MID_900 = 848000000
SYNCM_NONE = 0
SYNCM_15_of_16 = 1
SYNCM_16_of_16 = 2
SYNCM_30_of_32 = 3
SYNCM_CARRIER = 4
SYNCM_CARRIER_15_of_16 = 5
SYNCM_CARRIER_16_of_16 = 6
SYNCM_CARRIER_30_of_32 = 7
RF_SUCCESS = 0
RF_MAX_TX_BLOCK = 255
RF_MAX_TX_CHUNK = 240 # must match MAX_TX_MSGLEN in firmware/include/FHSS.h
# and be divisible by 16 for crypto operations
RF_MAX_TX_LONG = 65535
RF_MAX_RX_BLOCK = 512 # must match BUFFER_SIZE definition in firmware/include/cc1111rf.h
APP_NIC = 0x42
APP_SPECAN = 0x43
NIC_RECV = 0x1
NIC_XMIT = 0x2
NIC_SET_ID = 0x3
NIC_SET_RECV_LARGE = 0x5
NIC_SET_AES_MODE = 0x6
NIC_GET_AES_MODE = 0x7
NIC_SET_AES_IV = 0x8
NIC_SET_AES_KEY = 0x9
NIC_SET_AMP_MODE = 0xa
NIC_GET_AMP_MODE = 0xb
NIC_XMIT_LONG = 0xc
NIC_XMIT_LONG_MORE = 0xd
FHSS_SET_CHANNELS = 0x10
FHSS_NEXT_CHANNEL = 0x11
FHSS_CHANGE_CHANNEL = 0x12
FHSS_SET_MAC_THRESHOLD = 0x13
FHSS_GET_MAC_THRESHOLD = 0x14
FHSS_SET_MAC_DATA = 0x15
FHSS_GET_MAC_DATA = 0x16
FHSS_XMIT = 0x17
FHSS_GET_CHANNELS = 0x18
FHSS_SET_STATE = 0x20
FHSS_GET_STATE = 0x21
FHSS_START_SYNC = 0x22
FHSS_START_HOPPING = 0x23
FHSS_STOP_HOPPING = 0x24
FHSS_STATE_NONHOPPING = 0
FHSS_STATE_DISCOVERY = 1
FHSS_STATE_SYNCHING = 2
FHSS_LAST_NONHOPPING_STATE = FHSS_STATE_SYNCHING
FHSS_STATE_SYNCHED = 3
FHSS_STATE_SYNC_MASTER = 4
FHSS_STATE_SYNCINGMASTER = 5
FHSS_LAST_STATE = 5 # used for testing
FHSS_STATES = {}
for key,val in globals().items():
if key.startswith("FHSS_STATE_"):
FHSS_STATES[key] = val
FHSS_STATES[val] = key
""" MODULATIONS
Note that MSK is only supported for data rates above 26 kBaud and GFSK,
ASK , and OOK is only supported for data rate up until 250 kBaud. MSK
cannot be used if Manchester encoding/decoding is enabled.
"""
MOD_2FSK = 0x00
MOD_GFSK = 0x10
MOD_ASK_OOK = 0x30
MOD_MSK = 0x70
MANCHESTER = 0x08
MODULATIONS = {
MOD_2FSK : "2FSK",
MOD_GFSK : "GFSK",
MOD_ASK_OOK : "ASK/OOK",
MOD_MSK : "MSK",
MOD_2FSK | MANCHESTER : "2FSK/Manchester encoding",
MOD_GFSK | MANCHESTER : "GFSK/Manchester encoding",
MOD_ASK_OOK | MANCHESTER : "ASK/OOK/Manchester encoding",
MOD_MSK | MANCHESTER : "MSK/Manchester encoding",
}
SYNCMODES = {
SYNCM_NONE: "None",
SYNCM_15_of_16: "15 of 16 bits must match",
SYNCM_16_of_16: "16 of 16 bits must match",
SYNCM_30_of_32: "30 of 32 sync bits must match",
SYNCM_CARRIER: "Carrier Detect",
SYNCM_CARRIER_15_of_16: "Carrier Detect and 15 of 16 sync bits must match",
SYNCM_CARRIER_16_of_16: "Carrier Detect and 16 of 16 sync bits must match",
SYNCM_CARRIER_30_of_32: "Carrier Detect and 30 of 32 sync bits must match",
}
BSLIMITS = {
BSCFG_BS_LIMIT_0: "No data rate offset compensation performed",
BSCFG_BS_LIMIT_3: "+/- 3.125% data rate offset",
BSCFG_BS_LIMIT_6: "+/- 6.25% data rate offset",
BSCFG_BS_LIMIT_12: "+/- 12.5% data rate offset",
}
AESMODES = {
ENCCS_MODE_CBC: "CBC - Cipher Block Chaining",
ENCCS_MODE_CBCMAC: "CBC-MAC - Cipher Block Chaining Message Authentication Code",
ENCCS_MODE_CFB: "CFB - Cipher Feedback",
ENCCS_MODE_CTR: "CTR - Counter",
ENCCS_MODE_ECB: "ECB - Electronic Codebook",
ENCCS_MODE_OFB: "OFB - Output Feedback",
}
NUM_PREAMBLE = [2, 3, 4, 6, 8, 12, 16, 24 ]
ADR_CHK_TYPES = [
"No address check",
"Address Check, No Broadcast",
"Address Check, 0x00 is broadcast",
"Address Check, 0x00 and 0xff are broadcast",
]
PKT_FORMATS = [
"Normal mode",
"reserved...",
"Random TX mode",
"reserved",
]
LENGTH_CONFIGS = [
"Fixed Packet Mode",
"Variable Packet Mode (len=first byte after sync word)",
"reserved",
"reserved",
]
MARC_STATE_MAPPINGS = [
(0, 'MARC_STATE_SLEEP', RFST_SIDLE),
(1, 'MARC_STATE_IDLE', RFST_SIDLE),
(3, 'MARC_STATE_VCOON_MC', RFST_SIDLE),
(4, 'MARC_STATE_REGON_MC', RFST_SIDLE),
(5, 'MARC_STATE_MANCAL', RFST_SCAL),
(6, 'MARC_STATE_VCOON', RFST_SIDLE),
(7, 'MARC_STATE_REGON', RFST_SIDLE),
(8, 'MARC_STATE_STARTCAL', RFST_SCAL),
(9, 'MARC_STATE_BWBOOST', RFST_SIDLE),
(10, 'MARC_STATE_FS_LOCK', RFST_SIDLE),
(11, 'MARC_STATE_IFADCON', RFST_SIDLE),
(12, 'MARC_STATE_ENDCAL', RFST_SCAL),
(13, 'MARC_STATE_RX', RFST_SRX),
(14, 'MARC_STATE_RX_END', RFST_SRX ), # FIXME: this should actually be the config setting in register
(15, 'MARC_STATE_RX_RST', RFST_SRX),
(16, 'MARC_STATE_TXRX_SWITCH', RFST_SIDLE),
(17, 'MARC_STATE_RX_OVERFLOW', RFST_SIDLE),
(18, 'MARC_STATE_FSTXON', RFST_SFSTXON),
(19, 'MARC_STATE_TX', RFST_STX),
(20, 'MARC_STATE_TX_END', RFST_STX), # FIXME: this should actually be the config setting in register
(21, 'MARC_STATE_RXTX_SWITCH', RFST_SIDLE),
(22, 'MARC_STATE_TX_UNDERFLOW', RFST_SIDLE) # FIXME: this should actually be the config setting in register
]
MODES = {}
for num,name,rfst in MARC_STATE_MAPPINGS:
MODES[num] = name
MODES[name] = num
T2SETTINGS = {}
T2SETTINGS_24MHz = {
100: (4, 147, 3),
150: (5, 110, 3),
200: (5, 146, 3),
250: (5, 183, 3),
}
T2SETTINGS_26MHz = {
100: (4, 158, 3),
150: (5, 119, 3),
200: (5, 158, 3),
250: (5, 198, 3),
}
TIP = (64,128,256,1024)
def makeFriendlyAscii(instring):
out = []
start = 0
last = -1
instrlen = len(instring)
for cidx in xrange(instrlen):
if (0x20 < ord(instring[cidx]) < 0x7f):
if last < cidx-1:
out.append( "." * (cidx-1-last))
start = cidx
last = cidx
else:
if last == cidx-1:
out.append( instring[ start:last+1 ] )
if last != cidx:
out.append( "." * (cidx-last) )
else: # if start == 0:
out.append( instring[ start: ] )
return ''.join(out)
def calculateT2(tick_ms, mhz=24):
# each tick, not each cycle
TICKSPD = [(mhz*1000000/pow(2,x)) for x in range(8)]
tick_ms = 1.0*tick_ms/1000
candidates = []
for tickidx in xrange(8):
for tipidx in range(4):
for PR in xrange(256):
T = 1.0 * PR * TIP[tipidx] / TICKSPD[tickidx]
if abs(T-tick_ms) < .010:
candidates.append((T, tickidx, tipidx, PR))
diff = 1024
best = None
for c in candidates:
if abs(c[0] - tick_ms) < diff:
best = c
diff = abs(c[0] - tick_ms)
return best
#return ms, candidates, best
class EnDeCode:
def encode(self, msg):
raise Exception("EnDeCode.encode() not implemented. Each subclass must implement their own")
def decode(self, msg):
raise Exception("EnDeCode.encode() not implemented. Each subclass must implement their own")
def savePkts(pkts, filename):
pickle.dump(pkts, file(filename, 'a'))
def loadPkts(filename):
return pickle.load( file(filename, 'r'))
def printSyncWords(syncworddict):
print "SyncWords seen:"
tmp = []
for x,y in syncworddict.items():
tmp.append((y,x))
tmp.sort()
for y,x in tmp:
print("0x%.4x: %d" % (x,y))
CHIPmhz = {
0x91: 24,
0x81: 26,
0x11: 24,
0x01: 26,
}
class NICxx11(USBDongle):
'''
NICxx11 implements radio-specific code for CCxx11 chips (2511, 1111),
as the xx11 chips keep a relatively consistent radio interface and use
the same radio concepts (frequency, channels, etc) and functionality
(AES, Manchester Encoding, etc).
'''
def __init__(self, idx=0, debug=False, copyDongle=None, RfMode=RFST_SRX):
USBDongle.__init__(self, idx, debug, copyDongle, RfMode)
self.max_packet_size = RF_MAX_RX_BLOCK
self.endec = None
self.mhz = CHIPmhz.get(self.chipnum)
self.freq_offset_accumulator = 0
######## RADIO METHODS #########
def setRfMode(self, rfmode, parms=''):
'''
sets the radio state to "rfmode", and makes
'''
self._rfmode = rfmode
r = self.send(APP_SYSTEM, SYS_CMD_RFMODE, "%c" % (self._rfmode) + parms)
### set standard radio state to TX/RX/IDLE (TX is pretty much only good for jamming). TX/RX modes are set to return to whatever state you choose here.
def setModeTX(self):
'''
BOTH: set radio to TX state
AND: set radio to return to TX state when done with other states
'''
self.setRfMode(RFST_STX) #FIXME: when firmware makes the change, so must this
def setModeRX(self):
'''
BOTH: set radio to RX state
AND: set radio to return to RX state when done with other states
'''
self.setRfMode(RFST_SRX)
def setModeIDLE(self):
'''
BOTH: set radio to IDLE state
AND: set radio to return to IDLE state when done with other states
'''
self.setRfMode(RFST_SIDLE)
### send raw state change to radio (doesn't update the return state for after RX/TX occurs)
def strobeModeTX(self):
'''
set radio to TX state (transient)
'''
self.poke(X_RFST, "%c"%RFST_STX)
def strobeModeRX(self):
'''
set radio to RX state (transient)
'''
self.poke(X_RFST, "%c"%RFST_SRX)
def strobeModeIDLE(self):
'''
set radio to IDLE state (transient)
'''
self.poke(X_RFST, "%c"%RFST_SIDLE)
def strobeModeFSTXON(self):
'''
set radio to FSTXON state (transient)
'''
self.poke(X_RFST, "%c"%RFST_SFSTXON)
def strobeModeCAL(self):
'''
set radio to CAL state (will return to whichever state is configured (via setMode* functions)
'''
self.poke(X_RFST, "%c"%RFST_SCAL)
def strobeModeReturn(self, marcstate=None):
"""
attempts to return the the correct mode after configuring some radio register(s).
it uses the marcstate provided (or self.radiocfg.marcstate if none are provided) to determine how to strobe the radio.
"""
#if marcstate is None:
#marcstate = self.radiocfg.marcstate
#if self._debug: print("MARCSTATE: %x returning to %x" % (marcstate, MARC_STATE_MAPPINGS[marcstate][2]) )
#self.poke(X_RFST, "%c"%MARC_STATE_MAPPINGS[marcstate][2])
self.poke(X_RFST, "%c" % self._rfmode)
#### radio config #####
def getRadioConfig(self):
bytedef = self.peek(0xdf00, 0x3e)
self.radiocfg.vsParse(bytedef)
return bytedef
def setRadioConfig(self, bytedef = None):
if bytedef is None:
bytedef = self.radiocfg.vsEmit()
statestr, marcstate = self.getMARCSTATE()
if marcstate != MARC_STATE_IDLE:
self.strobeModeIDLE()
self.poke(0xdf00, bytedef)
self.strobeModeReturn(marcstate)
#if (marcstate == MARC_STATE_RX):
#self.strobeModeRX()
#elif (marcstate == MARC_STATE_TX):
#self.strobeModeTX()
self.getRadioConfig()
return bytedef
##### GETTER/SETTERS for Radio Config/Status #####
### radio state
def getMARCSTATE(self, radiocfg=None):
if radiocfg is None:
self.getRadioConfig()
radiocfg=self.radiocfg
mode = radiocfg.marcstate
return (MODES[mode], mode)
def setRFRegister(self, regaddr, value, suppress=False):
'''
set the radio register 'regaddr' to 'value' (first setting RF state to IDLE, then returning to RX/TX)
value is always considered a 1-byte value
if 'suppress' the radio state (RX/TX/IDLE) is not modified
'''
if suppress:
self.poke(regaddr, chr(value))
return
marcstate = self.radiocfg.marcstate
if marcstate != MARC_STATE_IDLE:
self.strobeModeIDLE()
self.poke(regaddr, chr(value))
self.strobeModeReturn(marcstate)
#if (marcstate == MARC_STATE_RX):
#self.strobeModeRX()
#elif (marcstate == MARC_STATE_TX):
#self.strobeModeTX()
# if other than these, we can stay in IDLE
def setRFbits(self, addr, bitnum, bitsz, val, suppress=False):
''' sets individual bits of a register '''
mask = ((1<<bitsz) - 1) << bitnum
rmask = ~mask
temp = ord(self.peek(addr)) & rmask
temp |= ((val << bitnum) & mask)
self.setRFRegister(addr, temp, suppress=suppress)
def setEnableCCA(self, mode=3, absthresh=0, relthresh=1, magn=3, radiocfg=None):
'''
4 modes of CCA:
0 - ALWAYS, no CCA
1 - If RSSI below threshold
2 - Unless currently receiving a packet
3 - If RSSI below threshold unless currently receiving a packet
'''
if radiocfg is None:
radiocfg = self.radiocfg
else:
applyConfig = False
mcsm1 = radiocfg.mcsm1 & 0xf
mcsm1 |= (mode << 4)
radiocfg.mcsm1 = mcsm1
agcctrl2 = radiocfg.agcctrl2 & 0xf8
agcctrl2 |= magn
agcctrl1 = radiocfg.agcctrl1 & 0xc0
agcctrl1 |= (absthresh & 0xf)
agcctrl1 |= ((relthresh << 4) & 0x3)
self.setRFRegister(MCSM1, mcsm1)
self.setRFRegister(AGCCTRL1, agcctrl1)
self.setRFRegister(AGCCTRL2, agcctrl2)
def setFreq(self, freq=902000000, mhz=24, radiocfg=None, applyConfig=True):
if radiocfg is None:
radiocfg = self.radiocfg
else:
applyConfig = False
freqmult = (0x10000 / 1000000.0) / mhz
num = int(freq * freqmult)
radiocfg.freq2 = num >> 16
radiocfg.freq1 = (num>>8) & 0xff
radiocfg.freq0 = num & 0xff
if (freq > FREQ_EDGE_900 and freq < FREQ_MID_900) or (freq > FREQ_EDGE_400 and freq < FREQ_MID_400) or (freq < FREQ_MID_300):
# select low VCO
radiocfg.fscal2 = 0x0A
elif freq <1e9 and ((freq > FREQ_MID_900) or (freq > FREQ_MID_400) or (freq > FREQ_MID_300)):
# select high VCO
radiocfg.fscal2 = 0x2A
if applyConfig:
marcstate = radiocfg.marcstate
if marcstate != MARC_STATE_IDLE:
self.strobeModeIDLE()
self.poke(FREQ2, struct.pack("3B", self.radiocfg.freq2, self.radiocfg.freq1, self.radiocfg.freq0))
self.poke(FSCAL2, struct.pack("B", self.radiocfg.fscal2))
self.strobeModeReturn(marcstate)
#if (radiocfg.marcstate == MARC_STATE_RX):
#self.strobeModeRX()
#elif (radiocfg.marcstate == MARC_STATE_TX):
#self.strobeModeTX()
def getFreq(self, mhz=24, radiocfg=None):
freqmult = (0x10000 / 1000000.0) / mhz
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
num = (radiocfg.freq2<<16) + (radiocfg.freq1<<8) + radiocfg.freq0
freq = num / freqmult
return freq, hex(num)
def getFreqEst(self, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
return radiocfg.freqest
# auto-adjust frequency offset based on internal estimate from last received packet (FSK/MSK modes only)
# note radio must be in IDLE mode when called for this to have any effect
# the TI design note for this is missing from TI's main website but can be found here:
# http://e2e.ti.com/cfs-file/__key/telligent-evolution-components-attachments/00-155-01-00-00-73-46-38/DN015_5F00_Permanent_5F00_Frequency_5F00_Offset_5F00_Compensation.pdf
def adjustFreqOffset(self, mhz=24, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
self.freq_offset_accumulator += self.getFreqEst(radiocfg)
self.freq_offset_accumulator &= 0xff
self.setFsOffset(self.freq_offset_accumulator, mhz, radiocfg)
# set 'standard' power - for more complex power shaping this will need to be done manually
def setPower(self, power=None, radiocfg=None, invert=False):
if radiocfg == None:
self.getRadioConfig()
radiocfg = self.radiocfg
mod= self.getMdmModulation(radiocfg=radiocfg)
# we may be only changing PA_POWER, not power levels
if power is not None:
if mod == MOD_ASK_OOK and not invert:
radiocfg.pa_table0= 0x00
radiocfg.pa_table1= power
else:
radiocfg.pa_table0= power
radiocfg.pa_table1= 0x00
self.setRFRegister(PA_TABLE0, radiocfg.pa_table0)
self.setRFRegister(PA_TABLE1, radiocfg.pa_table1)
radiocfg.frend0 &= ~FREND0_PA_POWER
if mod == MOD_ASK_OOK:
radiocfg.frend0 |= 0x01
self.setRFRegister(FREND0, radiocfg.frend0)
# max power settings are frequency dependent, so set frequency before calling
def setMaxPower(self, radiocfg=None, invert=False):
if radiocfg == None:
self.getRadioConfig()
radiocfg = self.radiocfg
freq= self.getFreq(radiocfg=radiocfg)[0]
if freq <= 400000000:
power= 0xC2
elif freq <= 464000000:
power= 0xC0
elif freq <= 900000000:
power= 0xC2
else:
power= 0xC0
self.setPower(power, radiocfg=radiocfg, invert=invert)
def setMdmModulation(self, mod, radiocfg=None, invert=False):
if radiocfg == None:
self.getRadioConfig()
radiocfg = self.radiocfg
if (mod) & ~MDMCFG2_MOD_FORMAT:
raise(Exception("Please use constants MOD_FORMAT_* to specify modulation and "))
radiocfg.mdmcfg2 &= ~MDMCFG2_MOD_FORMAT
radiocfg.mdmcfg2 |= (mod)
power= None
# ASK_OOK needs to flip power table
if mod == MOD_ASK_OOK and not invert:
if radiocfg.pa_table1 == 0x00 and radiocfg.pa_table0 != 0x00:
power= radiocfg.pa_table0
else:
if radiocfg.pa_table0 == 0x00 and radiocfg.pa_table1 != 0x00:
power= radiocfg.pa_table1
self.setRFRegister(MDMCFG2, radiocfg.mdmcfg2)
self.setPower(power, radiocfg=radiocfg, invert=invert)
def getMdmModulation(self, radiocfg=None):
if radiocfg == None:
self.getRadioConfig()
radiocfg = self.radiocfg
mdmcfg2 = radiocfg.mdmcfg2
mod = (mdmcfg2) & MDMCFG2_MOD_FORMAT
return mod
def getMdmChanSpc(self, mhz=24, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
chanspc_m = radiocfg.mdmcfg0
chanspc_e = radiocfg.mdmcfg1 & 3
chanspc = 1000000.0 * mhz/pow(2,18) * (256 + chanspc_m) * pow(2, chanspc_e)
#print "chanspc_e: %x chanspc_m: %x chanspc: %f hz" % (chanspc_e, chanspc_m, chanspc)
return (chanspc)
def setMdmChanSpc(self, chanspc=None, chanspc_m=None, chanspc_e=None, mhz=24, radiocfg=None):
'''
calculates the appropriate exponent and mantissa and updates the correct registers
chanspc is in kHz. if you prefer, you may set the chanspc_m and chanspc_e settings
directly.
only use one or the other:
* chanspc
* chanspc_m and chanspc_e
'''
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
if (chanspc != None):
for e in range(4):
m = int(((chanspc * pow(2,18) / (1000000.0 * mhz * pow(2,e)))-256) +.5) # rounded evenly
if m < 256:
chanspc_e = e
chanspc_m = m
break
if chanspc_e is None or chanspc_m is None:
raise(Exception("ChanSpc does not translate into acceptable parameters. Should you be changing this?"))
#chanspc = 1000000.0 * mhz/pow(2,18) * (256 + chanspc_m) * pow(2, chanspc_e)
#print "chanspc_e: %x chanspc_m: %x chanspc: %f hz" % (chanspc_e, chanspc_m, chanspc)
radiocfg.mdmcfg1 &= ~MDMCFG1_CHANSPC_E # clear out old exponent value
radiocfg.mdmcfg1 |= chanspc_e
radiocfg.mdmcfg0 = chanspc_m
self.setRFRegister(MDMCFG1, (radiocfg.mdmcfg1))
self.setRFRegister(MDMCFG0, (radiocfg.mdmcfg0))
def makePktVLEN(self, maxlen=RF_MAX_TX_BLOCK, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
if maxlen > RF_MAX_TX_BLOCK:
raise(Exception("Packet too large (%d bytes). Maximum variable length packet is %d bytes." % (maxlen, RF_MAX_TX_BLOCK)))
radiocfg.pktctrl0 &= ~PKTCTRL0_LENGTH_CONFIG
radiocfg.pktctrl0 |= 1
radiocfg.pktlen = maxlen
self.setRFRegister(PKTCTRL0, (radiocfg.pktctrl0))
self.setRFRegister(PKTLEN, (radiocfg.pktlen))
def makePktFLEN(self, flen=RF_MAX_TX_BLOCK, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
if flen > EP5OUT_BUFFER_SIZE - 4:
raise(Exception("Packet too large (%d bytes). Maximum fixed length packet is %d bytes." % (flen, EP5OUT_BUFFER_SIZE - 6)))
radiocfg.pktctrl0 &= ~PKTCTRL0_LENGTH_CONFIG
# if we're sending a large block, pktlen is dealt with by the firmware
# using 'infinite' mode
if flen > RF_MAX_TX_BLOCK:
radiocfg.pktlen = 0x00
else:
radiocfg.pktlen = flen
self.setRFRegister(PKTCTRL0, (radiocfg.pktctrl0))
self.setRFRegister(PKTLEN, (radiocfg.pktlen))
def getPktLEN(self):
'''
returns (pktlen, pktctrl0)
'''
return (self.radiocfg.pktlen, self.radiocfg.pktctrl0 & PKTCTRL0_LENGTH_CONFIG)
def setEnablePktCRC(self, enable=True, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
crcE = (0,1)[enable]<<2
crcM = ~(1<<2)
radiocfg.pktctrl0 &= crcM
radiocfg.pktctrl0 |= crcE
self.setRFRegister(PKTCTRL0, (radiocfg.pktctrl0))
def getEnablePktCRC(self, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
return (radiocfg.pktctrl0 >>2) & 0x1
def setEnablePktDataWhitening(self, enable=True, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
dwEnable = (0,1)[enable]<<6
radiocfg.pktctrl0 &= ~PKTCTRL0_WHITE_DATA
radiocfg.pktctrl0 |= dwEnable
self.setRFRegister(PKTCTRL0, (radiocfg.pktctrl0))
def getEnablePktDataWhitening(self, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
return (radiocfg.pktctrl0 >>6) & 0x1
def setPktPQT(self, num=3, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
num &= 7
num <<= 5
numM = ~(7<<5)
radiocfg.pktctrl1 &= numM
radiocfg.pktctrl1 |= num
self.setRFRegister(PKTCTRL1, (radiocfg.pktctrl1))
def getPktPQT(self, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
return (radiocfg.pktctrl1 >> 5) & 7
def setEnablePktAppendStatus(self, enable=True, radiocfg=None):
'''
enable append status bytes. two bytes will be appended to the payload of the packet, containing
RSSI and LQI values as well as CRC OK.
'''
if radiocfg == None:
self.getRadioConfig()
radiocfg = self.radiocfg
radiocfg.pktctrl1 &= ~PKTCTRL1_APPEND_STATUS
radiocfg.pktctrl1 |= (enable<<2)
self.setRFRegister(PKTCTRL1, radiocfg.pktctrl1)
def getEnablePktAppendStatus(self, radiocfg=None):
'''
return append status bytes setting.
'''
if radiocfg == None:
self.getRadioConfig()
radiocfg = self.radiocfg
pktctrl1 = radiocfg.pktctrl1
append = (pktctrl1>>2) & 0x01
return append
def setEnableMdmManchester(self, enable=True, radiocfg=None):
if radiocfg == None:
self.getRadioConfig()
radiocfg = self.radiocfg
radiocfg.mdmcfg2 &= ~MDMCFG2_MANCHESTER_EN
radiocfg.mdmcfg2 |= (enable<<3)
self.setRFRegister(MDMCFG2, radiocfg.mdmcfg2)
def getEnableMdmManchester(self, radiocfg=None):
if radiocfg == None:
self.getRadioConfig()
radiocfg = self.radiocfg
mdmcfg2 = radiocfg.mdmcfg2
mchstr = (mdmcfg2>>3) & 0x01
return mchstr
def setEnableMdmFEC(self, enable=True, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
fecEnable = (0,1)[enable]<<7
radiocfg.mdmcfg1 &= ~MFMCFG1_FEC_EN
radiocfg.mdmcfg1 |= fecEnable
self.setRFRegister(MDMCFG1, (radiocfg.mdmcfg1))
def getEnableMdmFEC(self, radiocfg=None):
if radiocfg == None:
self.getRadioConfig()
radiocfg = self.radiocfg
mdmcfg1 = radiocfg.mdmcfg1
fecEnable = (mdmcfg1>>7) & 0x01
return fecEnable
def setEnableMdmDCFilter(self, enable=True, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
dcfEnable = (0,1)[enable]<<7
radiocfg.mdmcfg2 &= ~MDMCFG2_DEM_DCFILT_OFF
radiocfg.mdmcfg2 |= dcfEnable
self.setRFRegister(MDMCFG2, radiocfg.mdmcfg2)
def getEnableMdmDCFilter(self, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
dcfEnable = (radiocfg.mdmcfg2>>7) & 0x1
return dcfEnable
def setFsIF(self, freq_if, mhz=24, radiocfg=None):
'''
Note that the SmartRF Studio software
automatically calculates the optimum register
setting based on channel spacing and channel
filter bandwidth. (from cc1110f32.pdf)
'''
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
ifBits = freq_if * pow(2,10) / (1000000.0 * mhz)
ifBits = int(ifBits + .5) # rounded evenly
if ifBits >0x1f:
raise(Exception("FAIL: freq_if is too high? freqbits: %x (must be <0x1f)" % ifBits))
radiocfg.fsctrl1 &= ~(0x1f)
radiocfg.fsctrl1 |= int(ifBits)
self.setRFRegister(FSCTRL1, (radiocfg.fsctrl1))
def getFsIF(self, mhz=24, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
freq_if = (radiocfg.fsctrl1&0x1f) * (1000000.0 * mhz / pow(2,10))
return freq_if
def setFsOffset(self, if_off, mhz=24, radiocfg=None):
'''
Note that the SmartRF Studio software
automatically calculates the optimum register
setting based on channel spacing and channel
filter bandwidth. (from cc1110f32.pdf)
'''
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
radiocfg.fsctrl0 = if_off
self.setRFRegister(FSCTRL0, (radiocfg.fsctrl0))
def getFsOffset(self, mhz=24, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
freqoff = radiocfg.fsctrl0
return freqoff
def getChannel(self, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
self.getRadioConfig()
return radiocfg.channr
def setChannel(self, channr, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
radiocfg.channr = channr
self.setRFRegister(CHANNR, (radiocfg.channr))
def setMdmChanBW(self, bw, mhz=24, radiocfg=None):
'''
For best performance, the channel filter
bandwidth should be selected so that the
signal bandwidth occupies at most 80% of the
channel filter bandwidth. The channel centre
tolerance due to crystal accuracy should also
be subtracted from the signal bandwidth. The
following example illustrates this:
With the channel filter bandwidth set to 500
kHz, the signal should stay within 80% of 500
kHz, which is 400 kHz. Assuming 915 MHz
frequency and +/-20 ppm frequency uncertainty
for both the transmitting device and the
receiving device, the total frequency
uncertainty is +/-40 ppm of 915 MHz, which is
+/-37 kHz. If the whole transmitted signal
bandwidth is to be received within 400 kHz, the
transmitted signal bandwidth should be
maximum 400 kHz - 2*37 kHz, which is 326
kHz.
DR:1.2kb Dev:5.1khz Mod:GFSK RXBW:63kHz sensitive fsctrl1:06 mdmcfg:e5 a3 13 23 11 dev:16 foc/bscfg:17/6c agctrl:03 40 91 frend:56 10
DR:1.2kb Dev:5.1khz Mod:GFSK RXBW:63kHz lowpower fsctrl1:06 mdmcfg:e5 a3 93 23 11 dev:16 foc/bscfg:17/6c agctrl:03 40 91 frend:56 10 (DEM_DCFILT_OFF)
DR:2.4kb Dev:5.1khz Mod:GFSK RXBW:63kHz sensitive fsctrl1:06 mdmcfg:e6 a3 13 23 11 dev:16 foc/bscfg:17/6c agctrl:03 40 91 frend:56 10
DR:2.4kb Dev:5.1khz Mod:GFSK RXBW:63kHz lowpower fsctrl1:06 mdmcfg:e6 a3 93 23 11 dev:16 foc/bscfg:17/6c agctrl:03 40 91 frend:56 10 (DEM_DCFILT_OFF)
DR:38.4kb Dev:20khz Mod:GFSK RXBW:94kHz sensitive fsctrl1:08 mdmcfg:ca a3 13 23 11 dev:36 foc/bscfg:16/6c agctrl:43 40 91 frend:56 10 (IF changes, Deviation)
DR:38.4kb Dev:20khz Mod:GFSK RXBW:94kHz lowpower fsctrl1:08 mdmcfg:ca a3 93 23 11 dev:36 foc/bscfg:16/6c agctrl:43 40 91 frend:56 10 (.. DEM_DCFILT_OFF)
DR:250kb Dev:129khz Mod:GFSK RXBW:600kHz sensitive fsctrl1:0c mdmcfg:1d 55 13 23 11 dev:63 foc/bscfg:1d/1c agctrl:c7 00 b0 frend:b6 10 (IF_changes, Deviation)
DR:500kb Mod:MSK RXBW:750kHz sensitive fsctrl1:0e mdmcfg:0e 55 73 43 11 dev:00 foc/bscfg:1d/1c agctrl:c7 00 b0 frend:b6 10 (IF_changes, Modulation of course, Deviation has different meaning with MSK)
'''
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
chanbw_e = None
chanbw_m = None
for e in range(4):
m = int(((mhz*1000000.0 / (bw *pow(2,e) * 8.0 )) - 4) + .5) # rounded evenly
if m < 4:
chanbw_e = e
chanbw_m = m
break
if chanbw_e is None:
raise(Exception("ChanBW does not translate into acceptable parameters. Should you be changing this?"))
bw = 1000.0*mhz / (8.0*(4+chanbw_m) * pow(2,chanbw_e))
#print "chanbw_e: %x chanbw_m: %x chanbw: %f kHz" % (e, m, bw)
radiocfg.mdmcfg4 &= ~(MDMCFG4_CHANBW_E | MDMCFG4_CHANBW_M)
radiocfg.mdmcfg4 |= ((chanbw_e<<6) | (chanbw_m<<4))
self.setRFRegister(MDMCFG4, (radiocfg.mdmcfg4))
def getMdmChanBW(self, mhz=24, radiocfg=None):
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
chanbw_e = (radiocfg.mdmcfg4 >> 6) & 0x3
chanbw_m = (radiocfg.mdmcfg4 >> 4) & 0x3
bw = 1000000.0*mhz / (8.0*(4+chanbw_m) * pow(2,chanbw_e))
#print "chanbw_e: %x chanbw_m: %x chanbw: %f hz" % (chanbw_e, chanbw_m, bw)
return bw
def setMdmDRate(self, drate, mhz=24, radiocfg=None):
'''
set the baud of data being modulated through the radio
'''
if radiocfg==None:
self.getRadioConfig()
radiocfg = self.radiocfg
drate_e = None
drate_m = None
for e in range(16):
m = int((drate * pow(2,28) / (pow(2,e)* (mhz*1000000.0))-256) + .5) # rounded evenly
if m < 256:
drate_e = e
drate_m = m
break
if drate_e is None:
raise(Exception("DRate does not translate into acceptable parameters. Should you be changing this?"))
drate = 1000000.0 * mhz * (256+drate_m) * pow(2,drate_e) / pow(2,28)
if self._debug: print "drate_e: %x drate_m: %x drate: %f Hz" % (drate_e, drate_m, drate)
radiocfg.mdmcfg3 = drate_m
radiocfg.mdmcfg4 &= ~MDMCFG4_DRATE_E
radiocfg.mdmcfg4 |= drate_e
self.setRFRegister(MDMCFG3, (radiocfg.mdmcfg3))
self.setRFRegister(MDMCFG4, (radiocfg.mdmcfg4))
def getMdmDRate(self, mhz=24, radiocfg=None):
'''