forked from arduino12/n76e003_platformio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extra_script.py
117 lines (101 loc) · 4.37 KB
/
extra_script.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#
# python3 script for uploading firmware to Nuvoton MCUs via NuLink programmer using their windows apps.
# [email protected] 01/05/2021
#
import os.path
import warnings
Import('env')
NULINK_CLI_EXE = '"%PROGRAMFILES(X86)%\\Nuvoton Tools\\NuLink Command Tool\\NuLink.exe"'
NUMICRO_GUI_EXE = '%PROGRAMFILES(X86)%\\Nuvoton Tools\\ICPTool\\NuMicro ICP Programming Tool.exe'
NULINK_NOT_FOUND_MSG = '''
Please install %s from:
https://www.nuvoton.com/tool-and-software/software-development-tool/programmer
'''
# select whether to upload using NULINK_CLI_EXE (USE_FASTER_NUMICRO = False) or
# using NUMICRO_GUI_EXE (USE_FASTER_NUMICRO = True) - this uses pywinauto but still much faster to upload relative to the CLI...
# USE_FASTER_NUMICRO = False
USE_FASTER_NUMICRO = True
def on_upload(source, target, env):
firmware_path = os.path.abspath(str(source[0]))
if USE_FASTER_NUMICRO:
# install pywinauto if needed
try:
import pywinauto
except ImportError:
env.Execute(env.VerboseAction('$PYTHONEXE -m pip install pywinauto', 'Installing Python dependencies...'))
warnings.warn('Please run again!')
exit(1)
# connect to NuMicro app (run it if needed)
numicro_gui_exe = os.path.expandvars(NUMICRO_GUI_EXE)
app = pywinauto.Application(backend='win32')
# dont print 32/64bit application warning
warnings.simplefilter('ignore')
try:
app.connect(path=numicro_gui_exe)
print('Connected to NuMicro app')
except:
try:
app.start(numicro_gui_exe)
print('Started NuMicro app')
except:
app = None
warnings.simplefilter('default')
if app is None:
warnings.warn(NULINK_NOT_FOUND_MSG % 'NuMicroICP')
exit(2)
# config startup dialog if needed
window = app.window(title_re='^Nuvoton')
window.set_focus()
window.wait('enabled')
if window.control_count() < 50:
window.SelectTargetChipComboBox.select('8051 1T Series')
window.ContinueButton.click()
# helper to manage connection state
def set_connection(is_connected):
STATES = ('Disconnect', 'Connect')
is_connected = 1 if is_connected else 0
window.ConnectButton.wait('enabled')
if window.ConnectButton.window_text() == STATES[is_connected]:
window.ConnectButton.check_by_click()
pywinauto.timings.wait_until(3, 0.1, window.ConnectButton.window_text, STATES[1 - is_connected])
print(f'{"Connected to" if is_connected else "Disconnected from"} NuLink programmer')
set_connection(True)
window.set_focus()
# flash only APROM
window.ConfigCheckBox.uncheck_by_click()
window.LDROMCheckBox.uncheck_by_click()
window.APROMCheckBox.check_by_click()
# set APROM file path if needed
if firmware_path != window.FileNameEdit0.window_text():
print(f'Setting APROM file to {firmware_path}')
window.APROMButton.set_focus()
window.APROMButton.click_input()
app.Open.wait('enabled')
app.Open.FileNameEdit.set_text(firmware_path)
app.Open.OpenButton.click_input()
window.wait('enabled')
# handle dialogs
window.StartButton.click_input()
dialog = app.window(title_re='^NuMicro')
while True:
dialog.wait('enabled')
text = dialog.static.window_text()
dialog.send_keystrokes('{ENTER}{ESC}')
if text == 'Programming flash, OK!':
break
set_connection(False)
window.minimize()
else:
# erase APROM and write firmware to APROM
try:
env.Execute(f'{NULINK_CLI_EXE} -e aprom')
env.Execute(f'{NULINK_CLI_EXE} -w aprom {firmware_path}')
# env.Execute(f'{NULINK_CLI_EXE} -disconnect') # doesn't disconnect....
except:
warnings.warn(NULINK_NOT_FOUND_MSG % 'NuLinkCLI')
exit(2)
# warn user to manually disconnect by pressing the button on the NuLink because -disconnect doesn't work..
__import__('time').sleep(0.1)
warnings.warn('''
Press NuLink button to release (disconnect) mcu if needed!''')
env.Replace(UPLOADCMD=on_upload)