]> sigrok.org Git - libsigrokdecode.git/blob - decoders/arm_tpiu/pd.py
f50af65b471b652814aaa45f22185a8afc8ea7e3
[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 = 3
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.reset()
46
47     def reset(self):
48         self.buf = []
49         self.syncbuf = []
50         self.prevsample = 0
51         self.stream = 0
52         self.ss_stream = None
53         self.bytenum = 0
54
55     def start(self):
56         self.out_ann = self.register(srd.OUTPUT_ANN)
57         self.out_python = self.register(srd.OUTPUT_PYTHON)
58
59     def stream_changed(self, ss, stream):
60         if self.stream != stream:
61             if self.stream != 0:
62                 self.put(self.ss_stream, ss, self.out_ann,
63                          [0, ['Stream %d' % self.stream, 'S%d' % self.stream]])
64             self.stream = stream
65             self.ss_stream = ss
66
67     def emit_byte(self, ss, es, byte):
68         if self.stream == self.options['stream']:
69             self.put(ss, es, self.out_ann, [1, ['0x%02x' % byte]])
70             self.put(ss, es, self.out_python, ['DATA', 0, (byte, [])])
71
72     def process_frame(self, buf):
73         # Byte 15 contains the lowest bits of bytes 0, 2, ... 14.
74         lowbits = buf[15][2]
75
76         for i in range(0, 15, 2):
77             # Odd bytes can be stream ID or data.
78             delayed_stream_change = None
79             lowbit = (lowbits >> (i // 2)) & 0x01
80             if buf[i][2] & 0x01 != 0:
81                 if lowbit:
82                     delayed_stream_change = buf[i][2] >> 1
83                 else:
84                     self.stream_changed(buf[i][0], buf[i][2] >> 1)
85             else:
86                 byte = buf[i][2] | lowbit
87                 self.emit_byte(buf[i][0], buf[i][1], byte)
88
89             # Even bytes are always data.
90             if i < 14:
91                 self.emit_byte(buf[i+1][0], buf[i+1][1], buf[i+1][2])
92
93             # The stream change can be delayed to occur after the data byte.
94             if delayed_stream_change is not None:
95                 self.stream_changed(buf[i+1][1], delayed_stream_change)
96
97     def decode(self, ss, es, data):
98         ptype, rxtx, pdata = data
99
100         if ptype != 'DATA':
101             return
102
103         # Reset packet if there is a long pause between bytes.
104         self.byte_len = es - ss
105         if ss - self.prevsample > self.byte_len:
106             self.buf = []
107         self.prevsample = es
108
109         self.buf.append((ss, es, pdata[0]))
110         self.bytenum += 1
111
112         # Allow skipping N first bytes of the data. By adjusting the sync
113         # value, one can get initial synchronization as soon as the trace
114         # starts.
115         if self.bytenum < self.options['sync_offset']:
116             self.buf = []
117             return
118
119         # Keep separate buffer for detection of sync packets.
120         # Sync packets override everything else, so that we can regain sync
121         # even if some packets are corrupted.
122         self.syncbuf = self.syncbuf[-3:] + [pdata[0]]
123         if self.syncbuf == [0xFF, 0xFF, 0xFF, 0x7F]:
124             self.buf = []
125             self.syncbuf = []
126             return
127
128         if len(self.buf) == 16:
129             self.process_frame(self.buf)
130             self.buf = []