Skip to content

Commit

Permalink
llext
Browse files Browse the repository at this point in the history
  • Loading branch information
soburi committed Sep 2, 2024
1 parent 6efb037 commit 8eb498b
Show file tree
Hide file tree
Showing 21 changed files with 145 additions and 72 deletions.
79 changes: 69 additions & 10 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,16 +1,75 @@
# SPDX-License-Identifier: Apache-2.0

if (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/variants/${BOARD})
set(variant_dir variants/${BOARD})
elseif (IS_DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS})
set(variant_dir variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS})
else()
message(FATAL_ERROR "Variant dir not found: variants/${BOARD}, variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS}")
endif()

if (CONFIG_ARDUINO_API)
add_subdirectory(cores)
add_subdirectory(libraries)
zephyr_include_directories(${variant_dir})

function(arduino_sources)
if (IS_DIRECTORY ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/variants/${BOARD})
set(variant_dir variants/${BOARD})
elseif (IS_DIRECTORY ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS})
set(variant_dir variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS})
else()
message(FATAL_ERROR "Variant dir not found: variants/${BOARD}, variants/${BOARD}${NORMALIZED_BOARD_QUALIFIERS}")
endif()

set(ext_name arduino)
set(ext_bin ${ZEPHYR_BINARY_DIR}/${ext_name}.llext)
set(ext_inc ${ZEPHYR_BINARY_DIR}/include/generated/${ext_name}_ext.inc)

set(core_srcs
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/module_export.c
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/zephyrPrint.cpp
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/zephyrSerial.cpp
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/zephyrCommon.cpp
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/main.cpp
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/String.cpp
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/Stream.cpp
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/Common.cpp
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/Print.cpp
)

if (CONFIG_SPI)
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/libraries/SPI/SPI.cpp)
endif()

if (CONFIG_I2C)
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/libraries/Wire/Wire.cpp)
endif()

if (CONFIG_CAN)
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/CanMsgRingbuffer.cpp
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/CanMsg.cpp)
endif()

if (CONFIG_USB)
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/PluggableUSB.cpp)
endif()

if (CONFIG_NET_IP)
list(APPEND core_srcs ${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/IPAddress.cpp)
endif()

add_llext_target(${ext_name}_ext
OUTPUT ${ext_bin}
SOURCES
${core_srcs}
${ARGV}
)

llext_include_directories(${ext_name}_ext
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/cores/arduino/api/
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/libraries/SPI/
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/libraries/Wire/
${CMAKE_CURRENT_FUNCTION_LIST_DIR}/${variant_dir}
)

generate_inc_file_for_target(app ${ext_bin} ${ext_inc})
endfunction()

if(DEFINED CONFIG_ARDUINO_ENTRY)
target_sources(app PRIVATE cores/arduino/main_loader.c)
endif()

endif()

6 changes: 6 additions & 0 deletions Kconfig
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,16 @@ config ARDUINO_API
imply CBPRINTF_FP_SUPPORT
imply RING_BUFFER
select UART_INTERRUPT_DRIVEN
select LLEXT
select MODULES
default n

if ARDUINO_API

choice LLEXT_BINARY_TYPE
default LLEXT_TYPE_ELF_RELOCATABLE
endchoice

config QEMU_ICOUNT
bool "QEMU icount mode"
default n
Expand Down
3 changes: 0 additions & 3 deletions cores/CMakeLists.txt

This file was deleted.

16 changes: 0 additions & 16 deletions cores/arduino/CMakeLists.txt

This file was deleted.

6 changes: 5 additions & 1 deletion cores/arduino/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@

#include "Arduino.h"

int main(void) {
extern "C" {

int arduino_main(void) {
setup();

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

return 0;
}

}
46 changes: 46 additions & 0 deletions cores/arduino/main_loader.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
* Copyright (c) 2024 Arduino SA
*
* SPDX-License-Identifier: Apache-2.0
*/

#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL
#include <zephyr/logging/log.h>
LOG_MODULE_REGISTER(app);

#include <zephyr/llext/llext.h>
#include <zephyr/llext/buf_loader.h>

static const uint8_t llext_buf[] = {
#include "arduino_ext.inc"
};

int main(void)
{
LOG_INF("Calling hello world as a module");

size_t llext_buf_len = ARRAY_SIZE(llext_buf);
struct llext_buf_loader buf_loader = LLEXT_BUF_LOADER(llext_buf, llext_buf_len);
struct llext_loader *ldr = &buf_loader.loader;

struct llext_load_param ldr_parm = LLEXT_LOAD_PARAM_DEFAULT;
struct llext *ext;
int res;

res = llext_load(ldr, "ext", &ext, &ldr_parm);
if (res != 0) {
LOG_ERR("Failed to load extension, return code %d\n", res);
return res;
}

int (*arduino_main)() = llext_find_sym(&ext->exp_tab, "arduino_main");

if (arduino_main == NULL) {
LOG_ERR("Failed to find symbol\n");
return -1;
}

arduino_main();

return llext_unload(&ext);
}
5 changes: 5 additions & 0 deletions cores/arduino/module_export.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
#include <zephyr/llext/symbol.h>

int arduino_main(void);

LL_EXTENSION_SYMBOL(arduino_main);
13 changes: 0 additions & 13 deletions cores/arduino/zephyrPrint.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -87,16 +87,3 @@ size_t print_number_base_pow2(void *ctx, unsigned long long ull, unsigned bits)

} // namespace zephyr
} // namespace arduino

/*
* This is the default implementation.
* It will be overridden by subclassese.
*/
size_t arduino::Print::write(const uint8_t *buffer, size_t size)
{
size_t i;
for (i=0; i<size && write(buffer[i]); i++) {
}

return i;
}
4 changes: 0 additions & 4 deletions libraries/CMakeLists.txt

This file was deleted.

6 changes: 0 additions & 6 deletions libraries/SPI/CMakeLists.txt

This file was deleted.

8 changes: 0 additions & 8 deletions libraries/Wire/CMakeLists.txt

This file was deleted.

3 changes: 2 additions & 1 deletion samples/analog_input/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(analog_input)

target_sources(app PRIVATE src/main.cpp)
arduino_sources(src/main.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
2 changes: 1 addition & 1 deletion samples/attach_interrupt/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(attach_interrupt)

target_sources(app PRIVATE src/main.cpp)
arduino_sources(src/main.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
2 changes: 1 addition & 1 deletion samples/blinky_arduino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky)

target_sources(app PRIVATE src/main.cpp)
arduino_sources(src/main.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
2 changes: 1 addition & 1 deletion samples/button_press_led/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky)

target_sources(app PRIVATE src/main.cpp)
arduino_sources(src/main.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
2 changes: 1 addition & 1 deletion samples/fade/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(fade)

target_sources(app PRIVATE src/app.cpp)
arduino_sources(src/app.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
2 changes: 1 addition & 1 deletion samples/hello_arduino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(hello_world)

target_sources(app PRIVATE src/app.cpp)
arduino_sources(src/app.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
3 changes: 2 additions & 1 deletion samples/i2cdemo/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(blinky)

target_sources(app PRIVATE src/main.cpp)
arduino_sources(src/main.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
2 changes: 1 addition & 1 deletion samples/serial_event/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(serial_event)

target_sources(app PRIVATE src/app.cpp)
arduino_sources(src/app.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
2 changes: 1 addition & 1 deletion samples/spi_controller/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(spi_controller)

target_sources(app PRIVATE src/app.cpp)
arduino_sources(src/app.cpp)

zephyr_compile_options(-Wno-unused-variable -Wno-comment)
5 changes: 3 additions & 2 deletions samples/threads_arduino/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ set(DTC_OVERLAY_FILE ${ZephyrBase}/../modules/lib/Arduino-Zephyr-API/variants/${
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(threads)

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

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

0 comments on commit 8eb498b

Please sign in to comment.