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

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

#include <ch32_gpio.hpp>

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)
 构造 GPIO 对象 / Construct GPIO object
 
bool Read () override
 读取 GPIO 引脚状态。Reads the GPIO pin state.
 
void 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}
 EXTI 线路映射表 / EXTI line map.
 

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

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

Definition at line 48 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 )

构造 GPIO 对象 / Construct GPIO object

Definition at line 94 of file ch32_gpio.cpp.

96 : port_(port), pin_(pin), irq_(irq)
97{
98 if (irq_ != NonMaskableInt_IRQn)
99 {
100 map_[GetEXTIID(pin)] = this;
101 }
102
103 RCC_APB2PeriphClockCmd(ch32_get_gpio_periph(port_), ENABLE);
104
105 GPIO_InitTypeDef gpio_init = {};
106 gpio_init.GPIO_Pin = pin_;
107 gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
108
109 switch (direction)
110 {
114 case Direction::INPUT:
115 gpio_init.GPIO_Mode = (pull == Pull::UP) ? GPIO_Mode_IPU
116 : (pull == Pull::DOWN) ? GPIO_Mode_IPD
117 : GPIO_Mode_IN_FLOATING;
118 break;
120 gpio_init.GPIO_Mode = GPIO_Mode_Out_PP;
121 break;
123 gpio_init.GPIO_Mode = GPIO_Mode_Out_OD;
124 break;
125 }
126
127 GPIO_Init(port_, &gpio_init);
128
129 switch (direction)
130 {
132 ConfigureEXTI(EXTI_Trigger_Rising);
133 break;
135 ConfigureEXTI(EXTI_Trigger_Falling);
136 break;
138 ConfigureEXTI(EXTI_Trigger_Rising_Falling);
139 break;
140 default:
141 break;
142 }
143}
static CH32GPIO * map_[16]
EXTI 线路映射表 / EXTI line map.
@ 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 165 of file ch32_gpio.cpp.

166{
167 if (EXTI_GetITStatus(line) != RESET)
168 {
169 EXTI_ClearITPendingBit(line);
170
171 const uint8_t ID = GetEXTIID(static_cast<uint16_t>(line));
172 if (auto* gpio = map_[ID])
173 {
174 gpio->OnInterrupt();
175 }
176 }
177}

◆ ConfigureEXTI()

void CH32GPIO::ConfigureEXTI ( EXTITrigger_TypeDef trigger)
private

Definition at line 179 of file ch32_gpio.cpp.

180{
181 EXTI_InitTypeDef exti = {};
182 uint8_t pin_source = GetEXTIID(pin_);
183 uint8_t port_source = 0xFF;
184
185#if defined(GPIOA)
186 if (port_ == GPIOA)
187 {
188 port_source = GPIO_PortSourceGPIOA;
189#endif
190#if defined(GPIOB)
191 }
192 else if (port_ == GPIOB)
193 {
194 port_source = GPIO_PortSourceGPIOB;
195 }
196#endif
197#if defined(GPIOC)
198 else if (port_ == GPIOC)
199 {
200 port_source = GPIO_PortSourceGPIOC;
201 }
202#endif
203#if defined(GPIOD)
204 else if (port_ == GPIOD)
205 {
206 port_source = GPIO_PortSourceGPIOD;
207 }
208#endif
209#if defined(GPIOE)
210 else if (port_ == GPIOE)
211 {
212 port_source = GPIO_PortSourceGPIOE;
213 }
214#endif
215#if defined(GPIOF)
216 else if (port_ == GPIOF)
217 {
218 port_source = GPIO_PortSourceGPIOF;
219 }
220#endif
221#if defined(GPIOG)
222 else if (port_ == GPIOG)
223 {
224 port_source = GPIO_PortSourceGPIOG;
225 }
226#endif
227#if defined(GPIOH)
228 else if (port_ == GPIOH)
229 {
230 port_source = GPIO_PortSourceGPIOH;
231 }
232#endif
233#if defined(GPIOI)
234 else if (port_ == GPIOI)
235 {
236 port_source = GPIO_PortSourceGPIOI;
237 }
238#endif
239
240 ASSERT(port_source != 0xFF);
241
242 RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);
243 GPIO_EXTILineConfig(port_source, pin_source);
244
245 exti.EXTI_Line = 1 << pin_source;
246 exti.EXTI_Mode = EXTI_Mode_Interrupt;
247 exti.EXTI_Trigger = trigger;
248 exti.EXTI_LineCmd = ENABLE;
249 EXTI_Init(&exti);
250
251 NVIC_EnableIRQ(irq_);
252}

◆ 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 151 of file ch32_gpio.cpp.

152{
153 EXTI->INTENR &= ~static_cast<uint32_t>(pin_);
154 return ErrorCode::OK;
155}

◆ 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 145 of file ch32_gpio.cpp.

146{
147 EXTI->INTENR |= static_cast<uint32_t>(pin_);
148 return ErrorCode::OK;
149}

◆ GetEXTIID()

uint8_t CH32GPIO::GetEXTIID ( uint16_t pin)
staticprivate

Definition at line 254 of file ch32_gpio.cpp.

255{
256 ASSERT(pin != 0 && (pin & static_cast<uint16_t>(pin - 1)) == 0);
257 return __builtin_ctz(pin);
258}

◆ OnInterrupt()

void CH32GPIO::OnInterrupt ( )

Definition at line 157 of file ch32_gpio.cpp.

158{
159 if (!callback_.Empty())
160 {
161 callback_.Run(true);
162 }
163}
void Run(bool in_isr, PassArgs &&... args) const
执行回调函数并传递参数 / Execute the callback with arguments
Definition libxr_cb.hpp:225
bool Empty() const
检查回调是否为空 / Check whether the callback is empty
Definition libxr_cb.hpp:236
Callback callback_
GPIO 事件的回调函数。Callback function for GPIO events.
Definition gpio.hpp:56

◆ Read()

bool LibXR::CH32GPIO::Read ( )
inlineoverridevirtual

读取 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 58 of file ch32_gpio.hpp.

58{ return (port_->INDR & pin_) != (uint32_t)Bit_RESET; }

◆ SetConfig()

ErrorCode LibXR::CH32GPIO::SetConfig ( Configuration config)
inlineoverridevirtual

配置 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 76 of file ch32_gpio.hpp.

77 {
78 GPIO_InitTypeDef gpio_init = {};
79 gpio_init.GPIO_Pin = pin_;
80 gpio_init.GPIO_Speed = GPIO_Speed_50MHz;
81
82 switch (config.direction)
83 {
88 gpio_init.GPIO_Mode = (config.pull == Pull::UP) ? GPIO_Mode_IPU
89 : (config.pull == Pull::DOWN) ? GPIO_Mode_IPD
90 : GPIO_Mode_IN_FLOATING;
91 break;
92
94 gpio_init.GPIO_Mode = GPIO_Mode_Out_PP;
95 break;
96
98 gpio_init.GPIO_Mode = GPIO_Mode_Out_OD;
99 break;
100 }
101
102 GPIO_Init(port_, &gpio_init);
103
104 switch (config.direction)
105 {
107 ConfigureEXTI(EXTI_Trigger_Rising);
108 break;
110 ConfigureEXTI(EXTI_Trigger_Falling);
111 break;
113 ConfigureEXTI(EXTI_Trigger_Rising_Falling);
114 break;
115 default:
116 break;
117 }
118
119 return ErrorCode::OK;
120 }

◆ Write()

void LibXR::CH32GPIO::Write ( bool value)
inlineoverridevirtual

写入 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 60 of file ch32_gpio.hpp.

61 {
62 if (value)
63 {
64 port_->BSHR = pin_;
65 }
66 else
67 {
68 port_->BCR = pin_;
69 }
70 }

Field Documentation

◆ irq_

IRQn_Type LibXR::CH32GPIO::irq_
private

Definition at line 132 of file ch32_gpio.hpp.

◆ map_

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

EXTI 线路映射表 / EXTI line map.

Definition at line 127 of file ch32_gpio.hpp.

127{nullptr};

◆ pin_

uint16_t LibXR::CH32GPIO::pin_
private

Definition at line 131 of file ch32_gpio.hpp.

◆ port_

GPIO_TypeDef* LibXR::CH32GPIO::port_
private

Definition at line 130 of file ch32_gpio.hpp.


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