From 21e50a5ed1c05a4f467675328fbb595d84b41759 Mon Sep 17 00:00:00 2001 From: CarlWachter Date: Tue, 30 Apr 2024 15:27:01 +0200 Subject: [PATCH] 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