From bdec2db94d53814c1e06be466a293777e25460ee Mon Sep 17 00:00:00 2001 From: AlexanderHD27 Date: Wed, 27 Nov 2024 23:29:05 +0100 Subject: [PATCH] Gobot RPC Stack --- can-interface/.vscode/settings.json | 3 +- .../components/CAN-Protocol-Stack/canTP.cpp | 75 ++ .../docs/.$NetworkStack.drawio.bkp | 556 ++++++++++++++ .../docs/NetworkStack.drawio | 697 ++++++++++++++++++ .../CAN-Protocol-Stack/include/canTP.hpp | 51 ++ .../include/{gobotRPC.cpp => gobotRPC.hpp} | 0 .../.$MCP2521_Libary_ClassDiagram.drawio.bkp | 44 +- .../docs/MCP2521_Libary_ClassDiagram.drawio | 17 +- 8 files changed, 1407 insertions(+), 36 deletions(-) create mode 100644 can-interface/components/CAN-Protocol-Stack/docs/.$NetworkStack.drawio.bkp create mode 100644 can-interface/components/CAN-Protocol-Stack/docs/NetworkStack.drawio rename can-interface/components/CAN-Protocol-Stack/include/{gobotRPC.cpp => gobotRPC.hpp} (100%) diff --git a/can-interface/.vscode/settings.json b/can-interface/.vscode/settings.json index fc46840..f84ddcc 100644 --- a/can-interface/.vscode/settings.json +++ b/can-interface/.vscode/settings.json @@ -25,6 +25,7 @@ "regex": "cpp", "array": "cpp", "string": "cpp", - "span": "cpp" + "span": "cpp", + "cstring": "cpp" } } diff --git a/can-interface/components/CAN-Protocol-Stack/canTP.cpp b/can-interface/components/CAN-Protocol-Stack/canTP.cpp index e69de29..2d126d9 100644 --- a/can-interface/components/CAN-Protocol-Stack/canTP.cpp +++ b/can-interface/components/CAN-Protocol-Stack/canTP.cpp @@ -0,0 +1,75 @@ +#include "canTP.hpp" +#include +#include + +void onRxHandlerWrapper(void *arg) { + SocketCANTP *socket = (SocketCANTP *)arg; + socket->onRxHandler(); +} + +void SocketCANTP::onRxHandler() { + uint8_t tmpBuffer[8]; + rx_info info = mcp2521->get_rx_id(MCP2521_RX_BUFFER::RXB0); + mcp2521->read_rx_buf(MCP2521_RX_BUFFER::RXB0, MCP2521_BUFFER_TYPE::DATA, tmpBuffer, info.length); + + CANTP_FRAME_TYPE ft = static_cast((tmpBuffer[0] & 0xF0) >> 4); + + switch (ft) { + case CANTP_FRAME_TYPE::SINGLE_FRAME: + if (userHandler) { + userHandler(tmpBuffer + 1, info.length - 1, userArg); + } + break; + + case CANTP_FRAME_TYPE::FIRST_FRAME: + int sizeTotal = ((tmpBuffer[0] & 0x0F) << 8 | tmpBuffer[1]) - 6; + consecitonFrameCounter = ceil(((float)(sizeTotal)) / 7.0); + memcpy(rxBuffer, tmpBuffer + 1, 6); + + sendFlowControl(CANTP_FLOW_STATUS::CLEAR_TO_SEND, blockSizes, 0); + + break; + + case CANTP_FRAME_TYPE::CONSECUTIVE_FRAME: + + + consecitonFrameCounter--; + if(consecitonFrameCounter == 0) { + if (userHandler) { + userHandler(rxBuffer, rxLength, userArg); + } + } + break; + + case CANTP_FRAME_TYPE::FLOW_CONTROL: + break; + } +} + + +SocketCANTP::SocketCANTP( + MCP2521 *mcp2521, + uint32_t address, + bool is_extended, + uint8_t blockSizes +) { + this->mcp2521 = mcp2521; + this->address = address; + this->is_extended = is_extended; + this->rxLength = 0; + this->consecitonFrameCounter = 0; + this->blockSizes = 0; + + mcp2521->register_rx0_handler(onRxHandlerWrapper, this); +} + +void SocketCANTP::send(uint8_t *data, uint8_t length) { + mcp2521->prepare_tx( + MCP2521_TX_BUFFER::TXB0, address, data, length, is_extended, false); + mcp2521->request_to_send(MCP2521_TX_BUFFER::TXB0); +} + +void SocketCANTP::register_rx_handler(rx_handler handler, void *arg) { + userHandler = handler; + userArg = arg; +} \ No newline at end of file diff --git a/can-interface/components/CAN-Protocol-Stack/docs/.$NetworkStack.drawio.bkp b/can-interface/components/CAN-Protocol-Stack/docs/.$NetworkStack.drawio.bkp new file mode 100644 index 0000000..1699233 --- /dev/null +++ b/can-interface/components/CAN-Protocol-Stack/docs/.$NetworkStack.drawio.bkp @@ -0,0 +1,556 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/can-interface/components/CAN-Protocol-Stack/docs/NetworkStack.drawio b/can-interface/components/CAN-Protocol-Stack/docs/NetworkStack.drawio new file mode 100644 index 0000000..d0a99ce --- /dev/null +++ b/can-interface/components/CAN-Protocol-Stack/docs/NetworkStack.drawio @@ -0,0 +1,697 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/can-interface/components/CAN-Protocol-Stack/include/canTP.hpp b/can-interface/components/CAN-Protocol-Stack/include/canTP.hpp index e69de29..43bf9d6 100644 --- a/can-interface/components/CAN-Protocol-Stack/include/canTP.hpp +++ b/can-interface/components/CAN-Protocol-Stack/include/canTP.hpp @@ -0,0 +1,51 @@ +#pragma once +#include "mcp2521_toplevel.hpp" + +typedef void (*rx_handler)(uint8_t *data, uint8_t length, void *arg); + +enum CANTP_FRAME_TYPE { + SINGLE_FRAME = 0b00, + FIRST_FRAME = 0b01, + CONSECUTIVE_FRAME = 0b10, + FLOW_CONTROL = 0b11 +}; + +enum CANTP_FLOW_STATUS { + CLEAR_TO_SEND = 0b0000, + WAIT = 0b0001, + OVERLOAD = 0b0010 +}; + +enum CAN_MULTI_FRAME_STATES { + WAITING_FOR_FIRST_FRAME, + WAITING_FOR_CONSECUTIVE_FRAME, +} + +class SocketCANTP { +private: + rx_handler userHandler; + void *userArg; + + MCP2521 *mcp2521; + uint32_t address; + bool is_extended; + + uint8_t rxBuffer[4096]; + uint8_t txBuffer[4096]; + + size_t rxLength; + + int consecitonFrameCounter; + uint8_t blockSizes; + +protected: + void sendFlowControl(CANTP_FLOW_STATUS status, uint8_t blockSize, uint8_t separationTime); + +public: + SocketCANTP(MCP2521 *mcp2521, uint32_t address, bool is_extended, uint8_t blockSizes); + + void send(uint8_t *data, uint8_t length); + void register_rx_handler(rx_handler handler, void *arg); + + void onRxHandler(); +} \ No newline at end of file diff --git a/can-interface/components/CAN-Protocol-Stack/include/gobotRPC.cpp b/can-interface/components/CAN-Protocol-Stack/include/gobotRPC.hpp similarity index 100% rename from can-interface/components/CAN-Protocol-Stack/include/gobotRPC.cpp rename to can-interface/components/CAN-Protocol-Stack/include/gobotRPC.hpp diff --git a/common-libaries/mcp2521/docs/.$MCP2521_Libary_ClassDiagram.drawio.bkp b/common-libaries/mcp2521/docs/.$MCP2521_Libary_ClassDiagram.drawio.bkp index 4e5516c..79ac94a 100644 --- a/common-libaries/mcp2521/docs/.$MCP2521_Libary_ClassDiagram.drawio.bkp +++ b/common-libaries/mcp2521/docs/.$MCP2521_Libary_ClassDiagram.drawio.bkp @@ -1,31 +1,31 @@ - + - + - + - + - + - + - + - + @@ -34,7 +34,7 @@ - + @@ -43,54 +43,54 @@ - + - + - + - + - + - + - + - + - + - + - + - + - + diff --git a/common-libaries/mcp2521/docs/MCP2521_Libary_ClassDiagram.drawio b/common-libaries/mcp2521/docs/MCP2521_Libary_ClassDiagram.drawio index 79ac94a..d9c532f 100644 --- a/common-libaries/mcp2521/docs/MCP2521_Libary_ClassDiagram.drawio +++ b/common-libaries/mcp2521/docs/MCP2521_Libary_ClassDiagram.drawio @@ -1,6 +1,6 @@ - + @@ -78,22 +78,13 @@ - + - - - - + - - - - - - - +