summaryrefslogtreecommitdiff
path: root/decoders/sda2506/pd.py
blob: 986312b34ae1f96baff0b256c968265782a4e359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2018 Max Weller
##
## This program is free software; you can redistribute it and/or modify
## it under the terms of the GNU General Public License as published by
## the Free Software Foundation; either version 2 of the License, or
## (at your option) any later version.
##
## This program is distributed in the hope that it will be useful,
## but WITHOUT ANY WARRANTY; without even the implied warranty of
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
## GNU General Public License for more details.
##
## You should have received a copy of the GNU General Public License
## along with this program; if not, see <http://www.gnu.org/licenses/>.
##

import re
import sigrokdecode as srd

ann_cmdbit, ann_databit, ann_cmd, ann_data, ann_warning = range(5)

class Decoder(srd.Decoder):
    api_version = 3
    id = 'sda2506'
    name = 'SDA2506'
    longname = 'Siemens SDA 2506-5'
    desc = 'Serial nonvolatile 1-Kbit EEPROM.'
    license = 'gplv2+'
    inputs = ['logic']
    outputs = ['sda2506']
    tags = ['IC', 'Memory']
    channels = (
        {'id': 'clk', 'name': 'CLK', 'desc': 'Clock'},
        {'id': 'd', 'name': 'DATA', 'desc': 'Data'},
        {'id': 'ce', 'name': 'CE#', 'desc': 'Chip-enable'},
    )
    annotations = (
        ('cmdbit', 'Command bit'),
        ('databit', 'Data bit'),
        ('cmd', 'Command'),
        ('data', 'Data byte'),
        ('warnings', 'Human-readable warnings'),
    )
    annotation_rows = (
        ('bits', 'Bits', (ann_cmdbit, ann_databit)),
        ('commands', 'Commands', (ann_cmd,)),
        ('data', 'Data', (ann_data,)),
        ('warnings', 'Warnings', (ann_warning,)),
    )

    def __init__(self):
        self.samplerate = None
        self.reset()

    def reset(self):
        self.cmdbits = []
        self.databits = []

    def metadata(self, key, value):
        if key == srd.SRD_CONF_SAMPLERATE:
            self.samplerate = value

    def start(self):
        self.out_ann = self.register(srd.OUTPUT_ANN)

    def putbit(self, ss, es, typ, value):
        self.put(ss, es, self.out_ann, [typ, ['%s' % (value)]])

    def putdata(self, ss, es):
        value = 0
        for i in range(8):
            value = (value << 1) | self.databits[i]
        self.put(ss, es, self.out_ann, [ann_data, ['%02X' % (value)]])

    def decode_bits(self, offset, width):
        out = 0
        for i in range(width):
            out = (out << 1) | self.cmdbits[offset + i][0]
        return (out, self.cmdbits[offset + width - 1][1], self.cmdbits[offset][2])

    def decode_field(self, name, offset, width):
        val, ss, es = self.decode_bits(offset, width)
        self.put(ss, es, self.out_ann, [ann_data, ['%s: %02X' % (name, val)]])
        return val

    def decode(self):
        while True:
            # Wait for CLK edge or CE edge.
            clk, d, ce = self.wait([{0: 'e'}, {2: 'e'}])

            if self.matched[0] and ce == 1 and clk == 1:
                # Rising clk edge and command mode.
                bitstart = self.samplenum
                self.wait({0: 'f'})
                self.cmdbits = [(d, bitstart, self.samplenum)] + self.cmdbits
                if len(self.cmdbits) > 24:
                    self.cmdbits = self.cmdbits[0:24]
                self.putbit(bitstart, self.samplenum, ann_cmdbit, d)
            elif self.matched[0] and ce == 0 and clk == 0:
                # Falling clk edge and data mode.
                bitstart = self.samplenum
                clk, d, ce = self.wait([{'skip': int(2.5 * (1e6 / self.samplerate))}, {0: 'r'}, {2: 'e'}]) # Wait 25 us for data ready.
                if self.matched == (True, False, False):
                    self.wait([{0: 'r'}, {2: 'e'}])
                if len(self.databits) == 0:
                    self.datastart = bitstart
                self.databits = [d] + self.databits
                self.putbit(bitstart, self.samplenum, ann_databit, d)
                if len(self.databits) == 8:
                    self.putdata(self.datastart, self.samplenum)
                    self.databits = []
            elif self.matched[1] and ce == 0:
                # Chip enable edge.
                try:
                    self.decode_field('addr', 1, 7)
                    self.decode_field('CB', 0, 1)
                    if self.cmdbits[0][0] == 0:
                        # Beginning read command.
                        self.decode_field('read', 1, 7)
                        self.put(self.cmdbits[7][1], self.samplenum,
                            self.out_ann, [ann_cmd, ['read' ]])
                    elif d == 0:
                        # Beginning write command.
                        self.decode_field('data', 8, 8)
                        addr, ss, es = self.decode_bits(1, 7)
                        data, ss, es = self.decode_bits(8, 8)
                        cmdstart = self.samplenum
                        self.wait({2: 'r'})
                        self.put(cmdstart, self.samplenum, self.out_ann,
                            [ann_cmd, ['Write to %02X: %02X' % (addr, data)]])
                    else:
                        # Beginning erase command.
                        val, ss, es = self.decode_bits(1, 7)
                        cmdstart = self.samplenum
                        self.wait({2: 'r'})
                        self.put(cmdstart, self.samplenum, self.out_ann,
                            [ann_cmd, ['Erase: %02X' % (val)]])
                    self.databits = []
                except Exception as ex:
                    self.reset()