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 # Dallas DS1307 RTC protocol decoder
24 import sigrokdecode as srd
36 # Return the specified BCD number (max. 8 bits) as integer.
38 return (b & 0x0f) + ((b >> 4) * 10)
40 class Decoder(srd.Decoder):
44 longname = 'Dallas DS1307'
45 desc = 'Realtime clock module protocol.'
53 ['Text', 'Human-readable text'],
56 def __init__(self, **kwargs):
67 self.out_ann = self.register(srd.OUTPUT_ANN)
70 self.put(self.ss, self.es, self.out_ann, data)
72 def handle_reg_0x00(self, b): # Seconds
73 self.seconds = bcd2int(b & 0x7f)
74 self.putx([0, ['Seconds: %d' % self.seconds]])
76 def handle_reg_0x01(self, b): # Minutes
77 self.minutes = bcd2int(b & 0x7f)
78 self.putx([0, ['Minutes: %d' % self.minutes]])
80 def handle_reg_0x02(self, b): # Hours
81 self.hours = bcd2int(b & 0x3f)
82 self.putx([0, ['Hours: %d' % self.hours]])
84 def handle_reg_0x03(self, b): # Day of week
85 self.days = bcd2int(b & 0x7)
86 self.putx([0, ['Day of Week: %s' % days_of_week[self.days - 1]]])
88 def handle_reg_0x04(self, b): # Date
89 self.date = bcd2int(b & 0x3f)
90 self.putx([0, ['Days: %d' % self.date]])
92 def handle_reg_0x05(self, b): # Month
93 self.months = bcd2int(b & 0x1f)
94 self.putx([0, ['Months: %d' % self.months]])
96 def handle_reg_0x06(self, b): # Year
97 self.years = bcd2int(b & 0xff) + 2000;
98 self.putx([0, ['Years: %d' % self.years]])
100 def handle_reg_0x07(self, b): # Control Register
103 def decode(self, ss, es, data):
106 # Store the start/end samples of this I²C packet.
107 self.ss, self.es = ss, es
110 if self.state == 'IDLE':
111 # Wait for an I²C START condition.
114 self.state = 'GET SLAVE ADDR'
115 self.block_start_sample = ss
116 elif self.state == 'GET SLAVE ADDR':
117 # Wait for an address write operation.
118 # TODO: We should only handle packets to the RTC slave (0x68).
119 if cmd != 'ADDRESS WRITE':
121 self.state = 'GET REG ADDR'
122 elif self.state == 'GET REG ADDR':
123 # Wait for a data write (master selects the slave register).
124 if cmd != 'DATA WRITE':
127 self.state = 'WRITE RTC REGS'
128 elif self.state == 'WRITE RTC REGS':
129 # If we see a Repeated Start here, it's probably an RTC read.
130 if cmd == 'START REPEAT':
131 self.state = 'READ RTC REGS'
133 # Otherwise: Get data bytes until a STOP condition occurs.
134 if cmd == 'DATA WRITE':
135 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
138 # TODO: Check for NACK!
140 # TODO: Handle read/write of only parts of these items.
141 d = '%s, %02d.%02d.%02d %02d:%02d:%02d' % (
142 days_of_week[self.days - 1], self.date, self.months,
143 self.years, self.hours, self.minutes, self.seconds)
144 self.put(self.block_start_sample, es, self.out_ann,
145 [0, ['Written date/time: %s' % d]])
149 elif self.state == 'READ RTC REGS':
150 # Wait for an address read operation.
151 # TODO: We should only handle packets to the RTC slave (0x68).
152 if cmd == 'ADDRESS READ':
153 self.state = 'READ RTC REGS2'
157 elif self.state == 'READ RTC REGS2':
158 if cmd == 'DATA READ':
159 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
162 # TODO: Check for NACK!
164 d = '%s, %02d.%02d.%02d %02d:%02d:%02d' % (
165 days_of_week[self.days - 1], self.date, self.months,
166 self.years, self.hours, self.minutes, self.seconds)
167 self.put(self.block_start_sample, es, self.out_ann,
168 [0, ['Read date/time: %s' % d]])
173 raise Exception('Invalid state: %s' % self.state)