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

Public Member Functions

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

Static Public Member Functions

static void CheckInterrupt (uint32_t line)
 

Static Public Attributes

static CH32GPIOmap [16] = {nullptr}
 

Private Member Functions

void ConfigureEXTI (EXTITrigger_TypeDef trigger)
 

Static Private Member Functions

static uint8_t GetEXTIID (uint16_t pin)
 

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 45 of file ch32_gpio.hpp.

Constructor & Destructor Documentation

◆ CH32GPIO()

CH32GPIO::CH32GPIO ( GPIO_TypeDef * port,
uint16_t pin,
GPIO::Direction direction = GPIO::Direction::OUTPUT_PUSH_PULL,
GPIO::Pull pull = GPIO::Pull::NONE,
IRQn_Type irq = NonMaskableInt_IRQn )

Definition at line 85 of file ch32_gpio.cpp.

87 : port_(port), pin_(pin), irq_(irq)
88{
89 if (irq_ != NonMaskableInt_IRQn)
90 {
91 NVIC_EnableIRQ(irq_);
92 map[GetEXTIID(pin)] = this;
93 }
94
95 RCC_APB2PeriphClockCmd(CH32GetGPIOPeriph(port_), ENABLE);
96
97 GPIO_InitTypeDef gpio_init = {};
98 gpio_init.GPIO_Pin = pin_;
99 gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
100
101 switch (direction)
102 {
106 case Direction::INPUT:
107 gpio_init.GPIO_Mode = (pull == Pull::UP) ? GPIO_Mode_IPU
108 : (pull == Pull::DOWN) ? GPIO_Mode_IPD
109 : GPIO_Mode_IN_FLOATING;
110 break;
112 gpio_init.GPIO_Mode = GPIO_Mode_Out_PP;
113 break;
115 gpio_init.GPIO_Mode = GPIO_Mode_Out_OD;
116 break;
117 }
118
119 GPIO_Init(port_, &gpio_init);
120
121 switch (direction)
122 {
124 ConfigureEXTI(EXTI_Trigger_Rising);
125 break;
127 ConfigureEXTI(EXTI_Trigger_Falling);
128 break;
130 ConfigureEXTI(EXTI_Trigger_Rising_Falling);
131 break;
132 default:
133 break;
134 }
135}
@ 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.
Pull
定义 GPIO 引脚的上拉/下拉模式。Defines the pull-up/pull-down configurations for GPIO pins.
Definition gpio.hpp:35
@ DOWN
下拉模式。Pull-down mode.
@ UP
上拉模式。Pull-up mode.

Member Function Documentation

◆ CheckInterrupt()

void CH32GPIO::CheckInterrupt ( uint32_t line)
static

Definition at line 214 of file ch32_gpio.cpp.

215{
216 if (EXTI_GetITStatus(line) != RESET)
217 {
218 EXTI_ClearITPendingBit(line);
219 map[GetEXTIID(line)]->OnInterrupt();
220 }
221}

◆ ConfigureEXTI()

void CH32GPIO::ConfigureEXTI ( EXTITrigger_TypeDef trigger)
private

Definition at line 223 of file ch32_gpio.cpp.

224{
225 EXTI_InitTypeDef exti = {};
226 uint8_t pin_source = __builtin_ctz(pin_);
227 uint8_t port_source = 0xFF;
228
229#if defined(GPIOA)
230 if (port_ == GPIOA) port_source = GPIO_PortSourceGPIOA;
231#endif
232#if defined(GPIOB)
233 else if (port_ == GPIOB)
234 {
235 port_source = GPIO_PortSourceGPIOB;
236 }
237#endif
238#if defined(GPIOC)
239 else if (port_ == GPIOC)
240 {
241 port_source = GPIO_PortSourceGPIOC;
242 }
243#endif
244#if defined(GPIOD)
245 else if (port_ == GPIOD)
246 {
247 port_source = GPIO_PortSourceGPIOD;
248 }
249#endif
250#if defined(GPIOE)
251 else if (port_ == GPIOE)
252 {
253 port_source = GPIO_PortSourceGPIOE;
254 }
255#endif
256#if defined(GPIOF)
257 else if (port_ == GPIOF)
258 {
259 port_source = GPIO_PortSourceGPIOF;
260 }
261#endif
262#if defined(GPIOG)
263 else if (port_ == GPIOG)
264 {
265 port_source = GPIO_PortSourceGPIOG;
266 }
267#endif
268#if defined(GPIOH)
269 else if (port_ == GPIOH)
270 {
271 port_source = GPIO_PortSourceGPIOH;
272 }
273#endif
274#if defined(GPIOI)
275 else if (port_ == GPIOI)
276 {
277 port_source = GPIO_PortSourceGPIOI;
278 }
279#endif
280
281 ASSERT(port_source != 0xFF);
282
283 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
284 GPIO_EXTILineConfig(port_source, pin_source);
285
286 exti.EXTI_Line = 1 << pin_source;
287 exti.EXTI_Mode = EXTI_Mode_Interrupt;
288 exti.EXTI_Trigger = trigger;
289 exti.EXTI_LineCmd = ENABLE;
290 EXTI_Init(&exti);
291
292 NVIC_EnableIRQ(irq_);
293}

◆ DisableInterrupt()

ErrorCode CH32GPIO::DisableInterrupt ( )
overridevirtual

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

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

Implements LibXR::GPIO.

Definition at line 154 of file ch32_gpio.cpp.

155{
156 EXTI->INTENR &= ~(1 << GetEXTIID(pin_));
157 return ErrorCode::OK;
158}

◆ EnableInterrupt()

ErrorCode CH32GPIO::EnableInterrupt ( )
overridevirtual

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

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

Implements LibXR::GPIO.

Definition at line 148 of file ch32_gpio.cpp.

149{
150 EXTI->INTENR |= (1 << GetEXTIID(pin_));
151 return ErrorCode::OK;
152}

◆ GetEXTIID()

uint8_t CH32GPIO::GetEXTIID ( uint16_t pin)
staticprivate

Definition at line 295 of file ch32_gpio.cpp.

295{ return __builtin_ctz(pin); }

◆ OnInterrupt()

void CH32GPIO::OnInterrupt ( )

Definition at line 206 of file ch32_gpio.cpp.

207{
208 if (!callback_.Empty())
209 {
210 callback_.Run(true);
211 }
212}
void Run(bool in_isr, PassArgs &&...args) const
执行回调函数,并传递参数。 Executes the callback function, passing the arguments.
Definition libxr_cb.hpp:207
bool Empty() const
检查回调是否为空。 Checks if the callback is empty.
Definition libxr_cb.hpp:222
Callback callback_
GPIO 事件的回调函数。Callback function for GPIO events.
Definition gpio.hpp:56

◆ Read()

bool CH32GPIO::Read ( )
overridevirtual

读取 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 137 of file ch32_gpio.cpp.

137{ return GPIO_ReadInputDataBit(port_, pin_) == Bit_SET; }

◆ SetConfig()

ErrorCode CH32GPIO::SetConfig ( Configuration config)
overridevirtual

配置 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 160 of file ch32_gpio.cpp.

161{
162 GPIO_InitTypeDef gpio_init = {};
163 gpio_init.GPIO_Pin = pin_;
164 gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
165
166 switch (config.direction)
167 {
168 case Direction::INPUT:
169 gpio_init.GPIO_Mode = (config.pull == Pull::UP) ? GPIO_Mode_IPU
170 : (config.pull == Pull::DOWN) ? GPIO_Mode_IPD
171 : GPIO_Mode_IN_FLOATING;
172 break;
174 gpio_init.GPIO_Mode = GPIO_Mode_Out_PP;
175 break;
177 gpio_init.GPIO_Mode = GPIO_Mode_Out_OD;
178 break;
182 gpio_init.GPIO_Mode = GPIO_Mode_IN_FLOATING;
183 break;
184 }
185
186 GPIO_Init(port_, &gpio_init);
187
188 switch (config.direction)
189 {
191 ConfigureEXTI(EXTI_Trigger_Rising);
192 break;
194 ConfigureEXTI(EXTI_Trigger_Falling);
195 break;
197 ConfigureEXTI(EXTI_Trigger_Rising_Falling);
198 break;
199 default:
200 break;
201 }
202
203 return ErrorCode::OK;
204}

◆ Write()

ErrorCode CH32GPIO::Write ( bool value)
overridevirtual

写入 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 139 of file ch32_gpio.cpp.

140{
141 if (value)
142 GPIO_SetBits(port_, pin_);
143 else
144 GPIO_ResetBits(port_, pin_);
145 return ErrorCode::OK;
146}

Field Documentation

◆ irq_

IRQn_Type LibXR::CH32GPIO::irq_
private

Definition at line 71 of file ch32_gpio.hpp.

◆ map

CH32GPIO* LibXR::CH32GPIO::map[16] = {nullptr}
inlinestatic

Definition at line 66 of file ch32_gpio.hpp.

66{nullptr};

◆ pin_

uint16_t LibXR::CH32GPIO::pin_
private

Definition at line 70 of file ch32_gpio.hpp.

◆ port_

GPIO_TypeDef* LibXR::CH32GPIO::port_
private

Definition at line 69 of file ch32_gpio.hpp.


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