From b4ecf03e951ff7c544a2f83f49897da3f227026b Mon Sep 17 00:00:00 2001 From: chauyin Date: Sat, 10 May 2025 18:58:02 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E6=9E=84=20SerialPort=20=E7=B1=BB?= =?UTF-8?q?=EF=BC=8C=E7=A7=BB=E9=99=A4=E9=9D=99=E6=80=81=E6=88=90=E5=91=98?= =?UTF-8?q?=EF=BC=8C=E6=94=B9=E4=B8=BA=E5=AE=9E=E4=BE=8B=E6=88=90=E5=91=98?= =?UTF-8?q?=EF=BC=8C=E4=BC=98=E5=8C=96=20UART=20=E8=AF=BB=E5=86=99?= =?UTF-8?q?=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Core/App/Common/serial_port.cpp | 5 -- Core/App/Common/serial_port.hpp | 108 +++++++++----------------------- Core/App/app.cpp | 1 - 3 files changed, 31 insertions(+), 83 deletions(-) delete mode 100644 Core/App/Common/serial_port.cpp diff --git a/Core/App/Common/serial_port.cpp b/Core/App/Common/serial_port.cpp deleted file mode 100644 index 44c7f3e..0000000 --- a/Core/App/Common/serial_port.cpp +++ /dev/null @@ -1,5 +0,0 @@ -#include "serial_port.hpp" - -extern "C" void HAL_UART_TxCpltCallback([[maybe_unused]] UART_HandleTypeDef* huart) { - SerialPort::isTransmitting = false; -} diff --git a/Core/App/Common/serial_port.hpp b/Core/App/Common/serial_port.hpp index 550ce44..60ab6df 100644 --- a/Core/App/Common/serial_port.hpp +++ b/Core/App/Common/serial_port.hpp @@ -1,43 +1,35 @@ #pragma once -#include "stm32h5xx_nucleo.h" +#include "main.h" #include #include #include +#include #include class SerialPort { private: - static constexpr uint32_t kTransmitTimeout = 1000; - static constexpr auto* kHandle = &hcom_uart[COM1]; - - static inline char buffer[256] = {0}; - - friend void HAL_UART_TxCpltCallback(UART_HandleTypeDef* huart); - static inline volatile bool isTransmitting = false; - - static void WaitTransmit() { - const auto tick = HAL_GetTick(); - while (isTransmitting) { - if (HAL_GetTick() - tick > kTransmitTimeout) { - HAL_UART_AbortTransmit(kHandle); - isTransmitting = false; - } - } - } + UART_HandleTypeDef* handle; + uint32_t timeout; + std::unique_ptr buffer; public: - static uint8_t ReadByte() { - return kHandle->Instance->RDR; + SerialPort(UART_HandleTypeDef* uart, uint32_t length, uint32_t timeout) + : handle(uart), timeout(timeout), buffer(std::make_unique(length)) {} + + ~SerialPort() = default; + + uint8_t ReadByte() { + return handle->Instance->RDR; } - static std::string ReadLine() { + std::string ReadLine() { std::string result; result.reserve(128); char c; ReadByte(); while (true) { - HAL_UART_Receive(kHandle, reinterpret_cast(&c), 1, HAL_MAX_DELAY); + HAL_UART_Receive(handle, reinterpret_cast(&c), sizeof(c), HAL_MAX_DELAY); if (c == '\n') { if (!result.empty() && result.back() == '\r') { result.pop_back(); @@ -49,70 +41,32 @@ public: return result; } - static void WriteLineDMA(const char* format, ...) { - WaitTransmit(); - isTransmitting = true; - va_list args; - va_start(args, format); - int len = vsnprintf(buffer, sizeof(buffer) - 1, format, args); - va_end(args); - if (len < 0 || len > static_cast(sizeof(buffer) - 1)) { - isTransmitting = false; - return; - } - buffer[len] = '\n'; - len++; - HAL_UART_Transmit_DMA(kHandle, reinterpret_cast(buffer), len); + void WriteBytesBlocking(const uint8_t* data, size_t size) { + HAL_UART_Transmit(handle, const_cast(data), size, timeout); } - static void WriteLineBlocking(const char* format, ...) { - WaitTransmit(); + void WriteBlocking(const char* format, ...) { + va_list args; va_start(args, format); - int len = vsnprintf(buffer, sizeof(buffer) - 1, format, args); + auto len = vsnprintf(buffer.get(), sizeof(buffer), format, args); + va_end(args); + if (len < 0 || len > static_cast(sizeof(buffer))) { + return; + } + HAL_UART_Transmit(handle, reinterpret_cast(buffer.get()), strlen(buffer.get()), timeout); + } + + void WriteLineBlocking(const char* format, ...) { + va_list args; + va_start(args, format); + int len = vsnprintf(buffer.get(), sizeof(buffer) - 1, format, args); va_end(args); if (len < 0 || len > static_cast(sizeof(buffer) - 1)) { return; } buffer[len] = '\n'; len++; - HAL_UART_Transmit(kHandle, reinterpret_cast(buffer), len, kTransmitTimeout); - } - - static void WriteDMA(const char* format, ...) { - WaitTransmit(); - isTransmitting = true; - va_list args; - va_start(args, format); - auto len = vsnprintf(buffer, sizeof(buffer), format, args); - va_end(args); - if (len < 0 || len > static_cast(sizeof(buffer))) { - isTransmitting = false; - return; - } - HAL_UART_Transmit_DMA(kHandle, reinterpret_cast(buffer), len); - } - - static void WriteBlocking(const char* format, ...) { - WaitTransmit(); - va_list args; - va_start(args, format); - auto len = vsnprintf(buffer, sizeof(buffer), format, args); - va_end(args); - if (len < 0 || len > static_cast(sizeof(buffer))) { - return; - } - HAL_UART_Transmit(kHandle, reinterpret_cast(buffer), strlen(buffer), kTransmitTimeout); - } - - static void WriteBytesDMA(const uint8_t* data, size_t size) { - WaitTransmit(); - isTransmitting = true; - HAL_UART_Transmit_DMA(kHandle, const_cast(data), size); - } - - static void WriteBytesBlocking(const uint8_t* data, size_t size) { - WaitTransmit(); - HAL_UART_Transmit(kHandle, const_cast(data), size, kTransmitTimeout); + HAL_UART_Transmit(handle, reinterpret_cast(buffer.get()), len, timeout); } }; diff --git a/Core/App/app.cpp b/Core/App/app.cpp index 26ea7b5..37498d6 100644 --- a/Core/App/app.cpp +++ b/Core/App/app.cpp @@ -17,7 +17,6 @@ extern "C" void AppStart() { auto gpio = GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_SET); while (true) { gpio.Toggle(); - SerialPort::WriteBlocking("LED is toggled,current state: %s", gpio.Read() ? "OFF" : "ON"); DelayS(1); } }