2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2019 Uli Huber
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.
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.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
21 import sigrokdecode as srd
23 ann_hdrbit, ann_databit, ann_paritybit, ann_bitlegende, ann_pos, ann_warning = range(6)
25 class Decoder(srd.Decoder):
29 longname = 'XY2-100 Galvo Protocol'
30 desc = 'Serial protocol for Galvo positioning'
34 tags = ['Embedded/industrial']
36 {'id': 'clk', 'name': 'D0', 'desc': 'Clock','default': 0},
37 {'id': 'sync', 'name': 'D1', 'desc': 'Sync','default': 1},
38 {'id': 'PosX', 'name': 'D2', 'desc': 'X/Y/Z','default': 2},
42 ('hdrbit', 'Header bit'),
43 ('databit', 'Data bit'),
44 ('paritybit', 'Parity bit'),
45 ('bitlegende', 'Bit Legende'),
46 ('position', 'Position Data'),
47 ('warning', 'Human-readable warnings'),
50 ('bits', 'Bits', (ann_hdrbit, ann_databit, ann_paritybit)),
51 ('legende', 'Legende', (ann_bitlegende,)),
52 ('positions', 'Position', (ann_pos,)),
53 ('warnings', 'Warnings', (ann_warning,)),
57 self.samplerate = None
65 def metadata(self, key, value):
66 if key == srd.SRD_CONF_SAMPLERATE:
67 self.samplerate = value
70 self.out_ann = self.register(srd.OUTPUT_ANN)
72 def putbit(self, ss, es, typ, value):
73 self.put(ss, es, self.out_ann, [typ, ['%s' % (value)]])
81 # Wait for any edge CLK or SYNC
82 clk, sync, PosX = self.wait({0: 'r'})
83 bitstart = self.samplenum
84 bitend = bitstart+(bitstart-lastsample)
86 # start data collection
88 # wait for falling edge clk
89 clk, sync, PosX = self.wait({0: 'f'})
90 if len(self.hdrbits) < 3:
91 if len(self.hdrbits) == 0:
92 headerstart = bitstart
93 self.hdrbits = [(PosX, bitstart, self.samplenum)] + self.hdrbits
94 self.putbit(bitstart, bitend, ann_hdrbit, PosX)
96 if len(self.databits) == 0:
98 self.databits = [(PosX, bitstart, self.samplenum)] + self.databits
99 #self.putbit(bitstart, self.samplenum+1, ann_databit, PosX)
100 self.putbit(bitstart,bitend, ann_databit, PosX)
103 # get parity bit, calculate position
105 clk, sync, PosX = self.wait({0: 'f'})
106 self.paritybits = [PosX]
107 self.putbit(dataend, bitend, ann_paritybit, PosX)
108 self.put(dataend,bitend, self.out_ann, [ann_bitlegende, ['Parity' ]])
109 self.put(headerstart,datastart, self.out_ann, [ann_bitlegende, ['Header' ]])
110 self.put(datastart, dataend, self.out_ann, [ann_bitlegende, ['Position' ]])
113 for x in self.hdrbits:
117 for x in self.databits:
120 positionX = positionX + (1 << stelle)
123 self.put(datastart, dataend, self.out_ann, [ann_pos, ['%02d' % (positionX)]])
127 self.put(dataend, bitend, self.out_ann, [ann_pos, ['%02s' % (check)]])
129 #self.put(datastart, self.samplenum, self.out_ann,
130 #[ann_warning, ['%s: %02X' % ('WARNUNG: ', 4711)]])
135 lastsample = bitstart