From 7635cfe4ad139e6de90acf4bbf924ec89624cccc Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Mon, 1 Jan 2024 16:03:21 +0100 Subject: [PATCH 01/30] Rebase onto main --- .gitignore | 3 + README.md | 1 + include/sta/tacos/can_bus.hpp | 118 ++++++++++++++++++++++++++++++++++ src/can_bus.cpp | 115 +++++++++++++++++++++++++++++++++ 4 files changed, 237 insertions(+) create mode 100644 include/sta/tacos/can_bus.hpp create mode 100644 src/can_bus.cpp diff --git a/.gitignore b/.gitignore index b9309ce..64dcbf5 100644 --- a/.gitignore +++ b/.gitignore @@ -13,3 +13,6 @@ Release/ docs/html docs/latex +# Config +include/sta/config.hpp + diff --git a/README.md b/README.md index 51cdb5b..a2d3146 100644 --- a/README.md +++ b/README.md @@ -48,6 +48,7 @@ In order to use TACOS, you need to provide a configuration file in the path `sta #define STA_RTOS_SYSTEM_EVENTS_ENABLE // #define STA_RTOS_SYSTEM_WATCHDOG_ENABLE // #define STA_RTOS_WATCHDOG_ENABLE +// #define STA_RTOS_CAN_BUS_ENABLE // Uses the default configuration for TACOS. #include diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp new file mode 100644 index 0000000..ca3e4d4 --- /dev/null +++ b/include/sta/tacos/can_bus.hpp @@ -0,0 +1,118 @@ +#ifndef INCLUDE_TACOS_CAN_BUS_HPP_ +#define INCLUDE_TACOS_CAN_BUS_HPP_ + +#include + +//TODO UNCOMMENT THIS +//#ifdef STA_RTOS_CAN_BUS_ENABLE + +#include +#include +#include +#include + + +/** + * @defgroup tacos_can_bus Can Bus Task + * @ingroup tacos + * @brief Can Bus task for TACOS. + */ + +namespace sta +{ + namespace tacos + { + /** + * @brief Can Bus implementation for Tacos. + * + * @ingroup tacos_can_bus + */ + class CanBus : public TacosThread + { + public: + /** + * @brief Getter function for the singleton instance. + */ + static CanBus* instance(CAN_HandleTypeDef * handle) + { + static CGuard g; + + if (!_instance) + { + // Create the can bus singleton instance. + CanBus::_instance = new CanBus(handle); + } + + return _instance; + } + + /** + * @brief Place data message in CAN driver TX queue. + * + * @param msg Message to transmit + * @param timeout Timeout for placing message (0 = no wait, osWaitForever = blocking) + * @return True if message was queued successfully + */ + bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout); + /** + * @brief Place system message in CAN driver TX queue. + * + * @param msg Message to transmit + * @param timeout Timeout for placing message (0 = no wait, osWaitForever = blocking) + * @return True if message was queued successfully + */ + bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout); + + /** + * @brief Retrieve data message from CAN driver TX queue. + * + * @param[out] msg Output address for retrieved message + * @param timeout Timeout for retrieving message (0 = no wait, osWaitForever = blocking) + * @return True if message was retrieved successfully + */ + bool getCanBusMsg(CanDataMsg * msg, uint32_t timeout); + /** + * @brief Retrieve system message from CAN driver TX queue. + * + * @param[out] msg Destination for retrieved message + * @param timeout Timeout for retrieving message (0 = no wait, osWaitForever = blocking) + * @return True if message was retrieved successfully + */ + bool getCanBusMsg(CanSysMsg * msg, uint32_t timeout); + + + private: + static CanBus * _instance; + + class CGuard + { + public: + ~CGuard() + { + if( NULL != CanBus::_instance ) + { + delete CanBus::_instance; + CanBus::_instance = NULL; + } + } + }; + + + RtosQueue canBusDataQueue_; + RtosQueue canBusSysQueue_; + + CanDataMsg canBusDataQueueBuffer[queueLength]; + CanSysMsg canBusSysQueueBuffer[queueLength]; + uint32_t canBusStack[256]; + + sta::STM32CanController * canBusController_; + const size_t queueLength_; + AlpakaCanBus canBus_; + } + + } /* namespace tacos */ + +} /* namespace sta */ + +#endif /* STA_RTOS_CAN_BUS_ENABLE */ +#endif /* INCLUDE_TACOS_CAN_BUS_HPP_ */ \ No newline at end of file diff --git a/src/can_bus.cpp b/src/can_bus.cpp new file mode 100644 index 0000000..c6bd795 --- /dev/null +++ b/src/can_bus.cpp @@ -0,0 +1,115 @@ +#include +#include +#include + +namespace sta +{ + namespace tacos + { + CanBus::CanBus(CAN_HandleTypeDef * handle) + : TacosThread{"Can Bus", STA_TACOS_CAN_BUS_PRIORITY}, + canBusController_{handle}, + canBusDataQueueBuffer{nullptr}, + canBusSysQueueBuffer{nullptr}, + canSysDataQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, + canBusDataQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, + canBus_{&canBusController_, HAL_GetTick, dummy::handleSysMessage, dummy::handleDataMessage} + { + } + + void CanBus::init() + { + canBusController_.start(); + } + + void CanBus::func() + { + uint32_t flags = osThreadFlagsWait(STA_RTOS_THREAD_FLAGS_VALID_BITS, osFlagsWaitAny, 50); + + if (flags != static_cast(osErrorTimeout)) + { + STA_ASSERT_MSG((flags & osStatusReserved) == flags, "Unexpected error occurred in wait"); + + if (flags & STA_RTOS_CAN_FLAG_SYS_QUEUED) + { + // Take messages from queue until empty + CanSysMsg msg; + while (rtos::getCanBusMsg(&msg, 0)) + { + canBus_.send(msg); + } + } + + if (flags & STA_RTOS_CAN_FLAG_DATA_QUEUED) + { + // Take messages from queue until empty + CanDataMsg msg; + while (rtos::getCanBusMsg(&msg, 0)) + { + canBus_.send(msg); + } + } + + if (flags & STA_RTOS_CAN_FLAG_MSG_AVAIL) + { + STA_DEBUG_PRINTLN("[event] CAN INT"); + + canBus_.processRx(); + } + + if (flags & STA_RTOS_CAN_FLAG_SHOW_STATS) + { + canBus_.showStatistics(); + } + } + + // Process ISOTP transmissions + canBus.processTx(); + } + + bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout) + { + STA_ASSERT((msg.header.sid & STA_CAN_SID_SYS_BITS) == 0); + STA_ASSERT(msg.header.payloadLength <= sizeof(msg.payload)); + + if (canBusDataQueue_.put(&msg, timeout)) + { + // Signal thread + notify(STA_RTOS_CAN_FLAG_DATA_QUEUED); + return true; + } + else + { + return false; + } + } + + bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout) + { + STA_ASSERT((msg.header.sid & ~STA_CAN_SID_SYS_BITS) == 0); + + if (canBusSysQueue_.put(&msg, timeout)) + { + // Signal thread + notify(STA_RTOS_CAN_FLAG_SYS_QUEUED); + return true; + } + else + { + return false; + } + } + + + bool getCanBusMsg(CanDataMsg * msg, uint32_t timeout) + { + return (osOK == osMessageQueueGet(canBusDataQueueHandle, msg, 0, timeout)); + } + + bool getCanBusMsg(CanSysMsg * msg, uint32_t timeout) + { + return (osOK == osMessageQueueGet(canBusSysQueueHandle, msg, 0, timeout)); + } + + } /* namespace tacos */ +} /* namespace sta */ \ No newline at end of file From c327ec2ede4fd38bc7cabcf1875ad7ed287e2c18 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Mon, 1 Jan 2024 16:26:02 +0100 Subject: [PATCH 02/30] Cleanup --- include/sta/tacos/can_bus.hpp | 6 ++---- src/can_bus.cpp | 14 ++++++-------- 2 files changed, 8 insertions(+), 12 deletions(-) diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp index ca3e4d4..562970c 100644 --- a/include/sta/tacos/can_bus.hpp +++ b/include/sta/tacos/can_bus.hpp @@ -8,7 +8,6 @@ #include #include -#include #include @@ -101,12 +100,11 @@ namespace sta RtosQueue canBusDataQueue_; RtosQueue canBusSysQueue_; - CanDataMsg canBusDataQueueBuffer[queueLength]; - CanSysMsg canBusSysQueueBuffer[queueLength]; + CanDataMsg canBusDataQueueBuffer_[queueLength]; + CanSysMsg canBusSysQueueBuffer_[queueLength]; uint32_t canBusStack[256]; sta::STM32CanController * canBusController_; - const size_t queueLength_; AlpakaCanBus canBus_; } diff --git a/src/can_bus.cpp b/src/can_bus.cpp index c6bd795..2fd2cdc 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -9,8 +9,6 @@ namespace sta CanBus::CanBus(CAN_HandleTypeDef * handle) : TacosThread{"Can Bus", STA_TACOS_CAN_BUS_PRIORITY}, canBusController_{handle}, - canBusDataQueueBuffer{nullptr}, - canBusSysQueueBuffer{nullptr}, canSysDataQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, canBusDataQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, canBus_{&canBusController_, HAL_GetTick, dummy::handleSysMessage, dummy::handleDataMessage} @@ -67,7 +65,7 @@ namespace sta canBus.processTx(); } - bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout) + bool CanBus::queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout) { STA_ASSERT((msg.header.sid & STA_CAN_SID_SYS_BITS) == 0); STA_ASSERT(msg.header.payloadLength <= sizeof(msg.payload)); @@ -84,7 +82,7 @@ namespace sta } } - bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout) + bool CanBus::queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout) { STA_ASSERT((msg.header.sid & ~STA_CAN_SID_SYS_BITS) == 0); @@ -101,14 +99,14 @@ namespace sta } - bool getCanBusMsg(CanDataMsg * msg, uint32_t timeout) + bool CanBus::getCanBusMsg(CanDataMsg * msg, uint32_t timeout) { - return (osOK == osMessageQueueGet(canBusDataQueueHandle, msg, 0, timeout)); + return (canBusDataQueueBuffer_.get(msg, timeout)); } - bool getCanBusMsg(CanSysMsg * msg, uint32_t timeout) + bool CanBus::getCanBusMsg(CanSysMsg * msg, uint32_t timeout) { - return (osOK == osMessageQueueGet(canBusSysQueueHandle, msg, 0, timeout)); + return (canBusSysQueueBuffer_.get(msg, timeout)); } } /* namespace tacos */ From 72395bd0c1e478e80ba60b671465851e764c8c69 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 16 Feb 2024 14:41:58 +0100 Subject: [PATCH 03/30] Controller init updated --- src/can_bus.cpp | 4 ++-- src/startup.cpp | 5 +++++ 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/src/can_bus.cpp b/src/can_bus.cpp index 2fd2cdc..78ad971 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -6,9 +6,9 @@ namespace sta { namespace tacos { - CanBus::CanBus(CAN_HandleTypeDef * handle) + CanBus::CanBus(STM32CanController * controller) : TacosThread{"Can Bus", STA_TACOS_CAN_BUS_PRIORITY}, - canBusController_{handle}, + canBusController_{controller}, canSysDataQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, canBusDataQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, canBus_{&canBusController_, HAL_GetTick, dummy::handleSysMessage, dummy::handleDataMessage} diff --git a/src/startup.cpp b/src/startup.cpp index 8556665..b312a93 100644 --- a/src/startup.cpp +++ b/src/startup.cpp @@ -125,6 +125,11 @@ namespace sta Watchdog::instance()->start(); } #endif // STA_TACOS_WATCHDOG_ENABLED + STA_WEAK + CanController * getCanController(){ + extern CAN_HandleTypeDef hcan1; + return new STM32CanController(&hcan1); + } } // namespace tacos From 090d325b10f2474646bad35b5981784082d77341 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Tue, 2 Jan 2024 21:04:10 +0100 Subject: [PATCH 04/30] Syntax fixes --- include/sta/tacos/can_bus.hpp | 80 ++++++++++++++++++----------------- src/can_bus.cpp | 28 ++++++------ 2 files changed, 56 insertions(+), 52 deletions(-) diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp index 562970c..9704f20 100644 --- a/include/sta/tacos/can_bus.hpp +++ b/include/sta/tacos/can_bus.hpp @@ -3,8 +3,7 @@ #include -//TODO UNCOMMENT THIS -//#ifdef STA_RTOS_CAN_BUS_ENABLE +#ifdef STA_RTOS_CAN_BUS_ENABLE #include #include @@ -29,6 +28,9 @@ namespace sta class CanBus : public TacosThread { public: + + CanBus(CAN_HandleTypeDef * controller); + /** * @brief Getter function for the singleton instance. */ @@ -45,40 +47,42 @@ namespace sta return _instance; } - /** - * @brief Place data message in CAN driver TX queue. - * - * @param msg Message to transmit - * @param timeout Timeout for placing message (0 = no wait, osWaitForever = blocking) - * @return True if message was queued successfully - */ - bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout); - /** - * @brief Place system message in CAN driver TX queue. - * - * @param msg Message to transmit - * @param timeout Timeout for placing message (0 = no wait, osWaitForever = blocking) - * @return True if message was queued successfully - */ - bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout); + /** + * @brief Place data message in CAN driver TX queue. + * + * @param msg Message to transmit + * @param timeout Timeout for placing message (0 = no wait, osWaitForever = blocking) + * @return True if message was queued successfully + */ + bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout); + /** + * @brief Place system message in CAN driver TX queue. + * + * @param msg Message to transmit + * @param timeout Timeout for placing message (0 = no wait, osWaitForever = blocking) + * @return True if message was queued successfully + */ + bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout); - /** - * @brief Retrieve data message from CAN driver TX queue. - * - * @param[out] msg Output address for retrieved message - * @param timeout Timeout for retrieving message (0 = no wait, osWaitForever = blocking) - * @return True if message was retrieved successfully - */ - bool getCanBusMsg(CanDataMsg * msg, uint32_t timeout); - /** - * @brief Retrieve system message from CAN driver TX queue. - * - * @param[out] msg Destination for retrieved message - * @param timeout Timeout for retrieving message (0 = no wait, osWaitForever = blocking) - * @return True if message was retrieved successfully - */ - bool getCanBusMsg(CanSysMsg * msg, uint32_t timeout); + /** + * @brief Retrieve data message from CAN driver TX queue. + * + * @param[out] msg Output address for retrieved message + * @param timeout Timeout for retrieving message (0 = no wait, osWaitForever = blocking) + * @return True if message was retrieved successfully + */ + bool getCanBusMsg(CanDataMsg * msg, uint32_t timeout); + /** + * @brief Retrieve system message from CAN driver TX queue. + * + * @param[out] msg Destination for retrieved message + * @param timeout Timeout for retrieving message (0 = no wait, osWaitForever = blocking) + * @return True if message was retrieved successfully + */ + bool getCanBusMsg(CanSysMsg * msg, uint32_t timeout); + void init() override; + void func() override; private: static CanBus * _instance; @@ -100,17 +104,17 @@ namespace sta RtosQueue canBusDataQueue_; RtosQueue canBusSysQueue_; - CanDataMsg canBusDataQueueBuffer_[queueLength]; - CanSysMsg canBusSysQueueBuffer_[queueLength]; + CanDataMsg canBusDataQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; + CanSysMsg canBusSysQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; uint32_t canBusStack[256]; sta::STM32CanController * canBusController_; AlpakaCanBus canBus_; - } + }; } /* namespace tacos */ } /* namespace sta */ #endif /* STA_RTOS_CAN_BUS_ENABLE */ -#endif /* INCLUDE_TACOS_CAN_BUS_HPP_ */ \ No newline at end of file +#endif /* INCLUDE_TACOS_CAN_BUS_HPP_ */ diff --git a/src/can_bus.cpp b/src/can_bus.cpp index 78ad971..ee14e16 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -6,10 +6,10 @@ namespace sta { namespace tacos { - CanBus::CanBus(STM32CanController * controller) + CanBus::CanBus(CAN_HandleTypeDef * controller) : TacosThread{"Can Bus", STA_TACOS_CAN_BUS_PRIORITY}, - canBusController_{controller}, - canSysDataQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, + canBusController_(controller), + canBusSysQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, canBusDataQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, canBus_{&canBusController_, HAL_GetTick, dummy::handleSysMessage, dummy::handleDataMessage} { @@ -17,7 +17,7 @@ namespace sta void CanBus::init() { - canBusController_.start(); + canBusController_->start(); } void CanBus::func() @@ -32,7 +32,7 @@ namespace sta { // Take messages from queue until empty CanSysMsg msg; - while (rtos::getCanBusMsg(&msg, 0)) + while (CanBus::_instance->getCanBusMsg(&msg, 0)) { canBus_.send(msg); } @@ -42,7 +42,7 @@ namespace sta { // Take messages from queue until empty CanDataMsg msg; - while (rtos::getCanBusMsg(&msg, 0)) + while (CanBus::_instance->getCanBusMsg(&msg, 0)) { canBus_.send(msg); } @@ -62,15 +62,15 @@ namespace sta } // Process ISOTP transmissions - canBus.processTx(); + canBus_.processTx(); } - bool CanBus::queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout) + bool CanBus::queueCanBusMsg(const CanDataMsg& msg, uint32_t timeout) { STA_ASSERT((msg.header.sid & STA_CAN_SID_SYS_BITS) == 0); STA_ASSERT(msg.header.payloadLength <= sizeof(msg.payload)); - if (canBusDataQueue_.put(&msg, timeout)) + if (canBusDataQueue_.put(msg, timeout)) { // Signal thread notify(STA_RTOS_CAN_FLAG_DATA_QUEUED); @@ -82,11 +82,11 @@ namespace sta } } - bool CanBus::queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout) + bool CanBus::queueCanBusMsg(const CanSysMsg& msg, uint32_t timeout) { STA_ASSERT((msg.header.sid & ~STA_CAN_SID_SYS_BITS) == 0); - if (canBusSysQueue_.put(&msg, timeout)) + if (canBusSysQueue_.put(msg, timeout)) { // Signal thread notify(STA_RTOS_CAN_FLAG_SYS_QUEUED); @@ -101,13 +101,13 @@ namespace sta bool CanBus::getCanBusMsg(CanDataMsg * msg, uint32_t timeout) { - return (canBusDataQueueBuffer_.get(msg, timeout)); + return (canBusDataQueue_.get(msg, timeout)); } bool CanBus::getCanBusMsg(CanSysMsg * msg, uint32_t timeout) { - return (canBusSysQueueBuffer_.get(msg, timeout)); + return (canBusSysQueue_.get(msg, timeout)); } } /* namespace tacos */ -} /* namespace sta */ \ No newline at end of file +} /* namespace sta */ From 5f0a3e7630ac46c7d897e57f4494ed759e128199 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Sat, 6 Jan 2024 15:31:48 +0100 Subject: [PATCH 05/30] README added CAN configs --- README.md | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/README.md b/README.md index a2d3146..3a431ff 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,10 @@ In order to use TACOS, you need to provide a configuration file in the path `sta // #define STA_RTOS_WATCHDOG_ENABLE // #define STA_RTOS_CAN_BUS_ENABLE +// Settings for the CAN bus +#define STA_RTOS_CAN_BUS_ENABLE +#define STA_RTOS_CAN_BUS_QUEUE_LENGTH 5 + // Uses the default configuration for TACOS. #include From ebc0383b6165c199d0c67eccd7c5ab8b4877e6a9 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Wed, 3 Jan 2024 10:59:56 +0100 Subject: [PATCH 06/30] Init order --- include/sta/tacos/can_bus.hpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp index 9704f20..f235311 100644 --- a/include/sta/tacos/can_bus.hpp +++ b/include/sta/tacos/can_bus.hpp @@ -100,16 +100,19 @@ namespace sta } }; - - RtosQueue canBusDataQueue_; - RtosQueue canBusSysQueue_; + sta::STM32CanController * canBusController_; CanDataMsg canBusDataQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; CanSysMsg canBusSysQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; + + RtosQueue canBusSysQueue_; + RtosQueue canBusDataQueue_; + uint32_t canBusStack[256]; - sta::STM32CanController * canBusController_; AlpakaCanBus canBus_; + + }; } /* namespace tacos */ From 2dc3299186a608fa022432ffe7f360cd188ec3b6 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Wed, 3 Jan 2024 11:00:19 +0100 Subject: [PATCH 07/30] Constructor for CanBus --- src/can_bus.cpp | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/can_bus.cpp b/src/can_bus.cpp index ee14e16..3726a13 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -1,4 +1,8 @@ +#include +#ifdef STA_RTOS_CAN_BUS_ENABLE + #include +#include #include #include @@ -8,10 +12,10 @@ namespace sta { CanBus::CanBus(CAN_HandleTypeDef * controller) : TacosThread{"Can Bus", STA_TACOS_CAN_BUS_PRIORITY}, - canBusController_(controller), - canBusSysQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, - canBusDataQueue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, - canBus_{&canBusController_, HAL_GetTick, dummy::handleSysMessage, dummy::handleDataMessage} + canBusController_(new STM32CanController(controller)), + canBusSysQueue_(STA_RTOS_CAN_BUS_QUEUE_LENGTH), + canBusDataQueue_(STA_RTOS_CAN_BUS_QUEUE_LENGTH), + canBus_{canBusController_, HAL_GetTick, dummy::handleSysMessage, dummy::handleDataMessage} { } @@ -109,5 +113,9 @@ namespace sta return (canBusSysQueue_.get(msg, timeout)); } + CanBus* CanBus::_instance = nullptr; + } /* namespace tacos */ } /* namespace sta */ + +#endif // STA_RTOS_CAN_BUS_ENABLE \ No newline at end of file From becb50143816dfb5b769f41399008e8bcc85b7af Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 16 Feb 2024 14:42:38 +0100 Subject: [PATCH 08/30] Proper getCanController init --- src/startup.cpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/src/startup.cpp b/src/startup.cpp index b312a93..0fda9b3 100644 --- a/src/startup.cpp +++ b/src/startup.cpp @@ -40,6 +40,10 @@ extern osMutexId_t uartMutexHandle; * @brief Functions that are called during startup. */ +// CAN Handle +#ifdef STA_RTOS_CAN_BUS_ENABLE + extern CAN_HandleTypeDef hcan1; +#endif /* STA_RTOS_CAN_BUS_ENABLE */ namespace sta { @@ -150,6 +154,14 @@ namespace sta tacos::initWatchdog(); #endif // STA_TACOS_WATCHDOG_ENABLED } + +#ifdef STA_RTOS_CAN_BUS_ENABLE + STA_WEAK + CAN_HandleTypeDef * getCanController(){ + return &hcan1; + } +#endif /* STA_RTOS_CAN_BUS_ENABLE */ + } // namespace rtos } // namespace sta From 4a7b426bbc75c9c0aee0812a4e1339acc62db700 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Wed, 3 Jan 2024 11:07:45 +0100 Subject: [PATCH 09/30] README defines --- README.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 3a431ff..069474b 100644 --- a/README.md +++ b/README.md @@ -48,11 +48,12 @@ In order to use TACOS, you need to provide a configuration file in the path `sta #define STA_RTOS_SYSTEM_EVENTS_ENABLE // #define STA_RTOS_SYSTEM_WATCHDOG_ENABLE // #define STA_RTOS_WATCHDOG_ENABLE -// #define STA_RTOS_CAN_BUS_ENABLE // Settings for the CAN bus #define STA_RTOS_CAN_BUS_ENABLE #define STA_RTOS_CAN_BUS_QUEUE_LENGTH 5 +#define STA_RTOS_CAN_BUS_MAX_FILTER 14 +#define STA_RTOS_CAN_BUS_MAX_PAYLOAD_SIZE 8 // Uses the default configuration for TACOS. #include From 7e0d3bfd19c3832739b9a66cd9292d69065a0116 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 5 Jan 2024 15:28:02 +0100 Subject: [PATCH 10/30] removed tacos dependencies from rtos2-utils --- include/sta/tacos/can_bus.hpp | 8 ++++ src/can_bus.cpp | 90 ++++++++++++++++++++++++++++++++++- 2 files changed, 97 insertions(+), 1 deletion(-) diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp index f235311..aac82fd 100644 --- a/include/sta/tacos/can_bus.hpp +++ b/include/sta/tacos/can_bus.hpp @@ -8,6 +8,9 @@ #include #include #include +#include + +#include /** @@ -115,6 +118,11 @@ namespace sta }; + + void handleSysMessage(const sta::CanRxHeader & header, const uint8_t * payload); + + void handleDataMessage(const sta::IsotpMessage & msg); + } /* namespace tacos */ } /* namespace sta */ diff --git a/src/can_bus.cpp b/src/can_bus.cpp index 3726a13..48ecfa2 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -6,6 +6,19 @@ #include #include +namespace sta +{ + namespace rtos + { + + void initCanBus() + { + sta::tacos::CanBus::instance(getCanController())->start(); + } + + } // namespace rtos +} // namespace sta + namespace sta { namespace tacos @@ -15,7 +28,7 @@ namespace sta canBusController_(new STM32CanController(controller)), canBusSysQueue_(STA_RTOS_CAN_BUS_QUEUE_LENGTH), canBusDataQueue_(STA_RTOS_CAN_BUS_QUEUE_LENGTH), - canBus_{canBusController_, HAL_GetTick, dummy::handleSysMessage, dummy::handleDataMessage} + canBus_{canBusController_, HAL_GetTick, sta::tacos::handleSysMessage, sta::tacos::handleDataMessage} { } @@ -118,4 +131,79 @@ namespace sta } /* namespace tacos */ } /* namespace sta */ +namespace sta { + namespace tacos + { + void handleSysMessage(const sta::CanRxHeader & header, const uint8_t * payload) + { + // Write frame payload to DebugSerial + STA_DEBUG_PRINTLN("[event] RX sys frame"); + + debug::printFrameID(header.id); + debug::printPayloadHex(payload, header.payloadLength); + + // Sysmessage is mainly only state change from GRSM + // TODO add other cases + + // 0 is from state, 1 is to state, 2 is lockout, 3 is failsafe (-1 if inactive) + if(payload[1] > 0 && payload[1] < STA_TACOS_NUM_STATES){ + + if(payload[3] == -1){ + sta::tacos::Statemachine::instance()->requestStateTransition(payload[0], payload[1], payload[2]); + } + else{ + sta::tacos::Statemachine::instance()->requestTimedStateTransition(payload[0], payload[1], payload[2], payload[3]); + } + } + } + + void handleDataMessage(const sta::IsotpMessage & msg) + { + STA_ASSERT(msg.buffer != nullptr); + STA_ASSERT(msg.size != 0); + + STA_DEBUG_PRINTLN("[event] RX data message"); + + debug::printFrameID(msg.frameID); + + // TODO Forward message to other threads + + // if (buffer[0] == DEMO_BMP_PACKET_ID) + // { + // BmpPacket packet; + // if (unpack(buffer + 1, size - 1, &packet)) + // { + // STA_DEBUG_PRINTLN(); + // STA_DEBUG_PRINTLN("# ############"); + // STA_DEBUG_PRINTLN("# ## BMP380 ##"); + // STA_DEBUG_PRINTLN("# ############"); + // STA_DEBUG_PRINTLN("#"); + // + // STA_DEBUG_PRINT("# temperature: "); + // STA_DEBUG_PRINT(packet.temperature); + // STA_DEBUG_PRINTLN(" *C"); + // STA_DEBUG_PRINT("# pressure: "); + // STA_DEBUG_PRINT(packet.pressure); + // STA_DEBUG_PRINTLN(" Pa"); + // STA_DEBUG_PRINT("# altitude: "); + // STA_DEBUG_PRINT(packet.altitude); + // STA_DEBUG_PRINTLN(" m"); + // STA_DEBUG_PRINTLN(); + // } + // else + // { + // STA_DEBUG_PRINTLN("[error] BMP unpack failed"); + // } + // } + // else + { + STA_DEBUG_PRINT("ID: "); + STA_DEBUG_PRINTLN(msg.buffer[0], sta::IntegerBase::HEX); + STA_DEBUG_PRINT("size: "); + STA_DEBUG_PRINTLN(msg.size); + } + } + } // namespace tacos +} // namespace sta + #endif // STA_RTOS_CAN_BUS_ENABLE \ No newline at end of file From 35d069173615200989c4d9b54396172824aa58ea Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 16 Feb 2024 14:43:03 +0100 Subject: [PATCH 11/30] Added: Default includes CAN Settings --- include/sta/tacos/configs/default.hpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/include/sta/tacos/configs/default.hpp b/include/sta/tacos/configs/default.hpp index 119c247..c048c23 100644 --- a/include/sta/tacos/configs/default.hpp +++ b/include/sta/tacos/configs/default.hpp @@ -8,8 +8,14 @@ #define STA_TACOS_MANAGER_PRIORITY osPriorityHigh #define STA_TACOS_STATEMACHINE_PRIORITY osPriorityHigh #define STA_TACOS_WATCHDOG_PRIORITY osPriorityHigh +#define STA_TACOS_CAN_BUS_PRIORITY osPriorityNormal // Per default, we assume state 0 to be the initial state. #define STA_TACOS_INITIAL_STATE 0 +// Can Bus settings +#define STA_RTOS_CAN_BUS_QUEUE_LENGTH 5 +#define STA_RTOS_CAN_BUS_MAX_FILTER 14 +#define STA_RTOS_CAN_BUS_MAX_PAYLOAD_SIZE 8 + #endif // STA_TACOS_CONFIGS_DEFAULT_HPP From 15007ffed28ed87ff2210687df2dd412ca46810e Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Sat, 6 Jan 2024 15:42:30 +0100 Subject: [PATCH 12/30] Updated: config.hpp in README --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index 069474b..9d05874 100644 --- a/README.md +++ b/README.md @@ -44,16 +44,14 @@ In order to use TACOS, you need to provide a configuration file in the path `sta #define STA_ASSERT_FORCE #define STA_DEBUGGING_ENABLED -// Settings for the rtos-utils +// Enable Features #define STA_RTOS_SYSTEM_EVENTS_ENABLE // #define STA_RTOS_SYSTEM_WATCHDOG_ENABLE // #define STA_RTOS_WATCHDOG_ENABLE - -// Settings for the CAN bus #define STA_RTOS_CAN_BUS_ENABLE -#define STA_RTOS_CAN_BUS_QUEUE_LENGTH 5 -#define STA_RTOS_CAN_BUS_MAX_FILTER 14 -#define STA_RTOS_CAN_BUS_MAX_PAYLOAD_SIZE 8 + +// Statemachine settings. +#define STA_TACOS_NUM_STATES 3 // Uses the default configuration for TACOS. #include From db43a3c7df3b105b13d8ebf7eb75c482ca4a0120 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Jan 2024 14:57:39 +0100 Subject: [PATCH 13/30] Readme updated to recommended MCU and ASEAG --- README.md | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 9d05874..a765ae6 100644 --- a/README.md +++ b/README.md @@ -34,8 +34,10 @@ In order to use TACOS, you need to provide a configuration file in the path `sta #ifndef INC_STA_CONFIG_HPP_ #define INC_STA_CONFIG_HPP_ -// Use the STM32F411 microprocessor. -#include +// Using a board with an ASEAG module present. +#define STA_STM32_ASEAG +// Use the STM32F407 microprocessor. +#include // Doesn't really do too much right now. Has to be added for successful compilation. #define STA_PRINTF_USE_STDLIB From 7da60dc85ed63d4ae934ca9e3575ea125ad30142 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 16 Feb 2024 14:44:29 +0100 Subject: [PATCH 14/30] CAN Bus shifted to TACOS from rtos --- README.md | 2 +- include/sta/tacos/can_bus.hpp | 4 +-- src/can_bus.cpp | 17 ++----------- src/startup.cpp | 48 +++++++++++++++++++++++++---------- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/README.md b/README.md index a765ae6..c99602f 100644 --- a/README.md +++ b/README.md @@ -50,7 +50,7 @@ In order to use TACOS, you need to provide a configuration file in the path `sta #define STA_RTOS_SYSTEM_EVENTS_ENABLE // #define STA_RTOS_SYSTEM_WATCHDOG_ENABLE // #define STA_RTOS_WATCHDOG_ENABLE -#define STA_RTOS_CAN_BUS_ENABLE +#define STA_CAN_BUS_ENABLE // Statemachine settings. #define STA_TACOS_NUM_STATES 3 diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp index aac82fd..82a9cd8 100644 --- a/include/sta/tacos/can_bus.hpp +++ b/include/sta/tacos/can_bus.hpp @@ -3,7 +3,7 @@ #include -#ifdef STA_RTOS_CAN_BUS_ENABLE +#ifdef STA_CAN_BUS_ENABLE #include #include @@ -127,5 +127,5 @@ namespace sta } /* namespace sta */ -#endif /* STA_RTOS_CAN_BUS_ENABLE */ +#endif /* STA_CAN_BUS_ENABLE */ #endif /* INCLUDE_TACOS_CAN_BUS_HPP_ */ diff --git a/src/can_bus.cpp b/src/can_bus.cpp index 48ecfa2..c06a947 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -1,24 +1,11 @@ #include -#ifdef STA_RTOS_CAN_BUS_ENABLE +#ifdef STA_CAN_BUS_ENABLE #include #include #include #include -namespace sta -{ - namespace rtos - { - - void initCanBus() - { - sta::tacos::CanBus::instance(getCanController())->start(); - } - - } // namespace rtos -} // namespace sta - namespace sta { namespace tacos @@ -206,4 +193,4 @@ namespace sta { } // namespace tacos } // namespace sta -#endif // STA_RTOS_CAN_BUS_ENABLE \ No newline at end of file +#endif // STA_CAN_BUS_ENABLE \ No newline at end of file diff --git a/src/startup.cpp b/src/startup.cpp index 0fda9b3..46f8490 100644 --- a/src/startup.cpp +++ b/src/startup.cpp @@ -29,7 +29,10 @@ #include #include #include +#include +// TODO: WHY DO I HAVE TO PUT THIS HERE???? DO YOU THINK YOU ARE SPECIAL, HUH MR. HCAN1? WHY CAN'T YOU BE NORMAL LIKE HUART +extern CAN_HandleTypeDef hcan1; // The UART mutex defined in freertos.c extern osMutexId_t uartMutexHandle; @@ -39,12 +42,6 @@ extern osMutexId_t uartMutexHandle; * @ingroup tacos * @brief Functions that are called during startup. */ - -// CAN Handle -#ifdef STA_RTOS_CAN_BUS_ENABLE - extern CAN_HandleTypeDef hcan1; -#endif /* STA_RTOS_CAN_BUS_ENABLE */ - namespace sta { #ifdef STA_DEBUGGING_ENABLED @@ -63,6 +60,18 @@ namespace sta return &STA_STM32_USART_HANDLE; } +#if defined(STA_CAN_BUS_ENABLE) || defined(DOXYGEN) + /** + * @brief Function that returns the CAN handle used for the CAN bus. Override it in userspace to adjust. + * + * @ingroup tacos_startup + */ + STA_WEAK + CAN_HandleTypeDef * getCanController(){ + return &STA_STM32_CAN_HANDLE; + } +#endif /* STA_CAN_BUS_ENABLE */ + /** * @brief Function that initializes the printable object given by getUARThandle(). * @@ -134,6 +143,23 @@ namespace sta extern CAN_HandleTypeDef hcan1; return new STM32CanController(&hcan1); } + /** + * @brief Function that is called before the Can Bus task is started. Override it to adjust + * the Can bus to your specifications. + * + * @ingroup tacos_startup + */ + STA_WEAK + void onCanBusInit() + {} + + void initCanBus() + { + onCanBusInit(); + + CanBus::instance(getCanController())->start(); + } + } // namespace tacos @@ -153,15 +179,11 @@ namespace sta #ifdef STA_TACOS_WATCHDOG_ENABLED tacos::initWatchdog(); #endif // STA_TACOS_WATCHDOG_ENABLED - } -#ifdef STA_RTOS_CAN_BUS_ENABLE - STA_WEAK - CAN_HandleTypeDef * getCanController(){ - return &hcan1; +#ifdef STA_CAN_BUS_ENABLE + tacos::initCanBus(); +#endif // STA_CAN_BUS_ENABLE } -#endif /* STA_RTOS_CAN_BUS_ENABLE */ - } // namespace rtos } // namespace sta From b60ddccad54300ae830f2350870c479edd668601 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 16 Feb 2024 14:45:17 +0100 Subject: [PATCH 15/30] Removed default watchdog enable --- include/sta/tacos/configs/default.hpp | 3 --- 1 file changed, 3 deletions(-) diff --git a/include/sta/tacos/configs/default.hpp b/include/sta/tacos/configs/default.hpp index c048c23..f46c692 100644 --- a/include/sta/tacos/configs/default.hpp +++ b/include/sta/tacos/configs/default.hpp @@ -1,9 +1,6 @@ #ifndef STA_TACOS_CONFIGS_DEFAULT_HPP #define STA_TACOS_CONFIGS_DEFAULT_HPP -// Enable the watchdog provided by TACOS. -#define STA_TACOS_WATCHDOG_ENABLED - // Generally, we assume the TACOS threads to have the highest priorties. #define STA_TACOS_MANAGER_PRIORITY osPriorityHigh #define STA_TACOS_STATEMACHINE_PRIORITY osPriorityHigh From bfba17245ff5a4f2c5e2f7e08a8dc719676ae32a Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 16 Feb 2024 14:48:21 +0100 Subject: [PATCH 16/30] +Define guards -redundant getter for can controller --- src/startup.cpp | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/src/startup.cpp b/src/startup.cpp index 46f8490..3531b63 100644 --- a/src/startup.cpp +++ b/src/startup.cpp @@ -138,11 +138,8 @@ namespace sta Watchdog::instance()->start(); } #endif // STA_TACOS_WATCHDOG_ENABLED - STA_WEAK - CanController * getCanController(){ - extern CAN_HandleTypeDef hcan1; - return new STM32CanController(&hcan1); - } + +#ifdef STA_CAN_BUS_ENABLE /** * @brief Function that is called before the Can Bus task is started. Override it to adjust * the Can bus to your specifications. @@ -159,7 +156,7 @@ namespace sta CanBus::instance(getCanController())->start(); } - +#endif //STA_CAN_BUS_ENABLE } // namespace tacos From 16b2ed5a20df1c04b9bbcf5e12101d5e822e45d2 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 16 Feb 2024 17:43:28 +0100 Subject: [PATCH 17/30] Can bus start without handle --- include/sta/tacos.hpp | 3 +++ include/sta/tacos/can_bus.hpp | 8 ++++---- src/can_bus.cpp | 28 +++++++++++++++++----------- src/startup.cpp | 2 +- src/tacos.cpp | 6 +++++- 5 files changed, 30 insertions(+), 17 deletions(-) diff --git a/include/sta/tacos.hpp b/include/sta/tacos.hpp index c5d6edc..74f1593 100644 --- a/include/sta/tacos.hpp +++ b/include/sta/tacos.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -76,6 +77,8 @@ namespace sta return thread_ptr; } + + //bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout); } // namespace tacos } diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp index 82a9cd8..4762098 100644 --- a/include/sta/tacos/can_bus.hpp +++ b/include/sta/tacos/can_bus.hpp @@ -32,19 +32,19 @@ namespace sta { public: - CanBus(CAN_HandleTypeDef * controller); + CanBus(); /** * @brief Getter function for the singleton instance. */ - static CanBus* instance(CAN_HandleTypeDef * handle) + static CanBus* instance() { static CGuard g; if (!_instance) { // Create the can bus singleton instance. - CanBus::_instance = new CanBus(handle); + CanBus::_instance = new CanBus(); } return _instance; @@ -115,7 +115,7 @@ namespace sta AlpakaCanBus canBus_; - + static RtosEvent messageEvent; }; diff --git a/src/can_bus.cpp b/src/can_bus.cpp index c06a947..acbcff0 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -2,17 +2,16 @@ #ifdef STA_CAN_BUS_ENABLE #include -#include -#include #include - +// TODO fix this shit +extern CAN_HandleTypeDef hcan1; namespace sta { namespace tacos { - CanBus::CanBus(CAN_HandleTypeDef * controller) + CanBus::CanBus() : TacosThread{"Can Bus", STA_TACOS_CAN_BUS_PRIORITY}, - canBusController_(new STM32CanController(controller)), + canBusController_(new STM32CanController(&STA_STM32_CAN_HANDLE)), canBusSysQueue_(STA_RTOS_CAN_BUS_QUEUE_LENGTH), canBusDataQueue_(STA_RTOS_CAN_BUS_QUEUE_LENGTH), canBus_{canBusController_, HAL_GetTick, sta::tacos::handleSysMessage, sta::tacos::handleDataMessage} @@ -26,8 +25,10 @@ namespace sta void CanBus::func() { - uint32_t flags = osThreadFlagsWait(STA_RTOS_THREAD_FLAGS_VALID_BITS, osFlagsWaitAny, 50); + messageEvent.clear(STA_RTOS_CAN_ANY); + uint32_t flags = messageEvent.wait(STA_RTOS_CAN_ANY, osWaitForever); + STA_DEBUG_PRINTLN("CanBus received Flag"); if (flags != static_cast(osErrorTimeout)) { STA_ASSERT_MSG((flags & osStatusReserved) == flags, "Unexpected error occurred in wait"); @@ -38,6 +39,7 @@ namespace sta CanSysMsg msg; while (CanBus::_instance->getCanBusMsg(&msg, 0)) { + STA_DEBUG_PRINTLN("CanBus about to send"); canBus_.send(msg); } } @@ -76,8 +78,9 @@ namespace sta if (canBusDataQueue_.put(msg, timeout)) { - // Signal thread - notify(STA_RTOS_CAN_FLAG_DATA_QUEUED); + // Signal task + messageEvent.set(STA_RTOS_CAN_FLAG_DATA_QUEUED); + messageEvent.clear(STA_RTOS_CAN_ANY); return true; } else @@ -92,8 +95,9 @@ namespace sta if (canBusSysQueue_.put(msg, timeout)) { - // Signal thread - notify(STA_RTOS_CAN_FLAG_SYS_QUEUED); + // Signal tasek + messageEvent.set(STA_RTOS_CAN_FLAG_SYS_QUEUED); + messageEvent.clear(STA_RTOS_CAN_ANY); return true; } else @@ -115,6 +119,8 @@ namespace sta CanBus* CanBus::_instance = nullptr; + RtosEvent CanBus::messageEvent; + } /* namespace tacos */ } /* namespace sta */ @@ -193,4 +199,4 @@ namespace sta { } // namespace tacos } // namespace sta -#endif // STA_CAN_BUS_ENABLE \ No newline at end of file +#endif // STA_CAN_BUS_ENABLE diff --git a/src/startup.cpp b/src/startup.cpp index 3531b63..8acfc29 100644 --- a/src/startup.cpp +++ b/src/startup.cpp @@ -154,7 +154,7 @@ namespace sta { onCanBusInit(); - CanBus::instance(getCanController())->start(); + CanBus::instance()->start(); } #endif //STA_CAN_BUS_ENABLE } // namespace tacos diff --git a/src/tacos.cpp b/src/tacos.cpp index 45aa708..8cc7d1b 100644 --- a/src/tacos.cpp +++ b/src/tacos.cpp @@ -7,7 +7,6 @@ #include - namespace sta { namespace tacos @@ -26,6 +25,11 @@ namespace sta { Statemachine::instance()->requestTimedStateTransition(from, to, millis, lockout); } + /*bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout){ + //extern CAN_HandleTypeDef hcan1; + //return CanBus::instance(STA_STM32_CAN_HANDLE)->queueCanBusMsg(msg, timeout); + return false; + }*/ } // namespace tacos } // namespace sta From 3d586b3e0bb80784b42986c7c2013b4e9a99bbff Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Mon, 26 Feb 2024 18:17:56 +0100 Subject: [PATCH 18/30] FC frame after sending and synchronous testing --- src/can_bus.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/can_bus.cpp b/src/can_bus.cpp index acbcff0..4eba387 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -52,6 +52,8 @@ namespace sta { canBus_.send(msg); } + + canBus_.tx_.processFrame(); } if (flags & STA_RTOS_CAN_FLAG_MSG_AVAIL) @@ -67,6 +69,9 @@ namespace sta } } + // Synchronus message check + canBus_.processRx(); + // Process ISOTP transmissions canBus_.processTx(); } From b403559933e5b13679911386bba6431ef489a0cb Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Wed, 28 Feb 2024 12:07:30 +0100 Subject: [PATCH 19/30] Added timeout so ISOTP processTX gets run time --- src/can_bus.cpp | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/can_bus.cpp b/src/can_bus.cpp index 4eba387..a1aaf96 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -26,9 +26,8 @@ namespace sta void CanBus::func() { messageEvent.clear(STA_RTOS_CAN_ANY); - uint32_t flags = messageEvent.wait(STA_RTOS_CAN_ANY, osWaitForever); + uint32_t flags = messageEvent.wait(STA_RTOS_CAN_ANY, 800); - STA_DEBUG_PRINTLN("CanBus received Flag"); if (flags != static_cast(osErrorTimeout)) { STA_ASSERT_MSG((flags & osStatusReserved) == flags, "Unexpected error occurred in wait"); @@ -53,7 +52,7 @@ namespace sta canBus_.send(msg); } - canBus_.tx_.processFrame(); + //canBus_.tx_.processFrame(); } if (flags & STA_RTOS_CAN_FLAG_MSG_AVAIL) @@ -100,7 +99,7 @@ namespace sta if (canBusSysQueue_.put(msg, timeout)) { - // Signal tasek + // Signal task messageEvent.set(STA_RTOS_CAN_FLAG_SYS_QUEUED); messageEvent.clear(STA_RTOS_CAN_ANY); return true; From 43a8d19d08f6c8ceb0e890dd407d88e3b61f5db5 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Wed, 28 Feb 2024 13:23:13 +0100 Subject: [PATCH 20/30] made startup.cpp independent of hcan --- src/startup.cpp | 15 --------------- 1 file changed, 15 deletions(-) diff --git a/src/startup.cpp b/src/startup.cpp index 8acfc29..a841f00 100644 --- a/src/startup.cpp +++ b/src/startup.cpp @@ -31,9 +31,6 @@ #include #include -// TODO: WHY DO I HAVE TO PUT THIS HERE???? DO YOU THINK YOU ARE SPECIAL, HUH MR. HCAN1? WHY CAN'T YOU BE NORMAL LIKE HUART -extern CAN_HandleTypeDef hcan1; - // The UART mutex defined in freertos.c extern osMutexId_t uartMutexHandle; @@ -60,18 +57,6 @@ namespace sta return &STA_STM32_USART_HANDLE; } -#if defined(STA_CAN_BUS_ENABLE) || defined(DOXYGEN) - /** - * @brief Function that returns the CAN handle used for the CAN bus. Override it in userspace to adjust. - * - * @ingroup tacos_startup - */ - STA_WEAK - CAN_HandleTypeDef * getCanController(){ - return &STA_STM32_CAN_HANDLE; - } -#endif /* STA_CAN_BUS_ENABLE */ - /** * @brief Function that initializes the printable object given by getUARThandle(). * From f0176bb2ed106a86d31f5f8b3e78a81ac7b5290e Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Wed, 28 Feb 2024 14:38:38 +0100 Subject: [PATCH 21/30] TACOS interfaces for queing msg --- include/sta/tacos.hpp | 4 +++- src/tacos.cpp | 13 ++++++++----- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/include/sta/tacos.hpp b/include/sta/tacos.hpp index 74f1593..f969315 100644 --- a/include/sta/tacos.hpp +++ b/include/sta/tacos.hpp @@ -78,7 +78,9 @@ namespace sta return thread_ptr; } - //bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout); + bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout); + + bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout); } // namespace tacos } diff --git a/src/tacos.cpp b/src/tacos.cpp index 8cc7d1b..5c95424 100644 --- a/src/tacos.cpp +++ b/src/tacos.cpp @@ -25,11 +25,14 @@ namespace sta { Statemachine::instance()->requestTimedStateTransition(from, to, millis, lockout); } - /*bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout){ - //extern CAN_HandleTypeDef hcan1; - //return CanBus::instance(STA_STM32_CAN_HANDLE)->queueCanBusMsg(msg, timeout); - return false; - }*/ + + bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout){ + return CanBus::instance()->queueCanBusMsg(msg, timeout); + } + + bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout){ + return CanBus::instance()->queueCanBusMsg(msg, timeout); + } } // namespace tacos } // namespace sta From 7147d6de8308110eace178b11ab88548ca29177a Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 8 Mar 2024 13:55:01 +0100 Subject: [PATCH 22/30] CAN implementation for standard CAN --- include/sta/tacos.hpp | 4 +--- include/sta/tacos/thread.hpp | 23 +++++++++++++++++++++++ src/can_bus.cpp | 36 ++++++++++++++++++++---------------- src/tacos.cpp | 6 +----- src/thread.cpp | 14 ++++++++++++++ 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/include/sta/tacos.hpp b/include/sta/tacos.hpp index f969315..55f30f8 100644 --- a/include/sta/tacos.hpp +++ b/include/sta/tacos.hpp @@ -78,9 +78,7 @@ namespace sta return thread_ptr; } - bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout); - - bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout); + bool queueCanBusMsg(CanSysMsg & msg, uint32_t timeout); } // namespace tacos } diff --git a/include/sta/tacos/thread.hpp b/include/sta/tacos/thread.hpp index 82a07dd..6344d55 100644 --- a/include/sta/tacos/thread.hpp +++ b/include/sta/tacos/thread.hpp @@ -12,6 +12,8 @@ #include #include +#include +#include /** * @defgroup tacos_thread TACOS Thread @@ -126,6 +128,24 @@ namespace sta */ void sleep(uint32_t ticks); +#ifdef STA_CAN_BUS_ENABLE + /** + * @brief Set the ID of the CAN bus this thread is associated with. + * + * @param canID + */ + void setCanID(uint32_t canID); + + /** + * @brief Get the ID of the CAN bus this thread is associated with. + * + * @return can ID + */ + uint32_t getCanID(); + + sta::RtosQueue CAN_queue_; +#endif // STA_CAN_BUS_ENABLE + #ifdef STA_TACOS_WATCHDOG_ENABLED /** * @brief This macro wraps a given statement into waiting() and heartbeat() to make the code more readable. @@ -191,6 +211,9 @@ namespace sta bool running_; #ifdef STA_TACOS_WATCHDOG_ENABLED ThreadStatus status_; +#endif // STA_TACOS_WATCHDOG_ENABLED +#ifdef STA_CAN_BUS_ENABLE + uint32_t canID_; #endif // STA_TACOS_WATCHDOG_ENABLED bool terminate_; }; diff --git a/src/can_bus.cpp b/src/can_bus.cpp index a1aaf96..4ac2256 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -3,6 +3,8 @@ #include #include +#include + // TODO fix this shit extern CAN_HandleTypeDef hcan1; namespace sta @@ -43,18 +45,6 @@ namespace sta } } - if (flags & STA_RTOS_CAN_FLAG_DATA_QUEUED) - { - // Take messages from queue until empty - CanDataMsg msg; - while (CanBus::_instance->getCanBusMsg(&msg, 0)) - { - canBus_.send(msg); - } - - //canBus_.tx_.processFrame(); - } - if (flags & STA_RTOS_CAN_FLAG_MSG_AVAIL) { STA_DEBUG_PRINTLN("[event] CAN INT"); @@ -68,11 +58,25 @@ namespace sta } } - // Synchronus message check - canBus_.processRx(); + CanRxHeader rxHeader; //CAN Bus Receive Header + uint8_t canRX[8] = {0,0,0,0,0,0,0,0}; //CAN Bus Receive Buffer + + canBusController_->receiveFrame(CAN_RX_FIFO0, &rxHeader, canRX); + + // Create a CANSysMsg from the received data + CanSysMsg sysMsg; + sysMsg.header.sid = rxHeader.id.sid; + sysMsg.header.payloadLength = rxHeader.payloadLength; + std::memcpy(sysMsg.payload, canRX, rxHeader.payloadLength); + + // Append to the correct thread's queue + for (std::shared_ptr thread : Manager::instance()->getActiveThreads()){ + if (thread->getCanID() == rxHeader.id.sid){ + thread->CAN_queue_.put(sysMsg); + break; + } + } - // Process ISOTP transmissions - canBus_.processTx(); } bool CanBus::queueCanBusMsg(const CanDataMsg& msg, uint32_t timeout) diff --git a/src/tacos.cpp b/src/tacos.cpp index 5c95424..6860a51 100644 --- a/src/tacos.cpp +++ b/src/tacos.cpp @@ -25,12 +25,8 @@ namespace sta { Statemachine::instance()->requestTimedStateTransition(from, to, millis, lockout); } - - bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout){ - return CanBus::instance()->queueCanBusMsg(msg, timeout); - } - bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout){ + bool queueCanBusMsg(CanSysMsg & msg, uint32_t timeout){ return CanBus::instance()->queueCanBusMsg(msg, timeout); } } // namespace tacos diff --git a/src/thread.cpp b/src/thread.cpp index 41e2b16..a536b67 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -27,6 +27,10 @@ namespace sta #ifdef STA_TACOS_WATCHDOG_ENABLED status_{ThreadStatus::STOPPED}, #endif // STA_TACOS_WATCHDOG_ENABLED +#ifdef STA_CAN_BUS_ENABLE + canID_{0}, + CAN_queue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, +#endif // STA_CAN_BUS_ENABLE terminate_{false} { STA_ASSERT(stack_size >= 0); @@ -165,6 +169,16 @@ namespace sta TacosThread::~TacosThread(){} +#ifdef STA_CAN_BUS_ENABLE + void TacosThread::setCanID(uint32_t canID){ + canID_ = canID; + } + + uint32_t TacosThread::getCanID(){ + return canID_; + } +#endif // STA_CAN_BUS_ENABLE + } // namespace tacos } // namespace sta From 39024e4a8c53ed19f9313a1d52adff763d06ba75 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 8 Mar 2024 19:03:50 +0100 Subject: [PATCH 23/30] Basic working can implementation --- src/can_bus.cpp | 48 +++++++++++++++++++++++++++++++++--------------- 1 file changed, 33 insertions(+), 15 deletions(-) diff --git a/src/can_bus.cpp b/src/can_bus.cpp index 4ac2256..e6213e3 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -28,7 +28,7 @@ namespace sta void CanBus::func() { messageEvent.clear(STA_RTOS_CAN_ANY); - uint32_t flags = messageEvent.wait(STA_RTOS_CAN_ANY, 800); + uint32_t flags = messageEvent.wait(STA_RTOS_CAN_ANY, 500); if (flags != static_cast(osErrorTimeout)) { @@ -40,7 +40,6 @@ namespace sta CanSysMsg msg; while (CanBus::_instance->getCanBusMsg(&msg, 0)) { - STA_DEBUG_PRINTLN("CanBus about to send"); canBus_.send(msg); } } @@ -49,7 +48,7 @@ namespace sta { STA_DEBUG_PRINTLN("[event] CAN INT"); - canBus_.processRx(); + //canBus_.processRx(); } if (flags & STA_RTOS_CAN_FLAG_SHOW_STATS) @@ -61,19 +60,37 @@ namespace sta CanRxHeader rxHeader; //CAN Bus Receive Header uint8_t canRX[8] = {0,0,0,0,0,0,0,0}; //CAN Bus Receive Buffer - canBusController_->receiveFrame(CAN_RX_FIFO0, &rxHeader, canRX); + bool received_ = canBusController_->receiveFrame(CAN_RX_FIFO0, &rxHeader, canRX); + if (received_){ + // Create a CANSysMsg from the received data + CanSysMsg sysMsg; + sysMsg.header.sid = rxHeader.id.sid; + sysMsg.header.payloadLength = rxHeader.payloadLength; + std::memcpy(sysMsg.payload, canRX, rxHeader.payloadLength); - // Create a CANSysMsg from the received data - CanSysMsg sysMsg; - sysMsg.header.sid = rxHeader.id.sid; - sysMsg.header.payloadLength = rxHeader.payloadLength; - std::memcpy(sysMsg.payload, canRX, rxHeader.payloadLength); + // Append to the correct thread's queue + for (std::shared_ptr thread : Manager::instance()->getActiveThreads()){ + if (thread->getCanID() == rxHeader.id.sid){ + thread->CAN_queue_.put(sysMsg); + break; + } + } + } + received_ = canBusController_->receiveFrame(CAN_RX_FIFO1, &rxHeader, canRX); + if (received_){ + // Create a CANSysMsg from the received data + CanSysMsg sysMsg; + sysMsg.header.sid = rxHeader.id.sid; + sysMsg.header.payloadLength = rxHeader.payloadLength; + sysMsg.header.format = 0; + std::memcpy(sysMsg.payload, canRX, rxHeader.payloadLength); - // Append to the correct thread's queue - for (std::shared_ptr thread : Manager::instance()->getActiveThreads()){ - if (thread->getCanID() == rxHeader.id.sid){ - thread->CAN_queue_.put(sysMsg); - break; + // Append to the correct thread's queue + for (std::shared_ptr thread : Manager::instance()->getActiveThreads()){ + if (thread->getCanID() == rxHeader.id.sid){ + thread->CAN_queue_.put(sysMsg); + break; + } } } @@ -99,7 +116,8 @@ namespace sta bool CanBus::queueCanBusMsg(const CanSysMsg& msg, uint32_t timeout) { - STA_ASSERT((msg.header.sid & ~STA_CAN_SID_SYS_BITS) == 0); + // This technicall should check if we are using a system message, but we just pretending that everything is one of those rn + //STA_ASSERT((msg.header.sid & ~STA_CAN_SID_SYS_BITS) == 0); if (canBusSysQueue_.put(msg, timeout)) { From 8637be5bc9ee9d5dd9498b48653eab8fd17b133f Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Mon, 11 Mar 2024 14:58:29 +0100 Subject: [PATCH 24/30] Initializer list warning resolved --- src/thread.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/thread.cpp b/src/thread.cpp index a536b67..61bf62a 100644 --- a/src/thread.cpp +++ b/src/thread.cpp @@ -28,8 +28,8 @@ namespace sta status_{ThreadStatus::STOPPED}, #endif // STA_TACOS_WATCHDOG_ENABLED #ifdef STA_CAN_BUS_ENABLE - canID_{0}, CAN_queue_{STA_RTOS_CAN_BUS_QUEUE_LENGTH}, + canID_{0}, #endif // STA_CAN_BUS_ENABLE terminate_{false} { From 683b680f00c56e4a2e58edfc93937417e7ddc8c6 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 13:37:31 +0200 Subject: [PATCH 25/30] CAN via IRQ --- include/sta/tacos/can_bus.hpp | 13 ++++- src/can_bus.cpp | 96 ++++++++++++++++++++--------------- 2 files changed, 67 insertions(+), 42 deletions(-) diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp index 4762098..e265109 100644 --- a/include/sta/tacos/can_bus.hpp +++ b/include/sta/tacos/can_bus.hpp @@ -75,6 +75,7 @@ namespace sta * @return True if message was retrieved successfully */ bool getCanBusMsg(CanDataMsg * msg, uint32_t timeout); + /** * @brief Retrieve system message from CAN driver TX queue. * @@ -84,6 +85,13 @@ namespace sta */ bool getCanBusMsg(CanSysMsg * msg, uint32_t timeout); + /** + * @brief Buffers incoming message and sets event to notify the system. + * + * @param fifo FIFO number of the received message. + */ + void canCallback(uint32_t fifo); + void init() override; void func() override; @@ -105,8 +113,9 @@ namespace sta sta::STM32CanController * canBusController_; - CanDataMsg canBusDataQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; - CanSysMsg canBusSysQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; + //CanDataMsg canBusDataQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; + CanSysMsg* canBusSysQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; + uint8_t bufferIndex; RtosQueue canBusSysQueue_; RtosQueue canBusDataQueue_; diff --git a/src/can_bus.cpp b/src/can_bus.cpp index e6213e3..e26a8b2 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -18,17 +18,27 @@ namespace sta canBusDataQueue_(STA_RTOS_CAN_BUS_QUEUE_LENGTH), canBus_{canBusController_, HAL_GetTick, sta::tacos::handleSysMessage, sta::tacos::handleDataMessage} { + bufferIndex = 0; + for(int i = 0; i < STA_RTOS_CAN_BUS_QUEUE_LENGTH; i++){ + canBusSysQueueBuffer_[i] = nullptr; + } } void CanBus::init() { canBusController_->start(); + + if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK || + HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING)) + { + Error_Handler(); + } } void CanBus::func() { messageEvent.clear(STA_RTOS_CAN_ANY); - uint32_t flags = messageEvent.wait(STA_RTOS_CAN_ANY, 500); + uint32_t flags = messageEvent.wait(STA_RTOS_CAN_ANY, osWaitForever); if (flags != static_cast(osErrorTimeout)) { @@ -46,8 +56,22 @@ namespace sta if (flags & STA_RTOS_CAN_FLAG_MSG_AVAIL) { - STA_DEBUG_PRINTLN("[event] CAN INT"); + CanSysMsg sysMsg; + // Iterate through buffer and set back to nullptr after use + for(int i = 0; i < STA_RTOS_CAN_BUS_QUEUE_LENGTH; i++){ + if(canBusSysQueueBuffer_[i] != nullptr){ + sysMsg = *canBusSysQueueBuffer_[i]; + canBusSysQueueBuffer_[i] = nullptr; + // Append to the correct thread's queue + for (std::shared_ptr thread : Manager::instance()->getActiveThreads()){ + if (thread->getCanID() == sysMsg.header.sid){ + thread->CAN_queue_.put(sysMsg); + break; + } + } + } + } //canBus_.processRx(); } @@ -57,43 +81,6 @@ namespace sta } } - CanRxHeader rxHeader; //CAN Bus Receive Header - uint8_t canRX[8] = {0,0,0,0,0,0,0,0}; //CAN Bus Receive Buffer - - bool received_ = canBusController_->receiveFrame(CAN_RX_FIFO0, &rxHeader, canRX); - if (received_){ - // Create a CANSysMsg from the received data - CanSysMsg sysMsg; - sysMsg.header.sid = rxHeader.id.sid; - sysMsg.header.payloadLength = rxHeader.payloadLength; - std::memcpy(sysMsg.payload, canRX, rxHeader.payloadLength); - - // Append to the correct thread's queue - for (std::shared_ptr thread : Manager::instance()->getActiveThreads()){ - if (thread->getCanID() == rxHeader.id.sid){ - thread->CAN_queue_.put(sysMsg); - break; - } - } - } - received_ = canBusController_->receiveFrame(CAN_RX_FIFO1, &rxHeader, canRX); - if (received_){ - // Create a CANSysMsg from the received data - CanSysMsg sysMsg; - sysMsg.header.sid = rxHeader.id.sid; - sysMsg.header.payloadLength = rxHeader.payloadLength; - sysMsg.header.format = 0; - std::memcpy(sysMsg.payload, canRX, rxHeader.payloadLength); - - // Append to the correct thread's queue - for (std::shared_ptr thread : Manager::instance()->getActiveThreads()){ - if (thread->getCanID() == rxHeader.id.sid){ - thread->CAN_queue_.put(sysMsg); - break; - } - } - } - } bool CanBus::queueCanBusMsg(const CanDataMsg& msg, uint32_t timeout) @@ -116,7 +103,7 @@ namespace sta bool CanBus::queueCanBusMsg(const CanSysMsg& msg, uint32_t timeout) { - // This technicall should check if we are using a system message, but we just pretending that everything is one of those rn + // This technically should check if we are using a system message, but we just pretending that everything is one of those rn //STA_ASSERT((msg.header.sid & ~STA_CAN_SID_SYS_BITS) == 0); if (canBusSysQueue_.put(msg, timeout)) @@ -132,6 +119,29 @@ namespace sta } } + void CanBus::canCallback(uint32_t fifo){ + if(messageEvent.get() != STA_RTOS_CAN_FLAG_MSG_AVAIL){ + // get here does not work since FreeRTOS is a buggy mess + messageEvent.set(STA_RTOS_CAN_FLAG_MSG_AVAIL); + + CanRxHeader rxHeader; //CAN Bus Receive Header + uint8_t canRX[8] = {0,0,0,0,0,0,0,0}; //CAN Bus Receive Buffer + + bool received_ = canBusController_->receiveFrame(fifo, &rxHeader, canRX); + + if(received_){ + CanSysMsg sysMsg; + sysMsg.header.sid = rxHeader.id.sid; + sysMsg.header.payloadLength = rxHeader.payloadLength; + std::memcpy(sysMsg.payload, canRX, rxHeader.payloadLength); + + canBusSysQueueBuffer_[bufferIndex] = &sysMsg; + + bufferIndex++; + if (bufferIndex >= STA_RTOS_CAN_BUS_QUEUE_LENGTH) bufferIndex = 0; + } + } + } bool CanBus::getCanBusMsg(CanDataMsg * msg, uint32_t timeout) { @@ -150,7 +160,13 @@ namespace sta } /* namespace tacos */ } /* namespace sta */ + namespace sta { + + void CanBus_RxPendingCallback(uint32_t fifo){ + sta::tacos::CanBus::instance()->canCallback(fifo); + } + namespace tacos { void handleSysMessage(const sta::CanRxHeader & header, const uint8_t * payload) From 5ecc5f9211b97763f0ddf496d8cc021b1c8d67a2 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 13:37:55 +0200 Subject: [PATCH 26/30] Raised CAN Task Prio since it now uses less runtime --- include/sta/tacos/configs/default.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/sta/tacos/configs/default.hpp b/include/sta/tacos/configs/default.hpp index f46c692..a5967f7 100644 --- a/include/sta/tacos/configs/default.hpp +++ b/include/sta/tacos/configs/default.hpp @@ -5,7 +5,7 @@ #define STA_TACOS_MANAGER_PRIORITY osPriorityHigh #define STA_TACOS_STATEMACHINE_PRIORITY osPriorityHigh #define STA_TACOS_WATCHDOG_PRIORITY osPriorityHigh -#define STA_TACOS_CAN_BUS_PRIORITY osPriorityNormal +#define STA_TACOS_CAN_BUS_PRIORITY osPriorityHigh // Per default, we assume state 0 to be the initial state. #define STA_TACOS_INITIAL_STATE 0 From 104d2931be82af924f7cd6123357b9df0bd43949 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 15:24:28 +0200 Subject: [PATCH 27/30] Cut out isotp and added CAN ID for state transition --- include/sta/tacos/can_bus.hpp | 31 ++----- include/sta/tacos/configs/default.hpp | 3 + src/can_bus.cpp | 119 ++++---------------------- 3 files changed, 26 insertions(+), 127 deletions(-) diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp index e265109..b7e8c88 100644 --- a/include/sta/tacos/can_bus.hpp +++ b/include/sta/tacos/can_bus.hpp @@ -5,11 +5,12 @@ #ifdef STA_CAN_BUS_ENABLE -#include #include #include #include +#include +#include #include @@ -50,14 +51,6 @@ namespace sta return _instance; } - /** - * @brief Place data message in CAN driver TX queue. - * - * @param msg Message to transmit - * @param timeout Timeout for placing message (0 = no wait, osWaitForever = blocking) - * @return True if message was queued successfully - */ - bool queueCanBusMsg(const CanDataMsg & msg, uint32_t timeout); /** * @brief Place system message in CAN driver TX queue. * @@ -67,15 +60,6 @@ namespace sta */ bool queueCanBusMsg(const CanSysMsg & msg, uint32_t timeout); - /** - * @brief Retrieve data message from CAN driver TX queue. - * - * @param[out] msg Output address for retrieved message - * @param timeout Timeout for retrieving message (0 = no wait, osWaitForever = blocking) - * @return True if message was retrieved successfully - */ - bool getCanBusMsg(CanDataMsg * msg, uint32_t timeout); - /** * @brief Retrieve system message from CAN driver TX queue. * @@ -113,12 +97,10 @@ namespace sta sta::STM32CanController * canBusController_; - //CanDataMsg canBusDataQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; CanSysMsg* canBusSysQueueBuffer_[STA_RTOS_CAN_BUS_QUEUE_LENGTH]; uint8_t bufferIndex; RtosQueue canBusSysQueue_; - RtosQueue canBusDataQueue_; uint32_t canBusStack[256]; @@ -127,10 +109,11 @@ namespace sta static RtosEvent messageEvent; }; - - void handleSysMessage(const sta::CanRxHeader & header, const uint8_t * payload); - - void handleDataMessage(const sta::IsotpMessage & msg); + /** + * @brief Callback function for handling received messages. Intended for state transitions. + */ + STA_WEAK + void handleSysMessage(CanMsgHeader & header, uint8_t * payload); } /* namespace tacos */ diff --git a/include/sta/tacos/configs/default.hpp b/include/sta/tacos/configs/default.hpp index a5967f7..b19db1a 100644 --- a/include/sta/tacos/configs/default.hpp +++ b/include/sta/tacos/configs/default.hpp @@ -15,4 +15,7 @@ #define STA_RTOS_CAN_BUS_MAX_FILTER 14 #define STA_RTOS_CAN_BUS_MAX_PAYLOAD_SIZE 8 +// State transition message define with highest CAN priority +#define STA_TACOS_CAN_BUS_SYS_MSG_ID 0x0 + #endif // STA_TACOS_CONFIGS_DEFAULT_HPP diff --git a/src/can_bus.cpp b/src/can_bus.cpp index e26a8b2..a9767fe 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -5,8 +5,8 @@ #include #include -// TODO fix this shit -extern CAN_HandleTypeDef hcan1; +extern CAN_HandleTypeDef STA_STM32_CAN_HANDLE; + namespace sta { namespace tacos @@ -15,8 +15,7 @@ namespace sta : TacosThread{"Can Bus", STA_TACOS_CAN_BUS_PRIORITY}, canBusController_(new STM32CanController(&STA_STM32_CAN_HANDLE)), canBusSysQueue_(STA_RTOS_CAN_BUS_QUEUE_LENGTH), - canBusDataQueue_(STA_RTOS_CAN_BUS_QUEUE_LENGTH), - canBus_{canBusController_, HAL_GetTick, sta::tacos::handleSysMessage, sta::tacos::handleDataMessage} + canBus_{canBusController_, HAL_GetTick} { bufferIndex = 0; for(int i = 0; i < STA_RTOS_CAN_BUS_QUEUE_LENGTH; i++){ @@ -28,8 +27,8 @@ namespace sta { canBusController_->start(); - if (HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK || - HAL_CAN_ActivateNotification(&hcan1, CAN_IT_RX_FIFO1_MSG_PENDING)) + if (HAL_CAN_ActivateNotification(&STA_STM32_CAN_HANDLE, CAN_IT_RX_FIFO0_MSG_PENDING) != HAL_OK || + HAL_CAN_ActivateNotification(&STA_STM32_CAN_HANDLE, CAN_IT_RX_FIFO1_MSG_PENDING)) { Error_Handler(); } @@ -70,37 +69,18 @@ namespace sta break; } } + + if(sysMsg.header.sid == STA_TACOS_CAN_BUS_SYS_MSG_ID){ + // Handle system message + handleSysMessage(sysMsg.header, sysMsg.payload); + } } } - //canBus_.processRx(); - } - - if (flags & STA_RTOS_CAN_FLAG_SHOW_STATS) - { - canBus_.showStatistics(); } } } - bool CanBus::queueCanBusMsg(const CanDataMsg& msg, uint32_t timeout) - { - STA_ASSERT((msg.header.sid & STA_CAN_SID_SYS_BITS) == 0); - STA_ASSERT(msg.header.payloadLength <= sizeof(msg.payload)); - - if (canBusDataQueue_.put(msg, timeout)) - { - // Signal task - messageEvent.set(STA_RTOS_CAN_FLAG_DATA_QUEUED); - messageEvent.clear(STA_RTOS_CAN_ANY); - return true; - } - else - { - return false; - } - } - bool CanBus::queueCanBusMsg(const CanSysMsg& msg, uint32_t timeout) { // This technically should check if we are using a system message, but we just pretending that everything is one of those rn @@ -133,7 +113,10 @@ namespace sta CanSysMsg sysMsg; sysMsg.header.sid = rxHeader.id.sid; sysMsg.header.payloadLength = rxHeader.payloadLength; - std::memcpy(sysMsg.payload, canRX, rxHeader.payloadLength); + + for(int i = 0; i < rxHeader.payloadLength; i++){ + sysMsg.payload[i] = canRX[i]; + } canBusSysQueueBuffer_[bufferIndex] = &sysMsg; @@ -143,11 +126,6 @@ namespace sta } } - bool CanBus::getCanBusMsg(CanDataMsg * msg, uint32_t timeout) - { - return (canBusDataQueue_.get(msg, timeout)); - } - bool CanBus::getCanBusMsg(CanSysMsg * msg, uint32_t timeout) { return (canBusSysQueue_.get(msg, timeout)); @@ -169,74 +147,9 @@ namespace sta { namespace tacos { - void handleSysMessage(const sta::CanRxHeader & header, const uint8_t * payload) + void handleSysMessage(CanMsgHeader & header, uint8_t * payload) { - // Write frame payload to DebugSerial - STA_DEBUG_PRINTLN("[event] RX sys frame"); - - debug::printFrameID(header.id); - debug::printPayloadHex(payload, header.payloadLength); - - // Sysmessage is mainly only state change from GRSM - // TODO add other cases - - // 0 is from state, 1 is to state, 2 is lockout, 3 is failsafe (-1 if inactive) - if(payload[1] > 0 && payload[1] < STA_TACOS_NUM_STATES){ - - if(payload[3] == -1){ - sta::tacos::Statemachine::instance()->requestStateTransition(payload[0], payload[1], payload[2]); - } - else{ - sta::tacos::Statemachine::instance()->requestTimedStateTransition(payload[0], payload[1], payload[2], payload[3]); - } - } - } - - void handleDataMessage(const sta::IsotpMessage & msg) - { - STA_ASSERT(msg.buffer != nullptr); - STA_ASSERT(msg.size != 0); - - STA_DEBUG_PRINTLN("[event] RX data message"); - - debug::printFrameID(msg.frameID); - - // TODO Forward message to other threads - - // if (buffer[0] == DEMO_BMP_PACKET_ID) - // { - // BmpPacket packet; - // if (unpack(buffer + 1, size - 1, &packet)) - // { - // STA_DEBUG_PRINTLN(); - // STA_DEBUG_PRINTLN("# ############"); - // STA_DEBUG_PRINTLN("# ## BMP380 ##"); - // STA_DEBUG_PRINTLN("# ############"); - // STA_DEBUG_PRINTLN("#"); - // - // STA_DEBUG_PRINT("# temperature: "); - // STA_DEBUG_PRINT(packet.temperature); - // STA_DEBUG_PRINTLN(" *C"); - // STA_DEBUG_PRINT("# pressure: "); - // STA_DEBUG_PRINT(packet.pressure); - // STA_DEBUG_PRINTLN(" Pa"); - // STA_DEBUG_PRINT("# altitude: "); - // STA_DEBUG_PRINT(packet.altitude); - // STA_DEBUG_PRINTLN(" m"); - // STA_DEBUG_PRINTLN(); - // } - // else - // { - // STA_DEBUG_PRINTLN("[error] BMP unpack failed"); - // } - // } - // else - { - STA_DEBUG_PRINT("ID: "); - STA_DEBUG_PRINTLN(msg.buffer[0], sta::IntegerBase::HEX); - STA_DEBUG_PRINT("size: "); - STA_DEBUG_PRINTLN(msg.size); - } + // This is a weak function that can be overridden by the user } } // namespace tacos } // namespace sta From b5180002a7fb4248761583556a70c6646445c3c7 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 15:44:28 +0200 Subject: [PATCH 28/30] README: Instructions for CAN --- README.md | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index c99602f..97ea1a7 100644 --- a/README.md +++ b/README.md @@ -65,4 +65,18 @@ PS: For not officially supported chips use this as the include: #include #define STA_MCU_LITTLE_ENDIAN #define STA_PLATFORM_STM32 -``` \ No newline at end of file +``` +## Setting up the CAN Bus + +To enable the CAN Bus two things need to be done: +1. Enable CAN in the IOC with the RX0 and RX1 Interrupts enabled. +2. Add the following code to the `sta/config.hpp` file: +``` +#define STA_CAN_BUS_ENABLE +``` +PS: For not officially supported chips add this: +``` +#define STA_STM32_CAN_HANDLE {YOUR_HCAN_HANDLE} +``` + +After this messages will automatically be forwarded to the task with it's ID. To send messages use the interface defined in `tacos.hpp`. \ No newline at end of file From e3283848ef3eda29bacfacbdacca3039fcb02ff9 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 17:14:16 +0200 Subject: [PATCH 29/30] Minor Cleanup --- include/sta/tacos/can_bus.hpp | 2 -- include/sta/tacos/configs/default.hpp | 2 +- 2 files changed, 1 insertion(+), 3 deletions(-) diff --git a/include/sta/tacos/can_bus.hpp b/include/sta/tacos/can_bus.hpp index b7e8c88..216c370 100644 --- a/include/sta/tacos/can_bus.hpp +++ b/include/sta/tacos/can_bus.hpp @@ -102,8 +102,6 @@ namespace sta RtosQueue canBusSysQueue_; - uint32_t canBusStack[256]; - AlpakaCanBus canBus_; static RtosEvent messageEvent; diff --git a/include/sta/tacos/configs/default.hpp b/include/sta/tacos/configs/default.hpp index b19db1a..494a350 100644 --- a/include/sta/tacos/configs/default.hpp +++ b/include/sta/tacos/configs/default.hpp @@ -5,7 +5,7 @@ #define STA_TACOS_MANAGER_PRIORITY osPriorityHigh #define STA_TACOS_STATEMACHINE_PRIORITY osPriorityHigh #define STA_TACOS_WATCHDOG_PRIORITY osPriorityHigh -#define STA_TACOS_CAN_BUS_PRIORITY osPriorityHigh +#define STA_TACOS_CAN_BUS_PRIORITY osPriorityHigh // Per default, we assume state 0 to be the initial state. #define STA_TACOS_INITIAL_STATE 0 From 3af6656e0f122d0a32fe5407fd32c3fa7bfd2a19 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 17:33:52 +0200 Subject: [PATCH 30/30] Added Publishing and handling of CAN state transition --- include/sta/tacos.hpp | 24 ++++++++++++++++++++++++ src/can_bus.cpp | 9 ++++++++- src/tacos.cpp | 13 +++++++++++++ 3 files changed, 45 insertions(+), 1 deletion(-) diff --git a/include/sta/tacos.hpp b/include/sta/tacos.hpp index 55f30f8..4672dbf 100644 --- a/include/sta/tacos.hpp +++ b/include/sta/tacos.hpp @@ -78,7 +78,31 @@ namespace sta return thread_ptr; } + /** + * @brief Queue a message to be sent over the CAN bus. + * + * @param msg The message to be sent. + * @param timeout The time to wait for the message to be sent. + * + * @return bool True if the message was sent successfully. + * + * @ingroup tacos_api + */ bool queueCanBusMsg(CanSysMsg & msg, uint32_t timeout); + + /** + * @brief Publish a state transition message to the CAN bus. + * + * @param from The state we want to transition from. + * @param to The state we want to transition to. + * @param lockout An optional timer blocking state transition for a given time. + * + * @return bool True if the message was sent successfully. + * + * @ingroup tacos_api + */ + bool publishState(uint32_t from, uint32_t to, uint32_t lockout = 0); + } // namespace tacos } diff --git a/src/can_bus.cpp b/src/can_bus.cpp index a9767fe..52656c6 100644 --- a/src/can_bus.cpp +++ b/src/can_bus.cpp @@ -4,6 +4,7 @@ #include #include #include +#include extern CAN_HandleTypeDef STA_STM32_CAN_HANDLE; @@ -149,7 +150,13 @@ namespace sta { { void handleSysMessage(CanMsgHeader & header, uint8_t * payload) { - // This is a weak function that can be overridden by the user + // This is a weak function that can be overridden by the user, + // if they want to handle system messages in a different way, i.e. ignore them + + STA_ASSERT(header.payloadLength == 2); + + // First byte of payload is the origin state, second byte is the destination state + tacos::setState(payload[0], payload[1]); } } // namespace tacos } // namespace sta diff --git a/src/tacos.cpp b/src/tacos.cpp index 6860a51..b4e4ba3 100644 --- a/src/tacos.cpp +++ b/src/tacos.cpp @@ -29,6 +29,19 @@ namespace sta bool queueCanBusMsg(CanSysMsg & msg, uint32_t timeout){ return CanBus::instance()->queueCanBusMsg(msg, timeout); } + + bool publishState(uint32_t from, uint32_t to, uint32_t lockout /* = 0 */){ + CanSysMsg msg; + msg.header.sid = STA_TACOS_CAN_BUS_SYS_MSG_ID; + msg.header.payloadLength = 2; + msg.payload[0] = from; + msg.payload[1] = to; + + msg.header.eid = 0; + msg.header.format = 0; + + return CanBus::instance()->queueCanBusMsg(msg, lockout); + } } // namespace tacos } // namespace sta