diff --git a/apps/obc/prj.conf b/apps/obc/prj.conf index ea81758..f13417c 100644 --- a/apps/obc/prj.conf +++ b/apps/obc/prj.conf @@ -3,6 +3,9 @@ CONFIG_LOG=y CONFIG_LOG_DEFAULT_LEVEL=3 CONFIG_COMPILER_WARNINGS_AS_ERRORS=y +CONFIG_FINCH_CSP=y +CONFIG_FINCH_CSP_NODE_ADDRESS=1 +CONFIG_FINCH_CSP_HOSTNAME="obc" # Configure UART as logging backend CONFIG_GPIO=y diff --git a/apps/obc/src/main.c b/apps/obc/src/main.c index db7327a..eb14fbc 100644 --- a/apps/obc/src/main.c +++ b/apps/obc/src/main.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include @@ -11,6 +12,13 @@ LOG_MODULE_REGISTER(obc); int main(void) { + int ret = finch_csp_init(); + + if (ret < 0) { + LOG_ERR("Failed to initialize FINCH CSP (%d)", ret); + return ret; + } + while (1) { LOG_INF("obc"); k_msleep(1000); diff --git a/apps/pay/prj.conf b/apps/pay/prj.conf index ea81758..43699fb 100644 --- a/apps/pay/prj.conf +++ b/apps/pay/prj.conf @@ -3,6 +3,9 @@ CONFIG_LOG=y CONFIG_LOG_DEFAULT_LEVEL=3 CONFIG_COMPILER_WARNINGS_AS_ERRORS=y +CONFIG_FINCH_CSP=y +CONFIG_FINCH_CSP_NODE_ADDRESS=2 +CONFIG_FINCH_CSP_HOSTNAME="pay" # Configure UART as logging backend CONFIG_GPIO=y diff --git a/apps/pay/src/main.c b/apps/pay/src/main.c index deec1d9..24174c6 100644 --- a/apps/pay/src/main.c +++ b/apps/pay/src/main.c @@ -4,6 +4,7 @@ * SPDX-License-Identifier: Apache-2.0 */ +#include #include #include @@ -11,6 +12,13 @@ LOG_MODULE_REGISTER(pay); int main(void) { + int ret = finch_csp_init(); + + if (ret < 0) { + LOG_ERR("Failed to initialize FINCH CSP (%d)", ret); + return ret; + } + while (1) { LOG_INF("pay"); k_msleep(1000); diff --git a/include/finch/csp/csp.h b/include/finch/csp/csp.h new file mode 100644 index 0000000..7f8ebc9 --- /dev/null +++ b/include/finch/csp/csp.h @@ -0,0 +1,12 @@ +/* + * Copyright (c) 2026 The FINCH CubeSat Project Flight Software Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#ifndef FINCH_CSP_CSP_H_ +#define FINCH_CSP_CSP_H_ + +int finch_csp_init(void); + +#endif /* FINCH_CSP_CSP_H_ */ diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index ed1714e..ccec5fa 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -2,3 +2,4 @@ # SPDX-License-Identifier: Apache-2.0 add_subdirectory_ifdef(CONFIG_FINCH_CCSDS123B ccsds123b) +add_subdirectory_ifdef(CONFIG_FINCH_CSP csp) diff --git a/lib/Kconfig b/lib/Kconfig index a17df1b..835e0f4 100644 --- a/lib/Kconfig +++ b/lib/Kconfig @@ -4,5 +4,6 @@ menu "FINCH CubeSat Flight Software Libraries" rsource "ccsds123b/Kconfig" +rsource "csp/Kconfig" endmenu diff --git a/lib/csp/CMakeLists.txt b/lib/csp/CMakeLists.txt new file mode 100644 index 0000000..3466066 --- /dev/null +++ b/lib/csp/CMakeLists.txt @@ -0,0 +1,6 @@ +# Copyright (c) 2026 The FINCH CubeSat Project Flight Software Contributors +# SPDX-License-Identifier: Apache-2.0 + +zephyr_library() +zephyr_library_sources(src/core.c) +zephyr_library_link_libraries(csp) diff --git a/lib/csp/Kconfig b/lib/csp/Kconfig new file mode 100644 index 0000000..17bab62 --- /dev/null +++ b/lib/csp/Kconfig @@ -0,0 +1,50 @@ +# Copyright (c) 2026 The FINCH CubeSat Project Flight Software Contributors +# SPDX-License-Identifier: Apache-2.0 + +config FINCH_CSP + bool "Enable FINCH CSP integration" + select LIBCSP + help + Build the FINCH libcsp integration layer. + +if FINCH_CSP + +config FINCH_CSP_NODE_ADDRESS + int "CSP node address" + range 0 16382 + default 1 + help + CSP address used for this image. + 16383 is reserved as the CSP broadcast address. + +config FINCH_CSP_HOSTNAME + string "CSP hostname" + default "finch" + help + Hostname exposed via CSP management services. + +config FINCH_CSP_MODEL + string "CSP model" + default "finch-flight-software" + help + Model string exposed via CSP management services. + +config FINCH_CSP_REVISION + string "CSP revision" + default "dev" + help + Revision string exposed via CSP management services. + +config FINCH_CSP_ROUTER_STACK_SIZE + int "CSP router thread stack size" + default 1024 + help + Stack size for the CSP router thread. + +config FINCH_CSP_ROUTER_PRIORITY + int "CSP router thread priority" + default 0 + help + Zephyr thread priority for the CSP router thread. + +endif # FINCH_CSP diff --git a/lib/csp/src/core.c b/lib/csp/src/core.c new file mode 100644 index 0000000..b2e2e43 --- /dev/null +++ b/lib/csp/src/core.c @@ -0,0 +1,119 @@ +/* + * Copyright (c) 2026 The FINCH CubeSat Project Flight Software Contributors + * + * SPDX-License-Identifier: Apache-2.0 + */ + +#include +#include + +#include +#include +#include +#include +#include + +LOG_MODULE_REGISTER(finch_csp); + +K_THREAD_STACK_DEFINE(finch_csp_router_stack, CONFIG_FINCH_CSP_ROUTER_STACK_SIZE); +static struct k_thread finch_csp_router_thread_data; +static k_tid_t finch_csp_router_tid; +static K_MUTEX_DEFINE(finch_csp_lock); +static bool finch_csp_initialized; + +static void finch_csp_router_thread(void *arg1, void *arg2, void *arg3) +{ + ARG_UNUSED(arg1); + ARG_UNUSED(arg2); + ARG_UNUSED(arg3); + + while (1) { + (void)csp_route_work(); + } +} + +static int finch_csp_bind_service(uint8_t port) +{ + int ret = csp_bind_callback(csp_service_handler, port); + + if ((ret == CSP_ERR_NONE) || (ret == CSP_ERR_USED) || + (ret == CSP_ERR_ALREADY)) { + return 0; + } + + LOG_ERR("Failed to bind CSP service port %u (%d)", port, ret); + return -EIO; +} + +static int finch_csp_bind_services(void) +{ + static const uint8_t service_ports[] = { + CSP_CMP, + CSP_PING, + CSP_MEMFREE, + CSP_REBOOT, + CSP_BUF_FREE, + CSP_UPTIME, + }; + + for (size_t i = 0; i < ARRAY_SIZE(service_ports); i++) { + int ret = finch_csp_bind_service(service_ports[i]); + + if (ret < 0) { + return ret; + } + } + + return 0; +} + +int finch_csp_init(void) +{ + int ret = 0; + + k_mutex_lock(&finch_csp_lock, K_FOREVER); + + if (finch_csp_initialized) { + goto out; + } + + csp_conf.hostname = CONFIG_FINCH_CSP_HOSTNAME; + csp_conf.model = CONFIG_FINCH_CSP_MODEL; + csp_conf.revision = CONFIG_FINCH_CSP_REVISION; + + csp_init(); + + csp_if_lo.addr = CONFIG_FINCH_CSP_NODE_ADDRESS; + + ret = finch_csp_bind_services(); + if (ret < 0) { + goto out; + } + + finch_csp_router_tid = k_thread_create( + &finch_csp_router_thread_data, + finch_csp_router_stack, + K_THREAD_STACK_SIZEOF(finch_csp_router_stack), + finch_csp_router_thread, + NULL, NULL, NULL, + CONFIG_FINCH_CSP_ROUTER_PRIORITY, + 0, + K_NO_WAIT); + + if (finch_csp_router_tid == NULL) { + LOG_ERR("Failed to creat CSP router thread"); + ret = -EIO; + goto out; + } + + k_thread_name_set(finch_csp_router_tid, "finch_csp_router"); + finch_csp_initialized = true; + + LOG_INF("CSP core ready (addr=%u, host=%s)", + CONFIG_FINCH_CSP_NODE_ADDRESS, + CONFIG_FINCH_CSP_HOSTNAME); + +out: + k_mutex_unlock(&finch_csp_lock); + return ret; +}