]> sigrok.org Git - libsigrokdecode.git/blame - decoders/xy2-100/pd.py
xy2-100: Rewrite PD for XY2-100E compatibility and features
[libsigrokdecode.git] / decoders / xy2-100 / pd.py
CommitLineData
6e02446f
UH
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2019 Uli Huber
ae607f25 5## Copyright (C) 2020 Soeren Apel
6e02446f
UH
6##
7## This program is free software; you can redistribute it and/or modify
8## it under the terms of the GNU General Public License as published by
9## the Free Software Foundation; either version 2 of the License, or
10## (at your option) any later version.
11##
12## This program is distributed in the hope that it will be useful,
13## but WITHOUT ANY WARRANTY; without even the implied warranty of
14## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15## GNU General Public License for more details.
16##
17## You should have received a copy of the GNU General Public License
18## along with this program; if not, see <http://www.gnu.org/licenses/>.
19##
20
6e02446f
UH
21import sigrokdecode as srd
22
ae607f25
SA
23ann_bit, ann_type, ann_command, ann_parameter, ann_parity, ann_pos, ann_warning = range(7)
24frame_type_none, frame_type_command, frame_type_16bit_pos, frame_type_18bit_pos = range(4)
6e02446f
UH
25
26class Decoder(srd.Decoder):
27 api_version = 3
28 id = 'xy2-100'
29 name = 'XY2-100'
ae607f25
SA
30 longname = 'XY2-100(E) and XY-200(E) galvanometer protocol'
31 desc = 'Serial protocol for galvanometer positioning in laser systems'
6e02446f
UH
32 license = 'gplv2+'
33 inputs = ['logic']
34 outputs = []
ae607f25 35
6e02446f 36 tags = ['Embedded/industrial']
ae607f25 37
6e02446f 38 channels = (
ae607f25
SA
39 {'id': 'clk', 'name': 'CLK', 'desc': 'Clock'},
40 {'id': 'sync', 'name': 'SYNC', 'desc': 'Sync'},
41 {'id': 'data', 'name': 'DATA', 'desc': 'X, Y or Z axis data'},
6e02446f
UH
42 )
43
44 annotations = (
ae607f25
SA
45 ('bit', 'Bit'),
46 ('type', 'Frame Type'),
47 ('command', 'Command'),
48 ('parameter', 'Parameter'),
49 ('parity', 'Parity'),
50 ('position', 'Position'),
6e02446f
UH
51 ('warning', 'Human-readable warnings'),
52 )
53 annotation_rows = (
ae607f25
SA
54 ('bits', 'Bits', (ann_bit,)),
55 ('data', 'Data', (ann_type, ann_command, ann_parameter, ann_parity)),
56 ('positions', 'Positions', (ann_pos,)),
6e02446f
UH
57 ('warnings', 'Warnings', (ann_warning,)),
58 )
59
60 def __init__(self):
61 self.samplerate = None
62 self.reset()
63
64 def reset(self):
ae607f25 65 self.bits = []
6e02446f
UH
66
67 def metadata(self, key, value):
68 if key == srd.SRD_CONF_SAMPLERATE:
69 self.samplerate = value
70
71 def start(self):
72 self.out_ann = self.register(srd.OUTPUT_ANN)
73
ae607f25
SA
74 def put_ann(self, ss, es, ann_class, value):
75 self.put(ss, es, self.out_ann, [ann_class, value])
76
77 def process_bit(self, sync, bit_ss, bit_es, bit_value):
78 self.put_ann(bit_ss, bit_es, ann_bit, ['%d' % bit_value])
79 self.bits.append((bit_ss, bit_es, bit_value))
80
81 if sync == 0:
82 if len(self.bits) < 20:
83 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Not enough data bits'])
84 self.reset()
85 return
86
87 # Bit structure:
88 # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
89 # T --------------- 18-bit pos ----------------- PARITY or
90 # -TYPE-- ------------ 16-bit pos -------------- PARITY or
91 # -TYPE-- -8-bit command -8-bit parameter value- PARITY
92
93 # Calculate parity, excluding the parity bit itself
94 parity = 0
95 for ss, es, value in self.bits[:-1]:
96 parity ^= value
97
98 par_ss, par_es, par_value = self.bits[19]
99 parity_even = 0
100 parity_odd = 0
101 if (par_value == parity):
102 parity_even = 1
103 else:
104 parity_odd = 1
105
106 type_1_value = self.bits[0][2]
107 type_3_value = (self.bits[0][2] << 2) | (self.bits[1][2] << 1) | self.bits[2][2]
108
109 # Determine frame type
110 type = frame_type_none
111 parity_status = ['X', 'Unknown']
112 type_ss = self.bits[0][0]
113 type_es = self.bits[2][1]
114
115 ### 18-bit position
116 if (type_1_value == 1) and (parity_odd == 1):
117 type = frame_type_18bit_pos
118 type_es = self.bits[0][1]
119 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified'])
120 ### 16-bit position
121 elif (type_3_value == 1):
122 type = frame_type_16bit_pos
123 if (parity_even == 1):
124 parity_status = ['OK']
125 else:
126 parity_status = ['NOK']
127 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Parity error', 'PE'])
128 ### Command
129 elif (type_3_value == 7) and (parity_even == 1):
130 type = frame_type_command
131 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Careful: 18-bit position frames with wrong parity and command frames with wrong parity cannot be identified'])
132 ### Other
133 else:
134 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Error', 'Unknown command or parity error'])
135 self.reset()
136 return
137
138 # Output command and parity annotations
139 if (type == frame_type_16bit_pos):
140 self.put_ann(type_ss, type_es, ann_type, ['16 bit Position Frame', '16 bit Pos', 'Pos', 'P'])
141 if (type == frame_type_18bit_pos):
142 self.put_ann(type_ss, type_es, ann_type, ['18 bit Position Frame', '18 bit Pos', 'Pos', 'P'])
143 if (type == frame_type_command):
144 self.put_ann(type_ss, type_es, ann_type, ['Command Frame', 'Command', 'C'])
145
146 self.put_ann(par_ss, par_es, ann_parity, parity_status)
147
148 # Output value
149 if (type == frame_type_16bit_pos) or (type == frame_type_18bit_pos):
150 pos = 0
151
152 if (type == frame_type_16bit_pos):
153 count = 15
154 for ss, es, value in self.bits[3:19]:
155 pos |= value << count
156 count -= 1
157 pos = pos if pos < 32768 else pos - 65536
158 else:
159 count = 17
160 for ss, es, value in self.bits[3:19]:
161 pos |= value << count
162 count -= 1
163 pos = pos if pos < 131072 else pos - 262144
164
165 self.put_ann(type_es, par_ss, ann_pos, ['%d' % pos])
166
167 if (type == frame_type_command):
168 count = 7
169 cmd = 0
170 cmd_es = 0
171 for ss, es, value in self.bits[3:11]:
172 cmd |= value << count
173 count -= 1
174 cmd_es = es
175 self.put_ann(type_es, cmd_es, ann_command, ['Command 0x%X' % cmd, 'Cmd 0x%X' % cmd, '0x%X' % cmd])
176
177 count = 7
178 param = 0
179 for ss, es, value in self.bits[11:19]:
180 param |= value << count
181 count -= 1
182 self.put_ann(cmd_es, par_ss, ann_parameter, ['Parameter 0x%X / %d' % (param, param), '0x%X / %d' % (param, param),'0x%X' % param])
183
184 self.reset()
6e02446f
UH
185
186 def decode(self):
ae607f25
SA
187 bit_ss = None
188 bit_es = None
189 bit_value = 0
190 sync_value = 0
191
6e02446f 192 while True:
ae607f25
SA
193 # Wait for any edge on clk
194 clk, sync, data = self.wait({0: 'e'})
195
196 if clk == 1:
197 bit_es = self.samplenum
198 if bit_ss:
199 self.process_bit(sync_value, bit_ss, bit_es, bit_value)
200 bit_ss = self.samplenum
201 else:
202 bit_value = data
203 sync_value = sync