]> sigrok.org Git - libsigrokdecode.git/blob - decoders/ds1307/pd.py
bb904e7e4d17d0f9cddd91c744bca2a02d016039
[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 # Dallas DS1307 RTC protocol decoder
23
24 import sigrokdecode as srd
25
26 days_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.
37 def bcd2int(b):
38     return (b & 0x0f) + ((b >> 4) * 10)
39
40 class 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
66     def start(self):
67         self.out_ann = self.register(srd.OUTPUT_ANN)
68
69     def putx(self, data):
70         self.put(self.ss, self.es, self.out_ann, data)
71
72     def handle_reg_0x00(self, b): # Seconds
73         self.seconds = bcd2int(b & 0x7f)
74         self.putx([0, ['Seconds: %d' % self.seconds]])
75
76     def handle_reg_0x01(self, b): # Minutes
77         self.minutes = bcd2int(b & 0x7f)
78         self.putx([0, ['Minutes: %d' % self.minutes]])
79
80     def handle_reg_0x02(self, b): # Hours
81         self.hours = bcd2int(b & 0x3f)
82         self.putx([0, ['Hours: %d' % self.hours]])
83
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]]])
87
88     def handle_reg_0x04(self, b): # Date
89         self.date =  bcd2int(b & 0x3f)
90         self.putx([0, ['Days: %d' % self.date]])
91
92     def handle_reg_0x05(self, b): # Month
93         self.months = bcd2int(b & 0x1f)
94         self.putx([0, ['Months: %d' % self.months]])
95
96     def handle_reg_0x06(self, b): # Year
97         self.years = bcd2int(b & 0xff) + 2000;
98         self.putx([0, ['Years: %d' % self.years]])
99
100     def handle_reg_0x07(self, b): # Control Register
101         pass
102
103     def decode(self, ss, es, data):
104         cmd, databyte = data
105
106         # Store the start/end samples of this I2C packet.
107         self.ss, self.es = ss, es
108
109         # State machine.
110         if self.state == 'IDLE':
111             # Wait for an I2C START condition.
112             if cmd != 'START':
113                 return
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':
120                 return
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':
125                 return
126             self.reg = databyte
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'
132                 return
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)
136                 handle_reg(databyte)
137                 self.reg += 1
138                 # TODO: Check for NACK!
139             elif cmd == 'STOP':
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]])
146                 self.state = 'IDLE'
147             else:
148                 pass # TODO
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'
154                 return
155             else:
156                 pass # TODO
157         elif self.state == 'READ RTC REGS2':
158             if cmd == 'DATA READ':
159                 handle_reg = getattr(self, 'handle_reg_0x%02x' % self.reg)
160                 handle_reg(databyte)
161                 self.reg += 1
162                 # TODO: Check for NACK!
163             elif cmd == 'STOP':
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]])
169                 self.state = 'IDLE'
170             else:
171                 pass # TODO?
172         else:
173             raise Exception('Invalid state: %s' % self.state)
174