# command line now accepts filename (no .obj extension required) #auto detection of P2 com port added mytitle = "\nPython Prop2 loader Ver 1.2 14th Jan 2019 - ozpropdev" project = 'noname' import sys import serial import serial.tools.list_ports from time import sleep import string from base64 import b64encode baud = 3000000 xtal = 20000000 pll_div = 20 pll_mul = 180 sys_clk = xtal / pll_div * pll_mul clk = 1 << 24 | (pll_div-1) << 18 | (pll_mul-1) << 8 clk1 = clk + 0xf4 clk2 = clk + 0xf7 sp = serial.Serial() p2_list = [] ####################################################################### def loadp2(project): #if not project.endswith('.bin'): #project += '.bin' print("Project = ",project,end="") print(" on",p2_list[0]) sp.dtr = True #toggle DTR sleep(0.01) sp.dtr = False sleep(0.1) #switch P2 clock to max speed sp.timeout=1 sp.write(b'> Prop_Clk 0 0 0 0 ') m = hex(clk1)[2:]+'\n' cs = m.encode('ascii') sp.write(cs) reply = sp.read() if len(reply) == 1 and reply == b'.': print("HUBSET #1 Ok") sleep(0.02) sp.write(b'> Prop_Clk 0 0 0 0 ') m = hex(clk2)[2:]+'\n' cs = m.encode('ascii') sp.write(cs) reply = sp.read() if len(reply) == 1 and reply == b'.': sp.baudrate = baud print("HUBSET #2 Ok") else: print("No response from P2.") exit() myfile = open(project,"rb") buff1 = myfile.read(filesize(myfile)) myfile.close() print("Loading TXT ....",end="") sp.write(b'> Prop_Txt 0 0 0 0 ') sp.write(b64encode(bytes(buff1))) sleep(0.05) sp.write(b'~') #run print('complete') #exit() #! ####################################################################### def filesize(f): f.seek(0,2) z = f.tell() f.seek(0,0) return z ####################################################################### def find_p2(): print("Scanning ports....") for spx in portlist: if str(spx.device).strip() != "": print (spx.device + " = ",end="") sp.port = spx.device sp.baudrate = 19200 sp.datasize = 8 sp.stopbits=1 sp.parity = serial.PARITY_NONE sp.ReadBufferSize = 65000 #512000 #65536 sp.open() sp.timeout= 1 sp.dtr = True #toggle DTR sleep(0.01) sp.dtr = False sleep(0.1) #0.01) sp.write(b'> Prop_Chk 0 0 0 0 ') q = sp.read(14).decode('ascii') q = q.strip() #print(q) sp.close() if q.startswith("Prop"): print(q) p2_list.append(spx.device) else: print(" no P2 found.") return p2_list ####################################################################### ####################################################################### ####################################################################### script = 'blink.py' #default if len(sys.argv) == 2: project = sys.argv[1] elif len(sys.argv) == 3: project = sys.argv[1] script = sys.argv[2] else: project = input("Filename to load: ") print(mytitle) print("Python version:",sys.version,"\n") portlist = serial.tools.list_ports.comports() find_p2() if len(p2_list)==0: print("No P2's detected.") exit() sp.baudrate = baud sp.datasize = 8 sp.stopbits=1 sp.parity = serial.PARITY_NONE sp.timeout = .2 sp.port = p2_list[0] sp.dtr = False try: sp.open() except: ec = sys.exc_info()[1] m1 = str(ec) m2 = m1.split(":") m3 = m2[0] print(m3) sleep(5) exit() loadp2(project) ############################ ### end of original code ### ############################ sp.baudrate = 230400 print('enter Terminal mode\n') ans = sp.readline().decode('ascii') while ans: print(ans) ans = sp.readline().decode('ascii') sp.write(b'\r\n') sp.write(b'help()\r\n') sleep(0.05) ans = sp.readline().decode('ascii') while ans: print(ans) ans = sp.readline().decode('ascii') print('load python script\n') file = open(script,'r') for line in file: x = str.encode(line) + str.encode("\r\n") #x=x+str.encode("\r\n") #print(x) sp.write(x) sleep(0.05) file.close() ans = sp.readline().decode('ascii') while ans: print(ans) ans = sp.readline().decode('ascii') while True: while ans: print(ans) ans = sp.readline().decode('ascii') x = input() if x == 'exit': exit() x = str.encode(x) + str.encode("\r") sp.write(x) ans = sp.readline().decode('ascii')