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.'
51 ['text', 'Human-readable text'],
54 def __init__(self, **kwargs):
65 self.out_ann = self.register(srd.OUTPUT_ANN)
68 self.put(self.ss, self.es, self.out_ann, data)
70 def handle_reg_0x00(self, b): # Seconds
71 self.seconds = bcd2int(b & 0x7f)
72 self.putx([0, ['Seconds: %d' % self.seconds]])
74 def handle_reg_0x01(self, b): # Minutes
75 self.minutes = bcd2int(b & 0x7f)
76 self.putx([0, ['Minutes: %d' % self.minutes]])
78 def handle_reg_0x02(self, b): # Hours
79 self.hours = bcd2int(b & 0x3f)
80 self.putx([0, ['Hours: %d' % self.hours]])
82 def handle_reg_0x03(self, b): # Day of week
83 self.days = bcd2int(b & 0x7)
84 self.putx([0, ['Day of Week: %s' % days_of_week[self.days - 1]]])
86 def handle_reg_0x04(self, b): # Date
87 self.date = bcd2int(b & 0x3f)
88 self.putx([0, ['Days: %d' % self.date]])
90 def handle_reg_0x05(self, b): # Month
91 self.months = bcd2int(b & 0x1f)
92 self.putx([0, ['Months: %d' % self.months]])
94 def handle_reg_0x06(self, b): # Year
95 self.years = bcd2int(b & 0xff) + 2000;
96 self.putx([0, ['Years: %d' % self.years]])
98 def handle_reg_0x07(self, b): # Control Register
101 def decode(self, ss, es, data):
104 # Store the start/end samples of this I²C packet.
105 self.ss, self.es = ss, es
108 if self.state == 'IDLE':
109 # Wait for an I²C START condition.
112 self.state = 'GET SLAVE ADDR'
113 self.block_start_sample = ss
114 elif self.state == 'GET SLAVE ADDR':
115 # Wait for an address write operation.
116 # TODO: We should only handle packets to the RTC slave (0x68).
117 if cmd != 'ADDRESS WRITE':
119 self.state = 'GET REG ADDR'
120 elif self.state == 'GET REG ADDR':
121 # Wait for a data write (master selects the slave register).
122 if cmd != 'DATA WRITE':
125 self.state = 'WRITE RTC REGS'
126 elif self.state == 'WRITE RTC REGS':
127 # If we see a Repeated Start here, it's probably an RTC read.
128 if cmd == 'START REPEAT':
129 self.state = 'READ RTC REGS'
131 # Otherwise: Get data bytes until a STOP condition occurs.
132 if cmd == 'DATA WRITE':
133 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
136 # TODO: Check for NACK!
138 # TODO: Handle read/write of only parts of these items.
139 d = '%s, %02d.%02d.%02d %02d:%02d:%02d' % (
140 days_of_week[self.days - 1], self.date, self.months,
141 self.years, self.hours, self.minutes, self.seconds)
142 self.put(self.block_start_sample, es, self.out_ann,
143 [0, ['Written date/time: %s' % d]])
147 elif self.state == 'READ RTC REGS':
148 # Wait for an address read operation.
149 # TODO: We should only handle packets to the RTC slave (0x68).
150 if cmd == 'ADDRESS READ':
151 self.state = 'READ RTC REGS2'
155 elif self.state == 'READ RTC REGS2':
156 if cmd == 'DATA READ':
157 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
160 # TODO: Check for NACK!
162 d = '%s, %02d.%02d.%02d %02d:%02d:%02d' % (
163 days_of_week[self.days - 1], self.date, self.months,
164 self.years, self.hours, self.minutes, self.seconds)
165 self.put(self.block_start_sample, es, self.out_ann,
166 [0, ['Read date/time: %s' % d]])
171 raise Exception('Invalid state: %s' % self.state)