Skip to content

Commit 8eb498b

Browse files
committed
llext
1 parent 6efb037 commit 8eb498b

File tree

21 files changed

+145
-72
lines changed

21 files changed

+145
-72
lines changed

CMakeLists.txt

+69-10
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,75 @@
11
# SPDX-License-Identifier: Apache-2.0
22

3-
if (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/variants/${BOARD})
4-
set(variant_dir variants/${BOARD})
5-
elseif (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS})
6-
set(variant_dir variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS})
7-
else()
8-
message(FATAL_ERROR "Variant dir not found: variants/${BOARD}, variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS}")
9-
endif()
103

114
if (CONFIG_ARDUINO_API)
12-
add_subdirectory(cores)
13-
add_subdirectory(libraries)
14-
zephyr_include_directories(${variant_dir})
5+
6+
function(arduino_sources)
7+
if (IS_DIRECTORY ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/variants/${BOARD})
8+
set(variant_dir variants/${BOARD})
9+
elseif (IS_DIRECTORY ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS})
10+
set(variant_dir variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS})
11+
else()
12+
message(FATAL_ERROR "Variant dir not found: variants/${BOARD}, variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS}")
13+
endif()
14+
15+
set(ext_name arduino)
16+
set(ext_bin ${ZEPHYR_BINARY_DIR}/${ext_name}.llext)
17+
set(ext_inc ${ZEPHYR_BINARY_DIR}/include/generated/${ext_name}_ext.inc)
18+
19+
set(core_srcs
20+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/module_export.c
21+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/zephyrPrint.cpp
22+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/zephyrSerial.cpp
23+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/zephyrCommon.cpp
24+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/main.cpp
25+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/String.cpp
26+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/Stream.cpp
27+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/Common.cpp
28+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/Print.cpp
29+
)
30+
31+
if (CONFIG_SPI)
32+
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/libraries/SPI/SPI.cpp)
33+
endif()
34+
35+
if (CONFIG_I2C)
36+
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/libraries/Wire/Wire.cpp)
37+
endif()
38+
39+
if (CONFIG_CAN)
40+
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/CanMsgRingbuffer.cpp
41+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/CanMsg.cpp)
42+
endif()
43+
44+
if (CONFIG_USB)
45+
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/PluggableUSB.cpp)
46+
endif()
47+
48+
if (CONFIG_NET_IP)
49+
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/IPAddress.cpp)
50+
endif()
51+
52+
add_llext_target(${ext_name}_ext
53+
OUTPUT ${ext_bin}
54+
SOURCES
55+
${core_srcs}
56+
${ARGV}
57+
)
58+
59+
llext_include_directories(${ext_name}_ext
60+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/
61+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/
62+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/libraries/SPI/
63+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/libraries/Wire/
64+
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${variant_dir}
65+
)
66+
67+
generate_inc_file_for_target(app ${ext_bin} ${ext_inc})
68+
endfunction()
69+
70+
if(DEFINED CONFIG_ARDUINO_ENTRY)
71+
target_sources(app PRIVATE cores/arduino/main_loader.c)
72+
endif()
73+
1574
endif()
1675

Kconfig

+6
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,16 @@ config ARDUINO_API
1313
imply CBPRINTF_FP_SUPPORT
1414
imply RING_BUFFER
1515
select UART_INTERRUPT_DRIVEN
16+
select LLEXT
17+
select MODULES
1618
default n
1719

1820
if ARDUINO_API
1921

22+
choice LLEXT_BINARY_TYPE
23+
default LLEXT_TYPE_ELF_RELOCATABLE
24+
endchoice
25+
2026
config QEMU_ICOUNT
2127
bool "QEMU icount mode"
2228
default n

cores/CMakeLists.txt

-3
This file was deleted.

cores/arduino/CMakeLists.txt

-16
This file was deleted.

cores/arduino/main.cpp

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,9 @@
66

77
#include "Arduino.h"
88

9-
int main(void) {
9+
extern "C" {
10+
11+
int arduino_main(void) {
1012
setup();
1113

1214
for (;;) {
@@ -16,3 +18,5 @@ int main(void) {
1618

1719
return 0;
1820
}
21+
22+
}

cores/arduino/main_loader.c

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Copyright (c) 2024 Arduino SA
3+
*
4+
* SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
8+
#include <zephyr/logging/log.h>
9+
LOG_MODULE_REGISTER(app);
10+
11+
#include <zephyr/llext/llext.h>
12+
#include <zephyr/llext/buf_loader.h>
13+
14+
static const uint8_t llext_buf[] = {
15+
#include "arduino_ext.inc"
16+
};
17+
18+
int main(void)
19+
{
20+
LOG_INF("Calling hello world as a module");
21+
22+
size_t llext_buf_len = ARRAY_SIZE(llext_buf);
23+
struct llext_buf_loader buf_loader = LLEXT_BUF_LOADER(llext_buf, llext_buf_len);
24+
struct llext_loader *ldr = &buf_loader.loader;
25+
26+
struct llext_load_param ldr_parm = LLEXT_LOAD_PARAM_DEFAULT;
27+
struct llext *ext;
28+
int res;
29+
30+
res = llext_load(ldr, "ext", &ext, &ldr_parm);
31+
if (res != 0) {
32+
LOG_ERR("Failed to load extension, return code %d\n", res);
33+
return res;
34+
}
35+
36+
int (*arduino_main)() = llext_find_sym(&ext->exp_tab, "arduino_main");
37+
38+
if (arduino_main == NULL) {
39+
LOG_ERR("Failed to find symbol\n");
40+
return -1;
41+
}
42+
43+
arduino_main();
44+
45+
return llext_unload(&ext);
46+
}

cores/arduino/module_export.c

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
#include <zephyr/llext/symbol.h>
2+
3+
int arduino_main(void);
4+
5+
LL_EXTENSION_SYMBOL(arduino_main);

cores/arduino/zephyrPrint.cpp

-13
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,3 @@ size_t print_number_base_pow2(void *ctx, unsigned long long ull, unsigned bits)
8787

8888
} // namespace zephyr
8989
} // namespace arduino
90-
91-
/*
92-
* This is the default implementation.
93-
* It will be overridden by subclassese.
94-
*/
95-
size_t arduino::Print::write(const uint8_t *buffer, size_t size)
96-
{
97-
size_t i;
98-
for (i=0; i<size && write(buffer[i]); i++) {
99-
}
100-
101-
return i;
102-
}

libraries/CMakeLists.txt

-4
This file was deleted.

libraries/SPI/CMakeLists.txt

-6
This file was deleted.

libraries/Wire/CMakeLists.txt

-8
This file was deleted.

samples/analog_input/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(analog_input)
1010

11-
target_sources(app PRIVATE src/main.cpp)
11+
arduino_sources(src/main.cpp)
12+
1213
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/attach_interrupt/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(attach_interrupt)
1010

11-
target_sources(app PRIVATE src/main.cpp)
11+
arduino_sources(src/main.cpp)
1212

1313
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/blinky_arduino/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(blinky)
1010

11-
target_sources(app PRIVATE src/main.cpp)
11+
arduino_sources(src/main.cpp)
1212

1313
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/button_press_led/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(blinky)
1010

11-
target_sources(app PRIVATE src/main.cpp)
11+
arduino_sources(src/main.cpp)
1212

1313
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/fade/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(fade)
1010

11-
target_sources(app PRIVATE src/app.cpp)
11+
arduino_sources(src/app.cpp)
1212

1313
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/hello_arduino/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(hello_world)
1010

11-
target_sources(app PRIVATE src/app.cpp)
11+
arduino_sources(src/app.cpp)
1212

1313
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/i2cdemo/CMakeLists.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(blinky)
1010

11-
target_sources(app PRIVATE src/main.cpp)
11+
arduino_sources(src/main.cpp)
12+
1213
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/serial_event/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(serial_event)
1010

11-
target_sources(app PRIVATE src/app.cpp)
11+
arduino_sources(src/app.cpp)
1212

1313
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/spi_controller/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(spi_controller)
1010

11-
target_sources(app PRIVATE src/app.cpp)
11+
arduino_sources(src/app.cpp)
1212

1313
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

samples/threads_arduino/CMakeLists.txt

+3-2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
88
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
99
project(threads)
1010

11-
target_sources(app PRIVATE src/main.cpp)
12-
zephyr_compile_options(-Wno-unused-variable -Wno-comment)
11+
arduino_sources(src/main.cpp)
12+
13+
zephyr_compile_options(-Wno-unused-variable -Wno-comment)

0 commit comments

Comments
 (0)