]> sigrok.org Git - libsigrokdecode.git/blob - decoders/arm_tpiu/pd.py
license: remove FSF postal address from boiler plate license text
[libsigrokdecode.git] / decoders / arm_tpiu / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2015 Petteri Aimonen <jpa@sigrok.mail.kapsi.fi>
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 Decoder(srd.Decoder):
23     api_version = 2
24     id = 'arm_tpiu'
25     name = 'ARM TPIU'
26     longname = 'ARM Trace Port Interface Unit'
27     desc = 'Filter TPIU formatted trace data into separate streams.'
28     license = 'gplv2+'
29     inputs = ['uart']
30     outputs = ['uart'] # Emulate uart output so that arm_itm/arm_etm can stack.
31     options = (
32         {'id': 'stream', 'desc': 'Stream index', 'default': 1},
33         {'id': 'sync_offset', 'desc': 'Initial sync offset', 'default': 0},
34     )
35     annotations = (
36         ('stream', 'Current stream'),
37         ('data', 'Stream data'),
38     )
39     annotation_rows = (
40         ('stream', 'Current stream', (0,)),
41         ('data', 'Stream data', (1,)),
42     )
43
44     def __init__(self):
45         self.buf = []
46         self.syncbuf = []
47         self.prevsample = 0
48         self.stream = 0
49         self.ss_stream = None
50         self.bytenum = 0
51
52     def start(self):
53         self.out_ann = self.register(srd.OUTPUT_ANN)
54         self.out_python = self.register(srd.OUTPUT_PYTHON)
55
56     def stream_changed(self, ss, stream):
57         if self.stream != stream:
58             if self.stream != 0:
59                 self.put(self.ss_stream, ss, self.out_ann,
60                          [0, ['Stream %d' % self.stream, 'S%d' % self.stream]])
61             self.stream = stream
62             self.ss_stream = ss
63
64     def emit_byte(self, ss, es, byte):
65         if self.stream == self.options['stream']:
66             self.put(ss, es, self.out_ann, [1, ['0x%02x' % byte]])
67             self.put(ss, es, self.out_python, ['DATA', 0, (byte, [])])
68
69     def process_frame(self, buf):
70         # Byte 15 contains the lowest bits of bytes 0, 2, ... 14.
71         lowbits = buf[15][2]
72
73         for i in range(0, 15, 2):
74             # Odd bytes can be stream ID or data.
75             delayed_stream_change = None
76             lowbit = (lowbits >> (i // 2)) & 0x01
77             if buf[i][2] & 0x01 != 0:
78                 if lowbit:
79                     delayed_stream_change = buf[i][2] >> 1
80                 else:
81                     self.stream_changed(buf[i][0], buf[i][2] >> 1)
82             else:
83                 byte = buf[i][2] | lowbit
84                 self.emit_byte(buf[i][0], buf[i][1], byte)
85
86             # Even bytes are always data.
87             if i < 14:
88                 self.emit_byte(buf[i+1][0], buf[i+1][1], buf[i+1][2])
89
90             # The stream change can be delayed to occur after the data byte.
91             if delayed_stream_change is not None:
92                 self.stream_changed(buf[i+1][1], delayed_stream_change)
93
94     def decode(self, ss, es, data):
95         ptype, rxtx, pdata = data
96
97         if ptype != 'DATA':
98             return
99
100         # Reset packet if there is a long pause between bytes.
101         self.byte_len = es - ss
102         if ss - self.prevsample > self.byte_len:
103             self.buf = []
104         self.prevsample = es
105
106         self.buf.append((ss, es, pdata[0]))
107         self.bytenum += 1
108
109         # Allow skipping N first bytes of the data. By adjusting the sync
110         # value, one can get initial synchronization as soon as the trace
111         # starts.
112         if self.bytenum < self.options['sync_offset']:
113             self.buf = []
114             return
115
116         # Keep separate buffer for detection of sync packets.
117         # Sync packets override everything else, so that we can regain sync
118         # even if some packets are corrupted.
119         self.syncbuf = self.syncbuf[-3:] + [pdata[0]]
120         if self.syncbuf == [0xFF, 0xFF, 0xFF, 0x7F]:
121             self.buf = []
122             self.syncbuf = []
123             return
124
125         if len(self.buf) == 16:
126             self.process_frame(self.buf)
127             self.buf = []