/
ardwiino_script_post.py
executable file
·102 lines (95 loc) · 3.1 KB
/
ardwiino_script_post.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#!/usr/bin/env python3
from pprint import pp
import subprocess
import sys
import re
import traceback
try:
import usb
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "pyusb"])
try:
import libusb_package
except ImportError:
subprocess.check_call([sys.executable, "-m", "pip", "install", "libusb-package"])
import libusb_package
from os.path import join
import usb.core
import usb.util
from platformio.util import get_serial_ports
import os
REBOOT=48
BOOTLOADER=49
BOOTLOADER_SERIAL=50
def launch_dfu():
dev = libusb_package.find(idVendor=0x03eb)
dev.ctrl_transfer(0xA1, 3, 0, 0, 8)
command = [0x04, 0x03, 0x00]
dev.ctrl_transfer(0x21, 1, 0, 0, command)
def launch_dfu_no_reset():
dev = libusb_package.find(idVendor=0x03eb)
dev.ctrl_transfer(0xA1, 3, 0, 0, 8)
command = [0x04, 0x03, 0x01, 0x00, 0x00]
dev.ctrl_transfer(0x21, 1, 0, 0, command)
# Since the device disconnects after this, it is expected this request will fail
try:
dev.ctrl_transfer(0x21, 1, 0, 0)
except:
pass
Import("env")
def before_upload(source, target, env):
upload_options = env.BoardConfig().get("upload", {})
b_request = None
exists = False
id_vendor = None
id_product = None
wait_for_serial = False
if "ardwiino_bootloader_teensy" in upload_options and upload_options["ardwiino_bootloader_teensy"] == "true":
b_request = BOOTLOADER
elif "ardwiino_bootloader" in upload_options and upload_options["ardwiino_bootloader"] == "true":
b_request = BOOTLOADER
wait_for_serial = True
if "/arduino_uno/" in str(source[0]):
if libusb_package.find(idVendor=0x1209, idProduct=0x2882):
b_request = BOOTLOADER_SERIAL
id_vendor = 0x03eb
id_product = 0x0001
exists = libusb_package.find(idProduct=id_product, idVendor=id_vendor)
before_ports = get_serial_ports()
if b_request:
# find our device
dev = libusb_package.find(idVendor=0x1209, idProduct=0x2882)
try:
dev.detach_kernel_driver(0)
except:
pass
try:
dev.ctrl_transfer(0x21, b_request)
dev.ctrl_transfer(0x21, 0x09, b_request)
except:
pass
dev = libusb_package.find(idVendor=0x0314, idProduct=0x0830)
try:
dev.detach_kernel_driver(0)
except:
pass
try:
dev.ctrl_transfer(0x21, b_request)
dev.ctrl_transfer(0x21, 0x09, b_request)
except:
pass
if id_vendor:
args = {"idVendor": id_vendor}
if id_product:
args["idProduct"] = id_product
while not libusb_package.find(**args):
pass
if wait_for_serial:
env.Replace(UPLOAD_PORT=env.WaitForNewSerialPort(before_ports))
def post_upload(source, target, env):
if "/arduino_uno/" in str(source[0]):
env.TouchSerialPort("$UPLOAD_PORT", 2400)
if "_usb" in str(source[0]):
launch_dfu_no_reset()
env.AddPreAction("upload", before_upload)
env.AddPostAction("upload", post_upload)