From e6b8bbab9c5921c7751a77afb961bae2f371d72c Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Mon, 1 Jan 2024 16:04:20 +0100 Subject: [PATCH 01/18] WIP: Reworking can for TACOS compatibility --- include/sta/rtos/system/can_bus.hpp | 47 +------------ src/system/can_bus.cpp | 105 ++-------------------------- 2 files changed, 7 insertions(+), 145 deletions(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index 7a95306..bef0c11 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -5,6 +5,8 @@ #ifndef STA_RTOS_SYSTEM_CAN_BUS_HPP #define STA_RTOS_SYSTEM_CAN_BUS_HPP +// TODO REMOVE +#define STA_RTOS_CAN_BUS_ENABLE /** * @defgroup STA_RTOS_CanBus CAN driver @@ -114,51 +116,6 @@ namespace sta */ extern CanController * getCanController(); - - - /** - * @brief Send notification to CAN driver. - * - * @param flags Event flags - */ - void notifyCanBus(uint32_t flags); - - - /** - * @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); - - /** @} */ } // namespace rtos } // namespace sta diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index b1f06cf..2c1b26c 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -2,6 +2,10 @@ * @file * @brief CAN driver thread. */ + +// TODO REMOVE +#define STA_RTOS_CAN_BUS_ENABLE + #include #ifdef STA_RTOS_CAN_BUS_ENABLE @@ -42,111 +46,15 @@ namespace uint32_t canBusStack[256]; } - -extern "C" void canBusTask(void *); - - namespace sta { namespace rtos { void initCanBus() { - // Create thread using static allocation - const osThreadAttr_t taskAttributes = { - .name = "sysCanBus", - .cb_mem = &canBusCB, - .cb_size = sizeof(canBusCB), - .stack_mem = &canBusStack[0], - .stack_size = sizeof(canBusStack), - .priority = (osPriority_t) osPriorityLow, - }; - - canBusTaskHandle = osThreadNew(canBusTask, NULL, &taskAttributes); - STA_ASSERT_MSG(canBusTaskHandle != nullptr, "System CAN task initialization failed"); - - - // Create message queues using static allocation - const osMessageQueueAttr_t dataQueueAttributes = { - .name = "sysCanDataOut", - .attr_bits = 0, - .cb_mem = &canBusDataQueueCB, - .cb_size = sizeof(canBusDataQueueCB), - .mq_mem = &canBusDataQueueBuffer, - .mq_size = sizeof(canBusDataQueueBuffer) - }; - - canBusDataQueueHandle = osMessageQueueNew(queueLength, sizeof(CanDataMsg), &dataQueueAttributes); - STA_ASSERT_MSG(canBusDataQueueHandle != nullptr, "System CAN data message queue initialization failed"); - - const osMessageQueueAttr_t sysQueueAttributes = { - .name = "sysCanSysOut", - .attr_bits = 0, - .cb_mem = &canBusSysQueueCB, - .cb_size = sizeof(canBusSysQueueCB), - .mq_mem = &canBusSysQueueBuffer, - .mq_size = sizeof(canBusSysQueueBuffer) - }; - - canBusSysQueueHandle = osMessageQueueNew(queueLength, sizeof(CanSysMsg), &sysQueueAttributes); - STA_ASSERT_MSG(canBusSysQueueHandle != nullptr, "System CAN system message queue initialization failed"); - - - // Get initialized CAN controller from application - canBusController = getCanController(); + tacos::Canbus::instance(getCanController()).start() } - - void notifyCanBus(uint32_t flags) - { - // Send flags to thread - osThreadFlagsSet(canBusTaskHandle, flags); - } - - - 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 (osOK == osMessageQueuePut(canBusDataQueueHandle, &msg, 0, timeout)) - { - // Signal thread - osThreadFlagsSet(canBusTaskHandle, 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 (osOK == osMessageQueuePut(canBusSysQueueHandle, &msg, 0, timeout)) - { - // Signal thread - osThreadFlagsSet(canBusTaskHandle, 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 rtos } // namespace sta @@ -494,9 +402,6 @@ void canBusTask(void *) // Setup ISO-TP transceiver AlpakaCanBus canBus(canBusController, HAL_GetTick, dummy::handleSysMessage, dummy::handleDataMessage); - - rtos::waitForStartupEvent(); - while (true) { uint32_t flags = osThreadFlagsWait(STA_RTOS_THREAD_FLAGS_VALID_BITS, osFlagsWaitAny, 50); From 29ef79391a393ae8cae79c35523f7c1dfa1f8c04 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Mon, 1 Jan 2024 16:15:22 +0100 Subject: [PATCH 02/18] Fixed includes --- include/sta/rtos/system/can_bus.hpp | 3 +- src/system/can_bus.cpp | 69 +---------------------------- 2 files changed, 3 insertions(+), 69 deletions(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index bef0c11..71a5591 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -29,8 +29,7 @@ #include #ifdef STA_RTOS_CAN_BUS_ENABLE - -#include +#include #include #include diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index 2c1b26c..8efe9fa 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -10,7 +10,7 @@ #ifdef STA_RTOS_CAN_BUS_ENABLE #include -#include +#include #include #include #include @@ -18,7 +18,7 @@ #include #include #include -#include +#include #include #include @@ -28,10 +28,6 @@ namespace { - StaticTask_t canBusCB; - StaticQueue_t canBusDataQueueCB; - StaticQueue_t canBusSysQueueCB; - osThreadId_t canBusTaskHandle = nullptr; osMessageQueueId_t canBusDataQueueHandle = nullptr; osMessageQueueId_t canBusSysQueueHandle = nullptr; @@ -388,65 +384,4 @@ namespace sta } } // namespace sta - - -/** - * @brief CAN driver thread entry function. - */ -void canBusTask(void *) -{ - using namespace sta; - - STA_ASSERT_MSG(canBusController != nullptr, "System CAN bus not initialized"); - - // Setup ISO-TP transceiver - AlpakaCanBus canBus(canBusController, HAL_GetTick, dummy::handleSysMessage, dummy::handleDataMessage); - - while (true) - { - 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(); - } -} - - #endif // STA_RTOS_CAN_BUS_ENABLE From 810f34e2dae191eb1d1e616782e2e50298bc3bac Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Mon, 1 Jan 2024 16:18:21 +0100 Subject: [PATCH 03/18] Fixed more includes --- src/debug/stack_overflow.cpp | 2 +- src/system/can_bus.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/src/debug/stack_overflow.cpp b/src/debug/stack_overflow.cpp index 158c454..e4a07ba 100644 --- a/src/debug/stack_overflow.cpp +++ b/src/debug/stack_overflow.cpp @@ -1,7 +1,7 @@ #include #ifdef STA_RTOS_STACK_OVERFLOW_HOOK -#include +#include #include #include diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index 8efe9fa..a71cf6d 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -9,7 +9,7 @@ #include #ifdef STA_RTOS_CAN_BUS_ENABLE -#include +#include #include #include #include From 849e523d2ccda972c3bf8fdc099d74b064680b08 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Mon, 1 Jan 2024 16:34:58 +0100 Subject: [PATCH 04/18] Changed controller getter --- include/sta/rtos/system/can_bus.hpp | 2 +- src/system/can_bus.cpp | 19 +------------------ 2 files changed, 2 insertions(+), 19 deletions(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index 71a5591..8299581 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -113,7 +113,7 @@ namespace sta * * Implementation must be provided by application. */ - extern CanController * getCanController(); + extern STM32CanController * getCanController(); /** @} */ } // namespace rtos diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index a71cf6d..4298fca 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -25,23 +25,6 @@ #include - -namespace -{ - osThreadId_t canBusTaskHandle = nullptr; - osMessageQueueId_t canBusDataQueueHandle = nullptr; - osMessageQueueId_t canBusSysQueueHandle = nullptr; - - sta::CanController * canBusController = nullptr; - - const size_t queueLength = 8; - - // Static memory buffers - CanDataMsg canBusDataQueueBuffer[queueLength]; - CanSysMsg canBusSysQueueBuffer[queueLength]; - uint32_t canBusStack[256]; -} - namespace sta { namespace rtos @@ -274,7 +257,7 @@ namespace sta if (controller_->receiveFrame(fifo, &header, payload)) { -// debug::displayFrameUART(frame); + //debug::displayFrameUART(frame); // Forward frame to callback switch (fifo) From 3b21a9aa8fd9c5372bb587a079faf1e6a3f80378 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Tue, 2 Jan 2024 01:07:44 +0100 Subject: [PATCH 05/18] Removed test define --- include/sta/rtos/system/can_bus.hpp | 3 --- src/system/can_bus.cpp | 3 --- 2 files changed, 6 deletions(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index 8299581..e9855f1 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -5,9 +5,6 @@ #ifndef STA_RTOS_SYSTEM_CAN_BUS_HPP #define STA_RTOS_SYSTEM_CAN_BUS_HPP -// TODO REMOVE -#define STA_RTOS_CAN_BUS_ENABLE - /** * @defgroup STA_RTOS_CanBus CAN driver * @ingroup STA_RTOS_API diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index 4298fca..7d7b1c6 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -3,9 +3,6 @@ * @brief CAN driver thread. */ -// TODO REMOVE -#define STA_RTOS_CAN_BUS_ENABLE - #include #ifdef STA_RTOS_CAN_BUS_ENABLE From d4aa3c4081bc82e0a0a403a61ed05ce0717b9484 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Tue, 2 Jan 2024 11:49:11 +0100 Subject: [PATCH 06/18] Moved AlpakaCanBus to TACOS --- src/system/can_bus.cpp | 69 ------------------------------------------ 1 file changed, 69 deletions(-) diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index 7d7b1c6..a382eec 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -8,7 +8,6 @@ #include #include -#include #include #include #include @@ -138,74 +137,6 @@ namespace dummy namespace sta { - class AlpakaCanBus - { - public: - using SysMsgHandler = void (*)(const CanRxHeader &, const uint8_t *); - using DataMsgHandler = void (*)(const IsotpMessage &); - - static const uint8_t FIFO_SYS = 0; - static const uint8_t FIFO_DATA = 1; - - public: - AlpakaCanBus(CanController * controller, TimeMsFn timeMs, SysMsgHandler sysMsgHandler, DataMsgHandler dataMsgHandler); - - - /** - * @brief Send system message. - * - * @param msg Message - */ - void send(const CanSysMsg & msg); - - /** - * @brief Send data message. - * - * @param msg Message - */ - void send(const CanDataMsg & msg); - - - /** - * @brief Process transmissions. - * - * Call regularly to advance transmission. - */ - void processTx(); - - /** - * @brief Process received CAN messages. - */ - void processRx(); - - /** - * @brief Display ISOTP TX/RX statistics. - */ - void showStatistics(); - - private: - /** - * @brief Configure CAN filters. - */ - void setupSubscriptions(); - - /** - * @brief Handle received data message CAN frames. - * - * @param header CAN frame header - * @param payload Payload buffer - */ - void receiveDataFrame(const CanRxHeader & header, const uint8_t * payload); - - private: - CanController * controller_; - IsotpTransmitter tx_; - IsotpReceiver rx_; - SysMsgHandler handleSysMsg_; - DataMsgHandler handleDataMsg_; - }; - - AlpakaCanBus::AlpakaCanBus(CanController * controller, TimeMsFn timeMs, SysMsgHandler sysMsgHandler, DataMsgHandler dataMsgHandler) : controller_{controller}, tx_{controller, timeMs}, rx_{controller, timeMs}, handleSysMsg_{sysMsgHandler}, handleDataMsg_{dataMsgHandler} { From d7422260a6788921e12a85c59ceb26496f292d20 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Tue, 2 Jan 2024 11:49:29 +0100 Subject: [PATCH 07/18] Fixed Queue Sizes --- include/sta/rtos/queue.tpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/include/sta/rtos/queue.tpp b/include/sta/rtos/queue.tpp index 3814188..0d97029 100644 --- a/include/sta/rtos/queue.tpp +++ b/include/sta/rtos/queue.tpp @@ -24,19 +24,19 @@ namespace sta template RtosQueue::RtosQueue(uint32_t length) - : handle_{osMessageQueueNew(length, sizeof(Message), NULL)} + : handle_{osMessageQueueNew(length, sizeof(T), NULL)} { STA_ASSERT(handle_ != NULL); } template - bool RtosQueue::put(const Message & msg, uint32_t timeout /* = osWaitForever */) + bool RtosQueue::put(const T & msg, uint32_t timeout /* = osWaitForever */) { return (osOK == osMessageQueuePut(handle_, &msg, 0, timeout)); } template - bool RtosQueue::get(Message * outMsg, uint32_t timeout /* = osWaitForever */) + bool RtosQueue::get(T * outMsg, uint32_t timeout /* = osWaitForever */) { uint8_t prio; return (osOK == osMessageQueueGet(handle_, outMsg, &prio, timeout)); From 26062cec876925d33d6065eec2c4ebea20532673 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Tue, 2 Jan 2024 11:50:00 +0100 Subject: [PATCH 08/18] Moved AlpakaCanBus to .hpp here --- include/sta/rtos/system/can_bus.hpp | 76 +++++++++++++++++++++++++++++ 1 file changed, 76 insertions(+) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index e9855f1..642f32d 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -27,7 +27,11 @@ #ifdef STA_RTOS_CAN_BUS_ENABLE #include +#include #include +#include +#include +#include #include @@ -116,6 +120,78 @@ namespace sta } // namespace rtos } // namespace sta +namespace sta +{ + + class AlpakaCanBus + { + public: + using SysMsgHandler = void (*)(const CanRxHeader &, const uint8_t *); + using DataMsgHandler = void (*)(const IsotpMessage &); + + static const uint8_t FIFO_SYS = 0; + static const uint8_t FIFO_DATA = 1; + + public: + AlpakaCanBus(CanController * controller, TimeMsFn timeMs, SysMsgHandler sysMsgHandler, DataMsgHandler dataMsgHandler); + + + /** + * @brief Send system message. + * + * @param msg Message + */ + void send(const CanSysMsg & msg); + + /** + * @brief Send data message. + * + * @param msg Message + */ + void send(const CanDataMsg & msg); + + + /** + * @brief Process transmissions. + * + * Call regularly to advance transmission. + */ + void processTx(); + + /** + * @brief Process received CAN messages. + */ + void processRx(); + + /** + * @brief Display ISOTP TX/RX statistics. + */ + void showStatistics(); + + private: + /** + * @brief Configure CAN filters. + */ + void setupSubscriptions(); + + /** + * @brief Handle received data message CAN frames. + * + * @param header CAN frame header + * @param payload Payload buffer + */ + void receiveDataFrame(const CanRxHeader & header, const uint8_t * payload); + + private: + CanController * controller_; + IsotpTransmitter tx_; + IsotpReceiver rx_; + SysMsgHandler handleSysMsg_; + DataMsgHandler handleDataMsg_; + }; + +} // namespace sta + #endif // STA_RTOS_CAN_BUS_ENABLE From 481197d752e5b8b49e65704815a0abd932906b18 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Wed, 3 Jan 2024 11:08:24 +0100 Subject: [PATCH 09/18] Predeclaration of dummy namespace --- include/sta/rtos/system/can_bus.hpp | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index 642f32d..17e25eb 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -110,11 +110,11 @@ namespace sta /** - * @brief Return CanController for use in CAN system task. + * @brief Return CAN_HandleTypeDef for use in CAN system task. * * Implementation must be provided by application. */ - extern STM32CanController * getCanController(); + extern CAN_HandleTypeDef * getCanController(); /** @} */ } // namespace rtos @@ -192,6 +192,14 @@ namespace sta } // namespace sta +namespace dummy +{ + void handleSysMessage(const sta::CanRxHeader & header, const uint8_t * payload); + + void handleDataMessage(const sta::IsotpMessage & msg); + +} // namespace dummy + #endif // STA_RTOS_CAN_BUS_ENABLE From a66565e984cffd95d4709484e9ef447308b0de4e Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Wed, 3 Jan 2024 11:08:58 +0100 Subject: [PATCH 10/18] CanBus Init --- src/system/can_bus.cpp | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index a382eec..3e6a8b0 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -7,14 +7,18 @@ #ifdef STA_RTOS_CAN_BUS_ENABLE #include +#include #include #include #include #include #include -#include #include #include +#include + +#include +#include #include #include @@ -25,9 +29,10 @@ namespace sta { namespace rtos { + void initCanBus() { - tacos::Canbus::instance(getCanController()).start() + sta::tacos::CanBus::instance(getCanController())->start(); } } // namespace rtos @@ -170,7 +175,7 @@ namespace sta } - inline void AlpakaCanBus::processTx() + void AlpakaCanBus::processTx() { tx_.process(); } From 243d52f71eb60ba057222ba77e3c3d914be84da7 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Thu, 4 Jan 2024 16:55:21 +0100 Subject: [PATCH 11/18] State transition can msg handling --- src/system/can_bus.cpp | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index 3e6a8b0..9fc36be 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -20,6 +20,9 @@ #include #include +#include +#include + #include #include @@ -87,7 +90,19 @@ namespace dummy debug::printFrameID(header.id); debug::printPayloadHex(payload, header.payloadLength); - // TODO Forward message to other threads + // 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) From e90620c2d8eb2c0cac66c7dc2e85c62ff028a436 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 5 Jan 2024 15:27:37 +0100 Subject: [PATCH 12/18] removed tacos dependencies --- include/sta/rtos/system/can_bus.hpp | 21 +++++-- src/system/can_bus.cpp | 95 ----------------------------- 2 files changed, 15 insertions(+), 101 deletions(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index 17e25eb..7b909f0 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -192,14 +192,23 @@ namespace sta } // namespace sta -namespace dummy +namespace debug { - void handleSysMessage(const sta::CanRxHeader & header, const uint8_t * payload); - - void handleDataMessage(const sta::IsotpMessage & msg); - -} // namespace dummy + /** + * @brief Output CAN frame ID to UART. + * + * @param id Frame ID + */ + void printFrameID(const sta::CanFrameId & id); + /** + * @brief Output CAN frame payload to UART. + * + * @param payload Payload buffer + * @param size Payload size + */ + void printPayloadHex(const uint8_t * payload, uint8_t size); +} // namespace debug #endif // STA_RTOS_CAN_BUS_ENABLE diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index 9fc36be..c9dd465 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -16,32 +16,13 @@ #include #include #include - #include -#include - -#include -#include #include #include #include -namespace sta -{ - namespace rtos - { - - void initCanBus() - { - sta::tacos::CanBus::instance(getCanController())->start(); - } - - } // namespace rtos -} // namespace sta - - namespace debug { /** @@ -79,82 +60,6 @@ namespace debug } } // namespace debug - -namespace dummy -{ - 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 dummy - - - namespace sta { AlpakaCanBus::AlpakaCanBus(CanController * controller, TimeMsFn timeMs, SysMsgHandler sysMsgHandler, DataMsgHandler dataMsgHandler) From 16f33857f7f32f1b7b71959e5a322728a88a53ce Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Jan 2024 15:25:35 +0100 Subject: [PATCH 13/18] CAN Bus is no longer RTOS --- include/sta/rtos/system/can_bus.hpp | 6 +++--- src/system/can_bus.cpp | 4 ++-- src/system/startup.cpp | 3 --- 3 files changed, 5 insertions(+), 8 deletions(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index 7b909f0..f12a035 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -19,12 +19,12 @@ * * @ingroup STA_RTOS_BuildConfig */ -# define STA_RTOS_CAN_BUS_ENABLE +# define STA_CAN_BUS_ENABLE #endif // DOXYGEN #include -#ifdef STA_RTOS_CAN_BUS_ENABLE +#ifdef STA_CAN_BUS_ENABLE #include #include @@ -210,6 +210,6 @@ namespace debug void printPayloadHex(const uint8_t * payload, uint8_t size); } // namespace debug -#endif // STA_RTOS_CAN_BUS_ENABLE +#endif // STA_CAN_BUS_ENABLE #endif // STA_RTOS_SYSTEM_CAN_BUS_HPP diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index c9dd465..04fedc3 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -4,7 +4,7 @@ */ #include -#ifdef STA_RTOS_CAN_BUS_ENABLE +#ifdef STA_CAN_BUS_ENABLE #include #include @@ -220,4 +220,4 @@ namespace sta } } // namespace sta -#endif // STA_RTOS_CAN_BUS_ENABLE +#endif // STA_CAN_BUS_ENABLE diff --git a/src/system/startup.cpp b/src/system/startup.cpp index f68decc..bbe4b03 100644 --- a/src/system/startup.cpp +++ b/src/system/startup.cpp @@ -32,9 +32,6 @@ namespace sta initWatchdog(); #endif // STA_RTOS_WATCHDOG_ENABLE -#ifdef STA_RTOS_CAN_BUS_ENABLE - initCanBus(); -#endif // STA_RTOS_CAN_BUS_ENABLE } } // namespace rtos } // namespace sta From 1925a1668e847e048423499f523f3293ea9d923a Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 16 Feb 2024 17:43:42 +0100 Subject: [PATCH 14/18] Flag changes --- include/sta/rtos/system/can_bus.hpp | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index f12a035..11bc389 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -62,34 +62,34 @@ * @{ */ - /** * @brief CAN frame available. */ -#define STA_RTOS_CAN_FLAG_MSG_AVAIL 0x000010U +#define STA_RTOS_CAN_FLAG_MSG_AVAIL 0x1U /** * @brief Send CAN message. */ -#define STA_RTOS_CAN_FLAG_MSG_SEND 0x000020U +#define STA_RTOS_CAN_FLAG_MSG_SEND 0x1U << 1 /** * @brief CAN data message in queue. */ -#define STA_RTOS_CAN_FLAG_DATA_QUEUED 0x000040U +#define STA_RTOS_CAN_FLAG_DATA_QUEUED 0x1U << 2 /** * @brief CAN system message in queue. */ -#define STA_RTOS_CAN_FLAG_SYS_QUEUED 0x000080U +#define STA_RTOS_CAN_FLAG_SYS_QUEUED 0x1U << 3 /** * @brief Show ISOTP statistics. */ -#define STA_RTOS_CAN_FLAG_SHOW_STATS 0x000100U - +#define STA_RTOS_CAN_FLAG_SHOW_STATS 0x1U << 4 /** * @brief CAN SID bits used for system messages. */ #define STA_CAN_SID_SYS_BITS UINT32_C(0x3) +#define STA_RTOS_CAN_ANY STA_RTOS_CAN_FLAG_MSG_AVAIL | STA_RTOS_CAN_FLAG_MSG_AVAIL | STA_RTOS_CAN_FLAG_DATA_QUEUED | STA_RTOS_CAN_FLAG_SYS_QUEUED | STA_RTOS_CAN_FLAG_SHOW_STATS + /** @} */ From a6704f4c45a82cf607907854a0005d6450cb0f06 Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Fri, 8 Mar 2024 19:02:38 +0100 Subject: [PATCH 15/18] Removed sys message bits from messages --- src/system/can_bus.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index 04fedc3..fb10781 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -76,7 +76,7 @@ namespace sta { CanTxHeader header; header.id.format = static_cast(msg.header.format); - header.id.sid = msg.header.sid & STA_CAN_SID_SYS_BITS; + header.id.sid = msg.header.sid; header.id.eid = msg.header.eid; header.payloadLength = msg.header.payloadLength; @@ -87,7 +87,7 @@ namespace sta { CanFrameId frameID; frameID.format = static_cast(msg.header.format); - frameID.sid = msg.header.sid & ~STA_CAN_SID_SYS_BITS; + frameID.sid = msg.header.sid; frameID.eid = msg.header.eid; // Start transmission via ISO-TP From 21e50a5ed1c05a4f467675328fbb595d84b41759 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 15:27:01 +0200 Subject: [PATCH 16/18] CAN w/o isotp --- include/sta/rtos/system/can_bus.hpp | 48 +---------- src/system/can_bus.cpp | 120 +--------------------------- 2 files changed, 3 insertions(+), 165 deletions(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index 11bc389..fc037f6 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -29,8 +29,6 @@ #include #include #include -#include -#include #include #include @@ -78,10 +76,6 @@ * @brief CAN system message in queue. */ #define STA_RTOS_CAN_FLAG_SYS_QUEUED 0x1U << 3 -/** - * @brief Show ISOTP statistics. - */ -#define STA_RTOS_CAN_FLAG_SHOW_STATS 0x1U << 4 /** * @brief CAN SID bits used for system messages. @@ -126,14 +120,11 @@ namespace sta class AlpakaCanBus { public: - using SysMsgHandler = void (*)(const CanRxHeader &, const uint8_t *); - using DataMsgHandler = void (*)(const IsotpMessage &); - static const uint8_t FIFO_SYS = 0; static const uint8_t FIFO_DATA = 1; public: - AlpakaCanBus(CanController * controller, TimeMsFn timeMs, SysMsgHandler sysMsgHandler, DataMsgHandler dataMsgHandler); + AlpakaCanBus(CanController * controller, TimeMsFn timeMs); /** @@ -143,51 +134,14 @@ namespace sta */ void send(const CanSysMsg & msg); - /** - * @brief Send data message. - * - * @param msg Message - */ - void send(const CanDataMsg & msg); - - - /** - * @brief Process transmissions. - * - * Call regularly to advance transmission. - */ - void processTx(); - - /** - * @brief Process received CAN messages. - */ - void processRx(); - - /** - * @brief Display ISOTP TX/RX statistics. - */ - void showStatistics(); - private: /** * @brief Configure CAN filters. */ void setupSubscriptions(); - /** - * @brief Handle received data message CAN frames. - * - * @param header CAN frame header - * @param payload Payload buffer - */ - void receiveDataFrame(const CanRxHeader & header, const uint8_t * payload); - private: CanController * controller_; - IsotpTransmitter tx_; - IsotpReceiver rx_; - SysMsgHandler handleSysMsg_; - DataMsgHandler handleDataMsg_; }; } // namespace sta diff --git a/src/system/can_bus.cpp b/src/system/can_bus.cpp index fb10781..365e01d 100644 --- a/src/system/can_bus.cpp +++ b/src/system/can_bus.cpp @@ -10,8 +10,6 @@ #include #include #include -#include -#include #include #include #include @@ -62,12 +60,9 @@ namespace debug namespace sta { - AlpakaCanBus::AlpakaCanBus(CanController * controller, TimeMsFn timeMs, SysMsgHandler sysMsgHandler, DataMsgHandler dataMsgHandler) - : controller_{controller}, tx_{controller, timeMs}, rx_{controller, timeMs}, handleSysMsg_{sysMsgHandler}, handleDataMsg_{dataMsgHandler} + AlpakaCanBus::AlpakaCanBus(CanController * controller, TimeMsFn timeMs) + : controller_{controller} { - STA_ASSERT(handleSysMsg_ != nullptr); - STA_ASSERT(handleDataMsg_ != nullptr); - setupSubscriptions(); } @@ -83,91 +78,6 @@ namespace sta controller_->sendFrame(header, msg.payload); } - void AlpakaCanBus::send(const CanDataMsg & msg) - { - CanFrameId frameID; - frameID.format = static_cast(msg.header.format); - frameID.sid = msg.header.sid; - frameID.eid = msg.header.eid; - - // Start transmission via ISO-TP - tx_.send(frameID, msg.payload, msg.header.payloadLength); - } - - - void AlpakaCanBus::processTx() - { - tx_.process(); - } - - - void AlpakaCanBus::processRx() - { - for (auto fifo : controller_->getPendingRxFifos()) - { - CanRxHeader header; - uint8_t payload[STA_RTOS_CAN_BUS_MAX_PAYLOAD_SIZE]; - - if (controller_->receiveFrame(fifo, &header, payload)) - { - //debug::displayFrameUART(frame); - - // Forward frame to callback - switch (fifo) - { - case FIFO_SYS: - handleSysMsg_(header, payload); - break; - - case FIFO_DATA: - receiveDataFrame(header, payload); - break; - - default: - STA_ASSERT(false); - } - } - } - } - - void AlpakaCanBus::showStatistics() - { - STA_DEBUG_PRINTLN(); - STA_DEBUG_PRINTLN("# ######################"); - STA_DEBUG_PRINTLN("# ## ISOTP statistics ##"); - STA_DEBUG_PRINTLN("# ######################"); - STA_DEBUG_PRINTLN("#"); - - const auto & txStats = tx_.stats(); - STA_DEBUG_PRINTLN("# Transmitter"); - STA_DEBUG_PRINT("# messages: "); - STA_DEBUG_PRINTLN(txStats.messages); - STA_DEBUG_PRINT("# blocks: "); - STA_DEBUG_PRINTLN(txStats.blocks); - STA_DEBUG_PRINT("# frames: "); - STA_DEBUG_PRINTLN(txStats.frames); - STA_DEBUG_PRINT("# timeouts: "); - STA_DEBUG_PRINTLN(txStats.timeouts); - STA_DEBUG_PRINTLN("#"); - - const auto & rxStats = rx_.stats(); - STA_DEBUG_PRINTLN("# Receiver"); - STA_DEBUG_PRINT("# messages: "); - STA_DEBUG_PRINTLN(rxStats.messages); - STA_DEBUG_PRINT("# blocks: "); - STA_DEBUG_PRINTLN(rxStats.blocks); - STA_DEBUG_PRINT("# frames: "); - STA_DEBUG_PRINTLN(rxStats.frames); - STA_DEBUG_PRINT("# timeouts: "); - STA_DEBUG_PRINTLN(rxStats.timeouts); - STA_DEBUG_PRINT("# flow control errors: "); - STA_DEBUG_PRINTLN(rxStats.flowErrors); - STA_DEBUG_PRINT("# overflows: "); - STA_DEBUG_PRINTLN(rxStats.overflows); - STA_DEBUG_PRINTLN(); - } - - void AlpakaCanBus::setupSubscriptions() { // Make sure to receive all messages @@ -192,32 +102,6 @@ namespace sta filter.fifo = FIFO_DATA; controller_->configureFilter(FIFO_DATA, filter, true); } - - void AlpakaCanBus::receiveDataFrame(const CanRxHeader & header, const uint8_t * payload) - { - // Write frame payload to DebugSerial - STA_DEBUG_PRINTLN("[event] RX data frame"); - debug::printPayloadHex(payload, header.payloadLength); - - // Process RX frame - auto handle = rx_.processFrame(header, payload); - - if (handle != IsotpReceiver::INVALID_HANDLE) - { - // Get message if completed - IsotpMessage msg; - if (rx_.getMessage(handle, &msg)) - { - handleDataMsg_(msg); - } - - // Handle FC responses - rx_.processFC(handle); - } - - // Process TX frame - tx_.processFrame(header, payload); - } } // namespace sta #endif // STA_CAN_BUS_ENABLE From 017884bbcae9a28a47a589e8187ce08439bdf920 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 15:36:07 +0200 Subject: [PATCH 17/18] README: CAN Bus --- README.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2e6d246..4255e1f 100644 --- a/README.md +++ b/README.md @@ -20,9 +20,9 @@ anywhere in the application code. ## Can Bus -TODO Add description +Mainly defers to the TACOS CAN module, but provides a simple interface for sending and receiving messages. -Configuration: +Expandable for isotp. ## Watchdog From 55771dc6013735548a5fc583a0549a255e59cfc6 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 17:34:18 +0200 Subject: [PATCH 18/18] Removed undefined Flag --- include/sta/rtos/system/can_bus.hpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/include/sta/rtos/system/can_bus.hpp b/include/sta/rtos/system/can_bus.hpp index fc037f6..906af71 100644 --- a/include/sta/rtos/system/can_bus.hpp +++ b/include/sta/rtos/system/can_bus.hpp @@ -82,7 +82,7 @@ */ #define STA_CAN_SID_SYS_BITS UINT32_C(0x3) -#define STA_RTOS_CAN_ANY STA_RTOS_CAN_FLAG_MSG_AVAIL | STA_RTOS_CAN_FLAG_MSG_AVAIL | STA_RTOS_CAN_FLAG_DATA_QUEUED | STA_RTOS_CAN_FLAG_SYS_QUEUED | STA_RTOS_CAN_FLAG_SHOW_STATS +#define STA_RTOS_CAN_ANY STA_RTOS_CAN_FLAG_MSG_AVAIL | STA_RTOS_CAN_FLAG_MSG_AVAIL | STA_RTOS_CAN_FLAG_DATA_QUEUED | STA_RTOS_CAN_FLAG_SYS_QUEUED /** @} */