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