]>
Commit | Line | Data |
---|---|---|
1 | ## | |
2 | ## This file is part of the libsigrokdecode project. | |
3 | ## | |
4 | ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de> | |
5 | ## Copyright (C) 2013 Matt Ranostay <mranostay@gmail.com> | |
6 | ## | |
7 | ## This program is free software; you can redistribute it and/or modify | |
8 | ## it under the terms of the GNU General Public License as published by | |
9 | ## the Free Software Foundation; either version 2 of the License, or | |
10 | ## (at your option) any later version. | |
11 | ## | |
12 | ## This program is distributed in the hope that it will be useful, | |
13 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | ## GNU General Public License for more details. | |
16 | ## | |
17 | ## You should have received a copy of the GNU General Public License | |
18 | ## along with this program; if not, write to the Free Software | |
19 | ## Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA | |
20 | ## | |
21 | ||
22 | import sigrokdecode as srd | |
23 | ||
24 | days_of_week = [ | |
25 | 'Sunday', | |
26 | 'Monday', | |
27 | 'Tuesday', | |
28 | 'Wednesday', | |
29 | 'Thursday', | |
30 | 'Friday', | |
31 | 'Saturday', | |
32 | ] | |
33 | ||
34 | # Return the specified BCD number (max. 8 bits) as integer. | |
35 | def bcd2int(b): | |
36 | return (b & 0x0f) + ((b >> 4) * 10) | |
37 | ||
38 | class Decoder(srd.Decoder): | |
39 | api_version = 2 | |
40 | id = 'ds1307' | |
41 | name = 'DS1307' | |
42 | longname = 'Dallas DS1307' | |
43 | desc = 'Realtime clock module protocol.' | |
44 | license = 'gplv2+' | |
45 | inputs = ['i2c'] | |
46 | outputs = ['ds1307'] | |
47 | annotations = ( | |
48 | ('text', 'Human-readable text'), | |
49 | ) | |
50 | ||
51 | def __init__(self, **kwargs): | |
52 | self.state = 'IDLE' | |
53 | self.hours = -1 | |
54 | self.minutes = -1 | |
55 | self.seconds = -1 | |
56 | self.days = -1 | |
57 | self.date = -1 | |
58 | self.months = -1 | |
59 | self.years = -1 | |
60 | ||
61 | def start(self): | |
62 | self.out_ann = self.register(srd.OUTPUT_ANN) | |
63 | ||
64 | def putx(self, data): | |
65 | self.put(self.ss, self.es, self.out_ann, data) | |
66 | ||
67 | def handle_reg_0x00(self, b): # Seconds | |
68 | self.seconds = bcd2int(b & 0x7f) | |
69 | self.putx([0, ['Seconds: %d' % self.seconds]]) | |
70 | ||
71 | def handle_reg_0x01(self, b): # Minutes | |
72 | self.minutes = bcd2int(b & 0x7f) | |
73 | self.putx([0, ['Minutes: %d' % self.minutes]]) | |
74 | ||
75 | def handle_reg_0x02(self, b): # Hours | |
76 | self.hours = bcd2int(b & 0x3f) | |
77 | self.putx([0, ['Hours: %d' % self.hours]]) | |
78 | ||
79 | def handle_reg_0x03(self, b): # Day of week | |
80 | self.days = bcd2int(b & 0x7) | |
81 | self.putx([0, ['Day of Week: %s' % days_of_week[self.days - 1]]]) | |
82 | ||
83 | def handle_reg_0x04(self, b): # Date | |
84 | self.date = bcd2int(b & 0x3f) | |
85 | self.putx([0, ['Days: %d' % self.date]]) | |
86 | ||
87 | def handle_reg_0x05(self, b): # Month | |
88 | self.months = bcd2int(b & 0x1f) | |
89 | self.putx([0, ['Months: %d' % self.months]]) | |
90 | ||
91 | def handle_reg_0x06(self, b): # Year | |
92 | self.years = bcd2int(b & 0xff) + 2000 | |
93 | self.putx([0, ['Years: %d' % self.years]]) | |
94 | ||
95 | def handle_reg_0x07(self, b): # Control Register | |
96 | pass | |
97 | ||
98 | def decode(self, ss, es, data): | |
99 | cmd, databyte = data | |
100 | ||
101 | # Store the start/end samples of this I²C packet. | |
102 | self.ss, self.es = ss, es | |
103 | ||
104 | # State machine. | |
105 | if self.state == 'IDLE': | |
106 | # Wait for an I²C START condition. | |
107 | if cmd != 'START': | |
108 | return | |
109 | self.state = 'GET SLAVE ADDR' | |
110 | self.block_start_sample = ss | |
111 | elif self.state == 'GET SLAVE ADDR': | |
112 | # Wait for an address write operation. | |
113 | # TODO: We should only handle packets to the RTC slave (0x68). | |
114 | if cmd != 'ADDRESS WRITE': | |
115 | return | |
116 | self.state = 'GET REG ADDR' | |
117 | elif self.state == 'GET REG ADDR': | |
118 | # Wait for a data write (master selects the slave register). | |
119 | if cmd != 'DATA WRITE': | |
120 | return | |
121 | self.reg = databyte | |
122 | self.state = 'WRITE RTC REGS' | |
123 | elif self.state == 'WRITE RTC REGS': | |
124 | # If we see a Repeated Start here, it's probably an RTC read. | |
125 | if cmd == 'START REPEAT': | |
126 | self.state = 'READ RTC REGS' | |
127 | return | |
128 | # Otherwise: Get data bytes until a STOP condition occurs. | |
129 | if cmd == 'DATA WRITE': | |
130 | handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg) | |
131 | handle_reg(databyte) | |
132 | self.reg += 1 | |
133 | # TODO: Check for NACK! | |
134 | elif cmd == 'STOP': | |
135 | # TODO: Handle read/write of only parts of these items. | |
136 | d = '%s, %02d.%02d.%02d %02d:%02d:%02d' % ( | |
137 | days_of_week[self.days - 1], self.date, self.months, | |
138 | self.years, self.hours, self.minutes, self.seconds) | |
139 | self.put(self.block_start_sample, es, self.out_ann, | |
140 | [0, ['Written date/time: %s' % d]]) | |
141 | self.state = 'IDLE' | |
142 | else: | |
143 | pass # TODO | |
144 | elif self.state == 'READ RTC REGS': | |
145 | # Wait for an address read operation. | |
146 | # TODO: We should only handle packets to the RTC slave (0x68). | |
147 | if cmd == 'ADDRESS READ': | |
148 | self.state = 'READ RTC REGS2' | |
149 | return | |
150 | else: | |
151 | pass # TODO | |
152 | elif self.state == 'READ RTC REGS2': | |
153 | if cmd == 'DATA READ': | |
154 | handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg) | |
155 | handle_reg(databyte) | |
156 | self.reg += 1 | |
157 | # TODO: Check for NACK! | |
158 | elif cmd == 'STOP': | |
159 | d = '%s, %02d.%02d.%02d %02d:%02d:%02d' % ( | |
160 | days_of_week[self.days - 1], self.date, self.months, | |
161 | self.years, self.hours, self.minutes, self.seconds) | |
162 | self.put(self.block_start_sample, es, self.out_ann, | |
163 | [0, ['Read date/time: %s' % d]]) | |
164 | self.state = 'IDLE' | |
165 | else: | |
166 | pass # TODO? |