Skip to content
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

Modify upload script to be compatible with python3 #2

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
73 changes: 37 additions & 36 deletions magboot.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,47 +45,50 @@

def usage(*args):
sys.stdout = sys.stderr
print __doc__
print(__doc__)
for msg in args:
print msg
print(msg)
sys.exit(2)


def do_cmd(str, expect_reply=True):
ser.write(str)
def do_cmd(val, expect_reply=True):
if isinstance(val, str):
val = val.encode('latin1')
ser.write(val)
if not expect_reply:
print "> OK"
print("> OK")
return

reply = ser.read()
if (len(reply) == 0):
print "> FAILED (TIMEOUT)"
print("> FAILED (TIMEOUT)")
sys.exit(1)

reply = reply.decode()
if (reply == 'Y'):
print "> OK"
print("> OK")
return

if (reply == 'N'):
print "> FAILED"
print("> FAILED")
else:
print "> FAILED (UNKNOWN):"
print("> FAILED (UNKNOWN):")
while (len(reply) == 1):
print reply
print(reply)
reply = ser.read()
sys.exit(1)


def cmd_device_id():
print "ID"
print("ID")
do_cmd('I' + dev['signature'])


def cmd_load_addr(addr):
print "LOAD_ADDR"
print("LOAD_ADDR")
# Little-endian, 16-bit uint load address
load_addr = pack('<H', addr)
do_cmd('A' + load_addr)
print(load_addr)
do_cmd(b'A' + load_addr)


def checksum(data):
Expand All @@ -102,49 +105,47 @@ def checksum(data):


def cmd_write_file(fname):
print "WRITE_FILE"
print("WRITE_FILE")

if (fname == "-"):
if fname == "-":
f = sys.stdin
else:
f = open(fname, "rb")

eof = False

cmd_load_addr(0)

while (not eof):
buf = array('c')
while not eof:
buf = array('B') # Create an array of unsigned bytes
bytecount = 0
while (bytecount < dev['pagesize']):
data = f.read(1)
if (len(data) == 0):
while bytecount < dev['pagesize']:
data = f.read(1) # Read 1 byte
if len(data) == 0: # End of file
eof = True
break
buf.append(data)
bytecount = bytecount + 1
buf.append(data[0]) # Append the byte (data[0] is an integer)

bytecount += 1

# Zerofill remaining part of page
if (bytecount != dev['pagesize']):
while (bytecount < dev['pagesize']):
buf.append(chr(0))
bytecount = bytecount + 1
while bytecount < dev['pagesize']:
buf.append(0) # Append 0 to fill the remaining space
bytecount += 1

bufstr = buf.tostring()
bufstr = bytes(buf) # Convert array to bytes
csum = checksum(bufstr)
do_cmd('W' + csum + bufstr)

do_cmd(b'W' + csum + bufstr)

def cmd_reset():
print "RESET"
print("RESET")
do_cmd('R', False)


def cmd_wait():
print "WAIT"
print("WAIT")
while True:
sys.stdout.flush()
ser.write('I' + dev['signature'])
ser.write(('I' + dev['signature']).encode('latin1'))
data = ser.read()
if (len(data) == 1 and data == 'Y'):
break
Expand All @@ -159,14 +160,14 @@ def cmd_wait():
try:
dev = devicelist[sys.argv[2]]
except KeyError:
print "Unsupported device '" + sys.argv[2] + "'"
print("Unsupported device '" + sys.argv[2] + "'")
sys.exit(3)

ser = serial.Serial(port=sys.argv[1], baudrate=19200, timeout=1)

try:
opts, args = getopt.getopt(sys.argv[3:], 'a:w:ijrz')
except getopt.error, msg:
except getopt.error as msg:
usage(msg)

ser.flushInput()
Expand Down
9 changes: 6 additions & 3 deletions testapp/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,13 @@ int main(void)
{
LED_DIR |= _BV(LED_BIT);
LED_PORT ^= _BV(LED_BIT);

// wdt_enable(WDTO_250MS);

wdt_enable(WDTO_250MS);

while (1); /* Watchdog bite */
while (1) {
_delay_ms(1000);
LED_PORT ^= _BV(LED_BIT);
}

return 0;
}