添加串口操作类

This commit is contained in:
2025-05-06 22:34:51 +08:00
parent 23c70f1c39
commit 3a495e06ca
18 changed files with 22698 additions and 30 deletions

View File

@@ -0,0 +1,5 @@
#include "serial_port.hpp"
extern "C" void HAL_UART_TxCpltCallback([[maybe_unused]] UART_HandleTypeDef* huart) {
SerialPort::isTransmitting = false;
}

View File

@@ -0,0 +1,118 @@
#pragma once
#include "stm32h5xx_nucleo.h"
#include <cstdarg>
#include <cstdint>
#include <cstring>
#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;
}
}
}
public:
static uint8_t ReadByte() {
return kHandle->Instance->RDR;
}
static 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);
if (c == '\n') {
if (!result.empty() && result.back() == '\r') {
result.pop_back();
}
break;
}
result.push_back(c);
}
return result;
}
static void WriteLine(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);
}
static void WriteLineBlocking(const char* format, ...) {
WaitTransmit();
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)) {
return;
}
buffer[len] = '\n';
len++;
HAL_UART_Transmit(kHandle, reinterpret_cast<uint8_t*>(buffer), len, kTransmitTimeout);
}
static void Write(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 WriteBytes(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);
}
};

View File

@@ -0,0 +1,29 @@
//
// Created by Engineer on 2024/1/11.
//
#include "delay_helper.h"
#include "tim.h"
void DelaySetup(void) {
HAL_TIM_Base_Start(&TIM_DELAY);
}
void DelayUs(const uint16_t nus) {
__HAL_TIM_SET_COUNTER(&TIM_DELAY, 0);
while (__HAL_TIM_GET_COUNTER(&TIM_DELAY) < nus) {
}
}
void DelayMs(const uint32_t nms) {
for (uint32_t i = 0; i < nms; ++i) {
DelayUs(1000);
}
}
void DelayS(const uint32_t ns) {
for (uint32_t i = 0; i < ns; ++i) {
DelayMs(1000);
}
}

View File

@@ -0,0 +1,18 @@
#pragma once
#include "main.h"
#ifdef __cplusplus
extern "C" {
#endif
#define TIM_DELAY htim7
void DelaySetup(void);
void DelayUs(uint16_t nus);
void DelayMs(uint32_t nms);
void DelayS(uint32_t ns);
#ifdef __cplusplus
}
#endif

View File

@@ -0,0 +1,70 @@
#pragma once
#include "main.h"
#include <optional>
#include <string_view>
class GpioHelper {
public:
struct Gpio {
GPIO_TypeDef* port;
uint16_t pin;
void Set() const {
port->BSRR = pin;
}
void Reset() const {
port->BSRR = pin << 16;
}
void Toggle() const {
port->ODR ^= pin;
}
[[nodiscard]] GPIO_PinState Read() const {
return port->IDR & pin ? GPIO_PIN_SET : GPIO_PIN_RESET;
}
};
struct GpioPlus {
std::string_view name;
Gpio gpio;
};
static void EnableAllGpioPeripheral() {
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOE_CLK_ENABLE();
__HAL_RCC_GPIOF_CLK_ENABLE();
__HAL_RCC_GPIOG_CLK_ENABLE();
}
static Gpio GpioInit(GPIO_TypeDef* port, const uint16_t pin, const uint32_t mode, const uint32_t pull,
const std::optional<GPIO_PinState> state = std::nullopt) {
static GPIO_InitTypeDef o = {};
if (state.has_value()) {
HAL_GPIO_WritePin(port, pin, state.value());
}
o.Pin = pin;
o.Mode = mode;
o.Pull = pull;
o.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(port, &o);
return {port, pin};
}
static Gpio GpioInit(const Gpio& gpio, const uint32_t mode, const uint32_t pull, const GPIO_PinState state) {
return GpioInit(gpio.port, gpio.pin, mode, pull, state);
}
static void GpioDeInit(const Gpio& gpio) {
HAL_GPIO_DeInit(gpio.port, gpio.pin);
}
static void GpioDeInit(GPIO_TypeDef* port, const uint16_t pin) {
HAL_GPIO_DeInit(port, pin);
}
};

21
Core/App/app.cpp Normal file
View File

@@ -0,0 +1,21 @@
#include "Helper/delay_helper.h"
#include "stm32h563xx.h"
#include "Common/serial_port.hpp"
#include "Helper/gpio_helper.hpp"
void Setup() {
GpioHelper::EnableAllGpioPeripheral();
DelaySetup();
}
extern "C" void AppStart() {
Setup();
auto gpio = GpioHelper::GpioInit(GPIOB, GPIO_PIN_0, GPIO_MODE_OUTPUT_PP, GPIO_NOPULL, GPIO_PIN_SET);
while (true) {
gpio.Toggle();
SerialPort::WriteLine("LED is toggled,current state: %s", gpio.Read() ? "OFF" : "ON");
DelayS(1);
}
}

View File

@@ -78,7 +78,7 @@
/*#define HAL_SMBUS_MODULE_ENABLED */
/*#define HAL_SPI_MODULE_ENABLED */
/*#define HAL_SRAM_MODULE_ENABLED */
/*#define HAL_TIM_MODULE_ENABLED */
#define HAL_TIM_MODULE_ENABLED
/*#define HAL_RAMCFG_MODULE_ENABLED */
#define HAL_UART_MODULE_ENABLED
/*#define HAL_USART_MODULE_ENABLED */

52
Core/Inc/tim.h Normal file
View File

@@ -0,0 +1,52 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file tim.h
* @brief This file contains all the function prototypes for
* the tim.c file
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Define to prevent recursive inclusion -------------------------------------*/
#ifndef __TIM_H__
#define __TIM_H__
#ifdef __cplusplus
extern "C" {
#endif
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
extern TIM_HandleTypeDef htim7;
/* USER CODE BEGIN Private defines */
/* USER CODE END Private defines */
void MX_TIM7_Init(void);
/* USER CODE BEGIN Prototypes */
/* USER CODE END Prototypes */
#ifdef __cplusplus
}
#endif
#endif /* __TIM_H__ */

View File

@@ -19,6 +19,7 @@
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "memorymap.h"
#include "tim.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
@@ -57,7 +58,7 @@ void SystemClock_Config(void);
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
extern void AppStart(void);
/* USER CODE END 0 */
/**
@@ -89,6 +90,7 @@ int main(void)
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM7_Init();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
@@ -114,6 +116,7 @@ int main(void)
/* Infinite loop */
/* USER CODE BEGIN WHILE */
AppStart();
while (1)
{

97
Core/Src/tim.c Normal file
View File

@@ -0,0 +1,97 @@
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file tim.c
* @brief This file provides code for the configuration
* of the TIM instances.
******************************************************************************
* @attention
*
* Copyright (c) 2025 STMicroelectronics.
* All rights reserved.
*
* This software is licensed under terms that can be found in the LICENSE file
* in the root directory of this software component.
* If no LICENSE file comes with this software, it is provided AS-IS.
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "tim.h"
/* USER CODE BEGIN 0 */
/* USER CODE END 0 */
TIM_HandleTypeDef htim7;
/* TIM7 init function */
void MX_TIM7_Init(void)
{
/* USER CODE BEGIN TIM7_Init 0 */
/* USER CODE END TIM7_Init 0 */
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM7_Init 1 */
/* USER CODE END TIM7_Init 1 */
htim7.Instance = TIM7;
htim7.Init.Prescaler = 249;
htim7.Init.CounterMode = TIM_COUNTERMODE_UP;
htim7.Init.Period = 65535;
htim7.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(&htim7) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(&htim7, &sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM7_Init 2 */
/* USER CODE END TIM7_Init 2 */
}
void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM7)
{
/* USER CODE BEGIN TIM7_MspInit 0 */
/* USER CODE END TIM7_MspInit 0 */
/* TIM7 clock enable */
__HAL_RCC_TIM7_CLK_ENABLE();
/* USER CODE BEGIN TIM7_MspInit 1 */
/* USER CODE END TIM7_MspInit 1 */
}
}
void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* tim_baseHandle)
{
if(tim_baseHandle->Instance==TIM7)
{
/* USER CODE BEGIN TIM7_MspDeInit 0 */
/* USER CODE END TIM7_MspDeInit 0 */
/* Peripheral clock disable */
__HAL_RCC_TIM7_CLK_DISABLE();
/* USER CODE BEGIN TIM7_MspDeInit 1 */
/* USER CODE END TIM7_MspDeInit 1 */
}
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */