USB2SerialMux/tools/muxctrl.py

445 lines
11 KiB
Python
Raw Permalink Normal View History

2016-08-29 17:48:24 +02:00
###############################################################################
import argparse
import threading
import time
import serial
import copy
import binascii
2016-08-29 17:48:24 +02:00
###############################################################################
parser = argparse.ArgumentParser(description='USB2SerialMux control helper tool.')
parser.add_argument("-b", "--getBaudrate", default=False, help="Get the baudrate for the USART connection.", action='store_true')
2016-08-30 16:48:55 +02:00
parser.add_argument("-s", "--setBaudrate", type=int, help="Something like 9600 or 115200.")
parser.add_argument("-m", "--getMuxLine", default=False, help="Get the current multiplexer control line state.", action='store_true')
parser.add_argument("-l", "--setMuxLine", type=int, help="Something like 0 to 7.")
parser.add_argument("-r", "--resetToBtldr", default=False, help="Reset the device to the LUFA bootloader.", action='store_true')
parser.add_argument("-d", "--device", type=str, help="The control device like /dev/ttyACM0 or COM3.")
2016-08-29 17:48:24 +02:00
###############################################################################
MSG_SOD1 = 0x3c
MSG_SOD2 = 0x3e
MSG_EOD1 = 0x0d
MSG_EOD2 = 0x0a
MSG_TYPE_ANSWER_OK = 0x01
MSG_TYPE_ANSWER_NOK = 0x02
MSG_TYPE_BAUDRATE = 0x03
MSG_TYPE_MUXLINE = 0x04
2016-08-30 16:48:55 +02:00
MSG_TYPE_ANSWER_OK_DATA_TO_RECV = 0
MSG_TYPE_ANSWER_NOK_DATA_TO_RECV = 0
MSG_TYPE_BAUDRATE_DATA_TO_RECV = 4
MSG_TYPE_MUXLINE_DATA_TO_RECV = 1
2016-08-29 17:48:24 +02:00
CC_CMD_SET_BAUDRATE = 0x0A
CC_CMD_GET_BAUDRATE = 0x14
CC_CMD_SET_MUX_LINE = 0x1E
CC_CMD_GET_MUX_LINE = 0x28
CC_CMD_START_BTLDR = 0x32
###############################################################################
2016-08-30 16:48:55 +02:00
TIMEOUT_CNT_MAX = 50
2016-08-29 17:48:24 +02:00
MAIN_LOOP_DELAY_S = 0.05
THREAD_LOOP_DELAY_S = 0.01
###############################################################################
2016-08-30 16:48:55 +02:00
MUX_MIN_VAL = 0
MUX_MAX_VAL = 7
###############################################################################
ser = None
device = "/dev/ttyACM0"
###############################################################################
2016-08-29 17:48:24 +02:00
baudrate = 0xffffffff
muxLine = 0xff
###############################################################################
CC_STATE_WAIT_SOD1 = 0x01
CC_STATE_WAIT_SOD2 = 0x02
CC_STATE_READ_DATA = 0x03
CC_STATE_WAIT_EOD1 = 0x04
CC_STATE_WAIT_EOD2 = 0x05
2016-08-30 16:48:55 +02:00
CC_STATE_GET_TYPE = 0x06
2016-08-29 17:48:24 +02:00
cc_state = CC_STATE_WAIT_SOD1
2016-08-30 16:48:55 +02:00
cc_state_list = [ CC_STATE_WAIT_SOD1,
CC_STATE_WAIT_SOD2,
CC_STATE_READ_DATA,
CC_STATE_WAIT_EOD1,
CC_STATE_WAIT_EOD2 ]
cc_state_fn = {}
msg_type_list = [ MSG_TYPE_ANSWER_OK,
MSG_TYPE_ANSWER_NOK,
MSG_TYPE_BAUDRATE,
MSG_TYPE_MUXLINE, ]
msg_type_data_to_read = { MSG_TYPE_ANSWER_OK : MSG_TYPE_ANSWER_OK_DATA_TO_RECV,
MSG_TYPE_ANSWER_NOK : MSG_TYPE_ANSWER_NOK_DATA_TO_RECV,
MSG_TYPE_BAUDRATE : MSG_TYPE_BAUDRATE_DATA_TO_RECV,
MSG_TYPE_MUXLINE : MSG_TYPE_MUXLINE_DATA_TO_RECV, }
msg_type = 0
2016-08-29 17:48:24 +02:00
cc_data_read = 0
cc_data_buffer = []
# yes a separate counter to manage the order of the received messages
cc_message_cnt = 0
cc_received_messages = []
###############################################################################
2016-08-30 16:48:55 +02:00
thread_obj = None
thread_lock = None
thread_started = False
thread_stop = False
2016-08-29 17:48:24 +02:00
###############################################################################
2016-08-30 16:48:55 +02:00
def cc_init():
global cc_state_fn
global cc_state
cc_state = CC_STATE_WAIT_SOD1
cc_state_fn = { CC_STATE_WAIT_SOD1 : cc_state_fn_wait_for_sod1,
CC_STATE_WAIT_SOD2 : cc_state_fn_wait_for_sod2,
CC_STATE_WAIT_EOD1 : cc_state_fn_wait_for_eod1,
CC_STATE_WAIT_EOD2 : cc_state_fn_wait_for_eod2,
CC_STATE_GET_TYPE : cc_state_fn_get_type,
CC_STATE_READ_DATA : cc_state_fn_read_data, }
2016-08-29 17:48:24 +02:00
########## function to call by the thread
2016-08-30 16:48:55 +02:00
def cc_dataReceiverThread():
global ser
global cc_state
global cc_state_fn
global thread_started
global thread_stop
2016-08-29 17:48:24 +02:00
thread_started = True
2016-08-30 16:48:55 +02:00
while thread_stop == False:
2016-08-29 17:48:24 +02:00
# 1. read byte from serial port into incoming
2016-08-30 16:48:55 +02:00
incoming = []
bytesToRead = ser.inWaiting()
if bytesToRead > 0:
incoming = list(ser.read(64))
# 2. process the received data
for c in incoming:
c = int(binascii.hexlify(c), 16)
2016-08-30 16:48:55 +02:00
# call the cc_state specific function to process the currently received byte
cc_state_fn[cc_state](c)
2016-08-29 17:48:24 +02:00
if cc_state not in cc_state_list:
cc_state = CC_STATE_WAIT_SOD1
2016-08-30 16:48:55 +02:00
2016-08-29 17:48:24 +02:00
time.sleep(THREAD_LOOP_DELAY_S)
thread_started = False
##########
2016-08-30 16:48:55 +02:00
def cc_startReceiverThread():
global thread_obj
global thread_lock
global thread_stop
2016-08-29 17:48:24 +02:00
if thread_started == False:
thread_lock = threading.Lock()
2016-08-30 16:48:55 +02:00
thread_obj = threading.Thread(target=cc_dataReceiverThread)
2016-08-29 17:48:24 +02:00
thread_obj.start()
thread_stop = False
##########
2016-08-30 16:48:55 +02:00
def cc_stopReceiverThread():
global thread_obj
global thread_started
global thread_stop
if thread_started == True:
thread_stop = True
thread_obj.join() # wait for the thread to finish
2016-08-29 17:48:24 +02:00
##### CC_STATE_WAIT_SOD1
def cc_state_fn_wait_for_sod1(c):
2016-08-30 16:48:55 +02:00
global cc_data_read
global msg_type
global cc_data_buffer
global cc_state
2016-08-29 17:48:24 +02:00
cc_data_read = 0
2016-08-30 16:48:55 +02:00
msg_type = 0
2016-08-29 17:48:24 +02:00
cc_data_buffer = []
if c == MSG_SOD1:
cc_state = CC_STATE_WAIT_SOD2
2016-08-30 16:48:55 +02:00
else:
2016-08-29 17:48:24 +02:00
cc_state = CC_STATE_WAIT_SOD1
##### CC_STATE_WAIT_SOD2
def cc_state_fn_wait_for_sod2(c):
2016-08-30 16:48:55 +02:00
global cc_state
2016-08-29 17:48:24 +02:00
if c == MSG_SOD2:
cc_state = CC_STATE_GET_TYPE
2016-08-30 16:48:55 +02:00
else:
2016-08-29 17:48:24 +02:00
cc_state = CC_STATE_WAIT_SOD1
##### CC_STATE_GET_TYPE
def cc_state_fn_get_type(c):
2016-08-30 16:48:55 +02:00
global msg_type
global cc_state
if c in msg_type_list:
msg_type = c
2016-08-30 16:48:55 +02:00
if msg_type_data_to_read[msg_type] > 0:
2016-08-29 17:48:24 +02:00
cc_state = CC_STATE_READ_DATA
else:
cc_state = CC_STATE_WAIT_EOD1
2016-08-30 16:48:55 +02:00
else:
2016-08-29 17:48:24 +02:00
cc_state = CC_STATE_WAIT_SOD1
##### CC_STATE_READ_DATA
def cc_state_fn_read_data(c):
2016-08-30 16:48:55 +02:00
global cc_data_buffer
global cc_data_read
global cc_state
if cc_data_read <= msg_type_data_to_read[msg_type] - 1:
cc_data_buffer.append(c)
2016-08-29 17:48:24 +02:00
cc_data_read = cc_data_read + 1
2016-08-30 16:48:55 +02:00
if cc_data_read == msg_type_data_to_read[msg_type]:
2016-08-29 17:48:24 +02:00
cc_state = CC_STATE_WAIT_EOD1
##### CC_STATE_WAIT_EOD1
def cc_state_fn_wait_for_eod1(c):
2016-08-30 16:48:55 +02:00
global cc_state
2016-08-29 17:48:24 +02:00
if c == MSG_EOD1:
cc_state = CC_STATE_WAIT_EOD2
2016-08-30 16:48:55 +02:00
else:
2016-08-29 17:48:24 +02:00
cc_state = CC_STATE_WAIT_SOD1
##### CC_STATE_WAIT_EOD2
def cc_state_fn_wait_for_eod2(c):
2016-08-30 16:48:55 +02:00
global thread_lock
global cc_message_cnt
global cc_state
2016-08-29 17:48:24 +02:00
if c == MSG_EOD2:
is_message_read = False
thread_lock.acquire()
2016-08-30 16:48:55 +02:00
cc_received_messages.append([ cc_message_cnt,
msg_type,
is_message_read,
copy.deepcopy(cc_data_buffer) ])
2016-08-29 17:48:24 +02:00
thread_lock.release()
cc_message_cnt = cc_message_cnt + 1
cc_state = CC_STATE_WAIT_SOD1
###############################################################################
2016-08-30 16:48:55 +02:00
def send_getBaudrate():
print "send: get baudrate (0x%02x)" % (CC_CMD_GET_BAUDRATE)
2016-08-29 17:48:24 +02:00
sendSerialData([CC_CMD_GET_BAUDRATE])
2016-08-30 16:48:55 +02:00
def send_setBaudrate(b):
2016-08-29 17:48:24 +02:00
2016-08-30 16:48:55 +02:00
print "send: set baudrate (0x%02x)" % (CC_CMD_SET_BAUDRATE)
sendSerialData([ CC_CMD_SET_BAUDRATE,
(b & 0xff000000) >> 24,
(b & 0xff0000) >> 16,
(b & 0xff00) >> 8,
(b & 0xff) ])
2016-08-29 17:48:24 +02:00
2016-08-30 16:48:55 +02:00
###############################################################################
2016-08-29 17:48:24 +02:00
2016-08-30 16:48:55 +02:00
def send_getMuxLine():
2016-08-29 17:48:24 +02:00
2016-08-30 16:48:55 +02:00
print "send: get mux line (0x%02x)" % (CC_CMD_GET_MUX_LINE)
sendSerialData([CC_CMD_GET_MUX_LINE])
2016-08-29 17:48:24 +02:00
2016-08-30 16:48:55 +02:00
def send_setMuxLine(l):
2016-08-29 17:48:24 +02:00
2016-08-30 16:48:55 +02:00
print "send: set mux line (0x%02x)" % (CC_CMD_SET_MUX_LINE)
sendSerialData([CC_CMD_SET_MUX_LINE, l])
2016-08-29 17:48:24 +02:00
###############################################################################
2016-08-30 16:48:55 +02:00
def send_resetToBtldr():
print "send: reset to bootloader message (0x%02x)" % (CC_CMD_START_BTLDR)
2016-08-29 17:48:24 +02:00
sendSerialData([CC_CMD_START_BTLDR])
###############################################################################
#####
def openSerialDevice(d):
2016-08-30 16:48:55 +02:00
global ser
# Why 115200? Because the host defines the baudrate for the USB serial connection.
try:
if "com" in d.lower():
d = "\\\\.\\" + d
ser = serial.Serial(d, 115200, timeout=0)
except:
print "ERROR: Can't open the serial device " + d
exit(1)
2016-08-29 17:48:24 +02:00
#####
def closeSerialDevice():
2016-08-30 16:48:55 +02:00
global ser
ser.close()
2016-08-29 17:48:24 +02:00
#####
def sendSerialData(data):
2016-08-30 16:48:55 +02:00
global ser
ser.write(bytearray([ MSG_SOD1, MSG_SOD2 ]))
2016-08-30 16:48:55 +02:00
ser.write(bytearray(data))
ser.write(bytearray([ MSG_EOD1, MSG_EOD2 ]))
2016-08-29 17:48:24 +02:00
###############################################################################
if __name__ == "__main__":
2016-08-30 16:48:55 +02:00
cc_init()
2016-08-29 17:48:24 +02:00
# parse the commandline arguments
args = parser.parse_args()
dataSend = 0
timeout = 0
# 1. open serial device or abort
if args.device != None:
device = args.device
2016-08-31 18:23:38 +02:00
openSerialDevice(device)
2016-08-29 17:48:24 +02:00
2016-08-30 16:48:55 +02:00
# 2. start thread to poll cc_dataReceiverThread()
cc_startReceiverThread()
2016-08-29 17:48:24 +02:00
2016-08-30 16:48:55 +02:00
# 3. get and process the commandline arguments/parameter
if args.resetToBtldr == True:
send_resetToBtldr()
2016-08-29 17:48:24 +02:00
else:
2016-08-30 16:48:55 +02:00
if args.setBaudrate != None:
baudrate = args.setBaudrate
send_setBaudrate(baudrate)
2016-08-29 17:48:24 +02:00
dataSend = dataSend + 1
2016-08-30 16:48:55 +02:00
if args.setMuxLine != None:
muxLine = args.setMuxLine
# keep the mux line in range
if muxLine < MUX_MIN_VAL:
muxLine = MUX_MIN_VAL
if muxLine > MUX_MAX_VAL:
muxLine = MUX_MAX_VAL
send_setMuxLine(muxLine)
2016-08-29 17:48:24 +02:00
dataSend = dataSend + 1
2016-08-30 16:48:55 +02:00
if args.getBaudrate == True:
send_getBaudrate()
2016-08-29 17:48:24 +02:00
dataSend = dataSend + 1
2016-08-30 16:48:55 +02:00
if args.getMuxLine == True:
send_getMuxLine()
2016-08-29 17:48:24 +02:00
dataSend = dataSend + 1
2016-08-30 16:48:55 +02:00
# 4. start main loop
while dataSend > 0 and timeout < TIMEOUT_CNT_MAX:
2016-08-29 17:48:24 +02:00
thread_lock.acquire()
tmp_messages = copy.deepcopy(cc_received_messages)
thread_lock.release()
2016-08-30 16:48:55 +02:00
# 4.1 test for the response(s)
2016-08-29 17:48:24 +02:00
for e in tmp_messages:
if e[2] == False: # test for unread message
# process it and set the data to read
if e[1] == MSG_TYPE_ANSWER_OK:
print "recv: OK"
2016-08-30 16:48:55 +02:00
2016-08-29 17:48:24 +02:00
elif e[1] == MSG_TYPE_ANSWER_NOK:
print "recv: NOT OK"
2016-08-30 16:48:55 +02:00
2016-08-29 17:48:24 +02:00
elif e[1] == MSG_TYPE_BAUDRATE:
2016-08-30 16:48:55 +02:00
baudrate = e[3][0] << 24
baudrate += e[3][1] << 16
baudrate += e[3][2] << 8
baudrate += e[3][3]
print "recv: baudrate = %d" % (baudrate)
2016-08-29 17:48:24 +02:00
elif e[1] == MSG_TYPE_MUXLINE:
muxLine = e[3][0]
2016-08-30 16:48:55 +02:00
print "recv: muxLine = %d" % (muxLine)
2016-08-29 17:48:24 +02:00
else:
2016-08-30 16:48:55 +02:00
print "err: unknown type 0x%02x" % (e[1])
2016-08-29 17:48:24 +02:00
break
thread_lock.acquire()
cc_received_messages[e[0]][2] = True
thread_lock.release()
timeout = 0 # reset the timeout
# reduce the number of messages to receive
dataSend = dataSend - 1
# manage the timeout behaviour
time.sleep(MAIN_LOOP_DELAY_S)
timeout = timeout + 1
2016-08-30 16:48:55 +02:00
if timeout >= TIMEOUT_CNT_MAX:
print "Timeout happened"
# 5. stop data processing thread
cc_stopReceiverThread()
2016-08-29 17:48:24 +02:00
2016-08-30 16:48:55 +02:00
# 6. close serial device
2016-08-29 17:48:24 +02:00
closeSerialDevice()
exit(0)
2016-08-31 18:23:38 +02:00