添加串口操作类
This commit is contained in:
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);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user