-
Notifications
You must be signed in to change notification settings - Fork 48
rust: Add a Rust compatible API implementation. #141
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -39,6 +39,12 @@ jobs: | |
| run: | | ||
| git clone https://github.com/arduino/ArduinoCore-API.git $MODULE_PATH/../ArduinoCore-API | ||
| cp -rfp $MODULE_PATH/../ArduinoCore-API/api $MODULE_PATH/cores/arduino/ | ||
| apt install rustup | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The recommended way to install Rust is via the rustup.rs installer script, Ubuntu's rustup package from apt is often outdated and may not work correctly There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, |
||
| rustup default stable | ||
| rustup target add thumbv6m-none-eabi | ||
| rustup target add thumbv7em-none-eabihf | ||
| rustup target add thumbv7em-none-eabi | ||
| rustup target add thumbv7m-none-eabi | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Installing targets one-by-one is slower. Rustup supports installing multiple targets in one command There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
rustup target add thumbv7m-none-eabi
rustup target add thumbv6m-none-eabi thumbv7em-none-eabihf thumbv7em-none-eabi thumbv7m-none-eabi
|
||
| - name: Build fade | ||
| run: | | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| rust/Cargo.lock |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -17,6 +17,11 @@ config ARDUINO_API | |
|
|
||
| if ARDUINO_API | ||
|
|
||
| config USE_ARDUINO_API_RUST_IMPLEMENTATION | ||
| bool "Use Rust implementation API core" | ||
| default y | ||
|
Comment on lines
+20
to
+22
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sorry to be nitpicky, but I asked this before and I'm still confused. 🙂 Is Rust expected to be installed by anyone trying to use the core at this stage? |
||
| select RUST | ||
|
|
||
| config QEMU_ICOUNT | ||
| bool "QEMU icount mode" | ||
| default n | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| /* | ||
| * Copyright (c) 2025 TOKITA Hiroshi | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <Arduino.h> | ||
| #include "zephyrInternal.h" | ||
|
|
||
| extern "C" { | ||
| int32_t map_i32(int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max); | ||
| uint16_t makeWord_w(uint16_t w); | ||
| uint16_t makeWord_hl(byte h, byte l); | ||
| } | ||
|
|
||
| long map(long x, long in_min, long in_max, long out_min, long out_max) | ||
| { | ||
| return map_i32(x, in_min, in_max, out_min, out_max); | ||
| } | ||
|
|
||
| uint16_t makeWord(uint16_t w) { | ||
| return makeWord_w(w); | ||
| } | ||
| uint16_t makeWord(byte h, byte l) { | ||
| return makeWord_hl(h, l); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| [package] | ||
| name = "arduinocore_api_rust" | ||
| version = "0.1.0" | ||
| edition = "2024" | ||
| license = "Apache-2.0" | ||
|
|
||
| [lib] | ||
| crate-type = ["staticlib"] |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| // Copyright (c) 2025 TOKITA Hiroshi | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn map_i32( | ||
| x: i32, in_min: i32, in_max: i32, out_min: i32, out_max: i32 | ||
| ) -> i32 { | ||
| let num = x.wrapping_sub(in_min).wrapping_mul(out_max.wrapping_sub(out_min)); | ||
| let den = in_max.wrapping_sub(in_min); | ||
| // Note: To keep compatibility, the panic when den=0 is left as is. | ||
| num / den.wrapping_add(out_min) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Due to operator precedence, this evaluates as: Original C++ code: return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;Can we just follow similar arithmetic syntax in rust too, the code using |
||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn makeWord_w(w: u16) -> u16 { | ||
| w | ||
| } | ||
|
|
||
| #[unsafe(no_mangle)] | ||
| pub extern "C" fn makeWord_hl(h: u8, l: u8) -> u16 { | ||
| ((h as u16) << 8) | (l as u16) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| // Copyright (c) 2025 TOKITA Hiroshi | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| #![no_std] | ||
|
|
||
| mod common; | ||
| pub use common::*; | ||
|
|
||
| use core::panic::PanicInfo; | ||
|
|
||
| #[panic_handler] | ||
| fn panic(_panic: &PanicInfo<'_>) -> ! { | ||
| loop {} | ||
| } |