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
22 class Decoder(srd.Decoder):
26 longname = '93Cxx EEPROM'
27 desc = '93Cxx series Microwire EEPROM protocol.'
29 inputs = ['microwire']
30 outputs = ['eeprom93cxx']
32 {'id': 'addresssize', 'desc': 'Address size', 'default': 8},
33 {'id': 'wordsize', 'desc': 'Word size', 'default': 16},
36 ('si-data', 'SI data'),
37 ('so-data', 'SO data'),
38 ('warning', 'Warning'),
41 ('data', 'Data', (0, 1)),
42 ('warnings', 'Warnings', (2,)),
49 self.out_ann = self.register(srd.OUTPUT_ANN)
50 self.addresssize = self.options['addresssize']
51 self.wordsize = self.options['wordsize']
53 def put_address(self, data):
54 # Get address (MSb first).
56 for b in range(len(data)):
57 a += (data[b]['si'] << (len(data) - b - 1))
58 self.put(data[0]['ss'], data[-1]['se'], self.out_ann,
59 [0, ['Address: 0x%x' % a, 'Addr: 0x%x' % a, '0x%x' % a]])
61 def put_word(self, si, data):
62 # Decode word (MSb first).
64 for b in range(len(data)):
66 word += (data[b]['si'] << (len(data) - b - 1))
68 word += (data[b]['so'] << (len(data) - b - 1))
70 self.put(data[0]['ss'], data[-1]['se'],
71 self.out_ann, [0, ['Data: 0x%x' % word, '0x%x' % word]])
73 self.put(data[0]['ss'], data[-1]['se'],
74 self.out_ann, [1, ['Data: 0x%x' % word, '0x%x' % word]])
76 def decode(self, ss, es, data):
77 if len(data) < (2 + self.addresssize):
78 self.put(ss, es, self.out_ann, [2, ['Not enough packet bits']])
81 opcode = (data[0]['si'] << 1) + (data[1]['si'] << 0)
85 self.put(data[0]['ss'], data[1]['se'],
86 self.out_ann, [0, ['Read word', 'READ']])
87 self.put_address(data[2:2 + self.addresssize])
90 word_start = 2 + self.addresssize
91 while len(data) - word_start > 0:
92 # Check if there are enough bits for a word.
93 if len(data) - word_start < self.wordsize:
94 self.put(data[word_start]['ss'], data[len(data) - 1]['se'],
95 self.out_ann, [2, ['Not enough word bits']])
97 self.put_word(False, data[word_start:word_start + self.wordsize])
99 word_start += self.wordsize
102 self.put(data[0]['ss'], data[1]['se'],
103 self.out_ann, [0, ['Write word', 'WRITE']])
104 self.put_address(data[2:2 + self.addresssize])
106 if len(data) < 2 + self.addresssize + self.wordsize:
107 self.put(data[2 + self.addresssize]['ss'],
108 data[len(data) - 1]['ss'],
109 self.out_ann, [2, ['Not enough word bits']])
111 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])
114 self.put(data[0]['ss'], data[1]['se'],
115 self.out_ann, [0, ['Erase word', 'ERASE']])
116 self.put_address(data[2:2 + self.addresssize])
118 if data[2]['si'] == 1 and data[3]['si'] == 1:
120 self.put(data[0]['ss'], data[2 + self.addresssize - 1]['se'],
121 self.out_ann, [0, ['Write enable', 'WEN']])
122 elif data[2]['si'] == 0 and data[3]['si'] == 0:
124 self.put(data[0]['ss'], data[2 + self.addresssize - 1]['se'],
125 self.out_ann, [0, ['Write disable', 'WDS']])
126 elif data[2]['si'] == 1 and data[3]['si'] == 0:
128 self.put(data[0]['ss'], data[2 + self.addresssize - 1]['se'],
129 self.out_ann, [0, ['Erase all memory',
130 'Erase all', 'ERAL']])
131 elif data[2]['si'] == 0 and data[3]['si'] == 1:
133 self.put(data[0]['ss'], data[2 + self.addresssize - 1]['se'],
134 self.out_ann, [0, ['Write all memory',
135 'Write all', 'WRAL']])
137 if len(data) < 2 + self.addresssize + self.wordsize:
138 self.put(data[2 + self.addresssize]['ss'],
139 data[len(data) - 1]['ss'],
140 self.out_ann, [2, ['Not enough word bits']])
142 self.put_word(True, data[2 + self.addresssize:2 + self.addresssize + self.wordsize])