libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
ch32_gpio.hpp
1#pragma once
2
3#include "gpio.hpp"
4#include "libxr.hpp"
5
6#include DEF2STR(LIBXR_CH32_CONFIG_FILE)
7
8namespace LibXR
9{
10
11typedef enum
12{
13#if defined(GPIOA)
14 CH32_GPIOA,
15#endif
16#if defined(GPIOB)
17 CH32_GPIOB,
18#endif
19#if defined(GPIOC)
20 CH32_GPIOC,
21#endif
22#if defined(GPIOD)
23 CH32_GPIOD,
24#endif
25#if defined(GPIOE)
26 CH32_GPIOE,
27#endif
28#if defined(GPIOF)
29 CH32_GPIOF,
30#endif
31#if defined(GPIOG)
32 CH32_GPIOG,
33#endif
34#if defined(GPIOH)
35 CH32_GPIOH,
36#endif
37#if defined(GPIOI)
38 CH32_GPIOI,
39#endif
40 CH32_GPIO_NUMBER
41} ch32_gpio_group_t;
42
43uint32_t ch32_get_gpio_periph(GPIO_TypeDef* port);
44
48class CH32GPIO final : public GPIO
49{
50 public:
54 CH32GPIO(GPIO_TypeDef* port, uint16_t pin,
56 GPIO::Pull pull = GPIO::Pull::NONE, IRQn_Type irq = NonMaskableInt_IRQn);
57
58 inline bool Read() override { return (port_->INDR & pin_) != (uint32_t)Bit_RESET; }
59
60 inline void Write(bool value) override
61 {
62 if (value)
63 {
64 port_->BSHR = pin_;
65 }
66 else
67 {
68 port_->BCR = pin_;
69 }
70 }
71
72 ErrorCode EnableInterrupt() override;
73
74 ErrorCode DisableInterrupt() override;
75
76 inline ErrorCode SetConfig(Configuration config) override
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 }
121
122 void OnInterrupt();
123
124 static void CheckInterrupt(uint32_t line);
125
127 static inline CH32GPIO* map_[16] = {nullptr};
128
129 private:
130 GPIO_TypeDef* port_;
131 uint16_t pin_;
132 IRQn_Type irq_;
133
134 void ConfigureEXTI(EXTITrigger_TypeDef trigger);
135
136 static uint8_t GetEXTIID(uint16_t pin);
137};
138
139} // namespace LibXR
CH32 GPIO 驱动实现 / CH32 GPIO driver implementation.
Definition ch32_gpio.hpp:49
ErrorCode SetConfig(Configuration config) override
配置 GPIO 引脚参数。Configures the GPIO pin settings.
Definition ch32_gpio.hpp:76
void Write(bool value) override
写入 GPIO 引脚状态。Writes the GPIO pin state.
Definition ch32_gpio.hpp:60
ErrorCode DisableInterrupt() override
禁用 GPIO 引脚中断。Disables the GPIO pin interrupt.
bool Read() override
读取 GPIO 引脚状态。Reads the GPIO pin state.
Definition ch32_gpio.hpp:58
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 ch32_gpio.cpp:94
ErrorCode EnableInterrupt() override
使能 GPIO 引脚中断。Enables the GPIO pin interrupt.
static CH32GPIO * map_[16]
EXTI 线路映射表 / EXTI line map.
通用输入输出(GPIO)接口类。General Purpose Input/Output (GPIO) interface class.
Definition gpio.hpp:13
Direction
定义 GPIO 引脚的方向类型。Defines the direction types for GPIO pins.
Definition gpio.hpp:20
@ 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
@ NONE
无上拉或下拉。No pull-up or pull-down.
@ DOWN
下拉模式。Pull-down mode.
@ UP
上拉模式。Pull-up mode.
LibXR 命名空间
Definition ch32_can.hpp:14
存储 GPIO 配置参数的结构体。Structure storing GPIO configuration parameters.
Definition gpio.hpp:46
Pull pull
GPIO 上拉/下拉配置。GPIO pull-up/pull-down configuration.
Definition gpio.hpp:48
Direction direction
GPIO 引脚方向。GPIO pin direction.
Definition gpio.hpp:47