]>
Commit | Line | Data |
---|---|---|
2a743706 M |
1 | ## |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2019 Marco Geisler <m-sigrok@mageis.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 | from .lists import * | |
22 | ||
743457ac UH |
23 | ANN_STROBE, ANN_SINGLE_READ, ANN_SINGLE_WRITE, ANN_BURST_READ, \ |
24 | ANN_BURST_WRITE, ANN_STATUS_READ, ANN_STATUS, ANN_WARN = range(8) | |
25 | ||
2a743706 M |
26 | class Decoder(srd.Decoder): |
27 | api_version = 3 | |
28 | id = 'cc1101' | |
29 | name = 'CC1101' | |
30 | longname = 'Texas Instruments CC1101' | |
31 | desc = 'Low-power sub-1GHz RF transceiver chip.' | |
32 | license = 'gplv2+' | |
33 | inputs = ['spi'] | |
34 | outputs = [] | |
35 | tags = ['IC', 'Wireless/RF'] | |
36 | annotations = ( | |
37 | ('strobe', 'Command strobe'), | |
38 | ('single_read', 'Single register read'), | |
39 | ('single_write', 'Single register write'), | |
40 | ('burst_read', 'Burst register read'), | |
41 | ('burst_write', 'Burst register write'), | |
42 | ('status', 'Status register'), | |
43 | ('warning', 'Warning'), | |
44 | ) | |
2a743706 | 45 | annotation_rows = ( |
743457ac UH |
46 | ('cmd', 'Commands', (ANN_STROBE,)), |
47 | ('data', 'Data', (ANN_SINGLE_READ, ANN_SINGLE_WRITE, ANN_BURST_READ, | |
48 | ANN_BURST_WRITE, ANN_STATUS_READ)), | |
49 | ('status', 'Status register', (ANN_STATUS,)), | |
50 | ('warnings', 'Warnings', (ANN_WARN,)), | |
2a743706 M |
51 | ) |
52 | ||
53 | def __init__(self): | |
54 | self.reset() | |
55 | ||
56 | def reset(self): | |
57 | self.next() | |
58 | self.requirements_met = True | |
59 | self.cs_was_released = False | |
60 | ||
61 | def start(self): | |
62 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
63 | ||
64 | def warn(self, pos, msg): | |
65 | '''Put a warning message 'msg' at 'pos'.''' | |
743457ac | 66 | self.put(pos[0], pos[1], self.out_ann, [ANN_WARN, [msg]]) |
2a743706 M |
67 | |
68 | def putp(self, pos, ann, msg): | |
69 | '''Put an annotation message 'msg' at 'pos'.''' | |
70 | self.put(pos[0], pos[1], self.out_ann, [ann, [msg]]) | |
71 | ||
72 | def putp2(self, pos, ann, msg1, msg2): | |
73 | '''Put an annotation message 'msg' at 'pos'.''' | |
74 | self.put(pos[0], pos[1], self.out_ann, [ann, [msg1, msg2]]) | |
75 | ||
76 | def next(self): | |
77 | '''Resets the decoder after a complete command was decoded.''' | |
78 | # 'True' for the first byte after CS# went low. | |
79 | self.first = True | |
80 | ||
81 | # The current command, and the minimum and maximum number | |
82 | # of data bytes to follow. | |
83 | self.cmd = None | |
84 | self.min = 0 | |
85 | self.max = 0 | |
86 | ||
87 | # Used to collect the bytes after the command byte | |
88 | # (and the start/end sample number). | |
89 | self.mb = [] | |
90 | self.ss_mb = -1 | |
91 | self.es_mb = -1 | |
92 | ||
93 | def mosi_bytes(self): | |
94 | '''Returns the collected MOSI bytes of a multi byte command.''' | |
95 | return [b[0] for b in self.mb] | |
96 | ||
97 | def miso_bytes(self): | |
98 | '''Returns the collected MISO bytes of a multi byte command.''' | |
99 | return [b[1] for b in self.mb] | |
100 | ||
101 | def decode_command(self, pos, b): | |
102 | '''Decodes the command byte 'b' at position 'pos' and prepares | |
103 | the decoding of the following data bytes.''' | |
104 | c = self.parse_command(b) | |
105 | if c is None: | |
106 | self.warn(pos, 'unknown command') | |
107 | return | |
108 | ||
109 | self.cmd, self.dat, self.min, self.max = c | |
110 | ||
111 | if self.cmd in ('STROBE_CMD',): | |
743457ac | 112 | self.putp(pos, ANN_STROBE, self.format_command()) |
2a743706 M |
113 | else: |
114 | # Don't output anything now, the command is merged with | |
115 | # the data bytes following it. | |
116 | self.ss_mb = pos[0] | |
117 | ||
118 | def format_command(self): | |
119 | '''Returns the label for the current command.''' | |
120 | if self.cmd == 'SINGLE_READ': | |
2a743706 M |
121 | return 'Read' |
122 | if self.cmd == 'BURST_READ': | |
2a743706 M |
123 | return 'Burst read' |
124 | if self.cmd == 'SINGLE_WRITE': | |
2a743706 M |
125 | return 'Write' |
126 | if self.cmd == 'BURST_WRITE': | |
2a743706 M |
127 | return 'Burst write' |
128 | if self.cmd == 'STATUS_READ': | |
2a743706 M |
129 | return 'Status read' |
130 | if self.cmd == 'STROBE_CMD': | |
4c58713b | 131 | reg = strobes.get(self.dat, 'unknown strobe') |
2a743706 M |
132 | return 'STROBE "{}"'.format(reg) |
133 | else: | |
134 | return 'TODO Cmd {}'.format(self.cmd) | |
135 | ||
136 | def parse_command(self, b): | |
137 | '''Parses the command byte. | |
138 | ||
139 | Returns a tuple consisting of: | |
140 | - the name of the command | |
141 | - additional data needed to dissect the following bytes | |
142 | - minimum number of following bytes | |
143 | - maximum number of following bytes (None for infinite) | |
144 | ''' | |
145 | ||
146 | addr = b & 0x3F | |
147 | if (addr < 0x30) or (addr == 0x3E) or (addr == 0x3F): | |
148 | if (b & 0xC0) == 0x00: | |
149 | return ('SINGLE_WRITE', addr, 1, 1) | |
150 | if (b & 0xC0) == 0x40: | |
151 | return ('BURST_WRITE', addr, 1, 99999) | |
152 | if (b & 0xC0) == 0x80: | |
153 | return ('SINGLE_READ', addr, 1, 1) | |
154 | if (b & 0xC0) == 0xC0: | |
155 | return ('BURST_READ', addr, 1, 99999) | |
156 | else: | |
157 | self.warn(pos, 'unknown address/command combination') | |
158 | else: | |
159 | if (b & 0x40) == 0x00: | |
160 | return ('STROBE_CMD', addr, 0, 0) | |
161 | if (b & 0xC0) == 0xC0: | |
162 | return ('STATUS_READ', addr, 1, 99999) | |
163 | else: | |
164 | self.warn(pos, 'unknown address/command combination') | |
165 | ||
166 | def decode_register(self, pos, ann, regid, data): | |
167 | '''Decodes a register. | |
168 | ||
169 | pos -- start and end sample numbers of the register | |
170 | ann -- the annotation number that is used to output the register. | |
171 | regid -- may be either an integer used as a key for the 'regs' | |
172 | dictionary, or a string directly containing a register name.' | |
173 | data -- the register content. | |
174 | ''' | |
175 | ||
176 | if type(regid) == int: | |
177 | # Get the name of the register. | |
178 | if regid not in regs: | |
179 | self.warn(pos, 'unknown register') | |
180 | return | |
181 | name = '{} (0x{:02X})'.format(regs[regid], regid) | |
182 | else: | |
183 | name = regid | |
184 | ||
743457ac | 185 | if regid == 'STATUS' and ann == ANN_STATUS: |
2a743706 M |
186 | label = 'Status' |
187 | self.decode_status_reg(pos, ann, data, label) | |
188 | else: | |
189 | if self.cmd in ('SINGLE_WRITE', 'SINGLE_READ', 'STATUS_READ', 'BURST_READ', 'BURST_WRITE'): | |
190 | label = '{}: {}'.format(self.format_command(), name) | |
191 | else: | |
192 | label = 'Reg ({}) {}'.format(self.cmd, name) | |
193 | self.decode_mb_data(pos, ann, data, label) | |
194 | ||
195 | def decode_status_reg(self, pos, ann, data, label): | |
196 | '''Decodes the data bytes 'data' of a status register at position | |
197 | 'pos'. The decoded data is prefixed with 'label'.''' | |
198 | status = data[0] | |
199 | # bit 7 --> CHIP_RDYn | |
200 | if status & 0b10000000 == 0b10000000: | |
201 | longtext_chiprdy = 'CHIP_RDYn is high! ' | |
202 | else: | |
203 | longtext_chiprdy = '' | |
204 | # bits 6:4 --> STATE | |
205 | state = (status & 0x70) >> 4 | |
206 | longtext_state = 'STATE is {}, '.format(status_reg_states[state]) | |
207 | # bits 3:0 --> FIFO_BYTES_AVAILABLE | |
208 | fifo_bytes = status & 0x0F | |
209 | if self.cmd in ('SINGLE_READ', 'STATUS_READ', 'BURST_READ'): | |
210 | longtext_fifo = '{} bytes available in RX FIFO'.format(fifo_bytes) | |
211 | else: | |
212 | longtext_fifo = '{} bytes free in TX FIFO'.format(fifo_bytes) | |
213 | ||
214 | text = '{} = "0x{:02X}"'.format(label, status) | |
215 | longtext = ''.join([text, '; ', longtext_chiprdy, longtext_state, longtext_fifo]) | |
216 | self.putp2(pos, ann, longtext, text) | |
217 | ||
218 | def decode_mb_data(self, pos, ann, data, label): | |
219 | '''Decodes the data bytes 'data' of a multibyte command at position | |
220 | 'pos'. The decoded data is prefixed with 'label'.''' | |
221 | ||
222 | def escape(b): | |
223 | return '{:02X}'.format(b) | |
224 | ||
225 | data = ' '.join([escape(b) for b in data]) | |
226 | text = '{} = "0x{}"'.format(label, data) | |
227 | self.putp(pos, ann, text) | |
228 | ||
229 | def finish_command(self, pos): | |
230 | '''Decodes the remaining data bytes at position 'pos'.''' | |
231 | ||
232 | if self.cmd == 'SINGLE_WRITE': | |
743457ac | 233 | self.decode_register(pos, ANN_SINGLE_WRITE, |
2a743706 M |
234 | self.dat, self.mosi_bytes()) |
235 | elif self.cmd == 'BURST_WRITE': | |
743457ac | 236 | self.decode_register(pos, ANN_BURST_WRITE, |
2a743706 M |
237 | self.dat, self.mosi_bytes()) |
238 | elif self.cmd == 'SINGLE_READ': | |
743457ac | 239 | self.decode_register(pos, ANN_SINGLE_READ, |
2a743706 M |
240 | self.dat, self.miso_bytes()) |
241 | elif self.cmd == 'BURST_READ': | |
743457ac | 242 | self.decode_register(pos, ANN_BURST_READ, |
2a743706 M |
243 | self.dat, self.miso_bytes()) |
244 | elif self.cmd == 'STROBE_CMD': | |
743457ac | 245 | self.decode_register(pos, ANN_STROBE, |
2a743706 M |
246 | self.dat, self.mosi_bytes()) |
247 | elif self.cmd == 'STATUS_READ': | |
743457ac | 248 | self.decode_register(pos, ANN_STATUS_READ, |
2a743706 M |
249 | self.dat, self.miso_bytes()) |
250 | else: | |
251 | self.warn(pos, 'unhandled command') | |
252 | ||
253 | def decode(self, ss, es, data): | |
254 | if not self.requirements_met: | |
255 | return | |
256 | ||
257 | ptype, data1, data2 = data | |
258 | ||
259 | if ptype == 'CS-CHANGE': | |
260 | if data1 is None: | |
261 | if data2 is None: | |
262 | self.requirements_met = False | |
263 | raise ChannelError('CS# pin required.') | |
264 | elif data2 == 1: | |
265 | self.cs_was_released = True | |
266 | ||
267 | if data1 == 0 and data2 == 1: | |
268 | # Rising edge, the complete command is transmitted, process | |
269 | # the bytes that were sent after the command byte. | |
270 | if self.cmd: | |
271 | # Check if we got the minimum number of data bytes | |
272 | # after the command byte. | |
273 | if len(self.mb) < self.min: | |
274 | self.warn((ss, ss), 'missing data bytes') | |
275 | elif self.mb: | |
276 | self.finish_command((self.ss_mb, self.es_mb)) | |
277 | ||
278 | self.next() | |
279 | self.cs_was_released = True | |
280 | ||
281 | elif ptype == 'DATA' and self.cs_was_released: | |
282 | mosi, miso = data1, data2 | |
283 | pos = (ss, es) | |
284 | ||
285 | if miso is None or mosi is None: | |
286 | self.requirements_met = False | |
287 | raise ChannelError('Both MISO and MOSI pins required.') | |
288 | ||
289 | if self.first: | |
290 | self.first = False | |
291 | # First MOSI byte is always the command. | |
292 | self.decode_command(pos, mosi) | |
293 | # First MISO byte is always the status register. | |
743457ac | 294 | self.decode_register(pos, ANN_STATUS, 'STATUS', [miso]) |
2a743706 M |
295 | else: |
296 | if not self.cmd or len(self.mb) >= self.max: | |
297 | self.warn(pos, 'excess byte') | |
298 | else: | |
299 | # Collect the bytes after the command byte. | |
300 | if self.ss_mb == -1: | |
301 | self.ss_mb = ss | |
302 | self.es_mb = es | |
303 | self.mb.append((mosi, miso)) |