]> sigrok.org Git - libsigrokdecode.git/blob - decoders/maxim_ds28ea00/maxim_ds28ea00.py
srd: Rename onewire_transport to maxim_ds28ea00.
[libsigrokdecode.git] / decoders / maxim_ds28ea00 / maxim_ds28ea00.py
1 ##
2 ## This file is part of the sigrok project.
3 ##
4 ## Copyright (C) 2012 Iztok Jeras <iztok.jeras@gmail.com>
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, write to the Free Software
18 ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
19 ##
20
21 # 1-Wire protocol decoder
22
23 import sigrokdecode as srd
24
25 # a dictionary of FUNCTION commands and their names
26 command = {
27     # scratchpad
28     0x4e: "WRITE SCRATCHPAD",
29     0xbe: "READ SCRATCHPAD",
30     0x48: "COPY SCRATCHPAD",
31     # thermometer
32     0x44: "CONVERT TEMPERATURE",
33     0xb4: "READ POWER MODE",
34     0xb8: "RECALL EEPROM",
35     0xf5: "PIO ACCESS READ",
36     0xA5: "PIO ACCESS WRITE",
37     0x99: "CHAIN",
38     # memory
39     0xf0: "READ MEMORY",
40     0xa5: "EXTENDED READ MEMORY",
41     0x0f: "WRITE MEMORY",
42     0x55: "WRITE STATUS",
43     0xaa: "READ STATUS",
44     0xf5: "CHANNEL ACCESS"
45 }
46
47 class Decoder(srd.Decoder):
48     api_version = 1
49     id = 'maxim_ds28ea00'
50     name = '1-Wire transport layer'
51     longname = '1-Wire serial communication bus'
52     desc = 'Bidirectional, half-duplex, asynchronous serial bus.'
53     license = 'gplv2+'
54     inputs = ['onewire_network']
55     outputs = []
56     probes = []
57     optional_probes = []
58     options = {}
59     annotations = [
60         ['Transport', 'Transport layer events'],
61     ]
62
63     def __init__(self, **kwargs):
64         # Event timing variables
65         self.trn_beg = 0
66         self.trn_end = 0
67         # Transport layer variables
68         self.state   = 'ROM'
69         self.rom     = 0x0000000000000000
70
71     def start(self, metadata):
72         self.out_ann   = self.add(srd.OUTPUT_ANN  , 'onewire_transport')
73
74     def report(self):
75         pass
76
77     def decode(self, ss, es, data):
78         [code, val] = data
79
80         # State machine.
81         if (code == "RESET/PRESENCE"):
82             self.put(ss, es, self.out_ann, [0, ['RESET/PRESENCE: %s' % ('True' if val else 'False')]])
83             self.state = "ROM"
84         elif (code == "ROM"):
85             self.rom = val
86             self.put(ss, es, self.out_ann, [0, ['ROM: 0x%016x' % (val)]])
87             self.state = "COMMAND"
88         elif (code == "DATA"):
89             if (self.state == "COMMAND"):
90                     if (val in command):
91                         self.put(ss, es, self.out_ann, [0, ['FUNCTION COMMAND: 0x%02x \'%s\'' % (val, command[val])]])
92                         self.state = command[val]
93                     else:
94                         self.put(ss, es, self.out_ann, [0, ['FUNCTION COMMAND: 0x%02x \'%s\'' % (val, 'UNRECOGNIZED')]])
95                         self.state = "UNRECOGNIZED"
96             elif (self.state == "READ SCRATCHPAD"):
97                 self.put(ss, es, self.out_ann, [0, ['SCRATCHPAD DATA: 0x%02x' % (val)]])
98             elif (self.state == "CONVERT TEMPERATURE"):
99                 self.put(ss, es, self.out_ann, [0, ['TEMPERATURE CONVERSION STATUS: 0x%02x' % (val)]])
100             elif (self.state in command.values()):
101                 self.put(ss, es, self.out_ann, [0, ['TODO "%s": 0x%02x' % (self.state, val)]])
102             elif (self.state == "UNRECOGNIZED"):
103                 self.put(ss, es, self.out_ann, [0, ['UNRECOGNIZED COMMAND: 0x%02x' % (val)]])
104             else:
105                 raise Exception('Invalid state: %s' % self.state)