libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32GPIO Class Reference
Inheritance diagram for LibXR::STM32GPIO:
[legend]
Collaboration diagram for LibXR::STM32GPIO:
[legend]

Public Member Functions

 STM32GPIO (GPIO_TypeDef *port, uint16_t pin, IRQn_Type irq=NonMaskableInt_IRQn)
 
bool Read ()
 读取 GPIO 引脚状态。Reads the GPIO pin state.
 
ErrorCode Write (bool value)
 写入 GPIO 引脚状态。Writes the GPIO pin state.
 
ErrorCode EnableInterrupt ()
 使能 GPIO 引脚中断。Enables the GPIO pin interrupt.
 
ErrorCode DisableInterrupt ()
 禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.
 
ErrorCode SetConfig (Configuration config)
 配置 GPIO 引脚参数。Configures the GPIO pin settings.
 
- Public Member Functions inherited from LibXR::GPIO
 GPIO ()
 默认构造函数。Default constructor.
 
ErrorCode RegisterCallback (Callback callback)
 注册 GPIO 事件回调函数。Registers a callback function for GPIO events.
 

Static Public Attributes

static STM32GPIOmap [STM32_GPIO_EXTI_NUMBER] = {nullptr}
 

Private Attributes

GPIO_TypeDef * port_
 
uint16_t pin_
 
IRQn_Type irq_
 

Additional Inherited Members

- Public Types inherited from LibXR::GPIO
enum class  Direction : uint8_t {
  INPUT , OUTPUT_PUSH_PULL , OUTPUT_OPEN_DRAIN , FALL_INTERRUPT ,
  RISING_INTERRUPT , FALL_RISING_INTERRUPT
}
 定义 GPIO 引脚的方向类型。Defines the direction types for GPIO pins. More...
 
enum class  Pull : uint8_t { NONE , UP , DOWN }
 定义 GPIO 引脚的上拉/下拉模式。Defines the pull-up/pull-down configurations for GPIO pins. More...
 
using Callback = LibXR::Callback<>
 
- Data Fields inherited from LibXR::GPIO
Callback callback_
 GPIO 事件的回调函数。Callback function for GPIO events.
 

Detailed Description

Definition at line 33 of file stm32_gpio.hpp.

Constructor & Destructor Documentation

◆ STM32GPIO()

STM32GPIO::STM32GPIO ( GPIO_TypeDef * port,
uint16_t pin,
IRQn_Type irq = NonMaskableInt_IRQn )

Definition at line 58 of file stm32_gpio.cpp.

59 : port_(port), pin_(pin), irq_(irq)
60{
61 if (irq_ != NonMaskableInt_IRQn)
62 {
63 map[STM32_GPIO_EXTI_GetID(pin)] = this;
64 }
65}

Member Function Documentation

◆ DisableInterrupt()

ErrorCode STM32GPIO::DisableInterrupt ( )
virtual

禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.

Returns
操作结果的错误码。Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 82 of file stm32_gpio.cpp.

83{
84 ASSERT(irq_ != NonMaskableInt_IRQn);
85 HAL_NVIC_DisableIRQ(irq_);
86 return ErrorCode::OK;
87}

◆ EnableInterrupt()

ErrorCode STM32GPIO::EnableInterrupt ( )
virtual

使能 GPIO 引脚中断。Enables the GPIO pin interrupt.

Returns
操作结果的错误码。Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 75 of file stm32_gpio.cpp.

76{
77 ASSERT(irq_ != NonMaskableInt_IRQn);
78 HAL_NVIC_EnableIRQ(irq_);
79 return ErrorCode::OK;
80}

◆ Read()

bool STM32GPIO::Read ( )
virtual

读取 GPIO 引脚状态。Reads the GPIO pin state.

Returns
返回引脚状态,true 表示高电平,false 表示低电平。Returns the pin state, true for high, false for low.

Implements LibXR::GPIO.

Definition at line 67 of file stm32_gpio.cpp.

67{ return HAL_GPIO_ReadPin(port_, pin_) == GPIO_PIN_SET; }

◆ SetConfig()

ErrorCode STM32GPIO::SetConfig ( Configuration config)
virtual

配置 GPIO 引脚参数。Configures the GPIO pin settings.

Parameters
config需要应用的 GPIO 配置。The GPIO configuration to apply.
Returns
操作结果的错误码。Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 89 of file stm32_gpio.cpp.

90{
91 GPIO_InitTypeDef gpio_init = {};
92
93 HAL_GPIO_DeInit(port_, pin_);
94
95 gpio_init.Pin = pin_;
96
97 switch (config.direction)
98 {
100 gpio_init.Mode = GPIO_MODE_INPUT;
101 break;
103 gpio_init.Mode = GPIO_MODE_OUTPUT_PP;
104 break;
106 gpio_init.Mode = GPIO_MODE_OUTPUT_OD;
107 break;
109 gpio_init.Mode = GPIO_MODE_IT_FALLING;
110 break;
112 gpio_init.Mode = GPIO_MODE_IT_RISING;
113 break;
115 gpio_init.Mode = GPIO_MODE_IT_RISING_FALLING;
116 break;
117 }
118
119 switch (config.pull)
120 {
121 case Pull::NONE:
122 gpio_init.Pull = GPIO_NOPULL;
123 break;
124 case Pull::UP:
125 gpio_init.Pull = GPIO_PULLUP;
126 break;
127 case Pull::DOWN:
128 gpio_init.Pull = GPIO_PULLDOWN;
129 break;
130 }
131
132 gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
133
134 HAL_GPIO_Init(port_, &gpio_init);
135
136 return ErrorCode::OK;
137}
@ OUTPUT_PUSH_PULL
推挽输出模式。Push-pull output mode.
@ RISING_INTERRUPT
上升沿中断模式。Rising edge interrupt mode.
@ FALL_RISING_INTERRUPT
双沿触发中断模式。Both edge interrupt mode.
@ OUTPUT_OPEN_DRAIN
开漏输出模式。Open-drain output mode.
@ INPUT
输入模式。Input mode.
@ FALL_INTERRUPT
下降沿中断模式。Falling edge interrupt mode.
@ NONE
无上拉或下拉。No pull-up or pull-down.
@ DOWN
下拉模式。Pull-down mode.
@ UP
上拉模式。Pull-up mode.

◆ Write()

ErrorCode STM32GPIO::Write ( bool value)
virtual

写入 GPIO 引脚状态。Writes the GPIO pin state.

Parameters
value要写入的状态,true 表示高电平,false 表示低电平。The value to write, true for high, false for low.
Returns
操作结果的错误码。Error code indicating the result of the operation.

Implements LibXR::GPIO.

Definition at line 69 of file stm32_gpio.cpp.

70{
71 HAL_GPIO_WritePin(port_, pin_, value ? GPIO_PIN_SET : GPIO_PIN_RESET);
72 return ErrorCode::OK;
73}

Field Documentation

◆ irq_

IRQn_Type LibXR::STM32GPIO::irq_
private

Definition at line 53 of file stm32_gpio.hpp.

◆ map

STM32GPIO * STM32GPIO::map = {nullptr}
static

Definition at line 48 of file stm32_gpio.hpp.

◆ pin_

uint16_t LibXR::STM32GPIO::pin_
private

Definition at line 52 of file stm32_gpio.hpp.

◆ port_

GPIO_TypeDef* LibXR::STM32GPIO::port_
private

Definition at line 51 of file stm32_gpio.hpp.


The documentation for this class was generated from the following files: