generated from Template/H563ZI-HAL-CMake-Template
重构 SerialPort 类,移除静态成员,改为实例成员,优化 UART 读写方法
This commit is contained in:
@@ -1,5 +0,0 @@
|
||||
#include "serial_port.hpp"
|
||||
|
||||
extern "C" void HAL_UART_TxCpltCallback([[maybe_unused]] UART_HandleTypeDef* huart) {
|
||||
SerialPort::isTransmitting = false;
|
||||
}
|
||||
@@ -1,43 +1,35 @@
|
||||
#pragma once
|
||||
|
||||
#include "stm32h5xx_nucleo.h"
|
||||
#include "main.h"
|
||||
#include <cstdarg>
|
||||
#include <cstdint>
|
||||
#include <cstring>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
|
||||
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<char[]> 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<char[]>(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<uint8_t*>(&c), 1, HAL_MAX_DELAY);
|
||||
HAL_UART_Receive(handle, reinterpret_cast<uint8_t*>(&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<int>(sizeof(buffer) - 1)) {
|
||||
isTransmitting = false;
|
||||
return;
|
||||
}
|
||||
buffer[len] = '\n';
|
||||
len++;
|
||||
HAL_UART_Transmit_DMA(kHandle, reinterpret_cast<uint8_t*>(buffer), len);
|
||||
void WriteBytesBlocking(const uint8_t* data, size_t size) {
|
||||
HAL_UART_Transmit(handle, const_cast<uint8_t*>(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<int>(sizeof(buffer))) {
|
||||
return;
|
||||
}
|
||||
HAL_UART_Transmit(handle, reinterpret_cast<uint8_t*>(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<int>(sizeof(buffer) - 1)) {
|
||||
return;
|
||||
}
|
||||
buffer[len] = '\n';
|
||||
len++;
|
||||
HAL_UART_Transmit(kHandle, reinterpret_cast<uint8_t*>(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<int>(sizeof(buffer))) {
|
||||
isTransmitting = false;
|
||||
return;
|
||||
}
|
||||
HAL_UART_Transmit_DMA(kHandle, reinterpret_cast<uint8_t*>(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<int>(sizeof(buffer))) {
|
||||
return;
|
||||
}
|
||||
HAL_UART_Transmit(kHandle, reinterpret_cast<uint8_t*>(buffer), strlen(buffer), kTransmitTimeout);
|
||||
}
|
||||
|
||||
static void WriteBytesDMA(const uint8_t* data, size_t size) {
|
||||
WaitTransmit();
|
||||
isTransmitting = true;
|
||||
HAL_UART_Transmit_DMA(kHandle, const_cast<uint8_t*>(data), size);
|
||||
}
|
||||
|
||||
static void WriteBytesBlocking(const uint8_t* data, size_t size) {
|
||||
WaitTransmit();
|
||||
HAL_UART_Transmit(kHandle, const_cast<uint8_t*>(data), size, kTransmitTimeout);
|
||||
HAL_UART_Transmit(handle, reinterpret_cast<uint8_t*>(buffer.get()), len, timeout);
|
||||
}
|
||||
};
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user