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