]> sigrok.org Git - libsigrokdecode.git/blob - decoders/xy2-100/pd.py
avr_isp: Add more parts
[libsigrokdecode.git] / decoders / xy2-100 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2019 Uli Huber
5 ## Copyright (C) 2020 Soeren Apel
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
21 import sigrokdecode as srd
22
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)
25
26 class Decoder(srd.Decoder):
27     api_version = 3
28     id = 'xy2-100'
29     name = 'XY2-100'
30     longname = 'XY2-100(E) and XY-200(E) galvanometer protocol'
31     desc = 'Serial protocol for galvanometer positioning in laser systems'
32     license = 'gplv2+'
33     inputs = ['logic']
34     outputs = []
35
36     tags = ['Embedded/industrial']
37
38     channels = (
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'},
42     )
43     optional_channels = (
44         {'id': 'status', 'name': 'STAT', 'desc': 'X, Y or Z axis status'},
45     )
46
47     annotations = (
48         ('bit', 'Data Bit'),
49         ('stat_bit', 'Status Bit'),
50         ('type', 'Frame Type'),
51         ('command', 'Command'),
52         ('parameter', 'Parameter'),
53         ('parity', 'Parity'),
54         ('position', 'Position'),
55         ('status', 'Status'),
56         ('warning', 'Human-readable warnings'),
57     )
58     annotation_rows = (
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,)),
65     )
66
67     def __init__(self):
68         self.samplerate = None
69         self.reset()
70
71     def reset(self):
72         self.bits = []
73         self.stat_bits = []
74         self.stat_skip_bit = True
75
76     def metadata(self, key, value):
77         if key == srd.SRD_CONF_SAMPLERATE:
78             self.samplerate = value
79
80     def start(self):
81         self.out_ann = self.register(srd.OUTPUT_ANN)
82
83     def put_ann(self, ss, es, ann_class, value):
84         self.put(ss, es, self.out_ann, [ann_class, value])
85
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))
89
90         if sync == 0:
91             if len(self.bits) < 20:
92                 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Not enough data bits'])
93                 self.reset()
94                 return
95
96             # Bit structure:
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
101
102             # Calculate parity, excluding the parity bit itself
103             parity = 0
104             for ss, es, value in self.bits[:-1]:
105                 parity ^= value
106
107             par_ss, par_es, par_value = self.bits[19]
108             parity_even = 0
109             parity_odd = 0
110             if (par_value == parity):
111                 parity_even = 1
112             else:
113                 parity_odd = 1
114
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]
117
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]
123
124             ### 18-bit position
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'])
129             ### 16-bit position
130             elif (type_3_value == 1):
131                 type = frame_type_16bit_pos
132                 if (parity_even == 1):
133                     parity_status = ['OK']
134                 else:
135                     parity_status = ['NOK']
136                     self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Parity error', 'PE'])
137             ### Command
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'])
141             ### Other
142             else:
143                 self.put_ann(self.bits[0][0], bit_es, ann_warning, ['Error', 'Unknown command or parity error'])
144                 self.reset()
145                 return
146
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'])
154
155             self.put_ann(par_ss, par_es, ann_parity, parity_status)
156
157             # Output value
158             if (type == frame_type_16bit_pos) or (type == frame_type_18bit_pos):
159                pos = 0
160
161                if (type == frame_type_16bit_pos):
162                    count = 15
163                    for ss, es, value in self.bits[3:19]:
164                        pos |= value << count
165                        count -= 1
166                    pos = pos if pos < 32768 else pos - 65536
167                else:
168                    count = 17
169                    for ss, es, value in self.bits[3:19]:
170                        pos |= value << count
171                        count -= 1
172                    pos = pos if pos < 131072 else pos - 262144
173
174                self.put_ann(type_es, par_ss, ann_pos, ['%d' % pos])
175
176             if (type == frame_type_command):
177                count = 7
178                cmd = 0
179                cmd_es = 0
180                for ss, es, value in self.bits[3:11]:
181                    cmd |= value << count
182                    count -= 1
183                    cmd_es = es
184                self.put_ann(type_es, cmd_es, ann_command, ['Command 0x%X' % cmd, 'Cmd 0x%X' % cmd, '0x%X' % cmd])
185
186                count = 7
187                param = 0
188                for ss, es, value in self.bits[11:19]:
189                    param |= value << count
190                    count -= 1
191                self.put_ann(cmd_es, par_ss, ann_parameter, ['Parameter 0x%X / %d' % (param, param), '0x%X / %d' % (param, param),'0x%X' % param])
192
193             self.reset()
194
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
198             return
199
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))
202
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]
206
207             status = 0
208             count = 18
209             for ss, es, value in self.stat_bits:
210                 status |= value << count
211                 count -= 1
212             self.put_ann(stat_ss, stat_es, ann_status, ['Status 0x%X' % status, '0x%X' % status])
213
214     def decode(self):
215         bit_ss = None
216         bit_es = None
217         bit_value = 0
218         stat_ss = None
219         stat_es = None
220         stat_value = 0
221         sync_value = 0
222         has_stat = self.has_channel(3)
223
224         while True:
225             # Wait for any edge on clk
226             clk, sync, data, stat = self.wait({0: 'e'})
227
228             if clk == 1:
229                 stat_value = stat
230
231                 bit_es = self.samplenum
232                 if bit_ss:
233                     self.process_bit(sync_value, bit_ss, bit_es, bit_value)
234                 bit_ss = self.samplenum
235             else:
236                 bit_value = data
237                 sync_value = sync
238
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