]> sigrok.org Git - libsigrokdecode.git/blame - decoders/x2444m/pd.py
x2444m: Make each command an extra annotation class.
[libsigrokdecode.git] / decoders / x2444m / pd.py
CommitLineData
c40477de
SP
1##
2## This file is part of the libsigrokdecode project.
3##
4## Copyright (C) 2018 Stefan Petersen <spe@ciellt.se>
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
20import re
21import sigrokdecode as srd
22
23registers = {
15a60d37
UH
24 0x80: ['WRDS', 0, lambda _: ''],
25 0x81: ['STO', 1, lambda _: ''],
26 0x82: ['SLEEP', 2, lambda _: ''],
27 0x83: ['WRITE', 3, lambda v: '0x%x' % v],
28 0x84: ['WREN', 4, lambda _: ''],
29 0x85: ['RCL', 5, lambda _: ''],
30 0x86: ['READ', 6, lambda v: '0x%x' % v],
31 0x87: ['READ', 7, lambda v: '0x%x' % v],
c40477de
SP
32}
33
c40477de
SP
34class Decoder(srd.Decoder):
35 api_version = 3
36 id = 'x2444m'
37 name = 'X2444M/P'
38 longname = 'Xicor X2444M/P'
39 desc = 'Xicor X2444M/P nonvolatile static RAM protocol.'
40 license = 'gplv2+'
41 inputs = ['spi']
42 outputs = ['x2444m']
43 annotations = (
15a60d37
UH
44 ('wrds', 'Write disable'),
45 ('sto', 'Store RAM data in EEPROM'),
46 ('sleep', 'Enter sleep mode'),
47 ('write', 'Write data into RAM'),
48 ('wren', 'Write enable'),
49 ('rcl', 'Recall EEPROM data into RAM'),
50 ('read', 'Data read from RAM'),
51 ('read', 'Data read from RAM'),
c40477de
SP
52 )
53
54 def __init__(self):
55 self.reset()
56
57 def reset(self):
58 pass
59
60 def start(self):
61 self.out_ann = self.register(srd.OUTPUT_ANN)
62 self.cs_start = 0
63 self.cs_asserted = False
64 self.cmd_digit = 0
65
15a60d37 66 def putreadwrite(self, ss, es, reg, idx, addr, value):
c40477de 67 self.put(ss, es, self.out_ann,
15a60d37 68 [idx, ['%s: %s => 0x%4.4x' % (reg, addr, value)]])
c40477de 69
15a60d37
UH
70 def putcmd(self, ss, es, reg, idx):
71 self.put(ss, es, self.out_ann, [idx, ['%s' % reg]])
c40477de
SP
72
73 def decode(self, ss, es, data):
74 ptype, mosi, miso = data
75
76 if ptype == 'DATA':
77 if not self.cs_asserted:
78 return
79
80 if self.cmd_digit == 0:
81 self.addr = mosi
82 self.addr_start = ss
83 elif self.cmd_digit > 0:
84 self.read_value = (self.read_value << 8) + miso
85 self.write_value = (self.write_value << 8) + mosi
86 self.cmd_digit += 1
87 elif ptype == 'CS-CHANGE':
88 self.cs_asserted = (miso == 1)
89 # When not asserted, CS has just changed from asserted to deasserted.
90 if not self.cs_asserted:
91 # Only one digit, simple command. Else read/write.
92 if self.cmd_digit == 1:
15a60d37
UH
93 name, idx, decoder = registers[self.addr & 0x87]
94 self.putcmd(self.addr_start, es, name, idx)
c40477de 95 elif self.cmd_digit > 1:
15a60d37 96 name, idx, decoder = registers[self.addr & 0x87]
c40477de
SP
97 if name == 'READ':
98 value = self.read_value
99 elif name == 'WRITE':
100 value = self.write_value
101 else:
102 value = 0
15a60d37 103 self.putreadwrite(self.addr_start, es, name, idx,
c40477de
SP
104 decoder((self.addr >> 3) & 0x0f), value)
105
106 if self.cs_asserted:
107 self.cs_start = ss
108 self.cmd_digit = 0
109 self.read_value = 0
110 self.write_value = 0