From cb439a829126a3d9b9b471689e09a7859e1128ff Mon Sep 17 00:00:00 2001 From: maxgerhardt Date: Sun, 21 May 2023 15:30:25 +0200 Subject: [PATCH 1/3] PlatformIO-enable Arduino core --- package.json | 7 ++ tools/platformio-build.py | 143 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 150 insertions(+) create mode 100644 package.json create mode 100644 tools/platformio-build.py diff --git a/package.json b/package.json new file mode 100644 index 0000000..02045fe --- /dev/null +++ b/package.json @@ -0,0 +1,7 @@ +{ + "description": "Arduino Core for CH32V003 RISC-V microcontroller", + "name": "framework-arduinoch32v003", + "system": "all", + "url": "https://github.com/AlexanderMandera/arduino-wch32v003", + "version": "0.0.0" +} \ No newline at end of file diff --git a/tools/platformio-build.py b/tools/platformio-build.py new file mode 100644 index 0000000..003efc3 --- /dev/null +++ b/tools/platformio-build.py @@ -0,0 +1,143 @@ +# Copyright 2023-present Maximilian Gerhardt +# +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +""" +Arduino + +Arduino Wiring-based Framework allows writing cross-platform software to +control devices attached to a wide range of Arduino boards to create all +kinds of creative coding, interactive objects, spaces or physical experiences. + +http://arduino.cc/en/Reference/HomePage +""" + +from os.path import isdir, join + +from SCons.Script import DefaultEnvironment + +env = DefaultEnvironment() +platform = env.PioPlatform() +board = env.BoardConfig() + +FRAMEWORK_DIR = platform.get_package_dir("framework-arduinoch32v003") +assert isdir(FRAMEWORK_DIR) + +machine_flags = [ + "-march=%s" % board.get("build.march"), + "-mabi=%s" % board.get("build.mabi"), +] + +# the compiler we're using in PlatformIO does not have "-misa-spec=2.2", so it's not included. + +env.Append( + ASFLAGS=machine_flags, + ASPPFLAGS=[ + "-x", "assembler-with-cpp" + ], + + CFLAGS=[ + "-std=gnu11" + ], + + CCFLAGS=machine_flags + [ + "-Os", + "-Wall", + "-ffunction-sections", + "-fdata-sections", + #"-flto", + #"-ffat-lto-objects", + #"-fuse-linker-plugin", + "-static-libgcc", + ], + + CXXFLAGS=[ + "-fno-exceptions", + "-fno-rtti", + "-fpermissive", + "-std=gnu++17" + ], + + LINKFLAGS=machine_flags + [ + "-Os", + "-Wl,--gc-sections", + #"-flto", + #"-fuse-linker-plugin", + "-nostdlib", + #"-fwhole-program", + '-Wl,-Map="%s"' % join("${BUILD_DIR}", "${PROGNAME}.map") + ], + + LDSCRIPT_PATH=join(FRAMEWORK_DIR, "cores", "arduino", "ch32v003fun", "ch32v003fun-stack.ld"), + + CPPDEFINES= [ + ("TINYVECTOR"), + "ARDUINO_ARCH_WCH", + ("ARDUINO", 10808) + ], + + LIBS=[ + #File(join(FRAMEWORK_DIR, "misc", "libgcc.a")) + "gcc" + ], + + LIBSOURCE_DIRS=[ + join(FRAMEWORK_DIR, "libraries") + ], + + CPPPATH=[ + join(FRAMEWORK_DIR, "cores", "arduino"), + # we excplicitly cannot include the API folder here because "String.h" (Arduino) + # conflicts with "string.h" (GCC toolchain standard header) on case-insensitive + # filesystems (Windows) + #join(FRAMEWORK_DIR, "cores", "arduino", "api"), + join(FRAMEWORK_DIR, "cores", "arduino", "api", "deprecated"), + join(FRAMEWORK_DIR, "cores", "arduino", "api", "deprecated-avr-comp"), + join(FRAMEWORK_DIR, "cores", "arduino", "ch32v003fun"), + join(FRAMEWORK_DIR, "cores", "hal"), + ] +) + +# +# Target: Build Core Library +# + +libs = [] + +if "build.variant" in board: + variants_dir = join( + "$PROJECT_DIR", board.get("build.variants_dir")) if board.get( + "build.variants_dir", "") else join(FRAMEWORK_DIR, "variants") + + env.Append( + CPPPATH=[ + join(variants_dir, board.get("build.variant")) + ] + ) + env.BuildSources( + join("$BUILD_DIR", "FrameworkArduinoVariant"), + join(variants_dir, board.get("build.variant")) + ) + +# We have build this as sources and not as library +# to get linking to work properly (??? not sure why yet) +env.BuildSources( + join("$BUILD_DIR", "FrameworkArduino"), + join(FRAMEWORK_DIR, "cores", "arduino"), + # Hack: including IPAddress.cpp in the build, although unused, + # blows up firmware size. Neither gc-sections nor LTO can + # get rid of this :( + "+<*> -" +) + +env.Prepend(LIBS=libs) From cf873692a7fb2da67f74bbcc93450bad2b6d5f0d Mon Sep 17 00:00:00 2001 From: maxgerhardt Date: Sat, 7 Oct 2023 20:52:05 +0200 Subject: [PATCH 2/3] Update build script --- tools/platformio-build.py | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/tools/platformio-build.py b/tools/platformio-build.py index 003efc3..cc40307 100644 --- a/tools/platformio-build.py +++ b/tools/platformio-build.py @@ -77,11 +77,12 @@ #"-fwhole-program", '-Wl,-Map="%s"' % join("${BUILD_DIR}", "${PROGNAME}.map") ], - - LDSCRIPT_PATH=join(FRAMEWORK_DIR, "cores", "arduino", "ch32v003fun", "ch32v003fun-stack.ld"), + # no actual GCC preprocessing needed since no macros are used + LDSCRIPT_PATH=join(FRAMEWORK_DIR, "ch32v003fun", "ch32v003fun", "ch32v003fun.ld"), CPPDEFINES= [ - ("TINYVECTOR"), + "TINYVECTOR", + "CPLUSPLUS", "ARDUINO_ARCH_WCH", ("ARDUINO", 10808) ], @@ -103,8 +104,8 @@ #join(FRAMEWORK_DIR, "cores", "arduino", "api"), join(FRAMEWORK_DIR, "cores", "arduino", "api", "deprecated"), join(FRAMEWORK_DIR, "cores", "arduino", "api", "deprecated-avr-comp"), - join(FRAMEWORK_DIR, "cores", "arduino", "ch32v003fun"), - join(FRAMEWORK_DIR, "cores", "hal"), + join(FRAMEWORK_DIR, "ch32v003fun", "ch32v003fun"), + join(FRAMEWORK_DIR, "cores", "arduino", "hal"), ] ) From 6dfcca54d3e57220e4b6a5f59c7ac3fe29a428f5 Mon Sep 17 00:00:00 2001 From: maxgerhardt Date: Wed, 1 Nov 2023 20:22:36 +0100 Subject: [PATCH 3/3] Fix building of ch32v003fun, now starts up again --- tools/platformio-build.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tools/platformio-build.py b/tools/platformio-build.py index cc40307..4fd2b01 100644 --- a/tools/platformio-build.py +++ b/tools/platformio-build.py @@ -140,5 +140,11 @@ # get rid of this :( "+<*> -" ) +# The "Released" package copies these files into core/arduino, but we use git version +# so we have to build those manually. +env.BuildSources( + join("$BUILD_DIR", "ch32v003fun"), + join(FRAMEWORK_DIR, "ch32v003fun", "ch32v003fun"), +) env.Prepend(LIBS=libs)