]>
Commit | Line | Data |
---|---|---|
ed5f826a | 1 | ## |
50bd5d25 | 2 | ## This file is part of the libsigrokdecode project. |
ed5f826a | 3 | ## |
6e256b1c | 4 | ## Copyright (C) 2012-2014 Uwe Hermann <uwe@hermann-uwe.de> |
ed5f826a UH |
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 | |
4539e9ca | 17 | ## along with this program; if not, see <http://www.gnu.org/licenses/>. |
ed5f826a UH |
18 | ## |
19 | ||
ed5f826a | 20 | import sigrokdecode as srd |
135b790c | 21 | from common.srdhelper import bcd2int |
ed5f826a | 22 | |
da9bcbd9 BV |
23 | def reg_list(): |
24 | l = [] | |
25 | for i in range(8 + 1): | |
26 | l.append(('reg-0x%02x' % i, 'Register 0x%02x' % i)) | |
27 | ||
28 | return tuple(l) | |
29 | ||
ed5f826a | 30 | class Decoder(srd.Decoder): |
b197383c | 31 | api_version = 3 |
ed5f826a UH |
32 | id = 'rtc8564' |
33 | name = 'RTC-8564' | |
34 | longname = 'Epson RTC-8564 JE/NB' | |
a465436e | 35 | desc = 'Realtime clock module protocol.' |
ed5f826a UH |
36 | license = 'gplv2+' |
37 | inputs = ['i2c'] | |
38 | outputs = ['rtc8564'] | |
da9bcbd9 BV |
39 | annotations = reg_list() + ( |
40 | ('read', 'Read date/time'), | |
41 | ('write', 'Write date/time'), | |
42 | ('bit-reserved', 'Reserved bit'), | |
43 | ('bit-vl', 'VL bit'), | |
44 | ('bit-century', 'Century bit'), | |
45 | ('reg-read', 'Register read'), | |
46 | ('reg-write', 'Register write'), | |
47 | ) | |
3161ab5a | 48 | annotation_rows = ( |
09d09ace UH |
49 | ('bits', 'Bits', tuple(range(0, 8 + 1)) + (11, 12, 13)), |
50 | ('regs', 'Register access', (14, 15)), | |
3161ab5a UH |
51 | ('date-time', 'Date/time', (9, 10)), |
52 | ) | |
ed5f826a | 53 | |
92b7b49f | 54 | def __init__(self): |
10aeb8ea GS |
55 | self.reset() |
56 | ||
57 | def reset(self): | |
2b716038 | 58 | self.state = 'IDLE' |
ed5f826a UH |
59 | self.hours = -1 |
60 | self.minutes = -1 | |
61 | self.seconds = -1 | |
62 | self.days = -1 | |
3d190141 | 63 | self.weekdays = -1 |
ed5f826a UH |
64 | self.months = -1 |
65 | self.years = -1 | |
09d09ace | 66 | self.bits = [] |
ed5f826a | 67 | |
8915b346 | 68 | def start(self): |
be465111 | 69 | self.out_ann = self.register(srd.OUTPUT_ANN) |
ed5f826a | 70 | |
ed5f826a UH |
71 | def putx(self, data): |
72 | self.put(self.ss, self.es, self.out_ann, data) | |
73 | ||
09d09ace UH |
74 | def putd(self, bit1, bit2, data): |
75 | self.put(self.bits[bit1][1], self.bits[bit2][2], self.out_ann, data) | |
76 | ||
77 | def putr(self, bit): | |
78 | self.put(self.bits[bit][1], self.bits[bit][2], self.out_ann, | |
79 | [11, ['Reserved bit', 'Reserved', 'Rsvd', 'R']]) | |
80 | ||
ed5f826a UH |
81 | def handle_reg_0x00(self, b): # Control register 1 |
82 | pass | |
83 | ||
84 | def handle_reg_0x01(self, b): # Control register 2 | |
85 | ti_tp = 1 if (b & (1 << 4)) else 0 | |
86 | af = 1 if (b & (1 << 3)) else 0 | |
87 | tf = 1 if (b & (1 << 2)) else 0 | |
88 | aie = 1 if (b & (1 << 1)) else 0 | |
89 | tie = 1 if (b & (1 << 0)) else 0 | |
90 | ||
91 | ann = '' | |
92 | ||
93 | s = 'repeated' if ti_tp else 'single-shot' | |
94 | ann += 'TI/TP = %d: %s operation upon fixed-cycle timer interrupt '\ | |
95 | 'events\n' % (ti_tp, s) | |
96 | s = '' if af else 'no ' | |
97 | ann += 'AF = %d: %salarm interrupt detected\n' % (af, s) | |
98 | s = '' if tf else 'no ' | |
99 | ann += 'TF = %d: %sfixed-cycle timer interrupt detected\n' % (tf, s) | |
100 | s = 'enabled' if aie else 'prohibited' | |
101 | ann += 'AIE = %d: INT# pin output %s when an alarm interrupt '\ | |
102 | 'occurs\n' % (aie, s) | |
103 | s = 'enabled' if tie else 'prohibited' | |
104 | ann += 'TIE = %d: INT# pin output %s when a fixed-cycle interrupt '\ | |
105 | 'event occurs\n' % (tie, s) | |
106 | ||
a4289441 | 107 | self.putx([1, [ann]]) |
ed5f826a | 108 | |
64134a4c | 109 | def handle_reg_0x02(self, b): # Seconds / Voltage-low bit |
ed5f826a | 110 | vl = 1 if (b & (1 << 7)) else 0 |
09d09ace | 111 | self.putd(7, 7, [12, ['Voltage low: %d' % vl, 'Volt. low: %d' % vl, |
bff3a0a0 | 112 | 'VL: %d' % vl, 'VL']]) |
09d09ace | 113 | s = self.seconds = bcd2int(b & 0x7f) |
bff3a0a0 | 114 | self.putd(6, 0, [2, ['Second: %d' % s, 'Sec: %d' % s, 'S: %d' % s, 'S']]) |
ed5f826a UH |
115 | |
116 | def handle_reg_0x03(self, b): # Minutes | |
09d09ace | 117 | self.putr(7) |
6e256b1c | 118 | m = self.minutes = bcd2int(b & 0x7f) |
bff3a0a0 | 119 | self.putd(6, 0, [3, ['Minute: %d' % m, 'Min: %d' % m, 'M: %d' % m, 'M']]) |
ed5f826a UH |
120 | |
121 | def handle_reg_0x04(self, b): # Hours | |
09d09ace UH |
122 | self.putr(7) |
123 | self.putr(6) | |
6e256b1c | 124 | h = self.hours = bcd2int(b & 0x3f) |
bff3a0a0 | 125 | self.putd(5, 0, [4, ['Hour: %d' % h, 'H: %d' % h, 'H']]) |
ed5f826a UH |
126 | |
127 | def handle_reg_0x05(self, b): # Days | |
09d09ace UH |
128 | self.putr(7) |
129 | self.putr(6) | |
6e256b1c | 130 | d = self.days = bcd2int(b & 0x3f) |
bff3a0a0 | 131 | self.putd(5, 0, [5, ['Day: %d' % d, 'D: %d' % d, 'D']]) |
ed5f826a | 132 | |
3d190141 | 133 | def handle_reg_0x06(self, b): # Weekdays |
09d09ace UH |
134 | for i in (7, 6, 5, 4, 3): |
135 | self.putr(i) | |
6e256b1c | 136 | w = self.weekdays = bcd2int(b & 0x07) |
bff3a0a0 | 137 | self.putd(2, 0, [6, ['Weekday: %d' % w, 'WD: %d' % w, 'WD', 'W']]) |
ed5f826a | 138 | |
64134a4c | 139 | def handle_reg_0x07(self, b): # Months / century bit |
64134a4c | 140 | c = 1 if (b & (1 << 7)) else 0 |
bff3a0a0 UH |
141 | self.putd(7, 7, [13, ['Century bit: %d' % c, 'Century: %d' % c, |
142 | 'Cent: %d' % c, 'C: %d' % c, 'C']]) | |
09d09ace UH |
143 | self.putr(6) |
144 | self.putr(5) | |
145 | m = self.months = bcd2int(b & 0x1f) | |
bff3a0a0 | 146 | self.putd(4, 0, [7, ['Month: %d' % m, 'Mon: %d' % m, 'M: %d' % m, 'M']]) |
ed5f826a UH |
147 | |
148 | def handle_reg_0x08(self, b): # Years | |
6e256b1c | 149 | y = self.years = bcd2int(b & 0xff) |
bff3a0a0 | 150 | self.putx([8, ['Year: %d' % y, 'Y: %d' % y, 'Y']]) |
ed5f826a UH |
151 | |
152 | def handle_reg_0x09(self, b): # Alarm, minute | |
153 | pass | |
154 | ||
155 | def handle_reg_0x0a(self, b): # Alarm, hour | |
156 | pass | |
157 | ||
158 | def handle_reg_0x0b(self, b): # Alarm, day | |
159 | pass | |
160 | ||
161 | def handle_reg_0x0c(self, b): # Alarm, weekday | |
162 | pass | |
163 | ||
164 | def handle_reg_0x0d(self, b): # CLKOUT output | |
165 | pass | |
166 | ||
167 | def handle_reg_0x0e(self, b): # Timer setting | |
168 | pass | |
169 | ||
170 | def handle_reg_0x0f(self, b): # Down counter for fixed-cycle timer | |
171 | pass | |
172 | ||
173 | def decode(self, ss, es, data): | |
1b75abfd | 174 | cmd, databyte = data |
ed5f826a | 175 | |
09d09ace UH |
176 | # Collect the 'BITS' packet, then return. The next packet is |
177 | # guaranteed to belong to these bits we just stored. | |
178 | if cmd == 'BITS': | |
179 | self.bits = databyte | |
180 | return | |
181 | ||
00197484 | 182 | # Store the start/end samples of this I²C packet. |
ed5f826a UH |
183 | self.ss, self.es = ss, es |
184 | ||
185 | # State machine. | |
2b716038 | 186 | if self.state == 'IDLE': |
00197484 | 187 | # Wait for an I²C START condition. |
ed5f826a UH |
188 | if cmd != 'START': |
189 | return | |
2b716038 | 190 | self.state = 'GET SLAVE ADDR' |
486b19ce | 191 | self.ss_block = ss |
2b716038 | 192 | elif self.state == 'GET SLAVE ADDR': |
ed5f826a UH |
193 | # Wait for an address write operation. |
194 | # TODO: We should only handle packets to the RTC slave (0xa2/0xa3). | |
195 | if cmd != 'ADDRESS WRITE': | |
196 | return | |
2b716038 UH |
197 | self.state = 'GET REG ADDR' |
198 | elif self.state == 'GET REG ADDR': | |
ed5f826a UH |
199 | # Wait for a data write (master selects the slave register). |
200 | if cmd != 'DATA WRITE': | |
201 | return | |
202 | self.reg = databyte | |
2b716038 UH |
203 | self.state = 'WRITE RTC REGS' |
204 | elif self.state == 'WRITE RTC REGS': | |
ed5f826a UH |
205 | # If we see a Repeated Start here, it's probably an RTC read. |
206 | if cmd == 'START REPEAT': | |
2b716038 | 207 | self.state = 'READ RTC REGS' |
ed5f826a UH |
208 | return |
209 | # Otherwise: Get data bytes until a STOP condition occurs. | |
210 | if cmd == 'DATA WRITE': | |
09d09ace UH |
211 | r, s = self.reg, '%02X: %02X' % (self.reg, databyte) |
212 | self.putx([15, ['Write register %s' % s, 'Write reg %s' % s, | |
213 | 'WR %s' % s, 'WR', 'W']]) | |
ed5f826a UH |
214 | handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg) |
215 | handle_reg(databyte) | |
216 | self.reg += 1 | |
217 | # TODO: Check for NACK! | |
218 | elif cmd == 'STOP': | |
219 | # TODO: Handle read/write of only parts of these items. | |
220 | d = '%02d.%02d.%02d %02d:%02d:%02d' % (self.days, self.months, | |
221 | self.years, self.hours, self.minutes, self.seconds) | |
486b19ce | 222 | self.put(self.ss_block, es, self.out_ann, |
6e256b1c UH |
223 | [9, ['Write date/time: %s' % d, 'Write: %s' % d, |
224 | 'W: %s' % d]]) | |
2b716038 | 225 | self.state = 'IDLE' |
ed5f826a UH |
226 | else: |
227 | pass # TODO | |
2b716038 | 228 | elif self.state == 'READ RTC REGS': |
ed5f826a UH |
229 | # Wait for an address read operation. |
230 | # TODO: We should only handle packets to the RTC slave (0xa2/0xa3). | |
231 | if cmd == 'ADDRESS READ': | |
2b716038 | 232 | self.state = 'READ RTC REGS2' |
ed5f826a UH |
233 | return |
234 | else: | |
e4f82268 | 235 | pass # TODO |
2b716038 | 236 | elif self.state == 'READ RTC REGS2': |
ed5f826a | 237 | if cmd == 'DATA READ': |
09d09ace UH |
238 | r, s = self.reg, '%02X: %02X' % (self.reg, databyte) |
239 | self.putx([15, ['Read register %s' % s, 'Read reg %s' % s, | |
240 | 'RR %s' % s, 'RR', 'R']]) | |
ed5f826a UH |
241 | handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg) |
242 | handle_reg(databyte) | |
243 | self.reg += 1 | |
244 | # TODO: Check for NACK! | |
245 | elif cmd == 'STOP': | |
246 | d = '%02d.%02d.%02d %02d:%02d:%02d' % (self.days, self.months, | |
247 | self.years, self.hours, self.minutes, self.seconds) | |
486b19ce | 248 | self.put(self.ss_block, es, self.out_ann, |
6e256b1c UH |
249 | [10, ['Read date/time: %s' % d, 'Read: %s' % d, |
250 | 'R: %s' % d]]) | |
2b716038 | 251 | self.state = 'IDLE' |
ed5f826a UH |
252 | else: |
253 | pass # TODO? |