Skip to content

Commit b7d1553

Browse files
Add checksum validation
1 parent a293522 commit b7d1553

1 file changed

Lines changed: 26 additions & 7 deletions

File tree

pd.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,13 +51,14 @@ class Decoder(srd.Decoder):
5151
('bit', 'Bit'),
5252
('cmd', 'Command'),
5353
('throttle', 'Throttle'),
54-
('dshot_errors', 'Throttle'),
54+
('checksum', 'CRC'),
55+
('errors', 'Errors'),
5556

5657
)
5758
annotation_rows = (
5859
('bit', 'Bits', (0,)),
59-
('throttle', 'Throttle', (1,2,)),
60-
('dshot_errors', 'Dshot Errors', (3,)),
60+
('throttle', 'Throttle', (1,2,3)),
61+
('dshot_errors', 'Dshot Errors', (4,)),
6162
)
6263

6364
dshot_period_lookup = {'150': 6.67e-6, '300': 3.33e-6,'600':1.67e-6,'1200':0.83e-6}
@@ -75,6 +76,7 @@ def reset(self):
7576
self.inreset = False
7677
self.bidirectional = False
7778
self.dshot_period = 3.33e-6
79+
self.actual_period = None
7880

7981

8082
def start(self):
@@ -89,16 +91,32 @@ def metadata(self, key, value):
8991

9092
def handle_bits(self, samplenum):
9193
if len(self.bits) == 16:
92-
# rgb = (grb & 0xff0000) >> 8 | (grb & 0x00ff00) << 8 | (grb & 0x0000ff)
93-
#print(self.bits)
9494
throttle = int(reduce(lambda a, b: (a << 1) | b, self.bits[:11]))
95-
self.put(self.ss_packet, samplenum, self.out_ann,
95+
telem_request = self.bits[11]
96+
received_crc = int(reduce(lambda a, b: (a << 1) | b, self.bits[12:]))
97+
98+
value_tocrc = int(reduce(lambda a, b: (a << 1) | b, self.bits[:12]))
99+
100+
calculated_crc = ((~(value_tocrc ^ (value_tocrc >> 4) ^ (value_tocrc >> 8))) & 0x0F)
101+
102+
crc_ok = True if value_tocrc == calculated_crc else False
103+
104+
# TODO: Align this correctly
105+
crc_startsample = samplenum-(self.actual_period*5)
106+
107+
self.put(self.ss_packet, crc_startsample, self.out_ann,
96108
[2, ['%04d' % throttle]])
109+
self.put(crc_startsample, samplenum, self.out_ann, [3, ['Calc CRC: '+('%04d' % calculated_crc)+' TXed CRC:'+('%04d' % received_crc)]])
110+
if not crc_ok:
111+
self.put(crc_startsample, samplenum, self.out_ann,
112+
[4, ['CRC INVALID']])
113+
114+
97115
self.bits = []
98116
self.ss_packet = None
99117
else:
100118
self.put(self.es, self.samplenum, self.out_ann,
101-
[1, ['ERROR', 'ERR', 'E']])
119+
[1, ['ERROR: INVALID PACKET LENGTH', 'ERR', 'E']])
102120

103121

104122
def handle_bit(self):
@@ -112,6 +130,7 @@ def handle_bit(self):
112130
[0, ['%d' % bit_]])
113131

114132
self.bits.append(bit_)
133+
self.actual_period = period
115134
#self.handle_bits(self.samplenum)
116135
if self.ss_packet is None:
117136
self.ss_packet = self.samplenum

0 commit comments

Comments
 (0)