]> sigrok.org Git - libsigrokdecode.git/blame - decoders/ps2/pd.py
ps2: Add more detailed per-bit annotations.
[libsigrokdecode.git] / decoders / ps2 / pd.py
CommitLineData
1a751158
DS
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2016 Daniel Schulte <trilader@schroedingers-bit.net>
5##
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.
10##
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.
15##
16## You should have received a copy of the GNU General Public License
17## along with this program; if not, write to the Free Software
18## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19##
20
21import sigrokdecode as srd
37a2cca9 22from collections import namedtuple
1a751158
DS
23
24class Ann:
37a2cca9
UH
25 BIT, START, STOP, PARITY_OK, PARITY_ERR, DATA, WORD = range(7)
26
27Bit = namedtuple('Bit', 'val ss es')
1a751158
DS
28
29class Decoder(srd.Decoder):
30 api_version = 2
31 id = 'ps2'
32 name = 'PS/2'
33 longname = 'PS/2'
34 desc = 'PS/2 keyboard/mouse interface.'
35 license = 'gplv2+'
36 inputs = ['logic']
37 outputs = ['ps2']
f90269a3 38 channels = (
1a751158
DS
39 {'id': 'clk', 'name': 'Clock', 'desc': 'Clock line'},
40 {'id': 'data', 'name': 'Data', 'desc': 'Data line'},
41 )
42 annotations = (
37a2cca9 43 ('bit', 'Bit'),
1a751158
DS
44 ('start-bit', 'Start bit'),
45 ('stop-bit', 'Stop bit'),
37a2cca9
UH
46 ('parity-ok', 'Parity OK bit'),
47 ('parity-err', 'Parity error bit'),
48 ('data-bit', 'Data bit'),
49 ('word', 'Word'),
50 )
51 annotation_rows = (
52 ('bits', 'Bits', (0,)),
53 ('fields', 'Fields', (1, 2, 3, 4, 5, 6)),
1a751158
DS
54 )
55
56 def __init__(self):
57 self.bits = []
58 self.prev_pins = None
59 self.prev_clock = None
60 self.samplenum = 0
1a751158 61 self.clock_was_high = False
37a2cca9 62 self.bitcount = 0
1a751158
DS
63
64 def start(self):
65 self.out_ann = self.register(srd.OUTPUT_ANN)
66
37a2cca9
UH
67 def putb(self, bit, ann_idx):
68 b = self.bits[bit]
69 self.put(b.ss, b.es, self.out_ann, [ann_idx, [str(b.val)]])
70
71 def putx(self, bit, ann):
72 self.put(self.bits[bit].ss, self.bits[bit].es, self.out_ann, ann)
73
1a751158
DS
74 def handle_bits(self, datapin):
75 # Ignore non start condition bits (useful during keyboard init).
37a2cca9 76 if self.bitcount == 0 and datapin == 1:
1a751158
DS
77 return
78
37a2cca9
UH
79 # Store individual bits and their start/end samplenumbers.
80 self.bits.append(Bit(datapin, self.samplenum, self.samplenum))
1a751158 81
37a2cca9
UH
82 # Fix up end sample numbers of the bits.
83 if self.bitcount > 0:
84 b = self.bits[self.bitcount - 1]
85 self.bits[self.bitcount - 1] = Bit(b.val, b.ss, self.samplenum)
86 if self.bitcount == 11:
87 self.bitwidth = self.bits[1].es - self.bits[2].es
88 b = self.bits[-1]
89 self.bits[-1] = Bit(b.val, b.ss, b.es + self.bitwidth)
1a751158
DS
90
91 # Find all 11 bits. Start + 8 data + odd parity + stop.
37a2cca9
UH
92 if self.bitcount < 11:
93 self.bitcount += 1
1a751158
DS
94 return
95
96 # Extract data word.
97 word = 0
98 for i in range(8):
37a2cca9
UH
99 word |= (self.bits[i + 1].val << i)
100
101 # Calculate parity.
102 parity_ok = (bin(word).count('1') + self.bits[9].val) % 2 == 1
103
104 # Emit annotations.
105 for i in range(11):
106 self.putb(i, Ann.BIT)
107 self.putx(0, [Ann.START, ['Start bit', 'Start', 'S']])
108 self.put(self.bits[1].ss, self.bits[8].es, self.out_ann, [Ann.WORD,
109 ['Data: %02x' % word, 'D: %02x' % word, '%02x' % word]])
110 if parity_ok:
111 self.putx(9, [Ann.PARITY_OK, ['Parity OK', 'Par OK', 'P']])
1a751158 112 else:
37a2cca9
UH
113 self.putx(9, [Ann.PARITY_ERR, ['Parity error', 'Par err', 'PE']])
114 self.putx(10, [Ann.STOP, ['Stop bit', 'Stop', 'St', 'T']])
1a751158 115
37a2cca9 116 self.bits, self.bitcount = [], 0
1a751158
DS
117
118 def find_clk_edge(self, clock_pin, data_pin):
119 # Ignore sample if the clock pin hasn't changed.
120 if clock_pin == self.prev_clock:
121 return
122 self.prev_clock = clock_pin
123
124 # Sample on falling clock edge.
125 if clock_pin == 1:
126 return
127
128 # Found the correct clock edge, now get the bits.
129 self.handle_bits(data_pin)
130
131 def decode(self, ss, es, data):
132 for (self.samplenum, pins) in data:
133 clock_pin, data_pin = pins[0], pins[1]
134
135 # Ignore identical samples.
136 if self.prev_pins == pins:
137 continue
138 self.prev_pins = pins
139
140 if clock_pin == 0 and not self.clock_was_high:
141 continue
142 self.clock_was_high = True
143
144 self.find_clk_edge(clock_pin, data_pin)