]> sigrok.org Git - libsigrokdecode.git/blame - decoders/rtc8564/pd.py
Drop obsolete report() method.
[libsigrokdecode.git] / decoders / rtc8564 / pd.py
CommitLineData
ed5f826a 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
ed5f826a
UH
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
156509ca 21# Epson RTC-8564 JE/NB protocol decoder
ed5f826a
UH
22
23import sigrokdecode as srd
24
ed5f826a
UH
25# Return the specified BCD number (max. 8 bits) as integer.
26def bcd2int(b):
27 return (b & 0x0f) + ((b >> 4) * 10)
28
29class Decoder(srd.Decoder):
30 api_version = 1
31 id = 'rtc8564'
32 name = 'RTC-8564'
33 longname = 'Epson RTC-8564 JE/NB'
a465436e 34 desc = 'Realtime clock module protocol.'
ed5f826a
UH
35 license = 'gplv2+'
36 inputs = ['i2c']
37 outputs = ['rtc8564']
38 probes = []
b77614bc 39 optional_probes = [
ed5f826a
UH
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 = [
ee3e279c 46 ['Text', 'Human-readable text'],
ed5f826a
UH
47 ]
48
49 def __init__(self, **kwargs):
2b716038 50 self.state = 'IDLE'
ed5f826a
UH
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
8915b346 58 def start(self):
be465111
BV
59 # self.out_proto = self.register(srd.OUTPUT_PYTHON)
60 self.out_ann = self.register(srd.OUTPUT_ANN)
ed5f826a 61
ed5f826a
UH
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):
1b75abfd 145 cmd, databyte = data
ed5f826a
UH
146
147 # Store the start/end samples of this I2C packet.
148 self.ss, self.es = ss, es
149
150 # State machine.
2b716038 151 if self.state == 'IDLE':
ed5f826a
UH
152 # Wait for an I2C START condition.
153 if cmd != 'START':
154 return
2b716038 155 self.state = 'GET SLAVE ADDR'
ed5f826a 156 self.block_start_sample = ss
2b716038 157 elif self.state == 'GET SLAVE ADDR':
ed5f826a
UH
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
2b716038
UH
162 self.state = 'GET REG ADDR'
163 elif self.state == 'GET REG ADDR':
ed5f826a
UH
164 # Wait for a data write (master selects the slave register).
165 if cmd != 'DATA WRITE':
166 return
167 self.reg = databyte
2b716038
UH
168 self.state = 'WRITE RTC REGS'
169 elif self.state == 'WRITE RTC REGS':
ed5f826a
UH
170 # If we see a Repeated Start here, it's probably an RTC read.
171 if cmd == 'START REPEAT':
2b716038 172 self.state = 'READ RTC REGS'
ed5f826a
UH
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]])
2b716038 186 self.state = 'IDLE'
ed5f826a
UH
187 else:
188 pass # TODO
2b716038 189 elif self.state == 'READ RTC REGS':
ed5f826a
UH
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':
2b716038 193 self.state = 'READ RTC REGS2'
ed5f826a
UH
194 return
195 else:
e4f82268 196 pass # TODO
2b716038 197 elif self.state == 'READ RTC REGS2':
ed5f826a
UH
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]])
2b716038 208 self.state = 'IDLE'
ed5f826a
UH
209 else:
210 pass # TODO?
211 else:
0eeeb544 212 raise Exception('Invalid state: %s' % self.state)
ed5f826a 213