|
33 | 33 | platform = env.PioPlatform() |
34 | 34 | board = env.BoardConfig() |
35 | 35 | mcu = env.BoardConfig().get("build.mcu") |
36 | | -chip_series: str = board.get("build.series", "")[0:-1].upper() + "x" |
| 36 | +if mcu.startswith("ch32x03"): |
| 37 | + chip_series: str = board.get("build.series", "").upper() |
| 38 | +else: |
| 39 | + chip_series: str = board.get("build.series", "")[0:-1].upper() + "x" |
37 | 40 | variant_h = board.get("build.arduino.openwch.variant_h") |
38 | 41 |
|
39 | 42 | FRAMEWORK_DIR = platform.get_package_dir("framework-arduino-openwch-ch32") |
|
93 | 96 |
|
94 | 97 | CPPDEFINES= [ |
95 | 98 | ("ARDUINO", 10808), |
| 99 | + ("ARDUINO_ARCH_CH32V"), |
| 100 | + ("ARDUINO_ARCH_CH32"), |
96 | 101 | ("VARIANT_H", env.StringifyMacro(variant_h)), |
97 | | - chip_series |
| 102 | + chip_series, |
| 103 | + "NDEBUG" |
98 | 104 | ], |
99 | 105 |
|
100 | 106 | # LIBS is handled in _LIBFLAGS below |
|
123 | 129 | SIZEDATAREGEXP=r"^(?:\.data|\.bss|\.noinit)\s+(\d+).*", |
124 | 130 | ) |
125 | 131 |
|
| 132 | +def configure_usb_flags(cpp_defines): |
| 133 | + if any([x in cpp_defines for x in ("PIO_FRAMEWORK_ARDUINO_USBD", "PIO_FRAMEWORK_ARDUINO_USBFS", "PIO_FRAMEWORK_ARDUINO_USBHS")]): |
| 134 | + env.Append(CPPPATH=[join( |
| 135 | + FRAMEWORK_DIR, "libraries", "Adafruit_TinyUSB_Arduino", "src", "arduino")]) |
| 136 | + # automatically build with lib_archive = no to make weak linking work, needed for TinyUSB |
| 137 | + env_section = "env:" + env["PIOENV"] |
| 138 | + platform.config.set(env_section, "lib_archive", False) |
| 139 | + else: |
| 140 | + return # nothing to do, bye |
| 141 | + if "PIO_FRAMEWORK_ARDUINO_USBD" in cpp_defines: |
| 142 | + env.Append(CPPDEFINES=[("CFG_TUD_WCH_USBIP_FSDEV", 1)]) |
| 143 | + elif "PIO_FRAMEWORK_ARDUINO_USBFS" in cpp_defines: |
| 144 | + env.Append(CPPDEFINES=[("CFG_TUD_WCH_USBIP_USBFS", 1)]) |
| 145 | + elif "PIO_FRAMEWORK_ARDUINO_USBHS" in cpp_defines: |
| 146 | + env.Append(CPPDEFINES=[("CFG_TUD_WCH_USBIP_USBHS", 1)]) |
| 147 | + # in any case, add standard flags |
| 148 | + # preferably use USB information from arduino.openwch section, |
| 149 | + # but fallback to sensible values derived from other parts otherwise. |
| 150 | + usb_pid = board.get("build.arduino.openwch.usb_pid", |
| 151 | + board.get("build.hwids", [[0, 0]])[0][1]) |
| 152 | + usb_vid = board.get("build.arduino.openwch.usb_vid", |
| 153 | + board.get("build.hwids", [[0, 0]])[0][0]) |
| 154 | + usb_manufacturer = board.get( |
| 155 | + "build.arduino.openwch.usb_manufacturer", board.get("vendor", "WCH")) |
| 156 | + usb_product = board.get( |
| 157 | + "build.arduino.openwch.usb_product", board.get("name", "CH32V")) |
| 158 | + |
| 159 | + env.Append(CPPDEFINES=[ |
| 160 | + "USBCON", |
| 161 | + "USE_TINYUSB", |
| 162 | + ("USB_VID", usb_vid), |
| 163 | + ("USB_PID", usb_pid), |
| 164 | + ("USB_MANUFACTURER", '\\"%s\\"' % usb_manufacturer), |
| 165 | + ("USB_PRODUCT", '\\"%s\\"' % usb_product), |
| 166 | + ]) |
| 167 | + |
| 168 | +# |
| 169 | +# Process configuration flags |
| 170 | +# |
| 171 | +cpp_defines = env.Flatten(env.get("CPPDEFINES", [])) |
| 172 | +# Ignore TinyUSB automatically if not active without requiring ldf_mode = chain+ |
| 173 | +if not any([x in cpp_defines for x in ("PIO_FRAMEWORK_ARDUINO_USBD", "PIO_FRAMEWORK_ARDUINO_USBFS", "PIO_FRAMEWORK_ARDUINO_USBHS")]): |
| 174 | + env_section = "env:" + env["PIOENV"] |
| 175 | + ignored_libs = platform.config.get(env_section, "lib_ignore", []) |
| 176 | + if not "Adafruit TinyUSB Library" in ignored_libs: |
| 177 | + ignored_libs.append("Adafruit TinyUSB Library") |
| 178 | + platform.config.set(env_section, "lib_ignore", ignored_libs) |
| 179 | +else: |
| 180 | + # one of those macros was activated. explicitly add it to library dependencies. |
| 181 | + env_section = "env:" + env["PIOENV"] |
| 182 | + set_libs = platform.config.get(env_section, "lib_deps", []) |
| 183 | + if not "Adafruit TinyUSB Library" in set_libs: |
| 184 | + set_libs.append("Adafruit TinyUSB Library") |
| 185 | + platform.config.set(env_section, "lib_deps", set_libs) |
| 186 | + |
| 187 | +# configure USB stuff |
| 188 | +configure_usb_flags(cpp_defines) |
| 189 | + |
126 | 190 | # |
127 | 191 | # Target: Build Core Library |
128 | 192 | # |
129 | 193 |
|
130 | 194 | libs = [] |
131 | 195 |
|
132 | | -if "build.variant" in board: |
| 196 | +variant = board.get("build.arduino.openwch.variant", board.get("build.variant", "")) |
| 197 | +if variant != "": |
133 | 198 | variants_dir = join( |
134 | 199 | "$PROJECT_DIR", board.get("build.variants_dir")) if board.get( |
135 | 200 | "build.variants_dir", "") else join(FRAMEWORK_DIR, "variants") |
136 | | - |
137 | | - variant = board.get("build.arduino.openwch.variant", board.get("build.variant")) |
138 | 201 | env.Append( |
139 | 202 | CPPPATH=[ |
140 | 203 | join(variants_dir, variant) |
|
0 commit comments