Merge branch '2.0.x-maintenance' into master-merge-commit
[ExpressLRS.git] / src / python / stlink.py
blob1c514c09800f9eff86497f0164f013550db4f0a2
1 import os
2 import platform
5 def get_commands(env, firmware):
6 #platform.platform()
7 platform_name = platform.system().lower()
9 BL_CMD = []
10 APP_CMD = []
12 flash_start = app_start = 0x08000000
13 bootloader = None
14 upload_flags = env.get('UPLOAD_FLAGS', [])
16 for line in upload_flags:
17 flags = line.split()
18 for flag in flags:
19 if "BOOTLOADER=" in flag:
20 bootloader = flag.split("=")[1]
21 elif "VECT_OFFSET=" in flag:
22 offset = flag.split("=")[1]
23 if "0x" in offset:
24 offset = int(offset, 16)
25 else:
26 offset = int(offset, 10)
27 app_start = flash_start + offset
28 env_dir = env['PROJECT_PACKAGES_DIR']
29 if "windows" in platform_name:
30 TOOL = os.path.join(
31 env_dir,
32 "tool-stm32duino", "stlink", "ST-LINK_CLI.exe")
33 TOOL = '"%s"' % TOOL
34 if bootloader is not None:
35 BL_CMD = [TOOL, "-c SWD SWCLK=8 -P",
36 bootloader, hex(flash_start)]
37 APP_CMD = [TOOL, "-c SWD SWCLK=8 -P",
38 firmware, hex(app_start), "-RST"]
39 elif "linux" in platform_name or "darwin" in platform_name:
40 TOOL = os.path.join(
41 env_dir,
42 "tool-stm32duino", "stlink", "st-flash")
43 if bootloader is not None:
44 BL_CMD = [TOOL, "write", bootloader, hex(flash_start)]
45 APP_CMD = [TOOL, "--reset", "write", firmware, hex(app_start)]
46 elif "os x" in platform_name:
47 print("OS X not supported at the moment\n")
48 raise OSError
49 else:
50 print("Operating system: "+ platform_name + " is not supported.\n")
51 raise OSError
53 return " ".join(BL_CMD), " ".join(APP_CMD)
56 def on_upload(source, target, env):
57 firmware_path = str(source[0])
59 BL_CMD, APP_CMD = get_commands(env, firmware_path)
61 retval = 0
62 # flash bootloader
63 if BL_CMD:
64 print("Cmd: {}".format(BL_CMD))
65 retval = env.Execute(BL_CMD)
66 # flash application
67 if retval == 0 and APP_CMD:
68 print("Cmd: {}".format(APP_CMD))
69 retval = env.Execute(APP_CMD)
70 return retval