2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2017 Kevin Redon <kingkevin@cuvoodoo.info>
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.
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.
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/>.
20 import sigrokdecode as srd
26 [{'ss': bit start sample number,
27 'se': bit end sample number,
32 Since address and word size are variable, a list of all bits in each packet
33 need to be output. Since Microwire is a synchronous protocol with separate
34 input and output lines (SI and SO) they are provided together, but because
35 Microwire is half-duplex only the SI or SO bits will be considered at once.
36 To be able to annotate correctly the instructions formed by the bit, the start
37 and end sample number of each bit (pair of SI/SO bit) are provided.
40 class Decoder(srd.Decoder):
44 longname = 'Microwire'
45 desc = '3-wire, half-duplex, synchronous serial bus.'
48 outputs = ['microwire']
50 {'id': 'cs', 'name': 'CS', 'desc': 'Chip select'},
51 {'id': 'sk', 'name': 'SK', 'desc': 'Clock'},
52 {'id': 'si', 'name': 'SI', 'desc': 'Slave in'},
53 {'id': 'so', 'name': 'SO', 'desc': 'Slave out'},
58 ('status-check', 'Status check'),
59 ('warning', 'Warning'),
62 ('si-bits', 'SI bits', (0,)),
63 ('so-bits', 'SO bits', (1,)),
64 ('status', 'Status', (2,)),
65 ('warnings', 'Warnings', (3,)),
69 self.out_python = self.register(srd.OUTPUT_PYTHON)
70 self.out_ann = self.register(srd.OUTPUT_ANN)
74 # Wait for slave to be selected on rising CS.
75 cs, sk, si, so = self.wait({0: 'r'})
77 self.put(self.samplenum, self.samplenum, self.out_ann,
78 [3, ['Clock should be low on start',
79 'Clock high on start', 'Clock high', 'SK high']])
80 sk = 0 # Enforce correct state for correct clock handling.
81 # Because we don't know if this is bit communication or a
82 # status check we have to collect the SI and SO values on SK
83 # edges while the chip is selected and figure out afterwards.
87 packet.append({'samplenum': self.samplenum,
88 'matched': self.matched,
89 'cs': cs, 'sk': sk, 'si': si, 'so': so})
91 cs, sk, si, so = self.wait([{0: 'l'}, {1: 'r'}, {3: 'e'}])
93 cs, sk, si, so = self.wait([{0: 'l'}, {1: 'f'}, {3: 'e'}])
95 packet.append({'samplenum': self.samplenum,
96 'matched': self.matched,
97 'cs': cs, 'sk': sk, 'si': si, 'so': so})
99 # Figure out if this is a status check.
100 # Either there is no clock or no start bit (on first rising edge).
102 for change in packet:
103 # Get first clock rising edge.
104 if len(change['matched']) > 1 and change['matched'][1] \
110 # The packet is for a status check.
111 # SO low = busy, SO high = ready.
112 # The SO signal might be noisy in the beginning because it starts
115 start_samplenum = packet[0]['samplenum']
116 bit_so = packet[0]['so']
117 # Check for SO edges.
118 for change in packet:
119 if len(change['matched']) > 2 and change['matched'][2]:
120 if bit_so == 0 and change['so']:
121 # Rising edge Busy -> Ready.
122 self.put(start_samplenum, change['samplenum'],
123 self.out_ann, [2, ['Busy', 'B']])
124 start_samplenum = change['samplenum']
125 bit_so = change['so']
128 self.put(start_samplenum, packet[-1]['samplenum'],
129 self.out_ann, [2, ['Busy', 'B']])
131 self.put(start_samplenum, packet[-1]['samplenum'],
132 self.out_ann, [2, ['Ready', 'R']])
135 # Since the slave samples SI on clock rising edge we do the
136 # same. Because the slave changes SO on clock rising edge we
137 # sample on the falling edge.
138 bit_start = 0 # Rising clock sample of bit start.
139 bit_si = 0 # SI value at rising clock edge.
140 bit_so = 0 # SO value at falling clock edge.
141 start_bit = True # Start bit incoming (first bit).
142 python_output = [] # Python output data.
143 for change in packet:
144 if len(change['matched']) > 1 and change['matched'][1]:
146 if change['sk']: # Rising clock edge.
147 if bit_start > 0: # Bit completed.
149 if bit_si == 0: # Start bit missing.
150 self.put(bit_start, change['samplenum'],
152 [3, ['Start bit not high',
155 self.put(bit_start, change['samplenum'],
157 [0, ['Start bit', 'S']])
160 self.put(bit_start, change['samplenum'],
162 [0, ['SI bit: %d' % bit_si,
165 self.put(bit_start, change['samplenum'],
167 [1, ['SO bit: %d' % bit_so,
170 python_output.append({'ss': bit_start,
171 'se': change['samplenum'],
172 'si': bit_si, 'so': bit_so})
173 bit_start = change['samplenum']
174 bit_si = change['si']
175 else: # Falling clock edge.
176 bit_so = change['so']
177 elif change['matched'][0] and \
178 change['cs'] == 0 and change['sk'] == 0:
180 self.put(bit_start, change['samplenum'], self.out_ann,
181 [0, ['SI bit: %d' % bit_si,
182 'SI: %d' % bit_si, '%d' % bit_si]])
183 self.put(bit_start, change['samplenum'], self.out_ann,
184 [1, ['SO bit: %d' % bit_so,
185 'SO: %d' % bit_so, '%d' % bit_so]])
186 python_output.append({'ss': bit_start,
187 'se': change['samplenum'],
188 'si': bit_si, 'so': bit_so})
189 self.put(packet[0]['samplenum'],
190 packet[len(packet) - 1]['samplenum'],
191 self.out_python, python_output)