Check file type being uploaded. (#2756)
[ExpressLRS.git] / src / python / build_env_setup.py
blob392595e7fb0e95db92178157b0eccd8e9c1b73e5
1 Import("env", "projenv")
2 import os
3 import shutil
4 import stlink
5 import UARTupload
6 import opentx
7 import upload_via_esp8266_backpack
8 import esp_compress
9 import elrs_helpers
10 import BFinitPassthrough
11 import ETXinitPassthrough
12 import UnifiedConfiguration
14 def add_target_uploadoption(name: str, desc: str) -> None:
15 # Add an upload target 'uploadforce' that forces update if target mismatch
16 # This must be called after UPLOADCMD is set
17 env.AddCustomTarget(
18 name=name,
19 dependencies="${BUILD_DIR}/${PROGNAME}.bin",
20 title=name,
21 description=desc,
22 actions=env['UPLOADCMD']
25 def get_version(env):
26 return '%s (%s) %s' % (env.get('GIT_VERSION'), env.get('GIT_SHA'), env.get('REG_DOMAIN'))
28 platform = env.get('PIOPLATFORM', '')
29 stm = platform in ['ststm32']
31 target_name = env['PIOENV'].upper()
32 print("PLATFORM : '%s'" % platform)
33 print("BUILD ENV: '%s'" % target_name)
34 print("build version: %s\n\n" % get_version(env))
36 # don't overwrite if custom command defined
37 if stm and "$UPLOADER $UPLOADERFLAGS" in env.get('UPLOADCMD', '$UPLOADER $UPLOADERFLAGS'):
38 if target_name == "FRSKY_TX_R9M_VIA_STLINK_OLD_BOOTLOADER_DEPRECATED":
39 print("you are using the old bootloader, please update this will be removed soon")
40 env.AddPostAction("buildprog", opentx.gen_frsky)
42 # Check whether the target is using WIFI (backpack logger) upload
43 elif "_WIFI" in target_name:
44 # Generate 'firmware.elrs' file to be used for uploading (prio to bin file).
45 # This enables support for OTA updates with the "overlay double" R9M module
46 # bootloader. Bin file upload also requires the correct FLASH_OFFSET value
47 # and that can be ignored when '.elrs' file is used.
48 env.AddPostAction("buildprog", opentx.gen_elrs)
49 env.AddPreAction("upload", opentx.gen_elrs)
50 env.Replace(UPLOADCMD=upload_via_esp8266_backpack.on_upload)
52 # Check whether the target is using FC passthrough upload (receivers)
53 elif "_BETAFLIGHTPASSTHROUGH" in target_name:
54 env.Replace(UPLOADCMD=UARTupload.on_upload)
56 # Check whether the target is using DFU upload
57 elif "_DFU" in target_name:
58 board = env.BoardConfig()
59 # PIO's ststm32 forces stm32f103 to upload via maple_upload,
60 # but we really actually truly want dfu-util.
61 env.Replace(UPLOADER="dfu-util", UPLOADERFLAGS=["-d", "0483:df11",
62 "-s", "%s:leave" % board.get("upload.offset_address", "0x08001000"),
63 "-D"], UPLOADCMD='$UPLOADER $UPLOADERFLAGS "${SOURCE.get_abspath()}"')
65 # Default to ST-Link uploading
66 # Note: this target is also used to build 'firmware.elrs' binary
67 # for handset flashing
68 else:
69 if "_TX_" in target_name:
70 # Generate 'firmware.elrs' file for TX targets only
71 env.AddPostAction("buildprog", opentx.gen_elrs)
72 env.AddPreAction("upload", opentx.gen_elrs)
73 env.Replace(UPLOADCMD=stlink.on_upload)
75 elif platform in ['espressif8266']:
76 if "_WIFI" in target_name:
77 env.Replace(UPLOAD_PROTOCOL="custom")
78 env.Replace(UPLOADCMD=upload_via_esp8266_backpack.on_upload)
79 elif "_UART" in target_name:
80 env.Replace(
81 UPLOADER="$PROJECT_DIR/python/external/esptool/esptool.py",
82 UPLOAD_SPEED=460800,
83 UPLOADERFLAGS=[
84 "-b", "$UPLOAD_SPEED", "-p", "$UPLOAD_PORT",
85 "-c", "esp8266", "--before", "default_reset", "--after", "soft_reset", "write_flash"
88 elif "_BETAFLIGHTPASSTHROUGH" in target_name:
89 env.Replace(
90 UPLOADER="$PROJECT_DIR/python/external/esptool/esptool.py",
91 UPLOAD_SPEED=420000,
92 UPLOADERFLAGS=[
93 "--passthrough", "-b", "$UPLOAD_SPEED", "-p", "$UPLOAD_PORT",
94 "-c", "esp8266", "--before", "no_reset", "--after", "soft_reset", "write_flash"
97 env.AddPreAction("upload", BFinitPassthrough.init_passthrough)
99 elif platform in ['espressif32']:
100 if "_WIFI" in target_name:
101 env.Replace(UPLOAD_PROTOCOL="custom")
102 env.Replace(UPLOADCMD=upload_via_esp8266_backpack.on_upload)
103 elif "_UART" in target_name:
104 env.Replace(
105 UPLOADER="$PROJECT_DIR/python/external/esptool/esptool.py",
106 UPLOAD_SPEED=460800
108 if "_ETX" in target_name:
109 env.Replace(UPLOADER="$PROJECT_DIR/python/external/esptool/esptool.py")
110 env.AddPreAction("upload", ETXinitPassthrough.init_passthrough)
111 elif "_BETAFLIGHTPASSTHROUGH" in target_name:
112 if "ESP32S3" in target_name:
113 chip = "esp32-s3"
114 elif "ESP32C3" in target_name:
115 chip = "esp32-c3"
116 else:
117 chip = "esp32"
118 env.Replace(
119 UPLOADER="$PROJECT_DIR/python/external/esptool/esptool.py",
120 UPLOAD_SPEED=420000,
121 UPLOADERFLAGS=[
122 "--passthrough", "-b", "$UPLOAD_SPEED", "-p", "$UPLOAD_PORT",
123 "-c", chip, "--before", "no_reset", "--after", "hard_reset", "write_flash"
126 env.AddPreAction("upload", BFinitPassthrough.init_passthrough)
128 if "_WIFI" in target_name:
129 add_target_uploadoption("uploadconfirm", "Do not upload, just send confirm")
130 if "_TX_" in target_name:
131 env.SetDefault(UPLOAD_PORT="elrs_tx.local")
132 else:
133 env.SetDefault(UPLOAD_PORT="elrs_rx.local")
135 if platform != 'native':
136 add_target_uploadoption("uploadforce", "Upload even if target mismatch")
138 # Remove stale binary so the platform is forced to build a new one and attach options/hardware-layout files
139 try:
140 os.remove(env['PROJECT_BUILD_DIR'] + '/' + env['PIOENV'] +'/'+ env['PROGNAME'] + '.bin')
141 except FileNotFoundError:
142 None
143 env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", UnifiedConfiguration.appendConfiguration)
144 if platform in ['espressif8266'] and "_WIFI" in target_name:
145 env.AddPostAction("$BUILD_DIR/${PROGNAME}.bin", esp_compress.compressFirmware)
147 def copyBootApp0bin(source, target, env):
148 file = os.path.join(env.PioPlatform().get_package_dir("framework-arduinoespressif32"), "tools", "partitions", "boot_app0.bin")
149 shutil.copy2(file, os.path.join(env['PROJECT_BUILD_DIR'], env['PIOENV']))
151 if platform in ['espressif32']:
152 env.AddPreAction("$BUILD_DIR/${PROGNAME}.bin", copyBootApp0bin)
154 if platform in ['espressif32', 'espressif8266']:
155 if not os.path.exists('hardware'):
156 elrs_helpers.git_cmd('clone', 'https://github.com/ExpressLRS/targets', 'hardware')