Create a gist now

Instantly share code, notes, and snippets.

#!/usr/local/bin/python
import sys
def strip_line(line):
line=line.rstrip('\n')
line=line.replace('0x','')
return (line.split(',')) #time, packet_id, mosi, miso
def analyse_file(csv_file):
''' Return dict format:
{int(packet_id): (int(address), bytearray(payload)}
'''
LAST_FAKE_ID = 4242424242 #higher than any real address
written_reg = ""
current_packet = "-1" #This will force the first `if` iteration to fail
current_cmd = '00'
current_rw_block = '000000'
current_block_size = 0
print "___________________"
print "|Transmission Map|"
print "| MOSI | MISO |"
for line in csv_file:
time, packet_id, mosi, miso = strip_line(line)
try:
if packet_id != "": #Last packet doesnt have an ID (analyser bug)
packet_id=int(packet_id)
else:
packet_id=LAST_FAKE_ID
except ValueError:
#print "Non-hex characters detected. Ignoring line [{0}]".format(
# line.rstrip('\n'))
continue #Mostly so we don't need to strip the 1st line from the file
#The first byte will always be a command, so we're not losing data here:
if current_packet == packet_id:
if current_cmd=='02': #Page Programming
current_block_size += 1
elif current_cmd=='03': #Read Data bytes
current_block_size += 1
elif current_cmd=='01':
written_reg+=mosi
#elif current_cmd=='05': #Read Status Register
# pass #Ignore response. Will `continue`
#elif current_cmd=='D8': #64 kB Sector Erase
# pass #Shouldn't happen
#elif current_cmd=='06': #Write Enable
# pass #Shouldn't happen
#else:
# print "ERROR Unrecognised command [{0}]".format(current_cmd)
continue
elif current_packet != "-1": #Skip in the first iteration
if current_cmd=='02':
print "|{0:#8x}| | Size: {1}".format(current_rw_block,
current_block_size)
elif current_cmd=='03':
print "| |{0:#8x}| Size: {1}".format(current_rw_block,
current_block_size)
elif current_cmd=='D8':
print "| ERASE {0:#8x} | Size: 64kB".format(current_rw_block)
#elif current_cmd == '01':
# print "Written Status&Config Register [{0}]".format(written_reg)
# written_reg=""
current_block_size=0
current_packet = packet_id #Next time we'll process the packet
current_cmd=mosi
if mosi=='02': #Page Programming
t, id, addr0, so = strip_line(csv_file.next())
t, id, addr1, so = strip_line(csv_file.next())
t, id, addr2, so = strip_line(csv_file.next())
current_rw_block = int("{0}{1}{2}".format(addr0, addr1, addr2), 16)
#print "Programming Address [{0}]".format(hex(current_rw_block))
continue
elif mosi=='03': #Read Data bytes
t, id, addr0, so = strip_line(csv_file.next())
t, id, addr1, so = strip_line(csv_file.next())
t, id, addr2, so = strip_line(csv_file.next())
current_rw_block = int("{0}{1}{2}".format(addr0, addr1, addr2), 16)
#print "Reading Address [{0}]".format(hex(current_rw_block))
continue
elif mosi=='05': #Read Status Register
t, id, si, status_reg = strip_line(csv_file.next())
#print "Status Register Contents [{0}]".format(status_reg)
continue
elif mosi=='06': #Write Enable
#print "Write Enabled"
continue #This command is just 1 byte
elif mosi=='D8': #64 kB Sector Erase
t, id, addr0, so = strip_line(csv_file.next())
t, id, addr1, so = strip_line(csv_file.next())
t, id, addr2, so = strip_line(csv_file.next())
addr = int("{0}{1}{2}".format(addr0, addr1, addr2), 16)
#print "Erasing Address [{0}]".format(hex(addr))
continue #We will ignore this one
elif mosi=='01': #Write (Status & Configuration) Register
continue
if __name__ == '__main__':
inp_path = sys.argv[1]
with open(inp_path, 'r') as file:
analyse_file(file)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment