Skip to content

Python3 port #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
107 changes: 52 additions & 55 deletions mojo.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/usr/bin/env python
#!/usr/bin/env python3
###-------------------------------------------------------------------------
### Mojo.py is a bitstream uploader for the mojo v2 board
### author Matthew O'Gorman <[email protected]>
Expand Down Expand Up @@ -64,23 +64,23 @@ def main():
args = parser.parse_args()

if args.version:
print Version
print(Version)
sys.exit(0)

#Serial code
try:
ser = serial.Serial(args.mojo_tty, 19200, timeout=10)
except:
print 'No serial port found named ' + args.mojo_tty
print('No serial port found named ' + args.mojo_tty)
sys.exit(1)

# if (not args.bitstream and not args.install and not args.copy and not args.erase and not args.only_verify and not args.reboot):
if (not args.bitstream and not args.install and not args.erase):
print parser.print_help()
print(parser.print_help())
sys.exit(1)
if args.erase:
if args.verbose:
print "Preparing to erase"
print("Preparing to erase")
erase_mojo(ser, args.verbose)
sys.exit(0)
if args.bitstream:
Expand All @@ -102,95 +102,92 @@ def display_progress(p, width=30):
sys.stdout.flush()

def install_mojo(ser, bitstream, verbose, no_verify, ram, progress):
file = open(bitstream, 'r')
file = open(bitstream, 'rb')
bits = file.read()
length = len(bits)
reboot_mojo(ser, verbose)

if ram:
ser.write('R')
ser.write(b'R')
ret = ser.read(1)
if verbose and ret == 'R':
print 'Mojo is ready to recieve bitstream'
elif ret != 'R':
print 'Mojo did not respond correctly! Make sure the port is correct'
if verbose and ret == b'R':
print('Mojo is ready to recieve bitstream')
elif ret != b'R':
print('Mojo did not respond correctly! Make sure the port is correct')
sys.exit(1)

if not ram and no_verify:
ser.write('F')
ser.write(b'F')
ret = ser.read(1)
if verbose and ret == 'R':
print 'Mojo is ready to recieve bitstream'
elif ret != 'R':
print 'Mojo did not respond correctly! Make sure the port is correct'
if verbose and ret == b'R':
print('Mojo is ready to recieve bitstream')
elif ret != b'R':
print('Mojo did not respond correctly! Make sure the port is correct')
sys.exit(1)

if not ram and not no_verify:
ser.write('V')
ser.write(b'V')
ret = ser.read(1)
if verbose and ret == 'R':
print 'Mojo is ready to recieve bitstream'
elif ret != 'R':
print 'Mojo did not respond correctly! Make sure the port is correct'
if verbose and ret == b'R':
print('Mojo is ready to recieve bitstream')
elif ret != b'R':
print('Mojo did not respond correctly! Make sure the port is correct')
sys.exit(1)

buffer = struct.unpack("4B", struct.pack("I", length))
buf = ""
for i in buffer:
buf+=(chr(i))
buf = length.to_bytes(4, byteorder='little')
ser.write(buf)
ret = ser.read(1)
if verbose and ret == 'O':
print 'Mojo acknowledged size of bitstream. Writing bitstream'
elif ret != 'O':
print 'Mojo failed to acknowledge size of bitstream. Did not write'
if verbose and ret == b'O':
print('Mojo acknowledged size of bitstream. Writing bitstream')
elif ret != b'O':
print('Mojo failed to acknowledge size of bitstream. Did not write')
sys.exit(1)

if progress:
for i,bit in enumerate(bits):
ser.write(bit)
ser.write(bit.to_bytes(1, byteorder='little'))
display_progress(float(i + 1)/length)
sys.stdout.write('\n')
else:
ser.write(bits)

ret = ser.read(1)
if verbose and ret == 'D':
print 'Mojo has been flashed'
elif ret != 'D':
print 'Mojo failed to flash correctly'
if verbose and ret == b'D':
print('Mojo has been flashed')
elif ret != b'D':
print('Mojo failed to flash correctly')
sys.exit(1)

if not ram and not no_verify:
ser.write('S')
ser.write(b'S')
if verbose:
print 'Verifying Mojo'
print('Verifying Mojo')
ret = ser.read(1)
if ret == '\xAA' and verbose:
print 'First Byte was valid getting flash size.'
elif ret != '\xAA':
print 'Flash does not contain valid start byte.'
if ret == b'\xAA' and verbose:
print('First Byte was valid getting flash size.')
elif ret != b"\xAA":
print('Flash does not contain valid start byte.')
sys.exit(1)
ret = ser.read(4)
flash_length = struct.unpack("I", ret)[0] - 5
if flash_length == length and verbose:
print 'Flash and local bitstream match file size.'
print('Flash and local bitstream match file size.')
elif flash_length == length:
print 'Flash is not same size as local bitstream.'
print('Flash is not same size as local bitstream.')
sys.exit(1)
ret = ser.read(length)
if ret == bits and verbose:
print 'Flash and local bitstream are a match.'
print('Flash and local bitstream are a match.')
elif ret == bits:
print 'Flash and local bitstream do not match.'
print('Flash and local bitstream do not match.')
sys.exit(1)
if not ram:
ser.write('L')
ser.write(b'L')
ret = ser.read(1)
if verbose and ret == 'D':
print 'Mojo has been loaded bitsream'
elif ret != 'D':
print 'Mojo failed to load bitstream'
if verbose and ret == b'D':
print('Mojo has been loaded bitsream')
elif ret != b'D':
print('Mojo failed to load bitstream')
sys.exit(1)
return

Expand All @@ -203,17 +200,17 @@ def reboot_mojo(ser, verbose):
ser.setDTR(True)
time.sleep(0.005)
if verbose:
print 'Rebooting Mojo'
print('Rebooting Mojo')
return

def erase_mojo(ser, verbose):
reboot_mojo(ser, verbose)
ser.write('E')
ser.write(b'E')
ret = ser.read(1)
if verbose and ret == 'D':
print 'Erased mojo successfully.'
elif ret != 'D':
print 'Failed to erase Mojo. Error code: ' + ret
if verbose and ret == b'D':
print('Erased mojo successfully.')
elif ret != b'D':
print('Failed to erase Mojo. Error code: ' + ret)
sys.exit(1)
ser.close()
sys.exit(0)
Expand Down