From 1f4c171576df25db843b06f9f6a109fefc1633a8 Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 24 Mar 2023 11:33:43 +0100 Subject: [PATCH 1/2] custom CSP router --- bsp_q7s/CMakeLists.txt | 1 - bsp_q7s/comIF/CMakeLists.txt | 1 - bsp_q7s/core/ObjectFactory.cpp | 2 +- linux/csp/CspComIF.cpp | 57 +++++++++++++++++++-- linux/csp/CspComIF.h | 18 ++++++- linux/devices/startracker/StrComHandler.cpp | 4 +- 6 files changed, 71 insertions(+), 12 deletions(-) delete mode 100644 bsp_q7s/comIF/CMakeLists.txt diff --git a/bsp_q7s/CMakeLists.txt b/bsp_q7s/CMakeLists.txt index 2c1bae48..cf8fcacd 100644 --- a/bsp_q7s/CMakeLists.txt +++ b/bsp_q7s/CMakeLists.txt @@ -12,7 +12,6 @@ target_sources(${OBSW_NAME} PUBLIC main.cpp obsw.cpp) add_subdirectory(boardtest) add_subdirectory(boardconfig) -add_subdirectory(comIF) add_subdirectory(core) if(EIVE_Q7S_EM) diff --git a/bsp_q7s/comIF/CMakeLists.txt b/bsp_q7s/comIF/CMakeLists.txt deleted file mode 100644 index 431972e8..00000000 --- a/bsp_q7s/comIF/CMakeLists.txt +++ /dev/null @@ -1 +0,0 @@ -target_sources(${OBSW_NAME} PRIVATE) diff --git a/bsp_q7s/core/ObjectFactory.cpp b/bsp_q7s/core/ObjectFactory.cpp index 27d3225a..f6786446 100644 --- a/bsp_q7s/core/ObjectFactory.cpp +++ b/bsp_q7s/core/ObjectFactory.cpp @@ -179,7 +179,7 @@ void ObjectFactory::createCommunicationInterfaces(LinuxLibgpioIF** gpioComIF, *gpioComIF = new LinuxLibgpioIF(objects::GPIO_IF); /* Communication interfaces */ - new CspComIF(objects::CSP_COM_IF); + new CspComIF(objects::CSP_COM_IF, "CSP_ROUTER", 60); *i2cComIF = new I2cComIF(objects::I2C_COM_IF); *uartComIF = new SerialComIF(objects::UART_COM_IF); *spiMainComIF = new SpiComIF(objects::SPI_MAIN_COM_IF, q7s::SPI_DEFAULT_DEV, **gpioComIF); diff --git a/linux/csp/CspComIF.cpp b/linux/csp/CspComIF.cpp index 23359c0a..919bf983 100644 --- a/linux/csp/CspComIF.cpp +++ b/linux/csp/CspComIF.cpp @@ -14,7 +14,10 @@ using namespace GOMSPACE; -CspComIF::CspComIF(object_id_t objectId) : SystemObject(objectId) {} +CspComIF::CspComIF(object_id_t objectId, const char* routeTaskName, uint32_t routerRealTimePriority) + : SystemObject(objectId), + routerRealTimePriority(routerRealTimePriority), + routerTaskName(routeTaskName) {} CspComIF::~CspComIF() {} @@ -57,10 +60,8 @@ ReturnValue_t CspComIF::initializeInterface(CookieIF* cookie) { } /* Start the route task */ - unsigned int task_stack_size = 500; - unsigned int priority = 0; - result = csp_route_start_task(task_stack_size, priority); - if (result != CSP_ERR_NONE) { + result = startRouterTask(); + if (result != returnvalue::OK) { sif::error << "Failed to start csp route task" << std::endl; return returnvalue::FAILED; } @@ -343,3 +344,49 @@ void CspComIF::initiatePingRequest(uint8_t cspAddress, uint16_t querySize) { memcpy(replyBuffer, &replyTime, sizeof(replyTime)); iter->second.replyLen = sizeof(replyTime); } + +ReturnValue_t CspComIF::startRouterTask() { + pthread_attr_t attr; + int res = pthread_attr_init(&attr); + if (res) { + return returnvalue::FAILED; + } + + pthread_attr_setdetachstate(&attr, PTHREAD_CREATE_DETACHED); + + // Set scheduling policy to SCHED_RR + res = pthread_attr_setschedpolicy(&attr, SCHED_RR); + if (res) { + pthread_attr_destroy(&attr); + return returnvalue::FAILED; + } + + struct sched_param sched_param; + sched_param.sched_priority = routerRealTimePriority; + res = pthread_attr_setschedparam(&attr, &sched_param); + if (res) { + pthread_attr_destroy(&attr); + return returnvalue::FAILED; + } + + res = pthread_setname_np(pthread_self(), routerTaskName); + if (res) { + pthread_attr_destroy(&attr); + return returnvalue::FAILED; + } + + res = pthread_create(&routerTaskHandle, &attr, routerWorkWrapper, NULL); + pthread_attr_destroy(&attr); + if (res) { + return returnvalue::FAILED; + } + return returnvalue::OK; +} + +void* CspComIF::routerWorkWrapper(void* args) { + /* Here there be routing */ + while (1) { + csp_route_work(FIFO_TIMEOUT); + } + return nullptr; +} diff --git a/linux/csp/CspComIF.h b/linux/csp/CspComIF.h index d2bac4f9..47712230 100644 --- a/linux/csp/CspComIF.h +++ b/linux/csp/CspComIF.h @@ -2,9 +2,11 @@ #define LINUX_CSP_CSPCOMIF_H_ #include +#include #include #include #include +#include #include #include @@ -17,7 +19,7 @@ */ class CspComIF : public DeviceCommunicationIF, public SystemObject { public: - CspComIF(object_id_t objectId); + CspComIF(object_id_t objectId, const char *routeTaskName, uint32_t routerRealTimePriority); virtual ~CspComIF(); ReturnValue_t initializeInterface(CookieIF *cookie) override; @@ -27,6 +29,13 @@ class CspComIF : public DeviceCommunicationIF, public SystemObject { ReturnValue_t readReceivedMessage(CookieIF *cookie, uint8_t **readData, size_t *readLen) override; private: +#ifdef CSP_USE_RDP + //! If RDP is enabled, the router needs to awake some times to check timeouts + static constexpr uint32_t FIFO_TIMEOUT = 100; +#else + //! If no RDP, the router can sleep untill data arrives + static constexpr uint32_t FIFO_TIMEOUT = CSP_MAX_DELAY; +#endif /** * @brief This function initiates the CSP transfer. * @@ -56,6 +65,10 @@ class CspComIF : public DeviceCommunicationIF, public SystemObject { /* This is the CSP address of the OBC. */ node_t cspOwnAddress = 1; + pthread_t routerTaskHandle{}; + uint32_t routerRealTimePriority = 0; + const char *routerTaskName; + /* Interface struct for csp protocol stack */ csp_iface_t csp_if; char canInterface[5] = "can0"; @@ -72,6 +85,9 @@ class CspComIF : public DeviceCommunicationIF, public SystemObject { * @brief This function initiates the ping request. */ void initiatePingRequest(uint8_t cspAddress, uint16_t querySize); + + ReturnValue_t startRouterTask(); + static void *routerWorkWrapper(void *args); }; #endif /* LINUX_CSP_CSPCOMIF_H_ */ diff --git a/linux/devices/startracker/StrComHandler.cpp b/linux/devices/startracker/StrComHandler.cpp index 85be7871..fb11be27 100644 --- a/linux/devices/startracker/StrComHandler.cpp +++ b/linux/devices/startracker/StrComHandler.cpp @@ -155,9 +155,7 @@ ReturnValue_t StrComHandler::startImageDownload(std::string path) { return returnvalue::OK; } -void StrComHandler::stopProcess() { - terminate = true; -} +void StrComHandler::stopProcess() { terminate = true; } void StrComHandler::setDownloadImageName(std::string filename) { downloadImage.filename = filename; -- 2.43.0 From f273eaccfb8b6ffd05430316672e48f34b2e26df Mon Sep 17 00:00:00 2001 From: Robin Mueller Date: Fri, 24 Mar 2023 14:17:28 +0100 Subject: [PATCH 2/2] bump submodules --- fsfw | 2 +- tmtc | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/fsfw b/fsfw index f8a7c1d4..b814e719 160000 --- a/fsfw +++ b/fsfw @@ -1 +1 @@ -Subproject commit f8a7c1d4ed621a3375db0da9b9e9f8d5484abbc1 +Subproject commit b814e7198f720cad0fb063f54644d5dbc92c165c diff --git a/tmtc b/tmtc index 2263938b..9f17661f 160000 --- a/tmtc +++ b/tmtc @@ -1 +1 @@ -Subproject commit 2263938b8b1324b309a44d70c291800050ff4178 +Subproject commit 9f17661f529e045e92714817bdc52572f976da2d -- 2.43.0