From f07d2beb3ce36981d551d1e6c2339bebf73e14f0 Mon Sep 17 00:00:00 2001 From: chenyisong Date: Mon, 21 Oct 2024 18:16:51 +0800 Subject: [PATCH] support PlatformIO --- CMakeLists.txt | 20 ++++++ apps/tuya_cloud/switch_demo/project_build.ini | 4 +- tools/app_template/arduino/CMakeLists.txt | 32 ++++++++++ tools/app_template/arduino/Kconfig | 22 +++++++ tools/app_template/arduino/project_build.ini | 3 + .../app_template/arduino/src/hello_world.cpp | 12 ++++ tools/app_template/{ => base}/CMakeLists.txt | 0 .../app_template/{ => base}/project_build.ini | 1 + .../app_template/{ => base}/src/hello_world.c | 0 tools/cmake/util.cmake | 9 ++- tos | 61 +++++++++++++++++-- 11 files changed, 153 insertions(+), 11 deletions(-) create mode 100755 tools/app_template/arduino/CMakeLists.txt create mode 100755 tools/app_template/arduino/Kconfig create mode 100755 tools/app_template/arduino/project_build.ini create mode 100755 tools/app_template/arduino/src/hello_world.cpp rename tools/app_template/{ => base}/CMakeLists.txt (100%) rename tools/app_template/{ => base}/project_build.ini (77%) rename tools/app_template/{ => base}/src/hello_world.c (100%) mode change 100644 => 100755 diff --git a/CMakeLists.txt b/CMakeLists.txt index ced6483..6963917 100755 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -39,6 +39,10 @@ ENDIF () include(${TOP_SOURCE_DIR}/tools/cmake/colors.cmake) include(${TOP_SOURCE_DIR}/tools/cmake/util.cmake) +if(NOT DEFINED TOS_FRAMEWORK) + set(TOS_FRAMEWORK "base") +endif() +message(STATUS "[TOP] Using framework [${TOS_FRAMEWORK}].") if(DEFINED TOS_PROJECT_PLATFORM) set(PLATFORM_NAME ${TOS_PROJECT_PLATFORM}) set(PLATFORM_CHIP ${TOS_PROJECT_CHIP}) @@ -79,6 +83,13 @@ foreach(comp ${COMPONENT_LIST}) # message(STATUS "comp: ${comp}") add_subdirectory("${TOP_SOURCE_DIR}/src/${comp}") endforeach(comp) + +# add arduino src +if(TOS_FRAMEWORK STREQUAL "arduino") + include(${TOP_SOURCE_DIR}/arduino-tuyaopen/CMakeLists.txt) + message(STATUS "[TOP] LD_FILE: ${LD_FILE}") +endif() + # message(STATUS "[TOP] COMPONENT_LIBS: ${COMPONENT_LIBS}") # copy_public_include("${COMPONENT_PUBINC}" "${HEADER_DIR}") list(APPEND HEADER_DIR ${PLATFORM_PUBINC}) @@ -105,6 +116,11 @@ set(EXAMPLE_LIB "tuyaapp") list(APPEND PLATFORM_NEED_HDIR ${COMPONENT_PUBINC}) string(REPLACE ";" " " PLATFORM_NEED_HDIR "${PLATFORM_NEED_HDIR}") set(PLATFORM_NEED_LIBS "${EXAMPLE_LIB} ${COMPONENTS_ALL_LIB}") +if(TOS_FRAMEWORK STREQUAL "arduino") + if(PLATFORM_NAME STREQUAL "t2") + set(PLATFORM_NEED_LIBS "${EXAMPLE_LIB} ${COMPONENTS_ALL_LIB} stdc++") + endif() +endif() message(STATUS "[TOP] PLATFORM_NEED_LIBS: ${PLATFORM_NEED_LIBS}") include(${TOS_PROJECT_ROOT}/CMakeLists.txt) target_include_directories(${EXAMPLE_LIB} @@ -150,6 +166,7 @@ add_custom_command( ${PLATFORM_PATH} COMMAND + export OPEN_LD_FILE="${LD_FILE}" && ./build_example.sh "${EXAMPLE_NAME}" "${CONFIG_PROJECT_VERSION}" "${PLATFORM_NEED_HDIR}" "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}" "${PLATFORM_NEED_LIBS}" "${EXECUTABLE_OUTPUT_PATH}" "build" "${PLATFORM_CHIP}" @@ -164,6 +181,9 @@ add_custom_target(platform_clean ${PLATFORM_PATH} COMMAND + + COMMAND + export OPEN_LD_FILE="${LD_FILE}" && ./build_example.sh "${EXAMPLE_NAME}" "${CONFIG_PROJECT_VERSION}" "${PLATFORM_NEED_HDIR}" "${CMAKE_ARCHIVE_OUTPUT_DIRECTORY}" "${PLATFORM_NEED_LIBS}" "${EXECUTABLE_OUTPUT_PATH}" "clean" diff --git a/apps/tuya_cloud/switch_demo/project_build.ini b/apps/tuya_cloud/switch_demo/project_build.ini index 6363116..1597aa6 100755 --- a/apps/tuya_cloud/switch_demo/project_build.ini +++ b/apps/tuya_cloud/switch_demo/project_build.ini @@ -1,2 +1,2 @@ -[project:switch_demo_t2] -platform = t2 +[project:switch_demo_t3] +platform = t3 diff --git a/tools/app_template/arduino/CMakeLists.txt b/tools/app_template/arduino/CMakeLists.txt new file mode 100755 index 0000000..c00e989 --- /dev/null +++ b/tools/app_template/arduino/CMakeLists.txt @@ -0,0 +1,32 @@ +## +# @file CMakeLists.txt +# @brief +#/ + +# APP_PATH +set(APP_PATH ${CMAKE_CURRENT_LIST_DIR}) + +# APP_NAME +get_filename_component(APP_NAME ${APP_PATH} NAME) + +# APP_SRC +aux_source_directory(${APP_PATH}/src APP_SRC) + +# APP_INC +set(APP_INC ${APP_PATH}/include) + +######################################## +# Target Configure +######################################## +add_library(${EXAMPLE_LIB}) +message(STATUS "EXAMPLE_LIB:${APP_PATH}") + +target_sources(${EXAMPLE_LIB} + PRIVATE + ${APP_SRC} + ) + +target_include_directories(${EXAMPLE_LIB} + PRIVATE + ${APP_INC} + ) diff --git a/tools/app_template/arduino/Kconfig b/tools/app_template/arduino/Kconfig new file mode 100755 index 0000000..fb4fdb6 --- /dev/null +++ b/tools/app_template/arduino/Kconfig @@ -0,0 +1,22 @@ +choice + prompt "Choice Arduino Board" + default ENABLE_ARDUINO_TUYA_T2 + + config ENABLE_ARDUINO_TUYA_T2 + bool "ENABLE_ARDUINO_TUYA_T2" + + config ENABLE_ARDUINO_TUYA_T3 + bool "ENABLE_ARDUINO_TUYA_T3" +endchoice + +if (ENABLE_ARDUINO_TUYA_T2) + config ARDUINO_BOARD + string + default "ARDUINO_TUYA_T2" +endif + +if (ENABLE_ARDUINO_TUYA_T3) + config ARDUINO_BOARD + string + default "ARDUINO_TUYA_T3" +endif diff --git a/tools/app_template/arduino/project_build.ini b/tools/app_template/arduino/project_build.ini new file mode 100755 index 0000000..d677062 --- /dev/null +++ b/tools/app_template/arduino/project_build.ini @@ -0,0 +1,3 @@ +[project:###PROJECT_NAME###] +platform = ###PLATFORM_NAME### +framework = arduino diff --git a/tools/app_template/arduino/src/hello_world.cpp b/tools/app_template/arduino/src/hello_world.cpp new file mode 100755 index 0000000..20dc82c --- /dev/null +++ b/tools/app_template/arduino/src/hello_world.cpp @@ -0,0 +1,12 @@ +#include "Arduino.h" + +void setup(void) +{ + Serial.begin(115200); +} + +void loop() +{ + Serial.println("hello"); + delay(1000); +} diff --git a/tools/app_template/CMakeLists.txt b/tools/app_template/base/CMakeLists.txt similarity index 100% rename from tools/app_template/CMakeLists.txt rename to tools/app_template/base/CMakeLists.txt diff --git a/tools/app_template/project_build.ini b/tools/app_template/base/project_build.ini similarity index 77% rename from tools/app_template/project_build.ini rename to tools/app_template/base/project_build.ini index 8414992..a6ab324 100755 --- a/tools/app_template/project_build.ini +++ b/tools/app_template/base/project_build.ini @@ -1,2 +1,3 @@ [project:###PROJECT_NAME###] platform = ###PLATFORM_NAME### +framework = base diff --git a/tools/app_template/src/hello_world.c b/tools/app_template/base/src/hello_world.c old mode 100644 new mode 100755 similarity index 100% rename from tools/app_template/src/hello_world.c rename to tools/app_template/base/src/hello_world.c diff --git a/tools/cmake/util.cmake b/tools/cmake/util.cmake index df340ec..ba9d4c6 100755 --- a/tools/cmake/util.cmake +++ b/tools/cmake/util.cmake @@ -74,10 +74,13 @@ endfunction() function(list_subdirectories RETURN DIR) file(GLOB_RECURSE subdirectories LIST_DIRECTORIES true "${DIR}/*") - set(non_hidden_subdirectories "") + set(non_hidden_subdirectories ${DIR}) foreach(subdir ${subdirectories}) - if(IS_DIRECTORY "${subdir}" AND NOT subdir MATCHES "/[.]") - list(APPEND non_hidden_subdirectories "${subdir}") + if(IS_DIRECTORY "${subdir}") + get_filename_component(subdir_name ${subdir} NAME) + if(NOT ${subdir_name} MATCHES "^\\.") + list(APPEND non_hidden_subdirectories "${subdir}") + endif() endif() endforeach() # message(STATUS "[UTIL] non_hidden_subdirectories:[${non_hidden_subdirectories}]") diff --git a/tos b/tos index 47b3e57..339a152 100755 --- a/tos +++ b/tos @@ -47,6 +47,30 @@ function check_submodules() cd - > /dev/null } +function check_arduino() +{ + ARDUINO_REPO="https://github.com/tuya/arduino-tuyaopen.git" + ARDUINO_PATH=$OPEN_SDK_ROOT/arduino-tuyaopen + cd $OPEN_SDK_ROOT + if [ -d $ARDUINO_PATH ]; then + show "" "Skip download, exist [$ARDUINO_PATH]." + show "${fore[yellow]}" "If you want to download it again, please remove it." + return 0 + fi + git clone $ARDUINO_REPO + if [ ! "0" = "$?" ]; then + show "${fore[red]}" "Clone repo [$ARDUINO_REPO] failed, please try again." + if [ -d "$ARDUINO_PATH" ]; then + show "${fore[yellow]}" "Delete [$ARDUINO_PATH] first." + fi + exit 1 + fi + cd $ARDUINO_PATH + TAG=`git describe --tags --abbrev=0` + git checkout $TAG + git submodule update --init +} + function version_exec() { show "" "1.0.0" @@ -124,6 +148,17 @@ function download_platform_by_name() function new_exec() { + TEMPLATE_NAME=$1 + if [ -z "$TEMPLATE_NAME" ]; then + TEMPLATE_NAME="base" + fi + TEMPLATE_PATH=$OPEN_SDK_ROOT/tools/app_template/$TEMPLATE_NAME + if [ ! -d $TEMPLATE_PATH ]; then + NOTE="No template named [$TEMPLATE_NAME] exists." + show "${fore[red]}" "$NOTE" + exit 1 + fi + read -p "Input project name: " PROJ_NAME if [ -z "$PROJ_NAME" ]; then NOTE="Project name is empty." @@ -158,7 +193,7 @@ commit: $PLATFORM_COMMIT chip: $PLATFORM_CHIP" # end PLATFORM_STR show "" "$PLATFORM_STR" - cp -a $OPEN_SDK_ROOT/tools/app_template $PROJECT_ROOT/$PROJ_NAME + cp -a $TEMPLATE_PATH $PROJECT_ROOT/$PROJ_NAME sed -i "s/###PROJECT_NAME###/${PROJ_NAME}_${PLATFORM_NAME}/g" $PROJECT_ROOT/$PROJ_NAME/project_build.ini sed -i "s/###PLATFORM_NAME###/$PLATFORM_NAME/g" $PROJECT_ROOT/$PROJ_NAME/project_build.ini if [ -n "$PLATFORM_CHIP" ]; then @@ -194,6 +229,13 @@ function build_exec() RET=1 continue fi + PROJECT_FRAMEWORK=$(echo "$PROJECT_INFO" | grep -oP '(?<=framework = ).*(?=$)') + if [ -z "$PROJECT_FRAMEWORK" ]; then + PROJECT_FRAMEWORK="base" + fi + if [ "$PROJECT_FRAMEWORK" = "arduino" ]; then + check_arduino + fi PROJECT_CHIP=$(echo "$PROJECT_INFO" | grep -oP '(?<=chip = ).*(?=$)') PLATFORM_PATH=$OPEN_SDK_ROOT/platform/$PROJECT_PLATFORM @@ -208,7 +250,7 @@ function build_exec() mkdir -p $PROJECT_ROOT/.build/$PROJ cd $PROJECT_ROOT/.build/$PROJ if [ -f "$PLATFORM_PATH/build_setup.sh" ]; then - $PLATFORM_PATH/build_setup.sh "${PROJ}" "${PROJECT_PLATFORM}" "${PROJECT_CHIP}" + $PLATFORM_PATH/build_setup.sh "${PROJ}" "${PROJECT_PLATFORM}" "${PROJECT_CHIP}" "${PROJECT_FRAMEWORK}" fi cmake $OPEN_SDK_ROOT -DTOS_PROJECT_NAME=$PROJ -DTOS_PROJECT_ROOT=$PROJECT_ROOT -DTOS_PROJECT_PLATFORM=$PROJECT_PLATFORM -DTOS_FRAMEWORK=$PROJECT_FRAMEWORK -DTOS_PROJECT_CHIP=$PROJECT_CHIP make example @@ -216,7 +258,7 @@ function build_exec() RET=1 fi if [ -f "$PLATFORM_PATH/build_hock.sh" ]; then - $PLATFORM_PATH/build_hock.sh "${PROJ}" "${PROJECT_PLATFORM}" "${PROJECT_CHIP}" + $PLATFORM_PATH/build_hock.sh "${PROJ}" "${PROJECT_PLATFORM}" "${PROJECT_CHIP}" "${PROJECT_FRAMEWORK}" fi cd - > /dev/null done @@ -271,6 +313,10 @@ function menuconfig_exec() PLATFORMS_DIR="$OPEN_SDK_ROOT/platform" PLATFORM_PATH="${PLATFORMS_DIR}/${PROJECT_PLATFORM}" + if [ ! -d "$PLATFORM_PATH" ]; then + download_platform_by_name $PROJECT_PLATFORM $PROJECT_CHIP + fi + DOT_CONFIG_DIR="$PROJECT_ROOT/.build/$PROJ/cache" KCONFIG_CATALOG="CatalogKconfig" DOT_CONFIG="using.config" @@ -281,6 +327,9 @@ function menuconfig_exec() mkdir -p ${DOT_CONFIG_DIR} cd ${DOT_CONFIG_DIR} + if [ ! -f $DOT_CONFIG ]; then + cp $PLATFORM_PATH/default.config $DOT_CONFIG + fi python3 $KCONFIG_TOOLS/set_catalog_config.py -b $PLATFORM_PATH -s $OPEN_SDK_ROOT/src -a $PROJECT_ROOT -o $KCONFIG_CATALOG bash ${KCONFIG_TOOLS}/run_menuconfig.sh "${KCONFIG_CATALOG}" "$DOT_CONFIG" python3 ${KCONFIG_TOOLS}/conf2cmake.py -c "${DOT_CONFIG_DIR}/${DOT_CONFIG}" -o "${CMAKE_CONFIG}" @@ -342,7 +391,7 @@ Usage: $OPEN_BUILD COMMAND [ARGS]... Commands: version - Show TOS verson check - Check command and version - new - New project + new - New project [base(default) / auduino] build - Build specific project or all projects [name] (default build all project by project_build.ini) clean - Clean specific project or all projects @@ -366,7 +415,7 @@ case $1 in check_exec ;; new) - new_exec + new_exec $2 ;; build) build_exec $2 @@ -375,7 +424,7 @@ case $1 in clean_exec $2 ;; menuconfig) - menuconfig_exec + menuconfig_exec $2 ;; build_all) build_all_exec $2