grml...
[seabios.git] / tools / readserial.py
index 4524ca831fb7fe7a51ec2695e452bc7ca5ebe6cf..d85392eba67933e85ff58ab1ad53db6dd3759b29 100755 (executable)
 import sys
 import time
 import select
-import serial
+import optparse
 
 # Reset time counter after this much idle time.
 RESTARTINTERVAL = 60
-# Alter timing reports based on how much time would be spent writing
-# to serial.
-ADJUSTBAUD = 1
+# Number of bits in a transmitted byte - 8N1 is 1 start bit + 8 data
+# bits + 1 stop bit.
+BITSPERBYTE = 10
 
-def readserial(infile, logfile, baudrate):
+def calibrateserialwrite(outfile, byteadjust):
+    # Build 4000 bytes of dummy data.
+    data = "0123456789" * 4 + "012345678" + "\n"
+    data = data * 80
+    while 1:
+        st = time.time()
+        outfile.write(data)
+        outfile.flush()
+        et = time.time()
+        sys.stdout.write(
+            "Wrote %d - %.1fus per char (theory states %.1fus)\n" % (
+                len(data), (et-st) / len(data) * 1000000, byteadjust * 1000000))
+        sys.stdout.flush()
+        time.sleep(3)
+
+def calibrateserialread(infile, byteadjust):
+    starttime = lasttime = 0
+    totalchars = 0
+    while 1:
+        select.select([infile], [], [])
+        d = infile.read(4096)
+        curtime = time.time()
+        if curtime - lasttime > 1.0:
+            if starttime and totalchars:
+                sys.stdout.write(
+                    "Calibrating on %d bytes - %.1fus per char"
+                    " (theory states %.1fus)\n" % (
+                        totalchars,
+                        float(lasttime - starttime) * 1000000 / totalchars,
+                        byteadjust * 1000000))
+            totalchars = 0
+            starttime = curtime
+        else:
+            totalchars += len(d)
+        lasttime = curtime
+
+def readserial(infile, logfile, byteadjust):
     lasttime = 0
     while 1:
         # Read data
@@ -34,29 +70,34 @@ def readserial(infile, logfile, baudrate):
             lasttime = 0
             if len(res[0]) == 1:
                 continue
-        curtime = time.time()
         d = infile.read(4096)
+        if not d:
+            break
+        datatime = time.time()
+
+        datatime -= len(d) * byteadjust
 
         # Reset start time if no data for some time
-        if curtime - lasttime > RESTARTINTERVAL:
-            starttime = curtime
+        if datatime - lasttime > RESTARTINTERVAL:
+            starttime = datatime
             charcount = 0
             isnewline = 1
-            sys.stdout.write("\n")
-            logfile.write("\n")
-        lasttime = curtime
+            msg = "\n\n======= %s (adjust=%.1fus)\n" % (
+                time.asctime(time.localtime(datatime)), byteadjust * 1000000)
+            sys.stdout.write(msg)
+            logfile.write(msg)
+        lasttime = datatime
 
         # Translate unprintable chars; add timestamps
         out = ""
         for c in d:
             if isnewline:
-                delta = curtime - starttime
-                if ADJUSTBAUD:
-                    delta -= float(charcount * 9) / baudrate
+                delta = datatime - starttime - (charcount * byteadjust)
                 out += "%06.3f: " % delta
                 isnewline = 0
             oc = ord(c)
             charcount += 1
+            datatime += byteadjust
             if oc == 0x0d:
                 continue
             if oc == 0x00:
@@ -77,25 +118,69 @@ def readserial(infile, logfile, baudrate):
         logfile.write(out)
         logfile.flush()
 
-def printUsage():
-    print "Usage:\n   %s [<serialdevice> [<baud>]]" % (sys.argv[0],)
-    sys.exit(1)
-
 def main():
+    usage = "%prog [options] [<serialdevice> [<baud>]]"
+    opts = optparse.OptionParser(usage)
+    opts.add_option("-f", "--file",
+                    action="store_false", dest="serial", default=True,
+                    help="read from file instead of serialdevice")
+    opts.add_option("-n", "--no-adjust",
+                    action="store_false", dest="adjustbaud", default=True,
+                    help="don't adjust times by serial rate")
+    opts.add_option("-c", "--calibrate-read",
+                    action="store_true", dest="calibrate_read", default=False,
+                    help="read from serial port to calibrate it")
+    opts.add_option("-C", "--calibrate-write",
+                    action="store_true", dest="calibrate_write", default=False,
+                    help="write to serial port to calibrate it")
+    opts.add_option("-t", "--time",
+                    type="float", dest="time", default=None,
+                    help="time to write one byte on serial port (in us)")
+    options, args = opts.parse_args()
     serialport = 0
     baud = 115200
-    if len(sys.argv) > 3:
-        printUsage()
-    if len(sys.argv) > 1:
-        serialport = sys.argv[1]
-    if len(sys.argv) > 2:
-        baud = int(sys.argv[2])
+    if len(args) > 2:
+        opts.error("Too many arguments")
+    if len(args) > 0:
+        serialport = args[0]
+    if len(args) > 1:
+        baud = int(args[1])
+    byteadjust = float(BITSPERBYTE) / baud
+    if options.time is not None:
+        byteadjust = options.time / 1000000.0
+    if not options.adjustbaud:
+        byteadjust = 0.0
+
+    if options.serial:
+        # Read from serial port
+        try:
+            import serial
+        except ImportError:
+            print """
+Unable to find pyserial package ( http://pyserial.sourceforge.net/ ).
+On Linux machines try: yum install pyserial
+Or: apt-get install python-serial
+"""
+            sys.exit(1)
+        ser = serial.Serial(serialport, baud, timeout=0)
+    else:
+        # Read from a file
+        ser = open(serialport, 'rb')
+        import fcntl
+        import os
+        fcntl.fcntl(ser, fcntl.F_SETFL
+                    , fcntl.fcntl(ser, fcntl.F_GETFL) | os.O_NONBLOCK)
 
-    ser = serial.Serial(serialport, baud, timeout=0)
+    if options.calibrate_read:
+        calibrateserialread(ser, byteadjust)
+        return
+    if options.calibrate_write:
+        calibrateserialwrite(ser, byteadjust)
+        return
 
     logname = time.strftime("seriallog-%Y%m%d_%H%M%S.log")
     f = open(logname, 'wb')
-    readserial(ser, f, baud)
+    readserial(ser, f, byteadjust)
 
 if __name__ == '__main__':
     main()