]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2013-2016 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | ## | |
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. | |
10 | ## | |
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. | |
15 | ## | |
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/>. | |
18 | ## | |
19 | ||
20 | import sigrokdecode as srd | |
21 | ||
22 | class SamplerateError(Exception): | |
23 | pass | |
24 | ||
25 | class Decoder(srd.Decoder): | |
26 | api_version = 3 | |
27 | id = 'guess_bitrate' | |
28 | name = 'Guess bitrate' | |
29 | longname = 'Guess bitrate/baudrate' | |
30 | desc = 'Guess the bitrate/baudrate of a UART (or other) protocol.' | |
31 | license = 'gplv2+' | |
32 | inputs = ['logic'] | |
33 | outputs = ['guess_bitrate'] | |
34 | channels = ( | |
35 | {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, | |
36 | ) | |
37 | annotations = ( | |
38 | ('bitrate', 'Bitrate / baudrate'), | |
39 | ) | |
40 | ||
41 | def putx(self, data): | |
42 | self.put(self.ss_edge, self.samplenum, self.out_ann, data) | |
43 | ||
44 | def __init__(self): | |
45 | self.ss_edge = None | |
46 | self.first_transition = True | |
47 | self.bitwidth = None | |
48 | ||
49 | def start(self): | |
50 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
51 | ||
52 | def metadata(self, key, value): | |
53 | if key == srd.SRD_CONF_SAMPLERATE: | |
54 | self.samplerate = value | |
55 | ||
56 | def decode(self): | |
57 | if not self.samplerate: | |
58 | raise SamplerateError('Cannot decode without samplerate.') | |
59 | ||
60 | while True: | |
61 | # Wait for any transition/edge on the data line. | |
62 | self.wait({0: 'e'}) | |
63 | ||
64 | # Get the smallest distance between two transitions | |
65 | # and use that to calculate the bitrate/baudrate. | |
66 | if self.first_transition: | |
67 | self.ss_edge = self.samplenum | |
68 | self.first_transition = False | |
69 | else: | |
70 | b = self.samplenum - self.ss_edge | |
71 | if self.bitwidth is None or b < self.bitwidth: | |
72 | self.bitwidth = b | |
73 | bitrate = int(float(self.samplerate) / float(b)) | |
74 | self.putx([0, ['%d' % bitrate]]) | |
75 | self.ss_edge = self.samplenum |