]>
Commit | Line | Data |
---|---|---|
56c93143 UH |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
3aa79ba3 | 4 | ## Copyright (C) 2013-2016 Uwe Hermann <uwe@hermann-uwe.de> |
56c93143 UH |
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 | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
56c93143 UH |
18 | ## |
19 | ||
56c93143 UH |
20 | import sigrokdecode as srd |
21 | ||
21cda951 UH |
22 | class SamplerateError(Exception): |
23 | pass | |
24 | ||
56c93143 | 25 | class Decoder(srd.Decoder): |
3aa79ba3 | 26 | api_version = 3 |
56c93143 UH |
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'] | |
3f5f3a92 UH |
33 | outputs = [] |
34 | tags = ['Clock/timing', 'Util'] | |
6a15597a | 35 | channels = ( |
56c93143 | 36 | {'id': 'data', 'name': 'Data', 'desc': 'Data line'}, |
da9bcbd9 BV |
37 | ) |
38 | annotations = ( | |
39 | ('bitrate', 'Bitrate / baudrate'), | |
40 | ) | |
56c93143 UH |
41 | |
42 | def putx(self, data): | |
43 | self.put(self.ss_edge, self.samplenum, self.out_ann, data) | |
44 | ||
92b7b49f | 45 | def __init__(self): |
2ba06442 UH |
46 | self.reset() |
47 | ||
48 | def reset(self): | |
56c93143 | 49 | self.ss_edge = None |
56c93143 | 50 | |
e2ff870e | 51 | def start(self): |
be465111 | 52 | self.out_ann = self.register(srd.OUTPUT_ANN) |
56c93143 | 53 | |
e2ff870e UH |
54 | def metadata(self, key, value): |
55 | if key == srd.SRD_CONF_SAMPLERATE: | |
35b380b1 | 56 | self.samplerate = value |
e2ff870e | 57 | |
3aa79ba3 | 58 | def decode(self): |
21cda951 UH |
59 | if not self.samplerate: |
60 | raise SamplerateError('Cannot decode without samplerate.') | |
56c93143 | 61 | |
2ba06442 UH |
62 | # Get the first edge on the data line. |
63 | self.wait({0: 'e'}) | |
64 | self.ss_edge = self.samplenum | |
65 | ||
66 | # Get any subsequent edge on the data line. Get the smallest | |
67 | # distance between any two transitions, assuming it corresponds | |
68 | # to one bit time of the respective bitrate of the input stream. | |
69 | # This heuristics keeps getting better for longer captures. | |
70 | bitwidth = None | |
3aa79ba3 | 71 | while True: |
3aa79ba3 | 72 | self.wait({0: 'e'}) |
56c93143 | 73 | |
2ba06442 UH |
74 | b = self.samplenum - self.ss_edge |
75 | if bitwidth is None or b < bitwidth: | |
76 | bitwidth = b | |
77 | bitrate = int(float(self.samplerate) / float(b)) | |
78 | self.putx([0, ['%d' % bitrate]]) | |
79 | self.ss_edge = self.samplenum |