]> sigrok.org Git - libsigrokdecode.git/blob - decoders/rtc8564/rtc8564.py
srd: Remove TODOs from annotation format names.
[libsigrokdecode.git] / decoders / rtc8564 / rtc8564.py
1 ##
2 ## This file is part of the sigrok 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 = 'TODO.'
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, metadata):
59         # self.out_proto = self.add(srd.OUTPUT_PROTO, 'rtc8564')
60         self.out_ann = self.add(srd.OUTPUT_ANN, 'rtc8564')
61
62     def report(self):
63         pass
64
65     def putx(self, data):
66         self.put(self.ss, self.es, self.out_ann, data)
67
68     def handle_reg_0x00(self, b): # Control register 1
69         pass
70
71     def handle_reg_0x01(self, b): # Control register 2
72         ti_tp = 1 if (b & (1 << 4)) else 0
73         af = 1 if (b & (1 << 3)) else 0
74         tf = 1 if (b & (1 << 2)) else 0
75         aie = 1 if (b & (1 << 1)) else 0
76         tie = 1 if (b & (1 << 0)) else 0
77
78         ann = ''
79
80         s = 'repeated' if ti_tp else 'single-shot'
81         ann += 'TI/TP = %d: %s operation upon fixed-cycle timer interrupt '\
82                'events\n' % (ti_tp, s)
83         s = '' if af else 'no '
84         ann += 'AF = %d: %salarm interrupt detected\n' % (af, s)
85         s = '' if tf else 'no '
86         ann += 'TF = %d: %sfixed-cycle timer interrupt detected\n' % (tf, s)
87         s = 'enabled' if aie else 'prohibited'
88         ann += 'AIE = %d: INT# pin output %s when an alarm interrupt '\
89                'occurs\n' % (aie, s)
90         s = 'enabled' if tie else 'prohibited'
91         ann += 'TIE = %d: INT# pin output %s when a fixed-cycle interrupt '\
92                'event occurs\n' % (tie, s)
93
94         self.putx([0, [ann]])
95
96     def handle_reg_0x02(self, b): # Seconds / Voltage-low flag
97         self.seconds = bcd2int(b & 0x7f)
98         self.putx([0, ['Seconds: %d' % self.seconds]])
99         vl = 1 if (b & (1 << 7)) else 0
100         self.putx([0, ['Voltage low (VL) bit: %d' % vl]])
101
102     def handle_reg_0x03(self, b): # Minutes
103         self.minutes = bcd2int(b & 0x7f)
104         self.putx([0, ['Minutes: %d' % self.minutes]])
105
106     def handle_reg_0x04(self, b): # Hours
107         self.hours = bcd2int(b & 0x3f)
108         self.putx([0, ['Hours: %d' % self.hours]])
109
110     def handle_reg_0x05(self, b): # Days
111         self.days = bcd2int(b & 0x3f)
112         self.putx([0, ['Days: %d' % self.days]])
113
114     def handle_reg_0x06(self, b): # Day counter
115         pass
116
117     def handle_reg_0x07(self, b): # Months / century
118         # TODO: Handle century bit.
119         self.months = bcd2int(b & 0x1f)
120         self.putx([0, ['Months: %d' % self.months]])
121
122     def handle_reg_0x08(self, b): # Years
123         self.years = bcd2int(b & 0xff)
124         self.putx([0, ['Years: %d' % self.years]])
125
126     def handle_reg_0x09(self, b): # Alarm, minute
127         pass
128
129     def handle_reg_0x0a(self, b): # Alarm, hour
130         pass
131
132     def handle_reg_0x0b(self, b): # Alarm, day
133         pass
134
135     def handle_reg_0x0c(self, b): # Alarm, weekday
136         pass
137
138     def handle_reg_0x0d(self, b): # CLKOUT output
139         pass
140
141     def handle_reg_0x0e(self, b): # Timer setting
142         pass
143
144     def handle_reg_0x0f(self, b): # Down counter for fixed-cycle timer
145         pass
146
147     def decode(self, ss, es, data):
148         cmd, databyte = data
149
150         # Store the start/end samples of this I2C packet.
151         self.ss, self.es = ss, es
152
153         # State machine.
154         if self.state == 'IDLE':
155             # Wait for an I2C START condition.
156             if cmd != 'START':
157                 return
158             self.state = 'GET SLAVE ADDR'
159             self.block_start_sample = ss
160         elif self.state == 'GET SLAVE ADDR':
161             # Wait for an address write operation.
162             # TODO: We should only handle packets to the RTC slave (0xa2/0xa3).
163             if cmd != 'ADDRESS WRITE':
164                 return
165             self.state = 'GET REG ADDR'
166         elif self.state == 'GET REG ADDR':
167             # Wait for a data write (master selects the slave register).
168             if cmd != 'DATA WRITE':
169                 return
170             self.reg = databyte
171             self.state = 'WRITE RTC REGS'
172         elif self.state == 'WRITE RTC REGS':
173             # If we see a Repeated Start here, it's probably an RTC read.
174             if cmd == 'START REPEAT':
175                 self.state = 'READ RTC REGS'
176                 return
177             # Otherwise: Get data bytes until a STOP condition occurs.
178             if cmd == 'DATA WRITE':
179                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
180                 handle_reg(databyte)
181                 self.reg += 1
182                 # TODO: Check for NACK!
183             elif cmd == 'STOP':
184                 # TODO: Handle read/write of only parts of these items.
185                 d = '%02d.%02d.%02d %02d:%02d:%02d' % (self.days, self.months,
186                     self.years, self.hours, self.minutes, self.seconds)
187                 self.put(self.block_start_sample, es, self.out_ann,
188                          [0, ['Written date/time: %s' % d]])
189                 self.state = 'IDLE'
190             else:
191                 pass # TODO
192         elif self.state == 'READ RTC REGS':
193             # Wait for an address read operation.
194             # TODO: We should only handle packets to the RTC slave (0xa2/0xa3).
195             if cmd == 'ADDRESS READ':
196                 self.state = 'READ RTC REGS2'
197                 return
198             else:
199                 pass # TODO
200         elif self.state == 'READ RTC REGS2':
201             if cmd == 'DATA READ':
202                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
203                 handle_reg(databyte)
204                 self.reg += 1
205                 # TODO: Check for NACK!
206             elif cmd == 'STOP':
207                 d = '%02d.%02d.%02d %02d:%02d:%02d' % (self.days, self.months,
208                     self.years, self.hours, self.minutes, self.seconds)
209                 self.put(self.block_start_sample, es, self.out_ann,
210                          [0, ['Read date/time: %s' % d]])
211                 self.state = 'IDLE'
212             else:
213                 pass # TODO?
214         else:
215             raise Exception('Invalid state: %d' % self.state)
216