|
libxr
1.0
Want to be the best embedded framework
|
HPM 平台 GPIO 驱动实现 / GPIO driver implementation for HPM platform. More...
#include <hpm_gpio.hpp>
Data Structures | |
| struct | PortIrqRouteState |
| 端口级 IRQ 路由状态 / Shared port-level IRQ routing state. More... | |
Public Member Functions | |
| HPMGPIO (GPIO_Type *gpio, uint32_t port, uint8_t pin, uint32_t irq=INVALID_IRQ, uint16_t pad_index=INVALID_PAD_INDEX) | |
| 构造 HPM GPIO 对象 / Construct an HPM GPIO object. | |
| bool | Read () override |
| 读取引脚电平 / Read current pin level. | |
| void | Write (bool value) override |
| 写引脚电平 / Write output pin level. | |
| ErrorCode | EnableInterrupt () override |
| 使能当前引脚中断 / Enable GPIO interrupt for current pin. | |
| ErrorCode | DisableInterrupt () override |
| 失能当前引脚中断 / Disable GPIO interrupt for current pin. | |
| ErrorCode | SetConfig (Configuration config) override |
| 配置当前引脚模式 / Configure GPIO mode for current pin. | |
| ErrorCode | SetAnalogHighImpedance () |
| 将当前 PAD 配置为模拟高阻 / Configure current pad to analog high-impedance. | |
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 Member Functions | |
| static void | CheckInterrupt (uint32_t port) |
| 分发某一端口的 GPIO 中断回调 / Dispatch GPIO interrupt callbacks for one port. | |
Static Private Member Functions | |
| static uint16_t | ResolvePadIndex (GPIO_Type *gpio, uint32_t port, uint8_t pin) |
| 根据控制器与端口引脚推导 IOC PAD 编号 / Resolve IOC PAD index from controller/port/pin tuple. | |
Private Attributes | |
| GPIO_Type * | gpio_ |
| GPIO 控制器实例 / GPIO controller instance. | |
| uint32_t | port_ |
| GPIO 端口号 / GPIO port index. | |
| uint8_t | pin_ |
| GPIO 引脚号 / GPIO pin index. | |
| uint32_t | irq_ |
| 当前 port 对应 IRQ 号 / IRQ number for the current port. | |
| uint16_t | pad_index_ |
| IOC PAD 编号 / IOC PAD index. | |
| bool | interrupt_enabled_ = false |
| 当前 pin IRQ 使能状态 / Per-instance IRQ enabled flag. | |
Static Private Attributes | |
| static constexpr uint32_t | PORT_COUNT = 15u |
| 支持的端口数量 / Supported port count. | |
| static constexpr uint32_t | PIN_COUNT = 32 |
| 每个端口引脚数 / Pins per port. | |
| static constexpr uint32_t | INVALID_IRQ = 0xFFFFFFFFu |
| 无效 IRQ 标记 / Invalid IRQ marker. | |
| static constexpr uint16_t | INVALID_PAD_INDEX = 0xFFFFu |
| 无效 PAD 标记 / Invalid PAD marker. | |
| static HPMGPIO * | map [PORT_COUNT][PIN_COUNT] = {} |
| GPIO 对象映射表 / GPIO object dispatch map. | |
| static GPIO_Type * | port_controller_map [PORT_COUNT] = {} |
| static PortIrqRouteState | port_irq_route_map [PORT_COUNT] = {} |
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. | |
HPM 平台 GPIO 驱动实现 / GPIO driver implementation for HPM platform.
该类将 LibXR GPIO 抽象接口适配到 HPM SDK GPIO API。Read() 和 Write() 分别调用 gpio_read_pin() 与 gpio_write_pin();SetConfig() 调用 gpio_set_pin_input()、gpio_set_pin_output() 和 gpio_config_pin_interrupt(),并在可解析 PAD 时更新 IOC FUNC_CTL、 PAD_CTL 的 GPIO 复用、loopback、上下拉和开漏位。EnableInterrupt() 与 DisableInterrupt() 只支持普通 HPM_GPIO0 中断路径;完整 SDK GPIO 示例说明 FGPIO 不支持中断,因此 HPM_FGPIO 中断请求返回 ErrorCode::NOT_SUPPORT,并且 不会注册到中断分发表。
This class adapts the LibXR GPIO abstraction to HPM SDK GPIO APIs. Read() and Write() call gpio_read_pin() and gpio_write_pin() respectively. SetConfig() calls gpio_set_pin_input(), gpio_set_pin_output(), and gpio_config_pin_interrupt(), and updates IOC FUNC_CTL / PAD_CTL GPIO mux, loopback, pull, and open-drain bits when the PAD can be resolved. EnableInterrupt() and DisableInterrupt() support only the normal HPM_GPIO0 IRQ path; the full SDK GPIO sample states that FGPIO does not support interrupts, so interrupt requests on HPM_FGPIO return ErrorCode::NOT_SUPPORT and are not registered in the interrupt dispatch map.
中断回调由 CheckInterrupt() 统一分发。 Interrupt callbacks are dispatched through CheckInterrupt().
Definition at line 59 of file hpm_gpio.hpp.
| HPMGPIO::HPMGPIO | ( | GPIO_Type * | gpio, |
| uint32_t | port, | ||
| uint8_t | pin, | ||
| uint32_t | irq = INVALID_IRQ, | ||
| uint16_t | pad_index = INVALID_PAD_INDEX ) |
构造 HPM GPIO 对象 / Construct an HPM GPIO object.
构造 HPM GPIO 对象并注册到中断映射表 / Construct HPM GPIO object and register into interrupt map.
| gpio | GPIO 控制器基地址 / GPIO controller base address. |
| port | GPIO 端口号 / GPIO port index. |
| pin | GPIO 引脚号 / GPIO pin index. |
| irq | 当前 port 对应的 PLIC IRQ 号;没有中断需求时可保持默认值 / PLIC IRQ number for the current port. Keep the default when interrupt support is not needed. |
| pad_index | IOC PAD 编号,默认值表示自动由 (gpio, port, pin) 推导;无法 推导时仍可读写 GPIO,但 PAD 复用和模拟高阻配置会受限 / IOC PAD index. The default auto-resolves it from (gpio, port, pin). If it cannot be resolved, GPIO read/write can still work, while PAD mux and analog high-impedance configuration are limited. |
Definition at line 97 of file hpm_gpio.cpp.
|
static |
分发某一端口的 GPIO 中断回调 / Dispatch GPIO interrupt callbacks for one port.
扫描并分发指定端口的 GPIO 中断 / Scan and dispatch GPIO IRQ callbacks for one port.
该函数应在板级 GPIO IRQ Handler 中调用。 This function should be called by board IRQ handlers.
同时会清除触发引脚的中断标志位。 It also clears interrupt flags for triggered pins.
建议在板级 IRQHandler 中仅调用一次该函数:
Definition at line 393 of file hpm_gpio.cpp.
|
overridevirtual |
失能当前引脚中断 / Disable GPIO interrupt for current pin.
失能当前引脚中断 / Disable interrupt for current pin.
ErrorCode::OK;IRQ/端口参数无效返回 ErrorCode::ARG_ERR; HPM_FGPIO 返回 ErrorCode::NOT_SUPPORT;端口路由状态异常返回 ErrorCode::STATE_ERR / Returns ErrorCode::OK on success, ErrorCode::ARG_ERR for invalid irq/port/pin, ErrorCode::NOT_SUPPORT for HPM_FGPIO, and ErrorCode::STATE_ERR when shared port-route state is inconsistent.gpio_disable_pin_interrupt() 只清当前 pin 的 GPIO IE 位; 共享 PLIC 路由只在同 port 最后一个已启用 pin disable 后关闭 / HPM SDK gpio_disable_pin_interrupt() only clears the current pin's GPIO IE bit; the shared PLIC route is closed only after the last enabled pin on the same port is disabled.先关闭当前 pin 的 GPIO 事件; 仅当该 port 最后一个已使能 pin 被释放时,才关闭共享的 PLIC 路由。 FGPIO 没有 GPIO 中断能力,按完整 SDK GPIO 示例的约束返回 NOT_SUPPORT。 Disable the current pin's GPIO event first; the shared PLIC route is disabled only after the last enabled pin on the same port is released. FGPIO has no GPIO interrupt capability and returns NOT_SUPPORT according to the full SDK GPIO sample constraint.
Implements LibXR::GPIO.
Definition at line 178 of file hpm_gpio.cpp.
|
overridevirtual |
使能当前引脚中断 / Enable GPIO interrupt for current pin.
使能当前引脚中断 / Enable interrupt for current pin.
ErrorCode::OK;IRQ/端口参数无效返回 ErrorCode::ARG_ERR; HPM_FGPIO 返回 ErrorCode::NOT_SUPPORT;同 port 存在冲突 IRQ 路由返回 ErrorCode::STATE_ERR / Returns ErrorCode::OK on success, ErrorCode::ARG_ERR for invalid irq/port/pin, ErrorCode::NOT_SUPPORT for HPM_FGPIO, and ErrorCode::STATE_ERR when the same port is already routed to another IRQ.gpio_enable_pin_interrupt() 只设置 GPIO IE 位;PLIC 路由由 intc_m_enable_irq_with_priority() 打开,且同一 port 只在首个 pin enable 时调用 / HPM SDK gpio_enable_pin_interrupt() only sets the GPIO IE bit; PLIC routing is opened through intc_m_enable_irq_with_priority() only when the first pin on the same port is enabled.对于共享同一 port IRQ 的多个 pin:
gpio_enable_pin_interrupt() 始终按 pin 开事件;gpio_enable_pin_interrupt() always operates per pin;Implements LibXR::GPIO.
Definition at line 129 of file hpm_gpio.cpp.
|
inlineoverridevirtual |
读取引脚电平 / Read current pin level.
true 高电平,false 低电平 / true for high level, false for low level. Implements LibXR::GPIO.
Definition at line 83 of file hpm_gpio.hpp.
|
staticprivate |
根据控制器与端口引脚推导 IOC PAD 编号 / Resolve IOC PAD index from controller/port/pin tuple.
推导指定 GPIO 对应的 IOC PAD 编号 / Resolve IOC PAD index for a GPIO pin.
| gpio | GPIO 控制器基地址 / GPIO controller base address. |
| port | GPIO 端口号 / GPIO port index. |
| pin | GPIO 引脚号 / GPIO pin index. |
INVALID_PAD_INDEX / Resolved PAD index, or INVALID_PAD_INDEX if unavailable. 端口-引脚到对象实例的静态映射,用于中断分发 / Static port-pin to object map for interrupt dispatch. Definition at line 430 of file hpm_gpio.cpp.
| ErrorCode HPMGPIO::SetAnalogHighImpedance | ( | ) |
将当前 PAD 配置为模拟高阻 / Configure current pad to analog high-impedance.
将引脚切换为模拟高阻 / Put current pin into analog high-impedance mode.
ErrorCode::OK;port/pin 无效返回 ErrorCode::ARG_ERR; PAD 不可解析返回 ErrorCode::NOT_SUPPORT / Returns ErrorCode::OK on success, ErrorCode::ARG_ERR for invalid port/pin, or ErrorCode::NOT_SUPPORT when PAD is unavailable.FUNC_CTL 的 ANALOG 位并清除上下拉和开漏位,未上板前 仍需用实际电气连接确认高阻效果 / This API writes the IOC FUNC_CTL ANALOG bit and clears pull/open-drain bits. The high-impedance effect still needs board-level electrical verification. EnableInterrupt() 进入共享 port IRQ 路由,本接口会先 走 DisableInterrupt() 维护引用计数;否则只清当前 pin 的 GPIO IE 位 / If the pin is already routed through EnableInterrupt(), this API first calls DisableInterrupt() to keep the shared port IRQ reference count consistent; otherwise it only clears the current pin's GPIO IE bit.用途:
Definition at line 343 of file hpm_gpio.cpp.
|
overridevirtual |
配置当前引脚模式 / Configure GPIO mode for current pin.
配置 GPIO 方向、中断模式与上下拉 / Configure GPIO direction, interrupt mode, and pull.
ErrorCode::OK;port/pin 或枚举值无效返回 ErrorCode::ARG_ERR; HPM_FGPIO 的中断方向或 SDK 未提供双沿枚举时返回 ErrorCode::NOT_SUPPORT / Returns ErrorCode::OK on success, ErrorCode::ARG_ERR for invalid port/pin or enum values, or ErrorCode::NOT_SUPPORT for interrupt directions on HPM_FGPIO or when the SDK does not provide the both-edge enum. Read() 读取实际引脚电平 / When PAD can be resolved, IOC loopback is enabled so Read() can return the actual pad level even in output mode.与 STM32 HAL 的常见用法对齐:
Direction 对应 GPIO 模式(输入/输出/中断边沿)Pull 对应上拉/下拉配置 Aligned with common STM32 HAL usage:Direction maps to GPIO mode (input/output/IRQ edge)Pull maps to pull-up / pull-down configuration Implements LibXR::GPIO.
Definition at line 234 of file hpm_gpio.cpp.
|
inlineoverridevirtual |
写引脚电平 / Write output pin level.
| value | true 高电平,false 低电平 / true for high level, false for low level. |
Implements LibXR::GPIO.
Definition at line 94 of file hpm_gpio.hpp.
|
private |
GPIO 控制器实例 / GPIO controller instance.
Definition at line 241 of file hpm_gpio.hpp.
|
private |
当前 pin IRQ 使能状态 / Per-instance IRQ enabled flag.
Definition at line 246 of file hpm_gpio.hpp.
|
staticconstexprprivate |
无效 IRQ 标记 / Invalid IRQ marker.
Definition at line 206 of file hpm_gpio.hpp.
|
staticconstexprprivate |
无效 PAD 标记 / Invalid PAD marker.
Definition at line 207 of file hpm_gpio.hpp.
|
private |
当前 port 对应 IRQ 号 / IRQ number for the current port.
Definition at line 244 of file hpm_gpio.hpp.
|
staticprivate |
GPIO 对象映射表 / GPIO object dispatch map.
按端口和引脚保存对象指针,供中断入口快速找到具体实例并触发回调。 Stores instance pointers by port/pin so IRQ entry can dispatch callbacks quickly.
Definition at line 237 of file hpm_gpio.hpp.
|
private |
IOC PAD 编号 / IOC PAD index.
Definition at line 245 of file hpm_gpio.hpp.
|
private |
Definition at line 243 of file hpm_gpio.hpp.
|
staticconstexprprivate |
每个端口引脚数 / Pins per port.
Definition at line 205 of file hpm_gpio.hpp.
|
private |
Definition at line 242 of file hpm_gpio.hpp.
|
staticprivate |
Definition at line 238 of file hpm_gpio.hpp.
|
staticconstexprprivate |
支持的端口数量 / Supported port count.
Definition at line 203 of file hpm_gpio.hpp.
|
staticprivate |
Definition at line 239 of file hpm_gpio.hpp.