Skip to content

Commit 399fae6

Browse files
committed
x
1 parent 4dd64c6 commit 399fae6

55 files changed

Lines changed: 4686 additions & 232 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

CMakeLists.txt

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,13 +71,42 @@ if (CONFIG_ARDUINO_API)
7171
set(RUST_CRATE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/rust)
7272
set(RUST_OUT_DIR ${CMAKE_CURRENT_BINARY_DIR}/rust)
7373
set(RUST_LIB ${RUST_OUT_DIR}/target/${RUST_TARGET_TRIPLE}/release/libarduinocore_api_rust.a)
74+
set(RUST_ENUM_SRCS
75+
${RUST_CRATE_DIR}/src/lib.rs
76+
${RUST_CRATE_DIR}/src/string.rs
77+
${RUST_CRATE_DIR}/src/arduino_types/common.rs
78+
${RUST_CRATE_DIR}/src/arduino_types/hardware_spi.rs
79+
${RUST_CRATE_DIR}/src/arduino_types/stream.rs
80+
${RUST_CRATE_DIR}/src/arduino_types/ip_address.rs
81+
${RUST_CRATE_DIR}/src/arduino_types/hardware_can.rs
82+
)
83+
set(RUST_CBINDGEN_CONFIG ${RUST_CRATE_DIR}/cbindgen.toml)
84+
set(RUST_ENUM_HEADER_DIR ${CMAKE_CURRENT_BINARY_DIR}/generated/rust)
85+
set(RUST_ENUM_HEADER ${RUST_ENUM_HEADER_DIR}/arduino_types.h)
86+
87+
find_program(CBINDGEN_EXECUTABLE cbindgen REQUIRED)
88+
89+
add_custom_command(
90+
OUTPUT ${RUST_ENUM_HEADER}
91+
COMMAND ${CBINDGEN_EXECUTABLE}
92+
--config ${RUST_CBINDGEN_CONFIG}
93+
--crate arduinocore_api_rust
94+
--output ${RUST_ENUM_HEADER}
95+
WORKING_DIRECTORY ${RUST_CRATE_DIR}
96+
DEPENDS ${RUST_ENUM_SRCS} ${RUST_CBINDGEN_CONFIG}
97+
COMMENT "Generating C enum header from Rust definitions using cbindgen"
98+
VERBATIM
99+
)
100+
101+
add_custom_target(arduinocore_api_rust_enum_header ALL DEPENDS ${RUST_ENUM_HEADER})
74102

75103
add_custom_command(
76104
OUTPUT ${RUST_LIB}
77105
COMMAND ${CMAKE_COMMAND} -E env CARGO_TARGET_DIR=${RUST_OUT_DIR}/target
78106
cargo build --manifest-path ${RUST_CRATE_DIR}/Cargo.toml
79107
--target ${RUST_TARGET_TRIPLE} --release
80108
WORKING_DIRECTORY ${RUST_CRATE_DIR}
109+
DEPENDS ${RUST_CRATE_DIR}/Cargo.toml ${RUST_ENUM_SRCS}
81110
COMMENT "Building Rust staticlib for ${RUST_TARGET}"
82111
VERBATIM
83112
)
@@ -86,7 +115,9 @@ if (CONFIG_ARDUINO_API)
86115
add_library(arduinocore_api_rust STATIC IMPORTED GLOBAL)
87116
set_target_properties(arduinocore_api_rust PROPERTIES IMPORTED_LOCATION ${RUST_LIB})
88117
zephyr_link_libraries(arduinocore_api_rust)
89-
zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/zephyr/blobs/ArduinoCore-API/)
118+
add_dependencies(app arduinocore_api_rust_enum_header)
119+
zephyr_include_directories(${RUST_ENUM_HEADER_DIR})
120+
add_subdirectory(idl)
90121
else()
91122
zephyr_include_directories(${CMAKE_CURRENT_SOURCE_DIR}/zephyr/blobs/ArduinoCore-API/)
92123
zephyr_sources(${CMAKE_CURRENT_SOURCE_DIR}/zephyr/blobs/ArduinoCore-API/api/CanMsg.cpp)

Kconfig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ if ARDUINO_API
2020
config USE_ARDUINO_API_RUST_IMPLEMENTATION
2121
bool "Use Rust implementation API core"
2222
select RUST
23+
select NANOPB
2324

2425
config QEMU_ICOUNT
2526
bool "QEMU icount mode"

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The **Arduino Core API** module for zephyr leverages the power of Zephyr under a
66

77
* [Using external Arduino Libraries](/documentation/arduino_libs.md)
88
* [Adding custom boards/ variants](/documentation/variants.md)
9+
* [Arduino IDL code generation](/documentation/idl_codegen.md)
910

1011
## Adding Arduino Core API to Zephyr
1112

cores/arduino/CMakeLists.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,11 @@ zephyr_sources(zephyrCommon.cpp)
1616

1717
if(CONFIG_USE_ARDUINO_API_RUST_IMPLEMENTATION)
1818
zephyr_sources(zephyrPrint.cpp)
19-
zephyr_sources(apiCommon.cpp)
19+
zephyr_sources(zephyrString.cpp)
2020
endif()
2121

2222
if(DEFINED CONFIG_ARDUINO_ENTRY)
2323
zephyr_sources(main.cpp)
2424
endif()
2525

2626
endif()
27-

cores/arduino/apiCommon.cpp

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
#pragma once
2+
3+
#ifdef __cplusplus
4+
#include <cstdint>
5+
#include <cstddef>
6+
#include <stdlib.h>
7+
#else
8+
#include <stdint.h>
9+
#include <stddef.h>
10+
#include <stdlib.h>
11+
#endif
12+
#include <zephyr/drivers/gpio.h>
13+
14+
#include "api/HardwareSerial.h"
15+
#include "arduino_types.h"
16+
17+
#define lowByte(w) ((uint8_t) ((w) & 0xff))
18+
#define highByte(w) ((uint8_t) ((w) >> 8))
19+
20+
#define bit(b) BIT(b)
21+
#define bitRead(value, bit) FIELD_GET(BIT(bit), value)
22+
#define bitSet(value, bit) ((value) |= BIT(bit))
23+
#define bitClear(value, bit) ((value) &= ~BIT(bit))
24+
#define bitToggle(value, bit) ((value) ^= BIT(bit))
25+
#define bitWrite(value, bit, bitvalue) WRITE_BIT(value, bit, bitvalue)
26+
27+
28+
#ifdef __cplusplus
29+
extern "C" {
30+
#endif
31+
32+
typedef gpio_port_pins_t pin_size_t;
33+
34+
typedef void (*voidFuncPtr)(void);
35+
typedef void (*voidFuncPtrParam)(void*);
36+
37+
void yield(void);
38+
39+
void init(void);
40+
void initVariant(void);
41+
42+
int main() __attribute__((weak));
43+
44+
void pinMode(pin_size_t pinNumber, PinMode pinMode);
45+
void digitalWrite(pin_size_t pinNumber, PinStatus status);
46+
PinStatus digitalRead(pin_size_t pinNumber);
47+
int analogRead(pin_size_t pinNumber);
48+
void analogReference(uint8_t mode);
49+
void analogWrite(pin_size_t pinNumber, int value);
50+
51+
unsigned long millis(void);
52+
unsigned long micros(void);
53+
void delay(unsigned long);
54+
void delayMicroseconds(unsigned int us);
55+
unsigned long pulseIn(pin_size_t pin, uint8_t state, unsigned long timeout);
56+
unsigned long pulseInLong(pin_size_t pin, uint8_t state, unsigned long timeout);
57+
58+
void shiftOut(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder, uint8_t val);
59+
uint8_t shiftIn(pin_size_t dataPin, pin_size_t clockPin, BitOrder bitOrder);
60+
61+
void attachInterrupt(pin_size_t interruptNumber, voidFuncPtr callback, PinStatus mode);
62+
void attachInterruptParam(pin_size_t interruptNumber, voidFuncPtrParam callback, PinStatus mode, void* param);
63+
void detachInterrupt(pin_size_t interruptNumber);
64+
65+
void setup(void);
66+
void loop(void);
67+
68+
#ifdef __cplusplus
69+
} // extern "C"
70+
#endif
71+
72+
unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
73+
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);
74+
75+
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
76+
void noTone(uint8_t _pin);
77+
78+
// WMath prototypes
79+
long random(long);
80+
long random(long, long);
81+
void randomSeed(unsigned long);
82+
83+
84+
extern "C" {
85+
int32_t map_i32(int32_t x, int32_t in_min, int32_t in_max, int32_t out_min, int32_t out_max);
86+
uint16_t makeWord_w(uint16_t w);
87+
uint16_t makeWord_hl(uint8_t h, uint8_t l);
88+
}
89+
90+
inline long map(long x, long in_min, long in_max, long out_min, long out_max)
91+
{
92+
return map_i32(x, in_min, in_max, out_min, out_max);
93+
}
94+
95+
inline uint16_t makeWord(uint16_t w) {
96+
return makeWord_w(w);
97+
}
98+
99+
inline uint16_t makeWord(uint8_t h, uint8_t l) {
100+
return makeWord_hl(h, l);
101+
}
102+
103+
#define word(...) makeWord(__VA_ARGS__)
104+
105+
#ifdef __cplusplus
106+
template<class T, class L>
107+
auto min(const T& a, const L& b) -> decltype((b < a) ? b : a)
108+
{
109+
return (b < a) ? b : a;
110+
}
111+
112+
template<class T, class L>
113+
auto max(const T& a, const L& b) -> decltype((b < a) ? b : a)
114+
{
115+
return (a < b) ? b : a;
116+
}
117+
#else
118+
#ifndef min
119+
#define min(a,b) \
120+
({ __typeof__ (a) _a = (a); \
121+
__typeof__ (b) _b = (b); \
122+
_a < _b ? _a : _b; })
123+
#endif
124+
#ifndef max
125+
#define max(a,b) \
126+
({ __typeof__ (a) _a = (a); \
127+
__typeof__ (b) _b = (b); \
128+
_a > _b ? _a : _b; })
129+
#endif
130+
#endif

cores/arduino/zephyr/api/Binary.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright (c) 2026 TOKITA Hiroshi
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/* stub file for compat */

cores/arduino/zephyr/api/Client.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#pragma once
2+
3+
#include "Stream.h"
4+
#include <client_ifc.hpp>
5+
#include <arduino_types.h>
6+
7+
namespace arduino {
8+
9+
class Client : virtual public Stream, virtual public idl::ClientIfc {
10+
protected:
11+
uint8_t* rawIPAddress(IPAddress& addr); //TODO
12+
};
13+
14+
};

cores/arduino/zephyr/api/Common.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
/*
2+
* Copyright (c) 2026 TOKITA Hiroshi
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
/* stub file for compat */
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/*
2+
* Copyright (c) 2026 TOKITA Hiroshi
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#pragma once
8+
9+
#include "Stream.h"
10+
#include <hardware_i2c_ifc.hpp>
11+
#include <arduino_types.h>
12+
13+
namespace arduino {
14+
15+
class HardwareI2C : virtual public Stream, virtual public idl::HardwareI2CIfc {
16+
};
17+
18+
};

0 commit comments

Comments
 (0)