#!/usr/bin/env python
import serial, sys, argparse, time
# parse command line parameters
parser = argparse.ArgumentParser(description='Command line interface for the arduino based coreboot test interface board')
parser.add_argument('devicePath', help='Device path e.g. /dev/ttyUSB0')
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')
args = parser.parse_args()
def main(devicePath, operation):
# establish connection
try:
# create an instance of TIB serial device
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)
time.sleep(0.1) # to give some time before a command is executed
except:
print 'Connection failed'
sys.exit(1)
# execute given operation
try:
if operation=='getVCCP':
# talk and get response
response = interpret_string(talk(TIB, 'P', 4))
# check validity of response and print output
if response >= 0 and response < 50:
print response
else:
raise
elif operation=='getVCCF':
# talk and get response
response = interpret_string(talk(TIB, 'F', 4))
# check validity of response and print output
if response >= 0 and response < 50:
print response
else:
raise
elif operation=='getVCCM':
# talk and get response
response = interpret_string(talk(TIB, 'M', 4))
# check validity of response and print output
if response >= 0 and response < 50:
print response
else:
raise
elif operation=='getA3':
# talk and get response
response = interpret_string(talk(TIB, '3', 4))
# check validity of response and print output
if (response) >= 0 and response < 50:
print response
else:
raise
elif operation=='getA4':
# talk and get response
response = interpret_string(talk(TIB, '4', 4))
# check validity of response and print output
if response >= 0 and response < 50:
print response
else:
raise
elif operation=='getA5':
# talk and get response
response = interpret_string(talk(TIB, '5', 4))
# check validity of response and print output
if response >= 0 and response < 50:
print response
else:
raise
elif operation=='getVGASyncFreq':
# talk and get response
response = talk(TIB, 'f' ,13)
# check validity of response and print output
if response[0]=='(' and ',' in response and response[-1]==')' in response:
print response
else:
raise
elif operation=='isVGASyncOn':
# talk and get response
response = talk(TIB, 'v', 1)
# check validity of response and print output
if response == '1':
print 'VGA Sync is on'
elif response == '0':
print 'VGA Sync is off'
else:
raise
elif operation=='getICP':
# talk and get response
response = talk(TIB, 'i', 1)
# check validity of response and print output
if response == '1':
print 'ICP is on'
elif response == '0':
print 'ICP is off'
else:
raise
elif operation=='toggleICP':
# talk
talk(TIB, 'I', 0)
elif operation=='pressPWR':
# talk
talk(TIB, 'p', 0)
elif operation=='pressRST':
# talk
talk(TIB, 'r', 0)
elif operation=='longPressPWR':
# talk
talk(TIB, 'ppppppppp', 0)
elif operation=='longPressRST':
# talk
talk(TIB, 'rrrrrrrrr', 0)
elif operation=='getPWRLED':
# talk and get response
response = talk(TIB, 'l', 1)
# check validity of response and print output
if response == '1':
print 'ICP is on'
elif response == '0':
print 'ICP is off'
else:
raise
except:
print 'Operation failed'
print sys.exc_value
sys.exit(2)
def talk(serialDevice, sendString, maxExpectedCharCount):
"""
Sends a string to the given serial device and returns received string.
If the received string length exceeds the maximum expected character count
the function raises an exception
"""
# flush buffers
serialDevice.flushInput(); serialDevice.flushOutput()
# write the given string to the serial device
serialDevice.write(sendString)
receiveString = ''
loopWatch = 0
# if data to be received
if maxExpectedCharCount > 0:
# additional wait for VGA operations
if (sendString == 'f' or sendString == 'v'): time.sleep(1)
# if data available on receive buffer
while serialDevice.inWaiting > 0:
# pop out bytes from the serial buffer and add to string
receiveChar = serialDevice.read(1)
# if escape character detected
if receiveChar == '\r' or receiveChar == '\n':
# flush buffers
serialDevice.flushInput(); serialDevice.flushOutput()
break
else:
# append the character to a string
receiveString += receiveChar
# if received string is longer than expected
if len(receiveString) > maxExpectedCharCount:
# flush buffers
serialDevice.flushInput(); serialDevice.flushOutput()
# raise exception
raise
# loop watcher to avoid getting stuck in the loop
loopWatch += 1
if loopWatch > 20: break
return receiveString
def interpret_string(s):
'''
take a string and try to convert it to int or float as appropriate,
if failed return the input string
'''
if not isinstance(s, str):
return str(s)
if s.isdigit():
return int(s)
try:
return float(s)
except:
return s
if __name__ == "__main__":
main(args.devicePath, args.operation)
Comments