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