]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ade77xx/pd.py
decoders: Fix incorrect 'outputs' fields.
[libsigrokdecode.git] / decoders / ade77xx / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2017 Karl Palsson <karlp@etactica.com>
5 ##
6 ## Permission is hereby granted, free of charge, to any person obtaining a copy
7 ## of this software and associated documentation files (the "Software"), to deal
8 ## in the Software without restriction, including without limitation the rights
9 ## to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 ## copies of the Software, and to permit persons to whom the Software is
11 ## furnished to do so, subject to the following conditions:
12 ##
13 ## The above copyright notice and this permission notice shall be included in all
14 ## copies or substantial portions of the Software.
15 ##
16 ## THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 ## IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 ## FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 ## AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 ## LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 ## OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 ## SOFTWARE.
23
24 import math
25 import sigrokdecode as srd
26 from .lists import *
27
28 class Decoder(srd.Decoder):
29     api_version = 3
30     id = 'ade77xx'
31     name = 'ADE77xx'
32     longname = 'Analog Devices ADE77xx'
33     desc = 'Poly phase multifunction energy metering IC protocol.'
34     license = 'mit'
35     inputs = ['spi']
36     outputs = []
37     tags = ['Analog/digital', 'IC', 'Sensor']
38     annotations = (
39         ('read', 'Register read commands'),
40         ('write', 'Register write commands'),
41         ('warning', 'Warnings'),
42     )
43     annotation_rows = (
44         ('read', 'Read', (0,)),
45         ('write', 'Write', (1,)),
46         ('warnings', 'Warnings', (2,)),
47     )
48
49     def reset_data(self):
50         self.expected = 0
51         self.mosi_bytes, self.miso_bytes = [], []
52
53     def __init__(self):
54         self.reset()
55
56     def reset(self):
57         self.ss_cmd, self.es_cmd = 0, 0
58         self.reset_data()
59
60     def start(self):
61         self.out_ann = self.register(srd.OUTPUT_ANN)
62
63     def putx(self, data):
64         self.put(self.ss_cmd, self.es_cmd, self.out_ann, data)
65
66     def put_warn(self, pos, msg):
67         self.put(pos[0], pos[1], self.out_ann, [2, [msg]])
68
69     def decode(self, ss, es, data):
70         ptype = data[0]
71         if ptype == 'CS-CHANGE':
72             # Bear in mind, that CS is optional according to the datasheet.
73             # If we transition high mid-stream, toss out our data and restart.
74             cs_old, cs_new = data[1:]
75             if cs_old is not None and cs_old == 0 and cs_new == 1:
76                 if len(self.mosi_bytes) > 0 and len(self.mosi_bytes[1:]) < self.expected:
77                     # Mark short read/write for reg at least!
78                     self.es_cmd = es
79                     write, reg = self.cmd & 0x80, self.cmd & 0x7f
80                     rblob = regs.get(reg)
81                     idx = 1 if write else 0
82                     self.putx([idx, ['%s: %s' % (rblob[0], "SHORT")]])
83                     self.put_warn([self.ss_cmd, es], "Short transfer!")
84                 self.reset_data()
85             return
86
87         # Don't care about anything else.
88         if ptype != 'DATA':
89             return
90         mosi, miso = data[1:]
91
92         if len(self.mosi_bytes) == 0:
93             self.ss_cmd = ss
94         self.mosi_bytes.append(mosi)
95         self.miso_bytes.append(miso)
96
97         # A transfer is 2-4 bytes, (command + 1..3 byte reg).
98         if len(self.mosi_bytes) < 2:
99             return
100
101         self.cmd = self.mosi_bytes[0]
102         write, reg = self.cmd & 0x80, self.cmd & 0x7f
103         rblob = regs.get(reg)
104         if not rblob:
105             # If you don't have CS, this will _destroy_ comms!
106             self.put_warn([self.ss_cmd, es], 'Unknown register!')
107             return
108
109         self.expected = math.ceil(rblob[3] / 8)
110         if len(self.mosi_bytes[1:]) != self.expected:
111             return
112         valo, vali = None, None
113         self.es_cmd = es
114         if self.expected == 3:
115             valo = self.mosi_bytes[1] << 16 | self.mosi_bytes[2] << 8 | \
116                    self.mosi_bytes[3]
117             vali = self.miso_bytes[1] << 16 | self.miso_bytes[2] << 8 | \
118                    self.miso_bytes[3]
119         elif self.expected == 2:
120             valo = self.mosi_bytes[1] << 8 | self.mosi_bytes[2]
121             vali = self.miso_bytes[1] << 8 | self.miso_bytes[2]
122         elif self.expected == 1:
123             valo = self.mosi_bytes[1]
124             vali = self.miso_bytes[1]
125
126         if write:
127             self.putx([1, ['%s: %#x' % (rblob[0], valo)]])
128         else:
129             self.putx([0, ['%s: %#x' % (rblob[0], vali)]])
130
131         self.reset_data()