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