Advertisement
ayush3504

coreboot TIB script

Sep 16th, 2013
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 6.91 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import serial, sys, argparse, time
  4.  
  5. # parse command line parameters
  6. parser = argparse.ArgumentParser(description='Command line interface for the arduino based coreboot test interface board')
  7. parser.add_argument('devicePath', help='Device path e.g. /dev/ttyUSB0')
  8. parser.add_argument('operation', choices=['getVCCP','getVCCF','getVCCM','getA3','getA4','getA5','getVGASyncFreq','isVGASyncOn','getICP','toggleICP','pressPWR','pressRST','longPressPWR', 'longPressRST','getPWRLED'], help='command character to be sent')
  9. args = parser.parse_args()
  10.  
  11. def main(devicePath, operation):
  12.     # establish connection
  13.     try:
  14.         # create an instance of TIB serial device
  15.         TIB = serial.Serial(devicePath, timeout=0.1) # arguments (port=None, baudrate=9600, bytesize=EIGHTBITS, parity=PARITY_NONE, stopbits=STOPBITS_ONE, timeout=None, xonxoff=False, rtscts=False, writeTimeout=None, dsrdtr=False, interCharTimeout=None)
  16.         time.sleep(0.1) # to give some time before a command is executed
  17.     except:
  18.         print 'Connection failed'
  19.         sys.exit(1)
  20.  
  21.     # execute given operation
  22.     try:
  23.         if operation=='getVCCP':
  24.             # talk and get response
  25.             response = interpret_string(talk(TIB, 'P', 4))
  26.             # check validity of response and print output
  27.             if response >= 0 and response < 50:
  28.                 print response
  29.             else:
  30.                 raise
  31.         elif operation=='getVCCF':
  32.             # talk and get response
  33.             response = interpret_string(talk(TIB, 'F', 4))
  34.             # check validity of response and print output
  35.             if response >= 0 and response < 50:
  36.                 print response
  37.             else:
  38.                 raise
  39.         elif operation=='getVCCM':
  40.             # talk and get response
  41.             response = interpret_string(talk(TIB, 'M', 4))
  42.             # check validity of response and print output
  43.             if response >= 0 and response < 50:
  44.                 print response
  45.             else:
  46.                 raise
  47.         elif operation=='getA3':
  48.             # talk and get response
  49.             response = interpret_string(talk(TIB, '3', 4))
  50.             # check validity of response and print output
  51.             if (response) >= 0 and response < 50:
  52.                 print response
  53.             else:
  54.                 raise
  55.         elif operation=='getA4':
  56.             # talk and get response
  57.             response = interpret_string(talk(TIB, '4', 4))
  58.             # check validity of response and print output
  59.             if response >= 0 and response < 50:
  60.                 print response
  61.             else:
  62.                 raise
  63.         elif operation=='getA5':
  64.             # talk and get response
  65.             response = interpret_string(talk(TIB, '5', 4))
  66.             # check validity of response and print output
  67.             if response >= 0 and response < 50:
  68.                 print response
  69.             else:
  70.                 raise
  71.         elif operation=='getVGASyncFreq':
  72.             # talk and get response
  73.             response = talk(TIB, 'f' ,13)
  74.             # check validity of response and print output
  75.             if response[0]=='(' and ',' in response and response[-1]==')' in response:
  76.                 print response
  77.             else:
  78.                 raise
  79.         elif operation=='isVGASyncOn':
  80.             # talk and get response
  81.             response = talk(TIB, 'v', 1)
  82.             # check validity of response and print output
  83.             if response == '1':
  84.                 print 'VGA Sync is on'
  85.             elif response == '0':
  86.                 print 'VGA Sync is off'
  87.             else:
  88.                 raise
  89.         elif operation=='getICP':
  90.             # talk and get response
  91.             response = talk(TIB, 'i', 1)
  92.             # check validity of response and print output
  93.             if response == '1':
  94.                 print 'ICP is on'
  95.             elif response == '0':
  96.                 print 'ICP is off'
  97.             else:
  98.                 raise
  99.         elif operation=='toggleICP':
  100.             # talk
  101.             talk(TIB, 'I', 0)
  102.         elif operation=='pressPWR':
  103.             # talk
  104.             talk(TIB, 'p', 0)
  105.         elif operation=='pressRST':
  106.             # talk
  107.             talk(TIB, 'r', 0)
  108.         elif operation=='longPressPWR':
  109.             # talk
  110.             talk(TIB, 'ppppppppp', 0)
  111.         elif operation=='longPressRST':
  112.             # talk
  113.             talk(TIB, 'rrrrrrrrr', 0)
  114.         elif operation=='getPWRLED':
  115.             # talk and get response
  116.             response = talk(TIB, 'l', 1)
  117.             # check validity of response and print output
  118.             if response == '1':
  119.                 print 'ICP is on'
  120.             elif response == '0':
  121.                 print 'ICP is off'
  122.             else:
  123.                 raise
  124.              
  125.     except:
  126.         print 'Operation failed'
  127.         print sys.exc_value
  128.         sys.exit(2)
  129.  
  130. def talk(serialDevice, sendString, maxExpectedCharCount):
  131.     """
  132.    Sends a string to the given serial device and returns received string.
  133.    If the received string length exceeds the maximum expected character count
  134.    the function raises an exception
  135.    """
  136.     # flush buffers
  137.     serialDevice.flushInput(); serialDevice.flushOutput()
  138.     # write the given string to the serial device
  139.     serialDevice.write(sendString)
  140.     receiveString = ''
  141.     loopWatch = 0
  142.     # if data to be received
  143.     if maxExpectedCharCount > 0:
  144.         # additional wait for VGA operations
  145.         if (sendString == 'f' or sendString == 'v'): time.sleep(1)
  146.         # if data available on receive buffer
  147.         while serialDevice.inWaiting > 0:
  148.             # pop out bytes from the serial buffer and add to string
  149.             receiveChar = serialDevice.read(1)
  150.             # if escape character detected
  151.             if receiveChar == '\r' or receiveChar == '\n':
  152.                 # flush buffers
  153.                 serialDevice.flushInput(); serialDevice.flushOutput()
  154.                 break
  155.             else:
  156.                 # append the character to a string
  157.                 receiveString += receiveChar
  158.             # if received string is longer than expected
  159.             if len(receiveString) > maxExpectedCharCount:
  160.                 # flush buffers
  161.                 serialDevice.flushInput(); serialDevice.flushOutput()
  162.                 # raise exception
  163.                 raise
  164.             # loop watcher to avoid getting stuck in the loop
  165.             loopWatch += 1
  166.             if loopWatch > 20: break
  167.     return receiveString
  168.  
  169. def interpret_string(s):
  170.     '''
  171.    take a string and try to convert it to int or float as appropriate,
  172.    if failed return the input string
  173.    '''
  174.     if not isinstance(s, str):
  175.         return str(s)
  176.     if s.isdigit():
  177.         return int(s)
  178.     try:
  179.         return float(s)
  180.     except:
  181.         return s
  182.  
  183. if __name__ == "__main__":
  184.     main(args.devicePath, args.operation)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement