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_type, ann_command, ann_parameter, ann_parity, ann_pos, ann_warning = range(7)
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'},
46 ('type', 'Frame Type'),
47 ('command', 'Command'),
48 ('parameter', 'Parameter'),
50 ('position', 'Position'),
51 ('warning', 'Human-readable warnings'),
54 ('bits', 'Bits', (ann_bit,)),
55 ('data', 'Data', (ann_type, ann_command, ann_parameter, ann_parity)),
56 ('positions', 'Positions', (ann_pos,)),
57 ('warnings', 'Warnings', (ann_warning,)),
61 self.samplerate = None
67 def metadata(self, key, value):
68 if key == srd.SRD_CONF_SAMPLERATE:
69 self.samplerate = value
72 self.out_ann = self.register(srd.OUTPUT_ANN)
74 def put_ann(self, ss, es, ann_class, value):
75 self.put(ss, es, self.out_ann, [ann_class, value])
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))
82 if len(self.bits) < 20:
83 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Not enough data bits'])
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
93 # Calculate parity, excluding the parity bit itself
95 for ss, es, value in self.bits[:-1]:
98 par_ss, par_es, par_value = self.bits[19]
101 if (par_value == parity):
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]
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]
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'])
121 elif (type_3_value == 1):
122 type = frame_type_16bit_pos
123 if (parity_even == 1):
124 parity_status = ['OK']
126 parity_status = ['NOK']
127 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Parity error', 'PE'])
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'])
134 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Error', 'Unknown command or parity error'])
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'])
146 self.put_ann(par_ss, par_es, ann_parity, parity_status)
149 if (type == frame_type_16bit_pos) or (type == frame_type_18bit_pos):
152 if (type == frame_type_16bit_pos):
154 for ss, es, value in self.bits[3:19]:
155 pos |= value << count
157 pos = pos if pos < 32768 else pos - 65536
160 for ss, es, value in self.bits[3:19]:
161 pos |= value << count
163 pos = pos if pos < 131072 else pos - 262144
165 self.put_ann(type_es, par_ss, ann_pos, ['%d' % pos])
167 if (type == frame_type_command):
171 for ss, es, value in self.bits[3:11]:
172 cmd |= value << count
175 self.put_ann(type_es, cmd_es, ann_command, ['Command 0x%X' % cmd, 'Cmd 0x%X' % cmd, '0x%X' % cmd])
179 for ss, es, value in self.bits[11:19]:
180 param |= value << count
182 self.put_ann(cmd_es, par_ss, ann_parameter, ['Parameter 0x%X / %d' % (param, param), '0x%X / %d' % (param, param),'0x%X' % param])
193 # Wait for any edge on clk
194 clk, sync, data = self.wait({0: 'e'})
197 bit_es = self.samplenum
199 self.process_bit(sync_value, bit_ss, bit_es, bit_value)
200 bit_ss = self.samplenum