summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNicolas Schodet2010-07-17 17:28:40 +0200
committerNicolas Schodet2010-07-17 17:28:40 +0200
commitf32ef700f277704a8eb89aebf7099d19853364f1 (patch)
tree881f44e069e3a7fec72bbd8726a74f832988cccc
parentfc07db4a6cf35445a025f3e8134341cd683e133e (diff)
tools: add RTC control script
-rw-r--r--tools/rtc.py139
1 files changed, 139 insertions, 0 deletions
diff --git a/tools/rtc.py b/tools/rtc.py
new file mode 100644
index 0000000..7fcacb1
--- /dev/null
+++ b/tools/rtc.py
@@ -0,0 +1,139 @@
+# binwatch - Tiny binary wristwatch. {{{
+#
+# Copyright (C) 2010 Nicolas Schodet
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+#
+# Contact :
+# Web: http://ni.fr.eu.org/
+# Email: <nico at ni.fr.eu.org>
+# }}} */
+"""RTC control."""
+import struct
+import dev2.twi
+
+class ProtocolError (RuntimeError):
+ pass
+
+class RTC:
+
+ REGS = [
+ ('control1', 0xff),
+ ('control2', 0xbf),
+ ('second', 0xff),
+ ('minute', 0x7f),
+ ('hour', 0x3f),
+ ('day', 0x3f),
+ ('weekday', 0x07),
+ ('month_century', 0x9f),
+ ('year', 0xff),
+ ('alarm_minute', 0xff),
+ ('alarm_hour', 0xbf),
+ ('alarm_day', 0xbf),
+ ('alarm_weekday', 0x87),
+ ('clkout', 0x83),
+ ('timer_control', 0x83),
+ ('timer', 0xff),
+ ]
+
+ def __init__ (self, address, serial):
+ """Initialise and set address and serial object to use."""
+ self.address = address
+ self.twi = dev2.twi.Twi (serial)
+
+ def init (self):
+ s = struct.pack ('<B' + 18 * 'B', 0,
+ 0x20, 0, # STOP
+ 0, 0, 0, 0, 0, 0, 0,
+ 0x80, 0x80, 0x80, 0x80,
+ 0, 0, 0,
+ 0, 0) # !STOP
+ n = self.twi.send (self.address, s)
+ if n != len (s):
+ raise ProtocolError ("send init fail")
+
+ def dump (self):
+ # Send address.
+ s = struct.pack ('<B', 0)
+ n = self.twi.send (self.address, s)
+ if n != len (s):
+ raise ProtocolError ("send address fail")
+ # Read all registers.
+ r = self.twi.recv (self.address, 16)
+ if len (r) != 16:
+ raise ProtocolError ("receive fail")
+ rl = struct.unpack ('<' + 16 * 'B', r)
+ # Format result.
+ frl = [ '%s: 0x%02x' % (reg[0], value & reg[1])
+ for (reg, value) in zip (self.REGS, rl) ]
+ return '\n'.join (frl)
+
+ def _command (self, command, fmt, rfmt, *params):
+ """Send a command and check response.
+
+ - command: command to send.
+ - fmt: command format (see struct).
+ - rfmt: response format.
+ - params: command parameters.
+
+ Will return unpacked response or None on error."""
+ s = struct.pack ('<B' + fmt, command, *params)
+ n = self.twi.send (self.address, s)
+ if n != len (s):
+ raise ProtocolError ("send command fail")
+ rlen = struct.calcsize ('<B' + rfmt)
+ r = self.twi.recv (self.address, rlen)
+ if len (r) != rlen:
+ raise ProtocolError ("receive ack fail")
+ rl = struct.unpack ('<B' + rfmt, r)
+ if rl[0] != command:
+ raise ProtocolError ("received nack")
+ return rl[1:]
+
+
+def command (args = None):
+ """Implement command line interface."""
+ # Parse options.
+ import optparse
+ parser = optparse.OptionParser ()
+ parser.add_option ('-i', '--init', action = 'store_true',
+ help = "initialise RTC")
+ parser.add_option ('-d', '--dump', action = 'store_true',
+ help = "dump all registers")
+ parser.add_option ('-t', '--tty',
+ help = "tty connected to dev2", metavar = 'TTY')
+ parser.add_option ('-a', '--address', type = 'int', default = 0xa2,
+ help = "define RTC slave address", metavar = 'ADDR')
+ (options, extra) = parser.parse_args (args = args)
+ if extra:
+ parser.error ("extra unused arguments")
+ if options.tty is None:
+ parser.error ("no tty to open")
+ # Apply.
+ import serial
+ try:
+ s = serial.Serial (options.tty)
+ rtc = RTC (options.address, s)
+ if options.init:
+ rtc.init ()
+ if options.dump:
+ print rtc.dump ()
+ except Exception, e:
+ import sys
+ print >> sys.stderr, e.message
+ sys.exit (1)
+
+if __name__ == '__main__':
+ command ()