Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Arduino core version macros #13

Merged
merged 1 commit into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,21 @@ board_build.app_config = src/my_app_config.h
# ...
```

# Checking the Arduino Core Version

the current version of the arduino core can be accessed using the `ARDUINO_CORE_MAJOR`, `ARDUINO_CORE_MINOR`, and `ARDUINO_CORE_PATCH` macros.
Additionally, the helper macros `GET_VERSION_INT(major, minor, patch)` and `ARDUINO_CORE_VERSION_INT` are available in `core_util.h`.

example:

```cpp
#include <core_util.h>

#if ARDUINO_CORE_VERSION_INT < GET_VERSION_INT(1, 0, 0)
#error "expected Arduino core >= 1.0.0"
#endif
```

# Credits

This project could not have been possible without the following projects:
Expand Down
9 changes: 9 additions & 0 deletions cores/arduino/core_checks.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "drivers/panic/panic.h"
#include "core_util.h"

#ifdef __CORE_DEBUG
#warning "'__CORE_DEBUG' is defined, HC32 Arduino Core Debug is Enabled"
Expand Down Expand Up @@ -31,3 +32,11 @@
#ifdef ENABLE_MICROS
#warning "ENABLE_MICROS is deprecated. micros() is always available"
#endif

#if !defined(ARDUINO_CORE_MAJOR) || !defined(ARDUINO_CORE_MINOR) || !defined(ARDUINO_CORE_PATCH)
#error "ARDUINO_CORE_MAJOR, ARDUINO_CORE_MINOR, ARDUINO_CORE_PATCH must be defined by the builder"
#endif

#if ARDUINO_CORE_VERSION_INT < GET_VERSION_INT(1, 0, 0)
#error "expected ARDUINO_CORE_VERSION_INT >= 1.0.0"
#endif
3 changes: 3 additions & 0 deletions cores/arduino/core_util.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,6 @@
#define STRINGIFY_DETAIL(x) #x
#define STRINGIFY(x) STRINGIFY_DETAIL(x)
#endif

#define GET_VERSION_INT(major, minor, patch) ((major * 100000) + (minor * 1000) + patch)
#define ARDUINO_CORE_VERSION_INT GET_VERSION_INT(ARDUINO_CORE_MAJOR, ARDUINO_CORE_MINOR, ARDUINO_CORE_PATCH)
18 changes: 18 additions & 0 deletions tools/platformio/platformio-build-arduino.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
http://arduino.cc/en/Reference/HomePage
"""
import sys
import json
from os.path import isfile, isdir, join
from SCons.Script import DefaultEnvironment, SConscript

Expand All @@ -31,6 +32,22 @@
VARIANT_DIR = join(FRAMEWORK_DIR, "variants", board_variant)
assert isdir(VARIANT_DIR)

def get_version_defines() -> list[str]:
"""Read the version property in the package.json file and return a list of CPPDEFINES for the major, minor and patch version."""
package_json = join(FRAMEWORK_DIR, "package.json")
if not isfile(package_json):
sys.stderr.write(f"Error: failed to find package.json file for framework-arduino-hc32f46x at '{package_json}'")
env.Exit(1)

with open(package_json, "r") as f:
package_data = json.load(f)
version = package_data.get("version", "0.0.0")
major, minor, patch = version.split(".")
return [
f"ARDUINO_CORE_MAJOR={major}",
f"ARDUINO_CORE_MINOR={minor}",
f"ARDUINO_CORE_PATCH={patch}"
]

# app_config.h is a optional header file that is included via the command line in all source files
# it can be used to define custom configuration options for the application, as a replacement for the build_flags option
Expand Down Expand Up @@ -60,6 +77,7 @@
CPPDEFINES=[
("ARDUINO", 100),
"ARDUINO_ARCH_HC32",
*get_version_defines(),
],

# c/c++ include paths
Expand Down