2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012-2014 Uwe Hermann <uwe@hermann-uwe.de>
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.
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.
16 ## You should have received a copy of the GNU General Public License
17 ## along with this program; if not, see <http://www.gnu.org/licenses/>.
20 import sigrokdecode
as srd
21 from common
.srdhelper
import bcd2int
25 for i
in range(8 + 1):
26 l
.append(('reg-0x%02x' % i
, 'Register 0x%02x' % i
))
30 class Decoder(srd
.Decoder
):
34 longname
= 'Epson RTC-8564 JE/NB'
35 desc
= 'Realtime clock module protocol.'
39 tags
= ['Clock/timing']
40 annotations
= reg_list() + (
41 ('read', 'Read date/time'),
42 ('write', 'Write date/time'),
43 ('bit-reserved', 'Reserved bit'),
45 ('bit-century', 'Century bit'),
46 ('reg-read', 'Register read'),
47 ('reg-write', 'Register write'),
50 ('bits', 'Bits', tuple(range(0, 8 + 1)) + (11, 12, 13)),
51 ('regs', 'Register accesses', (14, 15)),
52 ('date-time', 'Date/time', (9, 10)),
70 self
.out_ann
= self
.register(srd
.OUTPUT_ANN
)
73 self
.put(self
.ss
, self
.es
, self
.out_ann
, data
)
75 def putd(self
, bit1
, bit2
, data
):
76 self
.put(self
.bits
[bit1
][1], self
.bits
[bit2
][2], self
.out_ann
, data
)
79 self
.put(self
.bits
[bit
][1], self
.bits
[bit
][2], self
.out_ann
,
80 [11, ['Reserved bit', 'Reserved', 'Rsvd', 'R']])
82 def handle_reg_0x00(self
, b
): # Control register 1
85 def handle_reg_0x01(self
, b
): # Control register 2
86 ti_tp
= 1 if (b
& (1 << 4)) else 0
87 af
= 1 if (b
& (1 << 3)) else 0
88 tf
= 1 if (b
& (1 << 2)) else 0
89 aie
= 1 if (b
& (1 << 1)) else 0
90 tie
= 1 if (b
& (1 << 0)) else 0
94 s
= 'repeated' if ti_tp
else 'single-shot'
95 ann
+= 'TI/TP = %d: %s operation upon fixed-cycle timer interrupt '\
96 'events\n' % (ti_tp
, s
)
97 s
= '' if af
else 'no '
98 ann
+= 'AF = %d: %salarm interrupt detected\n' % (af
, s
)
99 s
= '' if tf
else 'no '
100 ann
+= 'TF = %d: %sfixed-cycle timer interrupt detected\n' % (tf
, s
)
101 s
= 'enabled' if aie
else 'prohibited'
102 ann
+= 'AIE = %d: INT# pin output %s when an alarm interrupt '\
103 'occurs\n' % (aie
, s
)
104 s
= 'enabled' if tie
else 'prohibited'
105 ann
+= 'TIE = %d: INT# pin output %s when a fixed-cycle interrupt '\
106 'event occurs\n' % (tie
, s
)
108 self
.putx([1, [ann
]])
110 def handle_reg_0x02(self
, b
): # Seconds / Voltage-low bit
111 vl
= 1 if (b
& (1 << 7)) else 0
112 self
.putd(7, 7, [12, ['Voltage low: %d' % vl
, 'Volt. low: %d' % vl
,
113 'VL: %d' % vl
, 'VL']])
114 s
= self
.seconds
= bcd2int(b
& 0x7f)
115 self
.putd(6, 0, [2, ['Second: %d' % s
, 'Sec: %d' % s
, 'S: %d' % s
, 'S']])
117 def handle_reg_0x03(self
, b
): # Minutes
119 m
= self
.minutes
= bcd2int(b
& 0x7f)
120 self
.putd(6, 0, [3, ['Minute: %d' % m
, 'Min: %d' % m
, 'M: %d' % m
, 'M']])
122 def handle_reg_0x04(self
, b
): # Hours
125 h
= self
.hours
= bcd2int(b
& 0x3f)
126 self
.putd(5, 0, [4, ['Hour: %d' % h
, 'H: %d' % h
, 'H']])
128 def handle_reg_0x05(self
, b
): # Days
131 d
= self
.days
= bcd2int(b
& 0x3f)
132 self
.putd(5, 0, [5, ['Day: %d' % d
, 'D: %d' % d
, 'D']])
134 def handle_reg_0x06(self
, b
): # Weekdays
135 for i
in (7, 6, 5, 4, 3):
137 w
= self
.weekdays
= bcd2int(b
& 0x07)
138 self
.putd(2, 0, [6, ['Weekday: %d' % w
, 'WD: %d' % w
, 'WD', 'W']])
140 def handle_reg_0x07(self
, b
): # Months / century bit
141 c
= 1 if (b
& (1 << 7)) else 0
142 self
.putd(7, 7, [13, ['Century bit: %d' % c
, 'Century: %d' % c
,
143 'Cent: %d' % c
, 'C: %d' % c
, 'C']])
146 m
= self
.months
= bcd2int(b
& 0x1f)
147 self
.putd(4, 0, [7, ['Month: %d' % m
, 'Mon: %d' % m
, 'M: %d' % m
, 'M']])
149 def handle_reg_0x08(self
, b
): # Years
150 y
= self
.years
= bcd2int(b
& 0xff)
151 self
.putx([8, ['Year: %d' % y
, 'Y: %d' % y
, 'Y']])
153 def handle_reg_0x09(self
, b
): # Alarm, minute
156 def handle_reg_0x0a(self
, b
): # Alarm, hour
159 def handle_reg_0x0b(self
, b
): # Alarm, day
162 def handle_reg_0x0c(self
, b
): # Alarm, weekday
165 def handle_reg_0x0d(self
, b
): # CLKOUT output
168 def handle_reg_0x0e(self
, b
): # Timer setting
171 def handle_reg_0x0f(self
, b
): # Down counter for fixed-cycle timer
174 def decode(self
, ss
, es
, data
):
177 # Collect the 'BITS' packet, then return. The next packet is
178 # guaranteed to belong to these bits we just stored.
183 # Store the start/end samples of this I²C packet.
184 self
.ss
, self
.es
= ss
, es
187 if self
.state
== 'IDLE':
188 # Wait for an I²C START condition.
191 self
.state
= 'GET SLAVE ADDR'
193 elif self
.state
== 'GET SLAVE ADDR':
194 # Wait for an address write operation.
195 # TODO: We should only handle packets to the RTC slave (0xa2/0xa3).
196 if cmd
!= 'ADDRESS WRITE':
198 self
.state
= 'GET REG ADDR'
199 elif self
.state
== 'GET REG ADDR':
200 # Wait for a data write (master selects the slave register).
201 if cmd
!= 'DATA WRITE':
204 self
.state
= 'WRITE RTC REGS'
205 elif self
.state
== 'WRITE RTC REGS':
206 # If we see a Repeated Start here, it's probably an RTC read.
207 if cmd
== 'START REPEAT':
208 self
.state
= 'READ RTC REGS'
210 # Otherwise: Get data bytes until a STOP condition occurs.
211 if cmd
== 'DATA WRITE':
212 r
, s
= self
.reg
, '%02X: %02X' % (self
.reg
, databyte
)
213 self
.putx([15, ['Write register %s' % s
, 'Write reg %s' % s
,
214 'WR %s' % s
, 'WR', 'W']])
215 handle_reg
= getattr(self
, 'handle_reg_0x%02x' % self
.reg
)
218 # TODO: Check for NACK!
220 # TODO: Handle read/write of only parts of these items.
221 d
= '%02d.%02d.%02d %02d:%02d:%02d' % (self
.days
, self
.months
,
222 self
.years
, self
.hours
, self
.minutes
, self
.seconds
)
223 self
.put(self
.ss_block
, es
, self
.out_ann
,
224 [9, ['Write date/time: %s' % d
, 'Write: %s' % d
,
229 elif self
.state
== 'READ RTC REGS':
230 # Wait for an address read operation.
231 # TODO: We should only handle packets to the RTC slave (0xa2/0xa3).
232 if cmd
== 'ADDRESS READ':
233 self
.state
= 'READ RTC REGS2'
237 elif self
.state
== 'READ RTC REGS2':
238 if cmd
== 'DATA READ':
239 r
, s
= self
.reg
, '%02X: %02X' % (self
.reg
, databyte
)
240 self
.putx([15, ['Read register %s' % s
, 'Read reg %s' % s
,
241 'RR %s' % s
, 'RR', 'R']])
242 handle_reg
= getattr(self
, 'handle_reg_0x%02x' % self
.reg
)
245 # TODO: Check for NACK!
247 d
= '%02d.%02d.%02d %02d:%02d:%02d' % (self
.days
, self
.months
,
248 self
.years
, self
.hours
, self
.minutes
, self
.seconds
)
249 self
.put(self
.ss_block
, es
, self
.out_ann
,
250 [10, ['Read date/time: %s' % d
, 'Read: %s' % d
,