# 通用输入/输出

**通用输入/输出**（GPIO）是集成电路上一种未预先分配功能的数字引脚，其行为——输入还是输出——由软件在运行时控制。GPIO 引脚让[微控制器](https://www.edgepedia.org/zh/microcontroller)或片上系统得以监测开关和传感器、驱动 LED 及其他负载、产生或响应中断；厂商词汇表将其描述为一种灵活的并行接口，允许各种定制连接。<sup>[1](https://www.analog.com/en/resources/glossary/gpio.html)</sup> 微控制器厂商则称其为最简单的外设，使设备得以监测和控制其他设备。<sup>[2](https://onlinedocs.microchip.com/oxy/GUID-450989FA-38E4-4D68-AB61-15ADB29AD718-en-US-6/GUID-616E86DD-1E2E-495C-AF18-872E487A2F67_3.html)</sup>

| 关键事实 | 详情 |
|---|---|
| 定义 | 一种未预先分配功能的数字引脚，可由软件配置为输入或输出，通常按端口分组，每个端口最多八个引脚，命名为 PORTA、PORTB 等<sup>[3](https://ww1.microchip.com/downloads/en/Appnotes/Getting-Started-with-GPIO-DS90003229B.pdf)</sup> |
| 方向控制 | 方向寄存器的某一位被置位时将该引脚使能为输出，被清零时使能为输入；GPIO 必须配置为输入才能作为中断源<sup>[4](https://ww1.microchip.com/downloads/aemDocuments/documents/UNG/ApplicationNotes/ApplicationNotes/AN4783-LAN8841-Register-Definitions-DS00004783.pdf)</sup> |
| 输出类型 | 推挽输出主动驱动两个逻辑电平；开漏输出只将线路拉低，其余时候呈高阻态<sup>[4](https://ww1.microchip.com/downloads/aemDocuments/documents/UNG/ApplicationNotes/ApplicationNotes/AN4783-LAN8841-Register-Definitions-DS00004783.pdf)</sup><sup> • </sup><sup>[5](https://kernel.org/doc/html/next/driver-api/gpio/driver.html)</sup> |
| 输入模式 | Arduino 的 INPUT 模式为高阻态，等效于一个 100 兆欧的串联电阻；浮空输入需要上拉或下拉电阻才能给出确定状态<sup>[6](https://docs.arduino.cc/language-reference/en/variables/constants/inputOutputPullup/)</sup> |
| 驱动强度 | 许多控制器上可编程；TI Tiva C 的焊盘提供 2、4、8 mA 三档设置，负载超过 15 pF 或驱动 LED 时建议使用 8 mA<sup>[7](https://www.ti.com/lit/an/spma056/spma056.pdf?ts=1747757409907)</sup> |
| 中断 | 通常支持边沿或电平检测，机械开关可配置去抖<sup>[8](https://onlinedocs.microchip.com/oxy/GUID-450989FA-38E4-4D68-AB61-15ADB29AD718-en-US-6/GUID-1FAC08B3-5A8E-4952-AF45-F53740BC350D.html)</sup><sup> • </sup><sup>[5](https://kernel.org/doc/html/next/driver-api/gpio/driver.html)</sup> |
| 电压电平 | 树莓派的 GPIO 固定为 3.3 V；输出驱动 3.3 V，输入最高耐受 3.3 V<sup>[9](https://pip-assets.raspberrypi.com/categories/685-whitepapers-app-notes/documents/RP-006553-WP/A-history-of-GPIO-usage-on-Raspberry-Pi-devices-and-current-best-practices)</sup> |

## GPIO 引脚的作用

GPIO 引脚是一个单一的电气连接，其功能由软件选择。设置方向寄存器决定该引脚是向外驱动一个值还是向内采样一个值；以 Microchip LAN8841 为例，GPIO_DIR 寄存器中某一位被置位时使能对应引脚为输出，被清零时使能为输入，而 GPIO_EN 寄存器必须首先为这个共享引脚选择 GPIO 功能。<sup>[4](https://ww1.microchip.com/downloads/aemDocuments/documents/UNG/ApplicationNotes/ApplicationNotes/AN4783-LAN8841-Register-Definitions-DS00004783.pdf)</sup> [Linux 内核](https://www.edgepedia.org/zh/linux-kernel)文档以更一般的方式陈述了同样的规则：GPIO 没有默认方向，未先设置方向就使用它是非法的，会导致未定义行为。<sup>[10](https://docs.kernel.org/7.1/driver-api/gpio/consumer.html)</sup>

引脚通常聚合成端口，以便软件一次读写多条线路。在基于 Microchip AVR 的器件上，每个端口实例最多有八个 I/O 引脚，每个引脚功能——包括上拉使能、输入禁用和中断生成——都可单独配置。<sup>[3](https://ww1.microchip.com/downloads/en/Appnotes/Getting-Started-with-GPIO-DS90003229B.pdf)</sup> 库通常提供两个层次的访问方式：操作单个引脚的引脚函数和操作一组引脚的端口函数。<sup>[2](https://onlinedocs.microchip.com/oxy/GUID-450989FA-38E4-4D68-AB61-15ADB29AD718-en-US-6/GUID-616E86DD-1E2E-495C-AF18-872E487A2F67_3.html)</sup>

## 输入配置

输入引脚对所采样的电路呈现高阻抗。[Arduino](https://www.edgepedia.org/zh/arduino) 文档针对 ATmega 器件量化了这一点：INPUT 引脚对电路的要求等效于一个 100 兆欧的串联电阻，这适合读取传感器，但当所接开关断开时，引脚会浮空，因而状态不可预测，除非用上拉或下拉电阻将引脚拉到已知状态。<sup>[6](https://docs.arduino.cc/language-reference/en/variables/constants/inputOutputPullup/)</sup> 许多控制器集成了这些电阻：PIC32 的 GPIO 模块为每个输入引脚提供独立弱上拉和弱下拉使能，Linux 的 GPIO 驱动接口则将上拉或下拉使能视为标准配置选项。<sup>[2](https://onlinedocs.microchip.com/oxy/GUID-450989FA-38E4-4D68-AB61-15ADB29AD718-en-US-6/GUID-616E86DD-1E2E-495C-AF18-872E487A2F67_3.html)</sup><sup> • </sup><sup>[5](https://kernel.org/doc/html/next/driver-api/gpio/driver.html)</sup>

有些引脚带有固定偏置。在[树莓派](https://www.edgepedia.org/zh/raspberry-pi)板上，GPIO2 和 GPIO3 有固定上拉电阻，而其他引脚的上拉可在软件中配置。<sup>[11](https://www.raspberrypi.com/documentation/computers/raspberry-pi.html?source=post_page-----192f9877df3----------------------)</sup>

## 输出配置与驱动强度

输出引脚要么主动驱动两个逻辑电平（推挽），要么只拉低（开漏）。在 LAN8841 的缓冲器类型寄存器中，置位配置为推挽驱动器，清零配置为开漏驱动器。<sup>[4](https://ww1.microchip.com/downloads/aemDocuments/documents/UNG/ApplicationNotes/ApplicationNotes/AN4783-LAN8841-Register-Definitions-DS00004783.pdf)</sup> Linux 文档解释了开漏模式：线路不会被主动驱动为高，因此晶体管关断时引脚对外部电源轨呈高阻态；如果硬件不支持开漏，GPIO 库会这样模拟：输出低值时将线路拉低，输出高值时将其切换为输入。<sup>[5](https://kernel.org/doc/html/next/driver-api/gpio/driver.html)</sup><sup> • </sup><sup>[12](https://docs.kernel.org/6.12/userspace-api/gpio/gpio-v2-get-line-ioctl.html)</sup><sup> • </sup><sup>[20](https://cdn.kernel.org/doc/html/latest/driver-api/gpio/driver.html)</sup> 开漏操作也可以使用连接到独立电源的外部电阻；在以开漏模式使用的 TI 器件上，该外部电源必须限制在 5.5 V 以内。<sup>[13](https://www.ti.com/lit/an/spma053/spma053.pdf?ts=1776330288841)</sup>

**驱动强度**决定输出在保持其逻辑电平时能提供多少电流。TI Tiva C 系列的焊盘具有可编程驱动强度；驱动高速总线、电容负载大于 15 pF 或 LED 的输出建议使用 8 mA 档，更高的强度则根据数据手册中的 VOL、VOH 和单侧电流限值选取。部分 Tiva C 焊盘限制为 2 mA，只应驱动 15 pF 以下的低电容负载或能容忍较慢上升下降时间的信号；TM4C129x 器件上与 USB0DP 和 USB0DM 共用的焊盘固定为 4 mA，且不能配置为开漏。<sup>[7](https://www.ti.com/lit/an/spma056/spma056.pdf?ts=1747757409907)</sup> 驱动强度与电源设计相互影响：树莓派的 3.3 V 电源设计为每个 GPIO 引脚最大约 3 mA 电流，抽取超过所配置驱动强度的电流会使输出电压降至规定 VOH 以下。<sup>[14](https://www.raspberrypi.com/documentation/computers/raspberry-pi.html?hash=a6be966c70758bbcd9f5115c4096cf2c)</sup>

## 中断与边沿检测

大多数 GPIO 控制器能将输入变化报告为中断。PIC32 的 GPIO 模块监视选定的输入，在引脚状态变化时产生中断，支持异步上升沿和下降沿检测以及高电平或低电平中断，并在休眠和空闲模式下继续工作。<sup>[8](https://onlinedocs.microchip.com/oxy/GUID-450989FA-38E4-4D68-AB61-15ADB29AD718-en-US-6/GUID-1FAC08B3-5A8E-4952-AF45-F53740BC350D.html)</sup> 在 PIC18 上，变化中断可配置为在输入引脚的下降沿触发，同一机制可将器件从休眠模式唤醒。<sup>[15](https://www.microchip.com/en-us/application-notes/tb3284)</sup>

机械开关会抖动，在短时间内将线路反复拉高拉低，因此控制器或驱动提供去抖——一种等待线路稳定后再报告事件的定时器；硬件不提供去抖时，Linux 内核通过过滤硬件中断在软件中模拟去抖。<sup>[5](https://kernel.org/doc/html/next/driver-api/gpio/driver.html)</sup><sup> • </sup><sup>[12](https://docs.kernel.org/6.12/userspace-api/gpio/gpio-v2-get-line-ioctl.html)</sup>

## 复用与共享

现代器件上的引脚是多种功能共享的稀缺资源。部分 PIC32 引脚与备用功能复用，一般而言，服务于活动外设的引脚不能同时用作通用 I/O 引脚。<sup>[2](https://onlinedocs.microchip.com/oxy/GUID-450989FA-38E4-4D68-AB61-15ADB29AD718-en-US-6/GUID-616E86DD-1E2E-495C-AF18-872E487A2F67_3.html)</sup> 在树莓派硬件上，相同的引脚可在软件控制下分配给 SPI、I2C 或串行功能，可用的备用功能列在各处理器的数据手册中。<sup>[9](https://pip-assets.raspberrypi.com/categories/685-whitepapers-app-notes/documents/RP-006553-WP/A-history-of-GPIO-usage-on-Raspberry-Pi-devices-and-current-best-practices)</sup> Linux 内核在此划了一条定义性的界线：一条线路只有真正通用才算 GPIO，而一条可用作 GPIO 的 LED 驱动线路仍应由 GPIO 芯片驱动来处理。<sup>[16](https://docs.kernel.org/6.12/driver-api/gpio/driver.html)</sup>

## 操作系统接口

**Linux。** 内核驱动通过基于描述符的消费者接口使用 gpiod_get() 及相关调用获取 GPIO，必须在使用前设置每个引脚的方向，并且可以在原子上下文中读取或设置值，因为大多数控制器对简单的内存访问即可响应。<sup>[10](https://docs.kernel.org/7.1/driver-api/gpio/consumer.html)</sup> 对于用户空间，内核 5.10 引入了字符设备 API 的第 2 版，其中每个控制器显示为 /dev/gpiochipN，线路通过 ioctl 请求，边沿事件带有纳秒级时间戳；默认内核事件缓冲区为每条请求线路保存 16 个事件，文档建议在已有合适内核驱动时，不要从用户空间通过软件模拟信号时序来操作硬件。<sup>[17](https://cdn.kernel.org/doc/html/latest/userspace-api/gpio/chardev.html)</sup> 对于 LED（leds-gpio）和键盘输入（gpio-keys，支持去抖）等常见 GPIO 任务，内核中有标准驱动。<sup>[18](https://cdn.kernel.org/doc/html/latest/driver-api/gpio/drivers-on-gpio.html)</sup>

**SoC 平台上的 Windows。** Windows 为片上系统平台上的 GPIO 硬件定义了一个通用抽象，要求 ACPI 命名空间提供支持；GPIO 控制器为外设提供中断、输入信号和输出信号，ACPI 5.0 增加了 GeneralPurposeIO 操作区域类型，使以 ASL 编写的固件可以访问 GPIO 引脚。<sup>[19](https://learn.microsoft.com/en-us/windows-hardware/drivers/bringup/general-purpose-i-o--gpio-)</sup>

## 实际限制与保护

GPIO 引脚在器件规定的电压和电流额定值内工作。在树莓派板上，通用引脚工作在 3.3 V，输入最高耐受 3.3 V，文档警告不要将 5 V 接到 3.3 V 元件上、不要直接用引脚驱动电机、不要在 LED 上省略限流电阻。<sup>[9](https://pip-assets.raspberrypi.com/categories/685-whitepapers-app-notes/documents/RP-006553-WP/A-history-of-GPIO-usage-on-Raspberry-Pi-devices-and-current-best-practices)</sup><sup> • </sup><sup>[11](https://www.raspberrypi.com/documentation/computers/raspberry-pi.html?source=post_page-----192f9877df3----------------------)</sup> 输入电压阈值有具体数值规定；对于 BCM2835 系列和基于 RP3A0 的产品，输入低电平电压最大为 0.9 V，输入高电平电压最小为 1.6 V，85 摄氏度时输入漏电流规定最高为 5 微安。<sup>[11](https://www.raspberrypi.com/documentation/computers/raspberry-pi.html?source=post_page-----192f9877df3----------------------)</sup> 集成钳位和保护电路在输入模式下将施加到 GPIO 引脚的电压转换为可接受的内部电平，在输出模式下将输出电压限制在 0 V 与 VDD 之间，串联端接电阻还可额外保护输入和输出引脚免受静电放电冲击。<sup>[13](https://www.ti.com/lit/an/spma053/spma053.pdf?ts=1776330288841)</sup><sup> • </sup><sup>[7](https://www.ti.com/lit/an/spma056/spma056.pdf?ts=1747757409907)</sup>

功耗行为也可配置。Microchip 建议禁用未使用引脚、用作模拟输入的引脚和用作输出的引脚的输入，以获得最佳功耗，PIC32 的 GPIO 模块支持在休眠和空闲模式下工作。<sup>[3](https://ww1.microchip.com/downloads/en/Appnotes/Getting-Started-with-GPIO-DS90003229B.pdf)</sup><sup> • </sup><sup>[2](https://onlinedocs.microchip.com/oxy/GUID-450989FA-38E4-4D68-AB61-15ADB29AD718-en-US-6/GUID-616E86DD-1E2E-495C-AF18-872E487A2F67_3.html)</sup>

## References

1. [GPIO, Analog Devices glossary](https://www.analog.com/en/resources/glossary/gpio.html)
2. [3.22.9 General Purpose I/O (GPIO), PIC32MX documentation, Microchip](https://onlinedocs.microchip.com/oxy/GUID-450989FA-38E4-4D68-AB61-15ADB29AD718-en-US-6/GUID-616E86DD-1E2E-495C-AF18-872E487A2F67_3.html)
3. [TB3229: Getting Started with GPIO, Microchip](https://ww1.microchip.com/downloads/en/Appnotes/Getting-Started-with-GPIO-DS90003229B.pdf)
4. [AN4783 LAN8841 Register Definitions, Microchip](https://ww1.microchip.com/downloads/aemDocuments/documents/UNG/ApplicationNotes/ApplicationNotes/AN4783-LAN8841-Register-Definitions-DS00004783.pdf)
5. [GPIO Driver Interface, Linux Kernel documentation](https://kernel.org/doc/html/next/driver-api/gpio/driver.html)
6. [INPUT | INPUT_PULLUP | OUTPUT, Arduino Documentation](https://docs.arduino.cc/language-reference/en/variables/constants/inputOutputPullup/)
7. [SPMA056: General Design Information for TM4C129x microcontrollers, Texas Instruments](https://www.ti.com/lit/an/spma056/spma056.pdf?ts=1747757409907)
8. [3.1.4 General Purpose I/O (GPIO), Microchip](https://onlinedocs.microchip.com/oxy/GUID-450989FA-38E4-4D68-AB61-15ADB29AD718-en-US-6/GUID-1FAC08B3-5A8E-4952-AF45-F53740BC350D.html)
9. [RP-006553-WP: A history of GPIO usage on Raspberry Pi devices and current best practices](https://pip-assets.raspberrypi.com/categories/685-whitepapers-app-notes/documents/RP-006553-WP/A-history-of-GPIO-usage-on-Raspberry-Pi-devices-and-current-best-practices)
10. [GPIO Descriptor Consumer Interface, Linux Kernel documentation](https://docs.kernel.org/7.1/driver-api/gpio/consumer.html)
11. [Raspberry Pi computer hardware, Raspberry Pi Documentation](https://www.raspberrypi.com/documentation/computers/raspberry-pi.html?source=post_page-----192f9877df3----------------------)
12. [GPIO_V2_GET_LINE_IOCTL, Linux Kernel documentation](https://docs.kernel.org/6.12/userspace-api/gpio/gpio-v2-get-line-ioctl.html)
13. [SPMA053: Use Conditions for GPIO, Texas Instruments](https://www.ti.com/lit/an/spma053/spma053.pdf?ts=1776330288841)
14. [Raspberry Pi computer hardware (voltage and drive strength), Raspberry Pi Documentation](https://www.raspberrypi.com/documentation/computers/raspberry-pi.html?hash=a6be966c70758bbcd9f5115c4096cf2c)
15. [TB3284: Getting Started with GPIO on PIC18, Microchip](https://www.microchip.com/en-us/application-notes/tb3284)
16. [GPIO Driver Interface (internal representation), Linux Kernel documentation](https://docs.kernel.org/6.12/driver-api/gpio/driver.html)
17. [GPIO Character Device Userspace API, Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/userspace-api/gpio/chardev.html)
18. [Subsystem drivers using GPIO, Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/driver-api/gpio/drivers-on-gpio.html)
19. [General-Purpose I/O (GPIO), Windows drivers, Microsoft Learn](https://learn.microsoft.com/en-us/windows-hardware/drivers/bringup/general-purpose-i-o--gpio-)
20. [GPIO Driver Interface, The Linux Kernel documentation](https://cdn.kernel.org/doc/html/latest/driver-api/gpio/driver.html)

---
*Initially written Sep 17, 2026 · Reviewed: Oct 15, 2026 · Edited: Oct 15, 2026; Oct 25, 2026 · Last review: Oct 15, 2026*

Topic: 技术与人造世界 › 计算技术与数字系统 › 计算机硬件 › Boards, peripherals & form factors › Peripherals & expansion hardware › Peripheral controller chips

Copyright 2026 Edgepedia

License: Edgepedia Community License 1.0, https://www.edgepedia.org/edgepedia/license

Source: https://www.edgepedia.org/zh/general-purpose-input-output

English original: https://www.edgepedia.org/general-purpose-input-output
