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
|
#pragma once
|
||||||
|
|
||||||
#include "stm32h5xx_nucleo.h"
|
#include "main.h"
|
||||||
#include <cstdarg>
|
#include <cstdarg>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
|
||||||
class SerialPort {
|
class SerialPort {
|
||||||
private:
|
private:
|
||||||
static constexpr uint32_t kTransmitTimeout = 1000;
|
UART_HandleTypeDef* handle;
|
||||||
static constexpr auto* kHandle = &hcom_uart[COM1];
|
uint32_t timeout;
|
||||||
|
std::unique_ptr<char[]> buffer;
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public:
|
public:
|
||||||
static uint8_t ReadByte() {
|
SerialPort(UART_HandleTypeDef* uart, uint32_t length, uint32_t timeout)
|
||||||
return kHandle->Instance->RDR;
|
: 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;
|
std::string result;
|
||||||
result.reserve(128);
|
result.reserve(128);
|
||||||
char c;
|
char c;
|
||||||
ReadByte();
|
ReadByte();
|
||||||
while (true) {
|
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 (c == '\n') {
|
||||||
if (!result.empty() && result.back() == '\r') {
|
if (!result.empty() && result.back() == '\r') {
|
||||||
result.pop_back();
|
result.pop_back();
|
||||||
@@ -49,70 +41,32 @@ public:
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteLineDMA(const char* format, ...) {
|
void WriteBytesBlocking(const uint8_t* data, size_t size) {
|
||||||
WaitTransmit();
|
HAL_UART_Transmit(handle, const_cast<uint8_t*>(data), size, timeout);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static void WriteLineBlocking(const char* format, ...) {
|
void WriteBlocking(const char* format, ...) {
|
||||||
WaitTransmit();
|
|
||||||
va_list args;
|
va_list args;
|
||||||
va_start(args, format);
|
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);
|
va_end(args);
|
||||||
if (len < 0 || len > static_cast<int>(sizeof(buffer) - 1)) {
|
if (len < 0 || len > static_cast<int>(sizeof(buffer) - 1)) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
buffer[len] = '\n';
|
buffer[len] = '\n';
|
||||||
len++;
|
len++;
|
||||||
HAL_UART_Transmit(kHandle, reinterpret_cast<uint8_t*>(buffer), len, kTransmitTimeout);
|
HAL_UART_Transmit(handle, reinterpret_cast<uint8_t*>(buffer.get()), len, timeout);
|
||||||
}
|
|
||||||
|
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -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);
|
auto gpio = GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_SET);
|
||||||
while (true) {
|
while (true) {
|
||||||
gpio.Toggle();
|
gpio.Toggle();
|
||||||
SerialPort::WriteBlocking("LED is toggled,current state: %s", gpio.Read() ? "OFF" : "ON");
|
|
||||||
DelayS(1);
|
DelayS(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user