-
Notifications
You must be signed in to change notification settings - Fork 0
lib: csp: add FINCH CSP integration layer #163
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,13 +4,21 @@ | |
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <finch/csp/csp.h> | ||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/logging/log.h> | ||
|
|
||
| LOG_MODULE_REGISTER(obc); | ||
|
|
||
| int main(void) | ||
| { | ||
| int ret = finch_csp_init(); | ||
|
|
||
| if (ret < 0) { | ||
| LOG_ERR("Failed to initialize FINCH CSP (%d)", ret); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We should think of a way to recover from this if libcsp fails. We especially don't want any failures in main initialization But this can be done in another PR |
||
| return ret; | ||
| } | ||
|
|
||
| while (1) { | ||
| LOG_INF("obc"); | ||
| k_msleep(1000); | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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_ */ |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4,5 +4,6 @@ | |
| menu "FINCH CubeSat Flight Software Libraries" | ||
|
|
||
| rsource "ccsds123b/Kconfig" | ||
| rsource "csp/Kconfig" | ||
|
|
||
| endmenu | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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 | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What is the reasoning for priority to default to 0? I am not very familiar with threading, but from what I can tell, this controls the priority for |
||
| help | ||
| Zephyr thread priority for the CSP router thread. | ||
|
|
||
| endif # FINCH_CSP | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| /* | ||
| * Copyright (c) 2026 The FINCH CubeSat Project Flight Software Contributors | ||
| * | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| #include <errno.h> | ||
| #include <stdbool.h> | ||
|
|
||
| #include <csp/csp.h> | ||
| #include <csp/interfaces/csp_if_lo.h> | ||
| #include <finch/csp/csp.h> | ||
| #include <zephyr/kernel.h> | ||
| #include <zephyr/logging/log.h> | ||
|
|
||
| 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(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do we ignore the return value here? Do we want to log any errors |
||
| } | ||
| } | ||
|
|
||
| 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should we add a log here to say something like "finch csp already initialized, skipping initialization"?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unlock and return is better than goto I think
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why do you think it's better? I think goto works better here because we're repeating less code. If we ever want to do anything else at the end of the function (like more cleanup) we can just add it to after The goto isn't that bad here because it's jumping further down the code, we aren't writing loops with a goto. But if you want to avoid gotos, would it be better to extract the main body of the function to another function and have this function just do the lock, call the other function, then unlock? |
||
| } | ||
|
|
||
| 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; | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A |
||
| } | ||
|
|
||
| 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"); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Spelling |
||
| 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; | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.