diff --git a/firmware/console/binary/serial_can.cpp b/firmware/console/binary/serial_can.cpp index 766493aa4d..343c1d3d13 100644 --- a/firmware/console/binary/serial_can.cpp +++ b/firmware/console/binary/serial_can.cpp @@ -180,6 +180,7 @@ int CanStreamerState::sendDataTimeout(const uint8_t *txbuf, int numBytes, can_sy int totalNumSent = numSent; // get a flow control (FC) frame +#if !EFI_UNIT_TEST // todo: add FC to unit-tests? CANRxFrame rxmsg; for (int numFcReceived = 0; ; numFcReceived++) { if (streamer->receive(CAN_ANY_MAILBOX, &rxmsg, timeout) != CAN_MSG_OK) { @@ -214,6 +215,7 @@ int CanStreamerState::sendDataTimeout(const uint8_t *txbuf, int numBytes, can_sy } break; } +#endif /* EFI_UNIT_TEST */ // send the rest of the data int idx = 1; @@ -250,16 +252,17 @@ int CanStreamerState::getDataFromFifo(uint8_t *rxbuf, size_t &numBytes) { can_msg_t CanStreamerState::streamAddToTxTimeout(size_t *np, const uint8_t *txbuf, can_sysinterval_t timeout) { int numBytes = *np; int offset = 0; - int minNumBytesRequiredToSend = 7 - txFifoBuf.getCount(); - while (numBytes >= minNumBytesRequiredToSend) { - txFifoBuf.put(txbuf + offset, numBytes); + + // we send here only if the TX FIFO buffer is getting overflowed + while (numBytes >= txFifoBuf.getSize() - txFifoBuf.getCount()) { + int numBytesToAdd = txFifoBuf.getSize() - txFifoBuf.getCount(); + txFifoBuf.put(txbuf + offset, numBytesToAdd); int numSent = sendDataTimeout((const uint8_t *)txFifoBuf.getElements(), txFifoBuf.getCount(), timeout); if (numSent < 1) break; txFifoBuf.clear(); offset += numSent; numBytes -= numSent; - minNumBytesRequiredToSend = 7; } // now we put the rest on hold diff --git a/firmware/hw_layer/drivers/can/can_msg_tx.cpp b/firmware/hw_layer/drivers/can/can_msg_tx.cpp index cb544a9bd6..81607ccf81 100644 --- a/firmware/hw_layer/drivers/can/can_msg_tx.cpp +++ b/firmware/hw_layer/drivers/can/can_msg_tx.cpp @@ -11,17 +11,18 @@ #include "can_msg_tx.h" -#if EFI_CAN_SUPPORT #include "can.h" extern int canWriteOk; extern int canWriteNotOk; +#if EFI_CAN_SUPPORT /*static*/ CANDriver* CanTxMessage::s_device = nullptr; /*static*/ void CanTxMessage::setDevice(CANDriver* device) { s_device = device; } +#endif // EFI_CAN_SUPPORT CanTxMessage::CanTxMessage(uint32_t eid, uint8_t dlc, bool isExtended) { #ifndef STM32H7XX @@ -46,6 +47,7 @@ CanTxMessage::CanTxMessage(uint32_t eid, uint8_t dlc, bool isExtended) { } CanTxMessage::~CanTxMessage() { +#if EFI_CAN_SUPPORT auto device = s_device; if (!device) { @@ -72,6 +74,7 @@ CanTxMessage::~CanTxMessage() { } else { canWriteNotOk++; } +#endif /* EFI_CAN_SUPPORT */ } void CanTxMessage::setDlc(uint8_t dlc) { @@ -87,20 +90,6 @@ void CanTxMessage::setBit(size_t byteIdx, size_t bitIdx) { m_frame.data8[byteIdx] |= 1 << bitIdx; } -#else - -CanTxMessage::CanTxMessage(uint32_t /*eid*/, uint8_t /*dlc*/, bool /*isExtended*/) { - -} - -CanTxMessage::~CanTxMessage() { - -} - -void CanTxMessage::setDlc(uint8_t) { } - -#endif // EFI_CAN_SUPPORT - uint8_t& CanTxMessage::operator[](size_t index) { return m_frame.data8[index]; } diff --git a/firmware/util/containers/fifo_buffer.h b/firmware/util/containers/fifo_buffer.h index 7a80f6d22c..27de106777 100644 --- a/firmware/util/containers/fifo_buffer.h +++ b/firmware/util/containers/fifo_buffer.h @@ -49,6 +49,10 @@ public: return cyclic_buffer::getCount(); } + int getSize() const { + return cyclic_buffer::getSize(); + } + const volatile T* getElements() const { return elements; } diff --git a/unit_tests/tests/test_can_serial.cpp b/unit_tests/tests/test_can_serial.cpp index d03c298ab1..f6a68c6ffc 100644 --- a/unit_tests/tests/test_can_serial.cpp +++ b/unit_tests/tests/test_can_serial.cpp @@ -28,6 +28,8 @@ public: } virtual can_msg_t receive(canmbx_t mailbox, CANRxFrame *crfp, can_sysinterval_t timeout) override { + if (crfList.empty()) + return CAN_MSG_TIMEOUT; *crfp = *crfList.begin(); crfList.pop_front(); return CAN_MSG_OK; @@ -51,6 +53,8 @@ public: TestCanStreamerState() : CanStreamerState(&streamer) {} void test(const std::vector & dataList, const std::vector & frames, int fifoLeftoverSize, const std::vector & receiveChunks) { + EngineTestHelper eth(TEST_ENGINE); + size_t totalSize = 0; std::string totalData; for (auto data : dataList) { @@ -110,37 +114,37 @@ protected: TestCanStreamer streamer; }; + TEST(testCanSerial, test1Frame) { - /* + { TestCanStreamerState state; state.test({ "1" }, { "\x01"s "1\0\0\0\0\0\0"s }, 1, { 1 }); // 1 byte -> 1 frame, 1 byte in FIFO } { TestCanStreamerState state; - state.test({ "0123456" }, { "\x07"s "0123456"s }, 0, { 7 }); // 7 bytes -> 1 8-byte frame + state.test({ "0123456" }, { "\x07"s "0123456"s }, 7, { 7 }); // 7 bytes -> 1 8-byte frame } { TestCanStreamerState state; - state.test({ "0123456" }, { "\x07"s "0123456"s }, 0, { 1, 1, 1, 1, 1, 1, 1 }); // 7 bytes -> 1 8-byte frame, split receive test + state.test({ "0123456" }, { "\x07"s "0123456"s }, 7, { 1, 1, 1, 1, 1, 1, 1 }); // 7 bytes -> 1 8-byte frame, split receive test } { TestCanStreamerState state; - state.test({ "0123456" }, { "\x07"s "0123456"s }, 0, { 3, 4 }); // 7 bytes -> 1 8-byte frame, split receive test + state.test({ "0123456" }, { "\x07"s "0123456"s }, 7, { 3, 4 }); // 7 bytes -> 1 8-byte frame, split receive test } { TestCanStreamerState state; - state.test({ "0", "1", "2", "3", "4", "5", "6" }, { "\x07"s "0123456"s }, 0, { 7 }); // 7 bytes separately -> 1 8-byte frame + state.test({ "0", "1", "2", "3", "4", "5", "6" }, { "\x07"s "0123456"s }, 7, { 7 }); // 7 bytes separately -> 1 8-byte frame } -*/ } -/* TEST(testCanSerial, test2Frames) { { TestCanStreamerState state; - state.test({ "01234567" }, { "\x07"s "0123456"s, "\x01"s "7\0\0\0\0\0\0"s }, 1, { 8 }); // 8 bytes -> 2 8-byte frames, 1 byte in FIFO + state.test({ "01234567" }, { "\x10"s "\x08"s "012345"s, "\x21"s "67\0\0\0\0\0"s }, 8, { 8 }); // 8 bytes -> 2 8-byte frames, 8 bytes in FIFO } + /* { TestCanStreamerState state; state.test({ "0123456ABCDEFG" }, { "\x07"s "0123456"s, "\x07"s "ABCDEFG"s }, 0, { 14 }); // 14 bytes -> 2 8-byte frames, empty FIFO @@ -149,8 +153,10 @@ TEST(testCanSerial, test2Frames) { TestCanStreamerState state; state.test({ "0123456ABCDEFG" }, { "\x07"s "0123456"s, "\x07"s "ABCDEFG"s }, 0, { 6, 1, 1, 6 }); // 14 bytes -> 2 8-byte frames, empty FIFO, split receive test } + */ } +/* TEST(testCanSerial, testIrregularSplits) { { TestCanStreamerState state;