diff --git a/.github/workflows/examples.yml b/.github/workflows/examples.yml index c7d7be6..5334536 100644 --- a/.github/workflows/examples.yml +++ b/.github/workflows/examples.yml @@ -15,6 +15,7 @@ jobs: - "examples/gd32-spl-blinky" - "examples/gd32-w51x-wifi-scan" - "examples/gd32-e503c_start-spl-blinky" + - "examples/zephyr-blinky" runs-on: ${{ matrix.os }} steps: - uses: actions/checkout@v4 diff --git a/boards/genericGD32A503RD.json b/boards/genericGD32A503RD.json index 90a66eb..e297acc 100644 --- a/boards/genericGD32A503RD.json +++ b/boards/genericGD32A503RD.json @@ -6,7 +6,10 @@ "f_cpu": "100000000L", "mcu": "gd32a503rdt3", "spl_series": "GD32A50X", - "series": "GD32A503" + "series": "GD32A503", + "zephyr": { + "variant": "gd32a503v_eval" + } }, "debug": { "jlink_device": "GD32A503RD", @@ -21,14 +24,15 @@ ] }, "frameworks": [ - "spl" + "spl", + "zephyr" ], "name": "GD32A503RD (48k RAM, 384k Flash)", "upload": { "disable_flushing": false, "maximum_ram_size": 49152, "maximum_size": 393216, - "protocol": "stlink", + "protocol": "gdlinkcli", "protocols": [ "jlink", "cmsis-dap", diff --git a/builder/frameworks/zephyr.py b/builder/frameworks/zephyr.py new file mode 100644 index 0000000..4a9f12f --- /dev/null +++ b/builder/frameworks/zephyr.py @@ -0,0 +1,30 @@ +# Copyright 2019-present PlatformIO +# +# 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. +""" +The Zephyr Project is a scalable real-time operating system (RTOS) supporting multiple +hardware architectures, optimized for resource constrained devices, and built with +safety and security in mind. + +https://github.com/zephyrproject-rtos/zephyr +""" + +from os.path import join + +from SCons.Script import Import, SConscript + +Import("env") + +SConscript( + join(env.PioPlatform().get_package_dir("framework-zephyr"), "scripts", + "platformio", "platformio-build.py"), exports="env") \ No newline at end of file diff --git a/builder/main.py b/builder/main.py index e16dcad..290dcc1 100644 --- a/builder/main.py +++ b/builder/main.py @@ -189,6 +189,12 @@ def _update_max_upload_size(env): join("frameworks", "wifi-sdk-pre.py"), exports={"env": env} ) +if "zephyr" in pioframework: + env.SConscript( + join(platform.get_package_dir( + "framework-zephyr"), "scripts", "platformio", "platformio-build-pre.py"), + exports={"env": env} + ) target_elf = None if "nobuild" in COMMAND_LINE_TARGETS: @@ -197,6 +203,11 @@ def _update_max_upload_size(env): else: target_elf = env.BuildProgram() target_firm = env.ElfToBin(join("$BUILD_DIR", "${PROGNAME}"), target_elf) + + if "zephyr" in pioframework and "mcuboot-image" in COMMAND_LINE_TARGETS: + target_firm = env.MCUbootImage( + join("$BUILD_DIR", "${PROGNAME}.mcuboot.bin"), target_firm) + env.Depends(target_firm, "checkprogsize") # replace target_firm variable with *combined* .bin image diff --git a/examples/gd32-spl-blinky/src/main.c b/examples/gd32-spl-blinky/src/main.c index fe381e7..27538b4 100644 --- a/examples/gd32-spl-blinky/src/main.c +++ b/examples/gd32-spl-blinky/src/main.c @@ -37,9 +37,9 @@ #define LEDPIN GPIO_PIN_1 #define LED_CLOCK RCU_GPIOA #else -#define LEDPORT GPIOC -#define LEDPIN GPIO_PIN_0 -#define LED_CLOCK RCU_GPIOC +#define LEDPORT GPIOA +#define LEDPIN GPIO_PIN_5 +#define LED_CLOCK RCU_GPIOA #endif void systick_config(void); diff --git a/examples/zephyr-blink/.gitignore b/examples/zephyr-blink/.gitignore new file mode 100644 index 0000000..aeaebb2 --- /dev/null +++ b/examples/zephyr-blink/.gitignore @@ -0,0 +1 @@ +.pio \ No newline at end of file diff --git a/examples/zephyr-blink/README.md b/examples/zephyr-blink/README.md new file mode 100644 index 0000000..86d050a --- /dev/null +++ b/examples/zephyr-blink/README.md @@ -0,0 +1,38 @@ +.. Copyright 2019-present PlatformIO + 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. + +How to build PlatformIO based project +===================================== + +1. [Install PlatformIO Core](https://docs.platformio.org/page/core.html) +2. Download [development platform with examples](https://github.com/platformio/platform-ststm32/archive/develop.zip) +3. Extract ZIP archive +4. Run these commands: + +```shell +# Change directory to example +$ cd platform-ststm32/examples/zephyr-blink + +# Build project +$ pio run + +# Upload firmware +$ pio run --target upload + +# Build specific environment +$ pio run -e blackpill_f103c8 + +# Upload firmware for the specific environment +$ pio run -e blackpill_f103c8 --target upload + +# Clean build files +$ pio run --target clean +``` diff --git a/examples/zephyr-blink/include/README b/examples/zephyr-blink/include/README new file mode 100644 index 0000000..194dcd4 --- /dev/null +++ b/examples/zephyr-blink/include/README @@ -0,0 +1,39 @@ + +This directory is intended for project header files. + +A header file is a file containing C declarations and macro definitions +to be shared between several project source files. You request the use of a +header file in your project source file (C, C++, etc) located in `src` folder +by including it, with the C preprocessing directive `#include'. + +```src/main.c + +#include "header.h" + +int main (void) +{ + ... +} +``` + +Including a header file produces the same results as copying the header file +into each source file that needs it. Such copying would be time-consuming +and error-prone. With a header file, the related declarations appear +in only one place. If they need to be changed, they can be changed in one +place, and programs that include the header file will automatically use the +new version when next recompiled. The header file eliminates the labor of +finding and changing all the copies as well as the risk that a failure to +find one copy will result in inconsistencies within a program. + +In C, the usual convention is to give header files names that end with `.h'. +It is most portable to use only letters, digits, dashes, and underscores in +header file names, and at most one dot. + +Read more about using header files in official GCC documentation: + +* Include Syntax +* Include Operation +* Once-Only Headers +* Computed Includes + +https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html diff --git a/examples/zephyr-blink/lib/README b/examples/zephyr-blink/lib/README new file mode 100644 index 0000000..6debab1 --- /dev/null +++ b/examples/zephyr-blink/lib/README @@ -0,0 +1,46 @@ + +This directory is intended for project specific (private) libraries. +PlatformIO will compile them to static libraries and link into executable file. + +The source code of each library should be placed in a an own separate directory +("lib/your_library_name/[here are source files]"). + +For example, see a structure of the following two libraries `Foo` and `Bar`: + +|--lib +| | +| |--Bar +| | |--docs +| | |--examples +| | |--src +| | |- Bar.c +| | |- Bar.h +| | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html +| | +| |--Foo +| | |- Foo.c +| | |- Foo.h +| | +| |- README --> THIS FILE +| +|- platformio.ini +|--src + |- main.c + +and a contents of `src/main.c`: +``` +#include +#include + +int main (void) +{ + ... +} + +``` + +PlatformIO Library Dependency Finder will find automatically dependent +libraries scanning project source files. + +More information about PlatformIO Library Dependency Finder +- https://docs.platformio.org/page/librarymanager/ldf.html diff --git a/examples/zephyr-blink/platformio.ini b/examples/zephyr-blink/platformio.ini new file mode 100644 index 0000000..3ea8078 --- /dev/null +++ b/examples/zephyr-blink/platformio.ini @@ -0,0 +1,13 @@ +; PlatformIO Project Configuration File +; +; Build options: build flags, source filter, extra scripting +; Upload options: custom port, speed and extra flags +; Library options: dependencies, extra library storages +; +; Please visit documentation for the other options and examples +; https://docs.platformio.org/page/projectconf.html + +[env:genericGD32A503RD] +platform = gd32 +framework = zephyr +board = genericGD32A503RD diff --git a/examples/zephyr-blink/src/main.c b/examples/zephyr-blink/src/main.c new file mode 100644 index 0000000..ca653b8 --- /dev/null +++ b/examples/zephyr-blink/src/main.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2016 Intel Corporation + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +/* 1000 msec = 1 sec */ +#define SLEEP_TIME_MS 300 + +/* The devicetree node identifier for the "led3" alias, defined in zephyr/app.overlay */ +#define LED_NODE DT_ALIAS(led3) + +/* + * A build error on this line means your board is unsupported. + * See the sample documentation for information on how to fix this. + */ +static const struct gpio_dt_spec led = GPIO_DT_SPEC_GET(LED_NODE, gpios); + +void main(void) +{ + int ret; + + if (!gpio_is_ready_dt(&led)) + { + return; + } + + ret = gpio_pin_configure_dt(&led, GPIO_OUTPUT_ACTIVE); + if (ret < 0) + { + return; + } + + while (1) + { + ret = gpio_pin_toggle_dt(&led); + if (ret < 0) + { + return; + } + k_msleep(SLEEP_TIME_MS); + } +} diff --git a/examples/zephyr-blink/test/README b/examples/zephyr-blink/test/README new file mode 100644 index 0000000..df5066e --- /dev/null +++ b/examples/zephyr-blink/test/README @@ -0,0 +1,11 @@ + +This directory is intended for PIO Unit Testing and project tests. + +Unit Testing is a software testing method by which individual units of +source code, sets of one or more MCU program modules together with associated +control data, usage procedures, and operating procedures, are tested to +determine whether they are fit for use. Unit testing finds problems early +in the development cycle. + +More information about PIO Unit Testing: +- https://docs.platformio.org/page/plus/unit-testing.html diff --git a/examples/zephyr-blink/zephyr/CMakeLists.txt b/examples/zephyr-blink/zephyr/CMakeLists.txt new file mode 100644 index 0000000..dce8623 --- /dev/null +++ b/examples/zephyr-blink/zephyr/CMakeLists.txt @@ -0,0 +1,7 @@ +# SPDX-License-Identifier: Apache-2.0 + +cmake_minimum_required(VERSION 3.13.1) +include($ENV{ZEPHYR_BASE}/cmake/app/boilerplate.cmake NO_POLICY_SCOPE) +project(blinky) + +target_sources(app PRIVATE ../src/main.c) diff --git a/examples/zephyr-blink/zephyr/app.overlay b/examples/zephyr-blink/zephyr/app.overlay new file mode 100644 index 0000000..42e7ff4 --- /dev/null +++ b/examples/zephyr-blink/zephyr/app.overlay @@ -0,0 +1,12 @@ +/ { + aliases { + led3 = &myled0; + }; + leds { + compatible = "gpio-leds"; + myled0: led_0 { + gpios = <&gpioa 7 GPIO_ACTIVE_LOW>; + label = "LED3"; + }; + }; +}; \ No newline at end of file diff --git a/examples/zephyr-blink/zephyr/prj.conf b/examples/zephyr-blink/zephyr/prj.conf new file mode 100644 index 0000000..91c3c15 --- /dev/null +++ b/examples/zephyr-blink/zephyr/prj.conf @@ -0,0 +1 @@ +CONFIG_GPIO=y diff --git a/platform.json b/platform.json index 75435bf..208ba3b 100644 --- a/platform.json +++ b/platform.json @@ -47,6 +47,13 @@ "description": "GD32W51x WIFI SDK", "homepage": "http://www.gd32mcu.com/en/download/0?kw=GD32W5", "title": "WiFi SDK" + }, + "zephyr": { + "package": "framework-zephyr", + "script": "builder/frameworks/zephyr.py", + "description": "The Zephyr Project is a scalable real-time operating system (RTOS) supporting multiple hardware architectures, optimized for resource constrained devices, and built with safety and security in mind", + "homepage": "https://www.zephyrproject.org", + "title": "Zephyr RTOS" } }, "packages": { @@ -87,6 +94,12 @@ "owner": "maxgerhardt", "version": "https://github.com/CommunityGD32Cores/gd32-pio-wifi-sdk.git" }, + "framework-zephyr": { + "type": "framework", + "optional": true, + "owner": "platformio", + "version": "https://github.com/CommunityGD32Cores/zephyr.git" + }, "tool-sreccat": { "owner": "platformio", "version": "~1.164.0", @@ -121,6 +134,26 @@ "optional": true, "owner": "platformio", "version": "~1.9.190708" + }, + "tool-cmake": { + "optional": true, + "owner": "platformio", + "version": "~3.21.0" + }, + "tool-dtc": { + "optional": true, + "owner": "platformio", + "version": "~1.4.7" + }, + "tool-ninja": { + "optional": true, + "owner": "platformio", + "version": "^1.7.0" + }, + "tool-gperf": { + "optional": true, + "owner": "platformio", + "version": "^3.0.0" } } } diff --git a/platform.py b/platform.py index 56fcc6d..bf991a5 100644 --- a/platform.py +++ b/platform.py @@ -15,6 +15,7 @@ import copy import json import os +import sys from platform import system @@ -56,7 +57,14 @@ def configure_default_packages(self, variables, targets): self.packages["toolchain-gccarmnoneeabi"]["version"] = "~1.90201.0" if "wifi-sdk" in frameworks: self.packages["tool-sreccat"]["optional"] = False - + if "zephyr" in variables.get("pioframework", []): + for p in self.packages: + if p in ("tool-cmake", "tool-dtc", "tool-ninja"): + self.packages[p]["optional"] = False + self.packages["toolchain-gccarmnoneeabi"]["version"] = "~1.120301.0" + IS_WINDOWS = sys.platform.startswith("win") + if not IS_WINDOWS: + self.packages["tool-gperf"]["optional"] = False # include or exclude other packages for different frameworks.. default_protocol = board_config.get("upload.protocol") or ""