From 05adf10e452d94b41d28dde26af4b56dafe1f790 Mon Sep 17 00:00:00 2001 From: GHOSCHT <31184695+GHOSCHT@users.noreply.github.com> Date: Thu, 30 Mar 2023 17:53:27 +0200 Subject: [PATCH] Make receiver work Switch from mutex to bin-semaphore (thread overarching) & Proper failure handling --- .../src/Communication/Serial/SerialReceiver.h | 43 +++++++++++++------ .../control/Firmware/src/Messages/Message.h | 5 ++- 2 files changed, 32 insertions(+), 16 deletions(-) diff --git a/modules/control/Firmware/src/Communication/Serial/SerialReceiver.h b/modules/control/Firmware/src/Communication/Serial/SerialReceiver.h index ae5b224..457e59f 100644 --- a/modules/control/Firmware/src/Communication/Serial/SerialReceiver.h +++ b/modules/control/Firmware/src/Communication/Serial/SerialReceiver.h @@ -16,7 +16,12 @@ public: UBaseType_t priority, BaseType_t coreID) : port{port}, messageQueue{messageQueue}, serialReceiverTask{ - exec, "receive serial data", stackDepth, this, priority, coreID} {} + exec, "receive serial data", stackDepth, this, priority, coreID} { + for (size_t i = 0; i < bufferSize; ++i) { + message::Message &msg = buffer.pop(); + msg.second.give(); + } + } private: static void exec(void *receiverPtr) { @@ -28,22 +33,32 @@ private: receiver->magicNumberBuf.clear(); receiver->magicNumberBuf.resize(receiver->magicNumber.length()); size_t availableBytes = receiver->port.available(); - if (availableBytes >= receiver->magicNumber.length() + 1) { + if (receiver->port.available() > 0 && receiver->port.peek() != 'H') { + receiver->port.read(); + receiver->port.println("flush"); + } else if (availableBytes >= receiver->magicNumber.length() + 1) { receiver->port.readBytes(receiver->magicNumberBuf.data(), receiver->magicNumber.length()); - receiver->port.println(receiver->magicNumberBuf == - receiver->magicNumber); + receiver->port.println("Received Magic Number:"); + receiver->port.println(receiver->magicNumberBuf == receiver->magicNumber + ? "correct" + : "incorrect"); - uint8_t size; - receiver->port.readBytes(&size, sizeof(size)); - if (receiver->port.available() >= size) { - message::Message &msg = receiver->buffer.pop(); - msg.second.lock(); - msg.first.clear(); - msg.first.resize(size); - receiver->port.readBytes(msg.first.data(), size); - msg.second.unlock(); - receiver->messageQueue.push(&msg); + if (receiver->magicNumberBuf == receiver->magicNumber) { + uint8_t size; + receiver->port.readBytes(&size, sizeof(size)); + if (receiver->port.available() >= size) { + message::Message &msg = receiver->buffer.pop(); + receiver->port.printf("Current semaphore count: %u\n", + msg.second.getCount()); + msg.second.take(); + msg.first.clear(); + msg.first.resize(size); + receiver->port.readBytes(msg.first.data(), size); + // msg.second.unlock(); + receiver->port.println("Push message to queue"); + receiver->messageQueue.push(&msg); + } } } } diff --git a/modules/control/Firmware/src/Messages/Message.h b/modules/control/Firmware/src/Messages/Message.h index 7987868..af77ffc 100644 --- a/modules/control/Firmware/src/Messages/Message.h +++ b/modules/control/Firmware/src/Messages/Message.h @@ -1,12 +1,13 @@ #pragma once #include -#include +#include #include #include #include namespace message { +/// @brief first: data, second: data ready to reuse using Message = etl::pair, - freertos::Mutex>; + freertos::BinarySemaphore>; } // namespace message