]> sigrok.org Git - libsigrokdecode.git/blob - decoders/rtc8564/pd.py
Do some more I2C to I²C changes.
[libsigrokdecode.git] / decoders / rtc8564 / pd.py
1 ##
2 ## This file is part of the libsigrokdecode project.
3 ##
4 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
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 # Epson RTC-8564 JE/NB protocol decoder
22
23 import sigrokdecode as srd
24
25 # Return the specified BCD number (max. 8 bits) as integer.
26 def bcd2int(b):
27     return (b & 0x0f) + ((b >> 4) * 10)
28
29 class Decoder(srd.Decoder):
30     api_version = 1
31     id = 'rtc8564'
32     name = 'RTC-8564'
33     longname = 'Epson RTC-8564 JE/NB'
34     desc = 'Realtime clock module protocol.'
35     license = 'gplv2+'
36     inputs = ['i2c']
37     outputs = ['rtc8564']
38     probes = []
39     optional_probes = [
40         {'id': 'clkout', 'name': 'CLKOUT', 'desc': 'TODO.'},
41         {'id': 'clkoe', 'name': 'CLKOE', 'desc': 'TODO.'},
42         {'id': 'int', 'name': 'INT#', 'desc': 'TODO.'},
43     ]
44     options = {}
45     annotations = [
46         ['Text', 'Human-readable text'],
47     ]
48
49     def __init__(self, **kwargs):
50         self.state = 'IDLE'
51         self.hours = -1
52         self.minutes = -1
53         self.seconds = -1
54         self.days = -1
55         self.months = -1
56         self.years = -1
57
58     def start(self):
59         # self.out_proto = self.register(srd.OUTPUT_PYTHON)
60         self.out_ann = self.register(srd.OUTPUT_ANN)
61
62     def putx(self, data):
63         self.put(self.ss, self.es, self.out_ann, data)
64
65     def handle_reg_0x00(self, b): # Control register 1
66         pass
67
68     def handle_reg_0x01(self, b): # Control register 2
69         ti_tp = 1 if (b & (1 << 4)) else 0
70         af = 1 if (b & (1 << 3)) else 0
71         tf = 1 if (b & (1 << 2)) else 0
72         aie = 1 if (b & (1 << 1)) else 0
73         tie = 1 if (b & (1 << 0)) else 0
74
75         ann = ''
76
77         s = 'repeated' if ti_tp else 'single-shot'
78         ann += 'TI/TP = %d: %s operation upon fixed-cycle timer interrupt '\
79                'events\n' % (ti_tp, s)
80         s = '' if af else 'no '
81         ann += 'AF = %d: %salarm interrupt detected\n' % (af, s)
82         s = '' if tf else 'no '
83         ann += 'TF = %d: %sfixed-cycle timer interrupt detected\n' % (tf, s)
84         s = 'enabled' if aie else 'prohibited'
85         ann += 'AIE = %d: INT# pin output %s when an alarm interrupt '\
86                'occurs\n' % (aie, s)
87         s = 'enabled' if tie else 'prohibited'
88         ann += 'TIE = %d: INT# pin output %s when a fixed-cycle interrupt '\
89                'event occurs\n' % (tie, s)
90
91         self.putx([0, [ann]])
92
93     def handle_reg_0x02(self, b): # Seconds / Voltage-low flag
94         self.seconds = bcd2int(b & 0x7f)
95         self.putx([0, ['Seconds: %d' % self.seconds]])
96         vl = 1 if (b & (1 << 7)) else 0
97         self.putx([0, ['Voltage low (VL) bit: %d' % vl]])
98
99     def handle_reg_0x03(self, b): # Minutes
100         self.minutes = bcd2int(b & 0x7f)
101         self.putx([0, ['Minutes: %d' % self.minutes]])
102
103     def handle_reg_0x04(self, b): # Hours
104         self.hours = bcd2int(b & 0x3f)
105         self.putx([0, ['Hours: %d' % self.hours]])
106
107     def handle_reg_0x05(self, b): # Days
108         self.days = bcd2int(b & 0x3f)
109         self.putx([0, ['Days: %d' % self.days]])
110
111     def handle_reg_0x06(self, b): # Day counter
112         pass
113
114     def handle_reg_0x07(self, b): # Months / century
115         # TODO: Handle century bit.
116         self.months = bcd2int(b & 0x1f)
117         self.putx([0, ['Months: %d' % self.months]])
118
119     def handle_reg_0x08(self, b): # Years
120         self.years = bcd2int(b & 0xff)
121         self.putx([0, ['Years: %d' % self.years]])
122
123     def handle_reg_0x09(self, b): # Alarm, minute
124         pass
125
126     def handle_reg_0x0a(self, b): # Alarm, hour
127         pass
128
129     def handle_reg_0x0b(self, b): # Alarm, day
130         pass
131
132     def handle_reg_0x0c(self, b): # Alarm, weekday
133         pass
134
135     def handle_reg_0x0d(self, b): # CLKOUT output
136         pass
137
138     def handle_reg_0x0e(self, b): # Timer setting
139         pass
140
141     def handle_reg_0x0f(self, b): # Down counter for fixed-cycle timer
142         pass
143
144     def decode(self, ss, es, data):
145         cmd, databyte = data
146
147         # Store the start/end samples of this I²C packet.
148         self.ss, self.es = ss, es
149
150         # State machine.
151         if self.state == 'IDLE':
152             # Wait for an I²C START condition.
153             if cmd != 'START':
154                 return
155             self.state = 'GET SLAVE ADDR'
156             self.block_start_sample = ss
157         elif self.state == 'GET SLAVE ADDR':
158             # Wait for an address write operation.
159             # TODO: We should only handle packets to the RTC slave (0xa2/0xa3).
160             if cmd != 'ADDRESS WRITE':
161                 return
162             self.state = 'GET REG ADDR'
163         elif self.state == 'GET REG ADDR':
164             # Wait for a data write (master selects the slave register).
165             if cmd != 'DATA WRITE':
166                 return
167             self.reg = databyte
168             self.state = 'WRITE RTC REGS'
169         elif self.state == 'WRITE RTC REGS':
170             # If we see a Repeated Start here, it's probably an RTC read.
171             if cmd == 'START REPEAT':
172                 self.state = 'READ RTC REGS'
173                 return
174             # Otherwise: Get data bytes until a STOP condition occurs.
175             if cmd == 'DATA WRITE':
176                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
177                 handle_reg(databyte)
178                 self.reg += 1
179                 # TODO: Check for NACK!
180             elif cmd == 'STOP':
181                 # TODO: Handle read/write of only parts of these items.
182                 d = '%02d.%02d.%02d %02d:%02d:%02d' % (self.days, self.months,
183                     self.years, self.hours, self.minutes, self.seconds)
184                 self.put(self.block_start_sample, es, self.out_ann,
185                          [0, ['Written date/time: %s' % d]])
186                 self.state = 'IDLE'
187             else:
188                 pass # TODO
189         elif self.state == 'READ RTC REGS':
190             # Wait for an address read operation.
191             # TODO: We should only handle packets to the RTC slave (0xa2/0xa3).
192             if cmd == 'ADDRESS READ':
193                 self.state = 'READ RTC REGS2'
194                 return
195             else:
196                 pass # TODO
197         elif self.state == 'READ RTC REGS2':
198             if cmd == 'DATA READ':
199                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
200                 handle_reg(databyte)
201                 self.reg += 1
202                 # TODO: Check for NACK!
203             elif cmd == 'STOP':
204                 d = '%02d.%02d.%02d %02d:%02d:%02d' % (self.days, self.months,
205                     self.years, self.hours, self.minutes, self.seconds)
206                 self.put(self.block_start_sample, es, self.out_ann,
207                          [0, ['Read date/time: %s' % d]])
208                 self.state = 'IDLE'
209             else:
210                 pass # TODO?
211         else:
212             raise Exception('Invalid state: %s' % self.state)
213