From cf5671b162101c8c9fbf4287d46bf90e17e4e7a2 Mon Sep 17 00:00:00 2001 From: Dzulfikar Ahmad Samhan <23522019@std.stei.itb.ac.id> Date: Tue, 23 Apr 2024 15:46:56 +0000 Subject: [PATCH] Initial commit --- .gitignore | 4 + .vscode/tasks.json | 97 +++++++++++++ .west/app-config | 7 + .west/base-config | 6 + .west/config | 7 + README.md | 11 +- debug-app/CMakeLists.txt | 4 + debug-app/Kconfig | 0 debug-app/app/CMakeLists.txt | 5 + debug-app/app/boot.conf | 6 + debug-app/app/prj.conf | 40 ++++++ debug-app/app/rsa-2048-priv.pem | 28 ++++ debug-app/app/src/main.cpp | 50 +++++++ debug-app/include/debug-handler.h | 27 ++++ debug-app/include/driver-hook.h | 25 ++++ debug-app/include/main-handler.h | 31 ++++ debug-app/include/utilities.h | 27 ++++ debug-app/module/CMakeLists.txt | 2 + debug-app/module/driver-hook/CMakeLists.txt | 1 + .../module/driver-hook/src/driver-hook.cpp | 74 ++++++++++ debug-app/module/handler/CMakeLists.txt | 2 + .../handler/debug-handler/CMakeLists.txt | 1 + .../debug-handler/src/debug-handler.cpp | 64 +++++++++ .../handler/main-handler/CMakeLists.txt | 1 + .../handler/main-handler/src/main-handler.cpp | 134 ++++++++++++++++++ debug-app/west.yml | 15 ++ debug-app/zephyr/module.yml | 7 + scripts/build_zephyr.sh | 31 ++++ scripts/docker_run.sh | 17 +++ scripts/env.sh | 5 + 30 files changed, 724 insertions(+), 5 deletions(-) create mode 100644 .gitignore create mode 100644 .vscode/tasks.json create mode 100644 .west/app-config create mode 100644 .west/base-config create mode 100644 .west/config create mode 100644 debug-app/CMakeLists.txt create mode 100644 debug-app/Kconfig create mode 100644 debug-app/app/CMakeLists.txt create mode 100644 debug-app/app/boot.conf create mode 100644 debug-app/app/prj.conf create mode 100644 debug-app/app/rsa-2048-priv.pem create mode 100644 debug-app/app/src/main.cpp create mode 100644 debug-app/include/debug-handler.h create mode 100644 debug-app/include/driver-hook.h create mode 100644 debug-app/include/main-handler.h create mode 100644 debug-app/include/utilities.h create mode 100644 debug-app/module/CMakeLists.txt create mode 100644 debug-app/module/driver-hook/CMakeLists.txt create mode 100644 debug-app/module/driver-hook/src/driver-hook.cpp create mode 100644 debug-app/module/handler/CMakeLists.txt create mode 100644 debug-app/module/handler/debug-handler/CMakeLists.txt create mode 100644 debug-app/module/handler/debug-handler/src/debug-handler.cpp create mode 100644 debug-app/module/handler/main-handler/CMakeLists.txt create mode 100644 debug-app/module/handler/main-handler/src/main-handler.cpp create mode 100644 debug-app/west.yml create mode 100644 debug-app/zephyr/module.yml create mode 100755 scripts/build_zephyr.sh create mode 100755 scripts/docker_run.sh create mode 100755 scripts/env.sh diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..99dcb4b --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.build* +.ccache +pythonenv* +sample* \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..7e398e0 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,97 @@ +{ + // See https://go.microsoft.com/fwlink/?LinkId=733558 + // for the documentation about the tasks.json format + "version": "2.0.0", + "tasks": [ + { + "label": "Build app posix", + "type": "shell", + "command": "./scripts/docker_run.sh west build -b native_sim debug-app/app --build-dir .build-debug-app-native", + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "clear": true + } + }, + { + "label": "Build app evk", + "type": "shell", + "command": "./scripts/docker_run.sh west build -b stm32f4_disco debug-app/app --build-dir .build-debug-app-evk", + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "clear": true + } + }, + // { + // "label": "Flash app only", + // "type": "shell", + // "command": "./scripts/docker_run.sh west flash --build-dir .build-mtp-lpmcu-lpc55s28-app-only", + // "problemMatcher": [ + // "$gcc" + // ], + // "group": { + // "kind": "build", + // "isDefault": true + // }, + // "presentation": { + // "clear": true + // } + // }, + // { + // "label": "Run Posix", + // "type": "shell", + // "command": " ./scripts/docker_run.sh west build -t run --build-dir .build-debug-app-qemu", + // "problemMatcher": [ + // "$gcc" + // ], + // "group": { + // "kind": "build", + // "isDefault": true + // }, + // "presentation": { + // "clear": true + // } + // }, + { + "label": "Clean", + "type": "shell", + "command": "./scripts/docker_run.sh rm -rf .build-debug-app*", + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "clear": true + } + }, + { + "label": "Update", + "type": "shell", + "command": "./scripts/docker_run.sh west update", + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "presentation": { + "clear": true + } + } + ] +} \ No newline at end of file diff --git a/.west/app-config b/.west/app-config new file mode 100644 index 0000000..4324b6b --- /dev/null +++ b/.west/app-config @@ -0,0 +1,7 @@ +[manifest] +path = debug-app +file = west.yml + +[zephyr] +base = zephyr + diff --git a/.west/base-config b/.west/base-config new file mode 100644 index 0000000..28e2fc4 --- /dev/null +++ b/.west/base-config @@ -0,0 +1,6 @@ +[manifest] +path = zephyr + +[zephyr] +base = zephyr + diff --git a/.west/config b/.west/config new file mode 100644 index 0000000..4324b6b --- /dev/null +++ b/.west/config @@ -0,0 +1,7 @@ +[manifest] +path = debug-app +file = west.yml + +[zephyr] +base = zephyr + diff --git a/README.md b/README.md index bf80dd5..e5b0217 100644 --- a/README.md +++ b/README.md @@ -4,11 +4,12 @@ ## Getting started -To make it easy for you to get started with GitLab, here's a list of recommended next steps. +- update SDK for the first time after clone. +- build `ctrl+shift+b` choose `build for evk` +- run on native `./scripts/docker_run.sh ./.build-debug-app-native/zephyr/zephyr.exe` +- run on evk `ctrl+shift+b` choose `Flash app evk` -Already a pro? Just edit this README.md and make it your own. Want to make it easy? [Use the template at the bottom](#editing-this-readme)! - -## Add your files +<!-- ## Add your files - [ ] [Create](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#create-a-file) or [upload](https://docs.gitlab.com/ee/user/project/repository/web_editor.html#upload-a-file) files - [ ] [Add files using the command line](https://docs.gitlab.com/ee/gitlab-basics/add-file.html#add-a-file-using-the-command-line) or push an existing Git repository with the following command: @@ -90,4 +91,4 @@ Show your appreciation to those who have contributed to the project. For open source projects, say how it is licensed. ## Project status -If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. +If you have run out of energy or time for your project, put a note at the top of the README saying that development has slowed down or stopped completely. Someone may choose to fork your project or volunteer to step in as a maintainer or owner, allowing your project to keep going. You can also make an explicit request for maintainers. --> diff --git a/debug-app/CMakeLists.txt b/debug-app/CMakeLists.txt new file mode 100644 index 0000000..5e39eb9 --- /dev/null +++ b/debug-app/CMakeLists.txt @@ -0,0 +1,4 @@ +zephyr_include_directories(include) + +add_subdirectory(module) +# add_subdirectory(drivers) \ No newline at end of file diff --git a/debug-app/Kconfig b/debug-app/Kconfig new file mode 100644 index 0000000..e69de29 diff --git a/debug-app/app/CMakeLists.txt b/debug-app/app/CMakeLists.txt new file mode 100644 index 0000000..578526d --- /dev/null +++ b/debug-app/app/CMakeLists.txt @@ -0,0 +1,5 @@ +cmake_minimum_required(VERSION 3.13.1) +find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE}) + +project(debug-tracing LANGUAGES C CXX) +target_sources(app PRIVATE src/main.cpp) diff --git a/debug-app/app/boot.conf b/debug-app/app/boot.conf new file mode 100644 index 0000000..3d9e7f6 --- /dev/null +++ b/debug-app/app/boot.conf @@ -0,0 +1,6 @@ +# CONFIG_BOOTLOADER_MCUBOOT=y +# CONFIG_MCUBOOT_SIGNATURE_KEY_FILE="mtp-lpmcu/app/rsa-2048-priv.pem" +# CONFIG_MCUBOOT_EXTRA_IMGTOOL_ARGS="--align 4 --header-size 0x400 --pad-header --slot-size 0x30000 --max-sectors 32" +# CONFIG_ROM_START_OFFSET=0x400 +# CONFIG_USE_DT_CODE_PARTITION=y + \ No newline at end of file diff --git a/debug-app/app/prj.conf b/debug-app/app/prj.conf new file mode 100644 index 0000000..dc438be --- /dev/null +++ b/debug-app/app/prj.conf @@ -0,0 +1,40 @@ +# C++ +CONFIG_CPP=y +CONFIG_CPP_MAIN=y +CONFIG_STD_CPP17=y +CONFIG_MINIMAL_LIBCPP=y +CONFIG_CPLUSPLUS=y +CONFIG_LIB_CPLUSPLUS=y +CONFIG_NEWLIB_LIBC=y + +# Zephyr +CONFIG_MAIN_STACK_SIZE=2048 +CONFIG_HEAP_MEM_POOL_SIZE=512 +CONFIG_THREAD_NAME=y +CONFIG_ASSERT=y +CONFIG_NO_OPTIMIZATIONS=y + +# Peripheral +# CONFIG_ADC=y +# CONFIG_ADC_ASYNC=y +# CONFIG_SPI=y +# CONFIG_SPI_ASYNC=y +# CONFIG_GPIO=y +# CONFIG_I2C=y +# CONFIG_SERIAL=y +# CONFIG_UART_ASYNC_API=y +# CONFIG_UART_INTERRUPT_DRIVEN=y + +#bus +CONFIG_ZBUS=y +CONFIG_ZBUS_LOG_LEVEL_INF=y +CONFIG_ZBUS_RUNTIME_OBSERVERS=y +CONFIG_ZBUS_CHANNEL_NAME=y + +# PM +# CONFIG_PM=y +# CONFIG_PM_DEVICE=y +# CONFIG_PM_DEVICE_RUNTIME=y + +# Log +CONFIG_LOG=y diff --git a/debug-app/app/rsa-2048-priv.pem b/debug-app/app/rsa-2048-priv.pem new file mode 100644 index 0000000..8cc07ae --- /dev/null +++ b/debug-app/app/rsa-2048-priv.pem @@ -0,0 +1,28 @@ +-----BEGIN PRIVATE KEY----- +MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCjicog4G1DPSIL +8hwIulmBSmRV7wxoIUePokMa0O1XAhLiQwepYQsgAJ0S8B+M9XduDeNJ3JZY8hEA +3RmrKw5m2OcLJ7Aix09DKAectRu9O9JFUyxoCdxRz6sjtL4XKNdBD+f19ez0cxHK +JptJEPxUjskFvfeN5BQXQXUsZoT/G/xvMDu0PpkZ2W2KxrAOzYjr07+AT7N73nOw +HB/SYb4qgycGI8sPfYr1OVZaKCM51Q4gl/qy2zsjCaKiZ6DiOm0IfSfxSy5qIe0Y +s/yuxvo3i6lGR299c+p2xBb/q4imnXjfnM5aikSBFBtyAS3dN2+eV1nqgqpS4WZG +qzAFfHZpAgMBAAECggEAITYaHaeeIN3RJH5oWZBonWRbcIUxrpL1mrKP8Kn1zSnv +ZlJJA8mgrgDZ5U7tBV4TQT8iRa19vfX45IVCUXvWuqW5dA89Cq8RxnhiZV11Uu6L +YwdDWSSJgPbM0V1DDmZ9ommlyJJHKxSUTAqflgzURaLSqa8cz0y+lMN3FYuDXmBA +Lc8NjVZoBPqiR58z2J5efrhoOsnjKxEJeJOMOLItdCrH3KxbgADtkmHqysHeOHad +tYJUdFU7HupTgt7YWWas44wtgC2I1E4rT5ti6njeALPzmaVYTRNNomkGW86f3HTT +yRNUp+GtHxvsj2c/cnhV/dQJ9Bd1mlIqKpQrQiP5tQKBgQDEvMoDYR4dHDGUAklh +dIltBCXM6HcH7WHJaUpmMcQqbpFCbTZUx2wIUiZ+pSFnLGFijOpSApIsu99gfUVp +dG6s8hu+T65MZjWyh9XT6ux0CRjnUu81PhnZzn1h9vosh6hY5kusfEIXPEzXsG3i +uYgWjtNGWld5EQ0phsuHV+lBDQKBgQDUzOBTKMcQlNjvxy0EXCSv1nJw7FUtbjKQ +BN9gRIk0YYNXOQ/tZ6rrnAT8RN7I0WslWruvw4za1PLJa1G2ZcKWynvXGVf281tc +LZfwBAKtWgVJcC8ZkHomPoVA3Tj9isnmV/p9YNiGHjnsrdH2kxAm/A80nFnN13/C +MM8OwXMbzQKBgQCorzaGrz6itwnLXiqM5N7wGh5PEwL8SQ0PJLgl54XK3AKwcfOj +vVljyQw4il2ZmPPnC+vdZVbwBCYDFWXuhWZf4zbmdmt8Fib0bINqsIMM+r/fl7yC +ajzAA8x9ssXOaIJvtwYrXl9MjOdWCHAvdeS5cry0FcHqjDQ85tabtUTKAQKBgAXO +QSCDsEqnhPOlv3iq+uVBOIYULI68vuxm9aEuvX+MWYT7rY+QkH01GH25c0qPTBkM +zKoS77jYrncIUoZjaFes1owbyOaX5RoytD58HYaVOieyw9YytuJsOj52yqNAMhny +tIbPKZkfIf1vEJdlk788iT2qNlkDVBz8L+n7JSTRAoGALLRp5o8alGFg1O1HFTTf +1P0x+5vC3mpg/K14xFMHttMcyPgMg4BAVdr/012tqmlwOS3vvankblECrR8e2ZCH +AfpGOcEPktCphYHFKY5+UmN06YQ2HdwIN7sNkpH+cYr7OX98f3NdO5pP30KLgPgO +zyPvjQG1+bSvH/bUpB1MluY= +-----END PRIVATE KEY----- diff --git a/debug-app/app/src/main.cpp b/debug-app/app/src/main.cpp new file mode 100644 index 0000000..b440f77 --- /dev/null +++ b/debug-app/app/src/main.cpp @@ -0,0 +1,50 @@ +#include <zephyr/kernel.h> +#include <zephyr/logging/log.h> +#include <zephyr/zbus/zbus.h> + +#include "main-handler.h" +#include "debug-handler.h" +#include "driver-hook.h" +#include "utilities.h" + + +LOG_MODULE_REGISTER(main); + +DriverMessage_t defaultDriverMsg; +StateMessage_t defaultStateMsg; + +ZBUS_CHAN_DEFINE(driverRequestChannel, /* Name */ + DriverMessage_t, /* Message type */ + NULL, /* Validator */ + NULL, /* User data */ + ZBUS_OBSERVERS_EMPTY, /* observers */ + ZBUS_MSG_INIT(defaultDriverMsg) +); + +ZBUS_CHAN_DEFINE(driverResponseChannel, /* Name */ + DriverMessage_t, /* Message type */ + NULL, /* Validator */ + NULL, /* User data */ + ZBUS_OBSERVERS_EMPTY, /* observers */ + ZBUS_MSG_INIT(defaultDriverMsg) +); + +ZBUS_CHAN_DEFINE(stateChannel, /* Name */ + StateMessage_t, /* Message type */ + NULL, /* Validator */ + NULL, /* User data */ + ZBUS_OBSERVERS_EMPTY, /* observers */ + ZBUS_MSG_INIT(defaultStateMsg) +); + +int main() +{ + MainHandler mainHandler; + DriverHook DriverHook; + DebugHandler debugHanddler; + + while(true) { + // LOG_INF("Hello World! run app\n"); + k_sleep(K_SECONDS(5)); + } +} \ No newline at end of file diff --git a/debug-app/include/debug-handler.h b/debug-app/include/debug-handler.h new file mode 100644 index 0000000..7264d2c --- /dev/null +++ b/debug-app/include/debug-handler.h @@ -0,0 +1,27 @@ +#include <zephyr/kernel.h> + +class DebugHandler { +public: + /** Constructor. + * + **/ + DebugHandler(); + + /** Destructor. + * + **/ + ~DebugHandler(); + + + void Init(); + + int Deinit(); + + static void DriverResponseListener(const struct zbus_channel *chan); + static void StateListener(const struct zbus_channel *chan); + +private: + static void DebugThread(DebugHandler *context); + + +}; \ No newline at end of file diff --git a/debug-app/include/driver-hook.h b/debug-app/include/driver-hook.h new file mode 100644 index 0000000..0ca8287 --- /dev/null +++ b/debug-app/include/driver-hook.h @@ -0,0 +1,25 @@ +#pragma once + +#include <zephyr/kernel.h> + +class DriverHook { +public: + /** Constructor. + * + **/ + DriverHook(); + + /** Destructor. + * + **/ + ~DriverHook(); + + + void Init(); + + int Deinit(); + + static void DriverRequestListener(const struct zbus_channel *chan); + +private: +}; \ No newline at end of file diff --git a/debug-app/include/main-handler.h b/debug-app/include/main-handler.h new file mode 100644 index 0000000..1c4bc03 --- /dev/null +++ b/debug-app/include/main-handler.h @@ -0,0 +1,31 @@ +#include <zephyr/kernel.h> +#include "utilities.h" + +class MainHandler { +public: + /** Constructor. + * + **/ + MainHandler(); + + /** Destructor. + * + **/ + ~MainHandler(); + + + void Init(); + + int Deinit(); + + static void DriverResponseListener(const struct zbus_channel *chan); + +private: + + DriverNumb_e requestedResponse; + DriverMessage_t driverResponse_g; + + static void MainThread(MainHandler *context); + bool DriverRequest(DriverMessage_t *sentMessage, DriverMessage_t *driverResponse, k_timeout_t timeout); + +}; \ No newline at end of file diff --git a/debug-app/include/utilities.h b/debug-app/include/utilities.h new file mode 100644 index 0000000..ce70b9e --- /dev/null +++ b/debug-app/include/utilities.h @@ -0,0 +1,27 @@ +#pragma once + +#include <vector> +#include <string> + +typedef enum DriverNumb{ + SENSOR_1, + SENSOR_2, + SENSOR_3, + ACTUATOR_1, +} DriverNumb_e; + +typedef enum StateFlag{ + STATE, + DATA_1, + DATA_2 +} StateFlag_e; + +typedef struct DriverMessage { + DriverNumb_e number; + std::vector<uint8_t> value; +} DriverMessage_t; + +typedef struct StateMessage { + StateFlag_e flag; + std::vector<uint8_t> value; +} StateMessage_t; \ No newline at end of file diff --git a/debug-app/module/CMakeLists.txt b/debug-app/module/CMakeLists.txt new file mode 100644 index 0000000..379a5da --- /dev/null +++ b/debug-app/module/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(driver-hook) +add_subdirectory(handler) \ No newline at end of file diff --git a/debug-app/module/driver-hook/CMakeLists.txt b/debug-app/module/driver-hook/CMakeLists.txt new file mode 100644 index 0000000..719b87d --- /dev/null +++ b/debug-app/module/driver-hook/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(src/driver-hook.cpp) \ No newline at end of file diff --git a/debug-app/module/driver-hook/src/driver-hook.cpp b/debug-app/module/driver-hook/src/driver-hook.cpp new file mode 100644 index 0000000..2fe05b4 --- /dev/null +++ b/debug-app/module/driver-hook/src/driver-hook.cpp @@ -0,0 +1,74 @@ +#include <zephyr/logging/log.h> +#include <zephyr/zbus/zbus.h> + +#include <stdio.h> +#include <string.h> + +#include "driver-hook.h" +#include "utilities.h" + +LOG_MODULE_REGISTER(Driver_Hook); +ZBUS_CHAN_DECLARE(driverRequestChannel, driverResponseChannel); + +static DriverHook *context; + +ZBUS_LISTENER_DEFINE(driverHookListener, context->DriverRequestListener); + +DriverHook::DriverHook(){ + context = this; + zbus_chan_add_obs(&driverRequestChannel, &driverHookListener, K_NO_WAIT); +} + +DriverHook::~DriverHook(){ + +} + + +void DriverHook::Init(){ + +} + +int DriverHook::Deinit(){ + return 0; +} + +void DriverHook::DriverRequestListener(const struct zbus_channel *chan){ + const DriverMessage_t *msg; + if (&driverRequestChannel == chan) { + msg = (DriverMessage_t *)zbus_chan_const_msg(chan); // Direct message access + LOG_DBG("From listener -> driver =%d, value 0=%d", msg->number, msg->value[0]); + } + else{ + return; + } + + std::vector<uint8_t> data; + data.push_back(msg->number); + + DriverMessage_t sentMessage = { + .number = msg->number, + .value = data + }; + + switch (msg->number) + { + case SENSOR_1: + LOG_INF("Sensor %d", msg->number); + break; + + case SENSOR_2: + LOG_INF("Sensor %d", msg->number); + break; + + case SENSOR_3: + LOG_INF("Sensor %d", msg->number); + break; + + default: + LOG_ERR("Invalid Sensor %d", msg->number); + break; + } + + int status = zbus_chan_pub(&driverResponseChannel, &sentMessage, K_MSEC(200)); + LOG_INF("Status pub %d", status); +} \ No newline at end of file diff --git a/debug-app/module/handler/CMakeLists.txt b/debug-app/module/handler/CMakeLists.txt new file mode 100644 index 0000000..2ae79be --- /dev/null +++ b/debug-app/module/handler/CMakeLists.txt @@ -0,0 +1,2 @@ +add_subdirectory(main-handler) +add_subdirectory(debug-handler) \ No newline at end of file diff --git a/debug-app/module/handler/debug-handler/CMakeLists.txt b/debug-app/module/handler/debug-handler/CMakeLists.txt new file mode 100644 index 0000000..bae7d3b --- /dev/null +++ b/debug-app/module/handler/debug-handler/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(src/debug-handler.cpp) \ No newline at end of file diff --git a/debug-app/module/handler/debug-handler/src/debug-handler.cpp b/debug-app/module/handler/debug-handler/src/debug-handler.cpp new file mode 100644 index 0000000..d7553c9 --- /dev/null +++ b/debug-app/module/handler/debug-handler/src/debug-handler.cpp @@ -0,0 +1,64 @@ +#include <zephyr/kernel.h> +#include <zephyr/logging/log.h> +#include <zephyr/zbus/zbus.h> + +#include <stdio.h> +#include <string.h> + +#include "debug-handler.h" +#include "utilities.h" + +#define DEBUG_THREAD_STACK_SIZE 2048 +#define DEBUG_THREAD_PRIORITY 5 + +LOG_MODULE_REGISTER(Debug_Handler); +ZBUS_CHAN_DECLARE(driverResponseChannel, stateChannel); + +K_THREAD_STACK_DEFINE(debugThreadStack, DEBUG_THREAD_STACK_SIZE); + +// Thread and kernels +struct k_thread debugThreadHandler; +k_tid_t debugThreadId; +static DebugHandler *context; + +ZBUS_LISTENER_DEFINE(debugDriverListener, context->DriverResponseListener); +ZBUS_LISTENER_DEFINE(stateListener, context->StateListener); + +DebugHandler::DebugHandler(){ + context = this; + debugThreadId = k_thread_create(&debugThreadHandler, debugThreadStack, DEBUG_THREAD_STACK_SIZE, + (k_thread_entry_t) DebugThread, + this, NULL, NULL, + DEBUG_THREAD_PRIORITY, 0, K_MSEC(10)); + k_thread_suspend(debugThreadId); + zbus_chan_add_obs(&driverResponseChannel, &debugDriverListener, K_NO_WAIT); + zbus_chan_add_obs(&stateChannel, &stateListener, K_NO_WAIT); +} + +void DebugHandler::Init(){ + +} + +int DebugHandler::Deinit(){ + return 0; +} + +void DebugHandler::DebugThread(DebugHandler *context){ + +} + +void DebugHandler::DriverResponseListener(const struct zbus_channel *chan){ + const DriverMessage_t *driver; + if (&driverResponseChannel == chan) { + driver = (DriverMessage_t *)zbus_chan_const_msg(chan); // Direct message access + LOG_INF("From listener -> driver =%d, value 0=%d", driver->number, driver->value[0]); + } +} + +void DebugHandler::StateListener(const struct zbus_channel *chan){ + const StateMessage_t *state; + if (&stateChannel == chan) { + state = (StateMessage_t *)zbus_chan_const_msg(chan); // Direct message access + LOG_INF("From listener -> state =%d, value 0=%d", state->flag, state->value[0]); + } +} \ No newline at end of file diff --git a/debug-app/module/handler/main-handler/CMakeLists.txt b/debug-app/module/handler/main-handler/CMakeLists.txt new file mode 100644 index 0000000..031d687 --- /dev/null +++ b/debug-app/module/handler/main-handler/CMakeLists.txt @@ -0,0 +1 @@ +zephyr_library_sources(src/main-handler.cpp) \ No newline at end of file diff --git a/debug-app/module/handler/main-handler/src/main-handler.cpp b/debug-app/module/handler/main-handler/src/main-handler.cpp new file mode 100644 index 0000000..24dd688 --- /dev/null +++ b/debug-app/module/handler/main-handler/src/main-handler.cpp @@ -0,0 +1,134 @@ +#include <zephyr/kernel.h> +#include <zephyr/logging/log.h> +#include <zephyr/zbus/zbus.h> + +#include <stdio.h> +#include <string.h> + +#include "main-handler.h" + +#define MAIN_THREAD_STACK_SIZE 2048 +#define MAIN_THREAD_PRIORITY 5 + +LOG_MODULE_REGISTER(Main_Handler); + +// Internal +static MainHandler *context; + +K_THREAD_STACK_DEFINE(mainThreadStack, MAIN_THREAD_STACK_SIZE); +ZBUS_CHAN_DECLARE(driverResponseChannel, driverRequestChannel, stateChannel); + +ZBUS_SUBSCRIBER_DEFINE(stateSubs, 1); +// ZBUS_SUBSCRIBER_DEFINE(sensorSubs, 1); +ZBUS_LISTENER_DEFINE(driverListener, context->DriverResponseListener); + +// ZBUS_CHAN_ADD_OBS(driverResponseChannel, sensorSubs, 3); + + +// Thread and kernels +struct k_thread mainThreadHandler; +k_tid_t mainThreadId; +static struct k_sem responseWait; + +MainHandler::MainHandler(){ + context = this; + mainThreadId = k_thread_create(&mainThreadHandler, mainThreadStack, MAIN_THREAD_STACK_SIZE, + (k_thread_entry_t) MainThread, + this, NULL, NULL, + MAIN_THREAD_PRIORITY, 0, K_NO_WAIT); + + k_sem_init(&responseWait, 0, 1); + + // zbus_chan_add_obs(&driverResponseChannel, &sensorSubs, K_MSEC(200)); + zbus_chan_add_obs(&driverResponseChannel, &driverListener, K_NO_WAIT); +} + +void MainHandler::Init(){ + +} + +int MainHandler::Deinit(){ + return 0; +} + +void MainHandler::MainThread(MainHandler *context){ + // const struct zbus_channel *chan; + uint8_t currentState = 1; + // while (!zbus_sub_wait(&stateSubs, &chan, K_MSEC(200))) { + // StateMessage_t state; + + // if (&stateChannel == chan) { + // // Indirect message access + // zbus_chan_read(&stateChannel, &state, K_NO_WAIT); + // currentState = state.value[0]; + // LOG_DBG("From subscriber -> flag %d data: %d", state.flag, state.value[0]); + // } + // } + while (true) + { + LOG_INF("main thread run"); + std::vector<uint8_t> stateVec; + stateVec.push_back(currentState); + StateMessage_t stateMsg{ + .flag = STATE, + .value = stateVec + }; + zbus_chan_pub(&stateChannel, &stateMsg, K_MSEC(200)); + + switch (currentState) + { + case 1:{ + LOG_INF("entering state 1. get data from sensor 1"); + DriverMessage_t configVector, responseVector; + configVector.number = SENSOR_1; + configVector.value.push_back(SENSOR_1); + if(context->DriverRequest(&configVector, &responseVector, K_MSEC(200))){ + LOG_INF("get sensor data %d", responseVector.value[0]); + + } + else{ + LOG_INF("fail"); + } + break; + } + + case 2: + /* code */ + break; + + case 3: + /* code */ + break; + default: + break; + } + } + +} + +void MainHandler::DriverResponseListener(const struct zbus_channel *chan){ + const DriverMessage_t *driver; + if (&driverResponseChannel == chan) { + driver = (DriverMessage_t *)zbus_chan_const_msg(chan); // Direct message access + if(driver->number == context->requestedResponse){ + k_sem_give(&responseWait); + context->driverResponse_g = *driver; + } + } +} + +bool MainHandler::DriverRequest(DriverMessage_t *sentMessage, DriverMessage_t *driverResponse, k_timeout_t timeout){ + zbus_chan_pub(&driverRequestChannel, sentMessage, K_MSEC(200)); + + requestedResponse = sentMessage->number; + + if (k_sem_take(&responseWait, timeout) == 0) { + *driverResponse = driverResponse_g; + LOG_INF("data available!"); + + } else { + LOG_ERR("timeout"); + return false; + } + return true; +} \ No newline at end of file diff --git a/debug-app/west.yml b/debug-app/west.yml new file mode 100644 index 0000000..afe498e --- /dev/null +++ b/debug-app/west.yml @@ -0,0 +1,15 @@ +manifest: + remotes: + - name: zephyrproject-rtos + url-base: https://github.com/zephyrproject-rtos + + projects: + - name: zephyr + remote: zephyrproject-rtos + revision: c0e6629 + import: + name-allowlist: + - cmsis + - mbedtls + - hal_stm32 + \ No newline at end of file diff --git a/debug-app/zephyr/module.yml b/debug-app/zephyr/module.yml new file mode 100644 index 0000000..b01da80 --- /dev/null +++ b/debug-app/zephyr/module.yml @@ -0,0 +1,7 @@ +build: + kconfig: Kconfig + cmake: . + settings: + board_root: . + dts_root: . + \ No newline at end of file diff --git a/scripts/build_zephyr.sh b/scripts/build_zephyr.sh new file mode 100755 index 0000000..8eae857 --- /dev/null +++ b/scripts/build_zephyr.sh @@ -0,0 +1,31 @@ +#!/bin/bash +set -e + +TOOLCHAIN_FILE="tools/toolchain-cmake/gnuarm.cmake" +BUILD_COMPILER="gnuarm" +BUILD_TYPE="Release" +OVERLAY_DIR="$(pwd)/mtp-lpmcu/boards/arm/mtp_lpc55s28/lpc_evk.overlay" + +while getopts ":l:o:" option; do + case $option in + l) if [[ $OPTARG == "clang" ]]; then TOOLCHAIN_FILE="tools/toolchain-cmake/clang.cmake";BUILD_COMPILER="clang"; fi ;; + o) OVERLAY_DIR=$OPTARG; echo "set overlay to $OPTARG";; + *) ;; + esac +done + +OPTIND=1 + +build_app() +{ + west build -b mtp_lpc55s28 mtp-lpmcu/app --build-dir .build-mtp-lpmcu-lpc55s28-app-only -- -DCMAKE_EXPORT_COMPILE_COMMANDS=ON -DDTC_OVERLAY_FILE=${OVERLAY_DIR} +} + +while getopts ":labepn" option; do + case $option in + # a) build_app ;; + # b) build_boot ;; + # n) build_app_only ;; + *) build_app;; + esac +done diff --git a/scripts/docker_run.sh b/scripts/docker_run.sh new file mode 100755 index 0000000..fc9ef5d --- /dev/null +++ b/scripts/docker_run.sh @@ -0,0 +1,17 @@ +#!/bin/bash +set -e + +# If your editor fails with "The input device is not a TTY", try exporting DOCKER_TTY=-t +# docker_tty=${DOCKER_TTY:--it} + +DOCKER_ARGS="--privileged \ +--rm=true \ +--user="$(id --user):$(id --group)" \ +-e ZEPHYR_SDK_INSTALL_DIR=/opt/toolchains/zephyr-sdk-0.16.4 \ +-e CCACHE_DIR=$(pwd)/.ccache \ +-v $(pwd):$(pwd) \ +-v /dev/bus/usb:/dev/bus/usb \ +-w $(pwd)" +DOCKER_IMAGE=zephyrprojectrtos/ci:v0.26.6 + +docker run $DOCKER_ARGS $DOCKER_IMAGE "$@" diff --git a/scripts/env.sh b/scripts/env.sh new file mode 100755 index 0000000..bf1920e --- /dev/null +++ b/scripts/env.sh @@ -0,0 +1,5 @@ +#!/bin/bash +set -e + +export ZEPHYR_SDK_INSTALL_DIR=/opt/toolchains/zephyr-sdk-0.16.1 +export CCACHE_DIR=$(pwd)/.ccache -- GitLab