From e6b8bbab9c5921c7751a77afb961bae2f371d72c Mon Sep 17 00:00:00 2001 From: "@CarlWachter" Date: Mon, 1 Jan 2024 16:04:20 +0100 Subject: [PATCH] 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);