首次能正常解析

This commit is contained in:
2026-06-17 21:16:46 +08:00
parent 7012873c90
commit 45d1e8d0ef
+41 -57
View File
@@ -48,17 +48,15 @@ For 'START', 'START REPEAT', 'STOP', 'ACK', and 'NACK' <pdata> is None.
'''
# CMD: [annotation-type-index, long annotation, short annotation]
proto = {
'START': [0, 'Start', 'S'],
'DATA READ': [1, 'Start repeat', 'Sr'],
'STOP': [2, 'Stop', 'P'],
'DATA READ1': [3, 'ACK', 'A'],
'DATA READ2': [4, 'NACK', 'N'],
'BIT': [5, 'Bit', 'B'],
'ERR': [6, 'Address read', 'AR'],
'CRC': [7, 'Address write', 'AW'],
'DATA READ': [8, 'Data read', 'DR'],
'DATA WRITE': [9, 'Data write', 'DW'],
proto = {
'BIT': [0, 'Bit', 'B'],
'START': [1, 'Start', 'Ss'],
'DATA READ': [2, 'Start repeat', 'Sr'],
'DATA READ1': [3, 'ACK', 'A'],
'DATA READ2': [4, 'NACK', 'N'],
'ERR': [5, 'Address read', 'AR'],
'CRC': [6, 'Address write', 'AW'],
'STOP': [7, 'Stop', 'P'],
}
class Decoder(srd.Decoder):
@@ -76,23 +74,19 @@ class Decoder(srd.Decoder):
{'id': 'sda', 'type': 108, 'name': 'SDA', 'desc': 'Serial data line', 'idn':'dec_1i2c_chan_sda'},
)
annotations = (
('7', 'start', 'Start condition'),
('6', 'repeat-start', 'Repeat start condition'),
('1', 'stop', 'Stop condition'),
('5', 'ack', 'ACK'),
('0', 'nack', 'NACK'),
('208', 'bit', 'Data/address bit'),
('112', 'address-read', 'Address read'),
('111', 'address-write', 'Address write'),
('110', 'data-read', 'Data read'),
('109', 'data-write', 'Data write'),
('207', 'start', 'Start condition'),
('6', 'data', 'Repeat start condition'),
('5', 'data1', 'ACK'),
('0', 'data2', 'NACK'),
('112', 'error', 'Address read'),
('111', 'crc', 'Address write'),
('201', 'stop', 'Stop condition'),
)
annotation_rows = (
('bits', 'Bits', (5,)),
('addr-data', 'Address/Data', (0, 1, 2, 3, 4, 6, 7, 8, 9)),
('warnings', 'Warnings', (10,)),
('bits', 'Bits', (0,)),
('addr-data', 'Address/Data', ( 2, 3, 4,)),
('flag', 'Flag', ( 1, 5, 6, 7)),
)
binary = (
('address-read', 'Address read'),
@@ -114,10 +108,6 @@ class Decoder(srd.Decoder):
self.pdu_bits = 0
self.bits = []
def metadata(self, key, value):
if key == srd.SRD_CONF_SAMPLERATE:
self.samplerate = value
def start(self):
self.out_python = self.register(srd.OUTPUT_PYTHON)
self.out_ann = self.register(srd.OUTPUT_ANN)
@@ -137,7 +127,6 @@ class Decoder(srd.Decoder):
def handle_start(self):
self.ss, self.es = self.samplenum, self.samplenum
self.pdu_start = self.samplenum
self.pdu_bits = 0
cmd = 'START'
self.putp([cmd, None])
self.putx([proto[cmd][0], proto[cmd][1:]])
@@ -145,44 +134,51 @@ class Decoder(srd.Decoder):
self.bitcount = self.databyte = 0
self.bits = []
def handle_stop(self):
cmd = 'STOP'
self.ss, self.es = self.samplenum, self.samplenum
self.putp([cmd, None])
self.putx([proto[cmd][0], proto[cmd][1:]])
self.state = 'FIND START'
self.bits = []
# Gather 8 bits of data plus the ACK/NACK bit.
def handle_data(self, scl, sda,cmd,size,next_state):
self.pdu_bits += 1
# Address and data are transmitted MSB-first.
# 记录数据值
self.databyte <<= 1
self.databyte |= sda
# Remember the start of the first data/address bit.
# 记录数据的起始位置
if self.bitcount == 0:
self.ss_byte = self.samplenum
# Store individual bits and their start/end samplenumbers.
# In the list, index 0 represents the LSB (I2C transmits MSB-first).
self.bits.insert(0, [sda, self.samplenum, self.samplenum])
if self.bitcount > 0:
self.bits[1][2] = self.samplenum
if self.bitcount == (size - 1):
self.bitwidth = self.bits[0][2] - self.bits[1][2]
self.bitcount += 1
if self.bitcount == 2:
self.bitwidth = self.bits[0][2] - self.bits[1][2]
if self.bitcount > 1:
self.bits[1][2] = self.samplenum
if self.bitcount == size:
self.bits[0][2] += self.bitwidth
# Return if we haven't collected all 8 + 1 bits, yet.
if self.bitcount < size:
self.bitcount += 1
if self.bitcount < size:
return
d = self.databyte
bin_class = -1
self.ss, self.es = self.ss_byte, self.samplenum + self.bitwidth
self.putp(['BITS', self.bits])
self.putp([cmd, d])
# self.putp(['BITS', self.bits])
# self.putp([cmd, d])
self.putb([bin_class, d])
# self.putb([bin_class, d])
for bit in self.bits:
self.put(bit[1], bit[2], self.out_ann, [5, ['%d' % bit[0]]])
self.put(bit[1], bit[2], self.out_ann, [0, ['%d' % bit[0]]])
self.putx([proto[cmd][0], ['%s: {$}' % proto[cmd][1], '%s: {$}' % proto[cmd][2], '{$}', d]])
@@ -193,19 +189,7 @@ class Decoder(srd.Decoder):
self.state = next_state
def handle_stop(self):
# Meta bitrate
if self.samplerate:
elapsed = 1 / float(self.samplerate) * (self.samplenum - self.pdu_start + 1)
bitrate = int(1 / elapsed * self.pdu_bits)
self.put(self.ss_byte, self.samplenum, self.out_bitrate, bitrate)
cmd = 'STOP'
self.ss, self.es = self.samplenum, self.samplenum
self.putp([cmd, None])
self.putx([proto[cmd][0], proto[cmd][1:]])
self.state = 'FIND START'
self.bits = []
def decode(self):
while True:
@@ -218,7 +202,7 @@ class Decoder(srd.Decoder):
self.handle_data(scl, sda,'DATA READ',2,'FIND DATA1')
elif self.state == 'FIND DATA1':
(scl, sda) = self.wait([{0: 'f'}])
self.handle_data(scl, sda,'DATA READ1',6,'FIND DATA2')
self.handle_data(scl, sda,'DATA READ1',12,'FIND DATA2')
elif self.state == 'FIND DATA2':
(scl, sda) = self.wait([{0: 'f'}])
self.handle_data(scl, sda,'DATA READ2',13,'FIND ERR')