generated from Template/H563ZI-HAL-CMake-Template
Initial commit
This commit is contained in:
5
Core/App/Common/serial_port.cpp
Normal file
5
Core/App/Common/serial_port.cpp
Normal file
@@ -0,0 +1,5 @@
|
||||
#include "serial_port.hpp"
|
||||
|
||||
extern "C" void HAL_UART_TxCpltCallback([[maybe_unused]] UART_HandleTypeDef* huart) {
|
||||
SerialPort::isTransmitting = false;
|
||||
}
|
||||
118
Core/App/Common/serial_port.hpp
Normal file
118
Core/App/Common/serial_port.hpp
Normal 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 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);
|
||||
}
|
||||
|
||||
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 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);
|
||||
}
|
||||
};
|
||||
29
Core/App/Helper/delay_helper.c
Normal file
29
Core/App/Helper/delay_helper.c
Normal 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);
|
||||
}
|
||||
}
|
||||
18
Core/App/Helper/delay_helper.h
Normal file
18
Core/App/Helper/delay_helper.h
Normal 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
|
||||
70
Core/App/Helper/gpio_helper.hpp
Normal file
70
Core/App/Helper/gpio_helper.hpp
Normal 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
21
Core/App/app.cpp
Normal 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::WriteBlocking("LED is toggled,current state: %s", gpio.Read() ? "OFF" : "ON");
|
||||
DelayS(1);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user