2 ## This file is part of the libsigrokdecode project.
4 ## Copyright (C) 2012 Uwe Hermann <uwe@hermann-uwe.de>
5 ## Copyright (C) 2013 Matt Ranostay <mranostay@gmail.com>
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.
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.
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
22 import sigrokdecode as srd
34 # Return the specified BCD number (max. 8 bits) as integer.
36 return (b & 0x0f) + ((b >> 4) * 10)
38 class Decoder(srd.Decoder):
42 longname = 'Dallas DS1307'
43 desc = 'Realtime clock module protocol.'
48 ('text', 'Human-readable text'),
51 def __init__(self, **kwargs):
62 self.out_ann = self.register(srd.OUTPUT_ANN)
65 self.put(self.ss, self.es, self.out_ann, data)
67 def handle_reg_0x00(self, b): # Seconds
68 self.seconds = bcd2int(b & 0x7f)
69 self.putx([0, ['Seconds: %d' % self.seconds]])
71 def handle_reg_0x01(self, b): # Minutes
72 self.minutes = bcd2int(b & 0x7f)
73 self.putx([0, ['Minutes: %d' % self.minutes]])
75 def handle_reg_0x02(self, b): # Hours
76 self.hours = bcd2int(b & 0x3f)
77 self.putx([0, ['Hours: %d' % self.hours]])
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]]])
83 def handle_reg_0x04(self, b): # Date
84 self.date = bcd2int(b & 0x3f)
85 self.putx([0, ['Days: %d' % self.date]])
87 def handle_reg_0x05(self, b): # Month
88 self.months = bcd2int(b & 0x1f)
89 self.putx([0, ['Months: %d' % self.months]])
91 def handle_reg_0x06(self, b): # Year
92 self.years = bcd2int(b & 0xff) + 2000;
93 self.putx([0, ['Years: %d' % self.years]])
95 def handle_reg_0x07(self, b): # Control Register
98 def decode(self, ss, es, data):
101 # Store the start/end samples of this I²C packet.
102 self.ss, self.es = ss, es
105 if self.state == 'IDLE':
106 # Wait for an I²C START condition.
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':
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':
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'
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)
133 # TODO: Check for NACK!
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]])
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'
152 elif self.state == 'READ RTC REGS2':
153 if cmd == 'DATA READ':
154 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
157 # TODO: Check for NACK!
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]])
168 raise Exception('Invalid state: %s' % self.state)