-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathDW1000.py
1520 lines (1284 loc) · 59.2 KB
/
DW1000.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
"""
This python module contains low-level functions to interact
with the DW1000 chip using a Raspberry Pi 3.
It requires the following modules:
math, time, spidev, Rpi.GPIO, random.
"""
import time
import math
from random import randint
import spidev
import RPi.GPIO as GPIO
import DW1000Constants as C
class DW1000(object):
spi = spidev.SpiDev()
_bus = 0
_device = 0
_ss = 0
_chipSelect = None
_deviceMode = C.IDLE_MODE
_permanentReceive = False
_operationMode = [None] * 6
# [dataRate, pulseFrequency, pacSize, preambleLength, channel, preacode]
callbacks = {}
_networkAndAddress = [0] * 4
_sysctrl = [0] * 4
_chanctrl = [0] * 4
_syscfg = [0] * 4
_sysmask = [0] * 4
_txfctrl = [0] * 5
_sysstatus = [0] * 5
"""
DW1000 general configuration.
"""
def __init__(self, **kwargs):
rst = None
bus = None
device = None
valid_keys = ["irq", "rst", "bus", "device"]
for key in valid_keys:
self.__dict__[key] = kwargs.get(key)
self.spi = spidev.SpiDev()
print self.spi
self.begin(self.irq, self.rst, self.bus, self.device)
def __del__(self):
self.close()
def begin(self, irq, rst=None, bus=None, device=None):
"""
This function opens the SPI connection available on the Raspberry Pi using the chip select #0.
Normally, spidev can auto enable chip select when necessary.
However, in our case, the dw1000's chip select is connected to GPIO16 so we have to enable/disable it manually.
It also sets up the interrupt detection event on the rising edge of the interrupt pin.
Args:
irq : The GPIO pin number managing interrupts.
"""
self._bus = 0
self._device = 0
self._rst = None
self._irq = irq
# Wait 5 us to open spi connection to let the chip enter idle state
# see 2.3.2 of the DW1000 user manual (INIT).
time.sleep(C.INIT_DELAY)
GPIO.setmode(GPIO.BCM)
if bus is not None:
self._bus = bus
if device is not None:
self._device = device
self.spi.open(self._bus, self._device)
# spi.max_speed_hz = 4000000
self._deviceMode = C.IDLE_MODE
GPIO.setup(self._irq, GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.add_event_detect(self._irq, GPIO.RISING, callback=self.handleInterrupt)
# Set reset pin for physical reset
if rst is not None:
self._rst = rst
GPIO.setup(self._rst, GPIO.INPUT)
def setup(self, ss):
"""
This function defines the GPIO used for the chip select
by configuring it as an output and by setting its initial state at inactive (HIGH).
It also clears interrupt configuration and performs
a soft reset before applying initial configurations for the chip.
Args:
ss: The GPIO pin number of the chip enable/select for the SPI bus.
"""
self._chipSelect = ss
GPIO.setup(self._chipSelect, GPIO.OUT)
GPIO.output(self._chipSelect, GPIO.HIGH)
self.enableClock(C.AUTO_CLOCK)
self.softReset()
# Default system configuration
self.setArray(self._syscfg, 4, 0x00)
self.setBit(self._syscfg, 4, C.DIS_DRXB_BIT, True)
self.setBit(self._syscfg, 4, C.HIRQ_POL_BIT, True)
self.writeBytes(C.SYS_CFG, C.NO_SUB, self._syscfg, 4)
# clear interrupts configuration
self.setArray(self._sysmask, 4, 0x00)
self.writeBytes(C.SYS_MASK, C.NO_SUB, self._sysmask, 4)
self.enableClock(C.XTI_CLOCK)
self.manageLDE()
self.enableClock(C.AUTO_CLOCK)
def handleInterrupt(self, channel):
"""
Callback invoked on the rising edge of the interrupt pin. Handle the configured interruptions.
"""
# print("\nInterrupt!")
self._sysstatus = self.readBytes(C.SYS_STATUS, C.NO_SUB, self._sysstatus, 5)
# print(_sysstatus)
msgReceived = self.getBit(self._sysstatus, 5, C.RXFCG_BIT)
receiveTimeStampAvailable = self.getBit(self._sysstatus, 5, C.LDEDONE_BIT)
transmitDone = self.getBit(self._sysstatus, 5, C.TXFRS_BIT)
if transmitDone:
self.callbacks["handleSent"]()
self.clearTransmitStatus()
if receiveTimeStampAvailable:
self.setBit(self._sysstatus, 5, C.LDEDONE_BIT, True)
self.writeBytes(C.SYS_STATUS, C.NO_SUB, self._sysstatus, 5)
if self.isReceiveFailed():
self.clearReceiveStatus()
if self._permanentReceive:
self.newReceive()
self.startReceive()
elif self.isReceiveTimeout():
self.clearReceiveStatus()
if self._permanentReceive:
self.newReceive()
self.startReceive()
elif msgReceived:
self.callbacks["handleReceived"]()
self.clearReceiveStatus()
if self._permanentReceive:
# no need to start a new receive since we enabled the permanent receive mode in the system configuration register. it created an interference causing problem
# with the reception
# newReceive()
self.startReceive()
self.clearAllStatus()
def registerCallback(self, string, callback):
"""
This function saves the callback sent by the script who imports this module for later use. It stores it in a dictionary with the
specified key.
Args:
string: This is the key used to store the callback in the dictionary.
callback: This is the saved callback.
"""
if callback not in self.callbacks:
self.callbacks[string] = callback
def softReset(self):
"""
This function performs a soft reset on the DW1000 chip.
"""
pmscctrl0 = [0] * 4
pmscctrl0 = self.readBytes(C.PMSC, C.PMSC_CTRL0_SUB, pmscctrl0, 4)
pmscctrl0[0] = C.SOFT_RESET_SYSCLKS
self.writeBytes(C.PMSC, C.PMSC_CTRL0_SUB, pmscctrl0, 4)
pmscctrl0[3] = C.SOFT_RESET_CLEAR
self.writeBytes(C.PMSC, C.PMSC_CTRL0_SUB, pmscctrl0, 4)
pmscctrl0[0] = C.SOFT_RESET_CLEAR
pmscctrl0[3] = C.SOFT_RESET_SET
self.writeBytes(C.PMSC, C.PMSC_CTRL0_SUB, pmscctrl0, 4)
self.idle()
def hardReset(self):
"""
This function performs a hard reset on the DW1000 chip.
"""
GPIO.setup(self._rst, GPIO.output)
GPIO.output(self._rst, GPIO.LOW)
time.sleep(0.002)
GPIO.setup(self._rst, GPIO.input)
time.sleep(0.010)
self.idle()
def manageLDE(self):
"""
This function manages the LDE micro-code. It is to setup the power management and system control unit as well as the OTP memory interface.
This is necessary as part of the DW1000 initialisation, since it is important to get timestamp and diagnostic info from received frames.
"""
pmscctrl0 = [None] * 4
otpctrl = [None] * 2
pmscctrl0 = self.readBytes(C.PMSC, C.PMSC_CTRL0_SUB, pmscctrl0, 4)
otpctrl = self.readBytes(C.OTP_IF, C.OTP_CTRL_SUB, otpctrl, 2)
pmscctrl0[0] = C.LDE_L1STEP1
pmscctrl0[1] = C.LDE_L1STEP2
otpctrl[0] = C.LDE_L2STEP1
otpctrl[1] = C.LDE_L2STEP2
self.writeBytes(C.PMSC, C.PMSC_CTRL0_SUB, pmscctrl0, 2)
self.writeBytes(C.OTP_IF, C.OTP_CTRL_SUB, otpctrl, 2)
# wait 150 us before writing the 0x36:00 sub-register, see 2.5.5.10 of the DW1000 user manual.
time.sleep(C.PMSC_CONFIG_DELAY)
pmscctrl0[0] = C.LDE_L3STEP1
pmscctrl0[1] = C.LDE_L3STEP2
self.writeBytes(C.PMSC, C.PMSC_CTRL0_SUB, pmscctrl0, 2)
def setDefaultConfiguration(self):
"""
This function sets the default mode on the chip initialization : MODE_LONGDATA_RANGE_LOWPOWER and with receive/transmit mask activated when in IDLE mode.
"""
if self._deviceMode == C.TX_MODE:
pass
elif self._deviceMode == C.RX_MODE:
pass
elif self._deviceMode == C.IDLE_MODE:
self._syscfg[2] &= C.ENABLE_MODE_MASK2
self._syscfg[2] |= 0x00
self.setBit(self._syscfg, 4, C.DIS_STXP_BIT, True)
self.setBit(self._syscfg, 4, C.FFEN_BIT, False)
# interrupt on sent
self.setBit(self._sysmask, 4, C.MTXFRS_BIT, True)
# interrupt on received
self.setBit(self._sysmask, 4, C.MRXDFR_BIT, True)
self.setBit(self._sysmask, 4, C.MRXFCG_BIT, True)
# interrupt on received failed
self.setBit(self._sysmask, 4, C.MLDEERR_BIT, True)
self.setBit(self._sysmask, 4, C.MRXFCE_BIT, True)
self.setBit(self._sysmask, 4, C.MRXPHE_BIT, True)
self.setBit(self._sysmask, 4, C.MRXRFSL_BIT, True)
# interrupt on receive time stamp available
self.setBit(self._sysmask, 4, C.MLDEDONE_BIT, False)
# interrupt on auto acknowledge trigger
self.setBit(self._sysmask, 4, C.MAAT_BIT, True)
# set receiver auto reenable
self.setBit(self._syscfg, 4, C.RXAUTR_BIT, True)
self.clearAllStatus()
self.enableMode(C.MODE_LONGDATA_RANGE_LOWPOWER)
def enableMode(self, mode):
"""
This function configures the DW1000 chip to perform with a specific mode. It sets up the TRX rate the TX pulse frequency and the preamble length.
"""
# setDataRate
rate = mode[0]
rate = rate & C.MASK_LS_2BITS
self._txfctrl[1] = self._txfctrl[1] & C.ENABLE_MODE_MASK1
self._txfctrl[1] = self._txfctrl[1] | ((rate << 5) & C.MASK_LS_BYTE)
if rate == C.TRX_RATE_110KBPS:
self.setBit(self._syscfg, 4, C.RXM110K_BIT, True)
else:
self.setBit(self._syscfg, 4, C.RXM110K_BIT, False)
if rate == C.TRX_RATE_6800KBPS:
self.setBit(self._chanctrl, 4, C.DWSFD_BIT, False)
self.setBit(self._chanctrl, 4, C.TNSSFD_BIT, False)
self.setBit(self._chanctrl, 4, C.RNSSFD_BIT, False)
else:
self.setBit(self._chanctrl, 4, C.DWSFD_BIT, True)
self.setBit(self._chanctrl, 4, C.TNSSFD_BIT, True)
self.setBit(self._chanctrl, 4, C.RNSSFD_BIT, True)
if rate == C.TRX_RATE_850KBPS:
sfdLength = [C.SFD_LENGTH_850KBPS]
elif rate == C.TRX_RATE_6800KBPS:
sfdLength = [C.SFD_LENGTH_6800KBPS]
else:
sfdLength = [C.SFD_LENGTH_OTHER]
self.writeBytes(C.USR_SFD, C.SFD_LENGTH_SUB, sfdLength, 1)
self._operationMode[C.DATA_RATE_BIT] = rate
# setPulseFreq
freq = mode[1]
freq = freq & C.MASK_LS_2BITS
self._txfctrl[2] = self._txfctrl[2] & C.ENABLE_MODE_MASK2
self._txfctrl[2] = self._txfctrl[2] | (freq & C.MASK_LS_BYTE)
self._chanctrl[2] = self._chanctrl[2] & C.ENABLE_MODE_MASK3
self._chanctrl[2] = self._chanctrl[2] | ((freq << 2) & C.MASK_LS_BYTE)
self._operationMode[C.PULSE_FREQUENCY_BIT] = freq
# setPreambleLength
prealen = mode[2]
prealen = prealen & C.MASK_NIBBLE
self._txfctrl[2] = self._txfctrl[2] & C.ENABLE_MODE_MASK4
self._txfctrl[2] = self._txfctrl[2] | ((prealen << 2) & C.MASK_LS_BYTE)
if prealen == C.TX_PREAMBLE_LEN_64 or prealen == C.TX_PREAMBLE_LEN_128:
self._operationMode[C.PAC_SIZE_BIT] = C.PAC_SIZE_8
elif prealen == C.TX_PREAMBLE_LEN_256 or prealen == C.TX_PREAMBLE_LEN_512:
self._operationMode[C.PAC_SIZE_BIT] = C.PAC_SIZE_16
elif prealen == C.TX_PREAMBLE_LEN_1024:
self._operationMode[C.PAC_SIZE_BIT] = C.PAC_SIZE_32
else:
self._operationMode[C.PAC_SIZE_BIT] = C.PAC_SIZE_64
self._operationMode[C.PREAMBLE_LENGTH_BIT] = prealen
# setChannel
self.setChannel(C.CHANNEL_5)
# setPreambleCode
if mode[1] == C.TX_PULSE_FREQ_16MHZ:
self.setPreambleCode(C.PREAMBLE_CODE_16MHZ_4)
else:
self.setPreambleCode(C.PREAMBLE_CODE_64MHZ_10)
def newConfiguration(self):
"""
This function resets the DW1000 chip to the idle state mode and reads all the configuration registers to prepare for a new configuration.
"""
self.idle()
self._networkAndAddress = self.readBytes(C.PANADR, C.NO_SUB, self._networkAndAddress, 4)
self._syscfg = self.readBytes(C.SYS_CFG, C.NO_SUB, self._syscfg, 4)
self._chanctrl = self.readBytes(C.CHAN_CTRL, C.NO_SUB, self._chanctrl, 4)
self._txfctrl = self.readBytes(C.TX_FCTRL, C.NO_SUB, self._txfctrl, 5)
self._sysmask = self.readBytes(C.SYS_MASK, C.NO_SUB, self._sysmask, 4)
def commitConfiguration(self):
"""
This function commits the configuration stored in the arrays previously filled. It writes into the corresponding registers to apply the changes to the DW1000 chip.
It also tunes the chip according to the current enabled mode.
"""
self.writeBytes(C.PANADR, C.NO_SUB, self._networkAndAddress, 4)
self.writeBytes(C.SYS_CFG, C.NO_SUB, self._syscfg, 4)
self.writeBytes(C.CHAN_CTRL, C.NO_SUB, self._chanctrl, 4)
self.writeBytes(C.TX_FCTRL, C.NO_SUB, self._txfctrl, 5)
self.writeBytes(C.SYS_MASK, C.NO_SUB, self._sysmask, 4)
self.tune()
def setAntennaDelay(self, val):
"""
This function sets the DW1000 chip's antenna delay value which needs to be calibrated to have better ranging accuracy.
Args:
val : The antenna delay value which will be configured into the chip.
"""
antennaDelayBytes = [None] * 5
self.writeValueToBytes(antennaDelayBytes, val, 5)
self.writeBytes(C.TX_ANTD, C.NO_SUB, antennaDelayBytes, 2)
self.writeBytes(C.LDE_CTRL, C.LDE_RXANTD_SUB, antennaDelayBytes, 2)
def setEUI(self, currentAddress):
"""
This function sets the extended unique identifier of the chip according to the value specified by the user in setup.
Args:
currentAddress : the array of bytes containing the EUI
"""
reverseEUI = [0] * 8
for i in range(0, 8):
reverseEUI[i] = currentAddress[8 - i - 1]
self.writeBytes(C.EUI, C.NO_SUB, reverseEUI, 8)
def setDeviceAddress(self, value):
"""
This function sets the device's address according to the specified value.
Args:
value : The address you want to set to the chip.
"""
self._networkAndAddress[0] = value & C.MASK_LS_BYTE
self._networkAndAddress[1] = (value >> 8) & C.MASK_LS_BYTE
def setNetworkId(self, value):
"""
This function sets the device's network ID according to the specified value.
Args:
value : The network id you want to assign to the chip.
"""
self._networkAndAddress[2] = value & C.MASK_LS_BYTE
self._networkAndAddress[3] = (value >> 8) & C.MASK_LS_BYTE
def setChannel(self, channel):
"""
This function configures the DW1000 chip to enable a the specified channel of operation.
Args:
channel : The channel value you want to assign to the chip.
"""
channel = channel & C.MASK_NIBBLE
self._chanctrl[0] = ((channel | (channel << 4)) & C.MASK_LS_BYTE)
self._operationMode[C.CHANNEL_BIT] = channel
def setPreambleCode(self, preacode):
"""
This function sets the preamble code used for the frames, depending on the the pulse repetition frequency and the channel used.
Args:
preacode : The preamble code type you want to assign to the chip.
"""
preacode = preacode & C.PREACODE_MASK1
self._chanctrl[2] = self._chanctrl[2] & C.PREACODE_MASK2
self._chanctrl[2] = self._chanctrl[2] | ((preacode << 6) & C.MASK_LS_BYTE)
self._chanctrl[3] = 0x00
self._chanctrl[3] = ((((preacode >> 2) & C.PREACODE_MASK3) |
(preacode << 3)) & C.MASK_LS_BYTE)
self._operationMode[C.PREAMBLE_CODE_BIT] = preacode
def tune(self):
"""
This function tunes/configures dw1000 chip's registers according to the enabled mode. Although the DW1000 will power up in a usable mode for the default configuration,
some of the register defaults are sub optimal and should be overwritten before using the chip in the default mode. See 2.5.5 of the user manual.
"""
agctune1 = [None] * 2
agctune2 = [None] * 4
agctune3 = [None] * 2
drxtune0b = [None] * 2
drxtune1a = [None] * 2
drxtune1b = [None] * 2
drxtune2 = [None] * 4
drxtune4H = [None] * 2
rfrxctrlh = [None] * 1
rftxctrl = [None] * 4
tcpgdelay = [None] * 1
fspllcfg = [None] * 4
fsplltune = [None] * 1
ldecfg1 = [None] * 1
ldecfg2 = [None] * 2
lderepc = [None] * 2
txpower = [None] * 4
fsxtalt = [None] * 1
preambleLength = self._operationMode[C.PREAMBLE_LENGTH_BIT]
channel = self._operationMode[C.CHANNEL_BIT]
self.tuneAgcTune1(agctune1)
self.writeValueToBytes(agctune2, C.AGC_TUNE2_OP, 4)
self.writeValueToBytes(agctune3, C.AGC_TUNE3_OP, 2)
self.tuneDrxTune0b(drxtune0b)
self.tuneDrxTune1aAndldecfg2(drxtune1a, ldecfg2)
self.tuneDrxtune1b(drxtune1b)
self.tuneDrxTune2(drxtune2)
if preambleLength == C.TX_PREAMBLE_LEN_64:
self.writeValueToBytes(drxtune4H, C.DRX_TUNE4H_64, 2)
else:
self.writeValueToBytes(drxtune4H, C.DRX_TUNE4H_128, 2)
if (channel != C.CHANNEL_4 and channel != C.CHANNEL_7):
self.writeValueToBytes(rfrxctrlh, C.RF_RXCTRLH_1235, 1)
else:
self.writeValueToBytes(rfrxctrlh, C.RF_RXCTRLH_147, 1)
self.tuneAccToChan(rftxctrl, tcpgdelay, fspllcfg, fsplltune, txpower)
self.writeValueToBytes(ldecfg1, C.LDE_CFG1_OP, 1)
self.tunelderepc(lderepc)
buf_otp = [None] * 4
buf_otp = self.readBytesOTP(C.OTP_XTAL_ADDRESS, buf_otp)
if buf_otp[0] == 0:
self.writeValueToBytes(
fsxtalt, ((C.TUNE_OPERATION & C.TUNE_MASK1) | C.TUNE_MASK2), 1)
else:
self.writeValueToBytes(
fsxtalt, ((buf_otp[0] & C.TUNE_MASK1) | C.TUNE_MASK2), 1)
self.writeBytes(C.AGC_CTRL, C.AGC_TUNE1_SUB, agctune1, 2)
self.writeBytes(C.AGC_CTRL, C.AGC_TUNE2_SUB, agctune2, 4)
self.writeBytes(C.AGC_CTRL, C.AGC_TUNE3_SUB, agctune3, 2)
self.writeBytes(C.DRX_CONF, C.DRX_TUNE0b_SUB, drxtune0b, 2)
self.writeBytes(C.DRX_CONF, C.DRX_TUNE1a_SUB, drxtune1a, 2)
self.writeBytes(C.DRX_CONF, C.DRX_TUNE1b_SUB, drxtune1b, 2)
self.writeBytes(C.DRX_CONF, C.DRX_TUNE2_SUB, drxtune2, 4)
self.writeBytes(C.DRX_CONF, C.DRX_TUNE4H_SUB, drxtune4H, 2)
self.writeBytes(C.LDE_CTRL, C.LDE_CFG1_SUB, ldecfg1, 1)
self.writeBytes(C.LDE_CTRL, C.LDE_CFG2_SUB, ldecfg2, 2)
self.writeBytes(C.LDE_CTRL, C.LDE_REPC_SUB, lderepc, 2)
self.writeBytes(C.TX_POWER, C.NO_SUB, txpower, 4)
self.writeBytes(C.RF_CONF, C.RF_RXCTRLH_SUB, rfrxctrlh, 1)
self.writeBytes(C.RF_CONF, C.RF_TXCTRL_SUB, rftxctrl, 4)
self.writeBytes(C.TX_CAL, C.TC_PGDELAY_SUB, tcpgdelay, 1)
self.writeBytes(C.FS_CTRL, C.FS_PLLTUNE_SUB, fsplltune, 1)
self.writeBytes(C.FS_CTRL, C.FS_PLLCFG_SUB, fspllcfg, 4)
self.writeBytes(C.FS_CTRL, C.FS_XTALT_SUB, fsxtalt, 1)
def generalConfiguration(self, address, mode):
"""
This function configures the DW1000 chip with general settings. It also defines the address and the network ID used by the device. It finally prints the
configured device.
Args:
address: The string address you want to set the device to.
"""
currentAddress = self.convertStringToByte(address)
currentShortAddress = [0] * 2
self.setEUI(currentAddress)
currentShortAddress[0] = randint(0, 256)
currentShortAddress[1] = randint(0, 256)
deviceAddress = currentShortAddress[0] * 256 + currentShortAddress[1]
# configure mode, network
self.newConfiguration()
self.setDefaultConfiguration()
# setDeviceAddress(2)
self.setDeviceAddress(deviceAddress)
# setNetworkId(10)
self.setNetworkId(0xDECA)
self.enableMode(mode)
self.setAntennaDelay(C.ANTENNA_DELAY)
self.commitConfiguration()
data = [0] * 4
data2 = [0] * 8
data3 = [0] * 4
data = self.readBytes(C.DEV_ID, C.NO_SUB, data, 4)
data2 = self.readBytes(C.EUI, C.NO_SUB, data2, 8)
data3 = self.readBytes(C.PANADR, C.NO_SUB, data3, 4)
print("\nDevice ID %02X - model: %d, version: %d, revision: %d" %
((data[3] << 8) | data[2], (data[1]), (data[0] >> 4) & C.MASK_NIBBLE, data[0] & C.MASK_NIBBLE))
print("Unique ID: %02X:%02X:%02X:%02X:%02X:%02X:%02X:%02X" % (
data2[7], data2[6], data2[5], data2[4], data2[3], data2[2], data2[1], data2[0]))
print("Network ID & Device Address: PAN: %02X, Short Address: %02X" %
(((data3[3] << 8) | data3[2]), ((data3[1] << 8) | data3[0])))
self.getDeviceModeInfo()
"""
Tuning functions
See tune() for more information
"""
def tuneAgcTune1(self, data):
"""
This function fills the array for the tuning of agctune1 according to the datasheet and the enabled mode.
Args:
data: The array which will store the correct values for agctune1.
"""
pulseFrequency = self._operationMode[C.PULSE_FREQUENCY_BIT]
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(data, C.AGC_TUNE1_16MHZ_OP, 2)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(data, C.AGC_TUNE1_DEFAULT_OP, 2)
def tuneDrxTune0b(self, data):
"""
This function fills the array for the tuning of drxtune0b according to the datasheet and the enabled mode.
Args:
data: The array which will store the correct values for drxtune0b.
"""
dataRate = self._operationMode[C.DATA_RATE_BIT]
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(data, C.DRX_TUNE0b_110KBPS_NOSTD_OP, 2)
elif dataRate == C.TRX_RATE_850KBPS:
self.writeValueToBytes(data, C.DRX_TUNE0b_850KBPS_NOSTD_OP, 2)
elif dataRate == C.TRX_RATE_6800KBPS:
self.writeValueToBytes(data, C.DRX_TUNE0b_6800KBPS_STD_OP, 2)
def tuneDrxTune1aAndldecfg2(self, data, data2):
"""
This function fills the array for the tuning of drxtune1a and ldecfg2 according to the datasheet and the enabled mode.
Args:
data: The array which will store the correct values for the drxtune1a.
data2: The array which will store the correct values for ldecfg2.
"""
pulseFrequency = self._operationMode[C.PULSE_FREQUENCY_BIT]
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(data, C.DRX_TUNE1a_16MHZ_OP, 2)
self.writeValueToBytes(data2, C.LDE_CFG2_16MHZ_OP, 2)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(data, C.DRX_TUNE1a_64MHZ_OP, 2)
self.writeValueToBytes(data2, C.LDE_CFG2_64MHZ_OP, 2)
def tuneDrxtune1b(self, data):
"""
This function fills the array for the tuning of drxtune1b according to the datasheet and the enabled mode.
Args:
data: The array which will store the correct values for drxtune1b.
"""
dataRate = self._operationMode[C.DATA_RATE_BIT]
preambleLength = self._operationMode[C.PREAMBLE_LENGTH_BIT]
if (preambleLength == C.TX_PREAMBLE_LEN_1536 or preambleLength == C.TX_PREAMBLE_LEN_2048 or preambleLength == C.TX_PREAMBLE_LEN_4096):
if (dataRate == C.TRX_RATE_110KBPS):
self.writeValueToBytes(data, C.DRX_TUNE1b_M1024, 2)
elif preambleLength != C.TX_PREAMBLE_LEN_64:
if (dataRate == C.TRX_RATE_850KBPS or dataRate == C.TRX_RATE_6800KBPS):
self.writeValueToBytes(data, C.DRX_TUNE1b_L1024, 2)
else:
if dataRate == C.TRX_RATE_6800KBPS:
self.writeValueToBytes(data, C.DRX_TUNE1b_64, 2)
def tuneDrxTune2(self, data):
"""
This function fills the array for the tuning of drxtune2 according to the datasheet and the enabled mode.
Args:
data: The array which will store the correct values for drxtune2.
"""
pacSize = self._operationMode[C.PAC_SIZE_BIT]
pulseFrequency = self._operationMode[C.PULSE_FREQUENCY_BIT]
if pacSize == C.PAC_SIZE_8:
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(data, C.DRX_TUNE2_8_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(data, C.DRX_TUNE2_8_64MHZ, 4)
elif pacSize == C.PAC_SIZE_16:
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(data, C.DRX_TUNE2_16_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(data, C.DRX_TUNE2_16_64MHZ, 4)
elif pacSize == C.PAC_SIZE_32:
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(data, C.DRX_TUNE2_32_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(data, C.DRX_TUNE2_32_64MHZ, 4)
elif pacSize == C.PAC_SIZE_64:
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(data, C.DRX_TUNE2_64_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(data, C.DRX_TUNE2_64_64MHZ, 4)
def tuneAccToChan(self, rftxctrl, tcpgdelay, fspllcfg, fsplltune, txpower):
"""
This function fills the arrays for the tuning of rftxctrl, tcpgdelay, fspllcfg, fsplltune and txpower according to the datasheet and the enabled mode.
"""
channel = self._operationMode[C.CHANNEL_BIT]
pulseFrequency = self._operationMode[C.PULSE_FREQUENCY_BIT]
if channel == C.CHANNEL_1:
self.writeValueToBytes(rftxctrl, C.RF_TXCTRL_1, 4)
self.writeValueToBytes(tcpgdelay, C.TC_PGDELAY_1, 1)
self.writeValueToBytes(fspllcfg, C.FS_PLLCFG_1, 4)
self.writeValueToBytes(fsplltune, C.FS_PLLTUNE_1, 1)
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_12_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_12_64MHZ, 4)
elif channel == C.CHANNEL_2:
self.writeValueToBytes(rftxctrl, C.RF_TXCTRL_2, 4)
self.writeValueToBytes(tcpgdelay, C.TC_PGDELAY_2, 1)
self.writeValueToBytes(fspllcfg, C.FS_PLLCFG_24, 4)
self.writeValueToBytes(fsplltune, C.FS_PLLTUNE_24, 1)
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_12_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_12_64MHZ, 4)
elif channel == C.CHANNEL_3:
self.writeValueToBytes(rftxctrl, C.RF_TXCTRL_3, 4)
self.writeValueToBytes(tcpgdelay, C.TC_PGDELAY_3, 1)
self.writeValueToBytes(fspllcfg, C.FS_PLLCFG_3, 4)
self.writeValueToBytes(fsplltune, C.FS_PLLTUNE_3, 1)
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_3_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_3_64MHZ, 4)
elif channel == C.CHANNEL_4:
self.writeValueToBytes(rftxctrl, C.RF_TXCTRL_4, 4)
self.writeValueToBytes(tcpgdelay, C.TC_PGDELAY_4, 1)
self.writeValueToBytes(fspllcfg, C.FS_PLLCFG_24, 4)
self.writeValueToBytes(fsplltune, C.FS_PLLTUNE_24, 1)
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_4_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_4_64MHZ, 4)
elif channel == C.CHANNEL_5:
self.writeValueToBytes(rftxctrl, C.RF_TXCTRL_5, 4)
self.writeValueToBytes(tcpgdelay, C.TC_PGDELAY_5, 1)
self.writeValueToBytes(fspllcfg, C.FS_PLLCFG_57, 4)
self.writeValueToBytes(fsplltune, C.FS_PLLTUNE_57, 1)
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_5_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_5_64MHZ, 4)
elif channel == C.CHANNEL_7:
self.writeValueToBytes(rftxctrl, C.RF_TXCTRL_7, 4)
self.writeValueToBytes(tcpgdelay, C.TC_PGDELAY_7, 1)
self.writeValueToBytes(fspllcfg, C.FS_PLLCFG_57, 4)
self.writeValueToBytes(fsplltune, C.FS_PLLTUNE_57, 1)
if pulseFrequency == C.TX_PULSE_FREQ_16MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_7_16MHZ, 4)
elif pulseFrequency == C.TX_PULSE_FREQ_64MHZ:
self.writeValueToBytes(txpower, C.TX_POWER_7_64MHZ, 4)
def tunelderepc(self, data):
"""
This function fills the arrays for the tuning of lderepc according to the datasheet and the enabled mode.
Args:
data: The array which will store the correct values for lderepC.
"""
preacode = self._operationMode[C.PREAMBLE_CODE_BIT]
dataRate = self._operationMode[C.DATA_RATE_BIT]
if (preacode == C.PREAMBLE_CODE_16MHZ_1 or preacode == C.PREAMBLE_CODE_16MHZ_2):
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_1AND2 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_1AND2, 2)
elif (preacode == C.PREAMBLE_CODE_16MHZ_3 or preacode == C.PREAMBLE_CODE_16MHZ_8):
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_3AND8 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_3, 2)
elif preacode == C.PREAMBLE_CODE_16MHZ_4:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_4 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_4, 2)
elif preacode == C.PREAMBLE_CODE_16MHZ_5:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_5 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_5, 2)
elif preacode == C.PREAMBLE_CODE_16MHZ_6:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_6 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_6, 2)
elif preacode == C.PREAMBLE_CODE_16MHZ_7:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_7 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_7, 2)
elif preacode == C.PREAMBLE_CODE_64MHZ_9:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_9 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_9, 2)
elif preacode == C.PREAMBLE_CODE_64MHZ_10 or preacode == C.PREAMBLE_CODE_64MHZ_17:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_1017 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_1017, 2)
elif preacode == C.PREAMBLE_CODE_64MHZ_11:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_111321 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_111321, 2)
elif preacode == C.PREAMBLE_CODE_64MHZ_12:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_12 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_12, 2)
elif preacode == C.PREAMBLE_CODE_64MHZ_18 or preacode == C.PREAMBLE_CODE_64MHZ_19:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_14161819 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_14161819, 2)
elif preacode == C.PREAMBLE_CODE_64MHZ_20:
if dataRate == C.TRX_RATE_110KBPS:
self.writeValueToBytes(
data, ((C.LDE_REPC_20 >> 3) & C.MASK_LS_2BYTES), 2)
else:
self.writeValueToBytes(data, C.LDE_REPC_20, 2)
def enableClock(self, clock):
"""
This function manages the dw1000 chip's clock by setting up the proper registers to activate the specified clock mode chosen.
Args:
clock: An hex value corresponding to the clock mode wanted:
AUTO=0x00
XTI=0x01
PLL=0X02.
"""
pmscctrl0 = [None] * 4
pmscctrl0 = self.readBytes(C.PMSC, C.PMSC_CTRL0_SUB, pmscctrl0, 4)
if clock == C.AUTO_CLOCK:
pmscctrl0[0] = C.AUTO_CLOCK
pmscctrl0[1] = pmscctrl0[1] & C.ENABLE_CLOCK_MASK1
elif clock == C.XTI_CLOCK:
pmscctrl0[0] = pmscctrl0[0] & C.ENABLE_CLOCK_MASK2
pmscctrl0[0] = pmscctrl0[0] | 1
self.writeBytes(C.PMSC, C.PMSC_CTRL0_SUB, pmscctrl0, 2)
def idle(self):
"""
This function puts the chip into idle mode.
"""
self.setArray(self._sysctrl, 4, 0x00)
self.setBit(self._sysctrl, 4, C.TRXOFF_BIT, True)
self._deviceMode = C.IDLE_MODE
self.writeBytes(C.SYS_CTRL, C.NO_SUB, self._sysctrl, 4)
"""
Message reception functions.
"""
def newReceive(self):
"""
This function prepares the chip for a new reception. It clears the system control register and also clear the RX latched bits in the SYS_STATUS register.
"""
self.idle()
self.setArray(self._sysctrl, 4, 0x00)
self.clearReceiveStatus()
self._deviceMode = C.RX_MODE
def startReceive(self):
"""
This function configures the chip to start the reception of a message sent by another DW1000 chip.
It turns on its receiver by setting RXENAB in the system control register.
"""
self.setBit(self._sysctrl, 4, C.SFCST_BIT, False)
self.setBit(self._sysctrl, 4, C.RXENAB_BIT, True)
self.writeBytes(C.SYS_CTRL, C.NO_SUB, self._sysctrl, 4)
def receivePermanently(self):
"""
This function configures the dw1000 chip to receive data permanently.
"""
self._permanentReceive = True
self.setBit(self._syscfg, 4, C.RXAUTR_BIT, True)
self.writeBytes(C.SYS_CFG, C.NO_SUB, self._syscfg, 4)
def isReceiveFailed(self):
"""
This function reads the system event status register and checks if the message reception failed.
Returns:
True if the reception failed.
False otherwise.
"""
ldeErr = self.getBit(self._sysstatus, 5, C.LDEERR_BIT)
rxCRCErr = self.getBit(self._sysstatus, 5, C.RXFCE_BIT)
rxHeaderErr = self.getBit(self._sysstatus, 5, C.RXPHE_BIT)
rxDecodeErr = self.getBit(self._sysstatus, 5, C.RXRFSL_BIT)
if ldeErr or rxCRCErr or rxHeaderErr or rxDecodeErr:
return True
else:
return False
def isReceiveTimeout(self):
"""
This function reads the system event status register and checks if there was a timeout in the message reception.
Returns:
True if there was a timeout in the reception.
False otherwise.
"""
isTimeout = self.getBit(self._sysstatus, 5, C.RXRFTO_BIT) | self.getBit(
self._sysstatus, 5, C.RXPTO_BIT) | self.getBit(self._sysstatus, 5, C.RXSFDTO_BIT)
return isTimeout
def clearReceiveStatus(self):
"""
This function clears the system event status register at the bits related to the reception of a message.
"""
self.setBit(self._sysstatus, 5, C.RXDFR_BIT, True)
self.setBit(self._sysstatus, 5, C.LDEDONE_BIT, True)
self.setBit(self._sysstatus, 5, C.LDEERR_BIT, True)
self.setBit(self._sysstatus, 5, C.RXPHE_BIT, True)
self.setBit(self._sysstatus, 5, C.RXFCE_BIT, True)
self.setBit(self._sysstatus, 5, C.RXFCG_BIT, True)
self.setBit(self._sysstatus, 5, C.RXRFSL_BIT, True)
self.writeBytes(C.SYS_STATUS, C.NO_SUB, self._sysstatus, 5)
def getFirstPathPower(self):
"""
This function calculates an estimate of the power in the first path signal. See section 4.7.1 of the DW1000 user manual for further details on the calculations.
Returns:
The estimated power in the first path signal.
"""
fpAmpl1Bytes = [None] * 2
fpAmpl2Bytes = [None] * 2
fpAmpl3Bytes = [None] * 2
rxFrameInfo = [None] * 4
fpAmpl1Bytes = self.readBytes(C.RX_TIME, C.FP_AMPL1_SUB, fpAmpl1Bytes, 2)
fpAmpl2Bytes = self.readBytes(C.RX_FQUAL, C.FP_AMPL2_SUB, fpAmpl2Bytes, 2)
fpAmpl3Bytes = self.readBytes(C.RX_FQUAL, C.FP_AMPL3_SUB, fpAmpl3Bytes, 2)
rxFrameInfo = self.readBytes(C.RX_FINFO, C.NO_SUB, rxFrameInfo, 4)
f1 = fpAmpl1Bytes[0] | fpAmpl1Bytes[1] << 8
f2 = fpAmpl2Bytes[0] | fpAmpl2Bytes[1] << 8
f3 = fpAmpl3Bytes[0] | fpAmpl3Bytes[1] << 8
N = ((rxFrameInfo[2] >> 4) & C.MASK_LS_BYTE) | (rxFrameInfo[3] << 4)
if self._operationMode[C.PULSE_FREQUENCY_BIT] == C.TX_PULSE_FREQ_16MHZ:
A = C.A_16MHZ
corrFac = C.CORRFAC_16MHZ
else:
A = C.A_64MHZ
corrFac = C.CORRFAC_64MHZ
estFPPower = C.PWR_COEFF2 * \
math.log10((f1 * f1 + f2 * f2 + f3 * f3) / (N * N)) - A
if estFPPower <= -C.PWR_COEFF:
return estFPPower
else:
estFPPower += (estFPPower + C.PWR_COEFF) * corrFac
return estFPPower
def getReceivePower(self):
"""
This function calculates an estimate of the receive power level. See section 4.7.2 of the DW1000 user manual for further details on the calculation.
Returns:
The estimated receive power for the current reception.
"""
cirPwrBytes = [None] * 2
rxFrameInfo = [None] * 4
cirPwrBytes = self.readBytes(C.RX_FQUAL, C.CIR_PWR_SUB, cirPwrBytes, 2)
rxFrameInfo = self.readBytes(C.RX_FINFO, C.NO_SUB, rxFrameInfo, 4)
cir = cirPwrBytes[0] | cirPwrBytes[1] << 8
N = ((rxFrameInfo[2] >> 4) & C.MASK_LS_BYTE) | rxFrameInfo[3] << 4
if self._operationMode[C.PULSE_FREQUENCY_BIT] == C.TX_PULSE_FREQ_16MHZ:
A = C.A_16MHZ
corrFac = C.CORRFAC_16MHZ
else:
A = C.A_64MHZ
corrFac = C.CORRFAC_64MHZ
estRXPower = 0
if (float(cir) * float(C.TWOPOWER17)) / (float(N) * float(N)) > 0:
estRXPower = C.PWR_COEFF2 * math.log10((float(cir) * float(C.TWOPOWER17)) / (float(N) * float(N))) - A
if estRXPower <= -C.PWR_COEFF:
return estRXPower
else:
estRXPower += (estRXPower + C.PWR_COEFF) * corrFac
return estRXPower
def getReceiveQuality(self):
"""
This function calculates an estimate of the receive quality.abs
Returns:
The estimated receive quality for the current reception.
"""
noiseBytes = [None] * 2
fpAmpl2Bytes = [None] * 2
noiseBytes = readBytes(C.RX_FQUAL, C.STD_NOISE_SUB, noiseBytes, 2)
fpAmpl2Bytes = readBytes(C.RX_FQUAL, C.FP_AMPL2_SUB, fpAmpl2Bytes, 2)
noise = float(noiseBytes[0] | noiseBytes[1] << 8)
f2 = float(fpAmpl2Bytes[0] | fpAmpl2Bytes[1] << 8)
return f2 / noise
def getReceiveTimestamp(self):
"""
This function reads the receive timestamp from the register and returns it.
Returns:
The timestamp value of the startReceive reception.
"""
rxTimeBytes = [0] * 5
rxTimeBytes = self.readBytes(C.RX_TIME, C.RX_STAMP_SUB, rxTimeBytes, 5)
timestamp = 0
for i in range(0, 5):