libxr
Embedded C++ framework for bare-metal, RTOS and Linux
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.
 
virtual ~GPIO ()=default
 虚析构函数 / Virtual destructor.
 
ErrorCode RegisterCallback (Callback callback)
 注册 GPIO 事件回调函数 / Registers a callback function for GPIO events.
 

Static Public Attributes

static STM32GPIO * map [16] = {nullptr}
 

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<>
 
- Public Attributes inherited from LibXR::GPIO
Callback callback_
 GPIO 事件的回调函数 / Callback function for GPIO events.
 

Detailed Description

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

Definition at line 61 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}
@ OK
操作成功 / Operation successful

◆ 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 69 of file stm32_gpio.hpp.

69{ 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 87 of file stm32_gpio.hpp.

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

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

Member Data Documentation

◆ map

STM32GPIO * STM32GPIO::map = {nullptr}
static

Definition at line 236 of file stm32_gpio.hpp.


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