]> sigrok.org Git - libsigrokdecode.git/blame - decoders/ds1307/pd.py
The start() method no longer takes a metadata parameter
[libsigrokdecode.git] / decoders / ds1307 / pd.py
CommitLineData
3bf68998 1##
50bd5d25 2## This file is part of the libsigrokdecode project.
3bf68998
MR
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# Dallas DS1307 RTC protocol decoder
23
24import sigrokdecode as srd
25
26days_of_week = [
27 'Sunday',
28 'Monday',
29 'Tuesday',
30 'Wednesday',
31 'Thursday',
32 'Friday',
33 'Saturday',
34]
35
36# Return the specified BCD number (max. 8 bits) as integer.
37def bcd2int(b):
38 return (b & 0x0f) + ((b >> 4) * 10)
39
40class Decoder(srd.Decoder):
41 api_version = 1
42 id = 'ds1307'
43 name = 'DS1307'
44 longname = 'Dallas DS1307'
45 desc = 'Realtime clock module protocol.'
46 license = 'gplv2+'
47 inputs = ['i2c']
48 outputs = ['ds1307']
49 probes = []
50 optional_probes = []
51 options = {}
52 annotations = [
53 ['Text', 'Human-readable text'],
54 ]
55
56 def __init__(self, **kwargs):
57 self.state = 'IDLE'
58 self.hours = -1
59 self.minutes = -1
60 self.seconds = -1
61 self.days = -1
62 self.date = -1
63 self.months = -1
64 self.years = -1
65
8915b346 66 def start(self):
3bf68998
MR
67 self.out_ann = self.add(srd.OUTPUT_ANN, 'ds1307')
68
69 def report(self):
70 pass
71
72 def putx(self, data):
73 self.put(self.ss, self.es, self.out_ann, data)
74
75 def handle_reg_0x00(self, b): # Seconds
76 self.seconds = bcd2int(b & 0x7f)
77 self.putx([0, ['Seconds: %d' % self.seconds]])
78
79 def handle_reg_0x01(self, b): # Minutes
80 self.minutes = bcd2int(b & 0x7f)
81 self.putx([0, ['Minutes: %d' % self.minutes]])
82
83 def handle_reg_0x02(self, b): # Hours
84 self.hours = bcd2int(b & 0x3f)
85 self.putx([0, ['Hours: %d' % self.hours]])
86
87 def handle_reg_0x03(self, b): # Day of week
88 self.days = bcd2int(b & 0x7)
89 self.putx([0, ['Day of Week: %s' % days_of_week[self.days - 1]]])
90
91 def handle_reg_0x04(self, b): # Date
92 self.date = bcd2int(b & 0x3f)
93 self.putx([0, ['Days: %d' % self.date]])
94
95 def handle_reg_0x05(self, b): # Month
96 self.months = bcd2int(b & 0x1f)
97 self.putx([0, ['Months: %d' % self.months]])
98
99 def handle_reg_0x06(self, b): # Year
100 self.years = bcd2int(b & 0xff) + 2000;
101 self.putx([0, ['Years: %d' % self.years]])
102
103 def handle_reg_0x07(self, b): # Control Register
104 pass
105
106 def decode(self, ss, es, data):
107 cmd, databyte = data
108
109 # Store the start/end samples of this I2C packet.
110 self.ss, self.es = ss, es
111
112 # State machine.
113 if self.state == 'IDLE':
114 # Wait for an I2C START condition.
115 if cmd != 'START':
116 return
117 self.state = 'GET SLAVE ADDR'
118 self.block_start_sample = ss
119 elif self.state == 'GET SLAVE ADDR':
120 # Wait for an address write operation.
121 # TODO: We should only handle packets to the RTC slave (0x68).
122 if cmd != 'ADDRESS WRITE':
123 return
124 self.state = 'GET REG ADDR'
125 elif self.state == 'GET REG ADDR':
126 # Wait for a data write (master selects the slave register).
127 if cmd != 'DATA WRITE':
128 return
129 self.reg = databyte
130 self.state = 'WRITE RTC REGS'
131 elif self.state == 'WRITE RTC REGS':
132 # If we see a Repeated Start here, it's probably an RTC read.
133 if cmd == 'START REPEAT':
134 self.state = 'READ RTC REGS'
135 return
136 # Otherwise: Get data bytes until a STOP condition occurs.
137 if cmd == 'DATA WRITE':
138 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
139 handle_reg(databyte)
140 self.reg += 1
141 # TODO: Check for NACK!
142 elif cmd == 'STOP':
143 # TODO: Handle read/write of only parts of these items.
8b97d010
UH
144 d = '%s, %02d.%02d.%02d %02d:%02d:%02d' % (
145 days_of_week[self.days - 1], self.date, self.months,
146 self.years, self.hours, self.minutes, self.seconds)
3bf68998
MR
147 self.put(self.block_start_sample, es, self.out_ann,
148 [0, ['Written date/time: %s' % d]])
149 self.state = 'IDLE'
150 else:
151 pass # TODO
152 elif self.state == 'READ RTC REGS':
153 # Wait for an address read operation.
154 # TODO: We should only handle packets to the RTC slave (0x68).
155 if cmd == 'ADDRESS READ':
156 self.state = 'READ RTC REGS2'
157 return
158 else:
159 pass # TODO
160 elif self.state == 'READ RTC REGS2':
161 if cmd == 'DATA READ':
162 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
163 handle_reg(databyte)
164 self.reg += 1
165 # TODO: Check for NACK!
166 elif cmd == 'STOP':
8b97d010
UH
167 d = '%s, %02d.%02d.%02d %02d:%02d:%02d' % (
168 days_of_week[self.days - 1], self.date, self.months,
169 self.years, self.hours, self.minutes, self.seconds)
3bf68998
MR
170 self.put(self.block_start_sample, es, self.out_ann,
171 [0, ['Read date/time: %s' % d]])
172 self.state = 'IDLE'
173 else:
174 pass # TODO?
175 else:
176 raise Exception('Invalid state: %s' % self.state)
177