Skip to content

Commit 6c221a7

Browse files
committed
llext
1 parent b0f521d commit 6c221a7

File tree

13 files changed

+106
-56
lines changed

13 files changed

+106
-56
lines changed

CMakeLists.txt

+38-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,43 @@
11
# SPDX-License-Identifier: Apache-2.0
22

33
if (CONFIG_ARDUINO_API)
4-
add_subdirectory(cores)
5-
add_subdirectory(libraries)
6-
zephyr_include_directories(variants/${BOARD})
4+
set(ext_name arduino)
5+
set(ext_bin ${ZEPHYR_BINARY_DIR}/${ext_name}.llext)
6+
set(ext_inc ${ZEPHYR_BINARY_DIR}/include/generated/${ext_name}_ext.inc)
7+
8+
add_llext_target(${ext_name}_ext
9+
OUTPUT ${ext_bin}
10+
SOURCES
11+
cores/arduino/module_export.c
12+
cores/arduino/zephyrPrint.cpp
13+
cores/arduino/zephyrSerial.cpp
14+
cores/arduino/zephyrCommon.cpp
15+
cores/arduino/main.cpp
16+
cores/arduino/api/CanMsgRingbuffer.cpp
17+
cores/arduino/api/String.cpp
18+
cores/arduino/api/Stream.cpp
19+
cores/arduino/api/Common.cpp
20+
cores/arduino/api/CanMsg.cpp
21+
cores/arduino/api/PluggableUSB.cpp
22+
cores/arduino/api/Print.cpp
23+
cores/arduino/api/IPAddress.cpp
24+
libraries/SPI/SPI.cpp
25+
libraries/Wire/Wire.cpp
26+
${ARDUINO_SOURCES}
27+
)
28+
29+
llext_include_directories(${ext_name}_ext
30+
cores/arduino/
31+
cores/arduino/api/
32+
libraries/SPI/
33+
libraries/Wire/
34+
variants/${BOARD}
35+
)
36+
37+
generate_inc_file_for_target(app ${ext_bin} ${ext_inc})
38+
39+
if(DEFINED CONFIG_ARDUINO_ENTRY)
40+
target_sources(app PRIVATE cores/arduino/main_loader.c)
41+
endif()
742
endif()
843

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
@@ -5,8 +5,9 @@ cmake_minimum_required(VERSION 3.20.0)
55
cmake_path(SET ZephyrBase $ENV{ZEPHYR_BASE})
66
set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay)
77

8+
list(APPEND ARDUINO_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)
9+
810
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
911
project(analog_input)
1012

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

samples/blinky_arduino/CMakeLists.txt

+4-1
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,13 @@ cmake_minimum_required(VERSION 3.20.0)
44

55
cmake_path(SET ZephyrBase $ENV{ZEPHYR_BASE})
66
set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${BOARD}/${BOARD}.overlay)
7+
list(APPEND ARDUINO_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)
8+
9+
list(APPEND ARDUINO_SOURCES ${CMAKE_CURRENT_SOURCE_DIR}/src/main.cpp)
710

811
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
912
project(blinky)
1013

11-
target_sources(app PRIVATE src/main.cpp)
14+
#target_sources(app PRIVATE src/main.cpp)
1215

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

0 commit comments

Comments
 (0)