summaryrefslogtreecommitdiff
path: root/decoders/ds2408/pd.py
blob: 33f2873fe9e092f276140cf501d85cf917a0cbec (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
##
## This file is part of the libsigrokdecode project.
##
## Copyright (C) 2019 Mariusz Bialonczyk <manio@skyboo.net>
##
## 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 sigrokdecode as srd

# Dictionary of FUNCTION commands and their names.
command = {
    0xf0: 'Read PIO Registers',
    0xf5: 'Channel Access Read',
    0x5a: 'Channel Access Write',
    0xcc: 'Write Conditional Search Register',
    0xc3: 'Reset Activity Latches',
    0x3c: 'Disable Test Mode',
}

class Decoder(srd.Decoder):
    api_version = 3
    id = 'ds2408'
    name = 'DS2408'
    longname = 'Maxim DS2408'
    desc = '1-Wire 8-channel addressable switch.'
    license = 'gplv2+'
    inputs = ['onewire_network']
    outputs = []
    tags = ['Embedded/industrial', 'IC']
    annotations = (
        ('text', 'Human-readable text'),
    )

    def __init__(self):
        self.reset()

    def reset(self):
        # Bytes for function command.
        self.bytes = []

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

    def putx(self, data):
        self.put(self.ss, self.es, self.out_ann, data)

    def decode(self, ss, es, data):
        code, val = data

        if code == 'RESET/PRESENCE':
            self.ss, self.es = ss, es
            self.putx([0, ['Reset/presence: %s'
                           % ('true' if val else 'false')]])
            self.bytes = []
        elif code == 'ROM':
            self.ss, self.es = ss, es
            family_code = val & 0xff
            self.putx([0, ['ROM: 0x%016x (family code 0x%02x)' % (val, family_code)]])
            self.bytes = []
        elif code == 'DATA':
            self.bytes.append(val)
            if 1 == len(self.bytes):
                self.ss, self.es = ss, es
                if val not in command:
                    self.putx([0, ['Unrecognized command: 0x%02x' % val]])
                else:
                    self.putx([0, ['%s (0x%02x)' % (command[val], val)]])
            elif 0xf0 == self.bytes[0]: # Read PIO Registers
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 3 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Target address: 0x%04x'
                                   % ((self.bytes[2] << 8) + self.bytes[1])]])
                elif 3 < len(self.bytes):
                    self.ss, self.es = ss, es
                    self.putx([0, ['Data: 0x%02x' % self.bytes[-1]]])
            elif 0xf5 == self.bytes[0]: # Channel Access Read
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 2 < len(self.bytes):
                    self.ss, self.es = ss, es
                    self.putx([0, ['PIO sample: 0x%02x' % self.bytes[-1]]])
            elif 0x5a == self.bytes[0]: # Channel Access Write
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 3 == len(self.bytes):
                    self.es = es
                    if (self.bytes[-1] == (self.bytes[-2] ^ 0xff)):
                      self.putx([0, ['Data: 0x%02x (bit-inversion correct: 0x%02x)' % (self.bytes[-2], self.bytes[-1])]])
                    else:
                      self.putx([0, ['Data error: second byte (0x%02x) is not bit-inverse of first (0x%02x)' % (self.bytes[-1], self.bytes[-2])]])
                elif 3 < len(self.bytes):
                    self.ss, self.es = ss, es
                    if 0xaa == self.bytes[-1]:
                      self.putx([0, ['Success']])
                    elif 0xff == self.bytes[-1]:
                      self.putx([0, ['Fail New State']])
            elif 0xcc == self.bytes[0]: # Write Conditional Search Register
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 3 == len(self.bytes):
                    self.es = es
                    self.putx([0, ['Target address: 0x%04x'
                                   % ((self.bytes[2] << 8) + self.bytes[1])]])
                elif 3 < len(self.bytes):
                    self.ss, self.es = ss, es
                    self.putx([0, ['Data: 0x%02x' % self.bytes[-1]]])
            elif 0xc3 == self.bytes[0]: # Reset Activity Latches
                if 2 == len(self.bytes):
                    self.ss = ss
                elif 2 < len(self.bytes):
                    self.ss, self.es = ss, es
                    if 0xaa == self.bytes[-1]:
                      self.putx([0, ['Success']])
                    else:
                      self.putx([0, ['Invalid byte']])