]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ds1307/pd.py
All PDs: Consistent naming/case for annotation shortnames/IDs.
[libsigrokdecode.git] / decoders / ds1307 / pd.py
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 = 1
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     probes = []
48     optional_probes = []
49     options = {}
50     annotations = [
51         ['text', 'Human-readable text'],
52     ]
53
54     def __init__(self, **kwargs):
55         self.state = 'IDLE'
56         self.hours = -1
57         self.minutes = -1
58         self.seconds = -1
59         self.days = -1
60         self.date = -1
61         self.months = -1
62         self.years = -1
63
64     def start(self):
65         self.out_ann = self.register(srd.OUTPUT_ANN)
66
67     def putx(self, data):
68         self.put(self.ss, self.es, self.out_ann, data)
69
70     def handle_reg_0x00(self, b): # Seconds
71         self.seconds = bcd2int(b & 0x7f)
72         self.putx([0, ['Seconds: %d' % self.seconds]])
73
74     def handle_reg_0x01(self, b): # Minutes
75         self.minutes = bcd2int(b & 0x7f)
76         self.putx([0, ['Minutes: %d' % self.minutes]])
77
78     def handle_reg_0x02(self, b): # Hours
79         self.hours = bcd2int(b & 0x3f)
80         self.putx([0, ['Hours: %d' % self.hours]])
81
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]]])
85
86     def handle_reg_0x04(self, b): # Date
87         self.date =  bcd2int(b & 0x3f)
88         self.putx([0, ['Days: %d' % self.date]])
89
90     def handle_reg_0x05(self, b): # Month
91         self.months = bcd2int(b & 0x1f)
92         self.putx([0, ['Months: %d' % self.months]])
93
94     def handle_reg_0x06(self, b): # Year
95         self.years = bcd2int(b & 0xff) + 2000;
96         self.putx([0, ['Years: %d' % self.years]])
97
98     def handle_reg_0x07(self, b): # Control Register
99         pass
100
101     def decode(self, ss, es, data):
102         cmd, databyte = data
103
104         # Store the start/end samples of this I²C packet.
105         self.ss, self.es = ss, es
106
107         # State machine.
108         if self.state == 'IDLE':
109             # Wait for an I²C START condition.
110             if cmd != 'START':
111                 return
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':
118                 return
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':
123                 return
124             self.reg = databyte
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'
130                 return
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)
134                 handle_reg(databyte)
135                 self.reg += 1
136                 # TODO: Check for NACK!
137             elif cmd == 'STOP':
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]])
144                 self.state = 'IDLE'
145             else:
146                 pass # TODO
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'
152                 return
153             else:
154                 pass # TODO
155         elif self.state == 'READ RTC REGS2':
156             if cmd == 'DATA READ':
157                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
158                 handle_reg(databyte)
159                 self.reg += 1
160                 # TODO: Check for NACK!
161             elif cmd == 'STOP':
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]])
167                 self.state = 'IDLE'
168             else:
169                 pass # TODO?
170         else:
171             raise Exception('Invalid state: %s' % self.state)
172