libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
LibXR::STM32GPIO Class Referencefinal

STM32 GPIO 驱动实现 / STM32 GPIO driver implementation. More...

#include <stm32_gpio.hpp>

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)
 构造 GPIO 对象 / Construct GPIO object
 
bool Read ()
 读取 GPIO 引脚状态。Reads the GPIO pin state.
 
void 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 [16] = {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

STM32 GPIO 驱动实现 / STM32 GPIO driver implementation.

Definition at line 59 of file stm32_gpio.hpp.

Constructor & Destructor Documentation

◆ STM32GPIO()

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

构造 GPIO 对象 / Construct GPIO object

Definition at line 17 of file stm32_gpio.cpp.

18 : port_(port), pin_(pin), irq_(irq)
19{
20 if (irq_ != NonMaskableInt_IRQn)
21 {
22 map[STM32_GPIO_PinToLine(pin)] = this;
23 }
24}

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 33 of file stm32_gpio.cpp.

34{
35 ASSERT(irq_ != NonMaskableInt_IRQn);
36 HAL_NVIC_DisableIRQ(irq_);
37 return ErrorCode::OK;
38}

◆ 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 26 of file stm32_gpio.cpp.

27{
28 ASSERT(irq_ != NonMaskableInt_IRQn);
29 HAL_NVIC_EnableIRQ(irq_);
30 return ErrorCode::OK;
31}

◆ Read()

bool LibXR::STM32GPIO::Read ( )
inlinevirtual

读取 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.hpp.

67{ return (port_->IDR & pin_) != 0u; }

◆ SetConfig()

ErrorCode LibXR::STM32GPIO::SetConfig ( Configuration config)
inlinevirtual

配置 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 85 of file stm32_gpio.hpp.

86 {
87 const bool IS_NON_IRQ_DIRECTION =
88 static_cast<uint8_t>(config.direction) <=
90
91 if (IS_NON_IRQ_DIRECTION)
92 {
93 const uint32_t PIN_POS = static_cast<uint32_t>(__builtin_ctz(pin_));
94
95#if XR_STM32_GPIO_HAS_F1_LAYOUT
96 // 寄存器快路径(F1 类):CRL/CRH 格式 / Register fast path (F1 class): CRL/CRH.
97 volatile uint32_t* cr = (PIN_POS < 8u) ? &port_->CRL : &port_->CRH;
98 const uint32_t SHIFT = (PIN_POS & 0x7u) * 4u;
99 uint32_t mode_cnf = 0u;
100
101 if (config.direction == Direction::INPUT)
102 {
103 if (config.pull == Pull::UP || config.pull == Pull::DOWN)
104 {
105 // MODE=00, CNF=10(上拉/下拉输入)/ Input with pull-up/pull-down.
106 mode_cnf = 0x8u;
107 if (config.pull == Pull::UP)
108 {
109 port_->BSRR = static_cast<uint32_t>(pin_);
110 }
111 else
112 {
113 port_->BSRR = static_cast<uint32_t>(pin_) << 16u;
114 }
115 }
116 else
117 {
118 // MODE=00, CNF=01(浮空输入)/ Floating input.
119 mode_cnf = 0x4u;
120 }
121 }
122 else if (config.direction == Direction::OUTPUT_PUSH_PULL)
123 {
124 // MODE=11(50MHz), CNF=00(推挽输出)/ General purpose output push-pull.
125 mode_cnf = 0x3u;
126 }
127 else
128 {
129 // MODE=11(50MHz), CNF=01(开漏输出)/ General purpose output open-drain.
130 mode_cnf = 0x7u;
131 }
132
133 uint32_t reg = *cr;
134 reg &= ~(0xFu << SHIFT);
135 reg |= (mode_cnf << SHIFT);
136 *cr = reg;
137 return ErrorCode::OK;
138
139#elif XR_STM32_GPIO_HAS_MODER_LAYOUT
140 // 寄存器快路径(MODER 类):MODER/OTYPER/PUPDR/OSPEEDR / Register fast path (MODER
141 // class).
142 const uint32_t SHIFT = PIN_POS * 2u;
143 const uint32_t MASK2 = 0x3u << SHIFT;
144
145 uint32_t moder = port_->MODER;
146 moder &= ~MASK2;
147 if (config.direction != Direction::INPUT)
148 {
149 moder |= (0x1u << SHIFT); // MODER=01:通用输出 / General purpose output.
150 }
151 port_->MODER = moder;
152
153 if (config.direction != Direction::INPUT)
154 {
155 const uint32_t PIN_MASK = 0x1u << PIN_POS;
156 if (config.direction == Direction::OUTPUT_OPEN_DRAIN)
157 {
158 port_->OTYPER |= PIN_MASK;
159 }
160 else
161 {
162 port_->OTYPER &= ~PIN_MASK;
163 }
164
165#if XR_STM32_GPIO_HAS_OSPEEDR
166 uint32_t ospeedr = port_->OSPEEDR;
167 ospeedr &= ~MASK2;
168 ospeedr |= (0x3u << SHIFT); // SWD 方向切换用最高速率 / Fastest slew for SWD.
169 port_->OSPEEDR = ospeedr;
170#endif
171 }
172
173 uint32_t pupdr = port_->PUPDR;
174 pupdr &= ~MASK2;
175 if (config.pull == Pull::UP)
176 {
177 pupdr |= (0x1u << SHIFT);
178 }
179 else if (config.pull == Pull::DOWN)
180 {
181 pupdr |= (0x2u << SHIFT);
182 }
183 port_->PUPDR = pupdr;
184 return ErrorCode::OK;
185#endif
186 }
187
188 GPIO_InitTypeDef gpio_init = {};
189
190 gpio_init.Pin = pin_;
191
192 switch (config.direction)
193 {
194 case Direction::INPUT:
195 gpio_init.Mode = GPIO_MODE_INPUT;
196 break;
198 gpio_init.Mode = GPIO_MODE_OUTPUT_PP;
199 break;
201 gpio_init.Mode = GPIO_MODE_OUTPUT_OD;
202 break;
204 gpio_init.Mode = GPIO_MODE_IT_FALLING;
205 break;
207 gpio_init.Mode = GPIO_MODE_IT_RISING;
208 break;
210 gpio_init.Mode = GPIO_MODE_IT_RISING_FALLING;
211 break;
212 }
213
214 switch (config.pull)
215 {
216 case Pull::NONE:
217 gpio_init.Pull = GPIO_NOPULL;
218 break;
219 case Pull::UP:
220 gpio_init.Pull = GPIO_PULLUP;
221 break;
222 case Pull::DOWN:
223 gpio_init.Pull = GPIO_PULLDOWN;
224 break;
225 }
226
227 gpio_init.Speed = GPIO_SPEED_FREQ_HIGH;
228
229 HAL_GPIO_Init(port_, &gpio_init);
230
231 return ErrorCode::OK;
232 }
@ 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()

void LibXR::STM32GPIO::Write ( bool value)
inlinevirtual

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

Parameters
value要写入的状态,true 表示高电平,false 表示低电平。The value to write, true for high, false for low.

Implements LibXR::GPIO.

Definition at line 69 of file stm32_gpio.hpp.

70 {
71 if (value)
72 {
73 port_->BSRR = static_cast<uint32_t>(pin_);
74 }
75 else
76 {
77 port_->BSRR = static_cast<uint32_t>(pin_) << 16;
78 }
79 }

Field Documentation

◆ irq_

IRQn_Type LibXR::STM32GPIO::irq_
private

Definition at line 239 of file stm32_gpio.hpp.

◆ map

STM32GPIO * STM32GPIO::map = {nullptr}
static

Definition at line 234 of file stm32_gpio.hpp.

◆ pin_

uint16_t LibXR::STM32GPIO::pin_
private

Definition at line 238 of file stm32_gpio.hpp.

◆ port_

GPIO_TypeDef* LibXR::STM32GPIO::port_
private

Definition at line 237 of file stm32_gpio.hpp.


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