From 8eb498b94fc64161d0df68c875185538cb9fc12c Mon Sep 17 00:00:00 2001 From: TOKITA Hiroshi Date: Sun, 4 Aug 2024 10:42:50 +0900 Subject: [PATCH] llext --- CMakeLists.txt | 79 +++++++++++++++++++++---- Kconfig | 6 ++ cores/CMakeLists.txt | 3 - cores/arduino/CMakeLists.txt | 16 ----- cores/arduino/main.cpp | 6 +- cores/arduino/main_loader.c | 46 ++++++++++++++ cores/arduino/module_export.c | 5 ++ cores/arduino/zephyrPrint.cpp | 13 ---- libraries/CMakeLists.txt | 4 -- libraries/SPI/CMakeLists.txt | 6 -- libraries/Wire/CMakeLists.txt | 8 --- samples/analog_input/CMakeLists.txt | 3 +- samples/attach_interrupt/CMakeLists.txt | 2 +- samples/blinky_arduino/CMakeLists.txt | 2 +- samples/button_press_led/CMakeLists.txt | 2 +- samples/fade/CMakeLists.txt | 2 +- samples/hello_arduino/CMakeLists.txt | 2 +- samples/i2cdemo/CMakeLists.txt | 3 +- samples/serial_event/CMakeLists.txt | 2 +- samples/spi_controller/CMakeLists.txt | 2 +- samples/threads_arduino/CMakeLists.txt | 5 +- 21 files changed, 145 insertions(+), 72 deletions(-) delete mode 100644 cores/CMakeLists.txt delete mode 100644 cores/arduino/CMakeLists.txt create mode 100644 cores/arduino/main_loader.c create mode 100644 cores/arduino/module_export.c delete mode 100644 libraries/CMakeLists.txt delete mode 100644 libraries/SPI/CMakeLists.txt delete mode 100644 libraries/Wire/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index 9d50d173..644a8b56 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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() diff --git a/Kconfig b/Kconfig index 27f9fb3c..29f5ae8b 100644 --- a/Kconfig +++ b/Kconfig @@ -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 diff --git a/cores/CMakeLists.txt b/cores/CMakeLists.txt deleted file mode 100644 index f7b781c9..00000000 --- a/cores/CMakeLists.txt +++ /dev/null @@ -1,3 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 - -add_subdirectory(arduino) \ No newline at end of file diff --git a/cores/arduino/CMakeLists.txt b/cores/arduino/CMakeLists.txt deleted file mode 100644 index 193fd3c1..00000000 --- a/cores/arduino/CMakeLists.txt +++ /dev/null @@ -1,16 +0,0 @@ -# SPDX-License-Identifier: Apache-2.0 -zephyr_include_directories(.) -zephyr_include_directories(../../variants) - -if(NOT DEFINED ARDUINO_BUILD_PATH) - -zephyr_sources(zephyrPrint.cpp) -zephyr_sources(zephyrSerial.cpp) -zephyr_sources(zephyrCommon.cpp) - -if(DEFINED CONFIG_ARDUINO_ENTRY) -zephyr_sources(main.cpp) -endif() - -endif() - diff --git a/cores/arduino/main.cpp b/cores/arduino/main.cpp index 97afd420..e21aaf6a 100644 --- a/cores/arduino/main.cpp +++ b/cores/arduino/main.cpp @@ -6,7 +6,9 @@ #include "Arduino.h" -int main(void) { +extern "C" { + +int arduino_main(void) { setup(); for (;;) { @@ -16,3 +18,5 @@ int main(void) { return 0; } + +} diff --git a/cores/arduino/main_loader.c b/cores/arduino/main_loader.c new file mode 100644 index 00000000..95f846fa --- /dev/null +++ b/cores/arduino/main_loader.c @@ -0,0 +1,46 @@ +/* + * Copyright (c) 2024 Arduino SA + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#define LOG_LEVEL CONFIG_LOG_DEFAULT_LEVEL +#include +LOG_MODULE_REGISTER(app); + +#include +#include + +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); +} diff --git a/cores/arduino/module_export.c b/cores/arduino/module_export.c new file mode 100644 index 00000000..4f5271ca --- /dev/null +++ b/cores/arduino/module_export.c @@ -0,0 +1,5 @@ +#include + +int arduino_main(void); + +LL_EXTENSION_SYMBOL(arduino_main); diff --git a/cores/arduino/zephyrPrint.cpp b/cores/arduino/zephyrPrint.cpp index 2e07c232..bf250532 100644 --- a/cores/arduino/zephyrPrint.cpp +++ b/cores/arduino/zephyrPrint.cpp @@ -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