2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2016 Daniel Schulte <trilader@schroedingers-bit.net>
6 ## This program is free software; you can redistribute it and/or modify
7 ## it under the terms of the GNU General Public License as published by
8 ## the Free Software Foundation; either version 2 of the License, or
9 ## (at your option) any later version.
11 ## This program is distributed in the hope that it will be useful,
12 ## but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ## GNU General Public License for more details.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode as srd
21 from collections import namedtuple
24 BIT, START, STOP, PARITY_OK, PARITY_ERR, DATA, WORD = range(7)
26 Bit = namedtuple('Bit', 'val ss es')
28 class Decoder(srd.Decoder):
33 desc = 'PS/2 keyboard/mouse interface.'
39 {'id': 'clk', 'name': 'Clock', 'desc': 'Clock line'},
40 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
44 ('start-bit', 'Start bit'),
45 ('stop-bit', 'Stop bit'),
46 ('parity-ok', 'Parity OK bit'),
47 ('parity-err', 'Parity error bit'),
48 ('data-bit', 'Data bit'),
52 ('bits', 'Bits', (0,)),
53 ('fields', 'Fields', (1, 2, 3, 4, 5, 6)),
64 self.out_ann = self.register(srd.OUTPUT_ANN)
66 def putb(self, bit, ann_idx):
68 self.put(b.ss, b.es, self.out_ann, [ann_idx, [str(b.val)]])
70 def putx(self, bit, ann):
71 self.put(self.bits[bit].ss, self.bits[bit].es, self.out_ann, ann)
73 def handle_bits(self, datapin):
74 # Ignore non start condition bits (useful during keyboard init).
75 if self.bitcount == 0 and datapin == 1:
78 # Store individual bits and their start/end samplenumbers.
79 self.bits.append(Bit(datapin, self.samplenum, self.samplenum))
81 # Fix up end sample numbers of the bits.
83 b = self.bits[self.bitcount - 1]
84 self.bits[self.bitcount - 1] = Bit(b.val, b.ss, self.samplenum)
85 if self.bitcount == 11:
86 self.bitwidth = self.bits[1].es - self.bits[2].es
88 self.bits[-1] = Bit(b.val, b.ss, b.es + self.bitwidth)
90 # Find all 11 bits. Start + 8 data + odd parity + stop.
91 if self.bitcount < 11:
98 word |= (self.bits[i + 1].val << i)
101 parity_ok = (bin(word).count('1') + self.bits[9].val) % 2 == 1
105 self.putb(i, Ann.BIT)
106 self.putx(0, [Ann.START, ['Start bit', 'Start', 'S']])
107 self.put(self.bits[1].ss, self.bits[8].es, self.out_ann, [Ann.WORD,
108 ['Data: %02x' % word, 'D: %02x' % word, '%02x' % word]])
110 self.putx(9, [Ann.PARITY_OK, ['Parity OK', 'Par OK', 'P']])
112 self.putx(9, [Ann.PARITY_ERR, ['Parity error', 'Par err', 'PE']])
113 self.putx(10, [Ann.STOP, ['Stop bit', 'Stop', 'St', 'T']])
115 self.bits, self.bitcount = [], 0
119 # Sample data bits on the falling clock edge (assume the device
120 # is the transmitter). Expect the data byte transmission to end
121 # at the rising clock edge. Cope with the absence of host activity.
122 _, data_pin = self.wait({0: 'f'})
123 self.handle_bits(data_pin)
124 if self.bitcount == 1 + 8 + 1 + 1:
125 _, data_pin = self.wait({0: 'r'})
126 self.handle_bits(data_pin)