feat: 新增LED的CPP支持,优化LED的C模块结构,新增interface
This commit is contained in:
3
Core/driver/base/gpio/CMakeLists.txt
Normal file
3
Core/driver/base/gpio/CMakeLists.txt
Normal file
@@ -0,0 +1,3 @@
|
||||
add_library(gpio INTERFACE)
|
||||
target_sources(gpio PUBLIC include/gpio_base.hpp)
|
||||
target_include_directories(gpio INTERFACE ./include)
|
||||
79
Core/driver/base/gpio/include/gpio_base.hpp
Normal file
79
Core/driver/base/gpio/include/gpio_base.hpp
Normal file
@@ -0,0 +1,79 @@
|
||||
//
|
||||
// Created by Wind on 2025/11/9.
|
||||
//
|
||||
|
||||
#ifndef CLION_STM32_GPIO_H
|
||||
#define CLION_STM32_GPIO_H
|
||||
#include "main.h"
|
||||
|
||||
class Gpio {
|
||||
public:
|
||||
enum class Mode {
|
||||
Input,
|
||||
Output,
|
||||
Alternate,
|
||||
Analog
|
||||
};
|
||||
|
||||
enum class PULL {
|
||||
None,
|
||||
PullUp,
|
||||
PullDown,
|
||||
};
|
||||
|
||||
Gpio(GPIO_TypeDef *port, const uint16_t pin) : port_(port), pin_(pin) {
|
||||
enableClock();
|
||||
}
|
||||
|
||||
void setMode(const Mode mode, const PULL pull) const {
|
||||
GPIO_InitTypeDef GPIO_InitStruct = {0};
|
||||
GPIO_InitStruct.Pin = pin_;
|
||||
switch (mode) {
|
||||
case Mode::Input: GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
|
||||
break;
|
||||
case Mode::Output: GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||||
break;
|
||||
case Mode::Alternate: GPIO_InitStruct.Mode = GPIO_MODE_AF_PP;
|
||||
break;
|
||||
case Mode::Analog: GPIO_InitStruct.Mode = GPIO_MODE_ANALOG;
|
||||
break;
|
||||
}
|
||||
|
||||
switch (pull) {
|
||||
case PULL::None: GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||||
break;
|
||||
case PULL::PullUp: GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||||
break;
|
||||
case PULL::PullDown: GPIO_InitStruct.Pull = GPIO_PULLDOWN;
|
||||
break;
|
||||
}
|
||||
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
|
||||
HAL_GPIO_Init(port_, &GPIO_InitStruct);
|
||||
}
|
||||
|
||||
void write(const bool v) const { HAL_GPIO_WritePin(port_, pin_, v ? GPIO_PIN_SET : GPIO_PIN_RESET); }
|
||||
|
||||
void toggle() const { HAL_GPIO_TogglePin(port_, pin_); }
|
||||
|
||||
bool read() const { return HAL_GPIO_ReadPin(port_, pin_); }
|
||||
|
||||
private:
|
||||
GPIO_TypeDef *port_{};
|
||||
uint16_t pin_{};
|
||||
|
||||
void enableClock() const {
|
||||
if (port_ == GPIOA)
|
||||
__HAL_RCC_GPIOA_CLK_ENABLE();
|
||||
else if (port_ == GPIOB)
|
||||
__HAL_RCC_GPIOB_CLK_ENABLE();
|
||||
else if (port_ == GPIOC)
|
||||
__HAL_RCC_GPIOC_CLK_ENABLE();
|
||||
else if (port_ == GPIOD)
|
||||
__HAL_RCC_GPIOD_CLK_ENABLE();
|
||||
else if (port_ == GPIOE)
|
||||
__HAL_RCC_GPIOE_CLK_ENABLE();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
#endif //CLION_STM32_GPIO_H
|
||||
33
Core/driver/base/interface.cpp
Normal file
33
Core/driver/base/interface.cpp
Normal file
@@ -0,0 +1,33 @@
|
||||
//
|
||||
// Created by Wind on 2025/11/9.
|
||||
//
|
||||
|
||||
#include "interface.h"
|
||||
#include "led.h"
|
||||
|
||||
#ifdef DRIVER_CPP
|
||||
#include "led_cpp.h"
|
||||
|
||||
void cpp_interface() {
|
||||
const auto led = LED(LED_GPIO_Port, LED_Pin);
|
||||
while (true) {
|
||||
led.toggle();
|
||||
HAL_Delay(50);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
void interface() {
|
||||
#ifdef DRIVER_CPP
|
||||
cpp_interface();
|
||||
#else
|
||||
const led_t led = {
|
||||
LED_GPIO_Port, LED_Pin
|
||||
};
|
||||
led_init(&led);
|
||||
while (true) {
|
||||
led_toggle(&led);
|
||||
HAL_Delay(1000);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
17
Core/driver/base/interface.h
Normal file
17
Core/driver/base/interface.h
Normal file
@@ -0,0 +1,17 @@
|
||||
//
|
||||
// Created by Wind on 2025/11/9.
|
||||
//
|
||||
|
||||
#ifndef CLION_STM32_CPP_INTERFACE_H
|
||||
#define CLION_STM32_CPP_INTERFACE_H
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
void interface();
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif //CLION_STM32_CPP_INTERFACE_H
|
||||
Reference in New Issue
Block a user