-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a715841
commit 63b72b9
Showing
1 changed file
with
56 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
#!/usr/bin/env python | ||
|
||
import sys | ||
import serial | ||
import os.path | ||
import time | ||
from struct import pack, unpack | ||
from socket import ntohs | ||
|
||
class MTKSerial(serial.Serial): | ||
|
||
def echo(self, command): | ||
self.flushInput() | ||
self.write(command) | ||
self.flush() | ||
return port.read(len(command)) | ||
|
||
if __name__ == '__main__': | ||
|
||
print 'waiting device connected' | ||
while not os.path.exists('/dev/ttyACM0'): | ||
time.sleep(1) | ||
print '.', | ||
|
||
port = None | ||
connected = False | ||
while not connected: | ||
print 'opening port' | ||
try: | ||
port = MTKSerial('/dev/ttyACM0', 921600, timeout = 1 ) | ||
connected = True | ||
except serial.serialutil.SerialException: | ||
print 'port is busy' | ||
|
||
print 'reading startup sequence' | ||
echo = port.read(10) | ||
if 'READY' not in echo: | ||
raise Exception('device not ready') | ||
|
||
print 'device ready, trying to be master' | ||
|
||
mastered = False | ||
while not mastered: | ||
mastered = port.echo("\xA0") == "\x5F" | ||
|
||
print 'device agreed to be slave, completing sequence' | ||
assert port.echo("\x0A") == "\xF5" | ||
assert port.echo("\x50") == "\xAF" | ||
assert port.echo("\x05") == "\xFA" | ||
|
||
print 'device communication channel is ok, asking preloader version' | ||
assert port.echo("\xFD") == "\xFD" | ||
version, status = unpack('hh', port.read(4)) | ||
print 'status:', ntohs(status) | ||
print 'preloader version reported:', hex(ntohs(version)) |