Code:
#!/usr/bin/env python
import serial
import time
import sys
import os
import socket, select
import re
Hostname = "" # *** Set default Hostname here
Port = "2000" # *** Set default Port here
Hexfilename = "" # *** Set default hexfile here
Debuglevel = 1 # *** Set default Debuglevel here (0, 1, 2, 3)
BufferSize = 1024 # size for socket.recv() buffer
Timestart = time.time()
Connection = None
Hexfile = None
def CONNECTION_READ():
data = Connection.recv(BufferSize)
if Debuglevel >= 3:
if data <> '':
print("|I " + str(data))
return data
def CONNECTION_WRITE(data):
if Debuglevel >= 3:
if data <> '':
print("|O " +str(data))
Connection.send(data)
return
def CLOSE_ALL ():
if not (Connection is None):
Connection.close()
if not (Hexfile is None):
Hexfile.close()
return
def ERROR (Message):
print
print ' ## ERROR: #########################################'
print ' # '+Message
print ' ###################################################'
print
CLOSE_ALL()
quit(1)
def GET_BOARDINFO ():
Boardinfo = -1
CONNECTION_WRITE(chr(72))
Infostring = ''
Timeout = 0
while len(Infostring) < 9:
Infostring = CONNECTION_READ()
Timeout = Timeout + 1
if Timeout > 1000:
ERROR ('Got no boardinfo!')
Boardnr = ord(Infostring[4])
if (Boardnr == 8):
Boardinfo = 'M256-Board'
return Boardinfo
def WAIT_FOR_BOOT ():
Readbuff = ''
Timeout = 0
while Readbuff <> '\n[WIFIBOOT]\n':
Readbuff = CONNECTION_READ()
Timeout = Timeout + 1
if Timeout > 1000:
ERROR ('No message "[WIFIBOOT]"!')
return
def WAIT_FOR_READY ():
Readbuff = ''
Timeout = 0
while Readbuff <> '\n[READY]\n':
Readbuff = CONNECTION_READ()
Timeout = Timeout + 1
if Timeout > 1000:
ERROR ('No message "[READY]"!')
return
def RESET ():
# Enter CMD mode
CONNECTION_WRITE('$$$\r\n')
response = CONNECTION_READ()
if(re.search(r'CMD\r\n',response) == None):
ERROR('Not expected response: ' + response)
# set reset pin
CONNECTION_WRITE('set sys output 0x0080 0x0080\r\n')
response = CONNECTION_READ()
if(re.search(r'set sys output 0x0080 0x0080\r\n', response) == None):
ERROR('Not expected response: ' + response)
response = CONNECTION_READ()
if(re.search(r'\r\nAOK\r\n<.*> ', response) == None):
ERROR('Not expected response: ' + response)
# clear reset pin
CONNECTION_WRITE('set sys output 0x0000 0x0080\r\n')
response = CONNECTION_READ()
if(re.search(r'set sys output 0x0000 0x0080\r\n', response) == None):
ERROR('Not expected response: ' + response)
response = CONNECTION_READ()
if(re.search(r'\r\nAOK\r\n<.*> ', response) == None):
ERROR('Not expected response: ' + response)
# exit CMD mode
CONNECTION_WRITE('exit\r\n')
response = CONNECTION_READ()
if(re.search(r'exit\r\n', response) == None):
ERROR('Not expected response: ' + response)
return
def HEX_ENCRYPT (Flashdata):
Bytecounter = 0
Linecounter = 1
for Line in Hexfile:
Line = Line[1:]
Checksum = 0
Charpos = 0
while Charpos < len(Line)-2:
Hexchar = Line[Charpos:Charpos+2]
Checksum = Checksum + int(Hexchar, 16)
Charpos = Charpos + 2
Checksum = Checksum & 255
Checksum = 1 + ( ~Checksum)
if Checksum<>0:
ERROR("Checksum in Hexfile ("+str(Linecounter)+") false!")
else:
if Debuglevel >= 2:
print "Checksum in Line "+str(Linecounter)+" is OK"
Line = Line[8:]
Line = Line[:-4]
Charpos=0
while Charpos < len(Line):
Hexchar = Line[Charpos:Charpos+2]
Hexwert = int(Hexchar, 16)
Flashdata = Flashdata + chr(Hexwert)
Charpos = Charpos + 2
Bytecounter = Bytecounter + 1
if Bytecounter==256:
Bytecounter = 0
Linecounter = Linecounter + 1
while Bytecounter<256:
Flashdata = Flashdata + chr(255)
Bytecounter = Bytecounter + 1
return Flashdata
def CHECKBYTE (databyte, errormessage):
Timeout = 0
while CONNECTION_READ()<>chr(databyte):
Timeout = Timeout + 1
if Timeout > 1000:
ERROR (errormessage)
return
def INIT_UPLOAD ():
CONNECTION_WRITE(chr(73))
Timeout = 0
while CONNECTION_READ()<>chr(103):
Timeout = Timeout + 1
if Timeout > 1000:
ERROR ('RP6 cancels upload-init!')
return
def INIT_FLASH ():
CONNECTION_WRITE(chr(75))
Timeout = 0
while CONNECTION_READ()<>chr(91):
Timeout = Timeout + 1
if Timeout > 1000:
ERROR ('RP6 cancels flash-init!')
return
def CRC (Checksum, Databyte):
Tmp = (Databyte & 255) ^ (Checksum & 255)
Databyte = Tmp ^ (Tmp << 4)
Tmp1 = ((Databyte & 255) << 8 | Checksum >> 8 & 255)
Tmp2 = (Databyte & 255) >> 4
Tmp3 = (Databyte & 255) << 3
return Tmp1 ^ Tmp2 ^ Tmp3
def FLASH (Flashdata):
Blockcounter = 0
Bytecounter = 0
while Bytecounter < len(Flashdata):
CONNECTION_WRITE('[P]\n')
print(CONNECTION_READ())
Blockcounter = Blockcounter + 1
Checksum = 65535
CONNECTION_WRITE(chr(170))
CONNECTION_WRITE(chr(128))
CONNECTION_WRITE(chr(0))
CONNECTION_WRITE(chr(Blockcounter-1))
Checksum = CRC(Checksum, 170)
Checksum = CRC(Checksum, 128)
Checksum = CRC(Checksum, 0)
Checksum = CRC(Checksum, Blockcounter-1)
Blockbytenr = 0
while Blockbytenr < 256:
Blockbytenr = Blockbytenr + 1
Databyte = ord(Flashdata[Bytecounter])
CONNECTION_WRITE(chr(Databyte))
Checksum = CRC(Checksum, Databyte)
Bytecounter = Bytecounter + 1
CONNECTION_WRITE(chr(Checksum & 255))
CONNECTION_WRITE(chr((Checksum >> 8) & 255))
CONNECTION_WRITE(chr(170))
if Debuglevel >= 1:
print " Block",
print Blockcounter,
print "DONE. Bytes:",
print Bytecounter,
print " Checksum:",
print Checksum
if Boardinfo == 'Base-Board':
CHECKBYTE(66, 'RP6 cancels Block '+str(Blockcounter)+'!')
CHECKBYTE(93, 'RP6 cancels Block '+str(Blockcounter)+'!')
CHECKBYTE(91, 'RP6 cancels Block '+str(Blockcounter)+'!')
if Boardinfo == 'M32-Board':
CHECKBYTE(66, 'RP6 cancels Block '+str(Blockcounter)+'!')
CHECKBYTE(32, 'RP6 cancels Block '+str(Blockcounter)+'!')
CHECKBYTE(93, 'RP6 cancels Block '+str(Blockcounter)+'!')
CHECKBYTE(91, 'RP6 cancels Block '+str(Blockcounter)+'!')
if Boardinfo == 'M256-Board':
check = CONNECTION_READ()
print('# Check: ' + check)
print('# Check length: ' + str(len(check)))
return
Flashdata = ''
print
print " ###################################################"
print " # #"
print " # ****** RP6 FlashWriter ****** #"
print " # by Simon Schumann #"
print " # #"
print " # simon.schumann@web.de #"
print " # #"
print " ###################################################"
print
print "DOING: Converting arguments"
if((len(sys.argv) < 3) | len(sys.argv) > 4):
print('Usage: WifiWriter.py <hexfilename> <host> [<port>]')
sys.exit()
Hexfilename = sys.argv[1]
Host = sys.argv[2]
if(len(sys.argv) == 4):
Port = sys.argv[3]
print " File: ",
print Hexfilename
print " Host: ",
print Host
print " Port: ",
print Port
print " Debuglevel: ",
print Debuglevel
print " DONE"
print "DOING: Open hexfile ",
if not os.path.isfile(Hexfilename):
ERROR("Open hexfile: No such file!")
Hexfile = open(Hexfilename, "r")
print "DONE"
print "DOING: Parsing hexfile"
Flashdata = HEX_ENCRYPT(Flashdata)
print " DONE"
print "DOING: Open connection to Robot ",
Connection = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
Connection.settimeout(2)
try:
Connection.connect((Host,int(Port)))
except:
ERROR("NETWORK ERROR: Can't connect to " + Host + ":" + Port)
print "DONE"
print "DOING: Reset ",
RESET()
print "DONE"
print "DOING: Waiting for bootmessage ",
WAIT_FOR_BOOT()
print "DONE"
print "DOING: [WIFIBOOT_START] ",
CONNECTION_WRITE('[WIFIBOOT_START]\n')
response = CONNECTION_READ()
if(response != '[ACK]\n'):
ERROR('No expected response: ' + response)
print "DONE"
print "DOING: Get boardinfo"
Boardinfo = GET_BOARDINFO()
if Boardinfo == -1:
ERROR("Connected board not known!")
print " ->",
print Boardinfo
print " DONE"
print "DOING: Init upload ",
INIT_UPLOAD()
print "DONE"
print "DOING: Init Flash ",
INIT_FLASH()
print "DONE"
#print "DOING: Flashing "
#FLASH(Flashdata)
#print " DONE"
print "DOING: [E] ",
CONNECTION_WRITE('[E]\n')
response = CONNECTION_READ()
if(response != '\n[READY]\n'):
ERROR('No expected response: ' + response)
print "DONE"
print "DOING: Reset ",
RESET()
print "DONE"
Timeend = time.time()
print
print ' ****** FLASHED SUCCESSFUL *******'
print ' in: '+str(round(Timeend-Timestart, 2))+' seconds'
CLOSE_ALL()
quit(0)
Ist noch nicht besonders schön, aber wenn jemand wegen oben genannter Probleme mit der FLASH() Funktion Infos/Lösungen hat, wäre mir sehr geholfen.
Lesezeichen