2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2019 Uli Huber
5 ## Copyright (C) 2020 Soeren Apel
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.
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.
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/>.
21 import sigrokdecode as srd
23 ann_bit, ann_stat_bit, ann_type, ann_command, ann_parameter, ann_parity, ann_pos, ann_status, ann_warning = range(9)
24 frame_type_none, frame_type_command, frame_type_16bit_pos, frame_type_18bit_pos = range(4)
26 class Decoder(srd.Decoder):
30 longname = 'XY2-100(E) and XY-200(E) galvanometer protocol'
31 desc = 'Serial protocol for galvanometer positioning in laser systems'
36 tags = ['Embedded/industrial']
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'},
44 {'id': 'status', 'name': 'STAT', 'desc': 'X, Y or Z axis status'},
49 ('stat_bit', 'Status Bit'),
50 ('type', 'Frame Type'),
51 ('command', 'Command'),
52 ('parameter', 'Parameter'),
54 ('position', 'Position'),
56 ('warning', 'Human-readable warnings'),
59 ('bits', 'Data Bits', (ann_bit,)),
60 ('stat_bits', 'Status Bits', (ann_stat_bit,)),
61 ('data', 'Data', (ann_type, ann_command, ann_parameter, ann_parity)),
62 ('positions', 'Positions', (ann_pos,)),
63 ('statuses', 'Statuses', (ann_status,)),
64 ('warnings', 'Warnings', (ann_warning,)),
68 self.samplerate = None
74 self.stat_skip_bit = True
76 def metadata(self, key, value):
77 if key == srd.SRD_CONF_SAMPLERATE:
78 self.samplerate = value
81 self.out_ann = self.register(srd.OUTPUT_ANN)
83 def put_ann(self, ss, es, ann_class, value):
84 self.put(ss, es, self.out_ann, [ann_class, value])
86 def process_bit(self, sync, bit_ss, bit_es, bit_value):
87 self.put_ann(bit_ss, bit_es, ann_bit, ['%d' % bit_value])
88 self.bits.append((bit_ss, bit_es, bit_value))
91 if len(self.bits) < 20:
92 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Not enough data bits'])
97 # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
98 # T --------------- 18-bit pos ----------------- PARITY or
99 # -TYPE-- ------------ 16-bit pos -------------- PARITY or
100 # -TYPE-- -8-bit command -8-bit parameter value- PARITY
102 # Calculate parity, excluding the parity bit itself
104 for ss, es, value in self.bits[:-1]:
107 par_ss, par_es, par_value = self.bits[19]
110 if (par_value == parity):
115 type_1_value = self.bits[0][2]
116 type_3_value = (self.bits[0][2] << 2) | (self.bits[1][2] << 1) | self.bits[2][2]
118 # Determine frame type
119 type = frame_type_none
120 parity_status = ['X', 'Unknown']
121 type_ss = self.bits[0][0]
122 type_es = self.bits[2][1]
125 if (type_1_value == 1) and (parity_odd == 1):
126 type = frame_type_18bit_pos
127 type_es = self.bits[0][1]
128 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'])
130 elif (type_3_value == 1):
131 type = frame_type_16bit_pos
132 if (parity_even == 1):
133 parity_status = ['OK']
135 parity_status = ['NOK']
136 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Parity error', 'PE'])
138 elif (type_3_value == 7) and (parity_even == 1):
139 type = frame_type_command
140 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'])
143 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Error', 'Unknown command or parity error'])
147 # Output command and parity annotations
148 if (type == frame_type_16bit_pos):
149 self.put_ann(type_ss, type_es, ann_type, ['16 bit Position Frame', '16 bit Pos', 'Pos', 'P'])
150 if (type == frame_type_18bit_pos):
151 self.put_ann(type_ss, type_es, ann_type, ['18 bit Position Frame', '18 bit Pos', 'Pos', 'P'])
152 if (type == frame_type_command):
153 self.put_ann(type_ss, type_es, ann_type, ['Command Frame', 'Command', 'C'])
155 self.put_ann(par_ss, par_es, ann_parity, parity_status)
158 if (type == frame_type_16bit_pos) or (type == frame_type_18bit_pos):
161 if (type == frame_type_16bit_pos):
163 for ss, es, value in self.bits[3:19]:
164 pos |= value << count
166 pos = pos if pos < 32768 else pos - 65536
169 for ss, es, value in self.bits[3:19]:
170 pos |= value << count
172 pos = pos if pos < 131072 else pos - 262144
174 self.put_ann(type_es, par_ss, ann_pos, ['%d' % pos])
176 if (type == frame_type_command):
180 for ss, es, value in self.bits[3:11]:
181 cmd |= value << count
184 self.put_ann(type_es, cmd_es, ann_command, ['Command 0x%X' % cmd, 'Cmd 0x%X' % cmd, '0x%X' % cmd])
188 for ss, es, value in self.bits[11:19]:
189 param |= value << count
191 self.put_ann(cmd_es, par_ss, ann_parameter, ['Parameter 0x%X / %d' % (param, param), '0x%X / %d' % (param, param),'0x%X' % param])
195 def process_stat_bit(self, sync, bit_ss, bit_es, bit_value):
196 if self.stat_skip_bit:
197 self.stat_skip_bit = False
200 self.put_ann(bit_ss, bit_es, ann_stat_bit, ['%d' % bit_value])
201 self.stat_bits.append((bit_ss, bit_es, bit_value))
203 if (sync == 0) and (len(self.stat_bits) == 19):
204 stat_ss = self.stat_bits[0][0]
205 stat_es = self.stat_bits[18][1]
209 for ss, es, value in self.stat_bits:
210 status |= value << count
212 self.put_ann(stat_ss, stat_es, ann_status, ['Status 0x%X' % status, '0x%X' % status])
222 has_stat = self.has_channel(3)
225 # Wait for any edge on clk
226 clk, sync, data, stat = self.wait({0: 'e'})
231 bit_es = self.samplenum
233 self.process_bit(sync_value, bit_ss, bit_es, bit_value)
234 bit_ss = self.samplenum
239 stat_es = self.samplenum
240 if stat_ss and has_stat:
241 self.process_stat_bit(sync_value, stat_ss, stat_es, stat_value)
242 stat_ss = self.samplenum