|
| 1 | +## |
| 2 | +## This file is part of the libsigrokdecode project. |
| 3 | +## |
| 4 | +## Copyright (C) 2023 Marc Font Freixa <[email protected]> |
| 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 collections import namedtuple |
| 22 | + |
| 23 | +Data = namedtuple('Data', ['ss', 'es', 'val']) |
| 24 | + |
| 25 | +''' |
| 26 | +OUTPUT_PYTHON format: |
| 27 | +
|
| 28 | +Packet: |
| 29 | +[<ptype>, <data1>, <data2>] |
| 30 | +
|
| 31 | +<ptype>: |
| 32 | + - 'DATA': <data1> contains the spi data. |
| 33 | + The data is _usually_ 8 bits (but can also be fewer or more bits). |
| 34 | + Both data items are Python numbers (not strings), or None if the respective |
| 35 | + channel was not supplied. |
| 36 | + - 'BITS': <data1>/<data2> contain a list of bit values in this sio0/sio1/sio2/sio3 data |
| 37 | + item, and for each of those also their respective start-/endsample numbers. |
| 38 | + - 'CS-CHANGE': <data1> is the old CS# pin value, <data2> is the new value. |
| 39 | + Both data items are Python numbers (0/1), not strings. At the beginning of |
| 40 | + the decoding a packet is generated with <data1> = None and <data2> being the |
| 41 | + initial state of the CS# pin or None if the chip select pin is not supplied. |
| 42 | + - 'TRANSFER': <data1> contain a list of Data() namedtuples for each |
| 43 | + byte transferred during this block of CS# asserted time. Each Data() has |
| 44 | + fields ss, es, and val. |
| 45 | +
|
| 46 | +Examples: |
| 47 | + ['CS-CHANGE', None, 1] |
| 48 | + ['CS-CHANGE', 1, 0] |
| 49 | + ['DATA', 0xff] |
| 50 | + ['BITS', [[1, 80, 82], [1, 83, 84], [1, 85, 86], [1, 87, 88], |
| 51 | + [1, 89, 90], [1, 91, 92], [1, 93, 94], [1, 95, 96]], |
| 52 | + [[0, 80, 82], [1, 83, 84], [0, 85, 86], [1, 87, 88], |
| 53 | + [1, 89, 90], [1, 91, 92], [0, 93, 94], [0, 95, 96]]] |
| 54 | + ['DATA', 0x65] |
| 55 | + ['DATA', 0xa8] |
| 56 | + ['DATA', 0x55] |
| 57 | + ['CS-CHANGE', 0, 1] |
| 58 | + ['TRANSFER', [Data(ss=80, es=96, val=0xff), ...]] |
| 59 | +''' |
| 60 | + |
| 61 | +# Key: (CPOL, CPHA). Value: SPI mode. |
| 62 | +# Clock polarity (CPOL) = 0/1: Clock is low/high when inactive. |
| 63 | +# Clock phase (CPHA) = 0/1: Data is valid on the leading/trailing clock edge. |
| 64 | +spi_mode = { |
| 65 | + (0, 0): 0, # Mode 0 |
| 66 | + (0, 1): 1, # Mode 1 |
| 67 | + (1, 0): 2, # Mode 2 |
| 68 | + (1, 1): 3, # Mode 3 |
| 69 | +} |
| 70 | + |
| 71 | +ann_spi_data, ann_spi_sio0, ann_spi_sio1, ann_spi_sio2, ann_spi_sio3, ann_spi_other, ann_spi_xfer = range(7) |
| 72 | + |
| 73 | +class ChannelError(Exception): |
| 74 | + pass |
| 75 | + |
| 76 | +class Decoder(srd.Decoder): |
| 77 | + api_version = 3 |
| 78 | + id = 'spi-dual-quad' |
| 79 | + name = 'SPI Dual/Quad' |
| 80 | + longname = 'Dual/Quad Serial Peripheral Interface' |
| 81 | + desc = 'Full-duplex, synchronous, serial bus.' |
| 82 | + license = 'gplv2+' |
| 83 | + inputs = ['logic'] |
| 84 | + outputs = ['spi'] |
| 85 | + tags = ['Embedded/industrial'] |
| 86 | + channels = ( |
| 87 | + {'id': 'clk', 'name': 'CLK', 'desc': 'Clock'}, |
| 88 | + {'id': 'sio0', 'name': 'SIO0', 'desc': 'SPI Input/Output 0'}, |
| 89 | + {'id': 'sio1', 'name': 'SIO1', 'desc': 'SPI Input/Output 1'}, |
| 90 | + ) |
| 91 | + optional_channels = ( |
| 92 | + {'id': 'sio2', 'name': 'SIO2', 'desc': 'SPI Input/Output 2'}, |
| 93 | + {'id': 'sio3', 'name': 'SIO3', 'desc': 'SPI Input/Output 3'}, |
| 94 | + {'id': 'cs', 'name': 'CS#', 'desc': 'Chip-select'}, |
| 95 | + ) |
| 96 | + options = ( |
| 97 | + {'id': 'cs_polarity', 'desc': 'CS# polarity', 'default': 'active-low', |
| 98 | + 'values': ('active-low', 'active-high')}, |
| 99 | + {'id': 'cpol', 'desc': 'Clock polarity', 'default': 0, |
| 100 | + 'values': (0, 1)}, |
| 101 | + {'id': 'cpha', 'desc': 'Clock phase', 'default': 0, |
| 102 | + 'values': (0, 1)}, |
| 103 | + {'id': 'bitorder', 'desc': 'Bit order', |
| 104 | + 'default': 'msb-first', 'values': ('msb-first', 'lsb-first')}, |
| 105 | + {'id': 'wordsize', 'desc': 'Word size', 'default': 8}, |
| 106 | + ) |
| 107 | + annotations = ( |
| 108 | + ('spi-data', 'SPI data'), |
| 109 | + ('sio0-bit', 'SIO0 bit'), |
| 110 | + ('sio1-bit', 'SIO1 bit'), |
| 111 | + ('sio2-bit', 'SIO2 bit'), |
| 112 | + ('sio3-bit', 'SIO3 bit'), |
| 113 | + ('warning', 'Warning'), |
| 114 | + ('spi-transfer', 'SPI transfer'), |
| 115 | + ) |
| 116 | + annotation_rows = ( |
| 117 | + ('sio0-bits', 'SIO0 bits', (ann_spi_sio0,)), |
| 118 | + ('sio1-bits', 'SIO1 bits', (ann_spi_sio1,)), |
| 119 | + ('sio2-bits', 'SIO2 bits', (ann_spi_sio2,)), |
| 120 | + ('sio3-bits', 'SIO3 bits', (ann_spi_sio3,)), |
| 121 | + ('spi-data-vals', 'SPI data', (ann_spi_data,)), |
| 122 | + ('spi-transfers', 'SPI transfers', (ann_spi_xfer,)), |
| 123 | + ('other', 'Other', (ann_spi_other,)), |
| 124 | + ) |
| 125 | + binary = ( |
| 126 | + ('spi-data', 'SPI Data'), |
| 127 | + ) |
| 128 | + |
| 129 | + def __init__(self): |
| 130 | + self.reset() |
| 131 | + |
| 132 | + def reset(self): |
| 133 | + self.samplerate = None |
| 134 | + self.bitcount = 0 |
| 135 | + self.spidata = 0 |
| 136 | + self.sio0bits = [] |
| 137 | + self.sio1bits = [] |
| 138 | + self.sio2bits = [] |
| 139 | + self.sio3bits = [] |
| 140 | + self.spibytes = [] |
| 141 | + self.ss_block = -1 |
| 142 | + self.ss_transfer = -1 |
| 143 | + self.cs_was_deasserted = False |
| 144 | + self.have_cs = None |
| 145 | + self.is_quad = None |
| 146 | + |
| 147 | + def start(self): |
| 148 | + self.out_python = self.register(srd.OUTPUT_PYTHON) |
| 149 | + self.out_ann = self.register(srd.OUTPUT_ANN) |
| 150 | + self.out_binary = self.register(srd.OUTPUT_BINARY) |
| 151 | + self.out_bitrate = self.register(srd.OUTPUT_META, |
| 152 | + meta=(int, 'Bitrate', 'Bitrate during transfers')) |
| 153 | + self.bw = (self.options['wordsize'] + 7) // 8 |
| 154 | + |
| 155 | + def metadata(self, key, value): |
| 156 | + if key == srd.SRD_CONF_SAMPLERATE: |
| 157 | + self.samplerate = value |
| 158 | + |
| 159 | + def putw(self, data): |
| 160 | + self.put(self.ss_block, self.samplenum, self.out_ann, data) |
| 161 | + |
| 162 | + def putdata(self): |
| 163 | + # Pass sio0/sio1/sio2/sio3 bits and then data to the next PD up the stack. |
| 164 | + data = self.spidata |
| 165 | + sio0_bits = self.sio0bits |
| 166 | + sio1_bits = self.sio1bits |
| 167 | + sio2_bits = self.sio2bits |
| 168 | + sio3_bits = self.sio3bits |
| 169 | + |
| 170 | + ss, es = self.sio0bits[-1][1], self.sio0bits[0][2] |
| 171 | + bdata = data.to_bytes(self.bw, byteorder='big') |
| 172 | + self.put(ss, es, self.out_binary, [0, bdata]) |
| 173 | + if self.is_quad: |
| 174 | + self.put(ss, es, self.out_python, ['BITS', sio3_bits, sio2_bits, sio1_bits, sio0_bits]) |
| 175 | + else: |
| 176 | + self.put(ss, es, self.out_python, ['BITS', sio1_bits, sio0_bits]) |
| 177 | + self.put(ss, es, self.out_python, ['DATA', data]) |
| 178 | + |
| 179 | + self.spibytes.append(Data(ss=ss, es=es, val=data)) |
| 180 | + |
| 181 | + # Bit annotations. |
| 182 | + for bit in self.sio0bits: |
| 183 | + self.put(bit[1], bit[2], self.out_ann, [ann_spi_sio0, ['%d' % bit[0]]]) |
| 184 | + for bit in self.sio1bits: |
| 185 | + self.put(bit[1], bit[2], self.out_ann, [ann_spi_sio1, ['%d' % bit[0]]]) |
| 186 | + |
| 187 | + if self.is_quad: |
| 188 | + for bit in self.sio2bits: |
| 189 | + self.put(bit[1], bit[2], self.out_ann, [ann_spi_sio2, ['%d' % bit[0]]]) |
| 190 | + for bit in self.sio3bits: |
| 191 | + self.put(bit[1], bit[2], self.out_ann, [ann_spi_sio3, ['%d' % bit[0]]]) |
| 192 | + |
| 193 | + # Dataword annotations. |
| 194 | + self.put(ss, es, self.out_ann, [ann_spi_data, ['%02X' % self.spidata]]) |
| 195 | + |
| 196 | + def reset_decoder_state(self): |
| 197 | + self.spidata = 0 |
| 198 | + self.sio0bits = [] |
| 199 | + self.sio1bits = [] |
| 200 | + self.sio2bits = [] |
| 201 | + self.sio3bits = [] |
| 202 | + self.bitcount = 0 |
| 203 | + |
| 204 | + def cs_asserted(self, cs): |
| 205 | + active_low = (self.options['cs_polarity'] == 'active-low') |
| 206 | + return (cs == 0) if active_low else (cs == 1) |
| 207 | + |
| 208 | + def handle_bit(self, sio0, sio1, sio2, sio3, clk, cs): |
| 209 | + # If this is the first bit of a dataword, save its sample number. |
| 210 | + if self.bitcount == 0: |
| 211 | + self.ss_block = self.samplenum |
| 212 | + self.cs_was_deasserted = \ |
| 213 | + not self.cs_asserted(cs) if self.have_cs else False |
| 214 | + |
| 215 | + ws = self.options['wordsize'] |
| 216 | + bo = self.options['bitorder'] |
| 217 | + |
| 218 | + if self.is_quad: |
| 219 | + # Quad SPI SIO0 bits 4,0 |
| 220 | + # Quad SPI SIO1 bits 5,1 |
| 221 | + # Quad SPI SIO2 bits 6,2 |
| 222 | + # Quad SPI SIO3 bits 7,3 |
| 223 | + if bo == 'msb-first': |
| 224 | + self.spidata |= sio3 << (ws - 1 - self.bitcount) |
| 225 | + self.spidata |= sio2 << (ws - 1 - self.bitcount - 1) |
| 226 | + self.spidata |= sio1 << (ws - 1 - self.bitcount - 2) |
| 227 | + self.spidata |= sio0 << (ws - 1 - self.bitcount - 3) |
| 228 | + else: |
| 229 | + self.spidata |= sio3 << self.bitcount |
| 230 | + self.spidata |= sio2 << (self.bitcount + 1) |
| 231 | + self.spidata |= sio1 << (self.bitcount + 2) |
| 232 | + self.spidata |= sio0 << (self.bitcount + 3) |
| 233 | + else: |
| 234 | + # Dual SPI SIO0 bits 6,4,2,0 |
| 235 | + # Dual SPI SIO1 bits 7,5,3,1 |
| 236 | + if bo == 'msb-first': |
| 237 | + self.spidata |= sio1 << (ws - 1 - self.bitcount) |
| 238 | + self.spidata |= sio0 << (ws - 1 - self.bitcount - 1) |
| 239 | + else: |
| 240 | + self.spidata |= sio1 << self.bitcount |
| 241 | + self.spidata |= sio0 << (self.bitcount + 1) |
| 242 | + |
| 243 | + # Guesstimate the endsample for this bit (can be overridden below). |
| 244 | + es = self.samplenum |
| 245 | + if self.bitcount > 0: |
| 246 | + es += self.samplenum - self.sio0bits[0][1] |
| 247 | + |
| 248 | + self.sio0bits.insert(0, [sio0, self.samplenum, es]) |
| 249 | + self.sio1bits.insert(0, [sio1, self.samplenum, es]) |
| 250 | + if self.is_quad: |
| 251 | + self.sio2bits.insert(0, [sio2, self.samplenum, es]) |
| 252 | + self.sio3bits.insert(0, [sio3, self.samplenum, es]) |
| 253 | + |
| 254 | + if self.bitcount > 0: |
| 255 | + self.sio0bits[1][2] = self.samplenum |
| 256 | + self.sio1bits[1][2] = self.samplenum |
| 257 | + if self.is_quad: |
| 258 | + self.sio2bits[1][2] = self.samplenum |
| 259 | + self.sio3bits[1][2] = self.samplenum |
| 260 | + |
| 261 | + if self.is_quad: |
| 262 | + self.bitcount += 4 |
| 263 | + else: |
| 264 | + self.bitcount += 2 |
| 265 | + |
| 266 | + # Continue to receive if not enough bits were received, yet. |
| 267 | + if self.bitcount != ws: |
| 268 | + return |
| 269 | + |
| 270 | + self.putdata() |
| 271 | + |
| 272 | + # Meta bitrate. |
| 273 | + if self.samplerate: |
| 274 | + elapsed = 1 / float(self.samplerate) |
| 275 | + elapsed *= (self.samplenum - self.ss_block + 1) |
| 276 | + bitrate = int(1 / elapsed * ws) |
| 277 | + self.put(self.ss_block, self.samplenum, self.out_bitrate, bitrate) |
| 278 | + |
| 279 | + if self.have_cs and self.cs_was_deasserted: |
| 280 | + self.putw([ann_spi_other, ['CS# was deasserted during this data word!']]) |
| 281 | + |
| 282 | + self.reset_decoder_state() |
| 283 | + |
| 284 | + def find_clk_edge(self, sio0, sio1, sio2, sio3, clk, cs, first): |
| 285 | + if self.have_cs and (first or self.matched[self.have_cs]): |
| 286 | + # Send all CS# pin value changes. |
| 287 | + oldcs = None if first else 1 - cs |
| 288 | + self.put(self.samplenum, self.samplenum, self.out_python, |
| 289 | + ['CS-CHANGE', oldcs, cs]) |
| 290 | + |
| 291 | + if self.cs_asserted(cs): |
| 292 | + self.ss_transfer = self.samplenum |
| 293 | + self.spibytes = [] |
| 294 | + elif self.ss_transfer != -1: |
| 295 | + self.put(self.ss_transfer, self.samplenum, self.out_ann, |
| 296 | + [ann_spi_xfer, [' '.join(format(x.val, '02X') for x in self.spibytes)]]) |
| 297 | + self.put(self.ss_transfer, self.samplenum, self.out_python, |
| 298 | + ['TRANSFER', self.spibytes]) |
| 299 | + |
| 300 | + # Reset decoder state when CS# changes (and the CS# pin is used). |
| 301 | + self.reset_decoder_state() |
| 302 | + |
| 303 | + # We only care about samples if CS# is asserted. |
| 304 | + if self.have_cs and not self.cs_asserted(cs): |
| 305 | + return |
| 306 | + |
| 307 | + # Ignore sample if the clock pin hasn't changed. |
| 308 | + if first or not self.matched[0]: |
| 309 | + return |
| 310 | + |
| 311 | + # Sample data on rising/falling clock edge (depends on mode). |
| 312 | + mode = spi_mode[self.options['cpol'], self.options['cpha']] |
| 313 | + if mode == 0 and clk == 0: # Sample on rising clock edge |
| 314 | + return |
| 315 | + elif mode == 1 and clk == 1: # Sample on falling clock edge |
| 316 | + return |
| 317 | + elif mode == 2 and clk == 1: # Sample on falling clock edge |
| 318 | + return |
| 319 | + elif mode == 3 and clk == 0: # Sample on rising clock edge |
| 320 | + return |
| 321 | + |
| 322 | + # Found the correct clock edge, now get the SPI bit(s). |
| 323 | + self.handle_bit(sio0, sio1, sio2, sio3, clk, cs) |
| 324 | + |
| 325 | + def decode(self): |
| 326 | + # The CLK input is mandatory. SIO0 and SIO1 are mandatory for Dual SPI. |
| 327 | + if not self.has_channel(0) and not self.has_channel(1) and not self.has_channel(2): |
| 328 | + raise ChannelError('For Dual SPI SIO0 and SIO1 are pins required.') |
| 329 | + # The CLK input is mandatory. SIO3 and SIO4 are mandatory for Quad SPI. |
| 330 | + if (self.has_channel(3) and not self.has_channel(4)) or (self.has_channel(4) and not self.has_channel(3)): |
| 331 | + raise ChannelError('For Quad SPI SIO2 and SIO3 are pins required.') |
| 332 | + |
| 333 | + # Mark that SPI is Quad |
| 334 | + self.is_quad = self.has_channel(3) and self.has_channel(4) |
| 335 | + self.have_cs = self.has_channel(5) |
| 336 | + if not self.have_cs: |
| 337 | + self.put(0, 0, self.out_python, ['CS-CHANGE', None, None]) |
| 338 | + |
| 339 | + # We want all CLK changes. We want all CS changes if CS is used. |
| 340 | + # Map 'have_cs' from boolean to an integer index. This simplifies |
| 341 | + # evaluation in other locations. |
| 342 | + wait_cond = [{0: 'e'}] |
| 343 | + if self.have_cs: |
| 344 | + self.have_cs = len(wait_cond) |
| 345 | + wait_cond.append({5: 'e'}) |
| 346 | + |
| 347 | + # "Pixel compatibility" with the v2 implementation. Grab and |
| 348 | + # process the very first sample before checking for edges. The |
| 349 | + # previous implementation did this by seeding old values with |
| 350 | + # None, which led to an immediate "change" in comparison. |
| 351 | + (clk, sio0, sio1, sio2, sio3, cs) = self.wait({}) |
| 352 | + self.find_clk_edge(sio0, sio1, sio2, sio3, clk, cs, True) |
| 353 | + |
| 354 | + while True: |
| 355 | + (clk, sio0, sio1, sio2, sio3, cs) = self.wait(wait_cond) |
| 356 | + self.find_clk_edge(sio0, sio1, sio2, sio3, clk, cs, False) |
0 commit comments