2015-03-04 21:21:10 +02:00
|
|
|
# Copyright (C) Ivan Kravets <me@ikravets.com>
|
|
|
|
# See LICENSE for details.
|
|
|
|
|
2015-03-13 00:02:31 +02:00
|
|
|
from os.path import join
|
2015-05-14 17:57:21 +03:00
|
|
|
from platform import system
|
2015-03-13 00:02:31 +02:00
|
|
|
from shutil import copyfile
|
2015-03-04 21:21:10 +02:00
|
|
|
from time import sleep
|
|
|
|
|
|
|
|
from SCons.Script import Exit
|
|
|
|
from serial import Serial
|
|
|
|
|
2015-03-09 12:27:54 +02:00
|
|
|
from platformio.util import get_logicaldisks, get_serialports
|
2015-03-04 21:21:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def FlushSerialBuffer(env, port):
|
|
|
|
s = Serial(env.subst(port))
|
|
|
|
s.flushInput()
|
|
|
|
s.setDTR(False)
|
|
|
|
s.setRTS(False)
|
|
|
|
sleep(0.1)
|
|
|
|
s.setDTR(True)
|
|
|
|
s.setRTS(True)
|
|
|
|
s.close()
|
|
|
|
|
|
|
|
|
|
|
|
def TouchSerialPort(env, port, baudrate):
|
2015-05-14 17:57:21 +03:00
|
|
|
if system() == "Linux":
|
|
|
|
try:
|
|
|
|
s = Serial(env.subst(port))
|
|
|
|
s.close()
|
|
|
|
except:
|
|
|
|
pass
|
2015-03-04 21:21:10 +02:00
|
|
|
s = Serial(port=env.subst(port), baudrate=baudrate)
|
2015-03-18 23:17:39 +02:00
|
|
|
s.setDTR(False)
|
2015-03-04 21:21:10 +02:00
|
|
|
s.close()
|
2015-03-18 23:17:39 +02:00
|
|
|
sleep(0.4)
|
2015-03-04 21:21:10 +02:00
|
|
|
|
|
|
|
|
|
|
|
def WaitForNewSerialPort(_, before):
|
|
|
|
new_port = None
|
|
|
|
elapsed = 0
|
|
|
|
while elapsed < 10:
|
|
|
|
now = [i['port'] for i in get_serialports()]
|
|
|
|
diff = list(set(now) - set(before))
|
|
|
|
if diff:
|
|
|
|
new_port = diff[0]
|
|
|
|
break
|
|
|
|
|
|
|
|
before = now
|
|
|
|
sleep(0.25)
|
|
|
|
elapsed += 0.25
|
|
|
|
|
|
|
|
if not new_port:
|
|
|
|
Exit("Error: Couldn't find a board on the selected port. "
|
|
|
|
"Check that you have the correct port selected. "
|
|
|
|
"If it is correct, try pressing the board's reset "
|
|
|
|
"button after initiating the upload.")
|
|
|
|
|
|
|
|
return new_port
|
|
|
|
|
|
|
|
|
|
|
|
def AutodetectUploadPort(env):
|
2015-03-09 12:27:54 +02:00
|
|
|
if "UPLOAD_PORT" in env:
|
|
|
|
return
|
|
|
|
|
|
|
|
if env.subst("$FRAMEWORK") == "mbed":
|
2015-03-13 00:02:31 +02:00
|
|
|
msdlabels = ("mbed", "nucleo", "frdm")
|
2015-03-09 12:27:54 +02:00
|
|
|
for item in get_logicaldisks():
|
2015-03-13 00:02:31 +02:00
|
|
|
if (not item['name'] or
|
|
|
|
not any([l in item['name'].lower() for l in msdlabels])):
|
2015-03-09 12:27:54 +02:00
|
|
|
continue
|
2015-03-13 00:02:31 +02:00
|
|
|
print "Auto-detected UPLOAD_PORT/DISK: %s" % item['disk']
|
2015-03-09 12:27:54 +02:00
|
|
|
env.Replace(UPLOAD_PORT=item['disk'])
|
|
|
|
break
|
|
|
|
else:
|
2015-03-04 21:21:10 +02:00
|
|
|
for item in get_serialports():
|
2015-03-09 12:27:54 +02:00
|
|
|
if "VID:PID" not in item['hwid']:
|
|
|
|
continue
|
|
|
|
print "Auto-detected UPLOAD_PORT: %s" % item['port']
|
|
|
|
env.Replace(UPLOAD_PORT=item['port'])
|
|
|
|
break
|
2015-03-04 21:21:10 +02:00
|
|
|
|
|
|
|
if "UPLOAD_PORT" not in env:
|
|
|
|
Exit("Error: Please specify `upload_port` for environment or use "
|
2015-03-09 12:27:54 +02:00
|
|
|
"global `--upload-port` option.\n"
|
|
|
|
"For the some development platforms it can be USB flash drive\n")
|
2015-03-04 21:21:10 +02:00
|
|
|
|
|
|
|
|
2015-03-13 00:02:31 +02:00
|
|
|
def UploadToDisk(_, target, source, env): # pylint: disable=W0613,W0621
|
|
|
|
env.AutodetectUploadPort()
|
|
|
|
copyfile(join(env.subst("$BUILD_DIR"), "firmware.bin"),
|
|
|
|
join(env.subst("$UPLOAD_PORT"), "firmware.bin"))
|
|
|
|
print ("Firmware has been successfully uploaded.\n"
|
|
|
|
"Please restart your board.")
|
|
|
|
|
|
|
|
|
2015-03-04 21:21:10 +02:00
|
|
|
def exists(_):
|
|
|
|
return True
|
|
|
|
|
|
|
|
|
|
|
|
def generate(env):
|
|
|
|
env.AddMethod(FlushSerialBuffer)
|
|
|
|
env.AddMethod(TouchSerialPort)
|
|
|
|
env.AddMethod(WaitForNewSerialPort)
|
|
|
|
env.AddMethod(AutodetectUploadPort)
|
2015-03-13 00:02:31 +02:00
|
|
|
env.AddMethod(UploadToDisk)
|
2015-03-04 21:21:10 +02:00
|
|
|
return env
|